1'use strict'; 2const { isWindows } = require('../common'); 3const assert = require('assert'); 4const url = require('url'); 5 6function testInvalidArgs(...args) { 7 for (const arg of args) { 8 assert.throws(() => url.fileURLToPath(arg), { 9 code: 'ERR_INVALID_ARG_TYPE' 10 }); 11 } 12} 13 14// Input must be string or URL 15testInvalidArgs(null, undefined, 1, {}, true); 16 17// Input must be a file URL 18assert.throws(() => url.fileURLToPath('https://a/b/c'), { 19 code: 'ERR_INVALID_URL_SCHEME' 20}); 21 22{ 23 const withHost = new URL('file://host/a'); 24 25 if (isWindows) { 26 assert.strictEqual(url.fileURLToPath(withHost), '\\\\host\\a'); 27 } else { 28 assert.throws(() => url.fileURLToPath(withHost), { 29 code: 'ERR_INVALID_FILE_URL_HOST' 30 }); 31 } 32} 33 34{ 35 if (isWindows) { 36 assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), { 37 code: 'ERR_INVALID_FILE_URL_PATH' 38 }); 39 assert.throws(() => url.fileURLToPath('file:///C:/a%5C/'), { 40 code: 'ERR_INVALID_FILE_URL_PATH' 41 }); 42 assert.throws(() => url.fileURLToPath('file:///?:/'), { 43 code: 'ERR_INVALID_FILE_URL_PATH' 44 }); 45 } else { 46 assert.throws(() => url.fileURLToPath('file:///a%2F/'), { 47 code: 'ERR_INVALID_FILE_URL_PATH' 48 }); 49 } 50} 51 52{ 53 let testCases; 54 if (isWindows) { 55 testCases = [ 56 // Lowercase ascii alpha 57 { path: 'C:\\foo', fileURL: 'file:///C:/foo' }, 58 // Uppercase ascii alpha 59 { path: 'C:\\FOO', fileURL: 'file:///C:/FOO' }, 60 // dir 61 { path: 'C:\\dir\\foo', fileURL: 'file:///C:/dir/foo' }, 62 // trailing separator 63 { path: 'C:\\dir\\', fileURL: 'file:///C:/dir/' }, 64 // dot 65 { path: 'C:\\foo.mjs', fileURL: 'file:///C:/foo.mjs' }, 66 // space 67 { path: 'C:\\foo bar', fileURL: 'file:///C:/foo%20bar' }, 68 // question mark 69 { path: 'C:\\foo?bar', fileURL: 'file:///C:/foo%3Fbar' }, 70 // number sign 71 { path: 'C:\\foo#bar', fileURL: 'file:///C:/foo%23bar' }, 72 // ampersand 73 { path: 'C:\\foo&bar', fileURL: 'file:///C:/foo&bar' }, 74 // equals 75 { path: 'C:\\foo=bar', fileURL: 'file:///C:/foo=bar' }, 76 // colon 77 { path: 'C:\\foo:bar', fileURL: 'file:///C:/foo:bar' }, 78 // semicolon 79 { path: 'C:\\foo;bar', fileURL: 'file:///C:/foo;bar' }, 80 // percent 81 { path: 'C:\\foo%bar', fileURL: 'file:///C:/foo%25bar' }, 82 // backslash 83 { path: 'C:\\foo\\bar', fileURL: 'file:///C:/foo/bar' }, 84 // backspace 85 { path: 'C:\\foo\bbar', fileURL: 'file:///C:/foo%08bar' }, 86 // tab 87 { path: 'C:\\foo\tbar', fileURL: 'file:///C:/foo%09bar' }, 88 // newline 89 { path: 'C:\\foo\nbar', fileURL: 'file:///C:/foo%0Abar' }, 90 // carriage return 91 { path: 'C:\\foo\rbar', fileURL: 'file:///C:/foo%0Dbar' }, 92 // latin1 93 { path: 'C:\\fóóbàr', fileURL: 'file:///C:/f%C3%B3%C3%B3b%C3%A0r' }, 94 // Euro sign (BMP code point) 95 { path: 'C:\\€', fileURL: 'file:///C:/%E2%82%AC' }, 96 // Rocket emoji (non-BMP code point) 97 { path: 'C:\\', fileURL: 'file:///C:/%F0%9F%9A%80' }, 98 // UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows) 99 { path: '\\\\nas\\My Docs\\File.doc', fileURL: 'file://nas/My%20Docs/File.doc' }, 100 ]; 101 } else { 102 testCases = [ 103 // Lowercase ascii alpha 104 { path: '/foo', fileURL: 'file:///foo' }, 105 // Uppercase ascii alpha 106 { path: '/FOO', fileURL: 'file:///FOO' }, 107 // dir 108 { path: '/dir/foo', fileURL: 'file:///dir/foo' }, 109 // trailing separator 110 { path: '/dir/', fileURL: 'file:///dir/' }, 111 // dot 112 { path: '/foo.mjs', fileURL: 'file:///foo.mjs' }, 113 // space 114 { path: '/foo bar', fileURL: 'file:///foo%20bar' }, 115 // question mark 116 { path: '/foo?bar', fileURL: 'file:///foo%3Fbar' }, 117 // number sign 118 { path: '/foo#bar', fileURL: 'file:///foo%23bar' }, 119 // ampersand 120 { path: '/foo&bar', fileURL: 'file:///foo&bar' }, 121 // equals 122 { path: '/foo=bar', fileURL: 'file:///foo=bar' }, 123 // colon 124 { path: '/foo:bar', fileURL: 'file:///foo:bar' }, 125 // semicolon 126 { path: '/foo;bar', fileURL: 'file:///foo;bar' }, 127 // percent 128 { path: '/foo%bar', fileURL: 'file:///foo%25bar' }, 129 // backslash 130 { path: '/foo\\bar', fileURL: 'file:///foo%5Cbar' }, 131 // backspace 132 { path: '/foo\bbar', fileURL: 'file:///foo%08bar' }, 133 // tab 134 { path: '/foo\tbar', fileURL: 'file:///foo%09bar' }, 135 // newline 136 { path: '/foo\nbar', fileURL: 'file:///foo%0Abar' }, 137 // carriage return 138 { path: '/foo\rbar', fileURL: 'file:///foo%0Dbar' }, 139 // latin1 140 { path: '/fóóbàr', fileURL: 'file:///f%C3%B3%C3%B3b%C3%A0r' }, 141 // Euro sign (BMP code point) 142 { path: '/€', fileURL: 'file:///%E2%82%AC' }, 143 // Rocket emoji (non-BMP code point) 144 { path: '/', fileURL: 'file:///%F0%9F%9A%80' }, 145 ]; 146 } 147 148 for (const { path, fileURL } of testCases) { 149 const fromString = url.fileURLToPath(fileURL); 150 assert.strictEqual(fromString, path); 151 const fromURL = url.fileURLToPath(new URL(fileURL)); 152 assert.strictEqual(fromURL, path); 153 } 154} 155