Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (hostname !== '') {
// If hostname is set, then we have a UNC path
// Pass the hostname through domainToUnicode just in case
// it is an IDN using punycode encoding. We do not need to worry
// about percent encoding because the URL parser will have
// already taken care of that for us. Note that this only
// causes IDNs with an appropriate `xn--` prefix to be decoded.
return `\\\\${domainToUnicode(hostname)}${pathname}`;
}
// Otherwise, it's a local path that requires a drive letter
const letter = pathname.codePointAt(1) | 0x20;
const sep = pathname[2];
if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || // a..z A..Z
(sep !== ':')) {
throw new RuntimeError('file URL must be absolute');
}
return pathname.slice(1);
}
function getPathFromURLWin32(url) {
const hostname = url.hostname;
let pathname = url.pathname;
for (let n = 0; n < pathname.length; n++) {
if (pathname[n] === '%') {
const third = pathname.codePointAt(n + 2) | 0x20;
if ((pathname[n + 1] === '2' && third === 102) || // 2f 2F /
(pathname[n + 1] === '5' && third === 99)) { // 5c 5C \
throw new RuntimeError('file URL must not include encoded \\ or / characters');
}
}
}
pathname = pathname.replace(forwardSlashRegEx, '\\');
pathname = decodeURIComponent(pathname);
if (hostname !== '') {
// If hostname is set, then we have a UNC path
// Pass the hostname through domainToUnicode just in case
// it is an IDN using punycode encoding. We do not need to worry
// about percent encoding because the URL parser will have
// already taken care of that for us. Note that this only
// causes IDNs with an appropriate `xn--` prefix to be decoded.
return `\\\\${domainToUnicode(hostname)}${pathname}`;
}
// Otherwise, it's a local path that requires a drive letter
it('throws an error if version is not 1 or 2', () => {
const glb = Buffer.alloc(20);
glb.write('glTF', 0);
glb.writeUInt32LE(3, 4);
let thrownError;
try {
parseGlb(glb);
} catch (e) {
thrownError = e;
}
expect(thrownError).toEqual(new RuntimeError('Binary glTF version is not 1 or 2'));
});
it('throws an error if content format is not JSON', () => {
const glb = Buffer.alloc(20);
glb.write('glTF', 0);
glb.writeUInt32LE(1, 4);
glb.writeUInt32LE(20, 8);
glb.writeUInt32LE(0, 12);
glb.writeUInt32LE(1, 16);
let thrownError;
try {
parseGlb(glb);
} catch (e) {
thrownError = e;
}
expect(thrownError).toEqual(new RuntimeError('Binary glTF scene format is not JSON'));
});
function getPathFromURLPosix(url) {
if (url.hostname !== '') {
throw new RuntimeError('Invalid platform');
}
const pathname = url.pathname;
for (let n = 0; n < pathname.length; n++) {
if (pathname[n] === '%') {
const third = pathname.codePointAt(n + 2) | 0x20;
if (pathname[n + 1] === '2' && third === 102) {
throw new RuntimeError('file URL must not include encoded \\ or / characters');
}
}
}
return decodeURIComponent(pathname);
}
function getPathFromURLPosix(url) {
if (url.hostname !== '') {
throw new RuntimeError('Invalid platform');
}
const pathname = url.pathname;
for (let n = 0; n < pathname.length; n++) {
if (pathname[n] === '%') {
const third = pathname.codePointAt(n + 2) | 0x20;
if (pathname[n + 1] === '2' && third === 102) {
throw new RuntimeError('file URL must not include encoded \\ or / characters');
}
}
}
return decodeURIComponent(pathname);
}
function fileURLToPath(path) {
Check.defined('path', path);
if (typeof path === 'string') {
path = new URL(path);
}
if (path.protocol !== 'file:') {
throw new RuntimeError('Expected path.protocol to start with file:');
}
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
}