@endo/compartment-mapper 1.6.3 → 2.1.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 (95) hide show
  1. package/package.json +24 -14
  2. package/src/archive-lite.d.ts +7 -7
  3. package/src/archive-lite.d.ts.map +1 -1
  4. package/src/archive-lite.js +81 -30
  5. package/src/archive.d.ts.map +1 -1
  6. package/src/archive.js +7 -0
  7. package/src/bundle-lite.d.ts +3 -3
  8. package/src/bundle-lite.d.ts.map +1 -1
  9. package/src/bundle-lite.js +19 -24
  10. package/src/bundle.d.ts +3 -3
  11. package/src/bundle.d.ts.map +1 -1
  12. package/src/bundle.js +19 -24
  13. package/src/capture-lite.d.ts +2 -2
  14. package/src/capture-lite.d.ts.map +1 -1
  15. package/src/capture-lite.js +243 -25
  16. package/src/compartment-map.d.ts +9 -2
  17. package/src/compartment-map.d.ts.map +1 -1
  18. package/src/compartment-map.js +738 -254
  19. package/src/digest.d.ts +22 -2
  20. package/src/digest.d.ts.map +1 -1
  21. package/src/digest.js +180 -57
  22. package/src/generic-graph.d.ts +7 -25
  23. package/src/generic-graph.d.ts.map +1 -1
  24. package/src/generic-graph.js +83 -108
  25. package/src/guards.d.ts +18 -0
  26. package/src/guards.d.ts.map +1 -0
  27. package/src/guards.js +109 -0
  28. package/src/hooks.md +124 -0
  29. package/src/import-archive-lite.d.ts.map +1 -1
  30. package/src/import-archive-lite.js +15 -11
  31. package/src/import-archive.d.ts +5 -19
  32. package/src/import-archive.d.ts.map +1 -1
  33. package/src/import-archive.js +7 -27
  34. package/src/import-hook.d.ts +4 -3
  35. package/src/import-hook.d.ts.map +1 -1
  36. package/src/import-hook.js +140 -70
  37. package/src/import-lite.d.ts +6 -6
  38. package/src/import-lite.d.ts.map +1 -1
  39. package/src/import-lite.js +8 -5
  40. package/src/import.d.ts +3 -3
  41. package/src/import.d.ts.map +1 -1
  42. package/src/import.js +16 -6
  43. package/src/infer-exports.d.ts +4 -2
  44. package/src/infer-exports.d.ts.map +1 -1
  45. package/src/infer-exports.js +172 -23
  46. package/src/link.d.ts +4 -3
  47. package/src/link.d.ts.map +1 -1
  48. package/src/link.js +122 -52
  49. package/src/node-modules.d.ts +4 -3
  50. package/src/node-modules.d.ts.map +1 -1
  51. package/src/node-modules.js +513 -151
  52. package/src/parse-cjs-shared-export-wrapper.d.ts.map +1 -1
  53. package/src/parse-cjs-shared-export-wrapper.js +3 -1
  54. package/src/pattern-replacement.d.ts +6 -0
  55. package/src/pattern-replacement.d.ts.map +1 -0
  56. package/src/pattern-replacement.js +198 -0
  57. package/src/policy-format.d.ts +22 -5
  58. package/src/policy-format.d.ts.map +1 -1
  59. package/src/policy-format.js +342 -108
  60. package/src/policy.d.ts +13 -28
  61. package/src/policy.d.ts.map +1 -1
  62. package/src/policy.js +161 -106
  63. package/src/types/canonical-name.d.ts +97 -0
  64. package/src/types/canonical-name.d.ts.map +1 -0
  65. package/src/types/canonical-name.ts +151 -0
  66. package/src/types/compartment-map-schema.d.ts +121 -35
  67. package/src/types/compartment-map-schema.d.ts.map +1 -1
  68. package/src/types/compartment-map-schema.ts +211 -37
  69. package/src/types/external.d.ts +240 -76
  70. package/src/types/external.d.ts.map +1 -1
  71. package/src/types/external.ts +305 -74
  72. package/src/types/generic-graph.d.ts +8 -2
  73. package/src/types/generic-graph.d.ts.map +1 -1
  74. package/src/types/generic-graph.ts +7 -2
  75. package/src/types/internal.d.ts +31 -50
  76. package/src/types/internal.d.ts.map +1 -1
  77. package/src/types/internal.ts +60 -58
  78. package/src/types/node-modules.d.ts +112 -14
  79. package/src/types/node-modules.d.ts.map +1 -1
  80. package/src/types/node-modules.ts +152 -13
  81. package/src/types/pattern-replacement.d.ts +62 -0
  82. package/src/types/pattern-replacement.d.ts.map +1 -0
  83. package/src/types/pattern-replacement.ts +70 -0
  84. package/src/types/policy-schema.d.ts +26 -11
  85. package/src/types/policy-schema.d.ts.map +1 -1
  86. package/src/types/policy-schema.ts +29 -16
  87. package/src/types/policy.d.ts +6 -2
  88. package/src/types/policy.d.ts.map +1 -1
  89. package/src/types/policy.ts +7 -2
  90. package/src/types/powers.d.ts +11 -9
  91. package/src/types/powers.d.ts.map +1 -1
  92. package/src/types/powers.ts +11 -10
  93. package/src/types/typescript.d.ts +28 -0
  94. package/src/types/typescript.d.ts.map +1 -1
  95. package/src/types/typescript.ts +37 -1
@@ -1,24 +1,10 @@
1
- export function parseArchive(archiveBytes: Uint8Array, archiveLocation?: string, options?: Options): Promise<Application>;
2
- export function loadArchive(readPowers: import("@endo/zip").ReadFn | ReadPowers, archiveLocation: string, options?: LoadArchiveOptions): Promise<Application>;
3
- export function importArchive(readPowers: import("@endo/zip").ReadFn | ReadPowers, archiveLocation: string, options: ExecuteOptions & LoadArchiveOptions): Promise<object>;
4
- export type CompartmentConstructor = typeof Compartment;
5
- export type Options = {
6
- expectedSha512?: string | undefined;
7
- computeSha512?: HashFn | undefined;
8
- modules?: Record<string, unknown> | undefined;
9
- importHook?: ExitModuleImportHook | undefined;
10
- Compartment?: typeof Compartment | undefined;
11
- computeSourceLocation?: ComputeSourceLocationHook | undefined;
12
- computeSourceMapLocation?: ComputeSourceMapLocationHook | undefined;
13
- parserForLanguage?: ParserForLanguage | undefined;
14
- };
1
+ export function parseArchive(archiveBytes: Uint8Array, archiveLocation?: string, options?: ParseArchiveOptions): Promise<Application>;
2
+ export function loadArchive(readPowers: ReadFn | ReadPowers, archiveLocation: string, options?: LoadArchiveOptions): Promise<Application>;
3
+ export function importArchive(readPowers: ReadFn | ReadPowers, archiveLocation: string, options: ExecuteOptions & LoadArchiveOptions): Promise<object>;
4
+ import type { ParseArchiveOptions } from './types.js';
15
5
  import type { Application } from './types.js';
6
+ import type { ReadFn } from './types.js';
16
7
  import type { ReadPowers } from './types.js';
17
8
  import type { LoadArchiveOptions } from './types.js';
18
9
  import type { ExecuteOptions } from './types.js';
19
- import type { HashFn } from './types.js';
20
- import type { ExitModuleImportHook } from './types.js';
21
- import type { ComputeSourceLocationHook } from './types.js';
22
- import type { ComputeSourceMapLocationHook } from './types.js';
23
- import type { ParserForLanguage } from './types.js';
24
10
  //# sourceMappingURL=import-archive.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"import-archive.d.ts","sourceRoot":"","sources":["import-archive.js"],"names":[],"mappings":"AA+EO,2CALI,UAAU,oBACV,MAAM,YACN,OAAO,GACL,OAAO,CAAC,WAAW,CAAC,CAW9B;AAQI,wCALI,OAAO,WAAW,EAAE,MAAM,GAAG,UAAU,mBACvC,MAAM,YACN,kBAAkB,GAChB,OAAO,CAAC,WAAW,CAAC,CAO9B;AAQI,0CALI,OAAO,WAAW,EAAE,MAAM,GAAG,UAAU,mBACvC,MAAM,WACN,cAAc,GAAG,kBAAkB,GACjC,OAAO,CAAC,MAAM,CAAC,CAOzB;qCArEW,OAAO,WAAW;;;;;;;;;;;iCAhBtB,YAAY;gCAAZ,YAAY;wCAAZ,YAAY;oCAAZ,YAAY;4BAAZ,YAAY;0CAAZ,YAAY;+CAAZ,YAAY;kDAAZ,YAAY;uCAAZ,YAAY"}
1
+ {"version":3,"file":"import-archive.d.ts","sourceRoot":"","sources":["import-archive.js"],"names":[],"mappings":"AA2DO,2CALI,UAAU,oBACV,MAAM,YACN,mBAAmB,GACjB,OAAO,CAAC,WAAW,CAAC,CAW9B;AAQI,wCALI,MAAM,GAAG,UAAU,mBACnB,MAAM,YACN,kBAAkB,GAChB,OAAO,CAAC,WAAW,CAAC,CAO9B;AAQI,0CALI,MAAM,GAAG,UAAU,mBACnB,MAAM,WACN,cAAc,GAAG,kBAAkB,GACjC,OAAO,CAAC,MAAM,CAAC,CAOzB;yCAnEO,YAAY;iCAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;wCAAZ,YAAY;oCAAZ,YAAY"}
@@ -19,14 +19,12 @@
19
19
  /**
20
20
  * @import {
21
21
  * Application,
22
- * ComputeSourceLocationHook,
23
- * ComputeSourceMapLocationHook,
24
22
  * ExecuteOptions,
25
- * ExitModuleImportHook,
26
- * HashFn,
27
23
  * LoadArchiveOptions,
28
24
  * ReadPowers,
29
25
  * ParserForLanguage,
26
+ * ParseArchiveOptions,
27
+ * ReadFn,
30
28
  * } from './types.js'
31
29
  */
32
30
 
@@ -39,28 +37,10 @@ import {
39
37
 
40
38
  const { assign, create, freeze } = Object;
41
39
 
42
- // Must give the type of Compartment a name to capture the external meaning of
43
- // Compartment Otherwise @param {typeof Compartment} takes the Compartment to
44
- // mean the const variable defined within the function.
45
- //
46
- /** @typedef {typeof Compartment} CompartmentConstructor */
47
-
48
- /**
49
- * @typedef {object} Options
50
- * @property {string} [expectedSha512]
51
- * @property {HashFn} [computeSha512]
52
- * @property {Record<string, unknown>} [modules]
53
- * @property {ExitModuleImportHook} [importHook]
54
- * @property {CompartmentConstructor} [Compartment]
55
- * @property {ComputeSourceLocationHook} [computeSourceLocation]
56
- * @property {ComputeSourceMapLocationHook} [computeSourceMapLocation]
57
- * @property {ParserForLanguage} [parserForLanguage]
58
- */
59
-
60
40
  /**
61
41
  * Add the default parserForLanguage option.
62
- * @param {Options} [options]
63
- * @returns {Options}
42
+ * @param {ParseArchiveOptions} [options]
43
+ * @returns {ParseArchiveOptions}
64
44
  */
65
45
  const assignParserForLanguage = (options = {}) => {
66
46
  const { parserForLanguage: parserForLanguageOption, ...rest } = options;
@@ -74,7 +54,7 @@ const assignParserForLanguage = (options = {}) => {
74
54
  /**
75
55
  * @param {Uint8Array} archiveBytes
76
56
  * @param {string} [archiveLocation]
77
- * @param {Options} [options]
57
+ * @param {ParseArchiveOptions} [options]
78
58
  * @returns {Promise<Application>}
79
59
  */
80
60
  export const parseArchive = async (
@@ -89,7 +69,7 @@ export const parseArchive = async (
89
69
  );
90
70
 
91
71
  /**
92
- * @param {import('@endo/zip').ReadFn | ReadPowers} readPowers
72
+ * @param {ReadFn | ReadPowers} readPowers
93
73
  * @param {string} archiveLocation
94
74
  * @param {LoadArchiveOptions} [options]
95
75
  * @returns {Promise<Application>}
@@ -102,7 +82,7 @@ export const loadArchive = async (readPowers, archiveLocation, options) =>
102
82
  );
103
83
 
104
84
  /**
105
- * @param {import('@endo/zip').ReadFn | ReadPowers} readPowers
85
+ * @param {ReadFn | ReadPowers} readPowers
106
86
  * @param {string} archiveLocation
107
87
  * @param {ExecuteOptions & LoadArchiveOptions} options
108
88
  * @returns {Promise<object>}
@@ -2,18 +2,19 @@
2
2
  * Synchronous import for dynamic requires.
3
3
  *
4
4
  * @param {ReadNowPowers} readPowers
5
- * @param {string} baseLocation
5
+ * @param {FileUrlString} baseLocation
6
6
  * @param {MakeImportNowHookMakerOptions} options
7
7
  * @returns {ImportNowHookMaker}
8
8
  */
9
- export function makeImportNowHookMaker(readPowers: ReadNowPowers, baseLocation: string, { sources, compartmentDescriptors, computeSha512, searchSuffixes, archiveOnly, sourceMapHook, importNowHook: exitModuleImportNowHook, }: MakeImportNowHookMakerOptions): ImportNowHookMaker;
9
+ export function makeImportNowHookMaker(readPowers: ReadNowPowers, baseLocation: FileUrlString, { sources, compartmentDescriptors, computeSha512, searchSuffixes, archiveOnly, sourceMapHook, importNowHook: exitModuleImportNowHook, moduleSourceHook, log, }: MakeImportNowHookMakerOptions): ImportNowHookMaker;
10
10
  export function exitModuleImportHookMaker({ modules, exitModuleImportHook, entryCompartmentName, }: {
11
11
  modules?: Record<string, any> | undefined;
12
12
  exitModuleImportHook?: ExitModuleImportHook | undefined;
13
13
  entryCompartmentName: string;
14
14
  }): ExitModuleImportHook | undefined;
15
- export function makeImportHookMaker(readPowers: ReadFn | ReadPowers, baseLocation: string, { sources, compartmentDescriptors, archiveOnly, computeSha512, searchSuffixes, sourceMapHook, entryCompartmentName, entryModuleSpecifier, importHook: exitModuleImportHook, }: MakeImportHookMakerOptions): ImportHookMaker;
15
+ export function makeImportHookMaker(readPowers: ReadFn | ReadPowers, baseLocation: FileUrlString, { sources, compartmentDescriptors, archiveOnly, computeSha512, searchSuffixes, sourceMapHook, entryCompartmentName, entryModuleSpecifier, importHook: exitModuleImportHook, moduleSourceHook, log, }: MakeImportHookMakerOptions): ImportHookMaker;
16
16
  import type { ReadNowPowers } from './types.js';
17
+ import type { FileUrlString } from './types.js';
17
18
  import type { MakeImportNowHookMakerOptions } from './types.js';
18
19
  import type { ImportNowHookMaker } from './types.js';
19
20
  import type { ExitModuleImportHook } from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"import-hook.d.ts","sourceRoot":"","sources":["import-hook.js"],"names":[],"mappings":"AAipBA;;;;;;;GAOG;AACH,mDALW,aAAa,gBACb,MAAM,2IACN,6BAA6B,GAC3B,kBAAkB,CAmN9B;AAlpBM,oGALJ;IAAqC,OAAO;IACN,oBAAoB;IACnC,oBAAoB,EAAnC,MAAM;CACd,GAAU,oBAAoB,GAAC,SAAS,CA8B1C;AA+PM,gDALI,MAAM,GAAC,UAAU,gBACjB,MAAM,iLACN,0BAA0B,GACxB,eAAe,CA8J3B;mCAxmBS,YAAY;mDAAZ,YAAY;wCAAZ,YAAY;0CAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;gDAAZ,YAAY;qCAAZ,YAAY"}
1
+ {"version":3,"file":"import-hook.d.ts","sourceRoot":"","sources":["import-hook.js"],"names":[],"mappings":"AAquBA;;;;;;;GAOG;AACH,mDALW,aAAa,gBACb,aAAa,kKACb,6BAA6B,GAC3B,kBAAkB,CAqM9B;AA9tBM,oGALJ;IAAqC,OAAO;IACN,oBAAoB;IACnC,oBAAoB,EAAnC,MAAM;CACd,GAAU,oBAAoB,GAAC,SAAS,CA8B1C;AA0UM,gDALI,MAAM,GAAC,UAAU,gBACjB,aAAa,wMACb,0BAA0B,GACxB,eAAe,CA6K3B;mCAtrBS,YAAY;mCAAZ,YAAY;mDAAZ,YAAY;wCAAZ,YAAY;0CAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;gDAAZ,YAAY;qCAAZ,YAAY"}
@@ -29,14 +29,20 @@
29
29
  * ImportNowHookMaker,
30
30
  * MakeImportHookMakerOptions,
31
31
  * MakeImportNowHookMakerOptions,
32
- * ModuleDescriptor,
33
32
  * ParseResult,
34
33
  * ReadFn,
35
34
  * ReadPowers,
36
35
  * ReadNowPowers,
37
36
  * StrictlyRequiredFn,
37
+ * DeferErrorFn,
38
+ * ErrorModuleSource,
39
+ * FileUrlString,
38
40
  * CompartmentSources,
39
- * DeferErrorFn
41
+ * CompartmentModuleConfiguration,
42
+ * LogOptions,
43
+ * CanonicalName,
44
+ * LocalModuleSource,
45
+ * ModuleSourceHook,
40
46
  * } from './types.js'
41
47
  */
42
48
 
@@ -44,14 +50,14 @@ import { asyncTrampoline, syncTrampoline } from '@endo/trampoline';
44
50
  import { resolve } from './node-module-specifier.js';
45
51
  import {
46
52
  attenuateModuleHook,
47
- ATTENUATORS_COMPARTMENT,
48
- enforceModulePolicy,
53
+ enforcePolicyByModule,
54
+ enforcePackagePolicyByCanonicalName,
49
55
  } from './policy.js';
56
+ import { ATTENUATORS_COMPARTMENT } from './policy-format.js';
50
57
  import { unpackReadPowers } from './powers.js';
51
58
 
52
59
  // q, as in quote, for quoting strings in error messages.
53
- const q = JSON.stringify;
54
-
60
+ const { quote: q } = assert;
55
61
  const { apply } = Reflect;
56
62
 
57
63
  /**
@@ -62,7 +68,7 @@ const { apply } = Reflect;
62
68
  */
63
69
  const freeze = Object.freeze;
64
70
 
65
- const { entries, keys, assign, create } = Object;
71
+ const { keys, assign, create } = Object;
66
72
 
67
73
  const { hasOwnProperty } = Object.prototype;
68
74
  /**
@@ -71,12 +77,15 @@ const { hasOwnProperty } = Object.prototype;
71
77
  */
72
78
  const has = (haystack, needle) => apply(hasOwnProperty, haystack, [needle]);
73
79
 
80
+ const noop = () => {};
81
+
74
82
  /**
75
83
  * @param {string} rel - a relative URL
76
- * @param {string} abs - a fully qualified URL
77
- * @returns {string}
84
+ * @param {FileUrlString} abs - a fully qualified URL
85
+ * @returns {FileUrlString}
78
86
  */
79
- const resolveLocation = (rel, abs) => new URL(rel, abs).toString();
87
+ const resolveLocation = (rel, abs) =>
88
+ /** @type {FileUrlString} */ (new URL(rel, abs).toString());
80
89
 
81
90
  // this is annoying
82
91
  function getImportsFromRecord(record) {
@@ -98,14 +107,14 @@ const nodejsConventionSearchSuffixes = [
98
107
 
99
108
  /**
100
109
  * Returns `true` if `absoluteModuleSpecifier` is within the path `compartmentLocation`.
101
- * @param {string} absoluteModudeSpecifier Absolute path to module specifier
110
+ * @param {string} absoluteModuleSpecifier Absolute path to module specifier
102
111
  * @param {string} compartmentLocation Absolute path to compartment location
103
112
  * @returns {boolean}
104
113
  */
105
114
  const isLocationWithinCompartment = (
106
- absoluteModudeSpecifier,
115
+ absoluteModuleSpecifier,
107
116
  compartmentLocation,
108
- ) => absoluteModudeSpecifier.startsWith(compartmentLocation);
117
+ ) => absoluteModuleSpecifier.startsWith(compartmentLocation);
109
118
 
110
119
  /**
111
120
  * Computes the relative path to a module from its compartment location (including a leading `./`)
@@ -159,30 +168,15 @@ const findRedirect = ({
159
168
  return undefined;
160
169
  }
161
170
 
162
- // this tests the compartment referred to by the absolute path
163
- // is a dependency of the compartment descriptor
164
- if (compartmentDescriptor.compartments.has(location)) {
165
- return {
166
- specifier: relativeSpecifier(moduleSpecifierLocation, location),
167
- compartment: compartments[location],
168
- };
169
- }
170
-
171
- // this tests if the compartment descriptor is a dependency of the
172
- // compartment referred to by the absolute path.
173
- // it may be in scope, but disallowed by policy.
174
- if (
175
- someCompartmentDescriptor.compartments.has(
176
- compartmentDescriptor.location,
177
- )
178
- ) {
179
- enforceModulePolicy(
180
- compartmentDescriptor.name,
171
+ if (compartmentDescriptor.policy) {
172
+ enforcePackagePolicyByCanonicalName(
181
173
  someCompartmentDescriptor,
174
+ compartmentDescriptor,
182
175
  {
183
- errorHint: `Blocked in import hook. ${q(absoluteModuleSpecifier)} is part of the compartment map and resolves to ${location}`,
176
+ errorHint: `Blocked in importNow hook by package policy. ${q(absoluteModuleSpecifier)} is part of the compartment map and resolves to ${location}`,
184
177
  },
185
178
  );
179
+
186
180
  return {
187
181
  specifier: relativeSpecifier(moduleSpecifierLocation, location),
188
182
  compartment: compartments[location],
@@ -264,6 +258,66 @@ const nominateCandidates = (moduleSpecifier, searchSuffixes) => {
264
258
  return candidates;
265
259
  };
266
260
 
261
+ /**
262
+ * Executes the moduleSource hook for a {@link LocalModuleSource}.
263
+ *
264
+ * Preprocesses the fields for the hook.
265
+ *
266
+ * @param {ModuleSourceHook | undefined} moduleSourceHook Hook function
267
+ * @param {LocalModuleSource} moduleSource Original `LocalModuleSource` object
268
+ * @param {CanonicalName} canonicalName Canonical name of the compartment/package
269
+ * @param {LogOptions} options Options
270
+ * @returns {void}
271
+ */
272
+ const executeLocalModuleSourceHook = (
273
+ moduleSourceHook,
274
+ moduleSource,
275
+ canonicalName,
276
+ { log = noop } = {},
277
+ ) => {
278
+ if (!moduleSourceHook) {
279
+ return;
280
+ }
281
+
282
+ const {
283
+ sourceLocation: location,
284
+ parser: language,
285
+ bytes,
286
+ record,
287
+ sha512,
288
+ } = moduleSource;
289
+ /** @type {string[]|undefined} */
290
+ let imports;
291
+ /** @type {string[]|undefined} */
292
+ let exports;
293
+ /** @type {string[]|undefined} */
294
+ let reexports;
295
+
296
+ if ('imports' in record) {
297
+ ({ imports } = record);
298
+ }
299
+ if ('exports' in record) {
300
+ ({ exports } = record);
301
+ }
302
+ if ('reexports' in record) {
303
+ ({ reexports } = record);
304
+ }
305
+
306
+ moduleSourceHook({
307
+ moduleSource: {
308
+ location,
309
+ language,
310
+ bytes,
311
+ imports,
312
+ exports,
313
+ reexports,
314
+ sha512,
315
+ },
316
+ canonicalName,
317
+ log,
318
+ });
319
+ };
320
+
267
321
  /**
268
322
  * Returns a generator which applies {@link ChooseModuleDescriptorOperators} in
269
323
  * `operators` using the options in options to ultimately result in a
@@ -297,7 +351,9 @@ function* chooseModuleDescriptor(
297
351
  readPowers,
298
352
  archiveOnly,
299
353
  sourceMapHook,
354
+ moduleSourceHook,
300
355
  strictlyRequiredForCompartment,
356
+ log = noop,
301
357
  },
302
358
  { maybeRead, parse, shouldDeferError = () => false },
303
359
  ) {
@@ -394,6 +450,7 @@ function* chooseModuleDescriptor(
394
450
  retained: true,
395
451
  module: candidateSpecifier,
396
452
  compartment: packageLocation,
453
+ __createdBy: 'import-hook',
397
454
  };
398
455
  }
399
456
  /** @type {StaticModuleType} */
@@ -420,15 +477,27 @@ function* chooseModuleDescriptor(
420
477
  const packageRelativeLocation = moduleLocation.slice(
421
478
  packageLocation.length,
422
479
  );
423
- packageSources[candidateSpecifier] = {
480
+
481
+ /** @type {LocalModuleSource} */
482
+ const localModuleSource = {
424
483
  location: packageRelativeLocation,
425
484
  sourceLocation: moduleLocation,
426
485
  sourceDirname,
427
486
  parser,
428
487
  bytes: transformedBytes,
429
488
  record: concreteRecord,
430
- sha512,
489
+ ...(sha512 !== undefined && { sha512 }),
431
490
  };
491
+
492
+ packageSources[candidateSpecifier] = localModuleSource;
493
+
494
+ executeLocalModuleSourceHook(
495
+ moduleSourceHook,
496
+ localModuleSource,
497
+ compartmentDescriptor.label,
498
+ { log },
499
+ );
500
+
432
501
  if (!shouldDeferError(parser)) {
433
502
  for (const importSpecifier of getImportsFromRecord(record)) {
434
503
  strictlyRequiredForCompartment(packageLocation).add(
@@ -458,9 +527,7 @@ const makeDeferError = (
458
527
  packageSources,
459
528
  ) => {
460
529
  /**
461
- * @param {string} specifier
462
- * @param {Error} error - error to throw on execute
463
- * @returns {StaticModuleType}
530
+ * @type {DeferErrorFn}
464
531
  */
465
532
  const deferError = (specifier, error) => {
466
533
  // strictlyRequired is populated with imports declared by modules whose parser is not using heuristics to figure
@@ -482,9 +549,11 @@ const makeDeferError = (
482
549
  throw error;
483
550
  },
484
551
  });
485
- packageSources[specifier] = {
552
+ /** @type {ErrorModuleSource} */
553
+ const moduleSource = {
486
554
  deferredError: error.message,
487
555
  };
556
+ packageSources[specifier] = moduleSource;
488
557
 
489
558
  return record;
490
559
  };
@@ -493,7 +562,7 @@ const makeDeferError = (
493
562
 
494
563
  /**
495
564
  * @param {ReadFn|ReadPowers} readPowers
496
- * @param {string} baseLocation
565
+ * @param {FileUrlString} baseLocation
497
566
  * @param {MakeImportHookMakerOptions} options
498
567
  * @returns {ImportHookMaker}
499
568
  */
@@ -510,6 +579,8 @@ export const makeImportHookMaker = (
510
579
  entryCompartmentName,
511
580
  entryModuleSpecifier,
512
581
  importHook: exitModuleImportHook = undefined,
582
+ moduleSourceHook,
583
+ log = noop,
513
584
  },
514
585
  ) => {
515
586
  // Set of specifiers for modules (scoped to compartment) whose parser is not
@@ -581,12 +652,23 @@ export const makeImportHookMaker = (
581
652
  if (record) {
582
653
  // It'd be nice to check the policy before importing it, but we can only throw a policy error if the
583
654
  // hook returns something. Otherwise, we need to fall back to the 'cannot find' error below.
584
- enforceModulePolicy(moduleSpecifier, compartmentDescriptor, {
655
+ enforcePolicyByModule(moduleSpecifier, compartmentDescriptor, {
585
656
  exit: true,
586
657
  errorHint: `Blocked in loading. ${q(
587
658
  moduleSpecifier,
588
659
  )} was not in the compartment map and an attempt was made to load it as a builtin`,
589
660
  });
661
+
662
+ if (moduleSourceHook) {
663
+ moduleSourceHook({
664
+ moduleSource: {
665
+ exit: moduleSpecifier,
666
+ },
667
+ canonicalName: compartmentDescriptor.label,
668
+ log,
669
+ });
670
+ }
671
+
590
672
  if (archiveOnly) {
591
673
  // Return a place-holder.
592
674
  // Archived compartments are not executed.
@@ -630,7 +712,9 @@ export const makeImportHookMaker = (
630
712
  readPowers,
631
713
  archiveOnly,
632
714
  sourceMapHook,
715
+ moduleSourceHook,
633
716
  strictlyRequiredForCompartment,
717
+ log,
634
718
  },
635
719
  { maybeRead, parse, shouldDeferError },
636
720
  );
@@ -659,7 +743,7 @@ export const makeImportHookMaker = (
659
743
  * Synchronous import for dynamic requires.
660
744
  *
661
745
  * @param {ReadNowPowers} readPowers
662
- * @param {string} baseLocation
746
+ * @param {FileUrlString} baseLocation
663
747
  * @param {MakeImportNowHookMakerOptions} options
664
748
  * @returns {ImportNowHookMaker}
665
749
  */
@@ -674,13 +758,14 @@ export function makeImportNowHookMaker(
674
758
  archiveOnly = false,
675
759
  sourceMapHook = undefined,
676
760
  importNowHook: exitModuleImportNowHook = undefined,
761
+ moduleSourceHook,
762
+ log = noop,
677
763
  },
678
764
  ) {
679
765
  // Set of specifiers for modules (scoped to compartment) whose parser is not
680
766
  // using heuristics to determine imports.
681
767
  /** @type {Map<string, Set<string>>} compartment name ->* module specifier */
682
768
  const strictlyRequired = new Map();
683
-
684
769
  /**
685
770
  * @param {string} compartmentName
686
771
  */
@@ -704,10 +789,13 @@ export function makeImportNowHookMaker(
704
789
  compartments,
705
790
  shouldDeferError,
706
791
  }) => {
792
+ packageLocation = resolveLocation(packageLocation, baseLocation);
793
+ const packageSources = sources[packageLocation] || create(null);
794
+ sources[packageLocation] = packageSources;
707
795
  const deferError = makeDeferError(
708
796
  strictlyRequiredForCompartment,
709
797
  packageLocation,
710
- sources,
798
+ packageSources,
711
799
  );
712
800
 
713
801
  /**
@@ -729,9 +817,9 @@ export function makeImportNowHookMaker(
729
817
  if (exitRecord) {
730
818
  // It'd be nice to check the policy before importing it, but we can only throw a policy error if the
731
819
  // hook returns something. Otherwise, we need to fall back to the 'cannot find' error below.
732
- enforceModulePolicy(moduleSpecifier, compartmentDescriptor, {
820
+ enforcePolicyByModule(moduleSpecifier, compartmentDescriptor, {
733
821
  exit: true,
734
- errorHint: `Blocked in loading. ${q(
822
+ errorHint: `Blocked exit module in loading. ${q(
735
823
  moduleSpecifier,
736
824
  )} was not in the compartment map and an attempt was made to load it as a builtin`,
737
825
  });
@@ -759,37 +847,17 @@ export function makeImportNowHookMaker(
759
847
  };
760
848
  }
761
849
 
762
- const compartmentDescriptor = compartmentDescriptors[packageLocation] || {};
850
+ const compartmentDescriptor =
851
+ compartmentDescriptors[packageLocation] || create(null);
763
852
 
764
- packageLocation = resolveLocation(packageLocation, baseLocation);
765
- const packageSources = sources[packageLocation] || create(null);
766
- sources[packageLocation] = packageSources;
767
853
  const {
768
854
  modules:
769
- moduleDescriptors = /** @type {Record<string, ModuleDescriptor>} */ (
855
+ moduleDescriptors = /** @type {Record<string, CompartmentModuleConfiguration>} */ (
770
856
  create(null)
771
857
  ),
772
858
  } = compartmentDescriptor;
773
- compartmentDescriptor.modules = moduleDescriptors;
774
859
 
775
- let { policy } = compartmentDescriptor;
776
- policy = policy || create(null);
777
-
778
- // Associates modules with compartment descriptors based on policy
779
- // in cases where the association was not made when building the
780
- // compartment map but is indicated by the policy.
781
- if ('packages' in policy && typeof policy.packages === 'object') {
782
- for (const [packageName, packagePolicyItem] of entries(policy.packages)) {
783
- if (
784
- !(packageName in compartmentDescriptor.modules) &&
785
- packageName in compartmentDescriptor.scopes &&
786
- packagePolicyItem
787
- ) {
788
- compartmentDescriptor.modules[packageName] =
789
- compartmentDescriptor.scopes[packageName];
790
- }
791
- }
792
- }
860
+ compartmentDescriptor.modules = moduleDescriptors;
793
861
 
794
862
  const { maybeReadNow, isAbsolute } = readPowers;
795
863
 
@@ -850,7 +918,9 @@ export function makeImportNowHookMaker(
850
918
  readPowers,
851
919
  archiveOnly,
852
920
  sourceMapHook,
921
+ moduleSourceHook,
853
922
  strictlyRequiredForCompartment,
923
+ log,
854
924
  },
855
925
  {
856
926
  maybeRead: maybeReadNow,
@@ -1,22 +1,22 @@
1
1
  /**
2
2
  * @overload
3
3
  * @param {ReadNowPowers} readPowers
4
- * @param {CompartmentMapDescriptor} compartmentMap
4
+ * @param {PackageCompartmentMapDescriptor} compartmentMap
5
5
  * @param {SyncImportLocationOptions} [opts]
6
6
  * @returns {Promise<Application>}
7
7
  */
8
- export function loadFromMap(readPowers: ReadNowPowers, compartmentMap: CompartmentMapDescriptor, opts?: SyncImportLocationOptions | undefined): Promise<Application>;
8
+ export function loadFromMap(readPowers: ReadNowPowers, compartmentMap: PackageCompartmentMapDescriptor, opts?: SyncImportLocationOptions | undefined): Promise<Application>;
9
9
  /**
10
10
  * @overload
11
11
  * @param {ReadFn | ReadPowers} readPowers
12
- * @param {CompartmentMapDescriptor} compartmentMap
12
+ * @param {PackageCompartmentMapDescriptor} compartmentMap
13
13
  * @param {ImportLocationOptions} [opts]
14
14
  * @returns {Promise<Application>}
15
15
  */
16
- export function loadFromMap(readPowers: ReadFn | ReadPowers, compartmentMap: CompartmentMapDescriptor, opts?: ImportLocationOptions | undefined): Promise<Application>;
17
- export function importFromMap(readPowers: ReadFn | ReadPowers, compartmentMap: CompartmentMapDescriptor, options?: ImportLocationOptions): Promise<SomeObject>;
16
+ export function loadFromMap(readPowers: ReadFn | ReadPowers, compartmentMap: PackageCompartmentMapDescriptor, opts?: ImportLocationOptions | undefined): Promise<Application>;
17
+ export function importFromMap(readPowers: ReadFn | ReadPowers, compartmentMap: PackageCompartmentMapDescriptor, options?: ImportLocationOptions): Promise<SomeObject>;
18
18
  import type { ReadNowPowers } from './types.js';
19
- import type { CompartmentMapDescriptor } from './types.js';
19
+ import type { PackageCompartmentMapDescriptor } from './types.js';
20
20
  import type { SyncImportLocationOptions } from './types.js';
21
21
  import type { Application } from './types.js';
22
22
  import type { ReadFn } from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"import-lite.d.ts","sourceRoot":"","sources":["import-lite.js"],"names":[],"mappings":";;;;;;;AAyEG,wCACQ,aAAa,kBACb,wBAAwB,iDAEtB,OAAO,CAAC,WAAW,CAAC,CAChC;;;;;;;;AAGE,wCACQ,MAAM,GAAG,UAAU,kBACnB,wBAAwB,6CAEtB,OAAO,CAAC,WAAW,CAAC,CAChC;AA4JM,0CANI,MAAM,GAAG,UAAU,kBACnB,wBAAwB,YACxB,qBAAqB,GACnB,OAAO,CAAC,UAAU,CAAC,CAU/B;mCAxNS,YAAY;8CAAZ,YAAY;+CAAZ,YAAY;iCAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;2CAAZ,YAAY;gCAAZ,YAAY"}
1
+ {"version":3,"file":"import-lite.d.ts","sourceRoot":"","sources":["import-lite.js"],"names":[],"mappings":";;;;;;;AAyEG,wCACQ,aAAa,kBACb,+BAA+B,iDAE7B,OAAO,CAAC,WAAW,CAAC,CAChC;;;;;;;;AAGE,wCACQ,MAAM,GAAG,UAAU,kBACnB,+BAA+B,6CAE7B,OAAO,CAAC,WAAW,CAAC,CAChC;AA+JM,0CANI,MAAM,GAAG,UAAU,kBACnB,+BAA+B,YAC/B,qBAAqB,GACnB,OAAO,CAAC,UAAU,CAAC,CAU/B;mCA3NS,YAAY;qDAAZ,YAAY;+CAAZ,YAAY;iCAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;2CAAZ,YAAY;gCAAZ,YAAY"}
@@ -21,7 +21,6 @@
21
21
 
22
22
  /**
23
23
  * @import {
24
- * CompartmentMapDescriptor,
25
24
  * SyncImportLocationOptions,
26
25
  * ImportNowHookMaker,
27
26
  * ReadNowPowers,
@@ -31,6 +30,7 @@
31
30
  * ReadFn,
32
31
  * ReadPowers,
33
32
  * SomeObject,
33
+ * PackageCompartmentMapDescriptor,
34
34
  * } from './types.js'
35
35
  */
36
36
 
@@ -73,7 +73,7 @@ const isSyncOptions = value => {
73
73
  /**
74
74
  * @overload
75
75
  * @param {ReadNowPowers} readPowers
76
- * @param {CompartmentMapDescriptor} compartmentMap
76
+ * @param {PackageCompartmentMapDescriptor} compartmentMap
77
77
  * @param {SyncImportLocationOptions} [opts]
78
78
  * @returns {Promise<Application>}
79
79
  */
@@ -81,14 +81,14 @@ const isSyncOptions = value => {
81
81
  /**
82
82
  * @overload
83
83
  * @param {ReadFn | ReadPowers} readPowers
84
- * @param {CompartmentMapDescriptor} compartmentMap
84
+ * @param {PackageCompartmentMapDescriptor} compartmentMap
85
85
  * @param {ImportLocationOptions} [opts]
86
86
  * @returns {Promise<Application>}
87
87
  */
88
88
 
89
89
  /**
90
90
  * @param {ReadFn|ReadPowers|ReadNowPowers} readPowers
91
- * @param {CompartmentMapDescriptor} compartmentMap
91
+ * @param {PackageCompartmentMapDescriptor} compartmentMap
92
92
  * @param {ImportLocationOptions} [options]
93
93
  * @returns {Promise<Application>}
94
94
  */
@@ -98,6 +98,7 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => {
98
98
  searchSuffixes = undefined,
99
99
  parserForLanguage: parserForLanguageOption = {},
100
100
  Compartment: LoadCompartmentOption = Compartment,
101
+ moduleSourceHook,
101
102
  } = options;
102
103
 
103
104
  const parserForLanguage = freeze(
@@ -172,6 +173,7 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => {
172
173
  entryCompartmentName,
173
174
  entryModuleSpecifier,
174
175
  importHook: compartmentExitModuleImportHook,
176
+ moduleSourceHook,
175
177
  },
176
178
  );
177
179
 
@@ -195,6 +197,7 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => {
195
197
  compartmentDescriptors: compartmentMap.compartments,
196
198
  searchSuffixes,
197
199
  importNowHook: exitModuleImportNowHook,
200
+ moduleSourceHook,
198
201
  },
199
202
  );
200
203
  ({ compartment, pendingJobsPromise } = link(compartmentMap, {
@@ -235,7 +238,7 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => {
235
238
 
236
239
  /**
237
240
  * @param {ReadFn | ReadPowers} readPowers
238
- * @param {CompartmentMapDescriptor} compartmentMap
241
+ * @param {PackageCompartmentMapDescriptor} compartmentMap
239
242
  * @param {ImportLocationOptions} [options]
240
243
  * @returns {Promise<SomeObject>} the object of the imported modules exported
241
244
  * names.