bare-module-traverse 1.4.0 → 1.5.0

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/README.md CHANGED
@@ -211,7 +211,7 @@ Options are the same as `traverse()` for all functions.
211
211
  > [!WARNING]
212
212
  > These functions are currently subject to change between minor releases. If using them directly, make sure to specify a tilde range (`~1.2.3`) when declaring the module dependency.
213
213
 
214
- #### `const generator = traverse.module(url, source, artifacts, visited[, options])`
214
+ #### `const generator = traverse.module(url, source, attributes, artifacts, visited[, options])`
215
215
 
216
216
  #### `const generator = traverse.package(url, source, artifacts, visited[, options])`
217
217
 
package/index.d.ts ADDED
@@ -0,0 +1,111 @@
1
+ import URL from 'bare-url'
2
+ import Buffer from 'bare-buffer'
3
+ import {
4
+ type ConditionalSpecifier,
5
+ type ImportsMap,
6
+ type ResolutionsMap
7
+ } from 'bare-module-resolve'
8
+ import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
9
+ import { type Import } from 'bare-module-lexer'
10
+
11
+ interface Dependency {
12
+ url: URL
13
+ source: string | Buffer
14
+ imports: ImportsMap
15
+ }
16
+
17
+ interface TraverseOptions extends ResolveOptions {
18
+ resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
19
+ }
20
+
21
+ declare function traverse(
22
+ entry: URL,
23
+ readModule: (url: URL) => Buffer | string | null,
24
+ listPrefix?: (url: URL) => Iterable<URL>
25
+ ): Iterable<Dependency>
26
+
27
+ declare function traverse(
28
+ entry: URL,
29
+ readModule: (url: URL) => Promise<Buffer | string | null>,
30
+ listPrefix?: (url: URL) => AsyncIterable<URL>
31
+ ): AsyncIterable<Dependency>
32
+
33
+ declare function traverse(
34
+ entry: URL,
35
+ opts: TraverseOptions,
36
+ readModule: (url: URL) => Buffer | string | null,
37
+ listPrefix?: (url: URL) => Iterable<URL>
38
+ ): Iterable<Dependency>
39
+
40
+ declare function traverse(
41
+ entry: URL,
42
+ opts: TraverseOptions,
43
+ readModule: (url: URL) => Promise<Buffer | string | null>,
44
+ listPrefix?: (url: URL) => AsyncIterable<URL>
45
+ ): AsyncIterable<Dependency>
46
+
47
+ declare namespace traverse {
48
+ export { type TraverseOptions }
49
+
50
+ export type Traversal = Generator<
51
+ { module: URL } | { prefix: URL } | { children: URL } | { dependency: URL },
52
+ boolean,
53
+ void | URL[] | Buffer | string | null
54
+ >
55
+
56
+ export interface Artifacts {
57
+ addons: URL[]
58
+ assets: URL[]
59
+ }
60
+
61
+ export function module(
62
+ url: URL,
63
+ source: string | Buffer,
64
+ artifacts: Artifacts,
65
+ visited: Set<string>,
66
+ opts?: TraverseOptions
67
+ ): Traversal
68
+
69
+ export function package(
70
+ url: URL,
71
+ source: string | Buffer,
72
+ artifacts: Artifacts,
73
+ visited: Set<string>,
74
+ opts?: TraverseOptions
75
+ ): Traversal
76
+
77
+ export function preresolved(
78
+ url: URL,
79
+ source: string | Buffer,
80
+ resolution: ResolutionsMap,
81
+ artifacts: Artifacts,
82
+ visited: Set<string>,
83
+ opts?: TraverseOptions
84
+ ): Traversal
85
+
86
+ export function imports(
87
+ parentURL: URL,
88
+ source: string | Buffer,
89
+ imports: ImportsMap,
90
+ artifacts: Artifacts,
91
+ visited: Set<string>,
92
+ opts?: TraverseOptions
93
+ ): Traversal
94
+
95
+ export function prebuilds(
96
+ packageURL: URL,
97
+ artifacts: Artifacts,
98
+ visited: Set<string>,
99
+ opts?: TraverseOptions
100
+ ): Traversal
101
+
102
+ export function assets(
103
+ patterns: ConditionalSpecifier,
104
+ packageURL: URL,
105
+ artifacts: Artifacts,
106
+ visited: Set<string>,
107
+ opts?: TraverseOptions
108
+ ): Traversal
109
+ }
110
+
111
+ export = traverse
package/index.js CHANGED
@@ -20,7 +20,9 @@ module.exports = exports = function traverse(
20
20
  *[Symbol.iterator]() {
21
21
  const artifacts = { addons: [], assets: [] }
22
22
 
23
- const queue = [exports.module(entry, null, artifacts, new Set(), opts)]
23
+ const queue = [
24
+ exports.module(entry, null, {}, artifacts, new Set(), opts)
25
+ ]
24
26
 
25
27
  while (queue.length > 0) {
26
28
  const generator = queue.pop()
@@ -61,7 +63,9 @@ module.exports = exports = function traverse(
61
63
  async *[Symbol.asyncIterator]() {
62
64
  const artifacts = { addons: [], assets: [] }
63
65
 
64
- const queue = [exports.module(entry, null, artifacts, new Set(), opts)]
66
+ const queue = [
67
+ exports.module(entry, null, {}, artifacts, new Set(), opts)
68
+ ]
65
69
 
66
70
  while (queue.length > 0) {
67
71
  const generator = queue.pop()
@@ -103,7 +107,14 @@ module.exports = exports = function traverse(
103
107
 
104
108
  exports.resolve = resolve
105
109
 
106
- exports.module = function* (url, source, artifacts, visited, opts = {}) {
110
+ exports.module = function* (
111
+ url,
112
+ source,
113
+ attributes,
114
+ artifacts,
115
+ visited,
116
+ opts = {}
117
+ ) {
107
118
  const { resolutions = null } = opts
108
119
 
109
120
  if (visited.has(url.href)) return false
@@ -149,6 +160,23 @@ exports.module = function* (url, source, artifacts, visited, opts = {}) {
149
160
  }
150
161
  }
151
162
 
163
+ if (
164
+ typeof attributes === 'object' &&
165
+ attributes !== null &&
166
+ typeof attributes.imports === 'string'
167
+ ) {
168
+ const url = new URL(attributes.imports)
169
+
170
+ const source = yield { module: url }
171
+
172
+ if (source !== null) {
173
+ opts = {
174
+ ...opts,
175
+ imports: mixinImports(opts.imports, JSON.parse(source), url)
176
+ }
177
+ }
178
+ }
179
+
152
180
  yield* exports.imports(url, source, imports, artifacts, visited, opts)
153
181
 
154
182
  yield { dependency: { url, source, imports: compressImportsMap(imports) } }
@@ -209,7 +237,7 @@ exports.preresolved = function* (
209
237
  }
210
238
  } else {
211
239
  yield {
212
- children: exports.module(url, null, artifacts, visited, opts)
240
+ children: exports.module(url, null, {}, artifacts, visited, opts)
213
241
  }
214
242
  }
215
243
  } else {
@@ -235,11 +263,14 @@ exports.imports = function* (
235
263
  resolve = exports.resolve.default,
236
264
  builtinProtocol = 'builtin:',
237
265
  linkedProtocol = 'linked:',
266
+ deferredProtocol = 'deferred:',
238
267
  matchedConditions = []
239
268
  } = opts
240
269
 
241
270
  let yielded = false
242
271
 
272
+ const queue = []
273
+
243
274
  for (const entry of lex(source).imports) {
244
275
  let specifier = entry.specifier
245
276
  let condition = 'default'
@@ -251,6 +282,28 @@ exports.imports = function* (
251
282
  condition = 'asset'
252
283
  }
253
284
 
285
+ if (entry.attributes.imports) {
286
+ const specifier = entry.attributes.imports
287
+
288
+ queue.push({
289
+ entry: {
290
+ type: 0,
291
+ specifier,
292
+ names: [],
293
+ attributes: {},
294
+ position: [0, 0, 0]
295
+ },
296
+ specifier,
297
+ condition: 'default'
298
+ })
299
+ }
300
+
301
+ queue.push({ entry, specifier, condition })
302
+ }
303
+
304
+ while (queue.length > 0) {
305
+ const { entry, specifier, condition } = queue.shift()
306
+
254
307
  matchedConditions.push(condition)
255
308
 
256
309
  const resolver = resolve(entry, parentURL, { ...opts, matchedConditions })
@@ -270,7 +323,8 @@ exports.imports = function* (
270
323
 
271
324
  if (
272
325
  url.protocol === builtinProtocol ||
273
- url.protocol === linkedProtocol
326
+ url.protocol === linkedProtocol ||
327
+ url.protocol === deferredProtocol
274
328
  ) {
275
329
  addResolution(imports, specifier, matchedConditions, url)
276
330
 
@@ -283,7 +337,14 @@ exports.imports = function* (
283
337
 
284
338
  for (const url of prefix) {
285
339
  yield {
286
- children: exports.module(url, null, artifacts, visited, opts)
340
+ children: exports.module(
341
+ url,
342
+ null,
343
+ {},
344
+ artifacts,
345
+ visited,
346
+ opts
347
+ )
287
348
  }
288
349
 
289
350
  addURL(artifacts.assets, url)
@@ -297,8 +358,21 @@ exports.imports = function* (
297
358
  if (source !== null) {
298
359
  addResolution(imports, specifier, matchedConditions, url)
299
360
 
361
+ const attributes = entry.attributes
362
+
363
+ if (attributes.imports) {
364
+ attributes.imports = imports[attributes.imports].default
365
+ }
366
+
300
367
  yield {
301
- children: exports.module(url, source, artifacts, visited, opts)
368
+ children: exports.module(
369
+ url,
370
+ source,
371
+ attributes,
372
+ artifacts,
373
+ visited,
374
+ opts
375
+ )
302
376
  }
303
377
 
304
378
  resolved = yielded = true
@@ -365,7 +439,7 @@ exports.prebuilds = function* (packageURL, artifacts, visited, opts = {}) {
365
439
  addURL(artifacts.addons, url)
366
440
 
367
441
  yield {
368
- children: exports.module(url, source, artifacts, visited, opts)
442
+ children: exports.module(url, source, {}, artifacts, visited, opts)
369
443
  }
370
444
 
371
445
  yielded = true
@@ -395,7 +469,9 @@ exports.assets = function* (
395
469
  if (source !== null) {
396
470
  addURL(artifacts.assets, url)
397
471
 
398
- yield { children: exports.module(url, source, artifacts, visited, opts) }
472
+ yield {
473
+ children: exports.module(url, source, {}, artifacts, visited, opts)
474
+ }
399
475
 
400
476
  yielded = true
401
477
  }
@@ -580,3 +656,17 @@ function compressImportsMapEntry(resolved) {
580
656
 
581
657
  return Object.fromEntries(entries)
582
658
  }
659
+
660
+ function mixinImports(target, imports, url) {
661
+ if (typeof imports === 'object' && imports !== null && 'imports' in imports) {
662
+ imports = imports.imports
663
+ }
664
+
665
+ if (typeof imports !== 'object' || imports === null) {
666
+ throw errors.INVALID_IMPORTS_MAP(
667
+ `Imports map at '${url.href}' is not valid`
668
+ )
669
+ }
670
+
671
+ return { ...target, ...imports }
672
+ }
package/lib/errors.js CHANGED
@@ -35,4 +35,12 @@ module.exports = class ModuleTraverseError extends Error {
35
35
  ModuleTraverseError.ASSET_NOT_FOUND
36
36
  )
37
37
  }
38
+
39
+ static INVALID_IMPORTS_MAP(msg) {
40
+ return new ModuleTraverseError(
41
+ msg,
42
+ 'INVALID_IMPORTS_MAP',
43
+ ModuleTraverseError.INVALID_IMPORTS_MAP
44
+ )
45
+ }
38
46
  }
@@ -0,0 +1,21 @@
1
+ import { type Import } from 'bare-module-lexer'
2
+ import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
3
+
4
+ interface BareResolveOptions extends ResolveOptions {
5
+ platform?: string
6
+ arch?: string
7
+ simulator?: boolean
8
+ target?: string[]
9
+ }
10
+
11
+ declare function resolve(
12
+ entry: Import,
13
+ parentURL: URL,
14
+ opts?: BareResolveOptions
15
+ ): Resolver
16
+
17
+ declare namespace resolve {
18
+ export { type BareResolveOptions }
19
+ }
20
+
21
+ export = resolve
@@ -1,14 +1,13 @@
1
1
  const lex = require('bare-module-lexer')
2
2
  const resolve = require('../resolve')
3
+ const runtime = require('../runtime')
3
4
 
4
5
  module.exports = function (entry, parentURL, opts = {}) {
5
- const runtime = require('#runtime')
6
-
7
6
  const {
8
7
  linked = false,
9
8
  platform = runtime.platform,
10
9
  arch = runtime.arch,
11
- simulator = false,
10
+ simulator = runtime.simulator,
12
11
  host = `${platform}-${arch}${simulator ? '-simulator' : ''}`,
13
12
  target = [host]
14
13
  } = opts
@@ -0,0 +1,10 @@
1
+ import { type Import } from 'bare-module-lexer'
2
+ import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
3
+
4
+ declare function resolve(
5
+ entry: Import,
6
+ parentURL: URL,
7
+ opts?: ResolveOptions
8
+ ): Resolver
9
+
10
+ export = resolve
@@ -0,0 +1,21 @@
1
+ import { type Import } from 'bare-module-lexer'
2
+ import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
3
+
4
+ interface NodeResolveOptions extends ResolveOptions {
5
+ platform?: string
6
+ arch?: string
7
+ simulator?: boolean
8
+ target?: string[]
9
+ }
10
+
11
+ declare function resolve(
12
+ entry: Import,
13
+ parentURL: URL,
14
+ opts?: NodeResolveOptions
15
+ ): Resolver
16
+
17
+ declare namespace resolve {
18
+ export { type NodeResolveOptions }
19
+ }
20
+
21
+ export = resolve
@@ -1,13 +1,12 @@
1
1
  const lex = require('bare-module-lexer')
2
2
  const resolve = require('../resolve')
3
+ const runtime = require('../runtime')
3
4
 
4
5
  module.exports = function (entry, parentURL, opts = {}) {
5
- const runtime = require('#runtime')
6
-
7
6
  const {
8
7
  platform = runtime.platform,
9
8
  arch = runtime.arch,
10
- simulator = false,
9
+ simulator = runtime.simulator,
11
10
  host = `${platform}-${arch}${simulator ? '-simulator' : ''}`,
12
11
  target = [host]
13
12
  } = opts
@@ -0,0 +1,8 @@
1
+ import { module } from 'bare-module-resolve'
2
+ import { addon } from 'bare-addon-resolve'
3
+
4
+ import defaultResolve from './resolve/default'
5
+ import bare from './resolve/bare'
6
+ import node from './resolve/node'
7
+
8
+ export { module, addon, defaultResolve as default, bare, node }
@@ -1,3 +1,3 @@
1
- /* global Bare */
2
1
  exports.platform = Bare.platform
3
2
  exports.arch = Bare.arch
3
+ exports.simulator = Bare.simulator
@@ -1,2 +1,3 @@
1
1
  exports.platform = process.platform
2
2
  exports.arch = process.arch
3
+ exports.simulator = false
package/lib/runtime.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('#runtime')
package/package.json CHANGED
@@ -1,17 +1,33 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
- ".": "./index.js",
7
- "./package": "./package.json",
8
- "./resolve": "./lib/resolve.js",
9
- "./resolve/default": "./lib/resolve/default.js",
10
- "./resolve/bare": "./lib/resolve/bare.js",
11
- "./resolve/node": "./lib/resolve/node.js"
6
+ ".": {
7
+ "types": "./index.d.ts",
8
+ "default": "./index.js"
9
+ },
10
+ "./resolve": {
11
+ "types": "./lib/resolve.d.ts",
12
+ "default": "./lib/resolve.js"
13
+ },
14
+ "./resolve/default": {
15
+ "types": "./lib/resolve/default.d.ts",
16
+ "default": "./lib/resolve/default.js"
17
+ },
18
+ "./resolve/bare": {
19
+ "types": "./lib/resolve/bare.d.ts",
20
+ "default": "./lib/resolve/bare.js"
21
+ },
22
+ "./resolve/node": {
23
+ "types": "./lib/resolve/node.d.ts",
24
+ "default": "./lib/resolve/node.js"
25
+ },
26
+ "./package": "./package.json"
12
27
  },
13
28
  "files": [
14
29
  "index.js",
30
+ "index.d.ts",
15
31
  "lib"
16
32
  ],
17
33
  "imports": {
@@ -38,12 +54,26 @@
38
54
  "homepage": "https://github.com/holepunchto/bare-module-traverse#readme",
39
55
  "dependencies": {
40
56
  "bare-addon-resolve": "^1.5.0",
41
- "bare-module-lexer": "^1.0.0",
57
+ "bare-module-lexer": "^1.4.0",
42
58
  "bare-module-resolve": "^1.7.0"
43
59
  },
44
60
  "devDependencies": {
61
+ "bare-buffer": "^3.0.2",
62
+ "bare-url": "^2.1.3",
45
63
  "brittle": "^3.2.1",
46
64
  "prettier": "^3.4.2",
47
65
  "prettier-config-standard": "^7.0.0"
66
+ },
67
+ "peerDependencies": {
68
+ "bare-buffer": "*",
69
+ "bare-url": "*"
70
+ },
71
+ "peerDependenciesMeta": {
72
+ "bare-buffer": {
73
+ "optional": true
74
+ },
75
+ "bare-url": {
76
+ "optional": true
77
+ }
48
78
  }
49
79
  }