@endo/compartment-mapper 1.1.5 → 1.2.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 (130) hide show
  1. package/README.md +17 -28
  2. package/archive-lite.d.ts +2 -0
  3. package/archive-lite.js +7 -0
  4. package/archive-parsers.d.ts +2 -0
  5. package/archive-parsers.js +1 -0
  6. package/capture-lite.d.ts +2 -0
  7. package/capture-lite.js +1 -0
  8. package/import-archive-lite.d.ts +2 -0
  9. package/import-archive-lite.js +5 -0
  10. package/import-archive-parsers.d.ts +2 -0
  11. package/import-archive-parsers.js +1 -0
  12. package/import-lite.d.ts +2 -0
  13. package/import-lite.js +1 -0
  14. package/import-parsers.d.ts +2 -0
  15. package/import-parsers.js +1 -0
  16. package/node-modules.d.ts +2 -0
  17. package/node-modules.js +1 -0
  18. package/package.json +23 -14
  19. package/src/archive-lite.d.ts +20 -0
  20. package/src/archive-lite.d.ts.map +1 -0
  21. package/src/archive-lite.js +495 -0
  22. package/src/archive-parsers.d.ts +11 -0
  23. package/src/archive-parsers.d.ts.map +1 -0
  24. package/src/archive-parsers.js +26 -0
  25. package/src/archive.d.ts +0 -6
  26. package/src/archive.d.ts.map +1 -1
  27. package/src/archive.js +94 -420
  28. package/src/bundle-cjs.d.ts +6 -17
  29. package/src/bundle-cjs.d.ts.map +1 -1
  30. package/src/bundle-cjs.js +11 -3
  31. package/src/bundle-json.d.ts +15 -0
  32. package/src/bundle-json.d.ts.map +1 -0
  33. package/src/bundle-json.js +24 -0
  34. package/src/bundle-mjs.d.ts +3 -19
  35. package/src/bundle-mjs.d.ts.map +1 -1
  36. package/src/bundle-mjs.js +6 -0
  37. package/src/bundle.d.ts +53 -10
  38. package/src/bundle.d.ts.map +1 -1
  39. package/src/bundle.js +118 -56
  40. package/src/capture-lite.d.ts +7 -0
  41. package/src/capture-lite.d.ts.map +1 -0
  42. package/src/capture-lite.js +324 -0
  43. package/src/compartment-map.d.ts +1 -1
  44. package/src/compartment-map.d.ts.map +1 -1
  45. package/src/compartment-map.js +18 -37
  46. package/src/extension.d.ts.map +1 -1
  47. package/src/extension.js +2 -0
  48. package/src/import-archive-lite.d.ts +26 -0
  49. package/src/import-archive-lite.d.ts.map +1 -0
  50. package/src/import-archive-lite.js +455 -0
  51. package/src/import-archive-parsers.d.ts +9 -0
  52. package/src/import-archive-parsers.d.ts.map +1 -0
  53. package/src/import-archive-parsers.js +24 -0
  54. package/src/import-archive.d.ts +12 -9
  55. package/src/import-archive.d.ts.map +1 -1
  56. package/src/import-archive.js +74 -406
  57. package/src/import-hook.d.ts.map +1 -1
  58. package/src/import-hook.js +10 -3
  59. package/src/import-lite.d.ts +9 -0
  60. package/src/import-lite.d.ts.map +1 -0
  61. package/src/import-lite.js +121 -0
  62. package/src/import-parsers.d.ts +9 -0
  63. package/src/import-parsers.d.ts.map +1 -0
  64. package/src/import-parsers.js +24 -0
  65. package/src/import.d.ts +1 -4
  66. package/src/import.d.ts.map +1 -1
  67. package/src/import.js +44 -97
  68. package/src/infer-exports.d.ts +4 -4
  69. package/src/infer-exports.d.ts.map +1 -1
  70. package/src/infer-exports.js +29 -20
  71. package/src/json.d.ts.map +1 -1
  72. package/src/json.js +1 -0
  73. package/src/link.d.ts +4 -4
  74. package/src/link.d.ts.map +1 -1
  75. package/src/link.js +62 -19
  76. package/src/node-module-specifier.d.ts.map +1 -1
  77. package/src/node-module-specifier.js +5 -0
  78. package/src/node-modules.d.ts +13 -2
  79. package/src/node-modules.d.ts.map +1 -1
  80. package/src/node-modules.js +127 -40
  81. package/src/node-powers.d.ts +4 -4
  82. package/src/node-powers.d.ts.map +1 -1
  83. package/src/node-powers.js +10 -0
  84. package/src/parse-archive-cjs.d.ts +2 -2
  85. package/src/parse-archive-cjs.d.ts.map +1 -1
  86. package/src/parse-archive-cjs.js +4 -0
  87. package/src/parse-archive-mjs.d.ts +2 -2
  88. package/src/parse-archive-mjs.d.ts.map +1 -1
  89. package/src/parse-archive-mjs.js +5 -2
  90. package/src/parse-bytes.d.ts +2 -2
  91. package/src/parse-bytes.d.ts.map +1 -1
  92. package/src/parse-bytes.js +4 -0
  93. package/src/parse-cjs-shared-export-wrapper.d.ts +2 -2
  94. package/src/parse-cjs-shared-export-wrapper.d.ts.map +1 -1
  95. package/src/parse-cjs-shared-export-wrapper.js +16 -1
  96. package/src/parse-cjs.d.ts +2 -2
  97. package/src/parse-cjs.d.ts.map +1 -1
  98. package/src/parse-cjs.js +4 -0
  99. package/src/parse-json.d.ts +2 -2
  100. package/src/parse-json.d.ts.map +1 -1
  101. package/src/parse-json.js +2 -0
  102. package/src/parse-mjs.d.ts +2 -2
  103. package/src/parse-mjs.d.ts.map +1 -1
  104. package/src/parse-mjs.js +4 -2
  105. package/src/parse-pre-cjs.d.ts +2 -2
  106. package/src/parse-pre-cjs.d.ts.map +1 -1
  107. package/src/parse-pre-cjs.js +6 -0
  108. package/src/parse-pre-mjs.d.ts +2 -2
  109. package/src/parse-pre-mjs.d.ts.map +1 -1
  110. package/src/parse-pre-mjs.js +6 -0
  111. package/src/parse-text.d.ts +2 -2
  112. package/src/parse-text.d.ts.map +1 -1
  113. package/src/parse-text.js +5 -0
  114. package/src/policy-format.d.ts +8 -6
  115. package/src/policy-format.d.ts.map +1 -1
  116. package/src/policy-format.js +12 -3
  117. package/src/policy.d.ts +9 -9
  118. package/src/policy.d.ts.map +1 -1
  119. package/src/policy.js +3 -0
  120. package/src/powers.d.ts +1 -1
  121. package/src/powers.d.ts.map +1 -1
  122. package/src/powers.js +7 -0
  123. package/src/search.d.ts +4 -2
  124. package/src/search.d.ts.map +1 -1
  125. package/src/search.js +22 -16
  126. package/src/types.d.ts +121 -15
  127. package/src/types.d.ts.map +1 -1
  128. package/src/types.js +161 -19
  129. package/src/url.d.ts.map +1 -1
  130. package/src/url.js +5 -0
package/src/archive.js CHANGED
@@ -1,425 +1,91 @@
1
- // @ts-check
2
- /* eslint no-shadow: 0 */
3
-
4
- /** @import {ArchiveOptions} from './types.js' */
5
- /** @import {ArchiveWriter} from './types.js' */
6
- /** @import {CompartmentDescriptor} from './types.js' */
7
- /** @import {CompartmentMapDescriptor} from './types.js' */
8
- /** @import {ModuleDescriptor} from './types.js' */
9
- /** @import {ParserImplementation} from './types.js' */
10
- /** @import {ReadFn} from './types.js' */
11
- /** @import {CaptureSourceLocationHook} from './types.js' */
12
- /** @import {ReadPowers} from './types.js' */
13
- /** @import {HashPowers} from './types.js' */
14
- /** @import {Sources} from './types.js' */
15
- /** @import {WriteFn} from './types.js' */
16
-
17
- import { writeZip } from '@endo/zip';
18
- import { resolve } from './node-module-specifier.js';
19
- import { compartmentMapForNodeModules } from './node-modules.js';
20
- import { search } from './search.js';
21
- import { link } from './link.js';
22
- import {
23
- exitModuleImportHookMaker,
24
- makeImportHookMaker,
25
- } from './import-hook.js';
26
- import parserJson from './parse-json.js';
27
- import parserText from './parse-text.js';
28
- import parserBytes from './parse-bytes.js';
29
- import parserArchiveCjs from './parse-archive-cjs.js';
30
- import parserArchiveMjs from './parse-archive-mjs.js';
31
- import { parseLocatedJson } from './json.js';
32
- import { unpackReadPowers } from './powers.js';
33
- import {
34
- assertCompartmentMap,
35
- stringCompare,
36
- pathCompare,
37
- } from './compartment-map.js';
38
- import { detectAttenuators } from './policy.js';
39
-
40
- const textEncoder = new TextEncoder();
41
-
42
- /** @type {Record<string, ParserImplementation>} */
43
- const parserForLanguage = {
44
- mjs: parserArchiveMjs,
45
- 'pre-mjs-json': parserArchiveMjs,
46
- cjs: parserArchiveCjs,
47
- 'pre-cjs-json': parserArchiveCjs,
48
- json: parserJson,
49
- text: parserText,
50
- bytes: parserBytes,
51
- };
52
-
53
- /**
54
- * @param {string} rel - a relative URL
55
- * @param {string} abs - a fully qualified URL
56
- * @returns {string}
57
- */
58
- const resolveLocation = (rel, abs) => new URL(rel, abs).toString();
59
-
60
- const { keys, entries, fromEntries } = Object;
61
-
62
- /**
63
- * We attempt to produce compartment maps that are consistent regardless of
64
- * whether the packages were originally laid out on disk for development or
65
- * production, and other trivia like the fully qualified path of a specific
66
- * installation.
1
+ /* Provides mechanisms for creating archives (zip files with a
2
+ * `compartmeent-map.json` and a file for every static dependency of an entry
3
+ * module).
67
4
  *
68
- * Naming compartments for the self-ascribed name and version of each Node.js
69
- * package is insufficient because they are not guaranteed to be unique.
70
- * Dependencies do not necessarilly come from the npm registry and may be
71
- * for example derived from fully qualified URL's or Github org and project
72
- * names.
73
- * Package managers are also not required to fully deduplicate the hard
74
- * copy of each package even when they are identical resources.
75
- * Duplication is undesirable, but we elect to defer that problem to solutions
76
- * in the package managers, as the alternative would be to consistently hash
77
- * the original sources of the packages themselves, which may not even be
78
- * available much less pristine for us.
5
+ * Uses the conventions of Node.js package managers and the `node_modules`.
6
+ * Archives will contain a copy for every physical copy of a package on disk,
7
+ * such that importing will construct a separate instance for each.
79
8
  *
80
- * So, instead, we use the lexically least path of dependency names, delimited
81
- * by hashes.
82
- * The compartment maps generated by the ./node-modules.js tooling pre-compute
83
- * these traces for our use here.
84
- * We sort the compartments lexically on their self-ascribed name and version,
85
- * and use the lexically least dependency name path as a tie-breaker.
86
- * The dependency path is logical and orthogonal to the package manager's
87
- * actual installation location, so should be orthogonal to the vagaries of the
88
- * package manager's deduplication algorithm.
9
+ * These archives will contain pre-compiled ESM and CJS and this module
10
+ * entrains a dependency on the core of Babel.
89
11
  *
90
- * @param {Record<string, CompartmentDescriptor>} compartments
91
- * @returns {Record<string, string>} map from old to new compartment names.
92
- */
93
- const renameCompartments = compartments => {
94
- /** @type {Record<string, string>} */
95
- const compartmentRenames = Object.create(null);
96
- let index = 0;
97
- let prev = '';
98
-
99
- // The sort below combines two comparators to avoid depending on sort
100
- // stability, which became standard as recently as 2019.
101
- // If that date seems quaint, please accept my regards from the distant past.
102
- // We are very proud of you.
103
- const compartmentsByPath = Object.entries(compartments)
104
- .map(([name, compartment]) => ({
105
- name,
106
- path: compartment.path,
107
- label: compartment.label,
108
- }))
109
- .sort((a, b) => {
110
- if (a.label === b.label) {
111
- assert(a.path !== undefined && b.path !== undefined);
112
- return pathCompare(a.path, b.path);
113
- }
114
- return stringCompare(a.label, b.label);
115
- });
116
-
117
- for (const { name, label } of compartmentsByPath) {
118
- if (label === prev) {
119
- compartmentRenames[name] = `${label}-n${index}`;
120
- index += 1;
121
- } else {
122
- compartmentRenames[name] = label;
123
- prev = label;
124
- index = 1;
125
- }
126
- }
127
- return compartmentRenames;
128
- };
129
-
130
- /**
131
- * @param {Record<string, CompartmentDescriptor>} compartments
132
- * @param {Sources} sources
133
- * @param {Record<string, string>} compartmentRenames
12
+ * See `archive-lite.js` for a variation that does not depend on Babel
13
+ * for cases like XS native Compartments where pre-compilation is not
14
+ * necessary or where the dependency on Babel can be dererred to runtime.
134
15
  */
135
- const translateCompartmentMap = (compartments, sources, compartmentRenames) => {
136
- const result = Object.create(null);
137
- for (const compartmentName of keys(compartmentRenames)) {
138
- const compartment = compartments[compartmentName];
139
- const { name, label, retained, policy } = compartment;
140
- if (retained) {
141
- // rename module compartments
142
- /** @type {Record<string, ModuleDescriptor>} */
143
- const modules = Object.create(null);
144
- const compartmentModules = compartment.modules;
145
- if (compartment.modules) {
146
- for (const name of keys(compartmentModules).sort()) {
147
- const module = compartmentModules[name];
148
- if (module.compartment !== undefined) {
149
- modules[name] = {
150
- ...module,
151
- compartment: compartmentRenames[module.compartment],
152
- };
153
- } else {
154
- modules[name] = module;
155
- }
156
- }
157
- }
158
-
159
- // integrate sources into modules
160
- const compartmentSources = sources[compartmentName];
161
- if (compartmentSources) {
162
- for (const name of keys(compartmentSources).sort()) {
163
- const source = compartmentSources[name];
164
- const { location, parser, exit, sha512, deferredError } = source;
165
- if (location !== undefined) {
166
- modules[name] = {
167
- location,
168
- parser,
169
- sha512,
170
- };
171
- } else if (exit !== undefined) {
172
- modules[name] = {
173
- exit,
174
- };
175
- } else if (deferredError !== undefined) {
176
- modules[name] = {
177
- deferredError,
178
- };
179
- }
180
- }
181
- }
182
-
183
- result[compartmentRenames[compartmentName]] = {
184
- name,
185
- label,
186
- location: compartmentRenames[compartmentName],
187
- modules,
188
- policy,
189
- // `scopes`, `types`, and `parsers` are not necessary since every
190
- // loadable module is captured in `modules`.
191
- };
192
- }
193
- }
194
-
195
- return result;
196
- };
16
+ // @ts-check
197
17
 
198
- /**
199
- * @param {Sources} sources
200
- * @param {Record<string, string>} compartmentRenames
201
- * @returns {Sources}
202
- */
203
- const renameSources = (sources, compartmentRenames) => {
204
- return fromEntries(
205
- entries(sources).map(([name, compartmentSources]) => [
206
- compartmentRenames[name],
207
- compartmentSources,
208
- ]),
209
- );
210
- };
18
+ import { defaultParserForLanguage } from './archive-parsers.js';
19
+ import { mapNodeModules } from './node-modules.js';
20
+ import {
21
+ makeAndHashArchiveFromMap,
22
+ makeArchiveFromMap,
23
+ mapFromMap,
24
+ hashFromMap,
25
+ writeArchiveFromMap,
26
+ } from './archive-lite.js';
211
27
 
212
- /**
213
- * @param {ArchiveWriter} archive
214
- * @param {Sources} sources
215
- */
216
- const addSourcesToArchive = async (archive, sources) => {
217
- for (const compartment of keys(sources).sort()) {
218
- const modules = sources[compartment];
219
- const compartmentLocation = resolveLocation(`${compartment}/`, 'file:///');
220
- for (const specifier of keys(modules).sort()) {
221
- const { bytes, location } = modules[specifier];
222
- if (location !== undefined) {
223
- const moduleLocation = resolveLocation(location, compartmentLocation);
224
- const path = new URL(moduleLocation).pathname.slice(1); // elide initial "/"
225
- if (bytes !== undefined) {
226
- // eslint-disable-next-line no-await-in-loop
227
- await archive.write(path, bytes);
228
- }
229
- }
230
- }
231
- }
232
- };
28
+ const { assign, create, freeze } = Object;
233
29
 
234
- /**
235
- * @param {Sources} sources
236
- * @param {CaptureSourceLocationHook} captureSourceLocation
237
- */
238
- const captureSourceLocations = async (sources, captureSourceLocation) => {
239
- for (const compartmentName of keys(sources).sort()) {
240
- const modules = sources[compartmentName];
241
- for (const moduleSpecifier of keys(modules).sort()) {
242
- const { sourceLocation } = modules[moduleSpecifier];
243
- if (sourceLocation !== undefined) {
244
- captureSourceLocation(compartmentName, moduleSpecifier, sourceLocation);
245
- }
246
- }
247
- }
248
- };
30
+ /** @import {ArchiveOptions} from './types.js' */
31
+ /** @import {ReadFn} from './types.js' */
32
+ /** @import {ReadPowers} from './types.js' */
33
+ /** @import {HashPowers} from './types.js' */
34
+ /** @import {WriteFn} from './types.js' */
249
35
 
250
36
  /**
251
- * @param {CompartmentMapDescriptor} compartmentMap
252
- * @param {Sources} sources
253
- * @returns {{archiveCompartmentMap: CompartmentMapDescriptor, archiveSources: Sources}}
37
+ * Add the default parserForLanguage option.
38
+ * @param {ArchiveOptions} [options]
39
+ * @returns {ArchiveOptions}
254
40
  */
255
- export const makeArchiveCompartmentMap = (compartmentMap, sources) => {
256
- const {
257
- compartments,
258
- entry: { compartment: entryCompartmentName, module: entryModuleSpecifier },
259
- } = compartmentMap;
260
-
261
- const compartmentRenames = renameCompartments(compartments);
262
- const archiveCompartments = translateCompartmentMap(
263
- compartments,
264
- sources,
265
- compartmentRenames,
41
+ const assignParserForLanguage = (options = {}) => {
42
+ const { parserForLanguage: parserForLanguageOption, ...rest } = options;
43
+ const parserForLanguage = freeze(
44
+ assign(create(null), defaultParserForLanguage, parserForLanguageOption),
266
45
  );
267
- const archiveEntryCompartmentName = compartmentRenames[entryCompartmentName];
268
- const archiveSources = renameSources(sources, compartmentRenames);
269
-
270
- const archiveCompartmentMap = {
271
- tags: [],
272
- entry: {
273
- compartment: archiveEntryCompartmentName,
274
- module: entryModuleSpecifier,
275
- },
276
- compartments: archiveCompartments,
277
- };
278
-
279
- // Cross-check:
280
- // We assert that we have constructed a valid compartment map, not because it
281
- // might not be, but to ensure that the assertCompartmentMap function can
282
- // accept all valid compartment maps.
283
- assertCompartmentMap(archiveCompartmentMap);
284
-
285
- return { archiveCompartmentMap, archiveSources };
46
+ return { ...rest, parserForLanguage };
286
47
  };
287
48
 
288
49
  /**
289
50
  * @param {ReadFn | ReadPowers} powers
290
51
  * @param {string} moduleLocation
291
52
  * @param {ArchiveOptions} [options]
292
- * @returns {Promise<{sources: Sources, compartmentMapBytes: Uint8Array, sha512?: string}>}
53
+ * @returns {Promise<{bytes: Uint8Array, sha512?: string}>}
293
54
  */
294
- const digestLocation = async (powers, moduleLocation, options) => {
295
- const {
296
- moduleTransforms,
297
- modules: exitModules = {},
298
- dev = false,
299
- tags = new Set(),
300
- captureSourceLocation = undefined,
301
- searchSuffixes = undefined,
302
- commonDependencies = undefined,
303
- importHook: exitModuleImportHook = undefined,
304
- policy = undefined,
305
- sourceMapHook = undefined,
306
- } = options || {};
307
- const { read, computeSha512 } = unpackReadPowers(powers);
308
- const {
309
- packageLocation,
310
- packageDescriptorText,
311
- packageDescriptorLocation,
312
- moduleSpecifier,
313
- } = await search(read, moduleLocation);
314
-
315
- tags.add('endo');
316
- tags.add('import');
317
- tags.add('default');
318
-
319
- const packageDescriptor = parseLocatedJson(
320
- packageDescriptorText,
321
- packageDescriptorLocation,
322
- );
323
- const compartmentMap = await compartmentMapForNodeModules(
55
+ export const makeAndHashArchive = async (
56
+ powers,
57
+ moduleLocation,
58
+ options = {},
59
+ ) => {
60
+ const compartmentMap = await mapNodeModules(powers, moduleLocation, options);
61
+ return makeAndHashArchiveFromMap(
324
62
  powers,
325
- packageLocation,
326
- tags,
327
- packageDescriptor,
328
- moduleSpecifier,
329
- { dev, commonDependencies, policy },
330
- );
331
-
332
- const {
333
- compartments,
334
- entry: { module: entryModuleSpecifier, compartment: entryCompartmentName },
335
- } = compartmentMap;
336
-
337
- /** @type {Sources} */
338
- const sources = Object.create(null);
339
-
340
- const consolidatedExitModuleImportHook = exitModuleImportHookMaker({
341
- modules: exitModules,
342
- exitModuleImportHook,
343
- });
344
-
345
- const makeImportHook = makeImportHookMaker(read, packageLocation, {
346
- sources,
347
- compartmentDescriptors: compartments,
348
- archiveOnly: true,
349
- computeSha512,
350
- searchSuffixes,
351
- entryCompartmentName,
352
- entryModuleSpecifier,
353
- exitModuleImportHook: consolidatedExitModuleImportHook,
354
- sourceMapHook,
355
- });
356
- // Induce importHook to record all the necessary modules to import the given module specifier.
357
- const { compartment, attenuatorsCompartment } = link(compartmentMap, {
358
- resolve,
359
- makeImportHook,
360
- moduleTransforms,
361
- parserForLanguage,
362
- archiveOnly: true,
363
- });
364
- await compartment.load(entryModuleSpecifier);
365
- if (policy) {
366
- // retain all attenuators.
367
- await Promise.all(
368
- detectAttenuators(policy).map(attenuatorSpecifier =>
369
- attenuatorsCompartment.load(attenuatorSpecifier),
370
- ),
371
- );
372
- }
373
-
374
- const { archiveCompartmentMap, archiveSources } = makeArchiveCompartmentMap(
375
63
  compartmentMap,
376
- sources,
64
+ assignParserForLanguage(options),
377
65
  );
378
-
379
- const archiveCompartmentMapText = JSON.stringify(
380
- archiveCompartmentMap,
381
- null,
382
- 2,
383
- );
384
- const archiveCompartmentMapBytes = textEncoder.encode(
385
- archiveCompartmentMapText,
386
- );
387
-
388
- if (captureSourceLocation !== undefined) {
389
- captureSourceLocations(archiveSources, captureSourceLocation);
390
- }
391
-
392
- let archiveSha512;
393
- if (computeSha512 !== undefined) {
394
- archiveSha512 = computeSha512(archiveCompartmentMapBytes);
395
- }
396
-
397
- return {
398
- compartmentMapBytes: archiveCompartmentMapBytes,
399
- sources: archiveSources,
400
- sha512: archiveSha512,
401
- };
402
66
  };
403
67
 
404
68
  /**
405
69
  * @param {ReadFn | ReadPowers} powers
406
70
  * @param {string} moduleLocation
407
71
  * @param {ArchiveOptions} [options]
408
- * @returns {Promise<{bytes: Uint8Array, sha512?: string}>}
72
+ * @returns {Promise<Uint8Array>}
409
73
  */
410
- export const makeAndHashArchive = async (powers, moduleLocation, options) => {
411
- const { compartmentMapBytes, sources, sha512 } = await digestLocation(
74
+ export const makeArchive = async (powers, moduleLocation, options = {}) => {
75
+ const { dev, tags, conditions = tags, commonDependencies, policy } = options;
76
+
77
+ const compartmentMap = await mapNodeModules(powers, moduleLocation, {
78
+ dev,
79
+ conditions,
80
+ commonDependencies,
81
+ policy,
82
+ });
83
+
84
+ return makeArchiveFromMap(
412
85
  powers,
413
- moduleLocation,
414
- options,
86
+ compartmentMap,
87
+ assignParserForLanguage(options),
415
88
  );
416
-
417
- const archive = writeZip();
418
- await archive.write('compartment-map.json', compartmentMapBytes);
419
- await addSourcesToArchive(archive, sources);
420
- const bytes = await archive.snapshot();
421
-
422
- return { bytes, sha512 };
423
89
  };
424
90
 
425
91
  /**
@@ -428,24 +94,17 @@ export const makeAndHashArchive = async (powers, moduleLocation, options) => {
428
94
  * @param {ArchiveOptions} [options]
429
95
  * @returns {Promise<Uint8Array>}
430
96
  */
431
- export const makeArchive = async (powers, moduleLocation, options) => {
432
- const { bytes } = await makeAndHashArchive(powers, moduleLocation, options);
433
- return bytes;
434
- };
97
+ export const mapLocation = async (powers, moduleLocation, options = {}) => {
98
+ const { dev, tags, conditions = tags, commonDependencies, policy } = options;
99
+
100
+ const compartmentMap = await mapNodeModules(powers, moduleLocation, {
101
+ dev,
102
+ conditions,
103
+ commonDependencies,
104
+ policy,
105
+ });
435
106
 
436
- /**
437
- * @param {ReadFn | ReadPowers} powers
438
- * @param {string} moduleLocation
439
- * @param {ArchiveOptions} [options]
440
- * @returns {Promise<Uint8Array>}
441
- */
442
- export const mapLocation = async (powers, moduleLocation, options) => {
443
- const { compartmentMapBytes } = await digestLocation(
444
- powers,
445
- moduleLocation,
446
- options,
447
- );
448
- return compartmentMapBytes;
107
+ return mapFromMap(powers, compartmentMap, assignParserForLanguage(options));
449
108
  };
450
109
 
451
110
  /**
@@ -454,14 +113,17 @@ export const mapLocation = async (powers, moduleLocation, options) => {
454
113
  * @param {ArchiveOptions} [options]
455
114
  * @returns {Promise<string>}
456
115
  */
457
- export const hashLocation = async (powers, moduleLocation, options) => {
458
- const { compartmentMapBytes } = await digestLocation(
459
- powers,
460
- moduleLocation,
461
- options,
462
- );
463
- const { computeSha512 } = powers;
464
- return computeSha512(compartmentMapBytes);
116
+ export const hashLocation = async (powers, moduleLocation, options = {}) => {
117
+ const { dev, tags, conditions = tags, commonDependencies, policy } = options;
118
+
119
+ const compartmentMap = await mapNodeModules(powers, moduleLocation, {
120
+ dev,
121
+ conditions,
122
+ commonDependencies,
123
+ policy,
124
+ });
125
+
126
+ return hashFromMap(powers, compartmentMap, assignParserForLanguage(options));
465
127
  };
466
128
 
467
129
  /**
@@ -476,8 +138,20 @@ export const writeArchive = async (
476
138
  readPowers,
477
139
  archiveLocation,
478
140
  moduleLocation,
479
- options,
141
+ options = {},
480
142
  ) => {
481
- const archiveBytes = await makeArchive(readPowers, moduleLocation, options);
482
- await write(archiveLocation, archiveBytes);
143
+ const { dev, tags, conditions = tags, commonDependencies, policy } = options;
144
+ const compartmentMap = await mapNodeModules(readPowers, moduleLocation, {
145
+ dev,
146
+ conditions,
147
+ commonDependencies,
148
+ policy,
149
+ });
150
+ return writeArchiveFromMap(
151
+ write,
152
+ readPowers,
153
+ archiveLocation,
154
+ compartmentMap,
155
+ assignParserForLanguage(options),
156
+ );
483
157
  };
@@ -1,19 +1,8 @@
1
- declare namespace _default {
2
- export { runtime };
3
- export function getBundlerKit({ index, indexedImports, record: { cjsFunctor, exports: exportsList }, }: {
4
- index: any;
5
- indexedImports: any;
6
- record: {
7
- cjsFunctor: any;
8
- exports?: {} | undefined;
9
- };
10
- }): {
11
- getFunctor: () => string;
12
- getCells: () => string;
13
- getReexportsWiring: () => string;
14
- getFunctorCall: () => string;
15
- };
16
- }
1
+ declare const _default: BundlerSupport<CjsModuleSource>;
17
2
  export default _default;
18
- declare const runtime: string;
3
+ export type CjsModuleSource = VirtualModuleSource & {
4
+ cjsFunctor: string;
5
+ };
6
+ import type { BundlerSupport } from './bundle.js';
7
+ import type { VirtualModuleSource } from 'ses';
19
8
  //# sourceMappingURL=bundle-cjs.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bundle-cjs.d.ts","sourceRoot":"","sources":["bundle-cjs.js"],"names":[],"mappings":";;IAuDE;;;;;;;;;;;;MAsBC;;;AA7DH,8BAmCa"}
1
+ {"version":3,"file":"bundle-cjs.d.ts","sourceRoot":"","sources":["bundle-cjs.js"],"names":[],"mappings":"wBA4DW,eAAe,eAAe,CAAC;;8BAvD5B,mBAAmB,GAAG;IAAC,UAAU,EAAE,MAAM,CAAA;CAAC;oCAFtB,aAAa;yCADR,KAAK"}
package/src/bundle-cjs.js CHANGED
@@ -1,4 +1,10 @@
1
- // @ts-nocheck
1
+ /* Provides CommonJS support for `bundle.js`. */
2
+
3
+ /** @import {VirtualModuleSource} from 'ses' */
4
+ /** @import {BundlerSupport} from './bundle.js' */
5
+
6
+ /** @typedef {VirtualModuleSource & {cjsFunctor: string}} CjsModuleSource */
7
+
2
8
  /** quotes strings */
3
9
  const q = JSON.stringify;
4
10
 
@@ -14,7 +20,8 @@ const exportsCellRecord = exportsList =>
14
20
  );
15
21
 
16
22
  // This function is serialized and references variables from its destination scope.
17
- const runtime = function wrapCjsFunctor(num) {
23
+ const runtime = `\
24
+ function wrapCjsFunctor(num) {
18
25
  /* eslint-disable no-undef */
19
26
  return ({ imports = {} }) => {
20
27
  const moduleCells = cells[num];
@@ -49,8 +56,9 @@ const runtime = function wrapCjsFunctor(num) {
49
56
  moduleCells['*'].set(Object.freeze(starExports));
50
57
  };
51
58
  /* eslint-enable no-undef */
52
- }.toString();
59
+ }`;
53
60
 
61
+ /** @type {BundlerSupport<CjsModuleSource>} */
54
62
  export default {
55
63
  runtime,
56
64
  getBundlerKit({
@@ -0,0 +1,15 @@
1
+ declare namespace _default {
2
+ let runtime: string;
3
+ function getBundlerKit({ index, indexedImports, bytes }: {
4
+ index: any;
5
+ indexedImports: any;
6
+ bytes: any;
7
+ }): {
8
+ getFunctor: () => string;
9
+ getCells: () => string;
10
+ getReexportsWiring: () => string;
11
+ getFunctorCall: () => string;
12
+ };
13
+ }
14
+ export default _default;
15
+ //# sourceMappingURL=bundle-json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bundle-json.d.ts","sourceRoot":"","sources":["bundle-json.js"],"names":[],"mappings":";;IAME;;;;;;;;;MAgBC"}
@@ -0,0 +1,24 @@
1
+ /* Provides JSON support for `bundle.js`. */
2
+
3
+ const textDecoder = new TextDecoder();
4
+
5
+ export default {
6
+ runtime: '',
7
+ getBundlerKit({ index, indexedImports, bytes }) {
8
+ // Round-trip to revalidate JSON and squeeze out space.
9
+ const json = JSON.stringify(JSON.parse(textDecoder.decode(bytes)));
10
+ return {
11
+ getFunctor: () => `\
12
+ // === functors[${index}] ===
13
+ (set) => set(${json}),
14
+ `,
15
+ getCells: () => `\
16
+ { default: cell('default') },
17
+ `,
18
+ getReexportsWiring: () => '',
19
+ getFunctorCall: () => `\
20
+ functors[${index}](cells[${index}].default.set);
21
+ `,
22
+ };
23
+ },
24
+ };