@endo/compartment-mapper 1.2.2 → 1.3.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 (67) hide show
  1. package/archive-lite.d.ts.map +1 -0
  2. package/archive-parsers.d.ts.map +1 -0
  3. package/archive.d.ts.map +1 -0
  4. package/bundle.d.ts.map +1 -0
  5. package/capture-lite.d.ts.map +1 -0
  6. package/import-archive-lite.d.ts.map +1 -0
  7. package/import-archive-parsers.d.ts.map +1 -0
  8. package/import-archive.d.ts.map +1 -0
  9. package/import-lite.d.ts.map +1 -0
  10. package/import-parsers.d.ts.map +1 -0
  11. package/import.d.ts.map +1 -0
  12. package/index.d.ts.map +1 -0
  13. package/node-modules.d.ts.map +1 -0
  14. package/node-powers.d.ts +1 -1
  15. package/node-powers.d.ts.map +1 -0
  16. package/node-powers.js +5 -1
  17. package/package.json +15 -11
  18. package/src/compartment-map.d.ts +1 -1
  19. package/src/compartment-map.d.ts.map +1 -1
  20. package/src/compartment-map.js +1 -3
  21. package/src/import-hook.d.ts +14 -1
  22. package/src/import-hook.d.ts.map +1 -1
  23. package/src/import-hook.js +493 -144
  24. package/src/import-lite.d.ts +20 -3
  25. package/src/import-lite.d.ts.map +1 -1
  26. package/src/import-lite.js +137 -15
  27. package/src/import.d.ts +45 -5
  28. package/src/import.d.ts.map +1 -1
  29. package/src/import.js +52 -6
  30. package/src/link.d.ts +2 -11
  31. package/src/link.d.ts.map +1 -1
  32. package/src/link.js +76 -154
  33. package/src/map-parser.d.ts +4 -0
  34. package/src/map-parser.d.ts.map +1 -0
  35. package/src/map-parser.js +339 -0
  36. package/src/node-modules.d.ts +2 -5
  37. package/src/node-modules.d.ts.map +1 -1
  38. package/src/node-modules.js +12 -5
  39. package/src/node-powers.d.ts +29 -23
  40. package/src/node-powers.d.ts.map +1 -1
  41. package/src/node-powers.js +102 -25
  42. package/src/parse-archive-cjs.js +2 -1
  43. package/src/parse-archive-mjs.js +2 -1
  44. package/src/parse-bytes.d.ts.map +1 -1
  45. package/src/parse-bytes.js +2 -6
  46. package/src/parse-cjs-shared-export-wrapper.d.ts.map +1 -1
  47. package/src/parse-cjs-shared-export-wrapper.js +23 -6
  48. package/src/parse-cjs.js +3 -2
  49. package/src/parse-json.d.ts +5 -3
  50. package/src/parse-json.d.ts.map +1 -1
  51. package/src/parse-json.js +9 -9
  52. package/src/parse-mjs.js +2 -1
  53. package/src/parse-pre-cjs.js +3 -2
  54. package/src/parse-pre-mjs.js +2 -1
  55. package/src/parse-text.d.ts.map +1 -1
  56. package/src/parse-text.js +2 -6
  57. package/src/policy.d.ts +21 -14
  58. package/src/policy.d.ts.map +1 -1
  59. package/src/policy.js +53 -43
  60. package/src/powers.d.ts +8 -1
  61. package/src/powers.d.ts.map +1 -1
  62. package/src/powers.js +60 -10
  63. package/src/search.d.ts.map +1 -1
  64. package/src/search.js +1 -2
  65. package/src/types.d.ts +343 -21
  66. package/src/types.d.ts.map +1 -1
  67. package/src/types.js +369 -22
@@ -12,6 +12,8 @@
12
12
  /* eslint no-shadow: 0 */
13
13
 
14
14
  /** @import {CanonicalFn} from './types.js' */
15
+ /** @import {CompartmentMapForNodeModulesOptions} from './types.js' */
16
+ /** @import {SomePolicy} from './types.js' */
15
17
  /** @import {CompartmentDescriptor} from './types.js' */
16
18
  /** @import {CompartmentMapDescriptor} from './types.js' */
17
19
  /** @import {Language} from './types.js' */
@@ -605,7 +607,7 @@ const graphPackages = async (
605
607
  * @param {Graph} graph
606
608
  * @param {Set<string>} conditions - build conditions about the target environment
607
609
  * for selecting relevant exports, e.g., "browser" or "node".
608
- * @param {import('./types.js').Policy} [policy]
610
+ * @param {SomePolicy} [policy]
609
611
  * @returns {CompartmentMapDescriptor}
610
612
  */
611
613
  const translateGraph = (
@@ -642,6 +644,12 @@ const translateGraph = (
642
644
  /** @type {Record<string, ScopeDescriptor>} */
643
645
  const scopes = Object.create(null);
644
646
 
647
+ /**
648
+ * List of all the compartments (by name) that this compartment can import from.
649
+ *
650
+ * @type {Set<string>}
651
+ */
652
+ const compartmentNames = new Set();
645
653
  const packagePolicy = getPolicyForPackage(
646
654
  {
647
655
  isEntry: dependeeLocation === entryPackageLocation,
@@ -699,6 +707,7 @@ const translateGraph = (
699
707
  for (const dependencyName of keys(dependencyLocations).sort()) {
700
708
  const dependencyLocation = dependencyLocations[dependencyName];
701
709
  digestExternalAliases(dependencyName, dependencyLocation);
710
+ compartmentNames.add(dependencyLocation);
702
711
  }
703
712
  // digest own internal aliases
704
713
  for (const modulePath of keys(internalAliases).sort()) {
@@ -724,6 +733,7 @@ const translateGraph = (
724
733
  parsers,
725
734
  types,
726
735
  policy: /** @type {SomePackagePolicy} */ (packagePolicy),
736
+ compartments: compartmentNames,
727
737
  };
728
738
  }
729
739
 
@@ -745,10 +755,7 @@ const translateGraph = (
745
755
  * @param {Set<string>} conditions
746
756
  * @param {object} packageDescriptor
747
757
  * @param {string} moduleSpecifier
748
- * @param {object} [options]
749
- * @param {boolean} [options.dev]
750
- * @param {object} [options.commonDependencies]
751
- * @param {object} [options.policy]
758
+ * @param {CompartmentMapForNodeModulesOptions} [options]
752
759
  * @returns {Promise<CompartmentMapDescriptor>}
753
760
  */
754
761
  export const compartmentMapForNodeModules = async (
@@ -1,44 +1,50 @@
1
+ export function makeReadNowPowers({ fs, url, crypto, path, }: {
2
+ fs: FsInterface;
3
+ url?: UrlInterface | undefined;
4
+ crypto?: CryptoInterface | undefined;
5
+ path?: PathInterface | undefined;
6
+ }): MaybeReadPowers & ReadNowPowers;
1
7
  /**
2
8
  * The implementation of `makeReadPowers` and the deprecated
3
9
  * `makeNodeReadPowers` handles the case when the `url` power is not provided,
4
10
  * but `makeReadPowers` presents a type that requires `url`.
5
11
  *
6
12
  * @param {object} args
7
- * @param {typeof import('fs')} args.fs
8
- * @param {typeof import('url')} [args.url]
9
- * @param {typeof import('crypto')} [args.crypto]
13
+ * @param {FsInterface} args.fs
14
+ * @param {UrlInterface} [args.url]
15
+ * @param {CryptoInterface} [args.crypto]
16
+ * @param {PathInterface} [args.path]
17
+ * @returns {MaybeReadPowers}
10
18
  */
11
- export function makeReadPowers({ fs, url, crypto }: {
12
- fs: typeof import("fs");
13
- url?: typeof import("url") | undefined;
14
- crypto?: typeof import("crypto") | undefined;
15
- }): {
16
- read: (location: string) => Promise<Buffer>;
17
- maybeRead: (location: string) => Promise<Buffer | undefined>;
18
- fileURLToPath: typeof import("url").fileURLToPath;
19
- pathToFileURL: ((path: string) => string) | typeof import("url").pathToFileURL;
20
- canonical: (location: string) => Promise<string>;
21
- computeSha512: HashFn | undefined;
22
- requireResolve: (from: any, specifier: any, options: any) => string;
23
- };
19
+ export function makeReadPowers({ fs, url, crypto, path, }: {
20
+ fs: FsInterface;
21
+ url?: UrlInterface | undefined;
22
+ crypto?: CryptoInterface | undefined;
23
+ path?: PathInterface | undefined;
24
+ }): MaybeReadPowers;
24
25
  /**
25
26
  * The implementation of `makeWritePowers` and the deprecated
26
27
  * `makeNodeWritePowers` handles the case when the `url` power is not provided,
27
28
  * but `makeWritePowers` presents a type that requires `url`.
28
29
  *
29
30
  * @param {object} args
30
- * @param {typeof import('fs')} args.fs
31
- * @param {typeof import('url')} [args.url]
31
+ * @param {FsInterface} args.fs
32
+ * @param {UrlInterface} [args.url]
32
33
  */
33
34
  export function makeWritePowers({ fs, url }: {
34
- fs: typeof import("fs");
35
- url?: typeof import("url") | undefined;
35
+ fs: FsInterface;
36
+ url?: UrlInterface | undefined;
36
37
  }): {
37
38
  write: (location: string, data: Uint8Array) => Promise<void>;
38
39
  };
39
- export function makeNodeReadPowers(fs: typeof import("fs"), crypto?: typeof import("crypto") | undefined): ReadPowers;
40
- export function makeNodeWritePowers(fs: typeof import("fs")): WritePowers;
41
- import type { HashFn } from './types.js';
40
+ export function makeNodeReadPowers(fs: FsInterface, crypto?: CryptoInterface | undefined): ReadPowers;
41
+ export function makeNodeWritePowers(fs: FsInterface): WritePowers;
42
+ import type { FsInterface } from './types.js';
43
+ import type { UrlInterface } from './types.js';
44
+ import type { CryptoInterface } from './types.js';
45
+ import type { PathInterface } from './types.js';
46
+ import type { MaybeReadPowers } from './types.js';
47
+ import type { ReadNowPowers } from './types.js';
42
48
  import type { ReadPowers } from './types.js';
43
49
  import type { WritePowers } from './types.js';
44
50
  //# sourceMappingURL=node-powers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"node-powers.d.ts","sourceRoot":"","sources":["node-powers.js"],"names":[],"mappings":"AAkCA;;;;;;;;;GASG;AACH,oDAJG;IAAkC,EAAE,EAA5B,cAAc,IAAI,CAAC;IACS,GAAG;IACA,MAAM;CAAC;qBAWpC,MAAM;0BAqBN,MAAM;;2BA9CR,MAAM;0BA2EJ,MAAM;;;EAqClB;AAED;;;;;;;;GAQG;AACH,6CAHG;IAAkC,EAAE,EAA5B,cAAc,IAAI,CAAC;IACS,GAAG;CAAC;sBAO9B,MAAM,QACN,UAAU;EAYtB;AA0BM,uCAJI,cAAc,IAAI,CAAC,iDAEjB,UAAU,CAItB;AAUM,wCAHI,cAAc,IAAI,CAAC,GACjB,WAAW,CAIvB;4BAtMyB,YAAY;gCADR,YAAY;iCAEX,YAAY"}
1
+ {"version":3,"file":"node-powers.d.ts","sourceRoot":"","sources":["node-powers.js"],"names":[],"mappings":"AAqLO,8DANJ;IAA0B,EAAE,EAApB,WAAW;IACS,GAAG;IACA,MAAM;IACR,IAAI;CACjC,GAAU,eAAe,GAAG,aAAa,CAoC3C;AAlKD;;;;;;;;;;;GAWG;AACH,2DANG;IAA0B,EAAE,EAApB,WAAW;IACS,GAAG;IACA,MAAM;IACR,IAAI;CACjC,GAAU,eAAe,CA0G3B;AAgDD;;;;;;;;GAQG;AACH,6CAHG;IAA0B,EAAE,EAApB,WAAW;IACS,GAAG;CAAC;sBAOtB,MAAM,QACN,UAAU;EAYtB;AA2BM,uCALI,WAAW,yCAET,UAAU,CAKtB;AAWM,wCAJI,WAAW,GACT,WAAW,CAKvB;iCAjR8B,YAAY;kCAWX,YAAY;qCAbT,YAAY;mCAOd,YAAY;qCADV,YAAY;mCAMd,YAAY;gCAFf,YAAY;iCAIX,YAAY"}
@@ -8,14 +8,28 @@
8
8
 
9
9
  // @ts-check
10
10
 
11
- /** @import {ReadPowers} from './types.js' */
11
+ /** @import {CanonicalFn} from './types.js' */
12
+ /** @import {CryptoInterface} from './types.js' */
13
+ /** @import {FileURLToPathFn} from './types.js' */
14
+ /** @import {FsInterface} from './types.js' */
12
15
  /** @import {HashFn} from './types.js' */
16
+ /** @import {IsAbsoluteFn} from './types.js' */
17
+ /** @import {MaybeReadFn} from './types.js' */
18
+ /** @import {MaybeReadPowers} from './types.js' */
19
+ /** @import {PathInterface} from './types.js' */
20
+ /** @import {PathToFileURLFn} from './types.js' */
21
+ /** @import {ReadFn} from './types.js' */
22
+ /** @import {ReadPowers} from './types.js' */
23
+ /** @import {RequireResolveFn} from './types.js' */
24
+ /** @import {ReadNowPowers} from './types.js' */
25
+ /** @import {UrlInterface} from './types.js' */
13
26
  /** @import {WritePowers} from './types.js' */
27
+ /** @import {MaybeReadNowFn} from './types.js' */
14
28
 
15
29
  import { createRequire } from 'module';
16
30
 
17
31
  /**
18
- * @param {string} location
32
+ * @type {FileURLToPathFn}
19
33
  */
20
34
  const fakeFileURLToPath = location => {
21
35
  const url = new URL(location);
@@ -26,32 +40,45 @@ const fakeFileURLToPath = location => {
26
40
  };
27
41
 
28
42
  /**
29
- * @param {string} path
43
+ * @type {PathToFileURLFn} path
30
44
  */
31
45
  const fakePathToFileURL = path => {
32
46
  return new URL(path, 'file://').toString();
33
47
  };
34
48
 
49
+ /**
50
+ * @type {IsAbsoluteFn}
51
+ */
52
+ const fakeIsAbsolute = () => false;
53
+
35
54
  /**
36
55
  * The implementation of `makeReadPowers` and the deprecated
37
56
  * `makeNodeReadPowers` handles the case when the `url` power is not provided,
38
57
  * but `makeReadPowers` presents a type that requires `url`.
39
58
  *
40
59
  * @param {object} args
41
- * @param {typeof import('fs')} args.fs
42
- * @param {typeof import('url')} [args.url]
43
- * @param {typeof import('crypto')} [args.crypto]
60
+ * @param {FsInterface} args.fs
61
+ * @param {UrlInterface} [args.url]
62
+ * @param {CryptoInterface} [args.crypto]
63
+ * @param {PathInterface} [args.path]
64
+ * @returns {MaybeReadPowers}
44
65
  */
45
- const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
66
+ const makeReadPowersSloppy = ({
67
+ fs,
68
+ url = undefined,
69
+ crypto = undefined,
70
+ path = undefined,
71
+ }) => {
46
72
  const fileURLToPath =
47
73
  url === undefined ? fakeFileURLToPath : url.fileURLToPath;
48
74
  const pathToFileURL =
49
75
  url === undefined ? fakePathToFileURL : url.pathToFileURL;
76
+ const isAbsolute = path === undefined ? fakeIsAbsolute : path.isAbsolute;
50
77
 
51
78
  let readMutex = Promise.resolve(undefined);
52
79
 
53
80
  /**
54
- * @param {string} location
81
+ * @type {ReadFn}
55
82
  */
56
83
  const read = async location => {
57
84
  const promise = readMutex;
@@ -61,18 +88,18 @@ const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
61
88
  });
62
89
  await promise;
63
90
 
64
- const path = fileURLToPath(location);
91
+ const filepath = fileURLToPath(location);
65
92
  try {
66
93
  // We await here to ensure that we release the mutex only after
67
94
  // completing the read.
68
- return await fs.promises.readFile(path);
95
+ return await fs.promises.readFile(filepath);
69
96
  } finally {
70
97
  release(undefined);
71
98
  }
72
99
  };
73
100
 
74
101
  /**
75
- * @param {string} location
102
+ * @type {MaybeReadFn}
76
103
  */
77
104
  const maybeRead = location =>
78
105
  read(location).catch(error => {
@@ -85,6 +112,7 @@ const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
85
112
  throw error;
86
113
  });
87
114
 
115
+ /** @type {RequireResolveFn} */
88
116
  const requireResolve = (from, specifier, options) =>
89
117
  createRequire(from).resolve(specifier, options);
90
118
 
@@ -101,7 +129,7 @@ const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
101
129
  * non-existent directory on the next step after canonicalizing the package
102
130
  * location.
103
131
  *
104
- * @param {string} location
132
+ * @type {CanonicalFn}
105
133
  */
106
134
  const canonical = async location => {
107
135
  await null;
@@ -120,7 +148,7 @@ const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
120
148
  }
121
149
  };
122
150
 
123
- /** @type {HashFn=} */
151
+ /** @type {HashFn | undefined} */
124
152
  const computeSha512 = crypto
125
153
  ? bytes => {
126
154
  const hash = crypto.createHash('sha512');
@@ -137,6 +165,53 @@ const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
137
165
  canonical,
138
166
  computeSha512,
139
167
  requireResolve,
168
+ isAbsolute,
169
+ };
170
+ };
171
+
172
+ /**
173
+ * Creates {@link ReadPowers} for dynamic module support
174
+ *
175
+ * @param {object} args
176
+ * @param {FsInterface} args.fs
177
+ * @param {UrlInterface} [args.url]
178
+ * @param {CryptoInterface} [args.crypto]
179
+ * @param {PathInterface} [args.path]
180
+ * @returns {MaybeReadPowers & ReadNowPowers}
181
+ */
182
+ export const makeReadNowPowers = ({
183
+ fs,
184
+ url = undefined,
185
+ crypto = undefined,
186
+ path = undefined,
187
+ }) => {
188
+ const powers = makeReadPowersSloppy({ fs, url, crypto, path });
189
+ const fileURLToPath = powers.fileURLToPath || fakeFileURLToPath;
190
+ const isAbsolute = powers.isAbsolute || fakeIsAbsolute;
191
+
192
+ /**
193
+ * @type {MaybeReadNowFn}
194
+ */
195
+ const maybeReadNow = location => {
196
+ const filePath = fileURLToPath(location);
197
+ try {
198
+ return fs.readFileSync(filePath);
199
+ } catch (error) {
200
+ if (
201
+ 'code' in error &&
202
+ (error.code === 'ENOENT' || error.code === 'EISDIR')
203
+ ) {
204
+ return undefined;
205
+ }
206
+ throw error;
207
+ }
208
+ };
209
+
210
+ return {
211
+ ...powers,
212
+ maybeReadNow,
213
+ fileURLToPath,
214
+ isAbsolute,
140
215
  };
141
216
  };
142
217
 
@@ -146,8 +221,8 @@ const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
146
221
  * but `makeWritePowers` presents a type that requires `url`.
147
222
  *
148
223
  * @param {object} args
149
- * @param {typeof import('fs')} args.fs
150
- * @param {typeof import('url')} [args.url]
224
+ * @param {FsInterface} args.fs
225
+ * @param {UrlInterface} [args.url]
151
226
  */
152
227
  const makeWritePowersSloppy = ({ fs, url = undefined }) => {
153
228
  const fileURLToPath =
@@ -171,39 +246,41 @@ const makeWritePowersSloppy = ({ fs, url = undefined }) => {
171
246
 
172
247
  /**
173
248
  * @param {object} args
174
- * @param {typeof import('fs')} args.fs
175
- * @param {typeof import('url')} args.url
176
- * @param {typeof import('crypto')} [args.crypto]
249
+ * @param {FsInterface} args.fs
250
+ * @param {UrlInterface} args.url
251
+ * @param {CryptoInterface} [args.crypto]
177
252
  */
178
253
  export const makeReadPowers = makeReadPowersSloppy;
179
254
 
180
255
  /**
181
256
  * @param {object} args
182
- * @param {typeof import('fs')} args.fs
183
- * @param {typeof import('url')} args.url
257
+ * @param {FsInterface} args.fs
258
+ * @param {UrlInterface} args.url
184
259
  */
185
260
  export const makeWritePowers = makeWritePowersSloppy;
186
261
 
187
262
  /**
188
- * @deprecated in favor of makeReadPowers.
263
+ * Deprecated in favor of {@link makeReadPowers}.
189
264
  * It transpires that positional arguments needed to become an arguments bag to
190
265
  * reasonably expand to multiple optional dependencies.
191
266
  *
192
- * @param {typeof import('fs')} fs
193
- * @param {typeof import('crypto')} [crypto]
267
+ * @param {FsInterface} fs
268
+ * @param {CryptoInterface} [crypto]
194
269
  * @returns {ReadPowers}
270
+ * @deprecated
195
271
  */
196
272
  export const makeNodeReadPowers = (fs, crypto = undefined) => {
197
273
  return makeReadPowersSloppy({ fs, crypto });
198
274
  };
199
275
 
200
276
  /**
201
- * @deprecated in favor of makeWritePowers.
277
+ * Deprecated in favor of {@link makeWritePowers}.
202
278
  * It transpires that positional arguments needed to become an arguments bag to
203
279
  * reasonably expand to multiple optional dependencies.
204
280
  *
205
- * @param {typeof import('fs')} fs
281
+ * @param {FsInterface} fs
206
282
  * @returns {WritePowers}
283
+ * @deprecated
207
284
  */
208
285
  export const makeNodeWritePowers = fs => {
209
286
  return makeWritePowersSloppy({ fs });
@@ -16,7 +16,7 @@ const noopExecute = () => {};
16
16
  freeze(noopExecute);
17
17
 
18
18
  /** @type {import('./types.js').ParseFn} */
19
- export const parseArchiveCjs = async (
19
+ export const parseArchiveCjs = (
20
20
  bytes,
21
21
  specifier,
22
22
  location,
@@ -66,4 +66,5 @@ export const parseArchiveCjs = async (
66
66
  export default {
67
67
  parse: parseArchiveCjs,
68
68
  heuristicImports: true,
69
+ synchronous: true,
69
70
  };
@@ -9,7 +9,7 @@ const textEncoder = new TextEncoder();
9
9
  const textDecoder = new TextDecoder();
10
10
 
11
11
  /** @type {import('./types.js').ParseFn} */
12
- export const parseArchiveMjs = async (
12
+ export const parseArchiveMjs = (
13
13
  bytes,
14
14
  specifier,
15
15
  sourceUrl,
@@ -35,4 +35,5 @@ export const parseArchiveMjs = async (
35
35
  export default {
36
36
  parse: parseArchiveMjs,
37
37
  heuristicImports: false,
38
+ synchronous: true,
38
39
  };
@@ -1 +1 @@
1
- {"version":3,"file":"parse-bytes.d.ts","sourceRoot":"","sources":["parse-bytes.js"],"names":[],"mappings":"AAcA,2CAA2C;AAC3C,yBADW,OAAO,YAAY,EAAE,OAAO,CA+BrC;wBAES,OAAO,YAAY,EAAE,oBAAoB"}
1
+ {"version":3,"file":"parse-bytes.d.ts","sourceRoot":"","sources":["parse-bytes.js"],"names":[],"mappings":"AAcA,2CAA2C;AAC3C,yBADW,OAAO,YAAY,EAAE,OAAO,CA0BrC;wBAES,OAAO,YAAY,EAAE,oBAAoB"}
@@ -13,12 +13,7 @@
13
13
  const freeze = Object.freeze;
14
14
 
15
15
  /** @type {import('./types.js').ParseFn} */
16
- export const parseBytes = async (
17
- bytes,
18
- _specifier,
19
- _location,
20
- _packageLocation,
21
- ) => {
16
+ export const parseBytes = (bytes, _specifier, _location, _packageLocation) => {
22
17
  // Snapshot ArrayBuffer
23
18
  const buffer = new ArrayBuffer(bytes.length);
24
19
  const bytesView = new Uint8Array(buffer);
@@ -49,4 +44,5 @@ export const parseBytes = async (
49
44
  export default {
50
45
  parse: parseBytes,
51
46
  heuristicImports: false,
47
+ synchronous: true,
52
48
  };
@@ -1 +1 @@
1
- {"version":3,"file":"parse-cjs-shared-export-wrapper.d.ts","sourceRoot":"","sources":["parse-cjs-shared-export-wrapper.js"],"names":[],"mappings":"AAmCO,2CAPI,UAAU,GAAG,MAAM,GAAG,SAAS,YAC/B,MAAM,GACJ;IACR,QAAQ,EAAC,MAAM,GAAC,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,GAAC,IAAI,CAAA;CACrB,CAgCH;AAmBM,uGAZJ;IAAmB,uBAAuB,EAAlC,MAAM;IACU,WAAW,EAA3B,WAAW;IACgB,eAAe,EAA1C,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IACX,QAAQ,EAAnB,MAAM;IAC8B,UAAU,EAA9C,MAAM,GAAG,UAAU,GAAG,SAAS;CACvC,GAAU;IACR,MAAM,EAAE;QAAE,OAAO,EAAE,GAAG,CAAA;KAAE,CAAC;IACzB,aAAa,EAAE,GAAG,CAAC;IACnB,YAAY,WAAW;IACvB,OAAO,WAAW;CACnB,CAsHH;gCAhM6B,YAAY;4BADhB,YAAY"}
1
+ {"version":3,"file":"parse-cjs-shared-export-wrapper.d.ts","sourceRoot":"","sources":["parse-cjs-shared-export-wrapper.js"],"names":[],"mappings":"AAuCO,2CAPI,UAAU,GAAG,MAAM,GAAG,SAAS,YAC/B,MAAM,GACJ;IACR,QAAQ,EAAC,MAAM,GAAC,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,GAAC,IAAI,CAAA;CACrB,CAgCH;AAmBM,uGAZJ;IAAmB,uBAAuB,EAAlC,MAAM;IACU,WAAW,EAA3B,WAAW;IACgB,eAAe,EAA1C,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IACX,QAAQ,EAAnB,MAAM;IAC8B,UAAU,EAA9C,MAAM,GAAG,UAAU,GAAG,SAAS;CACvC,GAAU;IACR,MAAM,EAAE;QAAE,OAAO,EAAE,GAAG,CAAA;KAAE,CAAC;IACzB,aAAa,EAAE,GAAG,CAAC;IACnB,YAAY,WAAW;IACvB,OAAO,WAAW;CACnB,CAmIH;gCA/M6B,YAAY;4BADhB,YAAY"}
@@ -3,6 +3,8 @@
3
3
  * module source.
4
4
  */
5
5
 
6
+ import { findInvalidReadNowPowersProps, isReadNowPowers } from './powers.js';
7
+
6
8
  // @ts-check
7
9
 
8
10
  /** @import {ReadFn} from './types.js' */
@@ -24,7 +26,9 @@ const noTrailingSlash = path => {
24
26
  };
25
27
 
26
28
  /**
27
- * Generates values for __filename and __dirname from location
29
+ * Generates values for __filename and __dirname from location _if and only if_
30
+ * `readPowers` is of type {@link ReadPowers} containing a
31
+ * {@link ReadPowers.fileURLToPath} method.
28
32
  *
29
33
  * @param {ReadPowers | ReadFn | undefined} readPowers
30
34
  * @param {string} location
@@ -141,13 +145,26 @@ export const wrap = ({
141
145
  },
142
146
  });
143
147
 
144
- const require = (/** @type {string} */ importSpecifier) => {
148
+ /** @param {string} importSpecifier */
149
+ const require = importSpecifier => {
150
+ // if this fails, tell user
151
+
152
+ /** @type {import('ses').ModuleExportsNamespace} */
153
+ let namespace;
154
+
145
155
  if (!has(resolvedImports, importSpecifier)) {
146
- throw new Error(
147
- `Cannot find module "${importSpecifier}" in "${location}"`,
148
- );
156
+ if (isReadNowPowers(readPowers)) {
157
+ namespace = compartment.importNow(importSpecifier);
158
+ } else {
159
+ const invalidProps = findInvalidReadNowPowersProps(readPowers).sort();
160
+ throw new Error(
161
+ `Synchronous readPowers required for dynamic import of ${assert.quote(importSpecifier)}; missing or invalid prop(s): ${invalidProps.join(', ')}`,
162
+ );
163
+ }
164
+ } else {
165
+ namespace = compartment.importNow(resolvedImports[importSpecifier]);
149
166
  }
150
- const namespace = compartment.importNow(resolvedImports[importSpecifier]);
167
+
151
168
  // If you read this file carefully, you'll see it's not possible for a cjs module to not have the default anymore.
152
169
  // It's currently possible to require modules that were not created by this file though.
153
170
  if (has(namespace, 'default')) {
package/src/parse-cjs.js CHANGED
@@ -12,7 +12,7 @@ const textDecoder = new TextDecoder();
12
12
  const { freeze } = Object;
13
13
 
14
14
  /** @type {import('./types.js').ParseFn} */
15
- export const parseCjs = async (
15
+ export const parseCjs = (
16
16
  bytes,
17
17
  _specifier,
18
18
  location,
@@ -31,7 +31,7 @@ export const parseCjs = async (
31
31
  exports.push('default');
32
32
  }
33
33
 
34
- const { filename, dirname } = await getModulePaths(readPowers, location);
34
+ const { filename, dirname } = getModulePaths(readPowers, location);
35
35
 
36
36
  /**
37
37
  * @param {object} moduleEnvironmentRecord
@@ -67,4 +67,5 @@ export const parseCjs = async (
67
67
  export default {
68
68
  parse: parseCjs,
69
69
  heuristicImports: true,
70
+ synchronous: true,
70
71
  };
@@ -1,5 +1,7 @@
1
- /** @type {import('./types.js').ParseFn} */
2
- export const parseJson: import("./types.js").ParseFn;
3
- declare const _default: import("./types.js").ParserImplementation;
1
+ /** @type {ParseFn} */
2
+ export const parseJson: ParseFn;
3
+ declare const _default: ParserImplementation;
4
4
  export default _default;
5
+ import type { ParseFn } from './types.js';
6
+ import type { ParserImplementation } from './types.js';
5
7
  //# sourceMappingURL=parse-json.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse-json.d.ts","sourceRoot":"","sources":["parse-json.js"],"names":[],"mappings":"AAgBA,2CAA2C;AAC3C,wBADW,OAAO,YAAY,EAAE,OAAO,CAyBrC;wBAES,OAAO,YAAY,EAAE,oBAAoB"}
1
+ {"version":3,"file":"parse-json.d.ts","sourceRoot":"","sources":["parse-json.js"],"names":[],"mappings":"AAoBA,sBAAsB;AACtB,wBADW,OAAO,CAoBhB;wBAES,oBAAoB;;6BArCJ,YAAY;0CACC,YAAY"}
package/src/parse-json.js CHANGED
@@ -2,25 +2,24 @@
2
2
 
3
3
  // @ts-check
4
4
 
5
+ /** @import {Harden} from 'ses' */
6
+ /** @import {ParseFn} from './types.js' */
7
+ /** @import {ParserImplementation} from './types.js' */
8
+
5
9
  import { parseLocatedJson } from './json.js';
6
10
 
7
11
  /**
8
12
  * TypeScript cannot be relied upon to deal with the nuances of Readonly, so we
9
13
  * borrow the pass-through type definition of harden here.
10
14
  *
11
- * @type {import('ses').Harden}
15
+ * @type {Harden}
12
16
  */
13
17
  const freeze = Object.freeze;
14
18
 
15
19
  const textDecoder = new TextDecoder();
16
20
 
17
- /** @type {import('./types.js').ParseFn} */
18
- export const parseJson = async (
19
- bytes,
20
- _specifier,
21
- location,
22
- _packageLocation,
23
- ) => {
21
+ /** @type {ParseFn} */
22
+ export const parseJson = (bytes, _specifier, location, _packageLocation) => {
24
23
  const source = textDecoder.decode(bytes);
25
24
  const imports = freeze([]);
26
25
 
@@ -41,8 +40,9 @@ export const parseJson = async (
41
40
  };
42
41
  };
43
42
 
44
- /** @type {import('./types.js').ParserImplementation} */
43
+ /** @type {ParserImplementation} */
45
44
  export default {
46
45
  parse: parseJson,
47
46
  heuristicImports: false,
47
+ synchronous: true,
48
48
  };
package/src/parse-mjs.js CHANGED
@@ -7,7 +7,7 @@ import { ModuleSource } from '@endo/module-source';
7
7
  const textDecoder = new TextDecoder();
8
8
 
9
9
  /** @type {import('./types.js').ParseFn} */
10
- export const parseMjs = async (
10
+ export const parseMjs = (
11
11
  bytes,
12
12
  _specifier,
13
13
  sourceUrl,
@@ -33,4 +33,5 @@ export const parseMjs = async (
33
33
  export default {
34
34
  parse: parseMjs,
35
35
  heuristicImports: false,
36
+ synchronous: true,
36
37
  };
@@ -12,7 +12,7 @@ import { wrap, getModulePaths } from './parse-cjs-shared-export-wrapper.js';
12
12
  const textDecoder = new TextDecoder();
13
13
 
14
14
  /** @type {import('./types.js').ParseFn} */
15
- export const parsePreCjs = async (
15
+ export const parsePreCjs = (
16
16
  bytes,
17
17
  _specifier,
18
18
  location,
@@ -25,7 +25,7 @@ export const parsePreCjs = async (
25
25
  location,
26
26
  );
27
27
 
28
- const { filename, dirname } = await getModulePaths(readPowers, location);
28
+ const { filename, dirname } = getModulePaths(readPowers, location);
29
29
 
30
30
  /**
31
31
  * @param {object} moduleEnvironmentRecord
@@ -64,4 +64,5 @@ export const parsePreCjs = async (
64
64
  export default {
65
65
  parse: parsePreCjs,
66
66
  heuristicImports: true,
67
+ synchronous: true,
67
68
  };
@@ -11,7 +11,7 @@ import { parseLocatedJson } from './json.js';
11
11
  const textDecoder = new TextDecoder();
12
12
 
13
13
  /** @type {import('./types.js').ParseFn} */
14
- export const parsePreMjs = async (
14
+ export const parsePreMjs = (
15
15
  bytes,
16
16
  _specifier,
17
17
  location,
@@ -38,4 +38,5 @@ export const parsePreMjs = async (
38
38
  export default {
39
39
  parse: parsePreMjs,
40
40
  heuristicImports: false,
41
+ synchronous: true,
41
42
  };
@@ -1 +1 @@
1
- {"version":3,"file":"parse-text.d.ts","sourceRoot":"","sources":["parse-text.js"],"names":[],"mappings":"AAiBA,2CAA2C;AAC3C,wBADW,OAAO,YAAY,EAAE,OAAO,CA4BrC;wBAES,OAAO,YAAY,EAAE,oBAAoB"}
1
+ {"version":3,"file":"parse-text.d.ts","sourceRoot":"","sources":["parse-text.js"],"names":[],"mappings":"AAiBA,2CAA2C;AAC3C,wBADW,OAAO,YAAY,EAAE,OAAO,CAuBrC;wBAES,OAAO,YAAY,EAAE,oBAAoB"}
package/src/parse-text.js CHANGED
@@ -16,12 +16,7 @@ const freeze = Object.freeze;
16
16
  const textDecoder = new TextDecoder();
17
17
 
18
18
  /** @type {import('./types.js').ParseFn} */
19
- export const parseText = async (
20
- bytes,
21
- _specifier,
22
- _location,
23
- _packageLocation,
24
- ) => {
19
+ export const parseText = (bytes, _specifier, _location, _packageLocation) => {
25
20
  const text = textDecoder.decode(bytes);
26
21
 
27
22
  /** @type {Array<string>} */
@@ -49,4 +44,5 @@ export const parseText = async (
49
44
  export default {
50
45
  parse: parseText,
51
46
  heuristicImports: false,
47
+ synchronous: true,
52
48
  };