@flex-development/mlly 1.0.0-alpha.16 → 1.0.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +61 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +5 -6
- package/dist/internal/dequote.mjs +1 -0
- package/dist/internal/dequote.mjs.map +8 -0
- package/dist/internal/format-type-map.mjs +1 -0
- package/dist/internal/format-type-map.mjs.map +8 -0
- package/dist/internal/get-specifier-kind.mjs +1 -0
- package/dist/internal/get-specifier-kind.mjs.map +8 -0
- package/dist/internal/get-subpaths.mjs +1 -0
- package/dist/internal/get-subpaths.mjs.map +8 -0
- package/dist/internal/regex-encoded-sep.mjs +1 -0
- package/dist/internal/regex-encoded-sep.mjs.map +8 -0
- package/dist/internal/regex-internal-specifier.mjs +1 -0
- package/dist/internal/regex-internal-specifier.mjs.map +8 -0
- package/dist/internal/regex-invalid-segment.mjs +1 -0
- package/dist/internal/regex-invalid-segment.mjs.map +8 -0
- package/dist/internal/regex-package-name.mjs +1 -0
- package/dist/internal/regex-package-name.mjs.map +8 -0
- package/dist/internal/regex-package-path.mjs +1 -0
- package/dist/internal/regex-package-path.mjs.map +8 -0
- package/dist/internal/resolver.mjs +1 -0
- package/dist/internal/resolver.mjs.map +8 -0
- package/dist/internal/validate-array-set.mjs +1 -0
- package/dist/internal/validate-array-set.mjs.map +8 -0
- package/dist/internal/validate-boolean.mjs +1 -0
- package/dist/internal/validate-boolean.mjs.map +8 -0
- package/dist/internal/validate-map.mjs +1 -0
- package/dist/internal/validate-map.mjs.map +8 -0
- package/dist/internal/validate-object.mjs +1 -0
- package/dist/internal/validate-object.mjs.map +8 -0
- package/dist/internal/validate-set.mjs +1 -0
- package/dist/internal/validate-set.mjs.map +8 -0
- package/dist/internal/validate-string.mjs +1 -0
- package/dist/internal/validate-string.mjs.map +8 -0
- package/dist/internal/validate-url-string.mjs +1 -0
- package/dist/internal/validate-url-string.mjs.map +8 -0
- package/dist/utils/compare-subpaths.mjs.map +4 -5
- package/dist/utils/conditions.mjs.map +4 -5
- package/dist/utils/detect-syntax.mjs.map +4 -5
- package/dist/utils/extension-format-map.mjs.map +4 -5
- package/dist/utils/extract-statements.mjs.map +4 -5
- package/dist/utils/fill-modules.mjs.map +5 -8
- package/dist/utils/find-dynamic-imports.mjs.map +5 -8
- package/dist/utils/find-exports.mjs.map +5 -8
- package/dist/utils/find-requires.mjs.map +5 -8
- package/dist/utils/find-static-imports.mjs.map +5 -8
- package/dist/utils/find-subpath.mjs.map +5 -9
- package/dist/utils/get-format.mjs.map +5 -8
- package/dist/utils/get-source.mjs.map +4 -5
- package/dist/utils/has-cjs-syntax.mjs.map +4 -5
- package/dist/utils/has-esm-syntax.mjs.map +4 -5
- package/dist/utils/index.mjs.map +5 -8
- package/dist/utils/is-absolute-specifier.mjs.map +4 -5
- package/dist/utils/is-bare-specifier.mjs.map +4 -5
- package/dist/utils/is-directory.mjs.map +4 -5
- package/dist/utils/is-exports-sugar.mjs.map +4 -5
- package/dist/utils/is-file.mjs.map +4 -5
- package/dist/utils/is-relative-specifier.mjs.map +4 -5
- package/dist/utils/lookup-package-scope.mjs.map +4 -5
- package/dist/utils/parse-data-url.mjs.map +4 -5
- package/dist/utils/parse-module-id.mjs.map +5 -9
- package/dist/utils/parse-subpath.mjs.map +5 -8
- package/dist/utils/pattern-character.mjs.map +4 -5
- package/dist/utils/read-package-json.mjs.map +4 -5
- package/dist/utils/resolve-alias.mjs.map +5 -9
- package/dist/utils/resolve-aliases.mjs.map +4 -5
- package/dist/utils/resolve-extensions.mjs.map +4 -5
- package/dist/utils/resolve-module.d.mts +1 -1
- package/dist/utils/resolve-module.mjs.map +4 -5
- package/dist/utils/resolve-modules.mjs.map +4 -5
- package/dist/utils/to-absolute-specifier.mjs.map +4 -5
- package/dist/utils/to-bare-specifier.mjs +8 -1
- package/dist/utils/to-bare-specifier.mjs.map +5 -6
- package/dist/utils/to-data-url.mjs.map +4 -5
- package/dist/utils/to-node-url.mjs.map +4 -5
- package/dist/utils/to-relative-specifier.mjs.map +4 -5
- package/dist/utils/to-url.mjs.map +4 -5
- package/dist/utils/validate-assertions.mjs.map +5 -8
- package/dist/utils/validate-exports.mjs.map +4 -5
- package/package.json +16 -17
- package/dist/interfaces/index.mjs +0 -0
- package/dist/types/index.mjs +0 -0
- package/src/enums/assert-type.ts +0 -23
- package/src/enums/format.ts +0 -19
- package/src/enums/index.ts +0 -11
- package/src/enums/kind-specifier-syntax.ts +0 -16
- package/src/enums/kind-specifier.ts +0 -19
- package/src/enums/kind-statement-syntax.ts +0 -24
- package/src/enums/kind-statement.ts +0 -17
- package/src/index.ts +0 -9
- package/src/interfaces/import-assertions.ts +0 -24
- package/src/interfaces/import-dynamic.ts +0 -46
- package/src/interfaces/import-static.ts +0 -46
- package/src/interfaces/index.ts +0 -25
- package/src/interfaces/options-fill-module.ts +0 -26
- package/src/interfaces/options-find-subpath.ts +0 -57
- package/src/interfaces/options-get-format.ts +0 -80
- package/src/interfaces/options-get-source.ts +0 -59
- package/src/interfaces/options-parse-module-id.ts +0 -39
- package/src/interfaces/options-parse-subpath.ts +0 -57
- package/src/interfaces/options-resolve-alias.ts +0 -59
- package/src/interfaces/options-resolve-module.ts +0 -72
- package/src/interfaces/options-resolve.ts +0 -17
- package/src/interfaces/package-scope.ts +0 -30
- package/src/interfaces/parsed-data-url.ts +0 -63
- package/src/interfaces/parsed-module-id.ts +0 -90
- package/src/interfaces/parsed-subpath.ts +0 -56
- package/src/interfaces/statement-export.ts +0 -52
- package/src/interfaces/statement-import.ts +0 -45
- package/src/interfaces/statement-require.ts +0 -45
- package/src/interfaces/statement.ts +0 -64
- package/src/internal/dequote.ts +0 -18
- package/src/internal/format-type-map.ts +0 -28
- package/src/internal/get-specifier-kind.ts +0 -41
- package/src/internal/get-subpaths.ts +0 -43
- package/src/internal/regex-encoded-sep.ts +0 -17
- package/src/internal/regex-internal-specifier.ts +0 -19
- package/src/internal/regex-invalid-segment.ts +0 -25
- package/src/internal/regex-package-name.ts +0 -18
- package/src/internal/regex-package-path.ts +0 -20
- package/src/internal/resolver.ts +0 -790
- package/src/internal/validate-array-set.ts +0 -35
- package/src/internal/validate-boolean.ts +0 -30
- package/src/internal/validate-map.ts +0 -36
- package/src/internal/validate-object.ts +0 -30
- package/src/internal/validate-set.ts +0 -32
- package/src/internal/validate-string.ts +0 -31
- package/src/internal/validate-url-string.ts +0 -34
- package/src/types/declaration.ts +0 -22
- package/src/types/fn-change-ext.ts +0 -33
- package/src/types/index.ts +0 -14
- package/src/types/mime-type.ts +0 -19
- package/src/types/module-id.ts +0 -15
- package/src/types/module-specifier-type.ts +0 -13
- package/src/types/protocol.ts +0 -40
- package/src/types/syntax-kind-export.ts +0 -20
- package/src/types/syntax-kind-import.ts +0 -22
- package/src/types/syntax-kind-require.ts +0 -15
- package/src/utils/compare-subpaths.ts +0 -100
- package/src/utils/conditions.ts +0 -15
- package/src/utils/detect-syntax.ts +0 -40
- package/src/utils/extension-format-map.ts +0 -34
- package/src/utils/extract-statements.ts +0 -46
- package/src/utils/fill-modules.ts +0 -133
- package/src/utils/find-dynamic-imports.ts +0 -75
- package/src/utils/find-exports.ts +0 -156
- package/src/utils/find-requires.ts +0 -75
- package/src/utils/find-static-imports.ts +0 -86
- package/src/utils/find-subpath.ts +0 -251
- package/src/utils/get-format.ts +0 -249
- package/src/utils/get-source.ts +0 -144
- package/src/utils/has-cjs-syntax.ts +0 -44
- package/src/utils/has-esm-syntax.ts +0 -40
- package/src/utils/index.ts +0 -45
- package/src/utils/is-absolute-specifier.ts +0 -53
- package/src/utils/is-bare-specifier.ts +0 -36
- package/src/utils/is-directory.ts +0 -25
- package/src/utils/is-exports-sugar.ts +0 -81
- package/src/utils/is-file.ts +0 -25
- package/src/utils/is-relative-specifier.ts +0 -34
- package/src/utils/lookup-package-scope.ts +0 -97
- package/src/utils/parse-data-url.ts +0 -71
- package/src/utils/parse-module-id.ts +0 -197
- package/src/utils/parse-subpath.ts +0 -331
- package/src/utils/pattern-character.ts +0 -13
- package/src/utils/read-package-json.ts +0 -113
- package/src/utils/resolve-alias.ts +0 -224
- package/src/utils/resolve-aliases.ts +0 -58
- package/src/utils/resolve-extensions.ts +0 -33
- package/src/utils/resolve-module.ts +0 -181
- package/src/utils/resolve-modules.ts +0 -60
- package/src/utils/to-absolute-specifier.ts +0 -30
- package/src/utils/to-bare-specifier.ts +0 -211
- package/src/utils/to-data-url.ts +0 -42
- package/src/utils/to-node-url.ts +0 -27
- package/src/utils/to-relative-specifier.ts +0 -55
- package/src/utils/to-url.ts +0 -30
- package/src/utils/validate-assertions.ts +0 -126
- package/src/utils/validate-exports.ts +0 -131
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file toBareSpecifier
|
|
3
|
-
* @module mlly/utils/toBareSpecifier
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { PackageScope, ParsedModuleId } from '#src/interfaces'
|
|
7
|
-
import validateSet from '#src/internal/validate-set'
|
|
8
|
-
import validateURLString from '#src/internal/validate-url-string'
|
|
9
|
-
import type { ModuleId } from '#src/types'
|
|
10
|
-
import {
|
|
11
|
-
ERR_OPERATION_FAILED,
|
|
12
|
-
ERR_PACKAGE_PATH_NOT_EXPORTED,
|
|
13
|
-
ErrorCode,
|
|
14
|
-
type NodeError
|
|
15
|
-
} from '@flex-development/errnode'
|
|
16
|
-
import { isBuiltin } from '@flex-development/is-builtin'
|
|
17
|
-
import pathe from '@flex-development/pathe'
|
|
18
|
-
import {
|
|
19
|
-
DOT,
|
|
20
|
-
cast,
|
|
21
|
-
isNIL,
|
|
22
|
-
regexp,
|
|
23
|
-
type Nullable
|
|
24
|
-
} from '@flex-development/tutils'
|
|
25
|
-
import { URL, fileURLToPath, pathToFileURL } from 'node:url'
|
|
26
|
-
import CONDITIONS from './conditions'
|
|
27
|
-
import findSubpath from './find-subpath'
|
|
28
|
-
import isAbsoluteSpecifier from './is-absolute-specifier'
|
|
29
|
-
import isRelativeSpecifier from './is-relative-specifier'
|
|
30
|
-
import lookupPackageScope from './lookup-package-scope'
|
|
31
|
-
import parseModuleId from './parse-module-id'
|
|
32
|
-
import parseSubpath from './parse-subpath'
|
|
33
|
-
import PATTERN_CHARACTER from './pattern-character'
|
|
34
|
-
import toAbsoluteSpecifier from './to-absolute-specifier'
|
|
35
|
-
import toNodeURL from './to-node-url'
|
|
36
|
-
import toURL from './to-url'
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Converts the given module `specifier` into a bare specifier.
|
|
40
|
-
*
|
|
41
|
-
* The new specifier is **not** guaranteed to resolve to an existing module, nor
|
|
42
|
-
* is it guaranteed to have a file extension, but it *is* guaranteed to be a
|
|
43
|
-
* package path containing a valid [subpath export][1] if the specifier's parent
|
|
44
|
-
* package uses package [`exports`][2]. If the parent package does not use
|
|
45
|
-
* `exports`, the new specifier is only guaranteed to begin with a valid NPM
|
|
46
|
-
* package name or Node.js [builtin module][3] name.
|
|
47
|
-
*
|
|
48
|
-
* The given module `specifier` should be absolute, syntactically bare (i.e. not
|
|
49
|
-
* begin with `'/'` or `'.'`), or relative to the current working directory
|
|
50
|
-
* (e.g. `'./dist/index.mjs'`).
|
|
51
|
-
*
|
|
52
|
-
* ::: warning
|
|
53
|
-
* Internal specifier (i.e. [subpath import][4]; `'#src'`, `'#src/utils'`)
|
|
54
|
-
* conversion is not supported. Converting *_to_* an internal specifier will be
|
|
55
|
-
* supported in the future.
|
|
56
|
-
* :::
|
|
57
|
-
*
|
|
58
|
-
* [1]: https://nodejs.org/api/packages.html#subpath-exports
|
|
59
|
-
* [2]: https://nodejs.org/api/packages.html#exports
|
|
60
|
-
* [3]: https://nodejs.org/api/module.html#modulebuiltinmodules
|
|
61
|
-
* [4]: https://nodejs.org/api/packages.html#subpath-imports
|
|
62
|
-
*
|
|
63
|
-
* @see {@linkcode CONDITIONS}
|
|
64
|
-
* @see {@linkcode ModuleId}
|
|
65
|
-
* @see https://nodejs.org/api/esm.html#terminology
|
|
66
|
-
*
|
|
67
|
-
* @param {ModuleId} specifier - Module specifier to convert
|
|
68
|
-
* @param {ModuleId} parent - URL of module to resolve from
|
|
69
|
-
* @param {Set<string>?} [conditions=CONDITIONS] - Export conditions
|
|
70
|
-
* @return {string} `specifier` as bare specifier
|
|
71
|
-
* @throws {NodeError}
|
|
72
|
-
*/
|
|
73
|
-
const toBareSpecifier = (
|
|
74
|
-
specifier: ModuleId,
|
|
75
|
-
parent: ModuleId,
|
|
76
|
-
conditions: Set<string> = CONDITIONS
|
|
77
|
-
): string => {
|
|
78
|
-
validateURLString(specifier, 'specifier')
|
|
79
|
-
validateURLString(parent, 'parent')
|
|
80
|
-
validateSet(conditions, 'conditions')
|
|
81
|
-
|
|
82
|
-
// ensure specifier is a string
|
|
83
|
-
if (specifier instanceof URL) specifier = specifier.href
|
|
84
|
-
|
|
85
|
-
// exit early if specifier is builtin module
|
|
86
|
-
if (isBuiltin(specifier)) return toNodeURL(specifier)
|
|
87
|
-
|
|
88
|
-
// ensure specifier is a path
|
|
89
|
-
if (specifier.startsWith('file:')) specifier = fileURLToPath(specifier)
|
|
90
|
-
|
|
91
|
-
// convert relative specifiers to absolute paths
|
|
92
|
-
if (isRelativeSpecifier(specifier)) {
|
|
93
|
-
specifier = fileURLToPath(toAbsoluteSpecifier(specifier))
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Location to begin package scope search for {@linkcode specifier}.
|
|
98
|
-
*
|
|
99
|
-
* @const {URL} url
|
|
100
|
-
*/
|
|
101
|
-
const url: URL = toURL(
|
|
102
|
-
isAbsoluteSpecifier(specifier) || specifier.startsWith('node_modules')
|
|
103
|
-
? specifier
|
|
104
|
-
: `node_modules/${specifier}`
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Package scope result for {@linkcode specifier}.
|
|
109
|
-
*
|
|
110
|
-
* @const {Nullable<PackageScope>} scope
|
|
111
|
-
*/
|
|
112
|
-
const scope: Nullable<PackageScope> =
|
|
113
|
-
lookupPackageScope(url, pathToFileURL(DOT)) ??
|
|
114
|
-
lookupPackageScope(specifier, pathToFileURL(DOT))
|
|
115
|
-
|
|
116
|
-
// throw if package scope was not found
|
|
117
|
-
if (!scope) {
|
|
118
|
-
throw new ERR_OPERATION_FAILED(`Package scope for '${specifier}' not found`)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// get package exports and name
|
|
122
|
-
const { exports, main, name = '', types } = scope.pkgjson
|
|
123
|
-
|
|
124
|
-
// convert specifier to bare specifier
|
|
125
|
-
specifier = url.pathname.includes(name)
|
|
126
|
-
? name + url.pathname.replace(new RegExp(`.*?${regexp(name)}`), '')
|
|
127
|
-
: name + specifier.replace(fileURLToPath(scope.dir.replace(/\/$/, '')), '')
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Parsed module specifier.
|
|
131
|
-
*
|
|
132
|
-
* Ensures {@linkcode specifier} has become a bare specifier.
|
|
133
|
-
*
|
|
134
|
-
* @const {ParsedModuleId} id
|
|
135
|
-
*/
|
|
136
|
-
const id: ParsedModuleId = parseModuleId(specifier, {
|
|
137
|
-
internal: false,
|
|
138
|
-
parent,
|
|
139
|
-
pkgname: true
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
// exit early if subpath does not need to be validated
|
|
143
|
-
if (isNIL(exports)) {
|
|
144
|
-
// set specifier to package name if specifier is main or types entry point
|
|
145
|
-
if ([main, types].some(ep => ep === id.path || ep === id.path.slice(2))) {
|
|
146
|
-
specifier = id.pkg
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// remove '@types/' prefix to avoid 'Cannot import type declaration files.
|
|
150
|
-
// Consider importing "*" instead of "@types/*"' (TS6137) error
|
|
151
|
-
specifier = specifier.replace(/^@types\//, '')
|
|
152
|
-
|
|
153
|
-
return specifier
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// check if specifier contains valid subpath export
|
|
157
|
-
try {
|
|
158
|
-
parseSubpath(specifier, exports, { dir: scope.dir, parent })
|
|
159
|
-
} catch (e: unknown) {
|
|
160
|
-
// try finding defined subpath if specifier is invalid package path
|
|
161
|
-
if (cast<NodeError>(e).code === ErrorCode.ERR_PACKAGE_PATH_NOT_EXPORTED) {
|
|
162
|
-
/**
|
|
163
|
-
* Package target to find defined subpath for.
|
|
164
|
-
*
|
|
165
|
-
* @const {string} target
|
|
166
|
-
*/
|
|
167
|
-
const target: string = specifier.replace(name, DOT)
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Subpath in {@linkcode exports} thats maps to {@linkcode target}, if any
|
|
171
|
-
* at all.
|
|
172
|
-
*
|
|
173
|
-
* @var {Nullable<string>} subpath
|
|
174
|
-
*/
|
|
175
|
-
let subpath: Nullable<string> = findSubpath(target, exports, {
|
|
176
|
-
conditions,
|
|
177
|
-
dir: scope.dir,
|
|
178
|
-
parent
|
|
179
|
-
})
|
|
180
|
-
|
|
181
|
-
// throw if target was not matched to a subpath
|
|
182
|
-
if (subpath === null) {
|
|
183
|
-
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
|
|
184
|
-
fileURLToPath(scope.dir),
|
|
185
|
-
id.path,
|
|
186
|
-
fileURLToPath(parent)
|
|
187
|
-
)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// replace pattern character in subpath
|
|
191
|
-
if (subpath.includes(PATTERN_CHARACTER)) {
|
|
192
|
-
subpath = subpath.slice(0, subpath.indexOf(PATTERN_CHARACTER)).slice(1)
|
|
193
|
-
subpath = DOT + target.slice(target.indexOf(subpath))
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// subpath should not include extension if extension is already included
|
|
197
|
-
// via package target
|
|
198
|
-
if (pathe.extname(target) === pathe.extname(subpath)) {
|
|
199
|
-
subpath = pathe.changeExt(subpath, '')
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return name + subpath.slice(1)
|
|
203
|
-
} /* c8 ignore next 3 */
|
|
204
|
-
// throw original error if package path error is not encountered
|
|
205
|
-
throw e
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return specifier
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export default toBareSpecifier
|
package/src/utils/to-data-url.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file toDataURL
|
|
3
|
-
* @module mlly/utils/toDataURL
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import validateString from '#src/internal/validate-string'
|
|
7
|
-
import type { MimeType } from '#src/types'
|
|
8
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Converts `code` into a [`data:` URL][1] using `base64` encoding.
|
|
12
|
-
*
|
|
13
|
-
* ::: tip
|
|
14
|
-
* `data:` URLs only resolve [bare specifiers][2] for builtin modules and
|
|
15
|
-
* [absolute specifiers][2].
|
|
16
|
-
*
|
|
17
|
-
* Call [`await resolveModules(code)`][3] to ensure all specifiers are absolute
|
|
18
|
-
* or bare.
|
|
19
|
-
* :::
|
|
20
|
-
*
|
|
21
|
-
* [1]: https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
|
|
22
|
-
* [2]: https://nodejs.org/api/esm.html#terminology
|
|
23
|
-
* [3]: {@link ./resolve-modules.ts}
|
|
24
|
-
*
|
|
25
|
-
* @see {@linkcode MimeType}
|
|
26
|
-
* @see https://nodejs.org/api/esm.html#esm_data_imports
|
|
27
|
-
*
|
|
28
|
-
* @param {string} code - Code to convert
|
|
29
|
-
* @param {MimeType} [mime='text/javascript'] - MIME type
|
|
30
|
-
* @return {string} `code` as `data:` URL
|
|
31
|
-
* @throws {NodeError<TypeError>} If `code` or `mime` is not a string
|
|
32
|
-
*/
|
|
33
|
-
const toDataURL = (
|
|
34
|
-
code: string,
|
|
35
|
-
mime: MimeType = 'text/javascript'
|
|
36
|
-
): string => {
|
|
37
|
-
validateString(code, 'code')
|
|
38
|
-
validateString(mime, 'mime')
|
|
39
|
-
return `data:${mime};base64,${Buffer.from(code).toString('base64')}`
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export default toDataURL
|
package/src/utils/to-node-url.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file toNodeURL
|
|
3
|
-
* @module mlly/utils/toNodeURL
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import validateString from '#src/internal/validate-string'
|
|
7
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Converts the given module `specifier` to a `node:` URL.
|
|
11
|
-
*
|
|
12
|
-
* ::: warning
|
|
13
|
-
* Does not guarantee the new specifier is an existing Node.js builtin module.
|
|
14
|
-
* :::
|
|
15
|
-
*
|
|
16
|
-
* @see https://nodejs.org/api/esm.html#node-imports
|
|
17
|
-
*
|
|
18
|
-
* @param {string} specifier - Module specifier to convert
|
|
19
|
-
* @return {string} `specifier` as `node:` URL
|
|
20
|
-
* @throws {NodeError<TypeError>} If `specifier` is not a string
|
|
21
|
-
*/
|
|
22
|
-
const toNodeURL = (specifier: string): string => {
|
|
23
|
-
validateString(specifier, 'specifier')
|
|
24
|
-
return specifier.startsWith('node:') ? specifier : `node:${specifier}`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export default toNodeURL
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file toRelativeSpecifier
|
|
3
|
-
* @module mlly/utils/toRelativeSpecifier
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import validateURLString from '#src/internal/validate-url-string'
|
|
7
|
-
import type { ModuleId } from '#src/types'
|
|
8
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
9
|
-
import pathe from '@flex-development/pathe'
|
|
10
|
-
import { DOT } from '@flex-development/tutils'
|
|
11
|
-
import { URL, fileURLToPath } from 'node:url'
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Converts `specifier` into a relative specifier.
|
|
15
|
-
*
|
|
16
|
-
* ::: info
|
|
17
|
-
* The relative specifier will only include a file extension if `specifier`
|
|
18
|
-
* includes a file extension.
|
|
19
|
-
* :::
|
|
20
|
-
*
|
|
21
|
-
* @see {@linkcode ModuleId}
|
|
22
|
-
* @see https://nodejs.org/api/esm.html#terminology
|
|
23
|
-
*
|
|
24
|
-
* @param {ModuleId} specifier - Module specifier to convert
|
|
25
|
-
* @param {ModuleId} parent - Parent module URL or path to resolve from
|
|
26
|
-
* @return {string} `specifier` as relative specifier
|
|
27
|
-
* @throws {NodeError<TypeError>} If either `specifier` or `parent` is not a
|
|
28
|
-
* string or an instance of {@linkcode URL}
|
|
29
|
-
*/
|
|
30
|
-
const toRelativeSpecifier = (specifier: ModuleId, parent: ModuleId): string => {
|
|
31
|
-
validateURLString(specifier, 'specifier')
|
|
32
|
-
validateURLString(parent, 'parent')
|
|
33
|
-
|
|
34
|
-
// convert file url objects to file url strings
|
|
35
|
-
if (parent instanceof URL) parent = parent.href
|
|
36
|
-
if (specifier instanceof URL) specifier = specifier.href
|
|
37
|
-
|
|
38
|
-
// convert file url strings to paths
|
|
39
|
-
if (parent.startsWith('file:')) parent = fileURLToPath(parent)
|
|
40
|
-
if (specifier.startsWith('file:')) specifier = fileURLToPath(specifier)
|
|
41
|
-
|
|
42
|
-
// convert specifier to relative specifier
|
|
43
|
-
specifier = pathe
|
|
44
|
-
.relative(pathe.resolve(parent), pathe.resolve(specifier))
|
|
45
|
-
.replace(/^\.\.\/?/, '')
|
|
46
|
-
.replace(/^(\w)/, './$1')
|
|
47
|
-
|
|
48
|
-
// set specifier to dot character if empty string
|
|
49
|
-
// this occurs when specifier is a directory, but is not fully specified
|
|
50
|
-
if (!specifier) specifier = DOT
|
|
51
|
-
|
|
52
|
-
return specifier
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export default toRelativeSpecifier
|
package/src/utils/to-url.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file toURL
|
|
3
|
-
* @module mlly/utils/toURL
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import validateURLString from '#src/internal/validate-url-string'
|
|
7
|
-
import type { ModuleId } from '#src/types'
|
|
8
|
-
import type { NodeError } from '@flex-development/errnode'
|
|
9
|
-
import { URL, pathToFileURL } from 'node:url'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Converts the given module `id` to an instance of [`URL`][1].
|
|
13
|
-
*
|
|
14
|
-
* [1]: https://nodejs.org/api/url.html#class-url
|
|
15
|
-
*
|
|
16
|
-
* @see {@linkcode ModuleId}
|
|
17
|
-
*
|
|
18
|
-
* @param {ModuleId} id - Module id to evaluate
|
|
19
|
-
* @param {ModuleId?} [base=pathToFileURL('./')] - Base URL to resolve against
|
|
20
|
-
* @return {URL} `id` as instance of {@linkcode URL}
|
|
21
|
-
* @throws {NodeError<TypeError>} If either `id` or `base` is not a string or an
|
|
22
|
-
* instance of {@linkcode URL}
|
|
23
|
-
*/
|
|
24
|
-
const toURL = (id: ModuleId, base: ModuleId = pathToFileURL('./')): URL => {
|
|
25
|
-
validateURLString(id, 'id')
|
|
26
|
-
validateURLString(base, 'base')
|
|
27
|
-
return id instanceof URL ? id : new URL(id, base)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export default toURL
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file validateAssertions
|
|
3
|
-
* @module mlly/utils/validateAssertions
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { AssertType, type Format } from '#src/enums'
|
|
7
|
-
import type { ImportAssertions } from '#src/interfaces'
|
|
8
|
-
import FORMAT_TYPE_MAP from '#src/internal/format-type-map'
|
|
9
|
-
import validateObject from '#src/internal/validate-object'
|
|
10
|
-
import validateString from '#src/internal/validate-string'
|
|
11
|
-
import validateURLString from '#src/internal/validate-url-string'
|
|
12
|
-
import type { ModuleId } from '#src/types'
|
|
13
|
-
import {
|
|
14
|
-
ERR_IMPORT_ASSERTION_TYPE_FAILED,
|
|
15
|
-
ERR_IMPORT_ASSERTION_TYPE_MISSING,
|
|
16
|
-
ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED,
|
|
17
|
-
type NodeError
|
|
18
|
-
} from '@flex-development/errnode'
|
|
19
|
-
import {
|
|
20
|
-
cast,
|
|
21
|
-
hasOwn,
|
|
22
|
-
includes,
|
|
23
|
-
type LiteralUnion,
|
|
24
|
-
type Optional
|
|
25
|
-
} from '@flex-development/tutils'
|
|
26
|
-
import { URL } from 'node:url'
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Test a module's `import` assertions.
|
|
30
|
-
*
|
|
31
|
-
* @see {@linkcode Format}
|
|
32
|
-
* @see {@linkcode ImportAssertions}
|
|
33
|
-
* @see {@linkcode ModuleId}
|
|
34
|
-
*
|
|
35
|
-
* @param {ModuleId} url - Module URL of imported module (for error reporting)
|
|
36
|
-
* @param {LiteralUnion<Format, string>} format - Module format
|
|
37
|
-
* @param {ImportAssertions} [assertions={}] - `import` assertions
|
|
38
|
-
* @return {true} `true` if `assertions` are valid
|
|
39
|
-
* @throws {NodeError<TypeError>}
|
|
40
|
-
*/
|
|
41
|
-
const validateAssertions = (
|
|
42
|
-
url: ModuleId,
|
|
43
|
-
format: LiteralUnion<Format, string>,
|
|
44
|
-
assertions: ImportAssertions = {}
|
|
45
|
-
): true => {
|
|
46
|
-
validateURLString(url, 'url')
|
|
47
|
-
validateString(format, 'format')
|
|
48
|
-
validateObject(assertions, 'assertions')
|
|
49
|
-
|
|
50
|
-
// ensure url is a string
|
|
51
|
-
if (url instanceof URL) url = url.href
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* `type` property check for {@linkcode assertions}.
|
|
55
|
-
*
|
|
56
|
-
* @const {boolean} has_type
|
|
57
|
-
*/
|
|
58
|
-
const has_type: boolean = hasOwn(assertions, 'type')
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* `import` assertion type.
|
|
62
|
-
*
|
|
63
|
-
* @const {Optional<AssertType>} type
|
|
64
|
-
*/
|
|
65
|
-
const type: Optional<AssertType> = FORMAT_TYPE_MAP.get(cast(format))
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* `import` assertion error check.
|
|
69
|
-
*
|
|
70
|
-
* @var {boolean} err
|
|
71
|
-
*/
|
|
72
|
-
let err: boolean = false
|
|
73
|
-
|
|
74
|
-
// validate import assertions
|
|
75
|
-
switch (type) {
|
|
76
|
-
case undefined:
|
|
77
|
-
// ignore assertions for unrecognized module formats to allow for new
|
|
78
|
-
// formats in the future
|
|
79
|
-
break
|
|
80
|
-
case AssertType.IMPLICIT:
|
|
81
|
-
// format doesn't allow an import assertion type, so the `type` property
|
|
82
|
-
// must not be set on the import assertions object
|
|
83
|
-
if (has_type) err = true
|
|
84
|
-
break
|
|
85
|
-
case assertions.type:
|
|
86
|
-
// asserted type is compatible with format
|
|
87
|
-
break
|
|
88
|
-
default:
|
|
89
|
-
// format has an expected type, but asserted type is not compatible
|
|
90
|
-
err = true
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// throw if error was encountered
|
|
94
|
-
if (err) {
|
|
95
|
-
// import assertion type was not specified
|
|
96
|
-
if (!has_type) throw new ERR_IMPORT_ASSERTION_TYPE_MISSING(url, type!)
|
|
97
|
-
|
|
98
|
-
// import assertion type may have not been a string
|
|
99
|
-
validateString(assertions.type, 'assertions.type')
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Supported `import` assertion types.
|
|
103
|
-
*
|
|
104
|
-
* @const {AssertType[]} supported
|
|
105
|
-
*/
|
|
106
|
-
const supported: AssertType[] = []
|
|
107
|
-
|
|
108
|
-
// get supported import assertion types
|
|
109
|
-
for (const [, type] of FORMAT_TYPE_MAP) {
|
|
110
|
-
if (type === AssertType.IMPLICIT) continue
|
|
111
|
-
supported.push(type)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// throw if asserted type is not supported
|
|
115
|
-
if (!includes(supported, assertions.type)) {
|
|
116
|
-
throw new ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED(assertions.type!)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// unknown error
|
|
120
|
-
throw new ERR_IMPORT_ASSERTION_TYPE_FAILED(url, assertions.type!)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return true
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export default validateAssertions
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file validateExports
|
|
3
|
-
* @module mlly/utils/validateExports
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import validateURLString from '#src/internal/validate-url-string'
|
|
7
|
-
import type { ModuleId } from '#src/types'
|
|
8
|
-
import {
|
|
9
|
-
ERR_INVALID_PACKAGE_CONFIG,
|
|
10
|
-
type NodeError
|
|
11
|
-
} from '@flex-development/errnode'
|
|
12
|
-
import type { Exports } from '@flex-development/pkg-types'
|
|
13
|
-
import {
|
|
14
|
-
DOT,
|
|
15
|
-
at,
|
|
16
|
-
cast,
|
|
17
|
-
isArray,
|
|
18
|
-
isArrayIndex,
|
|
19
|
-
isNIL,
|
|
20
|
-
isObjectCurly,
|
|
21
|
-
isString,
|
|
22
|
-
type Optional
|
|
23
|
-
} from '@flex-development/tutils'
|
|
24
|
-
import { URL, fileURLToPath } from 'node:url'
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Validates the given package [`exports`][1] configuration and schema.
|
|
28
|
-
*
|
|
29
|
-
* [1]: https://nodejs.org/api/packages.html#exports
|
|
30
|
-
*
|
|
31
|
-
* @see {@linkcode Exports}
|
|
32
|
-
* @see {@linkcode ModuleId}
|
|
33
|
-
*
|
|
34
|
-
* @param {Optional<Exports>} exports - Package `exports`
|
|
35
|
-
* @param {ModuleId} pkg - URL of relevant `package.json` file
|
|
36
|
-
* @param {ModuleId} parent - URL of module to resolve from
|
|
37
|
-
* @return {true} `true` if `exports` configuration and schema are valid
|
|
38
|
-
* @throws {NodeError<Error | TypeError>} If either `pkg` or `parent` is not a
|
|
39
|
-
* string or an instance of {@linkcode URL}, or if `exports` configuration or
|
|
40
|
-
* schema is invalid
|
|
41
|
-
*/
|
|
42
|
-
const validateExports = (
|
|
43
|
-
exports: Optional<Exports>,
|
|
44
|
-
pkg: ModuleId,
|
|
45
|
-
parent: ModuleId
|
|
46
|
-
): true => {
|
|
47
|
-
validateURLString(pkg, 'pkg')
|
|
48
|
-
validateURLString(parent, 'parent')
|
|
49
|
-
|
|
50
|
-
switch (true) {
|
|
51
|
-
case isArray(exports):
|
|
52
|
-
for (const item of cast<unknown[]>(exports)) {
|
|
53
|
-
switch (true) {
|
|
54
|
-
case !isObjectCurly(item) && !isString(item):
|
|
55
|
-
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
56
|
-
fileURLToPath(pkg),
|
|
57
|
-
fileURLToPath(parent),
|
|
58
|
-
'The "exports" array must contain either objects of package subpath keys, objects of condition name keys, or strings'
|
|
59
|
-
)
|
|
60
|
-
default:
|
|
61
|
-
validateExports(
|
|
62
|
-
cast<Record<string, Exports> | string>(item),
|
|
63
|
-
pkg,
|
|
64
|
-
parent
|
|
65
|
-
)
|
|
66
|
-
break
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
break
|
|
71
|
-
case isNIL(exports):
|
|
72
|
-
case isString(exports):
|
|
73
|
-
break
|
|
74
|
-
case !isObjectCurly(exports):
|
|
75
|
-
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
76
|
-
fileURLToPath(pkg),
|
|
77
|
-
fileURLToPath(parent),
|
|
78
|
-
'"exports" must be null, undefined, a string, an object of package subpath keys or condition name keys, or an array containing objects of package subpath keys, objects of condition name keys, or strings'
|
|
79
|
-
)
|
|
80
|
-
default:
|
|
81
|
-
exports = cast<Record<string, Exports>>(exports)
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Keys defined in {@linkcode exports}.
|
|
85
|
-
*
|
|
86
|
-
* @const {string[]} keys
|
|
87
|
-
*/
|
|
88
|
-
const keys: string[] = Object.getOwnPropertyNames(exports)
|
|
89
|
-
|
|
90
|
-
// validate keys
|
|
91
|
-
for (const [index, key] of keys.entries()) {
|
|
92
|
-
// ensure key is not an array index
|
|
93
|
-
if (isArrayIndex(key)) {
|
|
94
|
-
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
95
|
-
fileURLToPath(new URL(DOT, pkg)),
|
|
96
|
-
fileURLToPath(parent),
|
|
97
|
-
'"exports" cannot contain numeric property keys'
|
|
98
|
-
)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// if more than one key in keys, ensure all keys are either syntactical
|
|
102
|
-
// export conditions or subpath exports
|
|
103
|
-
if (index) {
|
|
104
|
-
/**
|
|
105
|
-
* Key before {@linkcode key} in {@linkcode keys}.
|
|
106
|
-
*
|
|
107
|
-
* @const {string} prev
|
|
108
|
-
*/
|
|
109
|
-
const prev: string = at(keys, index - 1)
|
|
110
|
-
|
|
111
|
-
// ensure keys are syntactical export conditions or subpath exports
|
|
112
|
-
if (
|
|
113
|
-
(!key.startsWith(DOT) && prev.startsWith(DOT)) ||
|
|
114
|
-
(key.startsWith(DOT) && !prev.startsWith(DOT))
|
|
115
|
-
) {
|
|
116
|
-
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
117
|
-
fileURLToPath(pkg),
|
|
118
|
-
fileURLToPath(parent),
|
|
119
|
-
'"exports" cannot contain some keys starting with \'.\' and some not. The exports object must either be an object of package subpath keys or an object of main entry condition name keys only.'
|
|
120
|
-
)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
break
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return true
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export default validateExports
|