@endo/compartment-mapper 1.2.2 → 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 +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
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":"AAiBA;;GAEG;AACH,sDAAuD;AAyDhD,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,
@@ -21,26 +31,26 @@ const q = JSON.stringify;
21
31
  export const ATTENUATORS_COMPARTMENT = '<ATTENUATORS>';
22
32
 
23
33
  /**
24
- * Copies properties (optionally limited to a specific list) from one object
25
- * to another. By default, copies all enumerable, own, string- and
26
- * symbol-named properties.
27
- *
28
- * @param {object} from
29
- * @param {object} to
30
- * @param {Array<string | symbol>} [list]
31
- * @returns {object}
34
+ * Copies properties (optionally limited to a specific list) from one object to another.
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]>}
32
42
  */
33
43
  const selectiveCopy = (from, to, list) => {
44
+ /** @type {Array<Partial<keyof T>>} */
45
+ let props;
34
46
  if (!list) {
35
47
  const descs = getOwnPropertyDescriptors(from);
36
- list = ownKeys(from).filter(
37
- key =>
38
- // @ts-expect-error TypeScript still confused about a symbol as index
39
- descs[key].enumerable,
40
- );
48
+ props = ownKeys(from).filter(key => descs[key].enumerable);
49
+ } else {
50
+ props = list;
41
51
  }
42
- for (let index = 0; index < list.length; index += 1) {
43
- const key = list[index];
52
+ for (let index = 0; index < props.length; index += 1) {
53
+ const key = props[index];
44
54
  // If an endowment is missing, global value is undefined.
45
55
  // This is an expected behavior if globals are used for platform feature detection
46
56
  to[key] = from[key];
@@ -53,7 +63,7 @@ const selectiveCopy = (from, to, list) => {
53
63
  *
54
64
  * Note: this function is recursive
55
65
  * @param {string[]} attenuators - List of attenuator names; may be mutated
56
- * @param {import('./types.js').AttenuationDefinition|import('./types.js').Policy} policyFragment
66
+ * @param {AttenuationDefinition|Policy} policyFragment
57
67
  */
58
68
  const collectAttenuators = (attenuators, policyFragment) => {
59
69
  if ('attenuate' in policyFragment) {
@@ -72,7 +82,7 @@ const attenuatorsCache = new WeakMap();
72
82
  * Goes through policy and lists all attenuator specifiers used.
73
83
  * Memoization keyed on policy object reference
74
84
  *
75
- * @param {import('./types.js').Policy} [policy]
85
+ * @param {Policy} [policy]
76
86
  * @returns {Array<string>} attenuators
77
87
  */
78
88
  export const detectAttenuators = policy => {
@@ -93,7 +103,7 @@ export const detectAttenuators = policy => {
93
103
  /**
94
104
  * Generates a string identifying a package for policy lookup purposes.
95
105
  *
96
- * @param {import('./types.js').PackageNamingKit} namingKit
106
+ * @param {PackageNamingKit} namingKit
97
107
  * @returns {string}
98
108
  */
99
109
  const generateCanonicalName = ({ isEntry = false, name, path }) => {
@@ -110,8 +120,8 @@ const generateCanonicalName = ({ isEntry = false, name, path }) => {
110
120
  * Verifies if a module identified by `namingKit` can be a dependency of a package per `packagePolicy`.
111
121
  * `packagePolicy` is required, when policy is not set, skipping needs to be handled by the caller.
112
122
  *
113
- * @param {import('./types.js').PackageNamingKit} namingKit
114
- * @param {import('./types.js').PackagePolicy} packagePolicy
123
+ * @param {PackageNamingKit} namingKit
124
+ * @param {PackagePolicy} packagePolicy
115
125
  * @returns {boolean}
116
126
  */
117
127
  export const dependencyAllowedByPolicy = (namingKit, packagePolicy) => {
@@ -127,25 +137,25 @@ export const dependencyAllowedByPolicy = (namingKit, packagePolicy) => {
127
137
  * Returns the policy applicable to the canonicalName of the package
128
138
  *
129
139
  * @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} 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
133
143
  */
134
144
 
135
145
  /**
136
146
  * Returns `undefined`
137
147
  *
138
148
  * @overload
139
- * @param {import('./types.js').PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
140
- * @param {import('./types.js').Policy} [policy] - user supplied policy
141
- * @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
142
152
  */
143
153
 
144
154
  /**
145
155
  * Returns the policy applicable to the canonicalName of the package
146
156
  *
147
- * @param {import('./types.js').PackageNamingKit} namingKit - a key in the policy resources spec is derived from these
148
- * @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
149
159
  */
150
160
  export const getPolicyForPackage = (namingKit, policy) => {
151
161
  if (!policy) {
@@ -171,7 +181,7 @@ export const getPolicyForPackage = (namingKit, policy) => {
171
181
 
172
182
  /**
173
183
  * Get list of globals from package policy
174
- * @param {import('./types.js').PackagePolicy} [packagePolicy]
184
+ * @param {PackagePolicy} [packagePolicy]
175
185
  * @returns {Array<string>}
176
186
  */
177
187
  const getGlobalsList = packagePolicy => {
@@ -188,8 +198,8 @@ const MODULE_ATTENUATOR = 'attenuateModule';
188
198
 
189
199
  /**
190
200
  * Imports attenuator per its definition and provider
191
- * @param {import('./types.js').AttenuationDefinition} attenuationDefinition
192
- * @param {import('./types.js').DeferredAttenuatorsProvider} attenuatorsProvider
201
+ * @param {AttenuationDefinition} attenuationDefinition
202
+ * @param {DeferredAttenuatorsProvider} attenuatorsProvider
193
203
  * @param {string} attenuatorExportName
194
204
  * @returns {Promise<Function>}
195
205
  */
@@ -218,8 +228,8 @@ const importAttenuatorForDefinition = async (
218
228
  /**
219
229
  * Makes an async provider for attenuators
220
230
  * @param {Record<string, Compartment>} compartments
221
- * @param {Record<string, import('./types.js').CompartmentDescriptor>} compartmentDescriptors
222
- * @returns {import('./types.js').DeferredAttenuatorsProvider}
231
+ * @param {Record<string, CompartmentDescriptor>} compartmentDescriptors
232
+ * @returns {DeferredAttenuatorsProvider}
223
233
  */
224
234
  export const makeDeferredAttenuatorsProvider = (
225
235
  compartments,
@@ -243,7 +253,7 @@ export const makeDeferredAttenuatorsProvider = (
243
253
  /**
244
254
  *
245
255
  * @param {string} attenuatorSpecifier
246
- * @returns {Promise<import('./types.js').Attenuator>}
256
+ * @returns {Promise<Attenuator>}
247
257
  */
248
258
  importAttenuator = async attenuatorSpecifier => {
249
259
  if (!attenuatorSpecifier) {
@@ -267,8 +277,8 @@ export const makeDeferredAttenuatorsProvider = (
267
277
  * Attenuates the `globalThis` object
268
278
  *
269
279
  * @param {object} options
270
- * @param {import('./types.js').DeferredAttenuatorsProvider} options.attenuators
271
- * @param {import('./types.js').AttenuationDefinition} options.attenuationDefinition
280
+ * @param {DeferredAttenuatorsProvider} options.attenuators
281
+ * @param {AttenuationDefinition} options.attenuationDefinition
272
282
  * @param {object} options.globalThis
273
283
  * @param {object} options.globals
274
284
  */
@@ -307,8 +317,8 @@ async function attenuateGlobalThis({
307
317
  *
308
318
  * @param {object} globalThis
309
319
  * @param {object} globals
310
- * @param {import('./types.js').PackagePolicy} packagePolicy
311
- * @param {import('./types.js').DeferredAttenuatorsProvider} attenuators
320
+ * @param {PackagePolicy} packagePolicy
321
+ * @param {DeferredAttenuatorsProvider} attenuators
312
322
  * @param {Array<Promise>} pendingJobs
313
323
  * @param {string} name
314
324
  * @returns {void}
@@ -383,7 +393,7 @@ const diagnoseModulePolicy = errorHint => {
383
393
  * Throws if importing of the specifier is not allowed by the policy
384
394
  *
385
395
  * @param {string} specifier
386
- * @param {import('./types.js').CompartmentDescriptor} compartmentDescriptor
396
+ * @param {CompartmentDescriptor} compartmentDescriptor
387
397
  * @param {EnforceModulePolicyOptions} [options]
388
398
  */
389
399
  export const enforceModulePolicy = (
@@ -421,8 +431,8 @@ export const enforceModulePolicy = (
421
431
  /**
422
432
  * Attenuates a module
423
433
  * @param {object} options
424
- * @param {import('./types.js').DeferredAttenuatorsProvider} options.attenuators
425
- * @param {import('./types.js').AttenuationDefinition} options.attenuationDefinition
434
+ * @param {DeferredAttenuatorsProvider} options.attenuators
435
+ * @param {AttenuationDefinition} options.attenuationDefinition
426
436
  * @param {import('ses').ThirdPartyStaticModuleInterface} options.originalModuleRecord
427
437
  * @returns {Promise<import('ses').ThirdPartyStaticModuleInterface>}
428
438
  */
@@ -460,8 +470,8 @@ async function attenuateModule({
460
470
  *
461
471
  * @param {string} specifier - exit module name
462
472
  * @param {import('ses').ThirdPartyStaticModuleInterface} originalModuleRecord - reference to the exit module
463
- * @param {import('./types.js').PackagePolicy} policy - local compartment policy
464
- * @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
465
475
  * @returns {Promise<import('ses').ThirdPartyStaticModuleInterface>} - the attenuated module
466
476
  */
467
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;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,yBAAyB,EAAE,MAAM,CAAC;IAClC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,eAAe,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) {