Skip to content

Commit 9a43317

Browse files
committed
feat(models): ERR_UNSUPPORTED_ESM_URL_SCHEME
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 1c910df commit 9a43317

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@
143143
"bundleDependencies": [
144144
"node-inspect-extracted"
145145
],
146+
"peerDependencies": {
147+
"@types/node": ">=14"
148+
},
149+
"peerDependenciesMeta": {
150+
"@types/node": {
151+
"optional": true
152+
}
153+
},
146154
"resolutions": {
147155
"@ardatan/sync-fetch": "larsgw/sync-fetch#head=worker_threads",
148156
"@flex-development/tutils": "6.0.0-alpha.7",
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @file Unit Tests - ERR_UNSUPPORTED_ESM_URL_SCHEME
3+
* @module errnode/models/tests/unit/ERR_UNSUPPORTED_ESM_URL_SCHEME
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import formatList from '#src/internal/format-list'
8+
import { URL } from 'node:url'
9+
import TestSubject from '../err-unsupported-esm-url-scheme'
10+
11+
describe('unit:models/ERR_UNSUPPORTED_ESM_URL_SCHEME', () => {
12+
let schemes: string
13+
let supported: string[]
14+
let url: URL
15+
16+
beforeEach(() => {
17+
supported = ['data', 'file']
18+
url = new URL('dist/index.mjs', 'https://example.com/')
19+
schemes = formatList(supported)
20+
})
21+
22+
it('should return Error instance', () => {
23+
// Act
24+
const result = new TestSubject(url, supported)
25+
26+
// Expect
27+
expect(result).to.be.instanceof(Error)
28+
expect(result).to.have.property('name').equal('Error')
29+
})
30+
31+
it('should set error code', () => {
32+
expect(new TestSubject(url, supported))
33+
.to.have.property('code')
34+
.equal(ErrorCode.ERR_UNSUPPORTED_ESM_URL_SCHEME)
35+
})
36+
37+
it('should set error message', () => {
38+
// Arrange
39+
const cases: [...ConstructorParameters<typeof TestSubject>, string][] = [
40+
[
41+
url,
42+
supported,
43+
undefined,
44+
`Only URLs with a scheme in: ${schemes} are supported by the default ESM loader. Received protocol '${url.protocol}'`
45+
],
46+
[
47+
new URL('dist/index.mjs', 'c://'),
48+
supported,
49+
true,
50+
`Only URLs with a scheme in: ${schemes} are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'`
51+
]
52+
]
53+
54+
// Act + Expect
55+
cases.forEach(([url, supported, windows, expected]) => {
56+
expect(new TestSubject(url, supported, windows))
57+
.to.have.property('message')
58+
.equal(expected)
59+
})
60+
})
61+
})
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file Error Models - ERR_UNSUPPORTED_ESM_URL_SCHEME
3+
* @module errnode/models/ERR_UNSUPPORTED_ESM_URL_SCHEME
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1698-L1706
5+
*/
6+
7+
import { ErrorCode } from '#src/enums'
8+
import formatList from '#src/internal/format-list'
9+
import type { NodeError, NodeErrorConstructor } from '#src/types'
10+
import { createNodeError } from '#src/utils'
11+
import type { URL } from 'node:url'
12+
13+
/**
14+
* `ERR_UNSUPPORTED_ESM_URL_SCHEME` model.
15+
*
16+
* Thrown when an unsupported URL scheme is used in an `import` statement. URL
17+
* schemes other than `file` and `data` are unsupported.
18+
*
19+
* @see https://nodejs.org/api/errors.html#err_unsupported_esm_url_scheme
20+
*
21+
* @class
22+
*
23+
* @param {URL} url - URL containing unsupported scheme
24+
* @param {string[]} supported - Supported URL schemes
25+
* @param {boolean?} [windows=false] - Windows operating system?
26+
* @return {NodeError} `Error` instance
27+
*/
28+
const ERR_UNSUPPORTED_ESM_URL_SCHEME: NodeErrorConstructor<
29+
ErrorConstructor,
30+
[URL, string[], boolean?]
31+
> = createNodeError(
32+
ErrorCode.ERR_UNSUPPORTED_ESM_URL_SCHEME,
33+
Error,
34+
/**
35+
* Creates an [`ERR_UNSUPPORTED_ESM_URL_SCHEME`][1] message.
36+
*
37+
* [1]: https://nodejs.org/api/errors.html#err_unsupported_esm_url_scheme
38+
*
39+
* @param {URL} url - URL containing unsupported scheme
40+
* @param {string[]} supported - Supported URL schemes
41+
* @param {boolean?} [windows=false] - Windows operating system?
42+
* @return {string} Error message
43+
*/
44+
(url: URL, supported: string[], windows: boolean = false): string => {
45+
/**
46+
* Supported URL schemes.
47+
*
48+
* @const {string} schemes
49+
*/
50+
const schemes: string = formatList(supported)
51+
52+
/**
53+
* Error message.
54+
*
55+
* @var {string} message
56+
*/
57+
let message: string = `Only URLs with a scheme in: ${schemes} are supported by the default ESM loader`
58+
59+
// add additional error details if operating system is windows
60+
if (windows && url.protocol.length === 2) {
61+
message += '. On Windows, absolute paths must be valid file:// URLs'
62+
}
63+
64+
// add url scheme provided by user
65+
message += `. Received protocol '${url.protocol}'`
66+
67+
return message
68+
}
69+
)
70+
71+
export default ERR_UNSUPPORTED_ESM_URL_SCHEME

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export { default as ERR_UNKNOWN_ENCODING } from './err-unknown-encoding'
2626
export { default as ERR_UNKNOWN_FILE_EXTENSION } from './err-unknown-file-extension'
2727
export { default as ERR_UNKNOWN_MODULE_FORMAT } from './err-unknown-module-format'
2828
export { default as ERR_UNSUPPORTED_DIR_IMPORT } from './err-unsupported-dir-import'
29+
export { default as ERR_UNSUPPORTED_ESM_URL_SCHEME } from './err-unsupported-esm-url-scheme'

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,11 @@ __metadata:
10631063
vitest: "npm:0.26.2"
10641064
vitest-github-actions-reporter: "npm:0.9.0"
10651065
yaml-eslint-parser: "npm:1.1.0"
1066+
peerDependencies:
1067+
"@types/node": ">=14"
1068+
peerDependenciesMeta:
1069+
"@types/node":
1070+
optional: true
10661071
languageName: unknown
10671072
linkType: soft
10681073

0 commit comments

Comments
 (0)