@endo/compartment-mapper 1.5.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +278 -111
  3. package/SECURITY.md +2 -2
  4. package/bundle.d.ts +1 -1
  5. package/bundle.js +4 -1
  6. package/functor-lite.d.ts +3 -0
  7. package/functor-lite.d.ts.map +1 -0
  8. package/functor-lite.js +4 -0
  9. package/functor.d.ts +3 -0
  10. package/functor.d.ts.map +1 -0
  11. package/functor.js +4 -0
  12. package/index.d.ts +1 -1
  13. package/index.js +4 -1
  14. package/package.json +15 -11
  15. package/script-lite.d.ts +3 -0
  16. package/script-lite.d.ts.map +1 -0
  17. package/script-lite.js +4 -0
  18. package/script.d.ts +3 -0
  19. package/script.d.ts.map +1 -0
  20. package/script.js +4 -0
  21. package/src/archive-lite.d.ts +5 -5
  22. package/src/archive-lite.d.ts.map +1 -1
  23. package/src/archive-lite.js +1 -1
  24. package/src/archive.d.ts +5 -5
  25. package/src/archive.d.ts.map +1 -1
  26. package/src/archive.js +3 -1
  27. package/src/bundle-cjs.d.ts +12 -2
  28. package/src/bundle-cjs.d.ts.map +1 -1
  29. package/src/bundle-cjs.js +57 -28
  30. package/src/bundle-json.d.ts.map +1 -1
  31. package/src/bundle-json.js +2 -3
  32. package/src/bundle-lite.d.ts +91 -0
  33. package/src/bundle-lite.d.ts.map +1 -0
  34. package/src/bundle-lite.js +667 -0
  35. package/src/bundle-mjs.d.ts +13 -3
  36. package/src/bundle-mjs.d.ts.map +1 -1
  37. package/src/bundle-mjs.js +36 -19
  38. package/src/bundle.d.ts +48 -10
  39. package/src/bundle.d.ts.map +1 -1
  40. package/src/bundle.js +392 -126
  41. package/src/capture-lite.d.ts +1 -1
  42. package/src/capture-lite.d.ts.map +1 -1
  43. package/src/capture-lite.js +4 -2
  44. package/src/compartment-map.d.ts +1 -1
  45. package/src/compartment-map.d.ts.map +1 -1
  46. package/src/import-archive-lite.d.ts +2 -2
  47. package/src/import-archive-lite.d.ts.map +1 -1
  48. package/src/import-archive-lite.js +3 -1
  49. package/src/import-archive.d.ts +3 -3
  50. package/src/import-archive.d.ts.map +1 -1
  51. package/src/import-archive.js +3 -1
  52. package/src/import-hook.d.ts +3 -16
  53. package/src/import-hook.d.ts.map +1 -1
  54. package/src/import-hook.js +214 -116
  55. package/src/import-lite.d.ts +1 -1
  56. package/src/import-lite.d.ts.map +1 -1
  57. package/src/import-lite.js +7 -3
  58. package/src/import.d.ts.map +1 -1
  59. package/src/import.js +3 -1
  60. package/src/infer-exports.d.ts +5 -7
  61. package/src/infer-exports.d.ts.map +1 -1
  62. package/src/infer-exports.js +23 -8
  63. package/src/link.d.ts.map +1 -1
  64. package/src/link.js +4 -1
  65. package/src/map-parser.d.ts.map +1 -1
  66. package/src/map-parser.js +51 -5
  67. package/src/node-module-specifier.d.ts.map +1 -1
  68. package/src/node-module-specifier.js +3 -1
  69. package/src/node-modules.d.ts +4 -47
  70. package/src/node-modules.d.ts.map +1 -1
  71. package/src/node-modules.js +267 -148
  72. package/src/node-powers.d.ts +1 -1
  73. package/src/node-powers.d.ts.map +1 -1
  74. package/src/node-powers.js +3 -1
  75. package/src/parse-archive-cjs.d.ts +5 -1
  76. package/src/parse-archive-cjs.d.ts.map +1 -1
  77. package/src/parse-archive-cjs.js +11 -4
  78. package/src/parse-archive-mjs.d.ts +5 -1
  79. package/src/parse-archive-mjs.d.ts.map +1 -1
  80. package/src/parse-archive-mjs.js +3 -1
  81. package/src/parse-bytes.d.ts +5 -1
  82. package/src/parse-bytes.d.ts.map +1 -1
  83. package/src/parse-bytes.js +3 -1
  84. package/src/parse-cjs-shared-export-wrapper.d.ts.map +1 -1
  85. package/src/parse-cjs-shared-export-wrapper.js +5 -11
  86. package/src/parse-cjs.d.ts +5 -1
  87. package/src/parse-cjs.d.ts.map +1 -1
  88. package/src/parse-cjs.js +4 -2
  89. package/src/parse-json.d.ts +5 -2
  90. package/src/parse-json.d.ts.map +1 -1
  91. package/src/parse-mjs.d.ts +5 -1
  92. package/src/parse-mjs.d.ts.map +1 -1
  93. package/src/parse-mjs.js +2 -2
  94. package/src/parse-pre-cjs.d.ts +5 -1
  95. package/src/parse-pre-cjs.d.ts.map +1 -1
  96. package/src/parse-pre-cjs.js +3 -1
  97. package/src/parse-pre-mjs.d.ts +5 -1
  98. package/src/parse-pre-mjs.d.ts.map +1 -1
  99. package/src/parse-pre-mjs.js +3 -1
  100. package/src/parse-text.d.ts +5 -1
  101. package/src/parse-text.d.ts.map +1 -1
  102. package/src/parse-text.js +3 -1
  103. package/src/policy-format.d.ts +2 -1
  104. package/src/policy-format.d.ts.map +1 -1
  105. package/src/policy-format.js +5 -2
  106. package/src/policy.d.ts +2 -2
  107. package/src/policy.d.ts.map +1 -1
  108. package/src/policy.js +10 -11
  109. package/src/powers.d.ts +1 -1
  110. package/src/powers.d.ts.map +1 -1
  111. package/src/powers.js +3 -1
  112. package/src/search.d.ts +7 -12
  113. package/src/search.d.ts.map +1 -1
  114. package/src/search.js +32 -13
  115. package/src/types/compartment-map-schema.d.ts +8 -1
  116. package/src/types/compartment-map-schema.d.ts.map +1 -1
  117. package/src/types/compartment-map-schema.ts +8 -1
  118. package/src/types/external.d.ts +127 -17
  119. package/src/types/external.d.ts.map +1 -1
  120. package/src/types/external.ts +142 -17
  121. package/src/types/internal.d.ts +116 -29
  122. package/src/types/internal.d.ts.map +1 -1
  123. package/src/types/internal.ts +144 -31
  124. package/src/types/node-modules.d.ts +79 -0
  125. package/src/types/node-modules.d.ts.map +1 -0
  126. package/src/types/node-modules.ts +89 -0
  127. package/src/types/node-powers.d.ts +7 -5
  128. package/src/types/node-powers.d.ts.map +1 -1
  129. package/src/types/node-powers.ts +7 -5
  130. package/src/types/policy-schema.d.ts +3 -1
  131. package/src/types/policy-schema.d.ts.map +1 -1
  132. package/src/types/policy-schema.ts +3 -1
  133. package/src/types/policy.d.ts +3 -1
  134. package/src/types/policy.d.ts.map +1 -1
  135. package/src/types/policy.ts +3 -1
  136. package/src/types/powers.d.ts +5 -3
  137. package/src/types/powers.d.ts.map +1 -1
  138. package/src/types/powers.ts +5 -3
  139. package/src/types/typescript.d.ts +3 -1
  140. package/src/types/typescript.d.ts.map +1 -1
  141. package/src/types/typescript.ts +3 -1
  142. package/src/url.d.ts.map +1 -1
  143. package/src/url.js +3 -1
@@ -1,4 +1,4 @@
1
- export function captureFromMap(powers: ReadFn | ReadPowers, compartmentMap: CompartmentMapDescriptor, options?: CaptureLiteOptions | undefined): Promise<CaptureResult>;
1
+ export function captureFromMap(powers: ReadFn | ReadPowers, compartmentMap: CompartmentMapDescriptor, options?: CaptureLiteOptions): Promise<CaptureResult>;
2
2
  import type { ReadFn } from './types.js';
3
3
  import type { ReadPowers } from './types.js';
4
4
  import type { CompartmentMapDescriptor } from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"capture-lite.d.ts","sourceRoot":"","sources":["capture-lite.js"],"names":[],"mappings":"AAoFO,uCALI,MAAM,GAAG,UAAU,kBACnB,wBAAwB,6CAEtB,OAAO,CAAC,aAAa,CAAC,CAmElC;4BA9GS,YAAY;gCAAZ,YAAY;8CAAZ,YAAY;wCAAZ,YAAY;mCAAZ,YAAY"}
1
+ {"version":3,"file":"capture-lite.d.ts","sourceRoot":"","sources":["capture-lite.js"],"names":[],"mappings":"AAsFO,uCALI,MAAM,GAAG,UAAU,kBACnB,wBAAwB,YACxB,kBAAkB,GAChB,OAAO,CAAC,aAAa,CAAC,CAmElC;4BA9GS,YAAY;gCAAZ,YAAY;8CAAZ,YAAY;wCAAZ,YAAY;mCAAZ,YAAY"}
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @module
2
+ *
3
3
  * This module provides {@link captureFromMap}, which only "captures" the
4
4
  * compartment map descriptors and sources from a partially completed
5
5
  * compartment map--_without_ creating an archive. The resulting compartment map
@@ -25,6 +25,8 @@
25
25
  * contain original sources, so to import the archive with
26
26
  * `src/import-archive-lite.js`, you will need to provide the archive parsers
27
27
  * and entrain a runtime dependency on Babel.
28
+ *
29
+ * @module
28
30
  */
29
31
 
30
32
  /* eslint no-shadow: 0 */
@@ -123,7 +125,7 @@ export const captureFromMap = async (powers, compartmentMap, options = {}) => {
123
125
  searchSuffixes,
124
126
  entryCompartmentName,
125
127
  entryModuleSpecifier,
126
- exitModuleImportHook: consolidatedExitModuleImportHook,
128
+ importHook: consolidatedExitModuleImportHook,
127
129
  sourceMapHook,
128
130
  });
129
131
  // Induce importHook to record all the necessary modules to import the given module specifier.
@@ -1,6 +1,6 @@
1
1
  /** @type {(a: string, b: string) => number} */
2
2
  export const stringCompare: (a: string, b: string) => number;
3
3
  export function pathCompare(a: Array<string> | undefined, b: Array<string> | undefined): number;
4
- export function assertCompartmentMap(allegedCompartmentMap: unknown, url?: string | undefined): asserts allegedCompartmentMap is CompartmentMapDescriptor;
4
+ export function assertCompartmentMap(allegedCompartmentMap: unknown, url?: string): asserts allegedCompartmentMap is CompartmentMapDescriptor;
5
5
  import type { CompartmentMapDescriptor } from './types.js';
6
6
  //# sourceMappingURL=compartment-map.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"compartment-map.d.ts","sourceRoot":"","sources":["compartment-map.js"],"names":[],"mappings":"AAYA,+CAA+C;AAE/C,4BAFW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CAE2B;AAc/D,+BAHI,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,KACzB,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,UAqCnC;AA8ZM,4DALI,OAAO,6BAEL,QAAQ,qBAAqB,IAAI,wBAAwB,CA+BrE;8CArf2C,YAAY"}
1
+ {"version":3,"file":"compartment-map.d.ts","sourceRoot":"","sources":["compartment-map.js"],"names":[],"mappings":"AAYA,+CAA+C;AAE/C,4BAFW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CAE2B;AAc/D,+BAHI,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,KACzB,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,UAqCnC;AA8ZM,4DALI,OAAO,QACP,MAAM,GACJ,QAAQ,qBAAqB,IAAI,wBAAwB,CA+BrE;8CArf2C,YAAY"}
@@ -1,5 +1,5 @@
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>;
1
+ export function parseArchive(archiveBytes: Uint8Array, archiveLocation?: string, options?: ParseArchiveOptions): Promise<Application>;
2
+ export function loadArchive(readPowers: ReadFn | ReadPowers, archiveLocation: string, options?: LoadArchiveOptions): Promise<Application>;
3
3
  export function importArchive(readPowers: ReadFn | ReadPowers, archiveLocation: string, options: LoadArchiveOptions): Promise<SomeObject>;
4
4
  import type { ParseArchiveOptions } from './types.js';
5
5
  import type { Application } from './types.js';
@@ -1 +1 @@
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
+ {"version":3,"file":"import-archive-lite.d.ts","sourceRoot":"","sources":["import-archive-lite.js"],"names":[],"mappings":"AAsPO,2CALI,UAAU,oBACV,MAAM,YACN,mBAAmB,GACjB,OAAO,CAAC,WAAW,CAAC,CAiKhC;AAQM,wCALI,MAAM,GAAG,UAAU,mBACnB,MAAM,YACN,kBAAkB,GAChB,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,5 +1,5 @@
1
1
  /**
2
- * @module Provides functions for evaluating the modules in an archive (a zip
2
+ * Provides functions for evaluating the modules in an archive (a zip
3
3
  * file with a `compartment-map.json` and a file for each module it contains.)
4
4
  *
5
5
  * These functions do not have a bias for any particular mapping, so you will
@@ -10,6 +10,8 @@
10
10
  * You will need to provide the `defaultParserForLanguage` from
11
11
  * `@endo/compartment-mapper/import-parsers.js` or
12
12
  * `@endo/compartment-mapper/archive-parsers.js`.
13
+ *
14
+ * @module
13
15
  */
14
16
 
15
17
  /* eslint no-shadow: "off" */
@@ -1,5 +1,5 @@
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?: import("./types.js").ParseArchiveOptions | undefined): Promise<Application>;
1
+ export function parseArchive(archiveBytes: Uint8Array, archiveLocation?: string, options?: Options): Promise<Application>;
2
+ export function loadArchive(readPowers: import("@endo/zip").ReadFn | ReadPowers, archiveLocation: string, options?: LoadArchiveOptions): 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 { ExecuteOptions } from './types.js';
18
17
  import type { LoadArchiveOptions } from './types.js';
18
+ import type { ExecuteOptions } 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":"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
+ {"version":3,"file":"import-archive.d.ts","sourceRoot":"","sources":["import-archive.js"],"names":[],"mappings":"AA+EO,2CALI,UAAU,oBACV,MAAM,YACN,OAAO,GACL,OAAO,CAAC,WAAW,CAAC,CAW9B;AAQI,wCALI,OAAO,WAAW,EAAE,MAAM,GAAG,UAAU,mBACvC,MAAM,YACN,kBAAkB,GAChB,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;wCAAZ,YAAY;oCAAZ,YAAY;4BAAZ,YAAY;0CAAZ,YAAY;+CAAZ,YAAY;kDAAZ,YAAY;uCAAZ,YAAY"}
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @module Provides functions for evaluating modules in an archive (a zip file
2
+ * Provides functions for evaluating modules in an archive (a zip file
3
3
  * with a `compartment-map.json` and a file for a module and each of its
4
4
  * transitive dependencies.)
5
5
  *
@@ -12,6 +12,8 @@
12
12
  * See `import-archive-lite.js` for functions that are not coupled to these
13
13
  * parsers or the `node_modules` conventions without necessarily entraining a
14
14
  * dependency on Babel.
15
+ *
16
+ * @module
15
17
  */
16
18
 
17
19
  /**
@@ -6,32 +6,19 @@
6
6
  * @param {MakeImportNowHookMakerOptions} options
7
7
  * @returns {ImportNowHookMaker}
8
8
  */
9
- export function makeImportNowHookMaker(readPowers: ReadNowPowers, baseLocation: string, { sources, compartmentDescriptors, computeSha512, searchSuffixes, sourceMapHook, exitModuleImportNowHook, }: MakeImportNowHookMakerOptions): ImportNowHookMaker;
9
+ export function makeImportNowHookMaker(readPowers: ReadNowPowers, baseLocation: string, { sources, compartmentDescriptors, computeSha512, searchSuffixes, archiveOnly, sourceMapHook, importNowHook: exitModuleImportNowHook, }: MakeImportNowHookMakerOptions): ImportNowHookMaker;
10
10
  export function exitModuleImportHookMaker({ modules, exitModuleImportHook, entryCompartmentName, }: {
11
11
  modules?: Record<string, any> | undefined;
12
12
  exitModuleImportHook?: ExitModuleImportHook | undefined;
13
13
  entryCompartmentName: string;
14
14
  }): ExitModuleImportHook | undefined;
15
- export function makeImportHookMaker(readPowers: ReadFn | ReadPowers, baseLocation: string, { sources, compartmentDescriptors, archiveOnly, computeSha512, searchSuffixes, sourceMapHook, entryCompartmentName, entryModuleSpecifier, exitModuleImportHook, }: {
16
- sources?: Sources | undefined;
17
- compartmentDescriptors?: Record<string, CompartmentDescriptor> | undefined;
18
- archiveOnly?: boolean | undefined;
19
- computeSha512?: HashFn | undefined;
20
- searchSuffixes?: string[] | undefined;
21
- entryCompartmentName: string;
22
- entryModuleSpecifier: string;
23
- exitModuleImportHook?: ExitModuleImportHook | undefined;
24
- sourceMapHook?: SourceMapHook | undefined;
25
- }): ImportHookMaker;
15
+ export function makeImportHookMaker(readPowers: ReadFn | ReadPowers, baseLocation: string, { sources, compartmentDescriptors, archiveOnly, computeSha512, searchSuffixes, sourceMapHook, entryCompartmentName, entryModuleSpecifier, importHook: exitModuleImportHook, }: MakeImportHookMakerOptions): ImportHookMaker;
26
16
  import type { ReadNowPowers } from './types.js';
27
17
  import type { MakeImportNowHookMakerOptions } from './types.js';
28
18
  import type { ImportNowHookMaker } from './types.js';
29
19
  import type { ExitModuleImportHook } from './types.js';
30
20
  import type { ReadFn } from './types.js';
31
21
  import type { ReadPowers } from './types.js';
32
- import type { Sources } from './types.js';
33
- import type { CompartmentDescriptor } from './types.js';
34
- import type { HashFn } from './types.js';
35
- import type { SourceMapHook } from './types.js';
22
+ import type { MakeImportHookMakerOptions } from './types.js';
36
23
  import type { ImportHookMaker } from './types.js';
37
24
  //# sourceMappingURL=import-hook.d.ts.map
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"import-hook.d.ts","sourceRoot":"","sources":["import-hook.js"],"names":[],"mappings":"AAkoBA;;;;;;;GAOG;AACH,mDALW,aAAa,gBACb,MAAM,2IACN,6BAA6B,GAC3B,kBAAkB,CAkN9B;AAnoBM,oGALJ;IAAqC,OAAO;IACN,oBAAoB;IACnC,oBAAoB,EAAnC,MAAM;CACd,GAAU,oBAAoB,GAAC,SAAS,CA8B1C;AAiPM,gDALI,MAAM,GAAC,UAAU,gBACjB,MAAM,iLACN,0BAA0B,GACxB,eAAe,CA8J3B;mCAzlBS,YAAY;mDAAZ,YAAY;wCAAZ,YAAY;0CAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;gDAAZ,YAAY;qCAAZ,YAAY"}
@@ -1,11 +1,13 @@
1
1
  /**
2
- * @module Provides the implementation of each compartment's `importHook` when
2
+ * Provides the implementation of each compartment's `importHook` when
3
3
  * using `import.js`, `import-lite.js`, `archive.js`, or `archive-lite.js`.
4
4
  * However, `import-archive.js` and `import-archive-lite.js` use their own
5
5
  * variant.
6
6
  *
7
7
  * For building archives, these import hooks create a table of all the modules
8
8
  * in a "working set" of transitive dependencies.
9
+ *
10
+ * @module
9
11
  */
10
12
 
11
13
  /**
@@ -13,7 +15,8 @@
13
15
  * ImportHook,
14
16
  * ImportNowHook,
15
17
  * RedirectStaticModuleInterface,
16
- * StaticModuleType
18
+ * StaticModuleType,
19
+ * VirtualModuleSource
17
20
  * } from 'ses'
18
21
  * @import {
19
22
  * CompartmentDescriptor,
@@ -22,17 +25,18 @@
22
25
  * ChooseModuleDescriptorYieldables,
23
26
  * ExitModuleImportHook,
24
27
  * FindRedirectParams,
25
- * HashFn,
26
28
  * ImportHookMaker,
27
29
  * ImportNowHookMaker,
30
+ * MakeImportHookMakerOptions,
28
31
  * MakeImportNowHookMakerOptions,
29
32
  * ModuleDescriptor,
30
33
  * ParseResult,
31
34
  * ReadFn,
32
35
  * ReadPowers,
33
- * SourceMapHook,
34
- * Sources,
35
- * ReadNowPowers
36
+ * ReadNowPowers,
37
+ * StrictlyRequiredFn,
38
+ * CompartmentSources,
39
+ * DeferErrorFn
36
40
  * } from './types.js'
37
41
  */
38
42
 
@@ -92,6 +96,32 @@ const nodejsConventionSearchSuffixes = [
92
96
  '/index.node',
93
97
  ];
94
98
 
99
+ /**
100
+ * Returns `true` if `absoluteModuleSpecifier` is within the path `compartmentLocation`.
101
+ * @param {string} absoluteModudeSpecifier Absolute path to module specifier
102
+ * @param {string} compartmentLocation Absolute path to compartment location
103
+ * @returns {boolean}
104
+ */
105
+ const isLocationWithinCompartment = (
106
+ absoluteModudeSpecifier,
107
+ compartmentLocation,
108
+ ) => absoluteModudeSpecifier.startsWith(compartmentLocation);
109
+
110
+ /**
111
+ * Computes the relative path to a module from its compartment location (including a leading `./`)
112
+ *
113
+ * @param {string} absoluteModuleSpecifier Absolute path (not URL) to a module specifier
114
+ * @param {string} compartmentLocation Absolute path to compartment location
115
+ * @returns {string} Redirect static module interface; the `specifier` prop of this value _must_ be a relative path with leading `./`
116
+ */
117
+ const relativeSpecifier = (absoluteModuleSpecifier, compartmentLocation) => {
118
+ assert(
119
+ isLocationWithinCompartment(absoluteModuleSpecifier, compartmentLocation),
120
+ `Module specifier location "${absoluteModuleSpecifier}" does not start with compartment location "${compartmentLocation}"`,
121
+ );
122
+ return `./${absoluteModuleSpecifier.substring(compartmentLocation.length)}`;
123
+ };
124
+
95
125
  /**
96
126
  * Given a module specifier which is an absolute path, attempt to match it with
97
127
  * an existing compartment; return a {@link RedirectStaticModuleInterface} if found.
@@ -105,12 +135,9 @@ const findRedirect = ({
105
135
  compartmentDescriptors,
106
136
  compartments,
107
137
  absoluteModuleSpecifier,
108
- packageLocation,
109
138
  }) => {
110
- const moduleSpecifierLocation = new URL(
111
- absoluteModuleSpecifier,
112
- packageLocation,
113
- ).href;
139
+ const moduleSpecifierLocation = new URL(absoluteModuleSpecifier, 'file:')
140
+ .href;
114
141
 
115
142
  // a file:// URL string
116
143
  let someLocation = new URL('./', moduleSpecifierLocation).href;
@@ -136,7 +163,7 @@ const findRedirect = ({
136
163
  // is a dependency of the compartment descriptor
137
164
  if (compartmentDescriptor.compartments.has(location)) {
138
165
  return {
139
- specifier: absoluteModuleSpecifier,
166
+ specifier: relativeSpecifier(moduleSpecifierLocation, location),
140
167
  compartment: compartments[location],
141
168
  };
142
169
  }
@@ -157,7 +184,7 @@ const findRedirect = ({
157
184
  },
158
185
  );
159
186
  return {
160
- specifier: absoluteModuleSpecifier,
187
+ specifier: relativeSpecifier(moduleSpecifierLocation, location),
161
188
  compartment: compartments[location],
162
189
  };
163
190
  }
@@ -267,11 +294,13 @@ function* chooseModuleDescriptor(
267
294
  packageLocation,
268
295
  packageSources,
269
296
  readPowers,
297
+ archiveOnly,
270
298
  sourceMapHook,
271
299
  strictlyRequiredForCompartment,
272
300
  },
273
301
  { maybeRead, parse, shouldDeferError = () => false },
274
302
  ) {
303
+ const { sourceDirname } = compartmentDescriptor;
275
304
  for (const candidateSpecifier of candidates) {
276
305
  const candidateModuleDescriptor = moduleDescriptors[candidateSpecifier];
277
306
  if (candidateModuleDescriptor !== undefined) {
@@ -327,6 +356,7 @@ function* chooseModuleDescriptor(
327
356
  packageLocation,
328
357
  {
329
358
  readPowers,
359
+ archiveOnly,
330
360
  sourceMapHook:
331
361
  sourceMapHook &&
332
362
  (nextSourceMapObject => {
@@ -378,6 +408,7 @@ function* chooseModuleDescriptor(
378
408
  packageSources[candidateSpecifier] = {
379
409
  location: packageRelativeLocation,
380
410
  sourceLocation: moduleLocation,
411
+ sourceDirname,
381
412
  parser,
382
413
  bytes: transformedBytes,
383
414
  record: concreteRecord,
@@ -397,25 +428,58 @@ function* chooseModuleDescriptor(
397
428
  return undefined;
398
429
  }
399
430
 
431
+ /**
432
+ * Creates a `deferError` function which decides whether to throw immediately or
433
+ * at execution time.
434
+ *
435
+ * @param {StrictlyRequiredFn} strictlyRequiredForCompartment
436
+ * @param {string} packageLocation
437
+ * @param {CompartmentSources} packageSources
438
+ * @returns {DeferErrorFn}
439
+ */
440
+ const makeDeferError = (
441
+ strictlyRequiredForCompartment,
442
+ packageLocation,
443
+ packageSources,
444
+ ) => {
445
+ /**
446
+ * @param {string} specifier
447
+ * @param {Error} error - error to throw on execute
448
+ * @returns {StaticModuleType}
449
+ */
450
+ const deferError = (specifier, error) => {
451
+ // strictlyRequired is populated with imports declared by modules whose parser is not using heuristics to figure
452
+ // out imports. We're guaranteed they're reachable. If the same module is imported and required, it will not
453
+ // defer, because importing from esm makes it strictly required.
454
+ // Note that ultimately a situation may arise, with exit modules, where the module never reaches importHook but
455
+ // its imports do. In that case the notion of strictly required is no longer boolean, it's true,false,noidea.
456
+ if (strictlyRequiredForCompartment(packageLocation).has(specifier)) {
457
+ throw error;
458
+ }
459
+ // Return a place-holder that'd throw an error if executed
460
+ // This allows cjs parser to more eagerly find calls to require
461
+ // - if parser identified a require call that's a local function, execute will never be called
462
+ // - if actual required module is missing, the error will happen anyway - at execution time
463
+ const record = freeze({
464
+ imports: [],
465
+ exports: [],
466
+ execute: () => {
467
+ throw error;
468
+ },
469
+ });
470
+ packageSources[specifier] = {
471
+ deferredError: error.message,
472
+ };
473
+
474
+ return record;
475
+ };
476
+ return deferError;
477
+ };
478
+
400
479
  /**
401
480
  * @param {ReadFn|ReadPowers} readPowers
402
481
  * @param {string} baseLocation
403
- * @param {object} options
404
- * @param {Sources} [options.sources]
405
- * @param {Record<string, CompartmentDescriptor>} [options.compartmentDescriptors]
406
- * @param {boolean} [options.archiveOnly]
407
- * @param {HashFn} [options.computeSha512]
408
- * @param {Array<string>} [options.searchSuffixes] - Suffixes to search if the
409
- * unmodified specifier is not found.
410
- * Pass [] to emulate Node.js' strict behavior.
411
- * The default handles Node.js' CommonJS behavior.
412
- * Unlike Node.js, the Compartment Mapper lifts CommonJS up, more like a
413
- * bundler, and does not attempt to vary the behavior of resolution depending
414
- * on the language of the importing module.
415
- * @param {string} options.entryCompartmentName
416
- * @param {string} options.entryModuleSpecifier
417
- * @param {ExitModuleImportHook} [options.exitModuleImportHook]
418
- * @param {SourceMapHook} [options.sourceMapHook]
482
+ * @param {MakeImportHookMakerOptions} options
419
483
  * @returns {ImportHookMaker}
420
484
  */
421
485
  export const makeImportHookMaker = (
@@ -430,7 +494,7 @@ export const makeImportHookMaker = (
430
494
  sourceMapHook = undefined,
431
495
  entryCompartmentName,
432
496
  entryModuleSpecifier,
433
- exitModuleImportHook = undefined,
497
+ importHook: exitModuleImportHook = undefined,
434
498
  },
435
499
  ) => {
436
500
  // Set of specifiers for modules (scoped to compartment) whose parser is not
@@ -471,37 +535,11 @@ export const makeImportHookMaker = (
471
535
  const { modules: moduleDescriptors = create(null) } = compartmentDescriptor;
472
536
  compartmentDescriptor.modules = moduleDescriptors;
473
537
 
474
- /**
475
- * @param {string} specifier
476
- * @param {Error} error - error to throw on execute
477
- * @returns {StaticModuleType}
478
- */
479
- const deferError = (specifier, error) => {
480
- // strictlyRequired is populated with imports declared by modules whose parser is not using heuristics to figure
481
- // out imports. We're guaranteed they're reachable. If the same module is imported and required, it will not
482
- // defer, because importing from esm makes it strictly required.
483
- // Note that ultimately a situation may arise, with exit modules, where the module never reaches importHook but
484
- // its imports do. In that case the notion of strictly required is no longer boolean, it's true,false,noidea.
485
- if (strictlyRequiredForCompartment(packageLocation).has(specifier)) {
486
- throw error;
487
- }
488
- // Return a place-holder that'd throw an error if executed
489
- // This allows cjs parser to more eagerly find calls to require
490
- // - if parser identified a require call that's a local function, execute will never be called
491
- // - if actual required module is missing, the error will happen anyway - at execution time
492
- const record = freeze({
493
- imports: [],
494
- exports: [],
495
- execute: () => {
496
- throw error;
497
- },
498
- });
499
- packageSources[specifier] = {
500
- deferredError: error.message,
501
- };
502
-
503
- return record;
504
- };
538
+ const deferError = makeDeferError(
539
+ strictlyRequiredForCompartment,
540
+ packageLocation,
541
+ packageSources,
542
+ );
505
543
 
506
544
  /** @type {ImportHook} */
507
545
  const importHook = async moduleSpecifier => {
@@ -575,6 +613,7 @@ export const makeImportHookMaker = (
575
613
  packageLocation,
576
614
  packageSources,
577
615
  readPowers,
616
+ archiveOnly,
578
617
  sourceMapHook,
579
618
  strictlyRequiredForCompartment,
580
619
  },
@@ -617,8 +656,9 @@ export function makeImportNowHookMaker(
617
656
  compartmentDescriptors = create(null),
618
657
  computeSha512 = undefined,
619
658
  searchSuffixes = nodejsConventionSearchSuffixes,
659
+ archiveOnly = false,
620
660
  sourceMapHook = undefined,
621
- exitModuleImportNowHook,
661
+ importNowHook: exitModuleImportNowHook = undefined,
622
662
  },
623
663
  ) {
624
664
  // Set of specifiers for modules (scoped to compartment) whose parser is not
@@ -647,7 +687,55 @@ export function makeImportNowHookMaker(
647
687
  packageName: _packageName,
648
688
  parse,
649
689
  compartments,
690
+ shouldDeferError,
650
691
  }) => {
692
+ const deferError = makeDeferError(
693
+ strictlyRequiredForCompartment,
694
+ packageLocation,
695
+ sources,
696
+ );
697
+
698
+ /**
699
+ * Attempt to load the moduleSpecifier via an {@link ExitModuleImportNowHook}, if one exists.
700
+ *
701
+ * If it doesn't exist, then throw an exception.
702
+ * @param {string} moduleSpecifier
703
+ * @param {CompartmentDescriptor} compartmentDescriptor
704
+ * @returns {VirtualModuleSource}
705
+ */
706
+ const importExitModuleOrFail = (moduleSpecifier, compartmentDescriptor) => {
707
+ if (exitModuleImportNowHook) {
708
+ // This hook is responsible for ensuring that the moduleSpecifier
709
+ // actually refers to an exit module.
710
+ const exitRecord = exitModuleImportNowHook(
711
+ moduleSpecifier,
712
+ packageLocation,
713
+ );
714
+ if (exitRecord) {
715
+ // It'd be nice to check the policy before importing it, but we can only throw a policy error if the
716
+ // hook returns something. Otherwise, we need to fall back to the 'cannot find' error below.
717
+ enforceModulePolicy(moduleSpecifier, compartmentDescriptor, {
718
+ exit: true,
719
+ errorHint: `Blocked in loading. ${q(
720
+ moduleSpecifier,
721
+ )} was not in the compartment map and an attempt was made to load it as a builtin`,
722
+ });
723
+ return exitRecord;
724
+ }
725
+ throw Error(
726
+ `Cannot find external module ${q(
727
+ moduleSpecifier,
728
+ )} in package at ${packageLocation}`,
729
+ );
730
+ } else {
731
+ throw Error(
732
+ `Cannot find external module ${q(
733
+ moduleSpecifier,
734
+ )} from package at ${packageLocation}; try providing an importNowHook`,
735
+ );
736
+ }
737
+ };
738
+
651
739
  if (!('isSyncParser' in parse)) {
652
740
  return function impossibleTransformImportNowHook() {
653
741
  throw new Error(
@@ -692,69 +780,79 @@ export function makeImportNowHookMaker(
692
780
 
693
781
  /** @type {ImportNowHook} */
694
782
  const importNowHook = moduleSpecifier => {
695
- if (isAbsolute(moduleSpecifier)) {
696
- const record = findRedirect({
697
- compartmentDescriptor,
698
- compartmentDescriptors,
699
- compartments,
700
- absoluteModuleSpecifier: moduleSpecifier,
701
- packageLocation,
702
- });
703
- if (record) {
704
- return record;
705
- }
706
- }
783
+ try {
784
+ // many dynamically-required specifiers will be absolute paths owing to use of `require.resolve()` and `path.resolve()`
785
+ if (isAbsolute(moduleSpecifier)) {
786
+ const record = findRedirect({
787
+ compartmentDescriptor,
788
+ compartmentDescriptors,
789
+ compartments,
790
+ absoluteModuleSpecifier: moduleSpecifier,
791
+ });
792
+ if (record) {
793
+ return record;
794
+ }
707
795
 
708
- const candidates = nominateCandidates(moduleSpecifier, searchSuffixes);
709
-
710
- const record = syncTrampoline(
711
- chooseModuleDescriptor,
712
- {
713
- candidates,
714
- compartmentDescriptor,
715
- compartmentDescriptors,
716
- compartments,
717
- computeSha512,
718
- moduleDescriptors,
719
- moduleSpecifier,
720
- packageLocation,
721
- packageSources,
722
- readPowers,
723
- sourceMapHook,
724
- strictlyRequiredForCompartment,
725
- },
726
- {
727
- maybeRead: maybeReadNow,
728
- parse,
729
- },
730
- );
796
+ // if and only if the module specifier is within the compartment can we
797
+ // make it a relative specifier. the following conditional avoids a try/catch
798
+ // since `relativeSpecifier` will throw if this condition is not met
799
+ if (
800
+ isLocationWithinCompartment(
801
+ moduleSpecifier,
802
+ compartmentDescriptor.location,
803
+ )
804
+ ) {
805
+ moduleSpecifier = relativeSpecifier(
806
+ moduleSpecifier,
807
+ compartmentDescriptor.location,
808
+ );
809
+ }
810
+ } else if (
811
+ moduleSpecifier !== '.' &&
812
+ !moduleSpecifier.startsWith('./')
813
+ ) {
814
+ // could be a builtin, which means we should not bother bouncing on the trampoline to find it.
815
+ return importExitModuleOrFail(moduleSpecifier, compartmentDescriptor);
816
+ }
731
817
 
732
- if (record) {
733
- return record;
734
- }
818
+ // we might have an absolute path here, but it might be within the compartment, so
819
+ // we will try to find it.
820
+ const candidates = nominateCandidates(moduleSpecifier, searchSuffixes);
735
821
 
736
- if (exitModuleImportNowHook) {
737
- // This hook is responsible for ensuring that the moduleSpecifier
738
- // actually refers to an exit module.
739
- const exitRecord = exitModuleImportNowHook(
740
- moduleSpecifier,
741
- packageLocation,
822
+ const record = syncTrampoline(
823
+ chooseModuleDescriptor,
824
+ {
825
+ candidates,
826
+ compartmentDescriptor,
827
+ compartmentDescriptors,
828
+ compartments,
829
+ computeSha512,
830
+ moduleDescriptors,
831
+ moduleSpecifier,
832
+ packageLocation,
833
+ packageSources,
834
+ readPowers,
835
+ archiveOnly,
836
+ sourceMapHook,
837
+ strictlyRequiredForCompartment,
838
+ },
839
+ {
840
+ maybeRead: maybeReadNow,
841
+ parse,
842
+ shouldDeferError,
843
+ },
742
844
  );
743
845
 
744
- if (!exitRecord) {
745
- throw new Error(`Could not import module: ${q(moduleSpecifier)}`);
846
+ if (record) {
847
+ return record;
746
848
  }
747
849
 
748
- return exitRecord;
850
+ // at this point, we haven't found the module by guessing, so we'll try the importer-of-last-resort
851
+ return importExitModuleOrFail(moduleSpecifier, compartmentDescriptor);
852
+ } catch (err) {
853
+ return deferError(moduleSpecifier, err);
749
854
  }
750
-
751
- throw new Error(
752
- `Could not import module: ${q(
753
- moduleSpecifier,
754
- )}; try providing an importNowHook`,
755
- );
756
855
  };
757
-
758
856
  return importNowHook;
759
857
  };
760
858
  return makeImportNowHook;
@@ -14,7 +14,7 @@ export function loadFromMap(readPowers: ReadNowPowers, compartmentMap: Compartme
14
14
  * @returns {Promise<Application>}
15
15
  */
16
16
  export function loadFromMap(readPowers: ReadFn | ReadPowers, compartmentMap: CompartmentMapDescriptor, opts?: ImportLocationOptions | undefined): Promise<Application>;
17
- export function importFromMap(readPowers: ReadFn | ReadPowers, compartmentMap: CompartmentMapDescriptor, options?: ImportLocationOptions | undefined): Promise<SomeObject>;
17
+ export function importFromMap(readPowers: ReadFn | ReadPowers, compartmentMap: CompartmentMapDescriptor, options?: ImportLocationOptions): Promise<SomeObject>;
18
18
  import type { ReadNowPowers } from './types.js';
19
19
  import type { CompartmentMapDescriptor } from './types.js';
20
20
  import type { SyncImportLocationOptions } from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"import-lite.d.ts","sourceRoot":"","sources":["import-lite.js"],"names":[],"mappings":";;;;;;;AAuEG,wCACQ,aAAa,kBACb,wBAAwB,iDAEtB,OAAO,CAAC,WAAW,CAAC,CAChC;;;;;;;;AAGE,wCACQ,MAAM,GAAG,UAAU,kBACnB,wBAAwB,6CAEtB,OAAO,CAAC,WAAW,CAAC,CAChC;AA0JM,0CANI,MAAM,GAAG,UAAU,kBACnB,wBAAwB,gDAEtB,OAAO,CAAC,UAAU,CAAC,CAU/B;mCAtNS,YAAY;8CAAZ,YAAY;+CAAZ,YAAY;iCAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;2CAAZ,YAAY;gCAAZ,YAAY"}
1
+ {"version":3,"file":"import-lite.d.ts","sourceRoot":"","sources":["import-lite.js"],"names":[],"mappings":";;;;;;;AAyEG,wCACQ,aAAa,kBACb,wBAAwB,iDAEtB,OAAO,CAAC,WAAW,CAAC,CAChC;;;;;;;;AAGE,wCACQ,MAAM,GAAG,UAAU,kBACnB,wBAAwB,6CAEtB,OAAO,CAAC,WAAW,CAAC,CAChC;AA4JM,0CANI,MAAM,GAAG,UAAU,kBACnB,wBAAwB,YACxB,qBAAqB,GACnB,OAAO,CAAC,UAAU,CAAC,CAU/B;mCAxNS,YAAY;8CAAZ,YAAY;+CAAZ,YAAY;iCAAZ,YAAY;4BAAZ,YAAY;gCAAZ,YAAY;2CAAZ,YAAY;gCAAZ,YAAY"}