@endo/compartment-mapper 1.5.0 → 1.6.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.
Files changed (71) hide show
  1. package/README.md +278 -111
  2. package/bundle.d.ts +1 -1
  3. package/bundle.js +4 -1
  4. package/functor-lite.d.ts +3 -0
  5. package/functor-lite.d.ts.map +1 -0
  6. package/functor-lite.js +4 -0
  7. package/functor.d.ts +3 -0
  8. package/functor.d.ts.map +1 -0
  9. package/functor.js +4 -0
  10. package/index.d.ts +1 -1
  11. package/index.js +4 -1
  12. package/package.json +8 -4
  13. package/script-lite.d.ts +3 -0
  14. package/script-lite.d.ts.map +1 -0
  15. package/script-lite.js +4 -0
  16. package/script.d.ts +3 -0
  17. package/script.d.ts.map +1 -0
  18. package/script.js +4 -0
  19. package/src/archive-lite.js +1 -1
  20. package/src/bundle-cjs.d.ts +1 -1
  21. package/src/bundle-cjs.d.ts.map +1 -1
  22. package/src/bundle-cjs.js +57 -28
  23. package/src/bundle-json.d.ts.map +1 -1
  24. package/src/bundle-json.js +2 -3
  25. package/src/bundle-lite.d.ts +91 -0
  26. package/src/bundle-lite.d.ts.map +1 -0
  27. package/src/bundle-lite.js +668 -0
  28. package/src/bundle-mjs.d.ts +2 -2
  29. package/src/bundle-mjs.d.ts.map +1 -1
  30. package/src/bundle-mjs.js +36 -19
  31. package/src/bundle.d.ts +48 -10
  32. package/src/bundle.d.ts.map +1 -1
  33. package/src/bundle.js +391 -125
  34. package/src/capture-lite.js +1 -1
  35. package/src/import-hook.d.ts +3 -16
  36. package/src/import-hook.d.ts.map +1 -1
  37. package/src/import-hook.js +11 -18
  38. package/src/import-lite.d.ts.map +1 -1
  39. package/src/import-lite.js +4 -2
  40. package/src/node-modules.d.ts +4 -47
  41. package/src/node-modules.d.ts.map +1 -1
  42. package/src/node-modules.js +121 -121
  43. package/src/parse-archive-cjs.d.ts.map +1 -1
  44. package/src/parse-archive-cjs.js +8 -3
  45. package/src/parse-cjs-shared-export-wrapper.d.ts.map +1 -1
  46. package/src/parse-cjs-shared-export-wrapper.js +2 -10
  47. package/src/parse-cjs.js +1 -1
  48. package/src/parse-mjs.js +2 -2
  49. package/src/policy.d.ts.map +1 -1
  50. package/src/policy.js +4 -7
  51. package/src/search.d.ts +6 -12
  52. package/src/search.d.ts.map +1 -1
  53. package/src/search.js +29 -12
  54. package/src/types/compartment-map-schema.d.ts +5 -0
  55. package/src/types/compartment-map-schema.d.ts.map +1 -1
  56. package/src/types/compartment-map-schema.ts +5 -0
  57. package/src/types/external.d.ts +112 -8
  58. package/src/types/external.d.ts.map +1 -1
  59. package/src/types/external.ts +127 -8
  60. package/src/types/internal.d.ts +85 -13
  61. package/src/types/internal.d.ts.map +1 -1
  62. package/src/types/internal.ts +106 -13
  63. package/src/types/node-modules.d.ts +79 -0
  64. package/src/types/node-modules.d.ts.map +1 -0
  65. package/src/types/node-modules.ts +89 -0
  66. package/src/types/node-powers.d.ts +4 -4
  67. package/src/types/node-powers.d.ts.map +1 -1
  68. package/src/types/node-powers.ts +4 -4
  69. package/src/types/powers.d.ts +2 -2
  70. package/src/types/powers.d.ts.map +1 -1
  71. package/src/types/powers.ts +2 -2
@@ -17,57 +17,27 @@
17
17
  * CompartmentDescriptor,
18
18
  * CompartmentMapDescriptor,
19
19
  * CompartmentMapForNodeModulesOptions,
20
- * Language,
21
20
  * LanguageForExtension,
22
21
  * MapNodeModulesOptions,
23
22
  * MaybeReadFn,
24
23
  * MaybeReadPowers,
25
- * ModuleDescriptor,
24
+ * PackageDescriptor,
25
+ * ReadDescriptorFn,
26
26
  * ReadFn,
27
27
  * ReadPowers,
28
- * ScopeDescriptor,
29
28
  * SomePackagePolicy,
30
29
  * SomePolicy,
31
30
  * } from './types.js'
32
- */
33
-
34
- /**
35
- * The graph is an intermediate object model that the functions of this module
36
- * build by exploring the `node_modules` tree dropped by tools like npm and
37
- * consumed by tools like Node.js.
38
- * This gets translated finally into a compartment map.
39
- *
40
- * @typedef {Record<string, Node>} Graph
41
- */
42
-
43
- /**
44
- * @typedef {object} Node
45
- * @property {string} label
46
- * @property {string} name
47
- * @property {Array<string>} path
48
- * @property {Array<string>} logicalPath
49
- * @property {boolean} explicitExports
50
- * @property {Record<string, string>} internalAliases
51
- * @property {Record<string, string>} externalAliases
52
- * @property {Record<string, string>} dependencyLocations - from module name to
53
- * location in storage.
54
- * @property {LanguageForExtension} parsers - the parser for
55
- * modules based on their extension.
56
- * @property {Record<string, Language>} types - the parser for specific
57
- * modules.
58
- */
59
-
60
- /**
61
- * @typedef {object} LanguageOptions
62
- * @property {LanguageForExtension} commonjsLanguageForExtension
63
- * @property {LanguageForExtension} moduleLanguageForExtension
64
- * @property {LanguageForExtension} workspaceCommonjsLanguageForExtension
65
- * @property {LanguageForExtension} workspaceModuleLanguageForExtension
66
- * @property {Set<string>} languages
67
- */
68
-
69
- /**
70
- * @typedef {Record<string, {spec: string, alias: string}>} CommonDependencyDescriptors
31
+ * @import {
32
+ * Graph,
33
+ * Node,
34
+ * LanguageOptions,
35
+ * CommonDependencyDescriptors,
36
+ * GatherDependencyOptions,
37
+ * GraphPackageOptions,
38
+ * GraphPackagesOptions,
39
+ * PackageDetails,
40
+ * } from './types/node-modules.js'
71
41
  */
72
42
 
73
43
  import { pathCompare } from './compartment-map.js';
@@ -90,6 +60,11 @@ const decoder = new TextDecoder();
90
60
  // q, as in quote, for enquoting strings in error messages.
91
61
  const q = JSON.stringify;
92
62
 
63
+ /**
64
+ * Default logger that does nothing.
65
+ */
66
+ const noop = () => {};
67
+
93
68
  /**
94
69
  * @param {string} rel - a relative URL
95
70
  * @param {string} abs - a fully qualified URL
@@ -99,12 +74,16 @@ const resolveLocation = (rel, abs) => {
99
74
  return new URL(rel, abs).toString();
100
75
  };
101
76
 
77
+ // Exported for testing:
102
78
  /**
103
79
  * @param {string} location
104
80
  * @returns {string}
105
81
  */
106
- const basename = location => {
107
- const { pathname } = new URL(location);
82
+ export const basename = location => {
83
+ let { pathname } = new URL(location);
84
+ if (pathname.endsWith('/')) {
85
+ pathname = pathname.slice(0, -1);
86
+ }
108
87
  const index = pathname.lastIndexOf('/');
109
88
  if (index < 0) {
110
89
  return pathname;
@@ -145,27 +124,19 @@ const readDescriptorWithMemo = async (memo, maybeRead, packageLocation) => {
145
124
  };
146
125
 
147
126
  /**
148
- * @callback ReadDescriptorFn
149
- * @param {string} packageLocation
150
- * @returns {Promise<object>}
151
- */
152
-
153
- /**
154
- * findPackage behaves as Node.js to find third-party modules by searching
127
+ * `findPackage` behaves as Node.js to find third-party modules by searching
155
128
  * parent to ancestor directories for a `node_modules` directory that contains
156
129
  * the name.
130
+ *
157
131
  * Node.js does not actually require these to be packages, but in practice,
158
- * these are the locations that pakcage managers drop a package so Node.js can
132
+ * these are the locations that package managers drop a package so Node.js can
159
133
  * find it efficiently.
160
134
  *
161
135
  * @param {ReadDescriptorFn} readDescriptor
162
136
  * @param {CanonicalFn} canonical
163
137
  * @param {string} directory
164
138
  * @param {string} name
165
- * @returns {Promise<{
166
- * packageLocation: string,
167
- * packageDescriptor: object,
168
- * } | undefined>}
139
+ * @returns {Promise<PackageDetails|undefined>}
169
140
  */
170
141
  const findPackage = async (readDescriptor, canonical, directory, name) => {
171
142
  await null;
@@ -198,6 +169,7 @@ const findPackage = async (readDescriptor, canonical, directory, name) => {
198
169
  }
199
170
  };
200
171
 
172
+ /** @satisfies {LanguageForExtension} */
201
173
  const defaultLanguageForExtension = /** @type {const} */ ({
202
174
  mjs: 'mjs',
203
175
  cjs: 'cjs',
@@ -205,15 +177,19 @@ const defaultLanguageForExtension = /** @type {const} */ ({
205
177
  text: 'text',
206
178
  bytes: 'bytes',
207
179
  });
180
+
181
+ /** @satisfies {LanguageForExtension} */
208
182
  const defaultCommonjsLanguageForExtension = /** @type {const} */ ({
209
183
  js: 'cjs',
210
184
  });
185
+
186
+ /** @satisfies {LanguageForExtension} */
211
187
  const defaultModuleLanguageForExtension = /** @type {const} */ ({
212
188
  js: 'mjs',
213
189
  });
214
190
 
215
191
  /**
216
- * @param {object} descriptor
192
+ * @param {PackageDescriptor} descriptor
217
193
  * @param {string} location
218
194
  * @param {LanguageOptions} languageOptions
219
195
  * @returns {Record<string, string>}
@@ -278,7 +254,7 @@ const inferParsers = (descriptor, location, languageOptions) => {
278
254
  };
279
255
 
280
256
  /**
281
- * graphPackage and gatherDependency are mutually recursive functions that
257
+ * `graphPackage` and {@link gatherDependency} are mutually recursive functions that
282
258
  * gather the metadata for a package and its transitive dependencies.
283
259
  * The keys of the graph are the locations of the package descriptors.
284
260
  * The metadata include a label (which is informative and not necessarily
@@ -289,16 +265,12 @@ const inferParsers = (descriptor, location, languageOptions) => {
289
265
  * @param {ReadDescriptorFn} readDescriptor
290
266
  * @param {CanonicalFn} canonical
291
267
  * @param {Graph} graph
292
- * @param {object} packageDetails
293
- * @param {string} packageDetails.packageLocation
294
- * @param {object} packageDetails.packageDescriptor
268
+ * @param {PackageDetails} packageDetails
295
269
  * @param {Set<string>} conditions
296
270
  * @param {boolean | undefined} dev
297
- * @param {CommonDependencyDescriptors} commonDependencyDescriptors
298
271
  * @param {LanguageOptions} languageOptions
299
272
  * @param {boolean} strict
300
- * @param {Map<string, Array<string>>} preferredPackageLogicalPathMap
301
- * @param {Array<string>} logicalPath
273
+ * @param {GraphPackageOptions} options
302
274
  * @returns {Promise<undefined>}
303
275
  */
304
276
  const graphPackage = async (
@@ -309,11 +281,14 @@ const graphPackage = async (
309
281
  { packageLocation, packageDescriptor },
310
282
  conditions,
311
283
  dev,
312
- commonDependencyDescriptors,
313
284
  languageOptions,
314
285
  strict,
315
- preferredPackageLogicalPathMap = new Map(),
316
- logicalPath = [],
286
+ {
287
+ commonDependencyDescriptors = {},
288
+ preferredPackageLogicalPathMap = new Map(),
289
+ logicalPath = [],
290
+ log = noop,
291
+ } = {},
317
292
  ) => {
318
293
  if (graph[packageLocation] !== undefined) {
319
294
  // Returning the promise here would create a causal cycle and stall recursion.
@@ -321,20 +296,19 @@ const graphPackage = async (
321
296
  }
322
297
 
323
298
  if (packageDescriptor.name !== name) {
324
- console.warn(
325
- `Package named ${q(
326
- name,
327
- )} does not match location ${packageLocation} got (${q(
328
- packageDescriptor.name,
329
- )})`,
330
- );
299
+ log('Package name does not match location', {
300
+ name,
301
+ packageDescriptorName: packageDescriptor.name,
302
+ packageLocation,
303
+ });
331
304
  }
332
305
 
333
- const result = {};
334
- graph[packageLocation] = /** @type {Node} */ (result);
306
+ const result = /** @type {Node} */ ({});
307
+ graph[packageLocation] = result;
335
308
 
336
- /** @type {Record<string, string>} */
309
+ /** @type {Node['dependencyLocations']} */
337
310
  const dependencyLocations = {};
311
+ /** @type {ReturnType<typeof gatherDependency>[]} */
338
312
  const children = [];
339
313
  const optionals = new Set();
340
314
  const {
@@ -387,17 +361,24 @@ const graphPackage = async (
387
361
  preferredPackageLogicalPathMap,
388
362
  languageOptions,
389
363
  strict,
390
- childLogicalPath,
391
- optional,
392
- commonDependencyDescriptors,
364
+ {
365
+ childLogicalPath,
366
+ optional,
367
+ commonDependencyDescriptors,
368
+ log,
369
+ },
393
370
  ),
394
371
  );
395
372
  }
396
373
 
397
374
  const { version = '', exports: exportsDescriptor } = packageDescriptor;
398
- /** @type {Record<string, Language>} */
375
+ /** @type {Node['types']} */
399
376
  const types = {};
400
377
 
378
+ /**
379
+ * @param {string} path
380
+ * @returns {Promise<PackageDescriptor>}
381
+ */
401
382
  const readDescriptorUpwards = async path => {
402
383
  const location = resolveLocation(path, packageLocation);
403
384
  // readDescriptor coming from above is memoized, so this is not awfully slow
@@ -405,9 +386,9 @@ const graphPackage = async (
405
386
  return data;
406
387
  };
407
388
 
408
- /** @type {Record<string, string>} */
389
+ /** @type {Node['externalAliases']} */
409
390
  const externalAliases = {};
410
- /** @type {Record<string, string>} */
391
+ /** @type {Node['internalAliases']} */
411
392
  const internalAliases = {};
412
393
 
413
394
  inferExportsAndAliases(
@@ -424,10 +405,13 @@ const graphPackage = async (
424
405
  languageOptions,
425
406
  );
426
407
 
408
+ const sourceDirname = basename(packageLocation);
409
+
427
410
  Object.assign(result, {
428
411
  name,
429
412
  path: logicalPath,
430
413
  label: `${name}${version ? `-v${version}` : ''}`,
414
+ sourceDirname,
431
415
  explicitExports: exportsDescriptor !== undefined,
432
416
  externalAliases,
433
417
  internalAliases,
@@ -479,6 +463,8 @@ const graphPackage = async (
479
463
  };
480
464
 
481
465
  /**
466
+ * Adds information for the dependency of the package at `packageLocation` to the `graph` object.
467
+ *
482
468
  * @param {ReadDescriptorFn} readDescriptor
483
469
  * @param {CanonicalFn} canonical
484
470
  * @param {Graph} graph - the partially build graph.
@@ -488,10 +474,9 @@ const graphPackage = async (
488
474
  * @param {Set<string>} conditions
489
475
  * @param {Map<string, Array<string>>} preferredPackageLogicalPathMap
490
476
  * @param {LanguageOptions} languageOptions
491
- * @param {boolean} strict
492
- * @param {Array<string>} [childLogicalPath]
493
- * @param {boolean} [optional] - whether the dependency is optional
494
- * @param {object} [commonDependencyDescriptors] - dependencies to be added to all packages
477
+ * @param {boolean} strict - If `true`, a missing dependency will throw an exception
478
+ * @param {GatherDependencyOptions} options
479
+ * @returns {Promise<void>}
495
480
  */
496
481
  const gatherDependency = async (
497
482
  readDescriptor,
@@ -504,9 +489,12 @@ const gatherDependency = async (
504
489
  preferredPackageLogicalPathMap,
505
490
  languageOptions,
506
491
  strict,
507
- childLogicalPath = [],
508
- optional = false,
509
- commonDependencyDescriptors = undefined,
492
+ {
493
+ childLogicalPath = [],
494
+ optional = false,
495
+ commonDependencyDescriptors = {},
496
+ log = noop,
497
+ } = {},
510
498
  ) => {
511
499
  const dependency = await findPackage(
512
500
  readDescriptor,
@@ -539,34 +527,35 @@ const gatherDependency = async (
539
527
  dependency,
540
528
  conditions,
541
529
  false,
542
- commonDependencyDescriptors,
543
530
  languageOptions,
544
531
  strict,
545
- preferredPackageLogicalPathMap,
546
- childLogicalPath,
532
+ {
533
+ commonDependencyDescriptors,
534
+ preferredPackageLogicalPathMap,
535
+ logicalPath: childLogicalPath,
536
+ log,
537
+ },
547
538
  );
548
539
  };
549
540
 
550
541
  /**
551
- * graphPackages returns a graph whose keys are nominally URLs, one per
552
- * package, with values that are label: (an informative Compartment name, built
553
- * as ${name}@${version}), dependencies: (a list of URLs), and exports: (an
554
- * object whose keys are the thing being imported, and the values are the names
555
- * of the matching module, relative to the containing package's root, that is,
556
- * the URL that was used as the key of graph).
557
- * The URLs in dependencies will all exist as other keys of graph.
542
+ * Resolves with a {@link Graph} representing the packages for which
543
+ * {@link CompartmentDescriptor CompartmentDescriptors} will be created.
558
544
  *
559
545
  * @param {MaybeReadFn} maybeRead
560
546
  * @param {CanonicalFn} canonical
561
547
  * @param {string} packageLocation - location of the main package.
562
548
  * @param {Set<string>} conditions
563
- * @param {object} mainPackageDescriptor - the parsed contents of the main
564
- * package.json, which was already read when searching for the package.json.
565
- * @param {boolean|undefined} dev - whether to use devDependencies from this package (and
566
- * only this package).
567
- * @param {Record<string,string>} commonDependencies - dependencies to be added to all packages
549
+ * @param {PackageDescriptor} mainPackageDescriptor - the parsed contents of the
550
+ * main `package.json`, which was already read when searching for the
551
+ * `package.json`.
552
+ * @param {boolean|undefined} dev - whether to use devDependencies from this
553
+ * package (and only this package).
554
+ * @param {Record<string,string>} commonDependencies - dependencies to be added
555
+ * to all packages
568
556
  * @param {LanguageOptions} languageOptions
569
557
  * @param {boolean} strict
558
+ * @param {GraphPackagesOptions} options
570
559
  */
571
560
  const graphPackages = async (
572
561
  maybeRead,
@@ -578,11 +567,12 @@ const graphPackages = async (
578
567
  commonDependencies,
579
568
  languageOptions,
580
569
  strict,
570
+ { log = noop } = {},
581
571
  ) => {
582
572
  const memo = create(null);
583
573
  /**
584
574
  * @param {string} packageLocation
585
- * @returns {Promise<object>}
575
+ * @returns {Promise<PackageDescriptor>}
586
576
  */
587
577
  const readDescriptor = packageLocation =>
588
578
  readDescriptorWithMemo(memo, maybeRead, packageLocation);
@@ -633,16 +623,19 @@ const graphPackages = async (
633
623
  },
634
624
  conditions,
635
625
  dev,
636
- commonDependencyDescriptors,
637
626
  languageOptions,
638
627
  strict,
628
+ {
629
+ commonDependencyDescriptors,
630
+ log,
631
+ },
639
632
  );
640
633
  return graph;
641
634
  };
642
635
 
643
636
  /**
644
- * translateGraph converts the graph returned by graph packages (above) into a
645
- * compartment map.
637
+ * `translateGraph` converts the graph returned by graph packages (above) into a
638
+ * {@link CompartmentMapDescriptor compartment map}.
646
639
  *
647
640
  * @param {string} entryPackageLocation
648
641
  * @param {string} entryModuleSpecifier
@@ -659,7 +652,7 @@ const translateGraph = (
659
652
  conditions,
660
653
  policy,
661
654
  ) => {
662
- /** @type {Record<string, CompartmentDescriptor>} */
655
+ /** @type {CompartmentMapDescriptor['compartments']} */
663
656
  const compartments = Object.create(null);
664
657
 
665
658
  // For each package, build a map of all the external modules the package can
@@ -676,14 +669,15 @@ const translateGraph = (
676
669
  name,
677
670
  path,
678
671
  label,
672
+ sourceDirname,
679
673
  dependencyLocations,
680
674
  internalAliases,
681
675
  parsers,
682
676
  types,
683
677
  } = graph[dependeeLocation];
684
- /** @type {Record<string, ModuleDescriptor>} */
678
+ /** @type {CompartmentDescriptor['modules']} */
685
679
  const moduleDescriptors = Object.create(null);
686
- /** @type {Record<string, ScopeDescriptor>} */
680
+ /** @type {CompartmentDescriptor['scopes']} */
687
681
  const scopes = Object.create(null);
688
682
 
689
683
  /**
@@ -770,6 +764,7 @@ const translateGraph = (
770
764
  name,
771
765
  path,
772
766
  location: dependeeLocation,
767
+ sourceDirname,
773
768
  modules: moduleDescriptors,
774
769
  scopes,
775
770
  parsers,
@@ -863,10 +858,11 @@ const makeLanguageOptions = ({
863
858
  * @param {ReadFn | ReadPowers | MaybeReadPowers} readPowers
864
859
  * @param {string} packageLocation
865
860
  * @param {Set<string>} conditionsOption
866
- * @param {object} packageDescriptor
861
+ * @param {PackageDescriptor} packageDescriptor
867
862
  * @param {string} moduleSpecifier
868
863
  * @param {CompartmentMapForNodeModulesOptions} [options]
869
864
  * @returns {Promise<CompartmentMapDescriptor>}
865
+ * @deprecated Use {@link mapNodeModules} instead.
870
866
  */
871
867
  export const compartmentMapForNodeModules = async (
872
868
  readPowers,
@@ -881,6 +877,7 @@ export const compartmentMapForNodeModules = async (
881
877
  commonDependencies = {},
882
878
  policy,
883
879
  strict = false,
880
+ log = noop,
884
881
  } = options;
885
882
  const { maybeRead, canonical } = unpackReadPowers(readPowers);
886
883
  const languageOptions = makeLanguageOptions(options);
@@ -902,6 +899,7 @@ export const compartmentMapForNodeModules = async (
902
899
  commonDependencies,
903
900
  languageOptions,
904
901
  strict,
902
+ { log },
905
903
  );
906
904
 
907
905
  if (policy) {
@@ -932,6 +930,11 @@ export const compartmentMapForNodeModules = async (
932
930
  };
933
931
 
934
932
  /**
933
+ * Creates a {@link CompartmentMapDescriptor} from the module at
934
+ * `moduleLocation`, considering dependencies found in `node_modules`.
935
+ *
936
+ * Locates the {@link PackageDescriptor} for the module at `moduleLocation`
937
+ *
935
938
  * @param {ReadFn | ReadPowers | MaybeReadPowers} readPowers
936
939
  * @param {string} moduleLocation
937
940
  * @param {MapNodeModulesOptions} [options]
@@ -940,20 +943,17 @@ export const compartmentMapForNodeModules = async (
940
943
  export const mapNodeModules = async (
941
944
  readPowers,
942
945
  moduleLocation,
943
- options = {},
946
+ { tags = new Set(), conditions = tags, log = noop, ...otherOptions } = {},
944
947
  ) => {
945
- const { tags = new Set(), conditions = tags, ...otherOptions } = options;
946
-
947
948
  const {
948
949
  packageLocation,
949
950
  packageDescriptorText,
950
951
  packageDescriptorLocation,
951
952
  moduleSpecifier,
952
- } = await search(readPowers, moduleLocation);
953
+ } = await search(readPowers, moduleLocation, { log });
953
954
 
954
- const packageDescriptor = parseLocatedJson(
955
- packageDescriptorText,
956
- packageDescriptorLocation,
955
+ const packageDescriptor = /** @type {PackageDescriptor} */ (
956
+ parseLocatedJson(packageDescriptorText, packageDescriptorLocation)
957
957
  );
958
958
 
959
959
  return compartmentMapForNodeModules(
@@ -962,6 +962,6 @@ export const mapNodeModules = async (
962
962
  conditions,
963
963
  packageDescriptor,
964
964
  moduleSpecifier,
965
- otherOptions,
965
+ { log, ...otherOptions },
966
966
  );
967
967
  };
@@ -1 +1 @@
1
- {"version":3,"file":"parse-archive-cjs.d.ts","sourceRoot":"","sources":["parse-archive-cjs.js"],"names":[],"mappings":"AAkBA,sBAAsB;AACtB,8BADW,OAAO,CA6ChB;wBAES,OAAO,YAAY,EAAE,oBAAoB;;6BA5DzB,YAAY"}
1
+ {"version":3,"file":"parse-archive-cjs.d.ts","sourceRoot":"","sources":["parse-archive-cjs.js"],"names":[],"mappings":"AAkBA,sBAAsB;AACtB,8BADW,OAAO,CAkDhB;wBAES,OAAO,YAAY,EAAE,oBAAoB;;6BAjEzB,YAAY"}
@@ -22,9 +22,12 @@ export const parseArchiveCjs = (
22
22
  _specifier,
23
23
  location,
24
24
  _packageLocation,
25
+ options = {},
25
26
  ) => {
26
27
  const source = textDecoder.decode(bytes);
27
28
 
29
+ const { archiveOnly = false } = options;
30
+
28
31
  const {
29
32
  requires: imports,
30
33
  exports,
@@ -35,18 +38,20 @@ export const parseArchiveCjs = (
35
38
  exports.push('default');
36
39
  }
37
40
 
38
- const cjsWrappedSource = `(function (require, exports, module, __filename, __dirname) { ${source} //*/\n})\n`;
41
+ let cjsFunctor = `(function (require, exports, module, __filename, __dirname) { 'use strict'; ${source} //*/\n})\n`;
39
42
 
40
43
  const pre = textEncoder.encode(
41
44
  JSON.stringify({
42
45
  imports,
43
46
  exports,
44
47
  reexports,
45
- source: cjsWrappedSource,
48
+ source: cjsFunctor,
46
49
  }),
47
50
  );
48
51
 
49
- const cjsFunctor = `${cjsWrappedSource}//# sourceURL=${location}\n`;
52
+ if (!archiveOnly) {
53
+ cjsFunctor = `${cjsFunctor}//# sourceURL=${location}\n`;
54
+ }
50
55
 
51
56
  return {
52
57
  parser: 'pre-cjs-json',
@@ -1 +1 @@
1
- {"version":3,"file":"parse-cjs-shared-export-wrapper.d.ts","sourceRoot":"","sources":["parse-cjs-shared-export-wrapper.js"],"names":[],"mappings":"AAwCO,2CAPI,UAAU,GAAG,MAAM,GAAG,SAAS,YAC/B,MAAM,GACJ;IACR,QAAQ,EAAC,MAAM,GAAC,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,GAAC,IAAI,CAAA;CACrB,CAgCH;AAmBM,uGAZJ;IAAmB,uBAAuB,EAAlC,MAAM;IACU,WAAW,EAA3B,WAAW;IACgB,eAAe,EAA1C,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IACX,QAAQ,EAAnB,MAAM;IAC8B,UAAU,EAA9C,MAAM,GAAG,UAAU,GAAG,SAAS;CACvC,GAAU;IACR,MAAM,EAAE;QAAE,OAAO,EAAE,GAAG,CAAA;KAAE,CAAC;IACzB,aAAa,EAAE,GAAG,CAAC;IACnB,YAAY,WAAW;IACvB,OAAO,WAAW;CACnB,CAmIH;gCApNqC,YAAY;4BAAZ,YAAY"}
1
+ {"version":3,"file":"parse-cjs-shared-export-wrapper.d.ts","sourceRoot":"","sources":["parse-cjs-shared-export-wrapper.js"],"names":[],"mappings":"AAwCO,2CAPI,UAAU,GAAG,MAAM,GAAG,SAAS,YAC/B,MAAM,GACJ;IACR,QAAQ,EAAC,MAAM,GAAC,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,GAAC,IAAI,CAAA;CACrB,CAgCH;AAmBM,uGAZJ;IAAmB,uBAAuB,EAAlC,MAAM;IACU,WAAW,EAA3B,WAAW;IACgB,eAAe,EAA1C,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IACX,QAAQ,EAAnB,MAAM;IAC8B,UAAU,EAA9C,MAAM,GAAG,UAAU,GAAG,SAAS;CACvC,GAAU;IACR,MAAM,EAAE;QAAE,OAAO,EAAE,GAAG,CAAA;KAAE,CAAC;IACzB,aAAa,EAAE,GAAG,CAAC;IACnB,YAAY,WAAW;IACvB,OAAO,WAAW;CACnB,CA2HH;gCA5MqC,YAAY;4BAAZ,YAAY"}
@@ -135,16 +135,7 @@ export const wrap = ({
135
135
  },
136
136
  });
137
137
 
138
- let finalExports = originalExports;
139
-
140
- const module = freeze({
141
- get exports() {
142
- return finalExports;
143
- },
144
- set exports(value) {
145
- finalExports = value;
146
- },
147
- });
138
+ const module = { exports: originalExports };
148
139
 
149
140
  /** @param {string} importSpecifier */
150
141
  const require = importSpecifier => {
@@ -192,6 +183,7 @@ export const wrap = ({
192
183
  freeze(require);
193
184
 
194
185
  const afterExecute = () => {
186
+ const finalExports = module.exports; // in case it's a getter, only call it once
195
187
  const exportsHaveBeenOverwritten = finalExports !== originalExports;
196
188
  // Promotes keys from redefined module.export to top level namespace for import *
197
189
  // Note: We could do it less consistently but closer to how node does it if we iterated over exports detected by
package/src/parse-cjs.js CHANGED
@@ -41,7 +41,7 @@ export const parseCjs = (
41
41
  */
42
42
  const execute = (moduleEnvironmentRecord, compartment, resolvedImports) => {
43
43
  const functor = compartment.evaluate(
44
- `(function (require, exports, module, __filename, __dirname) { ${source} //*/\n})\n//# sourceURL=${location}`,
44
+ `(function (require, exports, module, __filename, __dirname) { 'use strict'; ${source} })\n`,
45
45
  );
46
46
 
47
47
  const { require, moduleExports, module, afterExecute } = wrap({
package/src/parse-mjs.js CHANGED
@@ -14,10 +14,10 @@ export const parseMjs = (
14
14
  _packageLocation,
15
15
  options = {},
16
16
  ) => {
17
- const { sourceMap, sourceMapHook } = options;
17
+ const { sourceMap, sourceMapHook, archiveOnly = false } = options;
18
18
  const source = textDecoder.decode(bytes);
19
19
  const record = new ModuleSource(source, {
20
- sourceUrl,
20
+ sourceUrl: archiveOnly ? undefined : sourceUrl,
21
21
  sourceMap,
22
22
  sourceMapUrl: sourceUrl,
23
23
  sourceMapHook,
@@ -1 +1 @@
1
- {"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["policy.js"],"names":[],"mappings":"AAgCA;;GAEG;AACH,sDAAuD;AAyDhD,gEAFM,KAAK,CAAC,MAAM,CAAC,CAezB;AA0BM,qDAJI,gBAAgB,iBAChB,aAAa,GACX,OAAO,CASnB;;;;;;;;;AAKE,+CACQ,gBAAgB,UAChB,UAAU,GACR,iBAAiB,CAC7B;;;;;;;;;AAKE,+CACQ,gBAAgB,oCAEd,iBAAiB,GAAC,SAAS,CACvC;AAkFM,8DAJI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,0BAC3B,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,GACnC,2BAA2B,CA0CvC;AAoDM,6CARI,MAAM,WACN,MAAM,iBACN,aAAa,eACb,2BAA2B,eAC3B,KAAK,cAAS,SACd,MAAM,GACJ,IAAI,CAgDhB;AA2BM,+CAJI,MAAM,yBACN,qBAAqB,sEAiC/B;AAgDM,+CANI,MAAM,wBACN,OAAO,KAAK,EAAE,+BAA+B,UAC7C,aAAa,eACb,2BAA2B,GACzB,OAAO,CAAC,OAAO,KAAK,EAAE,+BAA+B,CAAC,CAyBlE;;;;;;;;;;;;;;4BAveS,YAAY;sCAAZ,YAAY;mCAAZ,YAAY;gCAAZ,YAAY;uCAAZ,YAAY;2CAAZ,YAAY;iDAAZ,YAAY"}
1
+ {"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["policy.js"],"names":[],"mappings":"AAgCA;;GAEG;AACH,sDAAuD;AAyDhD,gEAFM,KAAK,CAAC,MAAM,CAAC,CAezB;AA0BM,qDAJI,gBAAgB,iBAChB,aAAa,GACX,OAAO,CASnB;;;;;;;;;AAKE,+CACQ,gBAAgB,UAChB,UAAU,GACR,iBAAiB,CAC7B;;;;;;;;;AAKE,+CACQ,gBAAgB,oCAEd,iBAAiB,GAAC,SAAS,CACvC;AAkFM,8DAJI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,0BAC3B,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,GACnC,2BAA2B,CAuCvC;AAoDM,6CARI,MAAM,WACN,MAAM,iBACN,aAAa,eACb,2BAA2B,eAC3B,KAAK,cAAS,SACd,MAAM,GACJ,IAAI,CAgDhB;AA2BM,+CAJI,MAAM,yBACN,qBAAqB,sEAiC/B;AAgDM,+CANI,MAAM,wBACN,OAAO,KAAK,EAAE,+BAA+B,UAC7C,aAAa,eACb,2BAA2B,GACzB,OAAO,CAAC,OAAO,KAAK,EAAE,+BAA+B,CAAC,CAyBlE;;;;;;;;;;;;;;4BApeS,YAAY;sCAAZ,YAAY;mCAAZ,YAAY;gCAAZ,YAAY;uCAAZ,YAAY;2CAAZ,YAAY;iDAAZ,YAAY"}
package/src/policy.js CHANGED
@@ -240,6 +240,7 @@ export const makeDeferredAttenuatorsProvider = (
240
240
  compartments,
241
241
  compartmentDescriptors,
242
242
  ) => {
243
+ /** @type {DeferredAttenuatorsProvider['import']} */
243
244
  let importAttenuator;
244
245
  let defaultAttenuator;
245
246
  // Attenuators compartment is not created when there's no policy.
@@ -255,11 +256,6 @@ export const makeDeferredAttenuatorsProvider = (
255
256
  // At the time of this function being called, attenuators compartment won't
256
257
  // exist yet, we need to defer looking them up in the compartment to the
257
258
  // time of the import function being called.
258
- /**
259
- *
260
- * @param {string} attenuatorSpecifier
261
- * @returns {Promise<Attenuator>}
262
- */
263
259
  importAttenuator = async attenuatorSpecifier => {
264
260
  if (!attenuatorSpecifier) {
265
261
  if (!defaultAttenuator) {
@@ -267,8 +263,9 @@ export const makeDeferredAttenuatorsProvider = (
267
263
  }
268
264
  attenuatorSpecifier = defaultAttenuator;
269
265
  }
270
- const { namespace } =
271
- await compartments[ATTENUATORS_COMPARTMENT].import(attenuatorSpecifier);
266
+ const { namespace } = await compartments[ATTENUATORS_COMPARTMENT].import(
267
+ /** @type {string} */ (attenuatorSpecifier),
268
+ );
272
269
  return namespace;
273
270
  };
274
271
  }
package/src/search.d.ts CHANGED
@@ -1,16 +1,10 @@
1
- export function searchDescriptor<T>(location: string, maybeReadDescriptor: (location: string) => Promise<T | undefined>): Promise<{
2
- data: T;
3
- directory: string;
4
- location: string;
5
- packageDescriptorLocation: string;
6
- }>;
7
- export function search(readPowers: ReadFn | ReadPowers | MaybeReadPowers, moduleLocation: string): Promise<{
8
- packageLocation: string;
9
- packageDescriptorLocation: string;
10
- packageDescriptorText: string;
11
- moduleSpecifier: string;
12
- }>;
1
+ export function searchDescriptor<T>(location: string, maybeReadDescriptor: MaybeReadDescriptorFn<T>, { log: _log }?: SearchDescriptorOptions): Promise<SearchDescriptorResult<T>>;
2
+ export function search(readPowers: ReadFn | ReadPowers | MaybeReadPowers, moduleLocation: string, { log }?: import("./types.js").LogOptions | undefined): Promise<SearchResult>;
3
+ import type { MaybeReadDescriptorFn } from './types.js';
4
+ import type { SearchDescriptorOptions } from './types.js';
5
+ import type { SearchDescriptorResult } from './types.js';
13
6
  import type { ReadFn } from './types.js';
14
7
  import type { ReadPowers } from './types.js';
15
8
  import type { MaybeReadPowers } from './types.js';
9
+ import type { SearchResult } from './types.js';
16
10
  //# sourceMappingURL=search.d.ts.map