Skip to content

Commit b5ab3dc

Browse files
committed
feat(utils): getSource
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent f0507df commit b5ab3dc

33 files changed

+724
-78
lines changed

.cspell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"patches/",
3131
"yarn.lock"
3232
],
33-
"ignoreRegExpList": [],
33+
"ignoreRegExpList": ["/data:.+;base64,.+/"],
3434
"ignoreWords": [],
3535
"language": "en-US",
3636
"patterns": [],

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import {
6868
defaultMainFields,
6969
extensionFormatMap,
7070
formats,
71+
getSource,
7172
isAbsoluteSpecifier,
7273
isArrayIndex,
7374
isBareSpecifier,
@@ -106,6 +107,7 @@ This package exports the following identifiers:
106107
- [`defaultMainFields`](./src/lib/default-main-fields.mts)
107108
- [`extensionFormatMap`](./src/lib/extension-format-map.mts)
108109
- [`formats`](./src/lib/formats.mts)
110+
- [`getSource`](./src/lib/get-source.mts)
109111
- [`isAbsoluteSpecifier`](./src/lib/is-absolute-specifier.mts)
110112
- [`isArrayIndex`](./src/lib/is-array-index.mts)
111113
- [`isBareSpecifier`](./src/lib/is-bare-specifier.mts)

eslint.base.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,12 @@ export default [
171171
globals: {
172172
...globals.es2024,
173173
...globals.node,
174+
BufferEncoding: 'readonly',
174175
Chai: 'readonly',
175176
Console: 'readonly',
176177
NodeJS: 'readonly',
177-
React: fs.existsSync('node_modules/react') ? 'readonly' : false
178+
React: fs.existsSync('node_modules/react') ? 'readonly' : false,
179+
RequestInit: 'readonly'
178180
},
179181
parser: /** @type {Parser} */ (ts.parser),
180182
parserOptions: {

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@
6363
"node": "fs",
6464
"default": "fs"
6565
},
66+
"#internal/process": {
67+
"types": {
68+
"mlly": "./src/internal/process.d.mts",
69+
"default": "./dist/internal/process.d.mts"
70+
},
71+
"browser": {
72+
"mlly": "./src/internal/process.browser.mts",
73+
"default": "./dist/internal/process.browser.mjs"
74+
},
75+
"node": "process",
76+
"default": "process"
77+
},
6678
"#internal/*": {
6779
"mlly": "./src/internal/*.mts",
6880
"default": "./dist/internal/*.mjs"

src/__snapshots__/index.e2e.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ exports[`e2e:mlly > should expose public api 1`] = `
99
"defaultMainFields",
1010
"extensionFormatMap",
1111
"formats",
12+
"getSource",
1213
"isAbsoluteSpecifier",
1314
"isArrayIndex",
1415
"isBareSpecifier",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @file Type Tests - GetSourceContext
3+
* @module mlly/interfaces/tests/unit-d/GetSourceContext
4+
*/
5+
6+
import type TestSubject from '#interfaces/context-get-source'
7+
import type {
8+
FileSystem,
9+
GetSourceHandlers,
10+
GetSourceOptions
11+
} from '@flex-development/mlly'
12+
13+
describe('unit-d:interfaces/GetSourceContext', () => {
14+
it('should extend GetSourceOptions', () => {
15+
expectTypeOf<TestSubject>().toMatchTypeOf<GetSourceOptions>()
16+
})
17+
18+
it('should match [error: boolean]', () => {
19+
expectTypeOf<TestSubject>().toHaveProperty('error').toEqualTypeOf<boolean>()
20+
})
21+
22+
it('should match [fs: FileSystem]', () => {
23+
expectTypeOf<TestSubject>().toHaveProperty('fs').toEqualTypeOf<FileSystem>()
24+
})
25+
26+
it('should match [handlers: GetSourceHandlers]', () => {
27+
expectTypeOf<TestSubject>()
28+
.toHaveProperty('handlers')
29+
.toEqualTypeOf<GetSourceHandlers>()
30+
})
31+
32+
it('should match [req: RequestInit]', () => {
33+
expectTypeOf<TestSubject>()
34+
.toHaveProperty('req')
35+
.toEqualTypeOf<RequestInit>()
36+
})
37+
38+
it('should match [schemes: Set<string>', () => {
39+
expectTypeOf<TestSubject>()
40+
.toHaveProperty('schemes')
41+
.toEqualTypeOf<Set<string>>()
42+
})
43+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @file Type Tests - GetSourceOptions
3+
* @module mlly/interfaces/tests/unit-d/GetSourceOptions
4+
*/
5+
6+
import type TestSubject from '#interfaces/options-get-source'
7+
import type {
8+
FileSystem,
9+
GetSourceHandlers,
10+
ModuleFormat
11+
} from '@flex-development/mlly'
12+
import type { Nilable } from '@flex-development/tutils'
13+
14+
describe('unit-d:interfaces/GetSourceOptions', () => {
15+
it('should allow empty object', () => {
16+
assertType<TestSubject>({})
17+
})
18+
19+
it('should match [format?: ModuleFormat | null | undefined]', () => {
20+
expectTypeOf<TestSubject>()
21+
.toHaveProperty('format')
22+
.toEqualTypeOf<Nilable<ModuleFormat>>()
23+
})
24+
25+
it('should match [fs?: FileSystem | null | undefined]', () => {
26+
expectTypeOf<TestSubject>()
27+
.toHaveProperty('fs')
28+
.toEqualTypeOf<Nilable<FileSystem>>()
29+
})
30+
31+
it('should match [handlers?: GetSourceHandlers | null | undefined]', () => {
32+
expectTypeOf<TestSubject>()
33+
.toHaveProperty('handlers')
34+
.toEqualTypeOf<Nilable<GetSourceHandlers>>()
35+
})
36+
37+
it('should match [ignoreErrors?: boolean | null | undefined]', () => {
38+
expectTypeOf<TestSubject>()
39+
.toHaveProperty('ignoreErrors')
40+
.toEqualTypeOf<Nilable<boolean>>()
41+
})
42+
43+
it('should match [req?: RequestInit | null | undefined]', () => {
44+
expectTypeOf<TestSubject>()
45+
.toHaveProperty('req')
46+
.toEqualTypeOf<Nilable<RequestInit>>()
47+
})
48+
49+
it('should match [schemes?: Set<string> | readonly string[] | null | undefined]', () => {
50+
expectTypeOf<TestSubject>()
51+
.toHaveProperty('schemes')
52+
.toEqualTypeOf<Nilable<Set<string> | readonly string[]>>()
53+
})
54+
})

src/interfaces/__tests__/protocol-map.spec-d.mts

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,91 @@
66
import type TestSubject from '#interfaces/protocol-map'
77

88
describe('unit-d:interfaces/ProtocolMap', () => {
9-
it('should have property "blob:"', () => {
10-
expectTypeOf<TestSubject>().toHaveProperty('blob:')
9+
it('should match [blob: "blob:"]', () => {
10+
expectTypeOf<TestSubject>().toHaveProperty('blob').toEqualTypeOf<'blob:'>()
1111
})
1212

13-
it('should have property "content:"', () => {
14-
expectTypeOf<TestSubject>().toHaveProperty('content:')
13+
it('should match [content: "content:"]', () => {
14+
expectTypeOf<TestSubject>()
15+
.toHaveProperty('content')
16+
.toEqualTypeOf<'content:'>()
1517
})
1618

17-
it('should have property "cvs:"', () => {
18-
expectTypeOf<TestSubject>().toHaveProperty('cvs:')
19+
it('should match [cvs: "cvs:"]', () => {
20+
expectTypeOf<TestSubject>().toHaveProperty('cvs').toEqualTypeOf<'cvs:'>()
1921
})
2022

21-
it('should have property "data:"', () => {
22-
expectTypeOf<TestSubject>().toHaveProperty('data:')
23+
it('should match [data: "data:"]', () => {
24+
expectTypeOf<TestSubject>().toHaveProperty('data').toEqualTypeOf<'data:'>()
2325
})
2426

25-
it('should have property "dns:"', () => {
26-
expectTypeOf<TestSubject>().toHaveProperty('dns:')
27+
it('should match [dns: "dns:"]', () => {
28+
expectTypeOf<TestSubject>().toHaveProperty('dns').toEqualTypeOf<'dns:'>()
2729
})
2830

29-
it('should have property "file:"', () => {
30-
expectTypeOf<TestSubject>().toHaveProperty('file:')
31+
it('should match [file: "file:"]', () => {
32+
expectTypeOf<TestSubject>().toHaveProperty('file').toEqualTypeOf<'file:'>()
3133
})
3234

33-
it('should have property "fish:"', () => {
34-
expectTypeOf<TestSubject>().toHaveProperty('fish:')
35+
it('should match [fish: "fish:"]', () => {
36+
expectTypeOf<TestSubject>().toHaveProperty('fish').toEqualTypeOf<'fish:'>()
3537
})
3638

37-
it('should have property "ftp:"', () => {
38-
expectTypeOf<TestSubject>().toHaveProperty('ftp:')
39+
it('should match [ftp: "ftp:"]', () => {
40+
expectTypeOf<TestSubject>().toHaveProperty('ftp').toEqualTypeOf<'ftp:'>()
3941
})
4042

41-
it('should have property "git:"', () => {
42-
expectTypeOf<TestSubject>().toHaveProperty('git:')
43+
it('should match [git: "git:"]', () => {
44+
expectTypeOf<TestSubject>().toHaveProperty('git').toEqualTypeOf<'git:'>()
4345
})
4446

45-
it('should have property "http:"', () => {
46-
expectTypeOf<TestSubject>().toHaveProperty('http:')
47+
it('should match [http: "http:"]', () => {
48+
expectTypeOf<TestSubject>().toHaveProperty('http').toEqualTypeOf<'http:'>()
4749
})
4850

49-
it('should have property "https:"', () => {
50-
expectTypeOf<TestSubject>().toHaveProperty('https:')
51+
it('should match [https: "https:"]', () => {
52+
expectTypeOf<TestSubject>()
53+
.toHaveProperty('https')
54+
.toEqualTypeOf<'https:'>()
5155
})
5256

53-
it('should have property "mvn:"', () => {
54-
expectTypeOf<TestSubject>().toHaveProperty('mvn:')
57+
it('should match [mvn: "mvn:"]', () => {
58+
expectTypeOf<TestSubject>().toHaveProperty('mvn').toEqualTypeOf<'mvn:'>()
5559
})
5660

57-
it('should have property "redis:"', () => {
58-
expectTypeOf<TestSubject>().toHaveProperty('redis:')
61+
it('should match [node: "node:"]', () => {
62+
expectTypeOf<TestSubject>().toHaveProperty('node').toEqualTypeOf<'node:'>()
5963
})
6064

61-
it('should have property "sftp:"', () => {
62-
expectTypeOf<TestSubject>().toHaveProperty('sftp:')
65+
it('should match [redis: "redis:"]', () => {
66+
expectTypeOf<TestSubject>()
67+
.toHaveProperty('redis')
68+
.toEqualTypeOf<'redis:'>()
6369
})
6470

65-
it('should have property "ssh:"', () => {
66-
expectTypeOf<TestSubject>().toHaveProperty('ssh:')
71+
it('should match [sftp: "sftp:"]', () => {
72+
expectTypeOf<TestSubject>().toHaveProperty('sftp').toEqualTypeOf<'sftp:'>()
6773
})
6874

69-
it('should have property "svn:"', () => {
70-
expectTypeOf<TestSubject>().toHaveProperty('svn:')
75+
it('should match [ssh: "ssh:"]', () => {
76+
expectTypeOf<TestSubject>().toHaveProperty('ssh').toEqualTypeOf<'ssh:'>()
7177
})
7278

73-
it('should have property "view-source:"', () => {
74-
expectTypeOf<TestSubject>().toHaveProperty('view-source:')
79+
it('should match [svn: "svn:"]', () => {
80+
expectTypeOf<TestSubject>().toHaveProperty('svn').toEqualTypeOf<'svn:'>()
7581
})
7682

77-
it('should have property "ws:"', () => {
78-
expectTypeOf<TestSubject>().toHaveProperty('ws:')
83+
it('should match [viewSource: "view-source:"]', () => {
84+
expectTypeOf<TestSubject>()
85+
.toHaveProperty('viewSource')
86+
.toEqualTypeOf<'view-source:'>()
7987
})
8088

81-
it('should have property "wss:"', () => {
82-
expectTypeOf<TestSubject>().toHaveProperty('wss:')
89+
it('should match [ws: "ws:"]', () => {
90+
expectTypeOf<TestSubject>().toHaveProperty('ws').toEqualTypeOf<'ws:'>()
91+
})
92+
93+
it('should match [wss: "wss:"]', () => {
94+
expectTypeOf<TestSubject>().toHaveProperty('wss').toEqualTypeOf<'wss:'>()
8395
})
8496
})

src/interfaces/context-get-source.mts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @file Interfaces - GetSourceContext
3+
* @module mlly/interfaces/GetSourceContext
4+
*/
5+
6+
import type {
7+
FileSystem,
8+
GetSourceHandlers,
9+
GetSourceOptions
10+
} from '@flex-development/mlly'
11+
12+
/**
13+
* Source code retrieval context.
14+
*
15+
* @see {@linkcode GetSourceOptions}
16+
*
17+
* @extends {GetSourceOptions}
18+
*/
19+
interface GetSourceContext extends GetSourceOptions {
20+
/**
21+
* Throw [`ERR_UNSUPPORTED_ESM_URL_SCHEME`][err]?
22+
*
23+
* [err]: https://nodejs.org/api/errors.html#err_unsupported_esm_url_scheme
24+
*/
25+
error: boolean
26+
27+
/**
28+
* File system API.
29+
*
30+
* @see {@linkcode FileSystem}
31+
*
32+
* @override
33+
*/
34+
fs: FileSystem
35+
36+
/**
37+
* URL handler map.
38+
*
39+
* @see {@linkcode GetSourceHandlers}
40+
*
41+
* @override
42+
*/
43+
handlers: GetSourceHandlers
44+
45+
/**
46+
* Request options for network based modules.
47+
*
48+
* > 👉 **Note**: Only applicable if {@linkcode network} is
49+
* > enabled.
50+
*
51+
* @override
52+
*/
53+
req: RequestInit
54+
55+
/**
56+
* List of supported URL schemes.
57+
*
58+
* @override
59+
*/
60+
schemes: Set<string>
61+
}
62+
63+
export type { GetSourceContext as default }

src/interfaces/index.mts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
*/
55

66
export type { default as Aliases } from '#interfaces/aliases'
7+
export type {
8+
default as GetSourceContext
9+
} from '#interfaces/context-get-source'
710
export type { default as FileSystem } from '#interfaces/file-system'
811
export type { default as MainFieldMap } from '#interfaces/main-field-map'
912
export type { default as ModuleFormatMap } from '#interfaces/module-format-map'
13+
export type {
14+
default as GetSourceOptions
15+
} from '#interfaces/options-get-source'
1016
export type {
1117
default as ResolveAliasOptions
1218
} from '#interfaces/options-resolve-alias'

0 commit comments

Comments
 (0)