@agoric/internal 0.3.3-dev-228f08b.0 → 0.3.3-dev-4054171.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/internal",
3
- "version": "0.3.3-dev-228f08b.0+228f08b",
3
+ "version": "0.3.3-dev-4054171.0+4054171",
4
4
  "description": "Externally unsupported utilities internal to agoric-sdk",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -20,8 +20,9 @@
20
20
  "lint:types": "tsc"
21
21
  },
22
22
  "dependencies": {
23
- "@agoric/base-zone": "0.1.1-dev-228f08b.0+228f08b",
23
+ "@agoric/base-zone": "0.1.1-dev-4054171.0+4054171",
24
24
  "@endo/common": "^1.2.10",
25
+ "@endo/compartment-mapper": "^1.6.0",
25
26
  "@endo/errors": "^1.2.10",
26
27
  "@endo/far": "^1.1.11",
27
28
  "@endo/init": "^1.1.9",
@@ -35,7 +36,7 @@
35
36
  "jessie.js": "^0.3.4"
36
37
  },
37
38
  "devDependencies": {
38
- "@agoric/cosmic-proto": "0.4.1-dev-228f08b.0+228f08b",
39
+ "@agoric/cosmic-proto": "0.4.1-dev-4054171.0+4054171",
39
40
  "@endo/exo": "^1.5.9",
40
41
  "@endo/init": "^1.1.9",
41
42
  "@fast-check/ava": "^2.0.1",
@@ -62,5 +63,5 @@
62
63
  "typeCoverage": {
63
64
  "atLeast": 92.65
64
65
  },
65
- "gitHead": "228f08bb97e0f237e67e0d98115713770f25926a"
66
+ "gitHead": "4054171a6a562a8bad1824b0151d103b8fe2f502"
66
67
  }
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
@@ -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);
@@ -0,0 +1,2 @@
1
+ export function getSpecifier(fileUrl: string): Promise<string>;
2
+ //# sourceMappingURL=module-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module-utils.d.ts","sourceRoot":"","sources":["module-utils.js"],"names":[],"mappings":"AAaO,sCAHI,MAAM,GACJ,OAAO,CAAC,MAAM,CAAC,CAc3B"}
@@ -0,0 +1,27 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { search } from '@endo/compartment-mapper';
4
+ import { Fail } from '@endo/errors';
5
+
6
+ /**
7
+ * Given a file URL, return a deep import specifier that starts with the
8
+ * specifier for its containing module (e.g., the deep import specifier for this
9
+ * file is expected to be "@agoric/internal/src/module-utils.js").
10
+ *
11
+ * @param {string} fileUrl
12
+ * @returns {Promise<string>}
13
+ */
14
+ export const getSpecifier = async fileUrl => {
15
+ // eslint-disable-next-line no-new
16
+ new URL(fileUrl); // validates fileUrl
17
+ const read = async location => {
18
+ const path = fileURLToPath(new URL(location, fileUrl));
19
+ return readFile(path);
20
+ };
21
+ const searchResult = await search(read, fileUrl);
22
+ const { packageDescriptorText, moduleSpecifier } = searchResult;
23
+ const { name } = JSON.parse(packageDescriptorText);
24
+ name || Fail`containing package must have a name`;
25
+ return `${name}/${moduleSpecifier.replace(/^\.\//, '')}`;
26
+ };
27
+ harden(getSpecifier);