@flex-development/mlly 1.0.0-alpha.8 → 1.0.0-beta.1

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 (332) hide show
  1. package/CHANGELOG.md +1128 -355
  2. package/README.md +1540 -12
  3. package/dist/index.d.mts +3 -4
  4. package/dist/index.mjs +5 -5
  5. package/dist/interfaces/aliases.d.mts +21 -0
  6. package/dist/interfaces/condition-map.d.mts +24 -0
  7. package/dist/interfaces/file-system.d.mts +29 -0
  8. package/dist/interfaces/get-source-context.d.mts +44 -0
  9. package/dist/interfaces/get-source-options.d.mts +48 -0
  10. package/dist/interfaces/index.d.mts +18 -21
  11. package/dist/interfaces/is-directory.d.mts +17 -0
  12. package/dist/interfaces/is-file.d.mts +17 -0
  13. package/dist/interfaces/main-field-map.d.mts +22 -0
  14. package/dist/interfaces/module-format-map.d.mts +26 -0
  15. package/dist/interfaces/pattern-key-comparison-map.d.mts +31 -0
  16. package/dist/interfaces/protocol-map.d.mts +44 -0
  17. package/dist/interfaces/read-file.d.mts +24 -0
  18. package/dist/interfaces/realpath.d.mts +26 -0
  19. package/dist/interfaces/resolve-alias-options.d.mts +43 -0
  20. package/dist/interfaces/resolve-module-options.d.mts +79 -0
  21. package/dist/interfaces/stat.d.mts +24 -0
  22. package/dist/interfaces/stats.d.mts +23 -0
  23. package/dist/internal/chain-or-call.mjs +34 -0
  24. package/dist/internal/chars.mjs +17 -0
  25. package/dist/internal/check-invalid-segments.mjs +49 -0
  26. package/dist/internal/constant.mjs +23 -0
  27. package/dist/internal/fs.browser.mjs +42 -0
  28. package/dist/internal/fs.node.mjs +16 -0
  29. package/dist/internal/identity.mjs +23 -0
  30. package/dist/internal/invalid-package-target.mjs +36 -0
  31. package/dist/internal/invalid-subpath.mjs +37 -0
  32. package/dist/internal/is-promise.mjs +26 -0
  33. package/dist/internal/process.browser.mjs +8 -0
  34. package/dist/lib/can-parse-url.d.mts +23 -0
  35. package/dist/lib/can-parse-url.mjs +31 -0
  36. package/dist/lib/cwd.d.mts +14 -0
  37. package/dist/lib/cwd.mjs +17 -0
  38. package/dist/lib/default-conditions.d.mts +15 -0
  39. package/dist/lib/default-conditions.mjs +14 -0
  40. package/dist/lib/default-extensions.d.mts +14 -0
  41. package/dist/lib/default-extensions.mjs +29 -0
  42. package/dist/lib/default-main-fields.d.mts +14 -0
  43. package/dist/lib/default-main-fields.mjs +13 -0
  44. package/dist/lib/extension-format-map.d.mts +16 -0
  45. package/dist/lib/extension-format-map.mjs +31 -0
  46. package/dist/lib/formats.d.mts +21 -0
  47. package/dist/lib/formats.mjs +22 -0
  48. package/dist/lib/get-source.d.mts +46 -0
  49. package/dist/lib/get-source.mjs +156 -0
  50. package/dist/lib/index.d.mts +41 -0
  51. package/dist/lib/index.mjs +41 -0
  52. package/dist/lib/is-absolute-specifier.d.mts +23 -0
  53. package/dist/lib/is-absolute-specifier.mjs +34 -0
  54. package/dist/lib/is-array-index.d.mts +19 -0
  55. package/dist/lib/is-array-index.mjs +28 -0
  56. package/dist/lib/is-bare-specifier.d.mts +23 -0
  57. package/dist/lib/is-bare-specifier.mjs +33 -0
  58. package/dist/lib/is-directory.d.mts +27 -0
  59. package/dist/lib/is-directory.mjs +54 -0
  60. package/dist/lib/is-file.d.mts +27 -0
  61. package/dist/lib/is-file.mjs +54 -0
  62. package/dist/lib/is-imports-subpath.d.mts +26 -0
  63. package/dist/lib/is-imports-subpath.mjs +28 -0
  64. package/dist/lib/is-module-id.d.mts +23 -0
  65. package/dist/lib/is-module-id.mjs +24 -0
  66. package/dist/lib/is-relative-specifier.d.mts +23 -0
  67. package/dist/lib/is-relative-specifier.mjs +36 -0
  68. package/dist/lib/lookup-package-scope.d.mts +49 -0
  69. package/dist/lib/lookup-package-scope.mjs +102 -0
  70. package/dist/lib/pattern-key-compare.d.mts +28 -0
  71. package/dist/lib/pattern-key-compare.mjs +71 -0
  72. package/dist/lib/pattern-match.d.mts +22 -0
  73. package/dist/lib/pattern-match.mjs +85 -0
  74. package/dist/lib/read-package-json.d.mts +63 -0
  75. package/dist/lib/read-package-json.mjs +115 -0
  76. package/dist/lib/resolve-alias.d.mts +21 -0
  77. package/dist/lib/resolve-alias.mjs +102 -0
  78. package/dist/lib/resolve-module.d.mts +43 -0
  79. package/dist/lib/resolve-module.mjs +186 -0
  80. package/dist/lib/resolver.d.mts +346 -0
  81. package/dist/lib/resolver.mjs +1015 -0
  82. package/dist/lib/root.d.mts +11 -0
  83. package/dist/lib/root.mjs +12 -0
  84. package/dist/lib/to-relative-specifier.d.mts +27 -0
  85. package/dist/lib/to-relative-specifier.mjs +63 -0
  86. package/dist/lib/to-url.d.mts +26 -0
  87. package/dist/lib/to-url.mjs +34 -0
  88. package/dist/types/awaitable.d.mts +12 -0
  89. package/dist/types/change-ext-fn.d.mts +29 -0
  90. package/dist/types/condition.d.mts +13 -0
  91. package/dist/types/dot.d.mts +9 -0
  92. package/dist/types/empty-array.d.mts +9 -0
  93. package/dist/types/empty-object.d.mts +19 -0
  94. package/dist/types/empty-string.d.mts +9 -0
  95. package/dist/types/ext.d.mts +12 -0
  96. package/dist/types/get-source-handler.d.mts +22 -0
  97. package/dist/types/get-source-handlers.d.mts +15 -0
  98. package/dist/types/index.d.mts +19 -10
  99. package/dist/types/list.d.mts +12 -0
  100. package/dist/types/main-field.d.mts +13 -0
  101. package/dist/types/module-format.d.mts +13 -0
  102. package/dist/types/module-id.d.mts +3 -4
  103. package/dist/types/numeric.d.mts +9 -0
  104. package/dist/types/pattern-key-comparison.d.mts +14 -0
  105. package/dist/types/pattern-match.d.mts +10 -0
  106. package/dist/types/protocol.d.mts +6 -7
  107. package/package.json +151 -154
  108. package/changelog.config.ts +0 -404
  109. package/dist/enums/assert-type.d.mts +0 -21
  110. package/dist/enums/assert-type.mjs +0 -10
  111. package/dist/enums/assert-type.mjs.map +0 -6
  112. package/dist/enums/format.d.mts +0 -17
  113. package/dist/enums/format.mjs +0 -13
  114. package/dist/enums/format.mjs.map +0 -6
  115. package/dist/enums/index.d.mts +0 -10
  116. package/dist/enums/index.mjs +0 -15
  117. package/dist/enums/index.mjs.map +0 -6
  118. package/dist/enums/kind-specifier-syntax.d.mts +0 -14
  119. package/dist/enums/kind-specifier-syntax.mjs +0 -10
  120. package/dist/enums/kind-specifier-syntax.mjs.map +0 -6
  121. package/dist/enums/kind-specifier.d.mts +0 -17
  122. package/dist/enums/kind-specifier.mjs +0 -11
  123. package/dist/enums/kind-specifier.mjs.map +0 -6
  124. package/dist/enums/kind-statement-syntax.d.mts +0 -22
  125. package/dist/enums/kind-statement-syntax.mjs +0 -18
  126. package/dist/enums/kind-statement-syntax.mjs.map +0 -6
  127. package/dist/enums/kind-statement.d.mts +0 -15
  128. package/dist/enums/kind-statement.mjs +0 -11
  129. package/dist/enums/kind-statement.mjs.map +0 -6
  130. package/dist/index.mjs.map +0 -6
  131. package/dist/interfaces/import-assertions.d.mts +0 -20
  132. package/dist/interfaces/import-dynamic.d.mts +0 -43
  133. package/dist/interfaces/import-static.d.mts +0 -37
  134. package/dist/interfaces/index.mjs +0 -1
  135. package/dist/interfaces/index.mjs.map +0 -6
  136. package/dist/interfaces/options-fill-module.d.mts +0 -23
  137. package/dist/interfaces/options-find-subpath.d.mts +0 -49
  138. package/dist/interfaces/options-get-format.d.mts +0 -83
  139. package/dist/interfaces/options-get-source.d.mts +0 -63
  140. package/dist/interfaces/options-parse-module-id.d.mts +0 -33
  141. package/dist/interfaces/options-parse-subpath.d.mts +0 -31
  142. package/dist/interfaces/options-resolve-alias.d.mts +0 -53
  143. package/dist/interfaces/options-resolve-module.d.mts +0 -64
  144. package/dist/interfaces/options-resolve.d.mts +0 -15
  145. package/dist/interfaces/package-scope.d.mts +0 -25
  146. package/dist/interfaces/parsed-data-url.d.mts +0 -56
  147. package/dist/interfaces/parsed-module-id.d.mts +0 -80
  148. package/dist/interfaces/parsed-subpath.d.mts +0 -40
  149. package/dist/interfaces/statement-export.d.mts +0 -44
  150. package/dist/interfaces/statement-import.d.mts +0 -38
  151. package/dist/interfaces/statement-require.d.mts +0 -38
  152. package/dist/interfaces/statement.d.mts +0 -49
  153. package/dist/internal/escape-reg-exp.d.mts +0 -16
  154. package/dist/internal/escape-reg-exp.mjs +0 -10
  155. package/dist/internal/escape-reg-exp.mjs.map +0 -6
  156. package/dist/internal/format-type-map.d.mts +0 -17
  157. package/dist/internal/format-type-map.mjs +0 -13
  158. package/dist/internal/format-type-map.mjs.map +0 -6
  159. package/dist/internal/get-specifier-kind.d.mts +0 -17
  160. package/dist/internal/get-specifier-kind.mjs +0 -14
  161. package/dist/internal/get-specifier-kind.mjs.map +0 -6
  162. package/dist/internal/get-subpaths.d.mts +0 -25
  163. package/dist/internal/get-subpaths.mjs +0 -10
  164. package/dist/internal/get-subpaths.mjs.map +0 -6
  165. package/dist/internal/is-array-index.d.mts +0 -15
  166. package/dist/internal/is-array-index.mjs +0 -9
  167. package/dist/internal/is-array-index.mjs.map +0 -6
  168. package/dist/internal/is-directory.d.mts +0 -13
  169. package/dist/internal/is-directory.mjs +0 -16
  170. package/dist/internal/is-directory.mjs.map +0 -6
  171. package/dist/internal/is-file.d.mts +0 -13
  172. package/dist/internal/is-file.mjs +0 -16
  173. package/dist/internal/is-file.mjs.map +0 -6
  174. package/dist/internal/is-function.d.mts +0 -12
  175. package/dist/internal/is-function.mjs +0 -8
  176. package/dist/internal/is-function.mjs.map +0 -6
  177. package/dist/internal/regex-encoded-sep.d.mts +0 -13
  178. package/dist/internal/regex-encoded-sep.mjs +0 -6
  179. package/dist/internal/regex-encoded-sep.mjs.map +0 -6
  180. package/dist/internal/regex-internal-specifier.d.mts +0 -14
  181. package/dist/internal/regex-internal-specifier.mjs +0 -6
  182. package/dist/internal/regex-internal-specifier.mjs.map +0 -6
  183. package/dist/internal/regex-package-name.d.mts +0 -13
  184. package/dist/internal/regex-package-name.mjs +0 -6
  185. package/dist/internal/regex-package-name.mjs.map +0 -6
  186. package/dist/internal/regex-package-path.d.mts +0 -15
  187. package/dist/internal/regex-package-path.mjs +0 -6
  188. package/dist/internal/regex-package-path.mjs.map +0 -6
  189. package/dist/internal/resolver.d.mts +0 -130
  190. package/dist/internal/resolver.mjs +0 -486
  191. package/dist/internal/resolver.mjs.map +0 -6
  192. package/dist/internal/validate-boolean.d.mts +0 -20
  193. package/dist/internal/validate-boolean.mjs +0 -11
  194. package/dist/internal/validate-boolean.mjs.map +0 -6
  195. package/dist/internal/validate-object.d.mts +0 -23
  196. package/dist/internal/validate-object.mjs +0 -11
  197. package/dist/internal/validate-object.mjs.map +0 -6
  198. package/dist/internal/validate-set.d.mts +0 -23
  199. package/dist/internal/validate-set.mjs +0 -11
  200. package/dist/internal/validate-set.mjs.map +0 -6
  201. package/dist/internal/validate-string.d.mts +0 -20
  202. package/dist/internal/validate-string.mjs +0 -11
  203. package/dist/internal/validate-string.mjs.map +0 -6
  204. package/dist/internal/validate-url-string.d.mts +0 -22
  205. package/dist/internal/validate-url-string.mjs +0 -12
  206. package/dist/internal/validate-url-string.mjs.map +0 -6
  207. package/dist/types/declaration.d.mts +0 -9
  208. package/dist/types/fn-change-ext.d.mts +0 -28
  209. package/dist/types/index.mjs +0 -1
  210. package/dist/types/index.mjs.map +0 -6
  211. package/dist/types/mime-type.d.mts +0 -13
  212. package/dist/types/module-specifier-type.d.mts +0 -11
  213. package/dist/types/syntax-kind-export.d.mts +0 -12
  214. package/dist/types/syntax-kind-import.d.mts +0 -12
  215. package/dist/types/syntax-kind-require.d.mts +0 -12
  216. package/dist/utils/compare-subpaths.d.mts +0 -25
  217. package/dist/utils/compare-subpaths.mjs +0 -24
  218. package/dist/utils/compare-subpaths.mjs.map +0 -6
  219. package/dist/utils/conditions.d.mts +0 -13
  220. package/dist/utils/conditions.mjs +0 -6
  221. package/dist/utils/conditions.mjs.map +0 -6
  222. package/dist/utils/detect-syntax.d.mts +0 -21
  223. package/dist/utils/detect-syntax.mjs +0 -12
  224. package/dist/utils/detect-syntax.mjs.map +0 -6
  225. package/dist/utils/extension-format-map.d.mts +0 -16
  226. package/dist/utils/extension-format-map.mjs +0 -22
  227. package/dist/utils/extension-format-map.mjs.map +0 -6
  228. package/dist/utils/extract-statements.d.mts +0 -21
  229. package/dist/utils/extract-statements.mjs +0 -21
  230. package/dist/utils/extract-statements.mjs.map +0 -6
  231. package/dist/utils/fill-modules.d.mts +0 -23
  232. package/dist/utils/fill-modules.mjs +0 -62
  233. package/dist/utils/fill-modules.mjs.map +0 -6
  234. package/dist/utils/find-dynamic-imports.d.mts +0 -18
  235. package/dist/utils/find-dynamic-imports.mjs +0 -39
  236. package/dist/utils/find-dynamic-imports.mjs.map +0 -6
  237. package/dist/utils/find-exports.d.mts +0 -21
  238. package/dist/utils/find-exports.mjs +0 -95
  239. package/dist/utils/find-exports.mjs.map +0 -6
  240. package/dist/utils/find-requires.d.mts +0 -18
  241. package/dist/utils/find-requires.mjs +0 -32
  242. package/dist/utils/find-requires.mjs.map +0 -6
  243. package/dist/utils/find-static-imports.d.mts +0 -18
  244. package/dist/utils/find-static-imports.mjs +0 -34
  245. package/dist/utils/find-static-imports.mjs.map +0 -6
  246. package/dist/utils/find-subpath.d.mts +0 -30
  247. package/dist/utils/find-subpath.mjs +0 -106
  248. package/dist/utils/find-subpath.mjs.map +0 -6
  249. package/dist/utils/get-format.d.mts +0 -24
  250. package/dist/utils/get-format.mjs +0 -111
  251. package/dist/utils/get-format.mjs.map +0 -6
  252. package/dist/utils/get-source.d.mts +0 -22
  253. package/dist/utils/get-source.mjs +0 -63
  254. package/dist/utils/get-source.mjs.map +0 -6
  255. package/dist/utils/has-cjs-syntax.d.mts +0 -25
  256. package/dist/utils/has-cjs-syntax.mjs +0 -11
  257. package/dist/utils/has-cjs-syntax.mjs.map +0 -6
  258. package/dist/utils/has-esm-syntax.d.mts +0 -21
  259. package/dist/utils/has-esm-syntax.mjs +0 -11
  260. package/dist/utils/has-esm-syntax.mjs.map +0 -6
  261. package/dist/utils/index.d.mts +0 -42
  262. package/dist/utils/index.mjs +0 -79
  263. package/dist/utils/index.mjs.map +0 -6
  264. package/dist/utils/is-absolute-specifier.d.mts +0 -19
  265. package/dist/utils/is-absolute-specifier.mjs +0 -25
  266. package/dist/utils/is-absolute-specifier.mjs.map +0 -6
  267. package/dist/utils/is-bare-specifier.d.mts +0 -19
  268. package/dist/utils/is-bare-specifier.mjs +0 -10
  269. package/dist/utils/is-bare-specifier.mjs.map +0 -6
  270. package/dist/utils/is-exports-sugar.d.mts +0 -25
  271. package/dist/utils/is-exports-sugar.mjs +0 -26
  272. package/dist/utils/is-exports-sugar.mjs.map +0 -6
  273. package/dist/utils/is-relative-specifier.d.mts +0 -20
  274. package/dist/utils/is-relative-specifier.mjs +0 -11
  275. package/dist/utils/is-relative-specifier.mjs.map +0 -6
  276. package/dist/utils/lookup-package-scope.d.mts +0 -24
  277. package/dist/utils/lookup-package-scope.mjs +0 -34
  278. package/dist/utils/lookup-package-scope.mjs.map +0 -6
  279. package/dist/utils/parse-data-url.d.mts +0 -25
  280. package/dist/utils/parse-data-url.mjs +0 -25
  281. package/dist/utils/parse-data-url.mjs.map +0 -6
  282. package/dist/utils/parse-module-id.d.mts +0 -22
  283. package/dist/utils/parse-module-id.mjs +0 -122
  284. package/dist/utils/parse-module-id.mjs.map +0 -6
  285. package/dist/utils/parse-subpath.d.mts +0 -35
  286. package/dist/utils/parse-subpath.mjs +0 -54
  287. package/dist/utils/parse-subpath.mjs.map +0 -6
  288. package/dist/utils/pattern-character.d.mts +0 -11
  289. package/dist/utils/pattern-character.mjs +0 -6
  290. package/dist/utils/pattern-character.mjs.map +0 -6
  291. package/dist/utils/read-package-json.d.mts +0 -26
  292. package/dist/utils/read-package-json.mjs +0 -43
  293. package/dist/utils/read-package-json.mjs.map +0 -6
  294. package/dist/utils/resolve-alias.d.mts +0 -20
  295. package/dist/utils/resolve-alias.mjs +0 -106
  296. package/dist/utils/resolve-alias.mjs.map +0 -6
  297. package/dist/utils/resolve-aliases.d.mts +0 -19
  298. package/dist/utils/resolve-aliases.mjs +0 -28
  299. package/dist/utils/resolve-aliases.mjs.map +0 -6
  300. package/dist/utils/resolve-extensions.d.mts +0 -14
  301. package/dist/utils/resolve-extensions.mjs +0 -22
  302. package/dist/utils/resolve-extensions.mjs.map +0 -6
  303. package/dist/utils/resolve-module.d.mts +0 -39
  304. package/dist/utils/resolve-module.mjs +0 -64
  305. package/dist/utils/resolve-module.mjs.map +0 -6
  306. package/dist/utils/resolve-modules.d.mts +0 -25
  307. package/dist/utils/resolve-modules.mjs +0 -26
  308. package/dist/utils/resolve-modules.mjs.map +0 -6
  309. package/dist/utils/to-absolute-specifier.d.mts +0 -22
  310. package/dist/utils/to-absolute-specifier.mjs +0 -8
  311. package/dist/utils/to-absolute-specifier.mjs.map +0 -6
  312. package/dist/utils/to-bare-specifier.d.mts +0 -42
  313. package/dist/utils/to-bare-specifier.mjs +0 -90
  314. package/dist/utils/to-bare-specifier.mjs.map +0 -6
  315. package/dist/utils/to-data-url.d.mts +0 -30
  316. package/dist/utils/to-data-url.mjs +0 -11
  317. package/dist/utils/to-data-url.mjs.map +0 -6
  318. package/dist/utils/to-node-url.d.mts +0 -19
  319. package/dist/utils/to-node-url.mjs +0 -10
  320. package/dist/utils/to-node-url.mjs.map +0 -6
  321. package/dist/utils/to-relative-specifier.d.mts +0 -22
  322. package/dist/utils/to-relative-specifier.mjs +0 -21
  323. package/dist/utils/to-relative-specifier.mjs.map +0 -6
  324. package/dist/utils/to-url.d.mts +0 -22
  325. package/dist/utils/to-url.mjs +0 -12
  326. package/dist/utils/to-url.mjs.map +0 -6
  327. package/dist/utils/validate-assertions.d.mts +0 -23
  328. package/dist/utils/validate-assertions.mjs +0 -52
  329. package/dist/utils/validate-assertions.mjs.map +0 -6
  330. package/dist/utils/validate-exports.d.mts +0 -24
  331. package/dist/utils/validate-exports.mjs +0 -72
  332. package/dist/utils/validate-exports.mjs.map +0 -6
@@ -0,0 +1,156 @@
1
+ /**
2
+ * @file getSource
3
+ * @module mlly/lib/getSource
4
+ */
5
+ import chainOrCall from '#internal/chain-or-call';
6
+ import fs from '#internal/fs';
7
+ import isPromise from '#internal/is-promise';
8
+ import process from '#internal/process';
9
+ import isFile from '#lib/is-file';
10
+ import isModuleId from '#lib/is-module-id';
11
+ import toUrl from '#lib/to-url';
12
+ import { ERR_UNSUPPORTED_ESM_URL_SCHEME } from '@flex-development/errnode';
13
+ import { ok } from 'devlop';
14
+ export default getSource;
15
+ /**
16
+ * Get the source code for a module.
17
+ *
18
+ * > 👉 **Note**: Returns a promise if the handler for `id` is async.
19
+ *
20
+ * @see {@linkcode Awaitable}
21
+ * @see {@linkcode ErrUnsupportedEsmUrlScheme}
22
+ * @see {@linkcode GetSourceOptions}
23
+ * @see {@linkcode ModuleId}
24
+ *
25
+ * @this {void}
26
+ *
27
+ * @param {ModuleId | null | undefined} id
28
+ * The module id
29
+ * @param {GetSourceOptions | null | undefined} [options]
30
+ * Source code retrieval options
31
+ * @return {Awaitable<string | null | undefined>}
32
+ * The module source code
33
+ * @throws {ErrUnsupportedEsmUrlScheme}
34
+ */
35
+ function getSource(id, options) {
36
+ if (!isModuleId(id))
37
+ return null;
38
+ /**
39
+ * The source code retrieval context.
40
+ *
41
+ * @const {GetSourceContext} context
42
+ */
43
+ const context = {
44
+ ...options,
45
+ fs: options?.fs ?? fs,
46
+ handlers: {
47
+ 'data:': data,
48
+ 'file:': file,
49
+ 'http:': https,
50
+ 'https:': https,
51
+ 'node:': node,
52
+ ...options?.handlers
53
+ },
54
+ req: { ...options?.req },
55
+ schemes: new Set(options?.schemes
56
+ ? [...options.schemes]
57
+ : ['data', 'file', 'http', 'https', 'node'])
58
+ };
59
+ /**
60
+ * The module url.
61
+ *
62
+ * @const {URL} url
63
+ */
64
+ const url = toUrl(id);
65
+ /**
66
+ * The source code handler for {@linkcode url}.
67
+ *
68
+ * @var {GetSourceHandler | null | undefined} handle
69
+ */
70
+ let handle;
71
+ // get source code handler.
72
+ handle = context.handlers[url.protocol];
73
+ // handle unsupported url scheme.
74
+ if (typeof handle !== 'function') {
75
+ if (context.ignoreErrors)
76
+ return undefined;
77
+ throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, [...context.schemes], process.platform === 'win32');
78
+ }
79
+ /**
80
+ * The source code.
81
+ *
82
+ * @var {Awaitable<Uint8Array | string | null | undefined>} code
83
+ */
84
+ let code = handle.call(context, url);
85
+ // resolve source code.
86
+ if (isPromise(code)) {
87
+ void code.then(resolved => (code = resolved));
88
+ }
89
+ return chainOrCall(code, () => {
90
+ ok(!isPromise(code), 'expected `code` to be resolved');
91
+ if (code !== null && code !== undefined)
92
+ code = String(code);
93
+ return code;
94
+ });
95
+ }
96
+ /**
97
+ * @this {GetSourceContext}
98
+ *
99
+ * @param {URL} url
100
+ * The module URL
101
+ * @return {Buffer}
102
+ * The source code buffer
103
+ */
104
+ function data(url) {
105
+ const [mime, data = ''] = url.pathname.split(',');
106
+ ok(url.protocol === 'data:', 'expected `data:` URL');
107
+ ok(mime !== undefined, 'expected `mime` to be a string');
108
+ return Buffer.from(decodeURIComponent(data), mime.endsWith('base64') ? 'base64' : 'utf8');
109
+ }
110
+ /**
111
+ * > 👉 **Note**: Returns a promise if `this.fs.readFile` or `this.fs.stat`
112
+ * > is async.
113
+ *
114
+ * @this {GetSourceContext}
115
+ *
116
+ * @param {URL} url
117
+ * The module URL
118
+ * @return {Awaitable<Buffer | string | null>}
119
+ * The source code
120
+ */
121
+ function file(url) {
122
+ ok(url.protocol === 'file:', 'expected `file:` URL');
123
+ /**
124
+ * Whether the file exists.
125
+ *
126
+ * @const {Awaitable<boolean>} exists
127
+ */
128
+ const exists = isFile(url, this.fs);
129
+ return chainOrCall(exists, isFile => {
130
+ return isFile ?? exists ? this.fs.readFile(url) : null;
131
+ });
132
+ }
133
+ /**
134
+ * @async
135
+ *
136
+ * @this {GetSourceContext}
137
+ *
138
+ * @param {URL} url
139
+ * The module URL
140
+ * @return {Promise<string>}
141
+ * The source code
142
+ */
143
+ async function https(url) {
144
+ ok(/^https?:$/.test(url.protocol), 'expected `http:` or `https:` URL');
145
+ return (await fetch(url.href, this.req)).text();
146
+ }
147
+ /**
148
+ * @this {GetSourceContext}
149
+ *
150
+ * @param {URL} url
151
+ * The module URL
152
+ * @return {undefined}
153
+ */
154
+ function node(url) {
155
+ return void ok(url.protocol === 'node:', 'expected `node:` URL');
156
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @file Entry Point - Library
3
+ * @module mlly/lib
4
+ *
5
+ * @todo `detectModuleSyntax` (esast integration)
6
+ * @todo `findDynamicImports` (esast integration)
7
+ * @todo `findExports` (esast integration)
8
+ * @todo `findImports` (esast integration)
9
+ * @todo `findRequires` (esast integration)
10
+ * @todo `findSpecifiers` (esast integration)
11
+ * @todo `findStaticImports` (esast integration)
12
+ * @todo `resolveAliases` (esast integration)
13
+ * @todo `resolveModules` (esast integration)
14
+ */
15
+ export { default as canParseUrl } from '#lib/can-parse-url';
16
+ export { default as cwd } from '#lib/cwd';
17
+ export { default as defaultConditions } from '#lib/default-conditions';
18
+ export { default as defaultExtensions } from '#lib/default-extensions';
19
+ export { default as defaultMainFields } from '#lib/default-main-fields';
20
+ export { default as extensionFormatMap } from '#lib/extension-format-map';
21
+ export { default as formats } from '#lib/formats';
22
+ export { default as getSource } from '#lib/get-source';
23
+ export { default as isAbsoluteSpecifier } from '#lib/is-absolute-specifier';
24
+ export { default as isArrayIndex } from '#lib/is-array-index';
25
+ export { default as isBareSpecifier } from '#lib/is-bare-specifier';
26
+ export { default as isDirectory } from '#lib/is-directory';
27
+ export { default as isFile } from '#lib/is-file';
28
+ export { default as isImportsSubpath } from '#lib/is-imports-subpath';
29
+ export { default as isModuleId } from '#lib/is-module-id';
30
+ export { default as isRelativeSpecifier } from '#lib/is-relative-specifier';
31
+ export { default as lookupPackageScope } from '#lib/lookup-package-scope';
32
+ export { default as patternKeyCompare } from '#lib/pattern-key-compare';
33
+ export { default as patternMatch } from '#lib/pattern-match';
34
+ export { default as readPackageJson } from '#lib/read-package-json';
35
+ export { default as resolveAlias } from '#lib/resolve-alias';
36
+ export { default as resolveModule } from '#lib/resolve-module';
37
+ export * from '#lib/resolver';
38
+ export * as resolver from '#lib/resolver';
39
+ export { default as root } from '#lib/root';
40
+ export { default as toRelativeSpecifier } from '#lib/to-relative-specifier';
41
+ export { default as toUrl } from '#lib/to-url';
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @file Entry Point - Library
3
+ * @module mlly/lib
4
+ *
5
+ * @todo `detectModuleSyntax` (esast integration)
6
+ * @todo `findDynamicImports` (esast integration)
7
+ * @todo `findExports` (esast integration)
8
+ * @todo `findImports` (esast integration)
9
+ * @todo `findRequires` (esast integration)
10
+ * @todo `findSpecifiers` (esast integration)
11
+ * @todo `findStaticImports` (esast integration)
12
+ * @todo `resolveAliases` (esast integration)
13
+ * @todo `resolveModules` (esast integration)
14
+ */
15
+ export { default as canParseUrl } from '#lib/can-parse-url';
16
+ export { default as cwd } from '#lib/cwd';
17
+ export { default as defaultConditions } from '#lib/default-conditions';
18
+ export { default as defaultExtensions } from '#lib/default-extensions';
19
+ export { default as defaultMainFields } from '#lib/default-main-fields';
20
+ export { default as extensionFormatMap } from '#lib/extension-format-map';
21
+ export { default as formats } from '#lib/formats';
22
+ export { default as getSource } from '#lib/get-source';
23
+ export { default as isAbsoluteSpecifier } from '#lib/is-absolute-specifier';
24
+ export { default as isArrayIndex } from '#lib/is-array-index';
25
+ export { default as isBareSpecifier } from '#lib/is-bare-specifier';
26
+ export { default as isDirectory } from '#lib/is-directory';
27
+ export { default as isFile } from '#lib/is-file';
28
+ export { default as isImportsSubpath } from '#lib/is-imports-subpath';
29
+ export { default as isModuleId } from '#lib/is-module-id';
30
+ export { default as isRelativeSpecifier } from '#lib/is-relative-specifier';
31
+ export { default as lookupPackageScope } from '#lib/lookup-package-scope';
32
+ export { default as patternKeyCompare } from '#lib/pattern-key-compare';
33
+ export { default as patternMatch } from '#lib/pattern-match';
34
+ export { default as readPackageJson } from '#lib/read-package-json';
35
+ export { default as resolveAlias } from '#lib/resolve-alias';
36
+ export { default as resolveModule } from '#lib/resolve-module';
37
+ export * from '#lib/resolver';
38
+ export * as resolver from '#lib/resolver';
39
+ export { default as root } from '#lib/root';
40
+ export { default as toRelativeSpecifier } from '#lib/to-relative-specifier';
41
+ export { default as toUrl } from '#lib/to-url';
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @file isAbsoluteSpecifier
3
+ * @module mlly/lib/isAbsoluteSpecifier
4
+ */
5
+ /**
6
+ * Check if `value` is an *absolute specifier*.
7
+ *
8
+ * ::: warning
9
+ * Only checks specifier syntax.
10
+ * Does **not** guarantee the specifier references an existing module.
11
+ * :::
12
+ *
13
+ * @see https://nodejs.org/api/esm.html#terminology
14
+ *
15
+ * @this {void}
16
+ *
17
+ * @param {unknown} value
18
+ * The value to check
19
+ * @return {boolean}
20
+ * `true` if `value` is absolute specifier, `false` otherwise
21
+ */
22
+ declare function isAbsoluteSpecifier(this: void, value: unknown): boolean;
23
+ export default isAbsoluteSpecifier;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @file isAbsoluteSpecifier
3
+ * @module mlly/lib/isAbsoluteSpecifier
4
+ */
5
+ import canParseUrl from '#lib/can-parse-url';
6
+ import isModuleId from '#lib/is-module-id';
7
+ import pathe from '@flex-development/pathe';
8
+ /**
9
+ * Check if `value` is an *absolute specifier*.
10
+ *
11
+ * ::: warning
12
+ * Only checks specifier syntax.
13
+ * Does **not** guarantee the specifier references an existing module.
14
+ * :::
15
+ *
16
+ * @see https://nodejs.org/api/esm.html#terminology
17
+ *
18
+ * @this {void}
19
+ *
20
+ * @param {unknown} value
21
+ * The value to check
22
+ * @return {boolean}
23
+ * `true` if `value` is absolute specifier, `false` otherwise
24
+ */
25
+ function isAbsoluteSpecifier(value) {
26
+ return (!isModuleId(value)
27
+ ? false
28
+ : typeof value === 'string'
29
+ ? value.startsWith('file:')
30
+ ? canParseUrl(value)
31
+ : pathe.isAbsolute(value)
32
+ : value.protocol === 'file:' && canParseUrl(value));
33
+ }
34
+ export default isAbsoluteSpecifier;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @file isArrayIndex
3
+ * @module mlly/lib/isArrayIndex
4
+ */
5
+ import type { Numeric } from '@flex-development/mlly';
6
+ /**
7
+ * Check if `value` is a valid array index.
8
+ *
9
+ * @see {@linkcode Numeric}
10
+ *
11
+ * @this {void}
12
+ *
13
+ * @param {unknown} value
14
+ * The value to check
15
+ * @return {value is Numeric}
16
+ * `true` if `value` is valid array index, `false` otherwise
17
+ */
18
+ declare function isArrayIndex(this: void, value: unknown): value is Numeric;
19
+ export default isArrayIndex;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @file isArrayIndex
3
+ * @module mlly/lib/isArrayIndex
4
+ */
5
+ /**
6
+ * Check if `value` is a valid array index.
7
+ *
8
+ * @see {@linkcode Numeric}
9
+ *
10
+ * @this {void}
11
+ *
12
+ * @param {unknown} value
13
+ * The value to check
14
+ * @return {value is Numeric}
15
+ * `true` if `value` is valid array index, `false` otherwise
16
+ */
17
+ function isArrayIndex(value) {
18
+ if (typeof value !== 'string')
19
+ return false;
20
+ /**
21
+ * The {@linkcode value} as number.
22
+ *
23
+ * @const {number} num
24
+ */
25
+ const num = +value;
26
+ return value === `${num}` && num >= 0 && num < 0xFFFF_FFFF;
27
+ }
28
+ export default isArrayIndex;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @file isBareSpecifier
3
+ * @module mlly/lib/isBareSpecifier
4
+ */
5
+ /**
6
+ * Check if `value` is a *bare specifier*.
7
+ *
8
+ * ::: warning
9
+ * Only checks specifier syntax.
10
+ * Does **not** guarantee the specifier references an existing module.
11
+ * :::
12
+ *
13
+ * @see https://nodejs.org/api/esm.html#terminology
14
+ *
15
+ * @this {void}
16
+ *
17
+ * @param {unknown} value
18
+ * The value to check
19
+ * @return {boolean}
20
+ * `true` if `value` is bare specifier, `false` otherwise
21
+ */
22
+ declare function isBareSpecifier(this: void, value: unknown): boolean;
23
+ export default isBareSpecifier;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @file isBareSpecifier
3
+ * @module mlly/lib/isBareSpecifier
4
+ */
5
+ import isAbsoluteSpecifier from '#lib/is-absolute-specifier';
6
+ import isRelativeSpecifier from '#lib/is-relative-specifier';
7
+ import { isBuiltin } from '@flex-development/is-builtin';
8
+ /**
9
+ * Check if `value` is a *bare specifier*.
10
+ *
11
+ * ::: warning
12
+ * Only checks specifier syntax.
13
+ * Does **not** guarantee the specifier references an existing module.
14
+ * :::
15
+ *
16
+ * @see https://nodejs.org/api/esm.html#terminology
17
+ *
18
+ * @this {void}
19
+ *
20
+ * @param {unknown} value
21
+ * The value to check
22
+ * @return {boolean}
23
+ * `true` if `value` is bare specifier, `false` otherwise
24
+ */
25
+ function isBareSpecifier(value) {
26
+ if (isBuiltin(value))
27
+ return true;
28
+ return (typeof value === 'string' &&
29
+ !!value.length &&
30
+ !isAbsoluteSpecifier(value) &&
31
+ !isRelativeSpecifier(value));
32
+ }
33
+ export default isBareSpecifier;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @file isDirectory
3
+ * @module mlly/lib/isDirectory
4
+ */
5
+ import type { Awaitable, FileSystem } from '@flex-development/mlly';
6
+ export default isDirectory;
7
+ /**
8
+ * Check if a directory exists.
9
+ *
10
+ * > 👉 **Note**: Returns a promise if `fs.stat` is async.
11
+ *
12
+ * @see {@linkcode Awaitable}
13
+ * @see {@linkcode FileSystem}
14
+ *
15
+ * @template {Awaitable<boolean>} T
16
+ * The result of the check
17
+ *
18
+ * @this {void}
19
+ *
20
+ * @param {unknown} id
21
+ * The module id to check
22
+ * @param {FileSystem | null | undefined} fs
23
+ * The file system API
24
+ * @return {T}
25
+ * `true` if directory exists at `id`, `false` otherwise
26
+ */
27
+ declare function isDirectory<T extends Awaitable<boolean>>(this: void, id: unknown, fs?: FileSystem | null | undefined): T;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @file isDirectory
3
+ * @module mlly/lib/isDirectory
4
+ */
5
+ import chainOrCall from '#internal/chain-or-call';
6
+ import constant from '#internal/constant';
7
+ import dfs from '#internal/fs';
8
+ import identity from '#internal/identity';
9
+ import isPromise from '#internal/is-promise';
10
+ import isModuleId from '#lib/is-module-id';
11
+ export default isDirectory;
12
+ /**
13
+ * Check if a directory exists.
14
+ *
15
+ * > 👉 **Note**: Returns a promise if `fs.stat` is async.
16
+ *
17
+ * @see {@linkcode Awaitable}
18
+ * @see {@linkcode FileSystem}
19
+ *
20
+ * @this {void}
21
+ *
22
+ * @param {unknown} id
23
+ * The module id to check
24
+ * @param {FileSystem | null | undefined} fs
25
+ * The file system API
26
+ * @return {Awaitable<boolean>}
27
+ * `true` if directory exists at `id`, `false` otherwise
28
+ */
29
+ function isDirectory(id, fs) {
30
+ if (isModuleId(id)) {
31
+ try {
32
+ if (typeof id === 'string' && id.startsWith('file:'))
33
+ id = new URL(id);
34
+ /**
35
+ * The stats object.
36
+ *
37
+ * @var {Awaitable<Stats | null>} stats
38
+ */
39
+ let stats = (fs ?? dfs).stat(id);
40
+ // resolve the stats object.
41
+ if (isPromise(stats))
42
+ stats = stats.then(identity, constant(null));
43
+ return chainOrCall(stats, () => {
44
+ if (!isPromise(stats))
45
+ return stats.isDirectory();
46
+ return stats.then(stats => stats?.isDirectory() ?? false);
47
+ });
48
+ }
49
+ catch {
50
+ // swallow error.
51
+ }
52
+ }
53
+ return false;
54
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @file isFile
3
+ * @module mlly/lib/isFile
4
+ */
5
+ import type { Awaitable, FileSystem } from '@flex-development/mlly';
6
+ export default isFile;
7
+ /**
8
+ * Check if a file exists.
9
+ *
10
+ * > 👉 **Note**: Returns a promise if `fs.stat` is async.
11
+ *
12
+ * @see {@linkcode Awaitable}
13
+ * @see {@linkcode FileSystem}
14
+ *
15
+ * @template {Awaitable<boolean>} T
16
+ * The result of the check
17
+ *
18
+ * @this {void}
19
+ *
20
+ * @param {unknown} id
21
+ * The module id to check
22
+ * @param {FileSystem | null | undefined} fs
23
+ * The file system API
24
+ * @return {T}
25
+ * `true` if file exists at `id`, `false` otherwise
26
+ */
27
+ declare function isFile<T extends Awaitable<boolean>>(this: void, id: unknown, fs?: FileSystem | null | undefined): T;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @file isFile
3
+ * @module mlly/lib/isFile
4
+ */
5
+ import chainOrCall from '#internal/chain-or-call';
6
+ import constant from '#internal/constant';
7
+ import dfs from '#internal/fs';
8
+ import identity from '#internal/identity';
9
+ import isPromise from '#internal/is-promise';
10
+ import isModuleId from '#lib/is-module-id';
11
+ export default isFile;
12
+ /**
13
+ * Check if a file exists.
14
+ *
15
+ * > 👉 **Note**: Returns a promise if `fs.stat` is async.
16
+ *
17
+ * @see {@linkcode Awaitable}
18
+ * @see {@linkcode FileSystem}
19
+ *
20
+ * @this {void}
21
+ *
22
+ * @param {unknown} id
23
+ * The module id to check
24
+ * @param {FileSystem | null | undefined} fs
25
+ * The file system API
26
+ * @return {Awaitable<boolean>}
27
+ * `true` if file exists at `id`, `false` otherwise
28
+ */
29
+ function isFile(id, fs) {
30
+ if (isModuleId(id)) {
31
+ try {
32
+ if (typeof id === 'string' && id.startsWith('file:'))
33
+ id = new URL(id);
34
+ /**
35
+ * The stats object.
36
+ *
37
+ * @var {Awaitable<Stats>} stats
38
+ */
39
+ let stats = (fs ?? dfs).stat(id);
40
+ // resolve the stats object.
41
+ if (isPromise(stats))
42
+ stats = stats.then(identity, constant(null));
43
+ return chainOrCall(stats, () => {
44
+ if (!isPromise(stats))
45
+ return stats.isFile();
46
+ return stats.then(stats => stats?.isFile() ?? false);
47
+ });
48
+ }
49
+ catch {
50
+ // swallow error.
51
+ }
52
+ }
53
+ return false;
54
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @file isImportsSubpath
3
+ * @module mlly/lib/isImportsSubpath
4
+ */
5
+ import type { ImportsSubpath } from '@flex-development/pkg-types';
6
+ /**
7
+ * Check if `value` is an [`imports`][subpath-imports] subpath.
8
+ *
9
+ * ::: warning
10
+ * Only checks specifier syntax.
11
+ * Does **not** guarantee the specifier references an existing module.
12
+ * :::
13
+ *
14
+ * [subpath-imports]: https://nodejs.org/api/packages.html#subpath-imports
15
+ *
16
+ * @see {@linkcode ImportsSubpath}
17
+ *
18
+ * @this {void}
19
+ *
20
+ * @param {unknown} value
21
+ * The value to check
22
+ * @return {value is ImportsSubpath}
23
+ * `true` if `value` is `imports` subpath, `false` otherwise
24
+ */
25
+ declare function isImportsSubpath(this: void, value: unknown): value is ImportsSubpath;
26
+ export default isImportsSubpath;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @file isImportsSubpath
3
+ * @module mlly/lib/isImportsSubpath
4
+ */
5
+ import chars from '#internal/chars';
6
+ /**
7
+ * Check if `value` is an [`imports`][subpath-imports] subpath.
8
+ *
9
+ * ::: warning
10
+ * Only checks specifier syntax.
11
+ * Does **not** guarantee the specifier references an existing module.
12
+ * :::
13
+ *
14
+ * [subpath-imports]: https://nodejs.org/api/packages.html#subpath-imports
15
+ *
16
+ * @see {@linkcode ImportsSubpath}
17
+ *
18
+ * @this {void}
19
+ *
20
+ * @param {unknown} value
21
+ * The value to check
22
+ * @return {value is ImportsSubpath}
23
+ * `true` if `value` is `imports` subpath, `false` otherwise
24
+ */
25
+ function isImportsSubpath(value) {
26
+ return typeof value === 'string' && value[0] === chars.hash;
27
+ }
28
+ export default isImportsSubpath;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @file isModuleId
3
+ * @module mlly/lib/isModuleId
4
+ */
5
+ import type { ModuleId } from '@flex-development/mlly';
6
+ /**
7
+ * Check if `value` is a module id.
8
+ *
9
+ * ::: warning
10
+ * Does **not** guarantee `value` references an existing file or directory.
11
+ * :::
12
+ *
13
+ * @see {@linkcode ModuleId}
14
+ *
15
+ * @this {void}
16
+ *
17
+ * @param {unknown} value
18
+ * The value to check
19
+ * @return {value is ModuleId}
20
+ * `true` if `value` is module id, `false` otherwise
21
+ */
22
+ declare function isModuleId(this: void, value: unknown): value is ModuleId;
23
+ export default isModuleId;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @file isModuleId
3
+ * @module mlly/lib/isModuleId
4
+ */
5
+ /**
6
+ * Check if `value` is a module id.
7
+ *
8
+ * ::: warning
9
+ * Does **not** guarantee `value` references an existing file or directory.
10
+ * :::
11
+ *
12
+ * @see {@linkcode ModuleId}
13
+ *
14
+ * @this {void}
15
+ *
16
+ * @param {unknown} value
17
+ * The value to check
18
+ * @return {value is ModuleId}
19
+ * `true` if `value` is module id, `false` otherwise
20
+ */
21
+ function isModuleId(value) {
22
+ return typeof value === 'string' || value instanceof URL;
23
+ }
24
+ export default isModuleId;