@flex-development/mlly 1.0.0-alpha.16 → 1.0.0-alpha.17

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.
Files changed (175) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/index.mjs.map +4 -5
  3. package/dist/internal/dequote.mjs +1 -0
  4. package/dist/internal/dequote.mjs.map +8 -0
  5. package/dist/internal/format-type-map.mjs +1 -0
  6. package/dist/internal/format-type-map.mjs.map +8 -0
  7. package/dist/internal/get-specifier-kind.mjs +1 -0
  8. package/dist/internal/get-specifier-kind.mjs.map +8 -0
  9. package/dist/internal/get-subpaths.mjs +1 -0
  10. package/dist/internal/get-subpaths.mjs.map +8 -0
  11. package/dist/internal/regex-encoded-sep.mjs +1 -0
  12. package/dist/internal/regex-encoded-sep.mjs.map +8 -0
  13. package/dist/internal/regex-internal-specifier.mjs +1 -0
  14. package/dist/internal/regex-internal-specifier.mjs.map +8 -0
  15. package/dist/internal/regex-invalid-segment.mjs +1 -0
  16. package/dist/internal/regex-invalid-segment.mjs.map +8 -0
  17. package/dist/internal/regex-package-name.mjs +1 -0
  18. package/dist/internal/regex-package-name.mjs.map +8 -0
  19. package/dist/internal/regex-package-path.mjs +1 -0
  20. package/dist/internal/regex-package-path.mjs.map +8 -0
  21. package/dist/internal/resolver.mjs +1 -0
  22. package/dist/internal/resolver.mjs.map +8 -0
  23. package/dist/internal/validate-array-set.mjs +1 -0
  24. package/dist/internal/validate-array-set.mjs.map +8 -0
  25. package/dist/internal/validate-boolean.mjs +1 -0
  26. package/dist/internal/validate-boolean.mjs.map +8 -0
  27. package/dist/internal/validate-map.mjs +1 -0
  28. package/dist/internal/validate-map.mjs.map +8 -0
  29. package/dist/internal/validate-object.mjs +1 -0
  30. package/dist/internal/validate-object.mjs.map +8 -0
  31. package/dist/internal/validate-set.mjs +1 -0
  32. package/dist/internal/validate-set.mjs.map +8 -0
  33. package/dist/internal/validate-string.mjs +1 -0
  34. package/dist/internal/validate-string.mjs.map +8 -0
  35. package/dist/internal/validate-url-string.mjs +1 -0
  36. package/dist/internal/validate-url-string.mjs.map +8 -0
  37. package/dist/utils/compare-subpaths.mjs.map +4 -5
  38. package/dist/utils/conditions.mjs.map +4 -5
  39. package/dist/utils/detect-syntax.mjs.map +4 -5
  40. package/dist/utils/extension-format-map.mjs.map +4 -5
  41. package/dist/utils/extract-statements.mjs.map +4 -5
  42. package/dist/utils/fill-modules.mjs.map +5 -8
  43. package/dist/utils/find-dynamic-imports.mjs.map +5 -8
  44. package/dist/utils/find-exports.mjs.map +5 -8
  45. package/dist/utils/find-requires.mjs.map +5 -8
  46. package/dist/utils/find-static-imports.mjs.map +5 -8
  47. package/dist/utils/find-subpath.mjs.map +5 -9
  48. package/dist/utils/get-format.mjs.map +5 -8
  49. package/dist/utils/get-source.mjs.map +4 -5
  50. package/dist/utils/has-cjs-syntax.mjs.map +4 -5
  51. package/dist/utils/has-esm-syntax.mjs.map +4 -5
  52. package/dist/utils/index.mjs.map +5 -8
  53. package/dist/utils/is-absolute-specifier.mjs.map +4 -5
  54. package/dist/utils/is-bare-specifier.mjs.map +4 -5
  55. package/dist/utils/is-directory.mjs.map +4 -5
  56. package/dist/utils/is-exports-sugar.mjs.map +4 -5
  57. package/dist/utils/is-file.mjs.map +4 -5
  58. package/dist/utils/is-relative-specifier.mjs.map +4 -5
  59. package/dist/utils/lookup-package-scope.mjs.map +4 -5
  60. package/dist/utils/parse-data-url.mjs.map +4 -5
  61. package/dist/utils/parse-module-id.mjs.map +5 -9
  62. package/dist/utils/parse-subpath.mjs.map +5 -8
  63. package/dist/utils/pattern-character.mjs.map +4 -5
  64. package/dist/utils/read-package-json.mjs.map +4 -5
  65. package/dist/utils/resolve-alias.mjs.map +5 -9
  66. package/dist/utils/resolve-aliases.mjs.map +4 -5
  67. package/dist/utils/resolve-extensions.mjs.map +4 -5
  68. package/dist/utils/resolve-module.mjs.map +4 -5
  69. package/dist/utils/resolve-modules.mjs.map +4 -5
  70. package/dist/utils/to-absolute-specifier.mjs.map +4 -5
  71. package/dist/utils/to-bare-specifier.mjs.map +4 -5
  72. package/dist/utils/to-data-url.mjs.map +4 -5
  73. package/dist/utils/to-node-url.mjs.map +4 -5
  74. package/dist/utils/to-relative-specifier.mjs.map +4 -5
  75. package/dist/utils/to-url.mjs.map +4 -5
  76. package/dist/utils/validate-assertions.mjs.map +5 -8
  77. package/dist/utils/validate-exports.mjs.map +4 -5
  78. package/package.json +8 -9
  79. package/src/enums/assert-type.ts +0 -23
  80. package/src/enums/format.ts +0 -19
  81. package/src/enums/index.ts +0 -11
  82. package/src/enums/kind-specifier-syntax.ts +0 -16
  83. package/src/enums/kind-specifier.ts +0 -19
  84. package/src/enums/kind-statement-syntax.ts +0 -24
  85. package/src/enums/kind-statement.ts +0 -17
  86. package/src/index.ts +0 -9
  87. package/src/interfaces/import-assertions.ts +0 -24
  88. package/src/interfaces/import-dynamic.ts +0 -46
  89. package/src/interfaces/import-static.ts +0 -46
  90. package/src/interfaces/index.ts +0 -25
  91. package/src/interfaces/options-fill-module.ts +0 -26
  92. package/src/interfaces/options-find-subpath.ts +0 -57
  93. package/src/interfaces/options-get-format.ts +0 -80
  94. package/src/interfaces/options-get-source.ts +0 -59
  95. package/src/interfaces/options-parse-module-id.ts +0 -39
  96. package/src/interfaces/options-parse-subpath.ts +0 -57
  97. package/src/interfaces/options-resolve-alias.ts +0 -59
  98. package/src/interfaces/options-resolve-module.ts +0 -72
  99. package/src/interfaces/options-resolve.ts +0 -17
  100. package/src/interfaces/package-scope.ts +0 -30
  101. package/src/interfaces/parsed-data-url.ts +0 -63
  102. package/src/interfaces/parsed-module-id.ts +0 -90
  103. package/src/interfaces/parsed-subpath.ts +0 -56
  104. package/src/interfaces/statement-export.ts +0 -52
  105. package/src/interfaces/statement-import.ts +0 -45
  106. package/src/interfaces/statement-require.ts +0 -45
  107. package/src/interfaces/statement.ts +0 -64
  108. package/src/internal/dequote.ts +0 -18
  109. package/src/internal/format-type-map.ts +0 -28
  110. package/src/internal/get-specifier-kind.ts +0 -41
  111. package/src/internal/get-subpaths.ts +0 -43
  112. package/src/internal/regex-encoded-sep.ts +0 -17
  113. package/src/internal/regex-internal-specifier.ts +0 -19
  114. package/src/internal/regex-invalid-segment.ts +0 -25
  115. package/src/internal/regex-package-name.ts +0 -18
  116. package/src/internal/regex-package-path.ts +0 -20
  117. package/src/internal/resolver.ts +0 -790
  118. package/src/internal/validate-array-set.ts +0 -35
  119. package/src/internal/validate-boolean.ts +0 -30
  120. package/src/internal/validate-map.ts +0 -36
  121. package/src/internal/validate-object.ts +0 -30
  122. package/src/internal/validate-set.ts +0 -32
  123. package/src/internal/validate-string.ts +0 -31
  124. package/src/internal/validate-url-string.ts +0 -34
  125. package/src/types/declaration.ts +0 -22
  126. package/src/types/fn-change-ext.ts +0 -33
  127. package/src/types/index.ts +0 -14
  128. package/src/types/mime-type.ts +0 -19
  129. package/src/types/module-id.ts +0 -15
  130. package/src/types/module-specifier-type.ts +0 -13
  131. package/src/types/protocol.ts +0 -40
  132. package/src/types/syntax-kind-export.ts +0 -20
  133. package/src/types/syntax-kind-import.ts +0 -22
  134. package/src/types/syntax-kind-require.ts +0 -15
  135. package/src/utils/compare-subpaths.ts +0 -100
  136. package/src/utils/conditions.ts +0 -15
  137. package/src/utils/detect-syntax.ts +0 -40
  138. package/src/utils/extension-format-map.ts +0 -34
  139. package/src/utils/extract-statements.ts +0 -46
  140. package/src/utils/fill-modules.ts +0 -133
  141. package/src/utils/find-dynamic-imports.ts +0 -75
  142. package/src/utils/find-exports.ts +0 -156
  143. package/src/utils/find-requires.ts +0 -75
  144. package/src/utils/find-static-imports.ts +0 -86
  145. package/src/utils/find-subpath.ts +0 -251
  146. package/src/utils/get-format.ts +0 -249
  147. package/src/utils/get-source.ts +0 -144
  148. package/src/utils/has-cjs-syntax.ts +0 -44
  149. package/src/utils/has-esm-syntax.ts +0 -40
  150. package/src/utils/index.ts +0 -45
  151. package/src/utils/is-absolute-specifier.ts +0 -53
  152. package/src/utils/is-bare-specifier.ts +0 -36
  153. package/src/utils/is-directory.ts +0 -25
  154. package/src/utils/is-exports-sugar.ts +0 -81
  155. package/src/utils/is-file.ts +0 -25
  156. package/src/utils/is-relative-specifier.ts +0 -34
  157. package/src/utils/lookup-package-scope.ts +0 -97
  158. package/src/utils/parse-data-url.ts +0 -71
  159. package/src/utils/parse-module-id.ts +0 -197
  160. package/src/utils/parse-subpath.ts +0 -331
  161. package/src/utils/pattern-character.ts +0 -13
  162. package/src/utils/read-package-json.ts +0 -113
  163. package/src/utils/resolve-alias.ts +0 -224
  164. package/src/utils/resolve-aliases.ts +0 -58
  165. package/src/utils/resolve-extensions.ts +0 -33
  166. package/src/utils/resolve-module.ts +0 -181
  167. package/src/utils/resolve-modules.ts +0 -60
  168. package/src/utils/to-absolute-specifier.ts +0 -30
  169. package/src/utils/to-bare-specifier.ts +0 -211
  170. package/src/utils/to-data-url.ts +0 -42
  171. package/src/utils/to-node-url.ts +0 -27
  172. package/src/utils/to-relative-specifier.ts +0 -55
  173. package/src/utils/to-url.ts +0 -30
  174. package/src/utils/validate-assertions.ts +0 -126
  175. package/src/utils/validate-exports.ts +0 -131
@@ -1,40 +0,0 @@
1
- /**
2
- * @file hasESMSyntax
3
- * @module mlly/utils/hasESMSyntax
4
- */
5
-
6
- import validateString from '#src/internal/validate-string'
7
- import type { NodeError } from '@flex-development/errnode'
8
-
9
- /**
10
- * Detects if `code` contains ESM syntax. Ignores matches in comments.
11
- *
12
- * Syntax patterns:
13
- *
14
- * - `export` (declaration, default, named, star)
15
- * - `import` (default, dynamic, named, star)
16
- * - `import.meta.(env|resolve|url)`
17
- *
18
- * @see https://regex101.com/r/xfreyy
19
- *
20
- * @param {string} code - Code to evaluate
21
- * @return {boolean} `true` if `code` contains esm syntax, `false` otherwise
22
- * @throws {NodeError<TypeError>} If `code` is not a string
23
- */
24
- const hasESMSyntax = (code: string): boolean => {
25
- validateString(code, 'code')
26
-
27
- /**
28
- * ESM syntax regex.
29
- *
30
- * @see https://regex101.com/r/xfreyy
31
- *
32
- * @const {RegExp} ESM_SYNTAX_REGEX
33
- */
34
- const ESM_SYNTAX_REGEX: RegExp =
35
- /(?<!(?:\/\/|\*).*)((?:export|import)[\s\w*,{}]*(?=\sfrom)|export\b\s*(?:[*{]|async function|(?:abstract\s)?class|const|default|enum|function|interface|let|type|var)|await import|import\.meta\.(?:env(?:\.\w+)?|resolve|url))/gm
36
-
37
- return !!code.match(ESM_SYNTAX_REGEX)
38
- }
39
-
40
- export default hasESMSyntax
@@ -1,45 +0,0 @@
1
- /**
2
- * @file Utilities
3
- * @module mlly/utils
4
- */
5
-
6
- export { default as compareSubpaths } from './compare-subpaths'
7
- export { default as CONDITIONS } from './conditions'
8
- export { default as detectSyntax } from './detect-syntax'
9
- export { default as EXTENSION_FORMAT_MAP } from './extension-format-map'
10
- export { default as extractStatements } from './extract-statements'
11
- export { default as fillModules } from './fill-modules'
12
- export { default as findDynamicImports } from './find-dynamic-imports'
13
- export { default as findExports } from './find-exports'
14
- export { default as findRequires } from './find-requires'
15
- export { default as findStaticImports } from './find-static-imports'
16
- export { default as findSubpath } from './find-subpath'
17
- export { default as getFormat } from './get-format'
18
- export { default as getSource } from './get-source'
19
- export { default as hasCJSSyntax } from './has-cjs-syntax'
20
- export { default as hasESMSyntax } from './has-esm-syntax'
21
- export { default as isAbsoluteSpecifier } from './is-absolute-specifier'
22
- export { default as isBareSpecifier } from './is-bare-specifier'
23
- export { default as isDirectory } from './is-directory'
24
- export { default as isExportsSugar } from './is-exports-sugar'
25
- export { default as isFile } from './is-file'
26
- export { default as isRelativeSpecifier } from './is-relative-specifier'
27
- export { default as lookupPackageScope } from './lookup-package-scope'
28
- export { default as parseDataURL } from './parse-data-url'
29
- export { default as parseModuleId } from './parse-module-id'
30
- export { default as parseSubpath } from './parse-subpath'
31
- export { default as PATTERN_CHARACTER } from './pattern-character'
32
- export { default as readPackageJson } from './read-package-json'
33
- export { default as resolveAlias } from './resolve-alias'
34
- export { default as resolveAliases } from './resolve-aliases'
35
- export { default as RESOLVE_EXTENSIONS } from './resolve-extensions'
36
- export { default as resolveModule } from './resolve-module'
37
- export { default as resolveModules } from './resolve-modules'
38
- export { default as toAbsoluteSpecifier } from './to-absolute-specifier'
39
- export { default as toBareSpecifier } from './to-bare-specifier'
40
- export { default as toDataURL } from './to-data-url'
41
- export { default as toNodeURL } from './to-node-url'
42
- export { default as toRelativeSpecifier } from './to-relative-specifier'
43
- export { default as toURL } from './to-url'
44
- export { default as validateAssertions } from './validate-assertions'
45
- export { default as validateExports } from './validate-exports'
@@ -1,53 +0,0 @@
1
- /**
2
- * @file isAbsoluteSpecifier
3
- * @module mlly/utils/isAbsoluteSpecifier
4
- */
5
-
6
- import validateString from '#src/internal/validate-string'
7
- import type { NodeError } from '@flex-development/errnode'
8
- import pathe from '@flex-development/pathe'
9
- import { URL } from 'node:url'
10
-
11
- /**
12
- * Checks if the given `specifier` is an absolute specifier.
13
- *
14
- * ::: warning
15
- * Only checks specifier syntax. Does **not** guarantee the specifier references
16
- * a file that exists.
17
- * :::
18
- *
19
- * @see https://nodejs.org/api/esm.html#terminology
20
- *
21
- * @param {string} specifier - Specifier to evaluate
22
- * @return {boolean} `true` if `specifier` is absolute specifier
23
- * @throws {NodeError<TypeError>} If `specifier` is not a string
24
- */
25
- const isAbsoluteSpecifier = (specifier: string): boolean => {
26
- validateString(specifier, 'specifier')
27
-
28
- /**
29
- * Absolute specifier check.
30
- *
31
- * @var {string} absolute
32
- */
33
- let absolute: boolean = false
34
-
35
- // check if specifier is absolute path or valid file url
36
- switch (true) {
37
- case pathe.isAbsolute(specifier):
38
- absolute = true
39
- break
40
- case specifier.startsWith('file:'):
41
- try {
42
- new URL(specifier)
43
- absolute = true
44
- } /* c8 ignore next */ catch {}
45
- break
46
- default:
47
- absolute = false
48
- }
49
-
50
- return absolute
51
- }
52
-
53
- export default isAbsoluteSpecifier
@@ -1,36 +0,0 @@
1
- /**
2
- * @file isBareSpecifier
3
- * @module mlly/utils/isBareSpecifier
4
- */
5
-
6
- import validateString from '#src/internal/validate-string'
7
- import type { NodeError } from '@flex-development/errnode'
8
- import { trim } from '@flex-development/tutils'
9
- import isAbsoluteSpecifier from './is-absolute-specifier'
10
- import isRelativeSpecifier from './is-relative-specifier'
11
-
12
- /**
13
- * Checks if the given `specifier` is a bare specifier.
14
- *
15
- * ::: warning
16
- * Only checks specifier syntax. Does **not** guarantee the specifier references
17
- * a file that exists.
18
- * :::
19
- *
20
- * @see https://nodejs.org/api/esm.html#terminology
21
- *
22
- * @param {string} specifier - Specifier to evaluate
23
- * @return {boolean} `true` if `specifier` is bare specifier
24
- * @throws {NodeError<TypeError>} If `specifier` is not a string
25
- */
26
- const isBareSpecifier = (specifier: string): boolean => {
27
- validateString(specifier, 'specifier')
28
-
29
- return (
30
- !!trim(specifier).length &&
31
- !isAbsoluteSpecifier(specifier) &&
32
- !isRelativeSpecifier(specifier)
33
- )
34
- }
35
-
36
- export default isBareSpecifier
@@ -1,25 +0,0 @@
1
- /**
2
- * @file Internal - isDirectory
3
- * @module mlly/internal/isDirectory
4
- */
5
-
6
- import validateURLString from '#src/internal/validate-url-string'
7
- import type { ModuleId } from '#src/types'
8
- import fs from 'node:fs'
9
-
10
- /**
11
- * Checks if a directory exists at the given module `id`.
12
- *
13
- * @param {ModuleId} id - Module id to evaluate
14
- * @return {boolean} `true` if directory exists at `id`
15
- */
16
- const isDirectory = (id: ModuleId): boolean => {
17
- try {
18
- validateURLString(id, 'id')
19
- return fs.statSync(id, { throwIfNoEntry: false })?.isDirectory() ?? false
20
- } catch {
21
- return false
22
- }
23
- }
24
-
25
- export default isDirectory
@@ -1,81 +0,0 @@
1
- /**
2
- * @file isExportsSugar
3
- * @module mlly/utils/isExportsSugar
4
- */
5
-
6
- import type { ModuleId } from '#src/types'
7
- import type { NodeError } from '@flex-development/errnode'
8
- import type { Exports } from '@flex-development/pkg-types'
9
- import {
10
- at,
11
- cast,
12
- DOT,
13
- isArray,
14
- isObjectCurly,
15
- isString,
16
- type Nilable
17
- } from '@flex-development/tutils'
18
- import validateExports from './validate-exports'
19
-
20
- /**
21
- * Checks if the given package [`exports`][1] value is using [exports sugar][2].
22
- *
23
- * [1]: https://nodejs.org/api/packages.html#exports
24
- * [2]: https://nodejs.org/api/packages.html#exports-sugar
25
- *
26
- * @see {@linkcode Exports}
27
- * @see {@linkcode ModuleId}
28
- *
29
- * @param {Nilable<Exports>} exports - Package `exports`
30
- * @param {ModuleId} pkg - URL of relevant `package.json` file
31
- * @param {ModuleId} parent - URL of module to resolve from
32
- * @return {boolean} `true` if `exports` is using exports sugar
33
- * @throws {NodeError<Error | TypeError>} If either `pkg` or `parent` is not an
34
- * instance of {@linkcode URL} or a string, or if `exports` configuration or
35
- * schema is invalid
36
- */
37
- const isExportsSugar = (
38
- exports: Nilable<Exports>,
39
- pkg: ModuleId,
40
- parent: ModuleId
41
- ): boolean => {
42
- /**
43
- * [Exports sugar][1] check.
44
- *
45
- * [1]: https://nodejs.org/api/packages.html#exports-sugar
46
- *
47
- * @var {boolean} sugar
48
- */
49
- let sugar: boolean = false
50
-
51
- // check if exports sugar is being used
52
- switch (true) {
53
- case isArray(exports):
54
- case isString(exports):
55
- sugar = true
56
- break
57
- case !isObjectCurly(exports):
58
- sugar = false
59
- break
60
- default:
61
- exports = cast<Record<string, Exports>>(exports)
62
-
63
- /*
64
- * Validate exports object configuration.
65
- *
66
- * This ensures the exports object is either an object of package subpath
67
- * keys (i.e. keys starting with '.') or an object of main entry condition
68
- * name keys (i.e. keys not starting with '.') only. An error will be
69
- * thrown otherwise. This also means only the first key in `keys` needs to
70
- * be checked for exports sugar usage.
71
- */
72
- validateExports(exports, pkg, parent)
73
-
74
- // check for exports sugar
75
- sugar = !at(Object.getOwnPropertyNames(exports), 0, DOT).startsWith(DOT)
76
- }
77
-
78
- return sugar
79
- }
80
-
81
- export default isExportsSugar
@@ -1,25 +0,0 @@
1
- /**
2
- * @file Internal - isFile
3
- * @module mlly/internal/isFile
4
- */
5
-
6
- import validateURLString from '#src/internal/validate-url-string'
7
- import type { ModuleId } from '#src/types'
8
- import fs from 'node:fs'
9
-
10
- /**
11
- * Checks if a file exists at the given module `id`.
12
- *
13
- * @param {ModuleId} id - Module id to evaluate
14
- * @return {boolean} `true` if file exists at `id`
15
- */
16
- const isFile = (id: ModuleId): boolean => {
17
- try {
18
- validateURLString(id, 'id')
19
- return fs.statSync(id, { throwIfNoEntry: false })?.isFile() ?? false
20
- } catch {
21
- return false
22
- }
23
- }
24
-
25
- export default isFile
@@ -1,34 +0,0 @@
1
- /**
2
- * @file isRelativeSpecifier
3
- * @module mlly/utils/isRelativeSpecifier
4
- */
5
-
6
- import validateString from '#src/internal/validate-string'
7
- import pathe from '@flex-development/pathe'
8
- import { DOT, at } from '@flex-development/tutils'
9
-
10
- /**
11
- * Checks if the given `specifier` is a relative specifier.
12
- *
13
- * ::: warning
14
- * Only checks specifier syntax. Does **not** guarantee the specifier references
15
- * a file that exists.
16
- * :::
17
- *
18
- * @see https://nodejs.org/api/esm.html#terminology
19
- *
20
- * @param {string} specifier - Specifier to evaluate
21
- * @return {boolean} `true` if `specifier` is relative specifier
22
- */
23
- const isRelativeSpecifier = (specifier: string): boolean => {
24
- validateString(specifier, 'specifier')
25
-
26
- return specifier.startsWith(DOT)
27
- ? specifier.length === 1 || at(specifier, 1) === pathe.sep
28
- ? true
29
- : at(specifier, 1) === DOT &&
30
- (specifier.length === 2 || at(specifier, 2) === pathe.sep)
31
- : false
32
- }
33
-
34
- export default isRelativeSpecifier
@@ -1,97 +0,0 @@
1
- /**
2
- * @file lookupPackageScope
3
- * @module mlly/utils/lookupPackageScope
4
- */
5
-
6
- import type { PackageScope } from '#src/interfaces'
7
- import validateURLString from '#src/internal/validate-url-string'
8
- import type { ModuleId } from '#src/types'
9
- import type { NodeError } from '@flex-development/errnode'
10
- import pathe from '@flex-development/pathe'
11
- import type { PackageJson } from '@flex-development/pkg-types'
12
- import { DOT, type Nullable } from '@flex-development/tutils'
13
- import { pathToFileURL } from 'node:url'
14
- import readPackageJson from './read-package-json'
15
- import toURL from './to-url'
16
-
17
- /**
18
- * Finds a `package.json` file for the given module `id`. Ends the search at the
19
- * given end point **after** checking for a `package.json` file.
20
- *
21
- * Returns `null` if a `package.json` file is not found.
22
- *
23
- * @see {@linkcode ModuleId}
24
- * @see {@linkcode PackageScope}
25
- *
26
- * @param {ModuleId} id - Id of module to get package scope for
27
- * @param {ModuleId?} [stopdir=pathe.sep] - Directory to end search
28
- * @param {string?} [specifier] - Module specifier passed by user
29
- * @param {ModuleId?} [parent] - Id of module to resolve from
30
- * @return {?PackageScope} Package scope result or `null`
31
- * @throws {NodeError<TypeError>} If either `id` or `stopdir` is not an instance
32
- * of {@linkcode URL} or a string
33
- */
34
- const lookupPackageScope = (
35
- id: ModuleId,
36
- stopdir: ModuleId = pathe.sep,
37
- specifier?: string,
38
- parent?: ModuleId
39
- ): Nullable<PackageScope> => {
40
- validateURLString(id, 'id')
41
- validateURLString(stopdir, 'stopdir')
42
-
43
- // ensure search endpoint is a path
44
- stopdir = toURL(stopdir).pathname
45
-
46
- /**
47
- * Path to directory containing `package.json` file.
48
- *
49
- * @var {string} dir
50
- */
51
- let dir: string = toURL(id).pathname
52
-
53
- /**
54
- * Package scope result.
55
- *
56
- * @var {Nullable<PackageScope>} scope
57
- */
58
- let scope: Nullable<PackageScope> = null
59
-
60
- // search for package.json
61
- while (dir && !dir.endsWith('node_modules')) {
62
- // stop search if outside of endpoint
63
- if (pathe.relative(stopdir, dir).startsWith(DOT.repeat(2) + '/')) break
64
-
65
- /**
66
- * Possible `package.json` object.
67
- *
68
- * @const {Nullable<PackageJson>} pkgjson
69
- */
70
- const pkgjson: Nullable<PackageJson> = readPackageJson(
71
- dir,
72
- specifier,
73
- parent
74
- )
75
-
76
- // stop search if package.json has been found
77
- if (pkgjson) {
78
- scope = {
79
- dir: pathToFileURL((dir = pathe.resolve(process.cwd(), dir))).href,
80
- pkg: pathToFileURL(pathe.join(dir, 'package.json')).href,
81
- pkgjson
82
- }
83
-
84
- break
85
- }
86
-
87
- // stop search if endpoint has been reached
88
- if (dir === stopdir.replace(/\/$/, '')) break
89
-
90
- // continue searching for package.json
91
- dir = pathe.dirname(dir)
92
- }
93
-
94
- return scope
95
- }
96
-
97
- export default lookupPackageScope
@@ -1,71 +0,0 @@
1
- /**
2
- * @file parseDataURL
3
- * @module mlly/utils/parseDataURL
4
- */
5
-
6
- import type { ParsedDataUrl } from '#src/interfaces'
7
- import {
8
- ERR_INVALID_URL,
9
- type ErrInvalidUrl,
10
- type NodeError
11
- } from '@flex-development/errnode'
12
- import { cast, trim, type Nullable } from '@flex-development/tutils'
13
- import type { URL } from 'node:url'
14
- import toURL from './to-url'
15
-
16
- /**
17
- * Parses a [`data:` URL][1].
18
- *
19
- * If valid, returns an object representing the given `url`.
20
- *
21
- * **Note**: A [MIME type][2] string is required.
22
- *
23
- * [1]: https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URLs
24
- * [2]: https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/MIME_types
25
- *
26
- * @see https://nodejs.org/api/esm.html#data-imports
27
- *
28
- * @param {URL | string} url - URL to evaluate
29
- * @return {ParsedDataUrl} Object representing `url`
30
- * @throws {ErrInvalidUrl | NodeError<TypeError>} If `url` is not a string or
31
- * instance of {@linkcode URL}, or if `url` not a valid `data:` URL
32
- */
33
- const parseDataURL = (url: URL | string): ParsedDataUrl => {
34
- // ensure url is an instance of URL
35
- url = toURL(url)
36
-
37
- /**
38
- * Regex to extract `data:` URL components.
39
- *
40
- * @see https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URLs
41
- * @see https://regex101.com/r/Ie8Doq
42
- *
43
- * @const {RegExp} regex
44
- */
45
- const regex: RegExp =
46
- /^(?<protocol>data:)(?<mime>[^/]+\/[^,;]+)[^,]*?(?:;(?<base64>base64))?,(?<data>[\S\s]*?)$/
47
-
48
- /**
49
- * `data:` URL match.
50
- *
51
- * @const {Nullable<RegExpExecArray>} match
52
- */
53
- const match: Nullable<RegExpExecArray> = regex.exec(url.href)
54
-
55
- // throw if url is not data url
56
- if (!match) throw new ERR_INVALID_URL(url.href)
57
-
58
- // get data url components
59
- const [, , mime = '', base64, data = ''] = match
60
-
61
- return {
62
- base64: !!base64,
63
- data: trim(data),
64
- href: url.href,
65
- mime,
66
- pathname: url.pathname,
67
- protocol: cast(url.protocol)
68
- }
69
- }
70
-
71
- export default parseDataURL