Garrett Johnson 5 éve
szülő
commit
df5a952319
3 módosított fájl, 80 hozzáadás és 1 törlés
  1. 2 1
      src/base/TilesRendererBase.js
  2. 33 0
      src/utilities/urlJoin.js
  3. 45 0
      test/urlJoin.test.js

+ 2 - 1
src/base/TilesRendererBase.js

@@ -1,4 +1,5 @@
 import path from 'path';
+import { urlJoin } from '../utilities/urlJoin.js';
 import { LRUCache } from '../utilities/LRUCache.js';
 import { PriorityQueue } from '../utilities/PriorityQueue.js';
 import { determineFrustumSet, toggleTiles, skipTraversal, markUsedSetLeaves, traverseSet } from './traverseFunctions.js';
@@ -142,7 +143,7 @@ export class TilesRendererBase {
 
 			if ( tile.content.uri ) {
 
-				tile.content.uri = path.join( tileSetDir, tile.content.uri );
+				tile.content.uri = urlJoin( tileSetDir, tile.content.uri );
 
 			}
 

+ 33 - 0
src/utilities/urlJoin.js

@@ -0,0 +1,33 @@
+import path from 'path';
+
+// Function that properly handles path resolution for parts that have
+// a protocol component like "http://".
+export function urlJoin( ...args ) {
+
+	const protocolRegex = /^[a-zA-Z]+:\/\//;
+	let lastRoot = - 1;
+	for ( let i = 0, l = args.length; i < l; i ++ ) {
+
+		if ( protocolRegex.test( args[ i ] ) ) {
+
+			lastRoot = i;
+
+		}
+
+	}
+
+	if ( lastRoot === - 1 ) {
+
+		return path.join( ...args );
+
+	} else {
+
+		const parts = lastRoot <= 0 ? args : args.slice( lastRoot );
+		const protocol = parts[ 0 ].match( protocolRegex )[ 0 ].replace( /\//g, '\\' );
+		parts[ 0 ] = parts[ 0 ].substring( protocol.length );
+
+		return protocol + path.join( ...parts );
+
+	}
+
+}

+ 45 - 0
test/urlJoin.test.js

@@ -0,0 +1,45 @@
+import { urlJoin } from '../src/utilities/urlJoin.js';
+
+describe( 'urlJoin', () => {
+
+	it( 'should behave like path.join if no protocol is present', () => {
+
+		expect(
+			urlJoin( 'path', 'to', 'file.json' )
+		).toBe( 'path\\to\\file.json' );
+
+		expect(
+			urlJoin( 'path//', 'to\\other\\', 'file.json' )
+		).toBe( 'path\\to\\other\\file.json' );
+
+		expect(
+			urlJoin( '//path', 'to', 'file.json' )
+		).toBe( '\\\\path\\to\\file.json' );
+
+	} );
+
+	it( 'should handle protocols correctly.', () => {
+
+		expect(
+			urlJoin( 'http://path', 'to', 'file.json' )
+		).toBe( 'http:\\\\path\\to\\file.json' );
+
+		expect(
+			urlJoin( 'http://path', 'http://path2', 'to', 'file.json' )
+		).toBe( 'http:\\\\path2\\to\\file.json' );
+
+		expect(
+			urlJoin( 'https://path', 'to', 'file.json' )
+		).toBe( 'https:\\\\path\\to\\file.json' );
+
+		expect(
+			urlJoin( 'ftp://path', 'to', 'file.json' )
+		).toBe( 'ftp:\\\\path\\to\\file.json' );
+
+		expect(
+			urlJoin( 'ftp://http://path', 'to', 'file.json' )
+		).toBe( 'ftp:\\\\http:\\path\\to\\file.json' );
+
+	} );
+
+} );