@endo/compartment-mapper 1.3.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/README.md +64 -27
  2. package/archive-lite.d.ts +1 -0
  3. package/archive-lite.js +3 -0
  4. package/archive-parsers.d.ts +1 -0
  5. package/archive-parsers.js +3 -0
  6. package/archive.d.ts +1 -0
  7. package/archive.js +3 -0
  8. package/bundle.d.ts +1 -0
  9. package/bundle.js +3 -0
  10. package/capture-lite.d.ts +1 -0
  11. package/capture-lite.js +3 -0
  12. package/import-archive-all-parsers.d.ts +2 -0
  13. package/import-archive-all-parsers.d.ts.map +1 -0
  14. package/import-archive-all-parsers.js +1 -0
  15. package/import-archive-lite.d.ts +1 -0
  16. package/import-archive-lite.js +3 -0
  17. package/import-archive-parsers.d.ts +1 -0
  18. package/import-archive-parsers.js +3 -0
  19. package/import-archive.d.ts +1 -0
  20. package/import-archive.js +3 -0
  21. package/import-lite.d.ts +1 -0
  22. package/import-lite.js +3 -0
  23. package/import-parsers.d.ts +1 -0
  24. package/import-parsers.js +3 -0
  25. package/import.d.ts +1 -0
  26. package/import.js +3 -0
  27. package/index.d.ts +1 -0
  28. package/index.js +3 -1
  29. package/node-modules.d.ts +1 -0
  30. package/node-modules.js +3 -0
  31. package/node-powers.d.ts +1 -0
  32. package/node-powers.js +3 -0
  33. package/package.json +16 -15
  34. package/src/archive-lite.d.ts +8 -10
  35. package/src/archive-lite.d.ts.map +1 -1
  36. package/src/archive-lite.js +35 -209
  37. package/src/archive.d.ts.map +1 -1
  38. package/src/archive.js +140 -28
  39. package/src/bundle.d.ts.map +1 -1
  40. package/src/bundle.js +62 -16
  41. package/src/capture-lite.d.ts +2 -2
  42. package/src/capture-lite.d.ts.map +1 -1
  43. package/src/capture-lite.js +27 -201
  44. package/src/compartment-map.d.ts +2 -1
  45. package/src/compartment-map.d.ts.map +1 -1
  46. package/src/compartment-map.js +11 -3
  47. package/src/digest.d.ts +5 -0
  48. package/src/digest.d.ts.map +1 -0
  49. package/src/digest.js +235 -0
  50. package/src/extension.d.ts.map +1 -1
  51. package/src/extension.js +1 -3
  52. package/src/import-archive-all-parsers.d.ts +11 -0
  53. package/src/import-archive-all-parsers.d.ts.map +1 -0
  54. package/src/import-archive-all-parsers.js +29 -0
  55. package/src/import-archive-lite.d.ts +3 -19
  56. package/src/import-archive-lite.d.ts.map +1 -1
  57. package/src/import-archive-lite.js +47 -57
  58. package/src/import-archive-parsers.d.ts.map +1 -1
  59. package/src/import-archive-parsers.js +1 -0
  60. package/src/import-archive.d.ts +2 -2
  61. package/src/import-archive.d.ts.map +1 -1
  62. package/src/import-archive.js +15 -12
  63. package/src/import-hook.d.ts +2 -1
  64. package/src/import-hook.d.ts.map +1 -1
  65. package/src/import-hook.js +87 -76
  66. package/src/import-lite.d.ts.map +1 -1
  67. package/src/import-lite.js +26 -23
  68. package/src/import.d.ts.map +1 -1
  69. package/src/import.js +49 -23
  70. package/src/infer-exports.d.ts.map +1 -1
  71. package/src/infer-exports.js +2 -3
  72. package/src/json.d.ts.map +1 -1
  73. package/src/json.js +1 -2
  74. package/src/link.d.ts.map +1 -1
  75. package/src/link.js +14 -29
  76. package/src/map-parser.d.ts.map +1 -1
  77. package/src/map-parser.js +25 -17
  78. package/src/node-module-specifier.d.ts.map +1 -1
  79. package/src/node-module-specifier.js +2 -3
  80. package/src/node-modules.d.ts +10 -8
  81. package/src/node-modules.d.ts.map +1 -1
  82. package/src/node-modules.js +212 -97
  83. package/src/node-powers.d.ts +8 -8
  84. package/src/node-powers.d.ts.map +1 -1
  85. package/src/node-powers.js +29 -24
  86. package/src/parse-archive-cjs.d.ts +3 -2
  87. package/src/parse-archive-cjs.d.ts.map +1 -1
  88. package/src/parse-archive-cjs.js +5 -4
  89. package/src/parse-archive-mjs.d.ts +3 -2
  90. package/src/parse-archive-mjs.d.ts.map +1 -1
  91. package/src/parse-archive-mjs.js +7 -5
  92. package/src/parse-bytes.d.ts +3 -2
  93. package/src/parse-bytes.d.ts.map +1 -1
  94. package/src/parse-bytes.js +7 -5
  95. package/src/parse-cjs-shared-export-wrapper.d.ts.map +1 -1
  96. package/src/parse-cjs-shared-export-wrapper.js +7 -6
  97. package/src/parse-cjs.d.ts +3 -2
  98. package/src/parse-cjs.d.ts.map +1 -1
  99. package/src/parse-cjs.js +14 -5
  100. package/src/parse-json.d.ts.map +1 -1
  101. package/src/parse-json.js +6 -6
  102. package/src/parse-mjs.d.ts +3 -2
  103. package/src/parse-mjs.d.ts.map +1 -1
  104. package/src/parse-mjs.js +3 -3
  105. package/src/parse-pre-cjs.d.ts +3 -2
  106. package/src/parse-pre-cjs.d.ts.map +1 -1
  107. package/src/parse-pre-cjs.js +14 -4
  108. package/src/parse-pre-mjs.d.ts +3 -2
  109. package/src/parse-pre-mjs.d.ts.map +1 -1
  110. package/src/parse-pre-mjs.js +4 -3
  111. package/src/parse-text.d.ts +3 -2
  112. package/src/parse-text.d.ts.map +1 -1
  113. package/src/parse-text.js +6 -5
  114. package/src/policy-format.d.ts +1 -1
  115. package/src/policy-format.d.ts.map +1 -1
  116. package/src/policy-format.js +5 -7
  117. package/src/policy.d.ts.map +1 -1
  118. package/src/policy.js +15 -10
  119. package/src/powers.d.ts.map +1 -1
  120. package/src/powers.js +15 -12
  121. package/src/search.d.ts.map +1 -1
  122. package/src/search.js +10 -7
  123. package/src/types/compartment-map-schema.d.ts +98 -0
  124. package/src/types/compartment-map-schema.d.ts.map +1 -0
  125. package/src/types/compartment-map-schema.ts +116 -0
  126. package/src/types/external.d.ts +299 -0
  127. package/src/types/external.d.ts.map +1 -0
  128. package/src/types/external.ts +428 -0
  129. package/src/types/internal.d.ts +162 -0
  130. package/src/types/internal.d.ts.map +1 -0
  131. package/src/types/internal.ts +217 -0
  132. package/src/types/node-powers.d.ts +46 -0
  133. package/src/types/node-powers.d.ts.map +1 -0
  134. package/src/types/node-powers.ts +52 -0
  135. package/src/types/policy-schema.d.ts +81 -0
  136. package/src/types/policy-schema.d.ts.map +1 -0
  137. package/src/types/policy-schema.ts +131 -0
  138. package/src/types/policy.d.ts +20 -0
  139. package/src/types/policy.d.ts.map +1 -0
  140. package/src/types/policy.ts +42 -0
  141. package/src/types/powers.d.ts +83 -0
  142. package/src/types/powers.d.ts.map +1 -0
  143. package/src/types/powers.ts +120 -0
  144. package/src/types/typescript.d.ts +28 -0
  145. package/src/types/typescript.d.ts.map +1 -0
  146. package/src/types/typescript.ts +41 -0
  147. package/src/types-external.d.ts +14 -0
  148. package/src/types-external.js +2 -0
  149. package/src/types.d.ts +9 -787
  150. package/src/url.d.ts.map +1 -1
  151. package/src/url.js +2 -3
  152. package/src/types.d.ts.map +0 -1
  153. package/src/types.js +0 -995
  154. package/types.d.ts +0 -19
package/src/digest.js ADDED
@@ -0,0 +1,235 @@
1
+ /* eslint-disable no-shadow */
2
+ /**
3
+ * Provides {@link digestCompartmentMap} which creates a digest of a compartment
4
+ * map suitable for archival or further inspection.
5
+ *
6
+ * @module
7
+ */
8
+
9
+ /**
10
+ * @import {
11
+ * CompartmentDescriptor,
12
+ * CompartmentMapDescriptor,
13
+ * DigestResult,
14
+ * ModuleDescriptor,
15
+ * Sources,
16
+ * } from './types.js'
17
+ */
18
+
19
+ import {
20
+ assertCompartmentMap,
21
+ pathCompare,
22
+ stringCompare,
23
+ } from './compartment-map.js';
24
+
25
+ const { create, fromEntries, entries, keys } = Object;
26
+
27
+ /**
28
+ * We attempt to produce compartment maps that are consistent regardless of
29
+ * whether the packages were originally laid out on disk for development or
30
+ * production, and other trivia like the fully qualified path of a specific
31
+ * installation.
32
+ *
33
+ * Naming compartments for the self-ascribed name and version of each Node.js
34
+ * package is insufficient because they are not guaranteed to be unique.
35
+ * Dependencies do not necessarilly come from the npm registry and may be
36
+ * for example derived from fully qualified URL's or Github org and project
37
+ * names.
38
+ * Package managers are also not required to fully deduplicate the hard
39
+ * copy of each package even when they are identical resources.
40
+ * Duplication is undesirable, but we elect to defer that problem to solutions
41
+ * in the package managers, as the alternative would be to consistently hash
42
+ * the original sources of the packages themselves, which may not even be
43
+ * available much less pristine for us.
44
+ *
45
+ * So, instead, we use the lexically least path of dependency names, delimited
46
+ * by hashes.
47
+ * The compartment maps generated by the ./node-modules.js tooling pre-compute
48
+ * these traces for our use here.
49
+ * We sort the compartments lexically on their self-ascribed name and version,
50
+ * and use the lexically least dependency name path as a tie-breaker.
51
+ * The dependency path is logical and orthogonal to the package manager's
52
+ * actual installation location, so should be orthogonal to the vagaries of the
53
+ * package manager's deduplication algorithm.
54
+ *
55
+ * @param {Record<string, CompartmentDescriptor>} compartments
56
+ * @returns {Record<string, string>} map from old to new compartment names.
57
+ */
58
+ const renameCompartments = compartments => {
59
+ /** @type {Record<string, string>} */
60
+ const compartmentRenames = create(null);
61
+ let index = 0;
62
+ let prev = '';
63
+
64
+ // The sort below combines two comparators to avoid depending on sort
65
+ // stability, which became standard as recently as 2019.
66
+ // If that date seems quaint, please accept my regards from the distant past.
67
+ // We are very proud of you.
68
+ const compartmentsByPath = Object.entries(compartments)
69
+ .map(([name, compartment]) => ({
70
+ name,
71
+ path: compartment.path,
72
+ label: compartment.label,
73
+ }))
74
+ .sort((a, b) => {
75
+ if (a.label === b.label) {
76
+ assert(a.path !== undefined && b.path !== undefined);
77
+ return pathCompare(a.path, b.path);
78
+ }
79
+ return stringCompare(a.label, b.label);
80
+ });
81
+
82
+ for (const { name, label } of compartmentsByPath) {
83
+ if (label === prev) {
84
+ compartmentRenames[name] = `${label}-n${index}`;
85
+ index += 1;
86
+ } else {
87
+ compartmentRenames[name] = label;
88
+ prev = label;
89
+ index = 1;
90
+ }
91
+ }
92
+ return compartmentRenames;
93
+ };
94
+
95
+ /**
96
+ * @param {Record<string, CompartmentDescriptor>} compartments
97
+ * @param {Sources} sources
98
+ * @param {Record<string, string>} compartmentRenames
99
+ */
100
+ const translateCompartmentMap = (compartments, sources, compartmentRenames) => {
101
+ const result = create(null);
102
+ for (const compartmentName of keys(compartmentRenames)) {
103
+ const compartment = compartments[compartmentName];
104
+ const { name, label, retained: compartmentRetained, policy } = compartment;
105
+ if (compartmentRetained) {
106
+ // rename module compartments
107
+ /** @type {Record<string, ModuleDescriptor>} */
108
+ const modules = create(null);
109
+ const compartmentModules = compartment.modules;
110
+ if (compartment.modules) {
111
+ for (const name of keys(compartmentModules).sort()) {
112
+ const { retained: moduleRetained, ...retainedModule } =
113
+ compartmentModules[name];
114
+ if (moduleRetained) {
115
+ if (retainedModule.compartment !== undefined) {
116
+ modules[name] = {
117
+ ...retainedModule,
118
+ compartment: compartmentRenames[retainedModule.compartment],
119
+ };
120
+ } else {
121
+ modules[name] = retainedModule;
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ // integrate sources into modules
128
+ const compartmentSources = sources[compartmentName];
129
+ if (compartmentSources) {
130
+ for (const name of keys(compartmentSources).sort()) {
131
+ const source = compartmentSources[name];
132
+ const { location, parser, exit, sha512, deferredError } = source;
133
+ if (location !== undefined) {
134
+ modules[name] = {
135
+ location,
136
+ parser,
137
+ sha512,
138
+ };
139
+ } else if (exit !== undefined) {
140
+ modules[name] = {
141
+ exit,
142
+ };
143
+ } else if (deferredError !== undefined) {
144
+ modules[name] = {
145
+ deferredError,
146
+ };
147
+ }
148
+ }
149
+ }
150
+
151
+ result[compartmentRenames[compartmentName]] = {
152
+ name,
153
+ label,
154
+ location: compartmentRenames[compartmentName],
155
+ modules,
156
+ policy,
157
+ // `scopes`, `types`, and `parsers` are not necessary since every
158
+ // loadable module is captured in `modules`.
159
+ };
160
+ }
161
+ }
162
+
163
+ return result;
164
+ };
165
+
166
+ /**
167
+ * @param {Sources} sources
168
+ * @param {Record<string, string>} compartmentRenames
169
+ * @returns {Sources}
170
+ */
171
+ const renameSources = (sources, compartmentRenames) => {
172
+ return fromEntries(
173
+ entries(sources).map(([name, compartmentSources]) => [
174
+ compartmentRenames[name],
175
+ compartmentSources,
176
+ ]),
177
+ );
178
+ };
179
+
180
+ /**
181
+ * @param {CompartmentMapDescriptor} compartmentMap
182
+ * @param {Sources} sources
183
+ * @returns {DigestResult}
184
+ */
185
+ export const digestCompartmentMap = (compartmentMap, sources) => {
186
+ const {
187
+ compartments,
188
+ entry: { compartment: entryCompartmentName, module: entryModuleSpecifier },
189
+ } = compartmentMap;
190
+
191
+ const oldToNewCompartmentNames = renameCompartments(compartments);
192
+ const digestCompartments = translateCompartmentMap(
193
+ compartments,
194
+ sources,
195
+ oldToNewCompartmentNames,
196
+ );
197
+ const digestEntryCompartmentName =
198
+ oldToNewCompartmentNames[entryCompartmentName];
199
+ const digestSources = renameSources(sources, oldToNewCompartmentNames);
200
+
201
+ const digestCompartmentMap = {
202
+ // TODO graceful migration from tags to conditions
203
+ // https://github.com/endojs/endo/issues/2388
204
+ tags: [],
205
+ entry: {
206
+ compartment: digestEntryCompartmentName,
207
+ module: entryModuleSpecifier,
208
+ },
209
+ compartments: digestCompartments,
210
+ };
211
+
212
+ // Cross-check:
213
+ // We assert that we have constructed a valid compartment map, not because it
214
+ // might not be, but to ensure that the assertCompartmentMap function can
215
+ // accept all valid compartment maps.
216
+ assertCompartmentMap(digestCompartmentMap);
217
+
218
+ const newToOldCompartmentNames = fromEntries(
219
+ entries(oldToNewCompartmentNames).map(([oldName, newName]) => [
220
+ newName,
221
+ oldName,
222
+ ]),
223
+ );
224
+
225
+ /** @type {DigestResult} */
226
+ const digestResult = {
227
+ compartmentMap: digestCompartmentMap,
228
+ sources: digestSources,
229
+ oldToNewCompartmentNames,
230
+ newToOldCompartmentNames,
231
+ compartmentRenames: newToOldCompartmentNames,
232
+ };
233
+
234
+ return digestResult;
235
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"extension.d.ts","sourceRoot":"","sources":["extension.js"],"names":[],"mappings":"AAYO,yCAHI,MAAM,GACJ,MAAM,CAalB"}
1
+ {"version":3,"file":"extension.d.ts","sourceRoot":"","sources":["extension.js"],"names":[],"mappings":"AAUO,yCAHI,MAAM,GACJ,MAAM,CAalB"}
package/src/extension.js CHANGED
@@ -1,6 +1,4 @@
1
- /* Extracts the extension from a URL pathname. */
2
-
3
- // @ts-check
1
+ /** @module Extracts the extension from a URL pathname. */
4
2
 
5
3
  /**
6
4
  * `parseExtension` returns the file extension for the given URL, or an empty
@@ -0,0 +1,11 @@
1
+ /** @satisfies {Readonly<ParserForLanguage>} */
2
+ export const defaultParserForLanguage: Readonly<{
3
+ readonly 'pre-cjs-json': import("./types.js").ParserImplementation;
4
+ readonly 'pre-mjs-json': import("./types.js").ParserImplementation;
5
+ readonly cjs: import("./types.js").ParserImplementation;
6
+ readonly mjs: import("./types.js").ParserImplementation;
7
+ readonly json: import("./types.js").ParserImplementation;
8
+ readonly text: import("./types.js").ParserImplementation;
9
+ readonly bytes: import("./types.js").ParserImplementation;
10
+ }>;
11
+ //# sourceMappingURL=import-archive-all-parsers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"import-archive-all-parsers.d.ts","sourceRoot":"","sources":["import-archive-all-parsers.js"],"names":[],"mappings":"AAiBA,+CAA+C;AAC/C;;;;;;;;GAUE"}
@@ -0,0 +1,29 @@
1
+ /* Provides a set of default language behaviors (parsers) suitable for
2
+ * evaluating archives (zip files with a `compartment-map.json` and a file for
3
+ * each module) with pre-compiled _or_ original ESM and CommonJS.
4
+ *
5
+ * This module does not entrain a dependency on Babel on XS, but does on other
6
+ * platforms like Node.js.
7
+ */
8
+ /** @import {ParserForLanguage} from './types.js' */
9
+
10
+ import parserPreCjs from './parse-pre-cjs.js';
11
+ import parserJson from './parse-json.js';
12
+ import parserText from './parse-text.js';
13
+ import parserBytes from './parse-bytes.js';
14
+ import parserPreMjs from './parse-pre-mjs.js';
15
+ import parserMjs from './parse-mjs.js';
16
+ import parserCjs from './parse-cjs.js';
17
+
18
+ /** @satisfies {Readonly<ParserForLanguage>} */
19
+ export const defaultParserForLanguage = Object.freeze(
20
+ /** @type {const} */ ({
21
+ 'pre-cjs-json': parserPreCjs,
22
+ 'pre-mjs-json': parserPreMjs,
23
+ cjs: parserCjs,
24
+ mjs: parserMjs,
25
+ json: parserJson,
26
+ text: parserText,
27
+ bytes: parserBytes,
28
+ }),
29
+ );
@@ -1,23 +1,7 @@
1
- export function parseArchive(archiveBytes: Uint8Array, archiveLocation?: string | undefined, options?: {
2
- expectedSha512?: string | undefined;
3
- computeSha512?: HashFn | undefined;
4
- modules?: Record<string, unknown> | undefined;
5
- importHook?: ExitModuleImportHook | undefined;
6
- Compartment?: typeof Compartment | undefined;
7
- computeSourceLocation?: ComputeSourceLocationHook | undefined;
8
- computeSourceMapLocation?: ComputeSourceMapLocationHook | undefined;
9
- parserForLanguage?: ParserForLanguage | undefined;
10
- languageForExtension?: LanguageForExtension | undefined;
11
- } | undefined): Promise<Application>;
12
- export function loadArchive(readPowers: ReadFn | ReadPowers, archiveLocation: string, options?: LoadArchiveOptions | undefined): Promise<Application>;
1
+ export function parseArchive(archiveBytes: Uint8Array, archiveLocation?: string | undefined, options?: ParseArchiveOptions | undefined): Promise<Application>;
2
+ export function loadArchive(readPowers: ReadFn | ReadPowers, archiveLocation: string, options?: ParseArchiveOptions | undefined): Promise<Application>;
13
3
  export function importArchive(readPowers: ReadFn | ReadPowers, archiveLocation: string, options: LoadArchiveOptions): Promise<SomeObject>;
14
- export type CompartmentConstructor = typeof Compartment;
15
- import type { HashFn } from './types.js';
16
- import type { ExitModuleImportHook } from './types.js';
17
- import type { ComputeSourceLocationHook } from './types.js';
18
- import type { ComputeSourceMapLocationHook } from './types.js';
19
- import type { ParserForLanguage } from './types.js';
20
- import type { LanguageForExtension } from './types.js';
4
+ import type { ParseArchiveOptions } from './types.js';
21
5
  import type { Application } from './types.js';
22
6
  import type { ReadFn } from './types.js';
23
7
  import type { ReadPowers } from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"import-archive-lite.d.ts","sourceRoot":"","sources":["import-archive-lite.js"],"names":[],"mappings":"AA4PO,2CAdI,UAAU;;;;;;;;;;gBAYR,OAAO,CAAC,WAAW,CAAC,CAiKhC;AAQM,wCALI,MAAM,GAAG,UAAU,mBACnB,MAAM,6CAEJ,OAAO,CAAC,WAAW,CAAC,CA0BhC;AAQM,0CALI,MAAM,GAAG,UAAU,mBACnB,MAAM,WACN,kBAAkB,GAChB,OAAO,CAAC,UAAU,CAAC,CAK/B;qCA3Na,OAAO,WAAW;4BAlNN,YAAY;0CADE,YAAY;+CAHP,YAAY;kDACT,YAAY;uCAOvB,YAAY;0CAFT,YAAY;iCARrB,YAAY;4BAWjB,YAAY;gCACR,YAAY;wCAHJ,YAAY;gCAIpB,YAAY"}
1
+ {"version":3,"file":"import-archive-lite.d.ts","sourceRoot":"","sources":["import-archive-lite.js"],"names":[],"mappings":"AAoPO,2CALI,UAAU,oFAGR,OAAO,CAAC,WAAW,CAAC,CAiKhC;AAQM,wCALI,MAAM,GAAG,UAAU,mBACnB,MAAM,8CAEJ,OAAO,CAAC,WAAW,CAAC,CAwBhC;AAQM,0CALI,MAAM,GAAG,UAAU,mBACnB,MAAM,WACN,kBAAkB,GAChB,OAAO,CAAC,UAAU,CAAC,CAK/B;yCAxZS,YAAY;iCAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;wCAAZ,YAAY;gCAAZ,YAAY"}
@@ -1,4 +1,5 @@
1
- /* Provides functions for evaluating the modules in an archive (a zip
1
+ /**
2
+ * @module Provides functions for evaluating the modules in an archive (a zip
2
3
  * file with a `compartment-map.json` and a file for each module it contains.)
3
4
  *
4
5
  * These functions do not have a bias for any particular mapping, so you will
@@ -11,26 +12,30 @@
11
12
  * `@endo/compartment-mapper/archive-parsers.js`.
12
13
  */
13
14
 
14
- // @ts-check
15
15
  /* eslint no-shadow: "off" */
16
16
 
17
- /** @import {ImportHook} from 'ses' */
18
- /** @import {ModuleExportsNamespace} from 'ses' */
19
- /** @import {StaticModuleType} from 'ses' */
20
- /** @import {Application} from './types.js' */
21
- /** @import {CompartmentDescriptor} from './types.js' */
22
- /** @import {ComputeSourceLocationHook} from './types.js' */
23
- /** @import {ComputeSourceMapLocationHook} from './types.js' */
24
- /** @import {ExecuteFn} from './types.js' */
25
- /** @import {ExitModuleImportHook} from './types.js' */
26
- /** @import {HashFn} from './types.js' */
27
- /** @import {ImportHookMaker} from './types.js' */
28
- /** @import {LanguageForExtension} from './types.js' */
29
- /** @import {LoadArchiveOptions} from './types.js' */
30
- /** @import {ParserForLanguage} from './types.js' */
31
- /** @import {ReadFn} from './types.js' */
32
- /** @import {ReadPowers} from './types.js' */
33
- /** @import {SomeObject} from './types.js' */
17
+ /**
18
+ * @import {
19
+ * ImportHook,
20
+ * StaticModuleType,
21
+ * } from 'ses';
22
+ * @import {
23
+ * Application,
24
+ * CompartmentDescriptor,
25
+ * ComputeSourceLocationHook,
26
+ * ComputeSourceMapLocationHook,
27
+ * ExecuteFn,
28
+ * ExitModuleImportHook,
29
+ * HashFn,
30
+ * ImportHookMaker,
31
+ * LoadArchiveOptions,
32
+ * ParseArchiveOptions,
33
+ * ParserForLanguage,
34
+ * ReadFn,
35
+ * ReadPowers,
36
+ * SomeObject,
37
+ * } from './types.js'
38
+ */
34
39
 
35
40
  import { ZipReader } from '@endo/zip';
36
41
  import { link } from './link.js';
@@ -41,8 +46,6 @@ import { assertCompartmentMap } from './compartment-map.js';
41
46
  import { exitModuleImportHookMaker } from './import-hook.js';
42
47
  import { attenuateModuleHook, enforceModulePolicy } from './policy.js';
43
48
 
44
- const DefaultCompartment = Compartment;
45
-
46
49
  const { Fail, quote: q } = assert;
47
50
 
48
51
  const textDecoder = new TextDecoder();
@@ -119,7 +122,10 @@ const makeArchiveImportHookMaker = (
119
122
  moduleSpecifier,
120
123
  )} was not in the archive and an attempt was made to load it as a builtin`,
121
124
  });
122
- const record = await exitModuleImportHook(moduleSpecifier);
125
+ const record = await exitModuleImportHook(
126
+ moduleSpecifier,
127
+ packageLocation,
128
+ );
123
129
  if (record) {
124
130
  // note it's not being marked as exit in sources
125
131
  // it could get marked and the second pass, when the archive is being executed, would have the data
@@ -230,24 +236,10 @@ const makeArchiveImportHookMaker = (
230
236
  return makeImportHook;
231
237
  };
232
238
 
233
- // Have to give it a name to capture the external meaning of Compartment
234
- // Otherwise @param {typeof Compartment} takes the Compartment to mean
235
- // the const variable defined within the function.
236
- /** @typedef {typeof Compartment} CompartmentConstructor */
237
-
238
239
  /**
239
240
  * @param {Uint8Array} archiveBytes
240
241
  * @param {string} [archiveLocation]
241
- * @param {object} [options]
242
- * @param {string} [options.expectedSha512]
243
- * @param {HashFn} [options.computeSha512]
244
- * @param {Record<string, unknown>} [options.modules]
245
- * @param {ExitModuleImportHook} [options.importHook]
246
- * @param {CompartmentConstructor} [options.Compartment]
247
- * @param {ComputeSourceLocationHook} [options.computeSourceLocation]
248
- * @param {ComputeSourceMapLocationHook} [options.computeSourceMapLocation]
249
- * @param {ParserForLanguage} [options.parserForLanguage]
250
- * @param {LanguageForExtension} [options.languageForExtension]
242
+ * @param {ParseArchiveOptions} [options]
251
243
  * @returns {Promise<Application>}
252
244
  */
253
245
  export const parseArchive = async (
@@ -261,24 +253,16 @@ export const parseArchive = async (
261
253
  expectedSha512 = undefined,
262
254
  computeSourceLocation = undefined,
263
255
  computeSourceMapLocation = undefined,
264
- Compartment = DefaultCompartment,
256
+ Compartment: CompartmentParseOption = Compartment,
265
257
  modules = undefined,
266
258
  importHook: exitModuleImportHook = undefined,
267
259
  parserForLanguage: parserForLanguageOption = {},
268
- languageForExtension: languageForExtensionOption = {},
260
+ __native__ = false,
269
261
  } = options;
270
262
 
271
263
  const parserForLanguage = freeze(
272
264
  assign(create(null), parserForLanguageOption),
273
265
  );
274
- const languageForExtension = freeze(
275
- assign(create(null), languageForExtensionOption),
276
- );
277
-
278
- const compartmentExitModuleImportHook = exitModuleImportHookMaker({
279
- modules,
280
- exitModuleImportHook,
281
- });
282
266
 
283
267
  const archive = new ZipReader(archiveBytes, { name: archiveLocation });
284
268
 
@@ -324,9 +308,15 @@ export const parseArchive = async (
324
308
 
325
309
  const {
326
310
  compartments,
327
- entry: { module: moduleSpecifier },
311
+ entry: { module: entryModuleSpecifier, compartment: entryCompartmentName },
328
312
  } = compartmentMap;
329
313
 
314
+ const compartmentExitModuleImportHook = exitModuleImportHookMaker({
315
+ modules,
316
+ exitModuleImportHook,
317
+ entryCompartmentName,
318
+ });
319
+
330
320
  // Archive integrity checks: ensure every module is pre-loaded so its hash
331
321
  // gets checked, and ensure that every file in the archive is used, and
332
322
  // therefore checked.
@@ -348,18 +338,18 @@ export const parseArchive = async (
348
338
  const { compartment, pendingJobsPromise } = link(compartmentMap, {
349
339
  makeImportHook,
350
340
  parserForLanguage,
351
- languageForExtension,
352
341
  modules: Object.fromEntries(
353
342
  Object.keys(modules || {}).map(specifier => {
354
343
  return [specifier, { namespace: {} }];
355
344
  }),
356
345
  ),
357
- Compartment,
346
+ Compartment: CompartmentParseOption,
347
+ __native__,
358
348
  });
359
349
 
360
350
  await pendingJobsPromise;
361
351
 
362
- await compartment.load(moduleSpecifier);
352
+ await compartment.load(entryModuleSpecifier);
363
353
  unseen.size === 0 ||
364
354
  Fail`Archive contains extraneous files: ${q([...unseen])} in ${q(
365
355
  archiveLocation,
@@ -373,13 +363,15 @@ export const parseArchive = async (
373
363
  modules,
374
364
  transforms,
375
365
  __shimTransforms__,
376
- Compartment,
366
+ Compartment: CompartmentOption = CompartmentParseOption,
367
+ __native__,
377
368
  importHook: exitModuleImportHook,
378
369
  } = options || {};
379
370
 
380
371
  const compartmentExitModuleImportHook = exitModuleImportHookMaker({
381
372
  modules,
382
373
  exitModuleImportHook,
374
+ entryCompartmentName,
383
375
  });
384
376
  const makeImportHook = makeArchiveImportHookMaker(
385
377
  get,
@@ -394,18 +386,18 @@ export const parseArchive = async (
394
386
  const { compartment, pendingJobsPromise } = link(compartmentMap, {
395
387
  makeImportHook,
396
388
  parserForLanguage,
397
- languageForExtension,
398
389
  globals,
399
390
  modules,
400
391
  transforms,
401
392
  __shimTransforms__,
402
- Compartment,
393
+ Compartment: CompartmentOption,
394
+ __native__,
403
395
  });
404
396
 
405
397
  await pendingJobsPromise;
406
398
 
407
399
  // eslint-disable-next-line dot-notation
408
- return compartment['import'](moduleSpecifier);
400
+ return compartment['import'](entryModuleSpecifier);
409
401
  };
410
402
 
411
403
  return { import: execute, sha512 };
@@ -429,7 +421,6 @@ export const loadArchive = async (
429
421
  modules,
430
422
  computeSourceMapLocation,
431
423
  parserForLanguage,
432
- languageForExtension,
433
424
  } = options;
434
425
  const archiveBytes = await read(archiveLocation);
435
426
  return parseArchive(archiveBytes, archiveLocation, {
@@ -439,7 +430,6 @@ export const loadArchive = async (
439
430
  modules,
440
431
  computeSourceMapLocation,
441
432
  parserForLanguage,
442
- languageForExtension,
443
433
  });
444
434
  };
445
435
 
@@ -1 +1 @@
1
- {"version":3,"file":"import-archive-parsers.d.ts","sourceRoot":"","sources":["import-archive-parsers.js"],"names":[],"mappings":"AAcA,+CAA+C;AAC/C;;;;;;GAQE"}
1
+ {"version":3,"file":"import-archive-parsers.d.ts","sourceRoot":"","sources":["import-archive-parsers.js"],"names":[],"mappings":"AAeA,+CAA+C;AAC/C;;;;;;GAQE"}
@@ -4,6 +4,7 @@
4
4
  *
5
5
  * This module does not entrain a dependency on Babel.
6
6
  */
7
+
7
8
  /** @import {ParserForLanguage} from './types.js' */
8
9
 
9
10
  import parserPreCjs from './parse-pre-cjs.js';
@@ -1,5 +1,5 @@
1
1
  export function parseArchive(archiveBytes: Uint8Array, archiveLocation?: string | undefined, options?: Options | undefined): Promise<Application>;
2
- export function loadArchive(readPowers: import("@endo/zip").ReadFn | ReadPowers, archiveLocation: string, options?: LoadArchiveOptions | undefined): Promise<Application>;
2
+ export function loadArchive(readPowers: import("@endo/zip").ReadFn | ReadPowers, archiveLocation: string, options?: import("./types.js").ParseArchiveOptions | undefined): Promise<Application>;
3
3
  export function importArchive(readPowers: import("@endo/zip").ReadFn | ReadPowers, archiveLocation: string, options: ExecuteOptions & LoadArchiveOptions): Promise<object>;
4
4
  export type CompartmentConstructor = typeof Compartment;
5
5
  export type Options = {
@@ -14,8 +14,8 @@ export type Options = {
14
14
  };
15
15
  import type { Application } from './types.js';
16
16
  import type { ReadPowers } from './types.js';
17
- import type { LoadArchiveOptions } from './types.js';
18
17
  import type { ExecuteOptions } from './types.js';
18
+ import type { LoadArchiveOptions } from './types.js';
19
19
  import type { HashFn } from './types.js';
20
20
  import type { ExitModuleImportHook } from './types.js';
21
21
  import type { ComputeSourceLocationHook } from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"import-archive.d.ts","sourceRoot":"","sources":["import-archive.js"],"names":[],"mappings":"AA0EO,2CALI,UAAU,wEAGR,OAAO,CAAC,WAAW,CAAC,CAW9B;AAQI,wCALI,OAAO,WAAW,EAAE,MAAM,GAAG,UAAU,mBACvC,MAAM,6CAEJ,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;;;;;;;;;;;iCAdD,YAAY;gCAOb,YAAY;wCADJ,YAAY;oCAHhB,YAAY;4BAEpB,YAAY;0CADE,YAAY;+CAHP,YAAY;kDACT,YAAY;uCAMvB,YAAY"}
1
+ {"version":3,"file":"import-archive.d.ts","sourceRoot":"","sources":["import-archive.js"],"names":[],"mappings":"AA6EO,2CALI,UAAU,wEAGR,OAAO,CAAC,WAAW,CAAC,CAW9B;AAQI,wCALI,OAAO,WAAW,EAAE,MAAM,GAAG,UAAU,mBACvC,MAAM,mEAEJ,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;oCAAZ,YAAY;wCAAZ,YAAY;4BAAZ,YAAY;0CAAZ,YAAY;+CAAZ,YAAY;kDAAZ,YAAY;uCAAZ,YAAY"}
@@ -1,4 +1,5 @@
1
- /* Provides functions for evaluating modules in an archive (a zip file
1
+ /**
2
+ * @module Provides functions for evaluating modules in an archive (a zip file
2
3
  * with a `compartment-map.json` and a file for a module and each of its
3
4
  * transitive dependencies.)
4
5
  *
@@ -13,7 +14,19 @@
13
14
  * dependency on Babel.
14
15
  */
15
16
 
16
- // @ts-check
17
+ /**
18
+ * @import {
19
+ * Application,
20
+ * ComputeSourceLocationHook,
21
+ * ComputeSourceMapLocationHook,
22
+ * ExecuteOptions,
23
+ * ExitModuleImportHook,
24
+ * HashFn,
25
+ * LoadArchiveOptions,
26
+ * ReadPowers,
27
+ * ParserForLanguage,
28
+ * } from './types.js'
29
+ */
17
30
 
18
31
  import { defaultParserForLanguage } from './import-archive-parsers.js';
19
32
  import {
@@ -24,16 +37,6 @@ import {
24
37
 
25
38
  const { assign, create, freeze } = Object;
26
39
 
27
- /** @import {Application} from './types.js' */
28
- /** @import {ComputeSourceLocationHook} from './types.js' */
29
- /** @import {ComputeSourceMapLocationHook} from './types.js' */
30
- /** @import {ExecuteOptions} from './types.js' */
31
- /** @import {ExitModuleImportHook} from './types.js' */
32
- /** @import {HashFn} from './types.js' */
33
- /** @import {LoadArchiveOptions} from './types.js' */
34
- /** @import {ReadPowers} from './types.js' */
35
- /** @import {ParserForLanguage} from './types.js' */
36
-
37
40
  // Must give the type of Compartment a name to capture the external meaning of
38
41
  // Compartment Otherwise @param {typeof Compartment} takes the Compartment to
39
42
  // mean the const variable defined within the function.
@@ -7,9 +7,10 @@
7
7
  * @returns {ImportNowHookMaker}
8
8
  */
9
9
  export function makeImportNowHookMaker(readPowers: ReadNowPowers, baseLocation: string, { sources, compartmentDescriptors, computeSha512, searchSuffixes, sourceMapHook, exitModuleImportNowHook, }: MakeImportNowHookMakerOptions): ImportNowHookMaker;
10
- export function exitModuleImportHookMaker({ modules, exitModuleImportHook, }: {
10
+ export function exitModuleImportHookMaker({ modules, exitModuleImportHook, entryCompartmentName, }: {
11
11
  modules?: Record<string, any> | undefined;
12
12
  exitModuleImportHook?: ExitModuleImportHook | undefined;
13
+ entryCompartmentName: string;
13
14
  }): ExitModuleImportHook | undefined;
14
15
  export function makeImportHookMaker(readPowers: ReadFn | ReadPowers, baseLocation: string, { sources, compartmentDescriptors, archiveOnly, computeSha512, searchSuffixes, sourceMapHook, entryCompartmentName, entryModuleSpecifier, exitModuleImportHook, }: {
15
16
  sources?: Sources | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"import-hook.d.ts","sourceRoot":"","sources":["import-hook.js"],"names":[],"mappings":"AAilBA;;;;;;;GAOG;AACH,mDALW,aAAa,gBACb,MAAM,+GACN,6BAA6B,GAC3B,kBAAkB,CAsJ9B;AAnjBM,8EAJJ;IAAqC,OAAO,GAApC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,YAAC;IACU,oBAAoB;CAC1D,GAAU,oBAAoB,GAAC,SAAS,CA0B1C;AA0MM,gDApBI,MAAM,GAAC,UAAU,gBACjB,MAAM,qKAEd;IAA0B,OAAO;IACuB,sBAAsB;IACpD,WAAW;IACZ,aAAa;IACN,cAAc;IAOtB,oBAAoB,EAApC,MAAM;IACU,oBAAoB,EAApC,MAAM;IACyB,oBAAoB;IAC3B,aAAa;CAC7C,GAAU,eAAe,CAqL3B;mCA7iBS,YAAY;mDAAZ,YAAY;wCAAZ,YAAY;0CAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;6BAAZ,YAAY;2CAAZ,YAAY;4BAAZ,YAAY;mCAAZ,YAAY;qCAAZ,YAAY"}
1
+ {"version":3,"file":"import-hook.d.ts","sourceRoot":"","sources":["import-hook.js"],"names":[],"mappings":"AA2lBA;;;;;;;GAOG;AACH,mDALW,aAAa,gBACb,MAAM,+GACN,6BAA6B,GAC3B,kBAAkB,CAuJ9B;AA5jBM,oGALJ;IAAqC,OAAO;IACN,oBAAoB;IACnC,oBAAoB,EAAnC,MAAM;CACd,GAAU,oBAAoB,GAAC,SAAS,CA8B1C;AA4MM,gDApBI,MAAM,GAAC,UAAU,gBACjB,MAAM,qKAEd;IAA0B,OAAO;IACuB,sBAAsB;IACpD,WAAW;IACZ,aAAa;IACN,cAAc;IAOtB,oBAAoB,EAApC,MAAM;IACU,oBAAoB,EAApC,MAAM;IACyB,oBAAoB;IAC3B,aAAa;CAC7C,GAAU,eAAe,CAuL3B;mCAtjBS,YAAY;mDAAZ,YAAY;wCAAZ,YAAY;0CAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;6BAAZ,YAAY;2CAAZ,YAAY;4BAAZ,YAAY;mCAAZ,YAAY;qCAAZ,YAAY"}