@endo/compartment-mapper 1.5.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +278 -111
  3. package/SECURITY.md +2 -2
  4. package/bundle.d.ts +1 -1
  5. package/bundle.js +4 -1
  6. package/functor-lite.d.ts +3 -0
  7. package/functor-lite.d.ts.map +1 -0
  8. package/functor-lite.js +4 -0
  9. package/functor.d.ts +3 -0
  10. package/functor.d.ts.map +1 -0
  11. package/functor.js +4 -0
  12. package/index.d.ts +1 -1
  13. package/index.js +4 -1
  14. package/package.json +15 -11
  15. package/script-lite.d.ts +3 -0
  16. package/script-lite.d.ts.map +1 -0
  17. package/script-lite.js +4 -0
  18. package/script.d.ts +3 -0
  19. package/script.d.ts.map +1 -0
  20. package/script.js +4 -0
  21. package/src/archive-lite.d.ts +5 -5
  22. package/src/archive-lite.d.ts.map +1 -1
  23. package/src/archive-lite.js +1 -1
  24. package/src/archive.d.ts +5 -5
  25. package/src/archive.d.ts.map +1 -1
  26. package/src/archive.js +3 -1
  27. package/src/bundle-cjs.d.ts +12 -2
  28. package/src/bundle-cjs.d.ts.map +1 -1
  29. package/src/bundle-cjs.js +57 -28
  30. package/src/bundle-json.d.ts.map +1 -1
  31. package/src/bundle-json.js +2 -3
  32. package/src/bundle-lite.d.ts +91 -0
  33. package/src/bundle-lite.d.ts.map +1 -0
  34. package/src/bundle-lite.js +667 -0
  35. package/src/bundle-mjs.d.ts +13 -3
  36. package/src/bundle-mjs.d.ts.map +1 -1
  37. package/src/bundle-mjs.js +36 -19
  38. package/src/bundle.d.ts +48 -10
  39. package/src/bundle.d.ts.map +1 -1
  40. package/src/bundle.js +392 -126
  41. package/src/capture-lite.d.ts +1 -1
  42. package/src/capture-lite.d.ts.map +1 -1
  43. package/src/capture-lite.js +4 -2
  44. package/src/compartment-map.d.ts +1 -1
  45. package/src/compartment-map.d.ts.map +1 -1
  46. package/src/import-archive-lite.d.ts +2 -2
  47. package/src/import-archive-lite.d.ts.map +1 -1
  48. package/src/import-archive-lite.js +3 -1
  49. package/src/import-archive.d.ts +3 -3
  50. package/src/import-archive.d.ts.map +1 -1
  51. package/src/import-archive.js +3 -1
  52. package/src/import-hook.d.ts +3 -16
  53. package/src/import-hook.d.ts.map +1 -1
  54. package/src/import-hook.js +214 -116
  55. package/src/import-lite.d.ts +1 -1
  56. package/src/import-lite.d.ts.map +1 -1
  57. package/src/import-lite.js +7 -3
  58. package/src/import.d.ts.map +1 -1
  59. package/src/import.js +3 -1
  60. package/src/infer-exports.d.ts +5 -7
  61. package/src/infer-exports.d.ts.map +1 -1
  62. package/src/infer-exports.js +23 -8
  63. package/src/link.d.ts.map +1 -1
  64. package/src/link.js +4 -1
  65. package/src/map-parser.d.ts.map +1 -1
  66. package/src/map-parser.js +51 -5
  67. package/src/node-module-specifier.d.ts.map +1 -1
  68. package/src/node-module-specifier.js +3 -1
  69. package/src/node-modules.d.ts +4 -47
  70. package/src/node-modules.d.ts.map +1 -1
  71. package/src/node-modules.js +267 -148
  72. package/src/node-powers.d.ts +1 -1
  73. package/src/node-powers.d.ts.map +1 -1
  74. package/src/node-powers.js +3 -1
  75. package/src/parse-archive-cjs.d.ts +5 -1
  76. package/src/parse-archive-cjs.d.ts.map +1 -1
  77. package/src/parse-archive-cjs.js +11 -4
  78. package/src/parse-archive-mjs.d.ts +5 -1
  79. package/src/parse-archive-mjs.d.ts.map +1 -1
  80. package/src/parse-archive-mjs.js +3 -1
  81. package/src/parse-bytes.d.ts +5 -1
  82. package/src/parse-bytes.d.ts.map +1 -1
  83. package/src/parse-bytes.js +3 -1
  84. package/src/parse-cjs-shared-export-wrapper.d.ts.map +1 -1
  85. package/src/parse-cjs-shared-export-wrapper.js +5 -11
  86. package/src/parse-cjs.d.ts +5 -1
  87. package/src/parse-cjs.d.ts.map +1 -1
  88. package/src/parse-cjs.js +4 -2
  89. package/src/parse-json.d.ts +5 -2
  90. package/src/parse-json.d.ts.map +1 -1
  91. package/src/parse-mjs.d.ts +5 -1
  92. package/src/parse-mjs.d.ts.map +1 -1
  93. package/src/parse-mjs.js +2 -2
  94. package/src/parse-pre-cjs.d.ts +5 -1
  95. package/src/parse-pre-cjs.d.ts.map +1 -1
  96. package/src/parse-pre-cjs.js +3 -1
  97. package/src/parse-pre-mjs.d.ts +5 -1
  98. package/src/parse-pre-mjs.d.ts.map +1 -1
  99. package/src/parse-pre-mjs.js +3 -1
  100. package/src/parse-text.d.ts +5 -1
  101. package/src/parse-text.d.ts.map +1 -1
  102. package/src/parse-text.js +3 -1
  103. package/src/policy-format.d.ts +2 -1
  104. package/src/policy-format.d.ts.map +1 -1
  105. package/src/policy-format.js +5 -2
  106. package/src/policy.d.ts +2 -2
  107. package/src/policy.d.ts.map +1 -1
  108. package/src/policy.js +10 -11
  109. package/src/powers.d.ts +1 -1
  110. package/src/powers.d.ts.map +1 -1
  111. package/src/powers.js +3 -1
  112. package/src/search.d.ts +7 -12
  113. package/src/search.d.ts.map +1 -1
  114. package/src/search.js +32 -13
  115. package/src/types/compartment-map-schema.d.ts +8 -1
  116. package/src/types/compartment-map-schema.d.ts.map +1 -1
  117. package/src/types/compartment-map-schema.ts +8 -1
  118. package/src/types/external.d.ts +127 -17
  119. package/src/types/external.d.ts.map +1 -1
  120. package/src/types/external.ts +142 -17
  121. package/src/types/internal.d.ts +116 -29
  122. package/src/types/internal.d.ts.map +1 -1
  123. package/src/types/internal.ts +144 -31
  124. package/src/types/node-modules.d.ts +79 -0
  125. package/src/types/node-modules.d.ts.map +1 -0
  126. package/src/types/node-modules.ts +89 -0
  127. package/src/types/node-powers.d.ts +7 -5
  128. package/src/types/node-powers.d.ts.map +1 -1
  129. package/src/types/node-powers.ts +7 -5
  130. package/src/types/policy-schema.d.ts +3 -1
  131. package/src/types/policy-schema.d.ts.map +1 -1
  132. package/src/types/policy-schema.ts +3 -1
  133. package/src/types/policy.d.ts +3 -1
  134. package/src/types/policy.d.ts.map +1 -1
  135. package/src/types/policy.ts +3 -1
  136. package/src/types/powers.d.ts +5 -3
  137. package/src/types/powers.d.ts.map +1 -1
  138. package/src/types/powers.ts +5 -3
  139. package/src/types/typescript.d.ts +3 -1
  140. package/src/types/typescript.d.ts.map +1 -1
  141. package/src/types/typescript.ts +3 -1
  142. package/src/url.d.ts.map +1 -1
  143. package/src/url.js +3 -1
package/src/bundle-mjs.js CHANGED
@@ -1,7 +1,9 @@
1
1
  /* Provides ESM support for `bundle.js`. */
2
2
 
3
3
  /** @import {PrecompiledModuleSource} from 'ses' */
4
- /** @import {BundlerSupport} from './bundle.js' */
4
+ /** @import {BundlerSupport} from './bundle-lite.js' */
5
+
6
+ import { join } from './node-module-specifier.js';
5
7
 
6
8
  /** quotes strings */
7
9
  const q = JSON.stringify;
@@ -41,7 +43,7 @@ function observeImports(map, importName, importIndex) {
41
43
  for (const [name, observers] of map.get(importName)) {
42
44
  const cell = cells[importIndex][name];
43
45
  if (cell === undefined) {
44
- throw new ReferenceError(\`Cannot import name \${name}\`);
46
+ throw new ReferenceError(\`Cannot import name \${name} (has \${Object.getOwnPropertyNames(cells[importIndex]).join(', ')})\`);
45
47
  }
46
48
  for (const observer of observers) {
47
49
  cell.observe(observer);
@@ -53,21 +55,30 @@ function observeImports(map, importName, importIndex) {
53
55
  /** @type {BundlerSupport<PrecompiledModuleSource>} */
54
56
  export default {
55
57
  runtime,
56
- getBundlerKit({
57
- index,
58
- indexedImports,
59
- record: {
60
- __syncModuleProgram__,
61
- __fixedExportMap__ = {},
62
- __liveExportMap__ = {},
63
- __reexportMap__ = {},
64
- reexports,
58
+ getBundlerKit(
59
+ {
60
+ index,
61
+ indexedImports,
62
+ moduleSpecifier,
63
+ sourceDirname,
64
+ record: {
65
+ __syncModuleProgram__,
66
+ __fixedExportMap__ = {},
67
+ __liveExportMap__ = {},
68
+ __reexportMap__ = {},
69
+ reexports,
70
+ },
65
71
  },
66
- }) {
72
+ { useEvaluate = false },
73
+ ) {
74
+ let functor = __syncModuleProgram__;
75
+ if (useEvaluate) {
76
+ const sourceUrl = join(sourceDirname, moduleSpecifier);
77
+ functor = JSON.stringify([functor, sourceUrl]);
78
+ }
67
79
  return {
68
80
  getFunctor: () => `\
69
- // === functors[${index}] ===
70
- ${__syncModuleProgram__},
81
+ ${functor},
71
82
  `,
72
83
  getCells: () => `\
73
84
  {
@@ -79,14 +90,14 @@ ${exportsCellRecord(__fixedExportMap__)}${exportsCellRecord(
79
90
  getReexportsWiring: () => {
80
91
  const mappings = reexports.map(
81
92
  importSpecifier => `\
82
- Object.defineProperties(cells[${index}], Object.getOwnPropertyDescriptors(cells[${indexedImports[importSpecifier]}]));
93
+ defineProperties(cells[${index}], getOwnPropertyDescriptors(cells[${indexedImports[importSpecifier]}]));
83
94
  `,
84
95
  );
85
96
  // Create references for export name as newname
86
97
  const namedReexportsToProcess = Object.entries(__reexportMap__);
87
98
  if (namedReexportsToProcess.length > 0) {
88
99
  mappings.push(`
89
- Object.defineProperties(cells[${index}], {${namedReexportsToProcess.map(
100
+ defineProperties(cells[${index}], {${namedReexportsToProcess.map(
90
101
  ([specifier, renames]) => {
91
102
  return renames.map(
92
103
  ([localName, exportedName]) =>
@@ -100,8 +111,13 @@ ${exportsCellRecord(__fixedExportMap__)}${exportsCellRecord(
100
111
  }
101
112
  return mappings.join('');
102
113
  },
103
- getFunctorCall: () => `\
104
- functors[${index}]({
114
+ getFunctorCall: () => {
115
+ let functorExpression = `functors[${index}]`;
116
+ if (useEvaluate) {
117
+ functorExpression = `evaluateSource(...${functorExpression})`;
118
+ }
119
+ return `\
120
+ ${functorExpression}({
105
121
  imports(entries) {
106
122
  const map = new Map(entries);
107
123
  ${''.concat(
@@ -120,7 +136,8 @@ ${importsCellSetter(__fixedExportMap__, index)}\
120
136
  },
121
137
  importMeta: {},
122
138
  });
123
- `,
139
+ `;
140
+ },
124
141
  };
125
142
  },
126
143
  };
package/src/bundle.d.ts CHANGED
@@ -1,5 +1,32 @@
1
- export function makeBundle(readPowers: ReadFn | ReadPowers | MaybeReadPowers, moduleLocation: string, options?: ArchiveOptions | undefined): Promise<string>;
2
- export function writeBundle(write: WriteFn, read: ReadFn, bundleLocation: string, moduleLocation: string, options?: ArchiveOptions | undefined): Promise<void>;
1
+ export function makeFunctorFromMap(readPowers: ReadFn | ReadPowers | MaybeReadPowers, compartmentMap: CompartmentMapDescriptor, options?: BundleOptions): Promise<string>;
2
+ export function makeScriptFromMap(readPowers: ReadFn | ReadPowers | MaybeReadPowers, compartmentMap: CompartmentMapDescriptor, options?: BundleOptions): Promise<string>;
3
+ export function makeFunctor(readPowers: ReadFn | ReadPowers | MaybeReadPowers, moduleLocation: string, options?: BundleOptions): Promise<string>;
4
+ export function makeScript(readPowers: ReadFn | ReadPowers | MaybeReadPowers, moduleLocation: string, options?: BundleOptions): Promise<string>;
5
+ export function writeScript(write: WriteFn, read: ReadFn, bundleLocation: string, moduleLocation: string, options?: BundleOptions): Promise<void>;
6
+ /**
7
+ * The bundler kit defines a language-specific behavior for injecting a module
8
+ * into a bundle.
9
+ * Each module must allocate cells for its imports and exports, link those cells
10
+ * to the cells of dependencies, and provide both the linker and evaluation behavior
11
+ * for the module.
12
+ * The linker behavior gets injected in a lexical scope with the linker runtime
13
+ * and has access to the cells of all modules, whereas the evaluation behavior
14
+ * gets injected in the generated script's top level lexical scope, so has
15
+ * no accidental visibility into the linkage runtime.
16
+ *
17
+ * For example, JSON modules produce a single "default" cell ("getCells"):
18
+ *
19
+ * { default: cell('default') },
20
+ *
21
+ * Then, the JSON gets injected verbatim for the evaluation behavior ("getFunctor").
22
+ * The linker simply sets the cell to the value.
23
+ *
24
+ * functors[0]['default'].set(modules[0]);
25
+ *
26
+ * For an ECMAScript or CommonJS module, the evaluation behavior is a function
27
+ * that the linker runtime can call to inject it with the cells it needs by
28
+ * the names it sees for them.
29
+ */
3
30
  export type BundlerKit = {
4
31
  /**
5
32
  * Produces a JavaScript string consisting of
@@ -14,15 +41,13 @@ export type BundlerKit = {
14
41
  /**
15
42
  * Produces a JavaScript string consisting of
16
43
  * a JavaScript object and a trailing comma.
17
- * The string is evaluated in a lexical context with a `cell` maker, the `cells`
18
- * array of every module's internal environment record.
44
+ * The string is evaluated in the linker runtime's lexical context.
19
45
  */
20
46
  getCells: () => string;
21
47
  /**
22
- * Produces a JavaScript string may
23
- * be a statement that calls this module's functor with the calling convention
24
- * appropriate for its language, injecting whatever cells it needs to link to
25
- * other module namespaces.
48
+ * Produces a JavaScript string,
49
+ * a statement that effects the module's evaluation behavior using the cells
50
+ * it imports and exports and the evaluated "functor".
26
51
  */
27
52
  getFunctorCall: () => string;
28
53
  /**
@@ -33,8 +58,10 @@ export type BundlerKit = {
33
58
  };
34
59
  export type BundleModule<SpecificModuleSource extends unknown> = {
35
60
  key: string;
61
+ exit: string;
36
62
  compartmentName: string;
37
63
  moduleSpecifier: string;
64
+ sourceDirname: string;
38
65
  parser: string;
39
66
  record: StaticModuleType & SpecificModuleSource;
40
67
  resolvedImports: Record<string, string>;
@@ -43,7 +70,17 @@ export type BundleModule<SpecificModuleSource extends unknown> = {
43
70
  index: number;
44
71
  bundlerKit: BundlerKit;
45
72
  };
46
- export type GetBundlerKit<SpecificModuleSource extends unknown> = (module: BundleModule<SpecificModuleSource>) => BundlerKit;
73
+ export type BundleExit = {
74
+ exit: string;
75
+ index: number;
76
+ bundlerKit: BundlerKit;
77
+ indexedImports: Record<string, number>;
78
+ resolvedImports: Record<string, string>;
79
+ };
80
+ export type GetBundlerKit<SpecificModuleSource extends unknown> = (module: BundleModule<SpecificModuleSource>, params: {
81
+ useEvaluate?: boolean | undefined;
82
+ sourceUrlPrefix?: string | undefined;
83
+ }) => BundlerKit;
47
84
  export type BundlerSupport<SpecificModuleSource extends unknown> = {
48
85
  runtime: string;
49
86
  getBundlerKit: GetBundlerKit<SpecificModuleSource>;
@@ -51,7 +88,8 @@ export type BundlerSupport<SpecificModuleSource extends unknown> = {
51
88
  import type { ReadFn } from './types.js';
52
89
  import type { ReadPowers } from './types.js';
53
90
  import type { MaybeReadPowers } from './types.js';
54
- import type { ArchiveOptions } from './types.js';
91
+ import type { CompartmentMapDescriptor } from './types.js';
92
+ import type { BundleOptions } from './types.js';
55
93
  import type { WriteFn } from './types.js';
56
94
  import type { StaticModuleType } from 'ses';
57
95
  //# sourceMappingURL=bundle.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bundle.d.ts","sourceRoot":"","sources":["bundle.js"],"names":[],"mappings":"AAsOO,uCALI,MAAM,GAAG,UAAU,GAAG,eAAe,kBACrC,MAAM,yCAEJ,OAAO,CAAC,MAAM,CAAC,CAgN3B;AASM,mCANI,OAAO,QACP,MAAM,kBACN,MAAM,kBACN,MAAM,uDAahB;;;;;;;;;;;gBAlba,MAAM,MAAM;;;;;;;cAOZ,MAAM,MAAM;;;;;;;oBAIZ,MAAM,MAAM;;;;;wBAIZ,MAAM,MAAM;;yBAKH,oBAAoB,SAA9B,OAAS;SAER,MAAM;qBACN,MAAM;qBACN,MAAM;YACN,MAAM;YACN,gBAAgB,GAAG,oBAAoB;qBACvC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;oBACtB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;WACtB,UAAU;WACV,MAAM;gBACN,UAAU;;0BAID,oBAAoB,SAA9B,OAAS,aAEX,YAAY,CAAC,oBAAoB,CAAC,KAChC,UAAU;2BAIA,oBAAoB,SAA9B,OAAS;aAER,MAAM;mBACN,aAAa,CAAC,oBAAoB,CAAC;;4BAlDvC,YAAY;gCAAZ,YAAY;qCAAZ,YAAY;oCAAZ,YAAY;6BAAZ,YAAY;sCAVZ,KAAK"}
1
+ {"version":3,"file":"bundle.d.ts","sourceRoot":"","sources":["bundle.js"],"names":[],"mappings":"AAgUO,+CALI,MAAM,GAAG,UAAU,GAAG,eAAe,kBACrC,wBAAwB,YACxB,aAAa,GACX,OAAO,CAAC,MAAM,CAAC,CAiV3B;AAQM,8CALI,MAAM,GAAG,UAAU,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