@endo/compartment-mapper 1.6.3 → 2.0.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 (81) hide show
  1. package/package.json +12 -16
  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 +78 -27
  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 +217 -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 +737 -254
  19. package/src/digest.d.ts +22 -2
  20. package/src/digest.d.ts.map +1 -1
  21. package/src/digest.js +179 -56
  22. package/src/generic-graph.d.ts.map +1 -1
  23. package/src/generic-graph.js +8 -3
  24. package/src/guards.d.ts +18 -0
  25. package/src/guards.d.ts.map +1 -0
  26. package/src/guards.js +109 -0
  27. package/src/hooks.md +124 -0
  28. package/src/import-archive-lite.d.ts.map +1 -1
  29. package/src/import-archive-lite.js +15 -11
  30. package/src/import-archive.d.ts +5 -19
  31. package/src/import-archive.d.ts.map +1 -1
  32. package/src/import-archive.js +7 -27
  33. package/src/import-hook.d.ts +4 -3
  34. package/src/import-hook.d.ts.map +1 -1
  35. package/src/import-hook.js +138 -69
  36. package/src/import-lite.d.ts +6 -6
  37. package/src/import-lite.d.ts.map +1 -1
  38. package/src/import-lite.js +8 -5
  39. package/src/import.d.ts +3 -3
  40. package/src/import.d.ts.map +1 -1
  41. package/src/import.js +16 -6
  42. package/src/infer-exports.d.ts.map +1 -1
  43. package/src/infer-exports.js +16 -6
  44. package/src/link.d.ts +4 -3
  45. package/src/link.d.ts.map +1 -1
  46. package/src/link.js +70 -58
  47. package/src/node-modules.d.ts +4 -3
  48. package/src/node-modules.d.ts.map +1 -1
  49. package/src/node-modules.js +482 -114
  50. package/src/parse-cjs-shared-export-wrapper.d.ts.map +1 -1
  51. package/src/parse-cjs-shared-export-wrapper.js +3 -1
  52. package/src/policy-format.d.ts +22 -5
  53. package/src/policy-format.d.ts.map +1 -1
  54. package/src/policy-format.js +342 -108
  55. package/src/policy.d.ts +13 -28
  56. package/src/policy.d.ts.map +1 -1
  57. package/src/policy.js +161 -106
  58. package/src/types/canonical-name.d.ts +97 -0
  59. package/src/types/canonical-name.d.ts.map +1 -0
  60. package/src/types/canonical-name.ts +151 -0
  61. package/src/types/compartment-map-schema.d.ts +114 -35
  62. package/src/types/compartment-map-schema.d.ts.map +1 -1
  63. package/src/types/compartment-map-schema.ts +202 -37
  64. package/src/types/external.d.ts +168 -28
  65. package/src/types/external.d.ts.map +1 -1
  66. package/src/types/external.ts +215 -26
  67. package/src/types/internal.d.ts +23 -42
  68. package/src/types/internal.d.ts.map +1 -1
  69. package/src/types/internal.ts +51 -50
  70. package/src/types/node-modules.d.ts +71 -10
  71. package/src/types/node-modules.d.ts.map +1 -1
  72. package/src/types/node-modules.ts +107 -9
  73. package/src/types/policy-schema.d.ts +26 -11
  74. package/src/types/policy-schema.d.ts.map +1 -1
  75. package/src/types/policy-schema.ts +29 -16
  76. package/src/types/policy.d.ts +6 -2
  77. package/src/types/policy.d.ts.map +1 -1
  78. package/src/types/policy.ts +7 -2
  79. package/src/types/typescript.d.ts +28 -0
  80. package/src/types/typescript.d.ts.map +1 -1
  81. package/src/types/typescript.ts +37 -1
package/src/link.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- export function link({ entry, compartments: compartmentDescriptors }: CompartmentMapDescriptor, options: LinkOptions): LinkResult;
2
- export function assemble(compartmentMap: CompartmentMapDescriptor, options: LinkOptions): Compartment;
3
- import type { CompartmentMapDescriptor } from './types.js';
1
+ export function link({ entry, compartments: compartmentDescriptors }: PackageCompartmentMapDescriptor | FileCompartmentMapDescriptor, options: LinkOptions): LinkResult;
2
+ export function assemble(compartmentMap: PackageCompartmentMapDescriptor, options: LinkOptions): Compartment;
3
+ import type { PackageCompartmentMapDescriptor } from './types.js';
4
+ import type { FileCompartmentMapDescriptor } from './types.js';
4
5
  import type { LinkOptions } from './types.js';
5
6
  import type { LinkResult } from './types.js';
6
7
  //# sourceMappingURL=link.d.ts.map
package/src/link.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["link.js"],"names":[],"mappings":"AAwPO,sEAJI,wBAAwB,WACxB,WAAW,GACT,UAAU,CA2KtB;AAOM,yCAJI,wBAAwB,WACxB,WAAW,eAIqB;8CA/YjC,YAAY;iCAAZ,YAAY;gCAAZ,YAAY"}
1
+ {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["link.js"],"names":[],"mappings":"AA+PO,sEAJI,+BAA+B,GAAC,4BAA4B,WAC5D,WAAW,GACT,UAAU,CAgLtB;AAOM,yCAJI,+BAA+B,WAC/B,WAAW,eAIqB;qDArZjC,YAAY;kDAAZ,YAAY;iCAAZ,YAAY;gCAAZ,YAAY"}
package/src/link.js CHANGED
@@ -13,28 +13,38 @@
13
13
  /**
14
14
  * @import {ModuleMapHook} from 'ses'
15
15
  * @import {
16
- * CompartmentDescriptor,
17
- * CompartmentMapDescriptor,
18
16
  * ImportNowHookMaker,
19
17
  * LinkOptions,
20
18
  * LinkResult,
21
- * ModuleDescriptor,
22
19
  * ParseFn,
23
20
  * AsyncParseFn,
24
21
  * ParserForLanguage,
25
22
  * ParserImplementation,
26
23
  * ShouldDeferError,
24
+ * ScopeDescriptor,
25
+ * CompartmentModuleConfiguration,
26
+ * PackageCompartmentMapDescriptor,
27
+ * FileUrlString,
28
+ * PackageCompartmentDescriptor,
29
+ * FileCompartmentMapDescriptor,
30
+ * FileCompartmentDescriptor,
31
+ * FileModuleConfiguration,
32
+ * MakeModuleMapHookOptions,
27
33
  * } from './types.js'
28
34
  */
29
35
 
30
36
  import { makeMapParsers } from './map-parser.js';
31
37
  import { resolve as resolveFallback } from './node-module-specifier.js';
32
38
  import {
33
- ATTENUATORS_COMPARTMENT,
34
39
  attenuateGlobals,
35
- enforceModulePolicy,
40
+ enforcePolicyByModule,
36
41
  makeDeferredAttenuatorsProvider,
37
42
  } from './policy.js';
43
+ import { ATTENUATORS_COMPARTMENT } from './policy-format.js';
44
+ import {
45
+ isCompartmentModuleConfiguration,
46
+ isExitModuleConfiguration,
47
+ } from './guards.js';
38
48
 
39
49
  const { assign, create, entries, freeze } = Object;
40
50
  const { hasOwnProperty } = Object.prototype;
@@ -47,17 +57,17 @@ const { allSettled } = Promise;
47
57
  */
48
58
  const promiseAllSettled = allSettled.bind(Promise);
49
59
 
50
- const defaultCompartment = Compartment;
51
-
52
- // q, as in quote, for strings in error messages.
53
- const q = JSON.stringify;
60
+ const DefaultCompartment = Compartment;
54
61
 
55
62
  /**
63
+ * TODO: can we just use `Object.hasOwn` instead?
56
64
  * @param {Record<string, unknown>} object
57
65
  * @param {string} key
58
66
  * @returns {boolean}
59
67
  */
60
68
  const has = (object, key) => apply(hasOwnProperty, object, [key]);
69
+ // q, as in quote, for strings in error messages.
70
+ const { quote: q } = assert;
61
71
 
62
72
  /**
63
73
  * For a full, absolute module specifier like "dependency",
@@ -89,11 +99,11 @@ const trimModuleSpecifierPrefix = (moduleSpecifier, prefix) => {
89
99
  * Any module specifier with an absolute prefix should be captured by
90
100
  * the `moduleMap` or `moduleMapHook`.
91
101
  *
92
- * @param {CompartmentDescriptor} compartmentDescriptor
102
+ * @param {FileCompartmentDescriptor|PackageCompartmentDescriptor} compartmentDescriptor
93
103
  * @param {Record<string, Compartment>} compartments
94
104
  * @param {string} compartmentName
95
- * @param {Record<string, ModuleDescriptor>} moduleDescriptors
96
- * @param {Record<string, ModuleDescriptor>} scopeDescriptors
105
+ * @param {Record<string, FileModuleConfiguration|CompartmentModuleConfiguration>} moduleDescriptors
106
+ * @param {Record<string, ScopeDescriptor<FileUrlString>>} scopeDescriptors
97
107
  * @returns {ModuleMapHook | undefined}
98
108
  */
99
109
  const makeModuleMapHook = (
@@ -104,8 +114,7 @@ const makeModuleMapHook = (
104
114
  scopeDescriptors,
105
115
  ) => {
106
116
  /**
107
- * @param {string} moduleSpecifier
108
- * @returns {string | object | undefined}
117
+ * @type {ModuleMapHook}
109
118
  */
110
119
  const moduleMapHook = moduleSpecifier => {
111
120
  compartmentDescriptor.retained = true;
@@ -114,40 +123,42 @@ const makeModuleMapHook = (
114
123
  if (moduleDescriptor !== undefined) {
115
124
  moduleDescriptor.retained = true;
116
125
 
126
+ if (isExitModuleConfiguration(moduleDescriptor)) {
127
+ return undefined;
128
+ }
117
129
  // "foreignCompartmentName" refers to the compartment which
118
130
  // may differ from the current compartment
119
- const {
120
- compartment: foreignCompartmentName = compartmentName,
121
- module: foreignModuleSpecifier,
122
- exit,
123
- } = moduleDescriptor;
124
- if (exit !== undefined) {
125
- return undefined; // fall through to import hook
126
- }
127
- if (foreignModuleSpecifier !== undefined) {
128
- // archive goes through foreignModuleSpecifier for local modules too
129
- if (!moduleSpecifier.startsWith('./')) {
130
- // This code path seems to only be reached on subsequent imports of the same specifier in the same compartment.
131
- // The check should be redundant and is only left here out of abundance of caution.
132
- enforceModulePolicy(moduleSpecifier, compartmentDescriptor, {
133
- exit: false,
134
- errorHint:
135
- 'This check should not be reachable. If you see this error, please file an issue.',
136
- });
137
- }
138
-
139
- const foreignCompartment = compartments[foreignCompartmentName];
140
- if (foreignCompartment === undefined) {
141
- throw Error(
142
- `Cannot import from missing compartment ${q(
143
- foreignCompartmentName,
144
- )}}`,
145
- );
131
+ if (isCompartmentModuleConfiguration(moduleDescriptor)) {
132
+ const {
133
+ compartment: foreignCompartmentName = compartmentName,
134
+ module: foreignModuleSpecifier,
135
+ } = moduleDescriptor;
136
+ if (foreignModuleSpecifier !== undefined) {
137
+ // archive goes through foreignModuleSpecifier for local modules too
138
+ if (!moduleSpecifier.startsWith('./')) {
139
+ // This code path seems to only be reached on subsequent imports of the same specifier in the same compartment.
140
+ // The check should be redundant and is only left here out of abundance of caution.
141
+ enforcePolicyByModule(moduleSpecifier, compartmentDescriptor, {
142
+ exit: false,
143
+ errorHint:
144
+ 'This check should not be reachable. If you see this error, please file an issue.',
145
+ });
146
+ }
147
+
148
+ const foreignCompartment = compartments[foreignCompartmentName];
149
+ if (foreignCompartment === undefined) {
150
+ throw Error(
151
+ `Cannot import from missing compartment ${q(
152
+ foreignCompartmentName,
153
+ )}}`,
154
+ );
155
+ }
156
+ // actual module descriptor
157
+ return {
158
+ compartment: foreignCompartment,
159
+ namespace: foreignModuleSpecifier,
160
+ };
146
161
  }
147
- return {
148
- compartment: foreignCompartment,
149
- namespace: foreignModuleSpecifier,
150
- };
151
162
  }
152
163
  }
153
164
 
@@ -178,7 +189,7 @@ const makeModuleMapHook = (
178
189
  );
179
190
  }
180
191
 
181
- enforceModulePolicy(scopePrefix, compartmentDescriptor, {
192
+ enforcePolicyByModule(scopePrefix, compartmentDescriptor, {
182
193
  exit: false,
183
194
  errorHint: `Blocked in linking. ${q(
184
195
  moduleSpecifier,
@@ -198,7 +209,9 @@ const makeModuleMapHook = (
198
209
  retained: true,
199
210
  compartment: foreignCompartmentName,
200
211
  module: foreignModuleSpecifier,
212
+ __createdBy: 'link',
201
213
  };
214
+ // actual module descriptor
202
215
  return {
203
216
  compartment: foreignCompartment,
204
217
  namespace: foreignModuleSpecifier,
@@ -236,16 +249,10 @@ const impossibleImportNowHookMaker = () => {
236
249
  * - Passes the given globals and external modules into the root compartment
237
250
  * only.
238
251
  *
239
- * @param {CompartmentMapDescriptor} compartmentMap
252
+ * @param {PackageCompartmentMapDescriptor|FileCompartmentMapDescriptor} compartmentMap
240
253
  * @param {LinkOptions} options
241
254
  * @returns {LinkResult} the root compartment of the compartment DAG
242
255
  */
243
-
244
- /**
245
- * @param {CompartmentMapDescriptor} compartmentMap
246
- * @param {LinkOptions} options
247
- * @returns {LinkResult}
248
- */
249
256
  export const link = (
250
257
  { entry, compartments: compartmentDescriptors },
251
258
  options,
@@ -262,7 +269,7 @@ export const link = (
262
269
  __shimTransforms__ = [],
263
270
  __native__ = false,
264
271
  archiveOnly = false,
265
- Compartment = defaultCompartment,
272
+ Compartment = DefaultCompartment,
266
273
  } = options;
267
274
 
268
275
  const { compartment: entryCompartmentName } = entry;
@@ -291,9 +298,14 @@ export const link = (
291
298
  syncModuleTransforms,
292
299
  });
293
300
 
294
- for (const [compartmentName, compartmentDescriptor] of entries(
295
- compartmentDescriptors,
296
- )) {
301
+ const compartmentDescriptorEntries =
302
+ /** @type {[string, PackageCompartmentDescriptor|FileCompartmentDescriptor][]} */ (
303
+ entries(compartmentDescriptors)
304
+ );
305
+ for (const [
306
+ compartmentName,
307
+ compartmentDescriptor,
308
+ ] of compartmentDescriptorEntries) {
297
309
  const {
298
310
  location,
299
311
  name,
@@ -418,7 +430,7 @@ export const link = (
418
430
  };
419
431
 
420
432
  /**
421
- * @param {CompartmentMapDescriptor} compartmentMap
433
+ * @param {PackageCompartmentMapDescriptor} compartmentMap
422
434
  * @param {LinkOptions} options
423
435
  * @deprecated Use {@link link}.
424
436
  */
@@ -1,12 +1,13 @@
1
1
  export function basename(location: string): string;
2
- export function compartmentMapForNodeModules(readPowers: ReadFn | ReadPowers<FileUrlString> | MaybeReadPowers<FileUrlString>, packageLocation: FileUrlString, conditionsOption: Set<string>, packageDescriptor: PackageDescriptor, moduleSpecifier: string, options?: CompartmentMapForNodeModulesOptions): Promise<CompartmentMapDescriptor>;
3
- export function mapNodeModules(readPowers: ReadFn | ReadPowers<FileUrlString> | MaybeReadPowers<FileUrlString>, moduleLocation: string, { tags, conditions, log, ...otherOptions }?: MapNodeModulesOptions): Promise<CompartmentMapDescriptor>;
2
+ export function compartmentMapForNodeModules_(readPowers: ReadFn | ReadPowers<FileUrlString> | MaybeReadPowers<FileUrlString>, entryPackageLocation: FileUrlString, conditionsOption: Set<string>, packageDescriptor: PackageDescriptor, entryModuleSpecifier: string, options?: CompartmentMapForNodeModulesOptions): Promise<PackageCompartmentMapDescriptor>;
3
+ export function mapNodeModules(readPowers: ReadFn | ReadPowers<FileUrlString> | MaybeReadPowers<FileUrlString>, moduleLocation: string, { tags, conditions, log, unknownCanonicalNameHook, packageDataHook, packageDependenciesHook, policy, ...otherOptions }?: MapNodeModulesOptions): Promise<PackageCompartmentMapDescriptor>;
4
+ export function compartmentMapForNodeModules(readPowers: ReadFn | ReadPowers<FileUrlString> | MaybeReadPowers<FileUrlString>, entryPackageLocation: FileUrlString, conditionsOption: Set<string>, packageDescriptor: PackageDescriptor, entryModuleSpecifier: string, options?: CompartmentMapForNodeModulesOptions): Promise<PackageCompartmentMapDescriptor>;
4
5
  import type { ReadFn } from './types.js';
5
6
  import type { FileUrlString } from './types.js';
6
7
  import type { ReadPowers } from './types.js';
7
8
  import type { MaybeReadPowers } from './types.js';
8
9
  import type { PackageDescriptor } from './types.js';
9
10
  import type { CompartmentMapForNodeModulesOptions } from './types.js';
10
- import type { CompartmentMapDescriptor } from './types.js';
11
+ import type { PackageCompartmentMapDescriptor } from './types.js';
11
12
  import type { MapNodeModulesOptions } from './types.js';
12
13
  //# sourceMappingURL=node-modules.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"node-modules.d.ts","sourceRoot":"","sources":["node-modules.js"],"names":[],"mappings":"AAyHO,mCAHI,MAAM,GACJ,MAAM,CAYlB;AAw1BM,yDATI,MAAM,GAAG,WAAW,aAAa,CAAC,GAAG,gBAAgB,aAAa,CAAC,mBACnE,aAAa,oBACb,GAAG,CAAC,MAAM,CAAC,qBACX,iBAAiB,mBACjB,MAAM,YACN,mCAAmC,GACjC,OAAO,CAAC,wBAAwB,CAAC,CAgG7C;AAaM,2CALI,MAAM,GAAG,WAAW,aAAa,CAAC,GAAG,gBAAgB,aAAa,CAAC,kBACnE,MAAM,+CACN,qBAAqB,GACnB,OAAO,CAAC,wBAAwB,CAAC,CA6B7C;4BAnjCS,YAAY;mCAAZ,YAAY;gCAAZ,YAAY;qCAAZ,YAAY;uCAAZ,YAAY;yDAAZ,YAAY;8CAAZ,YAAY;2CAAZ,YAAY"}
1
+ {"version":3,"file":"node-modules.d.ts","sourceRoot":"","sources":["node-modules.js"],"names":[],"mappings":"AA4LO,mCAHI,MAAM,GACJ,MAAM,CAYlB;AAqlCM,0DARI,MAAM,GAAG,WAAW,aAAa,CAAC,GAAG,gBAAgB,aAAa,CAAC,wBACnE,aAAa,oBACb,GAAG,CAAC,MAAM,CAAC,qBACX,iBAAiB,wBACjB,MAAM,YACN,mCAAmC,GACjC,OAAO,CAAC,+BAA+B,CAAC,CA0HpD;AAaM,2CALI,MAAM,GAAG,WAAW,aAAa,CAAC,GAAG,gBAAgB,aAAa,CAAC,kBACnE,MAAM,2HACN,qBAAqB,GACnB,OAAO,CAAC,+BAA+B,CAAC,CA6CpD;AAhLM,yDARI,MAAM,GAAG,WAAW,aAAa,CAAC,GAAG,gBAAgB,aAAa,CAAC,wBACnE,aAAa,oBACb,GAAG,CAAC,MAAM,CAAC,qBACX,iBAAiB,wBACjB,MAAM,YACN,mCAAmC,GACjC,OAAO,CAAC,+BAA+B,CAAC,CA0HpD;4BA51CS,YAAY;mCAAZ,YAAY;gCAAZ,YAAY;qCAAZ,YAAY;uCAAZ,YAAY;yDAAZ,YAAY;qDAAZ,YAAY;2CAAZ,YAAY"}