@endo/compartment-mapper 1.2.1 → 1.3.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 (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 -1
  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 +56 -37
  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
package/src/policy.d.ts CHANGED
@@ -2,30 +2,30 @@
2
2
  * Const string to identify the internal attenuators compartment
3
3
  */
4
4
  export const ATTENUATORS_COMPARTMENT: "<ATTENUATORS>";
5
- export function detectAttenuators(policy?: import("./types.js").Policy<void, void, void, unknown> | undefined): Array<string>;
6
- export function dependencyAllowedByPolicy(namingKit: import("./types.js").PackageNamingKit, packagePolicy: import("./types.js").PackagePolicy): boolean;
5
+ export function detectAttenuators(policy?: Policy | undefined): Array<string>;
6
+ export function dependencyAllowedByPolicy(namingKit: PackageNamingKit, packagePolicy: PackagePolicy): boolean;
7
7
  /**
8
8
  * Returns the policy applicable to the canonicalName of the package
9
9
  *
10
10
  * @overload
11
- * @param {import('./types.js').PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
12
- * @param {import('./types.js').Policy} policy - user supplied policy
13
- * @returns {import('./types.js').PackagePolicy} packagePolicy if policy was specified
11
+ * @param {PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
12
+ * @param {SomePolicy} policy - user supplied policy
13
+ * @returns {SomePackagePolicy} packagePolicy if policy was specified
14
14
  */
15
- export function getPolicyForPackage(namingKit: import("./types.js").PackageNamingKit, policy: import("./types.js").Policy): import("./types.js").PackagePolicy;
15
+ export function getPolicyForPackage(namingKit: PackageNamingKit, policy: SomePolicy): SomePackagePolicy;
16
16
  /**
17
17
  * Returns `undefined`
18
18
  *
19
19
  * @overload
20
- * @param {import('./types.js').PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
21
- * @param {import('./types.js').Policy} [policy] - user supplied policy
22
- * @returns {import('./types.js').PackagePolicy|undefined} packagePolicy if policy was specified
20
+ * @param {PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
21
+ * @param {SomePolicy} [policy] - user supplied policy
22
+ * @returns {SomePackagePolicy|undefined} packagePolicy if policy was specified
23
23
  */
24
- export function getPolicyForPackage(namingKit: import("./types.js").PackageNamingKit, policy?: import("./types.js").Policy<void, void, void, unknown> | undefined): import("./types.js").PackagePolicy | undefined;
25
- export function makeDeferredAttenuatorsProvider(compartments: Record<string, Compartment>, compartmentDescriptors: Record<string, import("./types.js").CompartmentDescriptor>): import("./types.js").DeferredAttenuatorsProvider;
26
- export function attenuateGlobals(globalThis: object, globals: object, packagePolicy: import("./types.js").PackagePolicy, attenuators: import("./types.js").DeferredAttenuatorsProvider, pendingJobs: Array<Promise<any>>, name?: string): void;
27
- export function enforceModulePolicy(specifier: string, compartmentDescriptor: import("./types.js").CompartmentDescriptor, { exit, errorHint }?: EnforceModulePolicyOptions | undefined): void;
28
- export function attenuateModuleHook(specifier: string, originalModuleRecord: import("ses").ThirdPartyStaticModuleInterface, policy: import("./types.js").PackagePolicy, attenuators: import("./types.js").DeferredAttenuatorsProvider): Promise<import("ses").ThirdPartyStaticModuleInterface>;
24
+ export function getPolicyForPackage(namingKit: PackageNamingKit, policy?: SomePolicy | undefined): SomePackagePolicy | undefined;
25
+ export function makeDeferredAttenuatorsProvider(compartments: Record<string, Compartment>, compartmentDescriptors: Record<string, CompartmentDescriptor>): DeferredAttenuatorsProvider;
26
+ export function attenuateGlobals(globalThis: object, globals: object, packagePolicy: PackagePolicy, attenuators: DeferredAttenuatorsProvider, pendingJobs: Array<Promise<any>>, name?: string): void;
27
+ export function enforceModulePolicy(specifier: string, compartmentDescriptor: CompartmentDescriptor, { exit, errorHint }?: EnforceModulePolicyOptions | undefined): void;
28
+ export function attenuateModuleHook(specifier: string, originalModuleRecord: import("ses").ThirdPartyStaticModuleInterface, policy: PackagePolicy, attenuators: DeferredAttenuatorsProvider): Promise<import("ses").ThirdPartyStaticModuleInterface>;
29
29
  /**
30
30
  * Options for {@link enforceModulePolicy}
31
31
  */
@@ -39,4 +39,11 @@ export type EnforceModulePolicyOptions = {
39
39
  */
40
40
  errorHint?: string | undefined;
41
41
  };
42
+ import type { Policy } from './types.js';
43
+ import type { PackageNamingKit } from './types.js';
44
+ import type { PackagePolicy } from './types.js';
45
+ import type { SomePolicy } from './types.js';
46
+ import type { SomePackagePolicy } from './types.js';
47
+ import type { CompartmentDescriptor } from './types.js';
48
+ import type { DeferredAttenuatorsProvider } from './types.js';
42
49
  //# sourceMappingURL=policy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["policy.js"],"names":[],"mappings":"AAeA;;GAEG;AACH,sDAAuD;AAkDhD,gHAFM,KAAK,CAAC,MAAM,CAAC,CAezB;AA0BM,qDAJI,OAAO,YAAY,EAAE,gBAAgB,iBACrC,OAAO,YAAY,EAAE,aAAa,GAChC,OAAO,CASnB;;;;;;;;;AAKE,+CACQ,OAAO,YAAY,EAAE,gBAAgB,UACrC,OAAO,YAAY,EAAE,MAAM,GACzB,OAAO,YAAY,EAAE,aAAa,CAC9C;;;;;;;;;AAKE,+CACQ,OAAO,YAAY,EAAE,gBAAgB,gFAEnC,OAAO,YAAY,EAAE,aAAa,GAAC,SAAS,CACxD;AAkFM,8DAJI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,0BAC3B,MAAM,CAAC,MAAM,EAAE,OAAO,YAAY,EAAE,qBAAqB,CAAC,GACxD,OAAO,YAAY,EAAE,2BAA2B,CA0C5D;AAoDM,6CARI,MAAM,WACN,MAAM,iBACN,OAAO,YAAY,EAAE,aAAa,eAClC,OAAO,YAAY,EAAE,2BAA2B,eAChD,KAAK,cAAS,SACd,MAAM,GACJ,IAAI,CAgDhB;AA2BM,+CAJI,MAAM,yBACN,OAAO,YAAY,EAAE,qBAAqB,sEAiCpD;AAgDM,+CANI,MAAM,wBACN,OAAO,KAAK,EAAE,+BAA+B,UAC7C,OAAO,YAAY,EAAE,aAAa,eAClC,OAAO,YAAY,EAAE,2BAA2B,GAC9C,OAAO,CAAC,OAAO,KAAK,EAAE,+BAA+B,CAAC,CAyBlE"}
1
+ {"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["policy.js"],"names":[],"mappings":"AA2BA;;GAEG;AACH,sDAAuD;AAyDhD,gEAFM,KAAK,CAAC,MAAM,CAAC,CAezB;AA0BM,qDAJI,gBAAgB,iBAChB,aAAa,GACX,OAAO,CASnB;;;;;;;;;AAKE,+CACQ,gBAAgB,UAChB,UAAU,GACR,iBAAiB,CAC7B;;;;;;;;;AAKE,+CACQ,gBAAgB,oCAEd,iBAAiB,GAAC,SAAS,CACvC;AAkFM,8DAJI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,0BAC3B,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,GACnC,2BAA2B,CA0CvC;AAoDM,6CARI,MAAM,WACN,MAAM,iBACN,aAAa,eACb,2BAA2B,eAC3B,KAAK,cAAS,SACd,MAAM,GACJ,IAAI,CAgDhB;AA2BM,+CAJI,MAAM,yBACN,qBAAqB,sEAiC/B;AAgDM,+CANI,MAAM,wBACN,OAAO,KAAK,EAAE,+BAA+B,UAC7C,aAAa,eACb,2BAA2B,GACzB,OAAO,CAAC,OAAO,KAAK,EAAE,+BAA+B,CAAC,CAyBlE;;;;;;;;;;;;;;4BA9eyB,YAAY;sCAGF,YAAY;mCAFf,YAAY;gCAMf,YAAY;uCACL,YAAY;2CAHR,YAAY;iDADN,YAAY"}
package/src/policy.js CHANGED
@@ -3,6 +3,16 @@
3
3
 
4
4
  // @ts-check
5
5
 
6
+ /** @import {Policy} from './types.js' */
7
+ /** @import {PackagePolicy} from './types.js' */
8
+ /** @import {AttenuationDefinition} from './types.js' */
9
+ /** @import {PackageNamingKit} from './types.js' */
10
+ /** @import {DeferredAttenuatorsProvider} from './types.js' */
11
+ /** @import {CompartmentDescriptor} from './types.js' */
12
+ /** @import {Attenuator} from './types.js' */
13
+ /** @import {SomePolicy} from './types.js' */
14
+ /** @import {SomePackagePolicy} from './types.js' */
15
+
6
16
  import {
7
17
  getAttenuatorFromDefinition,
8
18
  isAllowingEverything,
@@ -10,7 +20,9 @@ import {
10
20
  policyLookupHelper,
11
21
  } from './policy-format.js';
12
22
 
13
- const { create, entries, values, assign, keys, freeze } = Object;
23
+ const { create, entries, values, assign, freeze, getOwnPropertyDescriptors } =
24
+ Object;
25
+ const { ownKeys } = Reflect;
14
26
  const q = JSON.stringify;
15
27
 
16
28
  /**
@@ -20,18 +32,25 @@ export const ATTENUATORS_COMPARTMENT = '<ATTENUATORS>';
20
32
 
21
33
  /**
22
34
  * Copies properties (optionally limited to a specific list) from one object to another.
23
- *
24
- * @param {object} from
25
- * @param {object} to
26
- * @param {Array<string | symbol>} [list]
27
- * @returns {object}
35
+ * @template {Record<PropertyKey, any>} T
36
+ * @template {Record<PropertyKey, any>} U
37
+ * @template {Array<Partial<keyof T>>} [K=Array<keyof T>]
38
+ * @param {T} from
39
+ * @param {U} to
40
+ * @param {K} [list]
41
+ * @returns {Omit<U, K[number]> & Pick<T, K[number]>}
28
42
  */
29
43
  const selectiveCopy = (from, to, list) => {
44
+ /** @type {Array<Partial<keyof T>>} */
45
+ let props;
30
46
  if (!list) {
31
- list = keys(from);
47
+ const descs = getOwnPropertyDescriptors(from);
48
+ props = ownKeys(from).filter(key => descs[key].enumerable);
49
+ } else {
50
+ props = list;
32
51
  }
33
- for (let index = 0; index < list.length; index += 1) {
34
- const key = list[index];
52
+ for (let index = 0; index < props.length; index += 1) {
53
+ const key = props[index];
35
54
  // If an endowment is missing, global value is undefined.
36
55
  // This is an expected behavior if globals are used for platform feature detection
37
56
  to[key] = from[key];
@@ -44,7 +63,7 @@ const selectiveCopy = (from, to, list) => {
44
63
  *
45
64
  * Note: this function is recursive
46
65
  * @param {string[]} attenuators - List of attenuator names; may be mutated
47
- * @param {import('./types.js').AttenuationDefinition|import('./types.js').Policy} policyFragment
66
+ * @param {AttenuationDefinition|Policy} policyFragment
48
67
  */
49
68
  const collectAttenuators = (attenuators, policyFragment) => {
50
69
  if ('attenuate' in policyFragment) {
@@ -63,7 +82,7 @@ const attenuatorsCache = new WeakMap();
63
82
  * Goes through policy and lists all attenuator specifiers used.
64
83
  * Memoization keyed on policy object reference
65
84
  *
66
- * @param {import('./types.js').Policy} [policy]
85
+ * @param {Policy} [policy]
67
86
  * @returns {Array<string>} attenuators
68
87
  */
69
88
  export const detectAttenuators = policy => {
@@ -84,7 +103,7 @@ export const detectAttenuators = policy => {
84
103
  /**
85
104
  * Generates a string identifying a package for policy lookup purposes.
86
105
  *
87
- * @param {import('./types.js').PackageNamingKit} namingKit
106
+ * @param {PackageNamingKit} namingKit
88
107
  * @returns {string}
89
108
  */
90
109
  const generateCanonicalName = ({ isEntry = false, name, path }) => {
@@ -101,8 +120,8 @@ const generateCanonicalName = ({ isEntry = false, name, path }) => {
101
120
  * Verifies if a module identified by `namingKit` can be a dependency of a package per `packagePolicy`.
102
121
  * `packagePolicy` is required, when policy is not set, skipping needs to be handled by the caller.
103
122
  *
104
- * @param {import('./types.js').PackageNamingKit} namingKit
105
- * @param {import('./types.js').PackagePolicy} packagePolicy
123
+ * @param {PackageNamingKit} namingKit
124
+ * @param {PackagePolicy} packagePolicy
106
125
  * @returns {boolean}
107
126
  */
108
127
  export const dependencyAllowedByPolicy = (namingKit, packagePolicy) => {
@@ -118,25 +137,25 @@ export const dependencyAllowedByPolicy = (namingKit, packagePolicy) => {
118
137
  * Returns the policy applicable to the canonicalName of the package
119
138
  *
120
139
  * @overload
121
- * @param {import('./types.js').PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
122
- * @param {import('./types.js').Policy} policy - user supplied policy
123
- * @returns {import('./types.js').PackagePolicy} packagePolicy if policy was specified
140
+ * @param {PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
141
+ * @param {SomePolicy} policy - user supplied policy
142
+ * @returns {SomePackagePolicy} packagePolicy if policy was specified
124
143
  */
125
144
 
126
145
  /**
127
146
  * Returns `undefined`
128
147
  *
129
148
  * @overload
130
- * @param {import('./types.js').PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
131
- * @param {import('./types.js').Policy} [policy] - user supplied policy
132
- * @returns {import('./types.js').PackagePolicy|undefined} packagePolicy if policy was specified
149
+ * @param {PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
150
+ * @param {SomePolicy} [policy] - user supplied policy
151
+ * @returns {SomePackagePolicy|undefined} packagePolicy if policy was specified
133
152
  */
134
153
 
135
154
  /**
136
155
  * Returns the policy applicable to the canonicalName of the package
137
156
  *
138
- * @param {import('./types.js').PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
139
- * @param {import('./types.js').Policy} [policy] - user supplied policy
157
+ * @param {PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
158
+ * @param {SomePolicy} [policy] - user supplied policy
140
159
  */
141
160
  export const getPolicyForPackage = (namingKit, policy) => {
142
161
  if (!policy) {
@@ -162,7 +181,7 @@ export const getPolicyForPackage = (namingKit, policy) => {
162
181
 
163
182
  /**
164
183
  * Get list of globals from package policy
165
- * @param {import('./types.js').PackagePolicy} [packagePolicy]
184
+ * @param {PackagePolicy} [packagePolicy]
166
185
  * @returns {Array<string>}
167
186
  */
168
187
  const getGlobalsList = packagePolicy => {
@@ -179,8 +198,8 @@ const MODULE_ATTENUATOR = 'attenuateModule';
179
198
 
180
199
  /**
181
200
  * Imports attenuator per its definition and provider
182
- * @param {import('./types.js').AttenuationDefinition} attenuationDefinition
183
- * @param {import('./types.js').DeferredAttenuatorsProvider} attenuatorsProvider
201
+ * @param {AttenuationDefinition} attenuationDefinition
202
+ * @param {DeferredAttenuatorsProvider} attenuatorsProvider
184
203
  * @param {string} attenuatorExportName
185
204
  * @returns {Promise<Function>}
186
205
  */
@@ -209,8 +228,8 @@ const importAttenuatorForDefinition = async (
209
228
  /**
210
229
  * Makes an async provider for attenuators
211
230
  * @param {Record<string, Compartment>} compartments
212
- * @param {Record<string, import('./types.js').CompartmentDescriptor>} compartmentDescriptors
213
- * @returns {import('./types.js').DeferredAttenuatorsProvider}
231
+ * @param {Record<string, CompartmentDescriptor>} compartmentDescriptors
232
+ * @returns {DeferredAttenuatorsProvider}
214
233
  */
215
234
  export const makeDeferredAttenuatorsProvider = (
216
235
  compartments,
@@ -234,7 +253,7 @@ export const makeDeferredAttenuatorsProvider = (
234
253
  /**
235
254
  *
236
255
  * @param {string} attenuatorSpecifier
237
- * @returns {Promise<import('./types.js').Attenuator>}
256
+ * @returns {Promise<Attenuator>}
238
257
  */
239
258
  importAttenuator = async attenuatorSpecifier => {
240
259
  if (!attenuatorSpecifier) {
@@ -258,8 +277,8 @@ export const makeDeferredAttenuatorsProvider = (
258
277
  * Attenuates the `globalThis` object
259
278
  *
260
279
  * @param {object} options
261
- * @param {import('./types.js').DeferredAttenuatorsProvider} options.attenuators
262
- * @param {import('./types.js').AttenuationDefinition} options.attenuationDefinition
280
+ * @param {DeferredAttenuatorsProvider} options.attenuators
281
+ * @param {AttenuationDefinition} options.attenuationDefinition
263
282
  * @param {object} options.globalThis
264
283
  * @param {object} options.globals
265
284
  */
@@ -298,8 +317,8 @@ async function attenuateGlobalThis({
298
317
  *
299
318
  * @param {object} globalThis
300
319
  * @param {object} globals
301
- * @param {import('./types.js').PackagePolicy} packagePolicy
302
- * @param {import('./types.js').DeferredAttenuatorsProvider} attenuators
320
+ * @param {PackagePolicy} packagePolicy
321
+ * @param {DeferredAttenuatorsProvider} attenuators
303
322
  * @param {Array<Promise>} pendingJobs
304
323
  * @param {string} name
305
324
  * @returns {void}
@@ -374,7 +393,7 @@ const diagnoseModulePolicy = errorHint => {
374
393
  * Throws if importing of the specifier is not allowed by the policy
375
394
  *
376
395
  * @param {string} specifier
377
- * @param {import('./types.js').CompartmentDescriptor} compartmentDescriptor
396
+ * @param {CompartmentDescriptor} compartmentDescriptor
378
397
  * @param {EnforceModulePolicyOptions} [options]
379
398
  */
380
399
  export const enforceModulePolicy = (
@@ -412,8 +431,8 @@ export const enforceModulePolicy = (
412
431
  /**
413
432
  * Attenuates a module
414
433
  * @param {object} options
415
- * @param {import('./types.js').DeferredAttenuatorsProvider} options.attenuators
416
- * @param {import('./types.js').AttenuationDefinition} options.attenuationDefinition
434
+ * @param {DeferredAttenuatorsProvider} options.attenuators
435
+ * @param {AttenuationDefinition} options.attenuationDefinition
417
436
  * @param {import('ses').ThirdPartyStaticModuleInterface} options.originalModuleRecord
418
437
  * @returns {Promise<import('ses').ThirdPartyStaticModuleInterface>}
419
438
  */
@@ -451,8 +470,8 @@ async function attenuateModule({
451
470
  *
452
471
  * @param {string} specifier - exit module name
453
472
  * @param {import('ses').ThirdPartyStaticModuleInterface} originalModuleRecord - reference to the exit module
454
- * @param {import('./types.js').PackagePolicy} policy - local compartment policy
455
- * @param {import('./types.js').DeferredAttenuatorsProvider} attenuators - a key-value where attenuations can be found
473
+ * @param {PackagePolicy} policy - local compartment policy
474
+ * @param {DeferredAttenuatorsProvider} attenuators - a key-value where attenuations can be found
456
475
  * @returns {Promise<import('ses').ThirdPartyStaticModuleInterface>} - the attenuated module
457
476
  */
458
477
  export const attenuateModuleHook = async (
package/src/powers.d.ts CHANGED
@@ -1,2 +1,9 @@
1
- export function unpackReadPowers(powers: import("./types.js").ReadFn | import("./types.js").ReadPowers | import("./types.js").MaybeReadPowers): import("./types.js").MaybeReadPowers;
1
+ export function unpackReadPowers(powers: ReadFn | ReadPowers | MaybeReadPowers): MaybeReadPowers;
2
+ export function isReadNowPowers(value: ReadPowers | ReadFn | undefined): value is ReadNowPowers;
3
+ export function findInvalidReadNowPowersProps(value?: ReadFn | ReadPowers | undefined): ReadNowPowersProp[];
4
+ import type { ReadFn } from './types.js';
5
+ import type { ReadPowers } from './types.js';
6
+ import type { MaybeReadPowers } from './types.js';
7
+ import type { ReadNowPowers } from './types.js';
8
+ import type { ReadNowPowersProp } from './types.js';
2
9
  //# sourceMappingURL=powers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"powers.d.ts","sourceRoot":"","sources":["powers.js"],"names":[],"mappings":"AAgBO,yCAHI,OAAO,YAAY,EAAE,MAAM,GAAG,OAAO,YAAY,EAAE,UAAU,GAAG,OAAO,YAAY,EAAE,eAAe,GAClG,OAAO,YAAY,EAAE,eAAe,CAkChD"}
1
+ {"version":3,"file":"powers.d.ts","sourceRoot":"","sources":["powers.js"],"names":[],"mappings":"AA0BO,yCAHI,MAAM,GAAG,UAAU,GAAG,eAAe,GACnC,eAAe,CAgC3B;AAiBM,uCAHI,UAAU,GAAC,MAAM,GAAC,SAAS,GACzB,KAAK,IAAI,aAAa,CAShC;AAWI,wFAFM,iBAAiB,EAAE,CAS/B;4BAtFyB,YAAY;gCACR,YAAY;qCACP,YAAY;mCAJd,YAAY;uCACR,YAAY"}
package/src/powers.js CHANGED
@@ -7,25 +7,35 @@
7
7
 
8
8
  // @ts-check
9
9
 
10
- /** @type {import('./types.js').CanonicalFn} */
10
+ /** @import {CanonicalFn} from './types.js' */
11
+ /** @import {ReadNowPowers} from './types.js' */
12
+ /** @import {ReadNowPowersProp} from './types.js' */
13
+ /** @import {ReadFn} from './types.js' */
14
+ /** @import {ReadPowers} from './types.js' */
15
+ /** @import {MaybeReadPowers} from './types.js' */
16
+ /** @import {MaybeReadFn} from './types.js' */
17
+
18
+ const { freeze } = Object;
19
+
20
+ /** @type {CanonicalFn} */
11
21
  const canonicalShim = async path => path;
12
22
 
13
23
  /**
14
- * @param {import('./types.js').ReadFn | import('./types.js').ReadPowers | import('./types.js').MaybeReadPowers} powers
15
- * @returns {import('./types.js').MaybeReadPowers}
24
+ * @param {ReadFn | ReadPowers | MaybeReadPowers} powers
25
+ * @returns {MaybeReadPowers}
16
26
  */
17
27
  export const unpackReadPowers = powers => {
18
- /** @type {import('./types.js').ReadFn | undefined} */
28
+ /** @type {ReadFn | undefined} */
19
29
  let read;
20
- /** @type {import('./types.js').MaybeReadFn | undefined} */
30
+ /** @type {MaybeReadFn | undefined} */
21
31
  let maybeRead;
22
- /** @type {import('./types.js').CanonicalFn | undefined} */
32
+ /** @type {CanonicalFn | undefined} */
23
33
  let canonical;
24
34
 
25
35
  if (typeof powers === 'function') {
26
36
  read = powers;
27
37
  } else {
28
- ({ read, maybeRead, canonical } = powers);
38
+ ({ read, maybeRead, canonical } = /** @type {MaybeReadPowers} */ (powers));
29
39
  }
30
40
 
31
41
  if (canonical === undefined) {
@@ -35,9 +45,7 @@ export const unpackReadPowers = powers => {
35
45
  if (maybeRead === undefined) {
36
46
  /** @param {string} path */
37
47
  maybeRead = path =>
38
- /** @type {import('./types.js').ReadFn} */ (read)(path).catch(
39
- _error => undefined,
40
- );
48
+ /** @type {ReadFn} */ (read)(path).catch(_error => undefined);
41
49
  }
42
50
 
43
51
  return {
@@ -47,3 +55,45 @@ export const unpackReadPowers = powers => {
47
55
  canonical,
48
56
  };
49
57
  };
58
+
59
+ /**
60
+ * Ordered array of every property in {@link ReadNowPowers} which is _required_.
61
+ *
62
+ * @satisfies {Readonly<{[K in ReadNowPowersProp]-?: {} extends Pick<ReadNowPowers, K> ? never : K}[ReadNowPowersProp][]>}
63
+ */
64
+ const requiredReadNowPowersProps = freeze(
65
+ /** @type {const} */ (['fileURLToPath', 'isAbsolute', 'maybeReadNow']),
66
+ );
67
+
68
+ /**
69
+ * Returns `true` if `value` is a {@link ReadNowPowers}
70
+ *
71
+ * @param {ReadPowers|ReadFn|undefined} value
72
+ * @returns {value is ReadNowPowers}
73
+ */
74
+ export const isReadNowPowers = value =>
75
+ Boolean(
76
+ value &&
77
+ typeof value === 'object' &&
78
+ requiredReadNowPowersProps.every(
79
+ prop => prop in value && typeof value[prop] === 'function',
80
+ ),
81
+ );
82
+
83
+ /**
84
+ * Returns a list of the properties missing from (or invalid within) `value` that are required for
85
+ * `value` to be a {@link ReadNowPowers}.
86
+ *
87
+ * Used for human-friendly error messages
88
+ *
89
+ * @param {ReadPowers | ReadFn} [value] The value to check for missing properties.
90
+ * @returns {ReadNowPowersProp[]}
91
+ */
92
+ export const findInvalidReadNowPowersProps = value => {
93
+ if (!value || typeof value === 'function') {
94
+ return [...requiredReadNowPowersProps];
95
+ }
96
+ return requiredReadNowPowersProps.filter(
97
+ prop => !(prop in value) || typeof value[prop] !== 'function',
98
+ );
99
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["search.js"],"names":[],"mappings":"AAqCO,iCALM,CAAC,YACH,MAAM,uBACN,CAAC,QAAQ,EAAC,MAAM,KAAG,OAAO,CAAC,CAAC,GAAC,SAAS,CAAC,GACrC,OAAO,CAAC;IAAC,IAAI,EAAC,CAAC,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAC,MAAM,CAAC;IAAC,yBAAyB,EAAE,MAAM,CAAA;CAAC,CAAC,CA0BpG;AAoCM,mCATI,MAAM,GAAG,UAAU,GAAG,eAAe,kBACrC,MAAM,GACJ,OAAO,CAAC;IACpB,eAAmB,EAAE,MAAM,CAAC;IAC5B,yBAA6B,EAAE,MAAM,CAAC;IACtC,qBAAyB,EAAE,MAAM,CAAC;IAClC,eAAmB,EAAE,MAAM,CAAC;CACzB,CAAC,CAqBJ;4BA9GyB,YAAY;gCACR,YAAY;qCACP,YAAY"}
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["search.js"],"names":[],"mappings":"AAqCO,iCALM,CAAC,YACH,MAAM,uBACN,CAAC,QAAQ,EAAC,MAAM,KAAG,OAAO,CAAC,CAAC,GAAC,SAAS,CAAC,GACrC,OAAO,CAAC;IAAC,IAAI,EAAC,CAAC,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAC,MAAM,CAAC;IAAC,yBAAyB,EAAE,MAAM,CAAA;CAAC,CAAC,CA0BpG;AAoCM,mCATI,MAAM,GAAG,UAAU,GAAG,eAAe,kBACrC,MAAM,GACJ,OAAO,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,yBAAyB,EAAE,MAAM,CAAC;IAClC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC,CAoBJ;4BA7GyB,YAAY;gCACR,YAAY;qCACP,YAAY"}
package/src/search.js CHANGED
@@ -96,10 +96,9 @@ const maybeReadDescriptorDefault = async (
96
96
  * }>}
97
97
  */
98
98
  export const search = async (readPowers, moduleLocation) => {
99
- const { maybeRead } = unpackReadPowers(readPowers);
100
99
  const { data, directory, location, packageDescriptorLocation } =
101
100
  await searchDescriptor(moduleLocation, loc =>
102
- maybeReadDescriptorDefault(maybeRead, loc),
101
+ maybeReadDescriptorDefault(readPowers, loc),
103
102
  );
104
103
 
105
104
  if (!data) {