source: node_modules/@npmcli/fs/lib/common/file-url-to-path/polyfill.js@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 3.1 KB
Line 
1const { URL, domainToUnicode } = require('url')
2
3const CHAR_LOWERCASE_A = 97
4const CHAR_LOWERCASE_Z = 122
5
6const isWindows = process.platform === 'win32'
7
8class ERR_INVALID_FILE_URL_HOST extends TypeError {
9 constructor (platform) {
10 super(`File URL host must be "localhost" or empty on ${platform}`)
11 this.code = 'ERR_INVALID_FILE_URL_HOST'
12 }
13
14 toString () {
15 return `${this.name} [${this.code}]: ${this.message}`
16 }
17}
18
19class ERR_INVALID_FILE_URL_PATH extends TypeError {
20 constructor (msg) {
21 super(`File URL path ${msg}`)
22 this.code = 'ERR_INVALID_FILE_URL_PATH'
23 }
24
25 toString () {
26 return `${this.name} [${this.code}]: ${this.message}`
27 }
28}
29
30class ERR_INVALID_ARG_TYPE extends TypeError {
31 constructor (name, actual) {
32 super(`The "${name}" argument must be one of type string or an instance ` +
33 `of URL. Received type ${typeof actual} ${actual}`)
34 this.code = 'ERR_INVALID_ARG_TYPE'
35 }
36
37 toString () {
38 return `${this.name} [${this.code}]: ${this.message}`
39 }
40}
41
42class ERR_INVALID_URL_SCHEME extends TypeError {
43 constructor (expected) {
44 super(`The URL must be of scheme ${expected}`)
45 this.code = 'ERR_INVALID_URL_SCHEME'
46 }
47
48 toString () {
49 return `${this.name} [${this.code}]: ${this.message}`
50 }
51}
52
53const isURLInstance = (input) => {
54 return input != null && input.href && input.origin
55}
56
57const getPathFromURLWin32 = (url) => {
58 const hostname = url.hostname
59 let pathname = url.pathname
60 for (let n = 0; n < pathname.length; n++) {
61 if (pathname[n] === '%') {
62 const third = pathname.codePointAt(n + 2) | 0x20
63 if ((pathname[n + 1] === '2' && third === 102) ||
64 (pathname[n + 1] === '5' && third === 99)) {
65 throw new ERR_INVALID_FILE_URL_PATH('must not include encoded \\ or / characters')
66 }
67 }
68 }
69
70 pathname = pathname.replace(/\//g, '\\')
71 pathname = decodeURIComponent(pathname)
72 if (hostname !== '') {
73 return `\\\\${domainToUnicode(hostname)}${pathname}`
74 }
75
76 const letter = pathname.codePointAt(1) | 0x20
77 const sep = pathname[2]
78 if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z ||
79 (sep !== ':')) {
80 throw new ERR_INVALID_FILE_URL_PATH('must be absolute')
81 }
82
83 return pathname.slice(1)
84}
85
86const getPathFromURLPosix = (url) => {
87 if (url.hostname !== '') {
88 throw new ERR_INVALID_FILE_URL_HOST(process.platform)
89 }
90
91 const pathname = url.pathname
92
93 for (let n = 0; n < pathname.length; n++) {
94 if (pathname[n] === '%') {
95 const third = pathname.codePointAt(n + 2) | 0x20
96 if (pathname[n + 1] === '2' && third === 102) {
97 throw new ERR_INVALID_FILE_URL_PATH('must not include encoded / characters')
98 }
99 }
100 }
101
102 return decodeURIComponent(pathname)
103}
104
105const fileURLToPath = (path) => {
106 if (typeof path === 'string') {
107 path = new URL(path)
108 } else if (!isURLInstance(path)) {
109 throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path)
110 }
111
112 if (path.protocol !== 'file:') {
113 throw new ERR_INVALID_URL_SCHEME('file')
114 }
115
116 return isWindows
117 ? getPathFromURLWin32(path)
118 : getPathFromURLPosix(path)
119}
120
121module.exports = fileURLToPath
Note: See TracBrowser for help on using the repository browser.