@agoric/internal 0.4.0-u19.2 → 0.4.0-u20.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.
- package/package.json +19 -16
- package/src/config.d.ts +2 -4
- package/src/config.js +4 -2
- package/src/hex.d.ts +15 -0
- package/src/hex.d.ts.map +1 -0
- package/src/hex.js +105 -0
- package/src/index.d.ts +2 -3
- package/src/index.js +2 -4
- package/src/js-utils.d.ts +29 -4
- package/src/js-utils.d.ts.map +1 -1
- package/src/js-utils.js +144 -23
- package/src/metrics.d.ts +183 -0
- package/src/metrics.d.ts.map +1 -0
- package/src/metrics.js +476 -0
- package/src/module-utils.d.ts +2 -0
- package/src/module-utils.d.ts.map +1 -0
- package/src/module-utils.js +27 -0
- package/src/ses-utils.d.ts +7 -0
- package/src/ses-utils.d.ts.map +1 -1
- package/src/ses-utils.js +66 -2
- package/src/tmpDir.d.ts +2 -0
- package/src/tmpDir.d.ts.map +1 -0
- package/src/tmpDir.js +17 -0
- package/src/types.d.ts +12 -0
- package/src/types.d.ts.map +1 -1
- package/src/types.ts +21 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agoric/internal",
|
|
3
|
-
"version": "0.4.0-
|
|
3
|
+
"version": "0.4.0-u20.0",
|
|
4
4
|
"description": "Externally unsupported utilities internal to agoric-sdk",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -20,23 +20,26 @@
|
|
|
20
20
|
"lint:types": "tsc"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@agoric/base-zone": "^0.1.1-
|
|
24
|
-
"@endo/common": "^1.2.
|
|
25
|
-
"@endo/
|
|
26
|
-
"@endo/
|
|
27
|
-
"@endo/
|
|
28
|
-
"@endo/
|
|
29
|
-
"@endo/
|
|
30
|
-
"@endo/
|
|
31
|
-
"@endo/
|
|
32
|
-
"@endo/
|
|
23
|
+
"@agoric/base-zone": "^0.1.1-u20.0",
|
|
24
|
+
"@endo/common": "^1.2.10",
|
|
25
|
+
"@endo/compartment-mapper": "^1.6.0",
|
|
26
|
+
"@endo/errors": "^1.2.10",
|
|
27
|
+
"@endo/far": "^1.1.11",
|
|
28
|
+
"@endo/init": "^1.1.9",
|
|
29
|
+
"@endo/marshal": "^1.6.4",
|
|
30
|
+
"@endo/nat": "^5.1.0",
|
|
31
|
+
"@endo/pass-style": "^1.5.0",
|
|
32
|
+
"@endo/patterns": "^1.5.0",
|
|
33
|
+
"@endo/promise-kit": "^1.1.10",
|
|
34
|
+
"@endo/stream": "^1.2.10",
|
|
33
35
|
"anylogger": "^0.21.0",
|
|
34
36
|
"jessie.js": "^0.3.4"
|
|
35
37
|
},
|
|
36
38
|
"devDependencies": {
|
|
37
|
-
"@agoric/cosmic-proto": "^0.5.0-
|
|
38
|
-
"@endo/exo": "^1.5.
|
|
39
|
-
"@endo/init": "^1.1.
|
|
39
|
+
"@agoric/cosmic-proto": "^0.5.0-u20.0",
|
|
40
|
+
"@endo/exo": "^1.5.9",
|
|
41
|
+
"@endo/init": "^1.1.9",
|
|
42
|
+
"@fast-check/ava": "^2.0.1",
|
|
40
43
|
"ava": "^5.3.0",
|
|
41
44
|
"tsd": "^0.31.1"
|
|
42
45
|
},
|
|
@@ -58,7 +61,7 @@
|
|
|
58
61
|
"access": "public"
|
|
59
62
|
},
|
|
60
63
|
"typeCoverage": {
|
|
61
|
-
"atLeast":
|
|
64
|
+
"atLeast": 92.65
|
|
62
65
|
},
|
|
63
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "8e4207fa19dabf76c1f91f8779b5b5b93570ecea"
|
|
64
67
|
}
|
package/src/config.d.ts
CHANGED
|
@@ -14,10 +14,8 @@ export namespace BridgeId {
|
|
|
14
14
|
let WALLET: "wallet";
|
|
15
15
|
}
|
|
16
16
|
export namespace CosmosInitKeyToBridgeId {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
import vibcPort = DIBC;
|
|
20
|
-
export { vibcPort };
|
|
17
|
+
let vbankPort: "bank";
|
|
18
|
+
let vibcPort: "dibc";
|
|
21
19
|
}
|
|
22
20
|
export namespace WalletName {
|
|
23
21
|
let depositFacet: "depositFacet";
|
package/src/config.js
CHANGED
|
@@ -31,10 +31,12 @@ export const BridgeId = /** @type {const} */ ({
|
|
|
31
31
|
});
|
|
32
32
|
harden(BridgeId);
|
|
33
33
|
|
|
34
|
+
/** @satisfies {Record<string, BridgeId>} */
|
|
34
35
|
export const CosmosInitKeyToBridgeId = {
|
|
35
|
-
vbankPort:
|
|
36
|
-
vibcPort:
|
|
36
|
+
vbankPort: 'bank',
|
|
37
|
+
vibcPort: 'dibc',
|
|
37
38
|
};
|
|
39
|
+
harden(CosmosInitKeyToBridgeId);
|
|
38
40
|
|
|
39
41
|
export const WalletName = /** @type {const} */ ({
|
|
40
42
|
depositFacet: 'depositFacet',
|
package/src/hex.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function makePortableHexCodec(): HexCodec;
|
|
2
|
+
export function makeBufferishHexCodec(Bufferish: BufferishConstructor): HexCodec;
|
|
3
|
+
export const encodeHex: (buf: Uint8Array) => string;
|
|
4
|
+
export const decodeHex: (hex: string) => Uint8Array;
|
|
5
|
+
export type HexCodec = {
|
|
6
|
+
encodeHex: (buf: Uint8Array) => string;
|
|
7
|
+
decodeHex: (hex: string) => Uint8Array;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* is the portion of the Node.js Buffer API we need for hex conversion.
|
|
11
|
+
*/
|
|
12
|
+
export type BufferishConstructor = Pick<BufferConstructor, "from" | "isBuffer"> & {
|
|
13
|
+
prototype: Pick<Buffer, "toString"> & Uint8Array;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=hex.d.ts.map
|
package/src/hex.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["hex.js"],"names":[],"mappings":"AAsCO,wCAFM,QAAQ,CAyBpB;AAgBM,iDAJI,oBAAoB,GAElB,QAAQ,CAoBpB;8BA7FmB,UAAU,KAAK,MAAM;8BACrB,MAAM,KAAK,UAAU;;eAD3B,CAAC,GAAG,EAAE,UAAU,KAAK,MAAM;eAC3B,CAAC,GAAG,EAAE,MAAM,KAAK,UAAU;;;;;mCA6D5B,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,UAAU,CAAC,GAAG;IACvD,SAAS,EAAE,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,UAAU,CAAC;CAClD"}
|
package/src/hex.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} HexCodec
|
|
3
|
+
* @property {(buf: Uint8Array) => string} encodeHex
|
|
4
|
+
* @property {(hex: string) => Uint8Array} decodeHex
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** @type {string[]} */
|
|
8
|
+
const encodings = Array.from({ length: 256 }, (_, b) =>
|
|
9
|
+
// Write the hex representation of the byte.
|
|
10
|
+
b.toString(16).padStart(2, '0'),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Create map entries for all four permutations of lowercase and uppercase
|
|
15
|
+
* transformations of the two hex digits per byte. The map is keyed by the hex
|
|
16
|
+
* string and the value is the byte value. This allows for fast lookups when
|
|
17
|
+
* decoding hex strings.
|
|
18
|
+
*
|
|
19
|
+
* @type {Map<string, number>}
|
|
20
|
+
*/
|
|
21
|
+
const decodings = new Map(
|
|
22
|
+
encodings.flatMap((hexdigits, b) => {
|
|
23
|
+
const lo = hexdigits.toLowerCase();
|
|
24
|
+
const UP = hexdigits.toUpperCase();
|
|
25
|
+
return [
|
|
26
|
+
[lo, b],
|
|
27
|
+
[`${lo[0]}${UP[1]}`, b],
|
|
28
|
+
[`${UP[0]}${lo[1]}`, b],
|
|
29
|
+
[UP, b],
|
|
30
|
+
];
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Create a hex codec that is portable across standard JS environments.
|
|
36
|
+
*
|
|
37
|
+
* @returns {HexCodec}
|
|
38
|
+
*/
|
|
39
|
+
export const makePortableHexCodec = () => {
|
|
40
|
+
/** @type {HexCodec} */
|
|
41
|
+
const portableHexCodec = {
|
|
42
|
+
encodeHex: buf => Array.from(buf, b => encodings[b]).join(''),
|
|
43
|
+
decodeHex: hex => {
|
|
44
|
+
const inputLen = hex.length;
|
|
45
|
+
if (inputLen % 2 !== 0) {
|
|
46
|
+
throw new Error(`Invalid hex string: ${hex}`);
|
|
47
|
+
}
|
|
48
|
+
const buf = new Uint8Array(inputLen / 2);
|
|
49
|
+
for (let i = 0; i < inputLen; i += 2) {
|
|
50
|
+
const b = decodings.get(hex.slice(i, i + 2));
|
|
51
|
+
if (b === undefined) {
|
|
52
|
+
throw new Error(`Invalid hex string: ${hex}`);
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line no-bitwise
|
|
55
|
+
buf[i >> 1] = b;
|
|
56
|
+
}
|
|
57
|
+
return buf;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return portableHexCodec;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @typedef {Pick<BufferConstructor, 'from' | 'isBuffer'> & {
|
|
66
|
+
* prototype: Pick<Buffer, 'toString'> & Uint8Array;
|
|
67
|
+
* }} BufferishConstructor
|
|
68
|
+
* is the portion of the Node.js Buffer API we need for hex conversion.
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Create a hex codec using parts of the Node.js Buffer API.
|
|
73
|
+
*
|
|
74
|
+
* @param {BufferishConstructor} Bufferish the object that implements the
|
|
75
|
+
* necessary pieces of Buffer
|
|
76
|
+
* @returns {HexCodec}
|
|
77
|
+
*/
|
|
78
|
+
export const makeBufferishHexCodec = Bufferish => {
|
|
79
|
+
/** @type {HexCodec} */
|
|
80
|
+
const attenuatedBufferHexCodec = {
|
|
81
|
+
encodeHex: buf =>
|
|
82
|
+
(Bufferish.isBuffer?.(buf) ? buf : Bufferish.from(buf)).toString('hex'),
|
|
83
|
+
decodeHex: hex => {
|
|
84
|
+
const buf = Bufferish.from(hex, 'hex');
|
|
85
|
+
|
|
86
|
+
// Coerce to Uint8Array to avoid leaking the abstraction.
|
|
87
|
+
const u8a = new Uint8Array(
|
|
88
|
+
buf.buffer,
|
|
89
|
+
buf.byteOffset,
|
|
90
|
+
buf.byteLength / Uint8Array.BYTES_PER_ELEMENT,
|
|
91
|
+
);
|
|
92
|
+
return u8a;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
return attenuatedBufferHexCodec;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Export a hex codec that can work with standard JS engines, but takes
|
|
100
|
+
* advantage of optimizations on some platforms (like Node.js's Buffer API).
|
|
101
|
+
*/
|
|
102
|
+
export const { encodeHex, decodeHex } =
|
|
103
|
+
typeof Buffer === 'undefined'
|
|
104
|
+
? makePortableHexCodec()
|
|
105
|
+
: makeBufferishHexCodec(Buffer);
|
package/src/index.d.ts
CHANGED
|
@@ -3,12 +3,11 @@ export * from "./debug.js";
|
|
|
3
3
|
export * from "./errors.js";
|
|
4
4
|
export * from "./js-utils.js";
|
|
5
5
|
export * from "./method-tools.js";
|
|
6
|
+
export * from "./metrics.js";
|
|
6
7
|
export * from "./ses-utils.js";
|
|
8
|
+
export * from "./tmpDir.js";
|
|
7
9
|
export * from "./typeCheck.js";
|
|
8
10
|
export * from "./typeGuards.js";
|
|
9
11
|
export * from "./types-index.js";
|
|
10
12
|
export { pureDataMarshaller } from "./marshal.js";
|
|
11
|
-
export { objectMap } from "@endo/common/object-map.js";
|
|
12
|
-
export { objectMetaMap } from "@endo/common/object-meta-map.js";
|
|
13
|
-
export { fromUniqueEntries } from "@endo/common/from-unique-entries.js";
|
|
14
13
|
//# sourceMappingURL=index.d.ts.map
|
package/src/index.js
CHANGED
|
@@ -8,13 +8,11 @@ export * from './errors.js';
|
|
|
8
8
|
export * from './js-utils.js';
|
|
9
9
|
export { pureDataMarshaller } from './marshal.js';
|
|
10
10
|
export * from './method-tools.js';
|
|
11
|
+
export * from './metrics.js';
|
|
11
12
|
export * from './ses-utils.js';
|
|
13
|
+
export * from './tmpDir.js';
|
|
12
14
|
export * from './typeCheck.js';
|
|
13
15
|
export * from './typeGuards.js';
|
|
14
16
|
|
|
15
17
|
// eslint-disable-next-line import/export -- just types
|
|
16
18
|
export * from './types-index.js';
|
|
17
|
-
|
|
18
|
-
export { objectMap } from '@endo/common/object-map.js';
|
|
19
|
-
export { objectMetaMap } from '@endo/common/object-meta-map.js';
|
|
20
|
-
export { fromUniqueEntries } from '@endo/common/from-unique-entries.js';
|
package/src/js-utils.d.ts
CHANGED
|
@@ -1,15 +1,40 @@
|
|
|
1
|
+
/** For overriding TypeScript inferring the type of `true` as boolean. */
|
|
2
|
+
export const TRUE: true;
|
|
1
3
|
/**
|
|
2
|
-
* @
|
|
3
|
-
*
|
|
4
|
+
* @typedef {<O extends Record<string, unknown>>(
|
|
5
|
+
* obj: O,
|
|
6
|
+
* ) => { [K in keyof O]: K extends string ? [K, O[K]] : never }[keyof O][]} TypedEntries
|
|
4
7
|
*/
|
|
8
|
+
export const typedEntries: TypedEntries;
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {<
|
|
11
|
+
* const Entries extends ReadonlyArray<readonly [PropertyKey, unknown]>,
|
|
12
|
+
* >(
|
|
13
|
+
* entries: Entries,
|
|
14
|
+
* ) => { [Entry in Entries[number] as Entry[0]]: Entry[1] }} FromTypedEntries
|
|
15
|
+
*/
|
|
16
|
+
export const fromTypedEntries: FromTypedEntries;
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {<A extends unknown[], V>(
|
|
19
|
+
* arr: A,
|
|
20
|
+
* mapper: <K extends number>(el: A[K], idx: K, arr: A) => V,
|
|
21
|
+
* ) => V[]} TypedMap
|
|
22
|
+
*/
|
|
23
|
+
export const typedMap: TypedMap;
|
|
5
24
|
export const logLevels: readonly ["debug", "log", "info", "warn", "error"];
|
|
6
25
|
export function deepCopyJsonable<T>(value: T): T;
|
|
7
|
-
export function deepMapObject(obj:
|
|
8
|
-
export function
|
|
26
|
+
export function deepMapObject<O extends Record<string, unknown>, M>(obj: O, mapper: <T extends Record<string, unknown>, K extends string & keyof T>(value: T[K], name: K, record: T) => T[K] | M): O | { [K_1 in keyof O]: K_1 extends string ? O[K_1] | M : never; };
|
|
27
|
+
export function defineName<F extends Function>(name: string, fn: F): F;
|
|
28
|
+
export function objectMapMutable<O extends Record<string, unknown>, M>(obj: O, mapper: <K extends keyof O>(value: O[K], key: K) => M): { [K in keyof O]: K extends string ? M : never; };
|
|
29
|
+
export function provideLazyMap<K, V>(map: K extends WeakKey ? WeakMap<K, V> : Map<K, V>, key: K, makeValue: (key: K) => V): V;
|
|
9
30
|
export function makeMeasureSeconds(currentTimeMillisec: () => number): <T>(fn: () => Promise<T>) => Promise<{
|
|
10
31
|
result: T;
|
|
11
32
|
duration: number;
|
|
12
33
|
}>;
|
|
34
|
+
export function unprefixedProperties<P extends string, K extends string, V>(obj: Record<`${P}${K}`, V>, prefix: P): Record<K, V>;
|
|
35
|
+
export type TypedEntries = <O extends Record<string, unknown>>(obj: O) => { [K in keyof O]: K extends string ? [K, O[K]] : never; }[keyof O][];
|
|
36
|
+
export type FromTypedEntries = <const Entries extends ReadonlyArray<readonly [PropertyKey, unknown]>>(entries: Entries) => { [Entry in Entries[number] as Entry[0]]: Entry[1]; };
|
|
37
|
+
export type TypedMap = <A extends unknown[], V>(arr: A, mapper: <K extends number>(el: A[K], idx: K, arr: A) => V) => V[];
|
|
13
38
|
export type LogLevel = (typeof logLevels)[keyof readonly ["debug", "log", "info", "warn", "error"] & number];
|
|
14
39
|
export type LimitedConsole = Pick<Console, LogLevel>;
|
|
15
40
|
//# sourceMappingURL=js-utils.d.ts.map
|
package/src/js-utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"js-utils.d.ts","sourceRoot":"","sources":["js-utils.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"js-utils.d.ts","sourceRoot":"","sources":["js-utils.js"],"names":[],"mappings":"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"}
|
package/src/js-utils.js
CHANGED
|
@@ -5,6 +5,39 @@
|
|
|
5
5
|
* dependent upon a hardened environment.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
const { defineProperty } = Object;
|
|
9
|
+
|
|
10
|
+
/** For overriding TypeScript inferring the type of `true` as boolean. */
|
|
11
|
+
export const TRUE = /** @type {const} */ (true);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {<O extends Record<string, unknown>>(
|
|
15
|
+
* obj: O,
|
|
16
|
+
* ) => { [K in keyof O]: K extends string ? [K, O[K]] : never }[keyof O][]} TypedEntries
|
|
17
|
+
*/
|
|
18
|
+
export const typedEntries = /** @type {TypedEntries} */ (Object.entries);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {<
|
|
22
|
+
* const Entries extends ReadonlyArray<readonly [PropertyKey, unknown]>,
|
|
23
|
+
* >(
|
|
24
|
+
* entries: Entries,
|
|
25
|
+
* ) => { [Entry in Entries[number] as Entry[0]]: Entry[1] }} FromTypedEntries
|
|
26
|
+
*/
|
|
27
|
+
export const fromTypedEntries = /** @type {FromTypedEntries} */ (
|
|
28
|
+
Object.fromEntries
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {<A extends unknown[], V>(
|
|
33
|
+
* arr: A,
|
|
34
|
+
* mapper: <K extends number>(el: A[K], idx: K, arr: A) => V,
|
|
35
|
+
* ) => V[]} TypedMap
|
|
36
|
+
*/
|
|
37
|
+
export const typedMap = /** @type {TypedMap} */ (
|
|
38
|
+
Function.prototype.call.bind(Array.prototype.map)
|
|
39
|
+
);
|
|
40
|
+
|
|
8
41
|
export const logLevels = /** @type {const} */ ([
|
|
9
42
|
'debug',
|
|
10
43
|
'log',
|
|
@@ -18,8 +51,6 @@ Object.freeze(logLevels);
|
|
|
18
51
|
|
|
19
52
|
/** @typedef {Pick<Console, LogLevel>} LimitedConsole */
|
|
20
53
|
|
|
21
|
-
const { entries, fromEntries } = Object;
|
|
22
|
-
|
|
23
54
|
/**
|
|
24
55
|
* Deep-copy a value by round-tripping it through JSON (which drops
|
|
25
56
|
* function/symbol/undefined values and properties that are non-enumerable
|
|
@@ -32,11 +63,14 @@ const { entries, fromEntries } = Object;
|
|
|
32
63
|
export const deepCopyJsonable = value => JSON.parse(JSON.stringify(value));
|
|
33
64
|
|
|
34
65
|
/**
|
|
35
|
-
* @
|
|
36
|
-
* @
|
|
37
|
-
* @
|
|
38
|
-
* @param {
|
|
39
|
-
* @
|
|
66
|
+
* @template {Record<PropertyKey, unknown>} O
|
|
67
|
+
* @template {string & keyof O} K
|
|
68
|
+
* @template M
|
|
69
|
+
* @param {O[K]} value
|
|
70
|
+
* @param {K | undefined} name
|
|
71
|
+
* @param {O | undefined} container
|
|
72
|
+
* @param {(value: O[K], name: K, record: O) => O[K] | M} mapper
|
|
73
|
+
* @returns {O[K] | M | { [K2 in keyof O[K]]: O[K][K2] | M }}
|
|
40
74
|
*/
|
|
41
75
|
const deepMapObjectInternal = (value, name, container, mapper) => {
|
|
42
76
|
if (container && typeof name === 'string') {
|
|
@@ -51,18 +85,30 @@ const deepMapObjectInternal = (value, name, container, mapper) => {
|
|
|
51
85
|
}
|
|
52
86
|
|
|
53
87
|
let wasMapped = false;
|
|
54
|
-
const
|
|
88
|
+
const valueObj = /** @type {Record<string, unknown>} */ (value);
|
|
89
|
+
/**
|
|
90
|
+
* @type {<T extends typeof value, K2 extends string & keyof T>(
|
|
91
|
+
* entry: [K2, T[K2]],
|
|
92
|
+
* ) => [K2, T[K2] | M]}
|
|
93
|
+
*/
|
|
94
|
+
const mapEntry = ([innerName, innerValue]) => {
|
|
55
95
|
const mappedInnerValue = deepMapObjectInternal(
|
|
56
96
|
innerValue,
|
|
57
97
|
innerName,
|
|
58
|
-
|
|
98
|
+
valueObj,
|
|
59
99
|
mapper,
|
|
60
100
|
);
|
|
61
101
|
wasMapped ||= mappedInnerValue !== innerValue;
|
|
62
|
-
return [innerName, mappedInnerValue];
|
|
63
|
-
}
|
|
102
|
+
return [innerName, /** @type {any} */ (mappedInnerValue)];
|
|
103
|
+
};
|
|
104
|
+
const mappedEntries = typedEntries(valueObj).map(mapEntry);
|
|
64
105
|
|
|
65
|
-
|
|
106
|
+
if (!wasMapped) {
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const mappedObj = fromTypedEntries(mappedEntries);
|
|
111
|
+
return /** @type {any} */ (mappedObj);
|
|
66
112
|
};
|
|
67
113
|
|
|
68
114
|
/**
|
|
@@ -74,23 +120,78 @@ const deepMapObjectInternal = (value, name, container, mapper) => {
|
|
|
74
120
|
* When the property value is an object, it is sent to the mapper like any other
|
|
75
121
|
* value, and then recursively traversed unless replaced with a distinct value.
|
|
76
122
|
*
|
|
77
|
-
* @
|
|
78
|
-
* @
|
|
79
|
-
* @
|
|
123
|
+
* @template {Record<string, unknown>} O
|
|
124
|
+
* @template M
|
|
125
|
+
* @param {O} obj
|
|
126
|
+
* @param {<T extends Record<string, unknown>, K extends string & keyof T>(
|
|
127
|
+
* value: T[K],
|
|
128
|
+
* name: K,
|
|
129
|
+
* record: T,
|
|
130
|
+
* ) => T[K] | M} mapper
|
|
131
|
+
* @returns {O | { [K in keyof O]: K extends string ? O[K] | M : never }}
|
|
80
132
|
*/
|
|
81
133
|
export const deepMapObject = (obj, mapper) =>
|
|
82
|
-
deepMapObjectInternal(obj, undefined, undefined, mapper);
|
|
134
|
+
/** @type {any} */ (deepMapObjectInternal(obj, undefined, undefined, mapper));
|
|
83
135
|
|
|
84
136
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
137
|
+
* Explicitly set a function's name, supporting use of arrow functions for which
|
|
138
|
+
* source text doesn't include a name and no initial name is set by
|
|
139
|
+
* NamedEvaluation
|
|
140
|
+
* https://tc39.es/ecma262/multipage/syntax-directed-operations.html#sec-runtime-semantics-namedevaluation
|
|
141
|
+
*
|
|
142
|
+
* `name` is the first parameter for better readability at call sites (e.g.,
|
|
143
|
+
* `return defineName('foo', () => { ... })`).
|
|
144
|
+
*
|
|
145
|
+
* @template {Function} F
|
|
146
|
+
* @param {string} name
|
|
147
|
+
* @param {F} fn
|
|
148
|
+
* @returns {F}
|
|
149
|
+
*/
|
|
150
|
+
export const defineName = (name, fn) =>
|
|
151
|
+
defineProperty(fn, 'name', { value: name });
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* By analogy with how `Array.prototype.map` will map the elements of an array
|
|
155
|
+
* to transformed elements of an array of the same shape, `objectMapMutable`
|
|
156
|
+
* will do likewise for the enumerable string-keyed properties of an object.
|
|
157
|
+
*
|
|
158
|
+
* Unlike endo's `objectMap`, this function returns a non-hardened object.
|
|
159
|
+
*
|
|
160
|
+
* @template {Record<string, unknown>} O
|
|
161
|
+
* @template M
|
|
87
162
|
* @param {O} obj
|
|
88
|
-
* @param {<K extends keyof O>(value: O[K], key: K) =>
|
|
89
|
-
* @returns {{ [K in keyof O]:
|
|
163
|
+
* @param {<K extends keyof O>(value: O[K], key: K) => M} mapper
|
|
164
|
+
* @returns {{ [K in keyof O]: K extends string ? M : never }}
|
|
90
165
|
*/
|
|
91
|
-
export const objectMapMutable = (obj,
|
|
92
|
-
const
|
|
93
|
-
|
|
166
|
+
export const objectMapMutable = (obj, mapper) => {
|
|
167
|
+
const oldEntries = typedEntries(obj);
|
|
168
|
+
/** @type {<K extends keyof O>(entry: [K, O[K]]) => [K, M]} */
|
|
169
|
+
const mapEntry = ([k, v]) => [k, mapper(v, k)];
|
|
170
|
+
const newEntries = typedMap(oldEntries, mapEntry);
|
|
171
|
+
const newObj = fromTypedEntries(newEntries);
|
|
172
|
+
return /** @type {any} */ (newObj);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Return the value from `map` associated with `key`. If there is not yet such a
|
|
177
|
+
* value, get one from `makeValue(key)` and update `map` before returning the
|
|
178
|
+
* new value.
|
|
179
|
+
*
|
|
180
|
+
* @template K
|
|
181
|
+
* @template V
|
|
182
|
+
* @param {K extends WeakKey ? WeakMap<K, V> : Map<K, V>} map
|
|
183
|
+
* @param {K} key
|
|
184
|
+
* @param {(key: K) => V} makeValue
|
|
185
|
+
* @returns {V}
|
|
186
|
+
*/
|
|
187
|
+
export const provideLazyMap = (map, key, makeValue) => {
|
|
188
|
+
const found = map.get(key);
|
|
189
|
+
if (found !== undefined || map.has(key)) {
|
|
190
|
+
return /** @type {V} */ (found);
|
|
191
|
+
}
|
|
192
|
+
const value = makeValue(key);
|
|
193
|
+
map.set(key, value);
|
|
194
|
+
return value;
|
|
94
195
|
};
|
|
95
196
|
|
|
96
197
|
/**
|
|
@@ -114,3 +215,23 @@ export const makeMeasureSeconds = currentTimeMillisec => {
|
|
|
114
215
|
};
|
|
115
216
|
return measureSeconds;
|
|
116
217
|
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Find all of an object's properties whose name starts with a prefix, and
|
|
221
|
+
* return a new object consisting of those properties without that prefix.
|
|
222
|
+
* Useful for filtering environment variables relevant to a particular purpose.
|
|
223
|
+
*
|
|
224
|
+
* @template {string} P
|
|
225
|
+
* @template {string} K
|
|
226
|
+
* @template V
|
|
227
|
+
* @param {Record<`${P}${K}`, V>} obj
|
|
228
|
+
* @param {P} prefix
|
|
229
|
+
*/
|
|
230
|
+
export const unprefixedProperties = (obj, prefix) =>
|
|
231
|
+
/** @type {Record<K, V>} */ (
|
|
232
|
+
fromTypedEntries(
|
|
233
|
+
typedEntries(obj)
|
|
234
|
+
.filter(([key]) => key.startsWith(prefix))
|
|
235
|
+
.map(([key, value]) => [key.slice(prefix.length), value]),
|
|
236
|
+
)
|
|
237
|
+
);
|