@atproto/lex-data 0.0.5 → 0.0.7
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/CHANGELOG.md +20 -0
- package/dist/blob.d.ts +39 -5
- package/dist/blob.d.ts.map +1 -1
- package/dist/blob.js +45 -16
- package/dist/blob.js.map +1 -1
- package/dist/cid.d.ts +83 -14
- package/dist/cid.d.ts.map +1 -1
- package/dist/cid.js +94 -35
- package/dist/cid.js.map +1 -1
- package/dist/lex-equals.js +1 -1
- package/dist/lex-equals.js.map +1 -1
- package/dist/uint8array-from-base64.d.ts.map +1 -1
- package/dist/uint8array-from-base64.js +1 -1
- package/dist/uint8array-from-base64.js.map +1 -1
- package/dist/uint8array-to-base64.d.ts.map +1 -1
- package/dist/uint8array-to-base64.js +2 -2
- package/dist/uint8array-to-base64.js.map +1 -1
- package/dist/utf8-from-base64.d.ts +4 -0
- package/dist/utf8-from-base64.d.ts.map +1 -0
- package/dist/utf8-from-base64.js +18 -0
- package/dist/utf8-from-base64.js.map +1 -0
- package/dist/utf8-to-base64.d.ts +4 -0
- package/dist/utf8-to-base64.d.ts.map +1 -0
- package/dist/utf8-to-base64.js +20 -0
- package/dist/utf8-to-base64.js.map +1 -0
- package/dist/utf8.d.ts +3 -0
- package/dist/utf8.d.ts.map +1 -1
- package/dist/utf8.js +16 -3
- package/dist/utf8.js.map +1 -1
- package/package.json +1 -1
- package/src/blob.test.ts +150 -25
- package/src/blob.ts +111 -27
- package/src/cid.test.ts +50 -33
- package/src/cid.ts +200 -35
- package/src/lex-equals.ts +2 -2
- package/src/uint8array-from-base64.ts +1 -1
- package/src/uint8array-to-base64.test.ts +2 -2
- package/src/uint8array-to-base64.ts +2 -2
- package/src/utf8-from-base64.test.ts +39 -0
- package/src/utf8-from-base64.ts +23 -0
- package/src/utf8-grapheme-len.test.ts +2 -2
- package/src/utf8-len.test.ts +2 -2
- package/src/utf8-to-base64.test.ts +35 -0
- package/src/utf8-to-base64.ts +22 -0
- package/src/utf8.ts +23 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uint8array-from-base64.js","sourceRoot":"","sources":["../src/uint8array-from-base64.ts"],"names":[],"mappings":";;;AAiDA,gDAOC;AAxDD,yDAAoD;AACpD,6DAAqD;AAGrD,MAAM,MAAM,GAAG,+BAAY,CAAA;AAkBd,QAAA,gBAAgB,GAC3B,OAAO,UAAU,CAAC,UAAU,KAAK,UAAU;IACzC,CAAC,CAAC,SAAS,gBAAgB,CACvB,GAAW,EACX,WAA2B,QAAQ;QAEnC,OAAO,UAAU,CAAC,UAAW,CAAC,GAAG,EAAE;YACjC,QAAQ;YACR,iBAAiB,EAAE,OAAO;SAC3B,CAAC,CAAA;IACJ,CAAC;IACH,CAAC,CAAC,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"uint8array-from-base64.js","sourceRoot":"","sources":["../src/uint8array-from-base64.ts"],"names":[],"mappings":"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","sourcesContent":["import { fromString } from 'uint8arrays/from-string'\nimport { NodeJSBuffer } from './lib/nodejs-buffer.js'\nimport { Base64Alphabet } from './uint8array-base64.js'\n\nconst Buffer = NodeJSBuffer\n\ndeclare global {\n interface Uint8ArrayConstructor {\n /**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64 Uint8Array.fromBase64()}\n */\n fromBase64?: (\n b64: string,\n options?: {\n /** @default 'base64' */\n alphabet?: 'base64' | 'base64url'\n lastChunkHandling?: 'loose' | 'strict' | 'stop-before-partial'\n },\n ) => Uint8Array\n }\n}\n\nexport const fromBase64Native =\n typeof Uint8Array.fromBase64 === 'function'\n ? function fromBase64Native(\n b64: string,\n alphabet: Base64Alphabet = 'base64',\n ): Uint8Array {\n return Uint8Array.fromBase64!(b64, {\n alphabet,\n lastChunkHandling: 'loose',\n })\n }\n : /* v8 ignore next -- @preserve */ null\n\nexport const fromBase64Node = Buffer\n ? function fromBase64Node(\n b64: string,\n alphabet: Base64Alphabet = 'base64',\n ): Uint8Array {\n const bytes = Buffer.from(b64, alphabet)\n verifyBase64ForBytes(b64, bytes)\n // Convert to Uint8Array because even though Buffer is a sub class of\n // Uint8Array, it serializes differently to Uint8Array (e.g. in JSON) and\n // results in unexpected behavior downstream (e.g. in tests)\n return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n }\n : /* v8 ignore next -- @preserve */ null\n\nexport function fromBase64Ponyfill(\n b64: string,\n alphabet: Base64Alphabet = 'base64',\n): Uint8Array {\n const bytes = fromString(b64, b64.endsWith('=') ? `${alphabet}pad` : alphabet)\n verifyBase64ForBytes(b64, bytes)\n return bytes\n}\n\n// @NOTE NodeJS will silently stop decoding at the first invalid character,\n// while \"uint8arrays/from-string\" will not validate that the padding is\n// correct. The following function performs basic validation to ensure that the\n// input was a valid base64 string. The availability of the \"bytes\" allows\n// to perform checks with O[1] complexity.\nfunction verifyBase64ForBytes(b64: string, bytes: Uint8Array): void {\n const paddingCount = b64.endsWith('==') ? 2 : b64.endsWith('=') ? 1 : 0\n const trimmedLength = b64.length - paddingCount\n const expectedByteLength = Math.floor((trimmedLength * 3) / 4)\n if (bytes.length !== expectedByteLength) {\n throw new Error('Invalid base64 string')\n }\n\n const expectedB64Length = (bytes.length / 3) * 4\n const expectedPaddingCount =\n expectedB64Length % 4 === 0 ? 0 : 4 - (expectedB64Length % 4)\n const expectedFullB64Length = expectedB64Length + expectedPaddingCount\n if (b64.length > expectedFullB64Length) {\n throw new Error('Invalid base64 string')\n }\n\n // The previous might still allow false positive if only the last few\n // chars are invalid.\n for (\n let i = Math.ceil(expectedB64Length);\n i < b64.length - paddingCount;\n i++\n ) {\n const code = b64.charCodeAt(i)\n if (\n !(code >= 65 && code <= 90) && // A-Z\n !(code >= 97 && code <= 122) && // a-z\n !(code >= 48 && code <= 57) && // 0-9\n code !== 43 && // +\n code !== 47 // /\n ) {\n throw new Error('Invalid base64 string')\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uint8array-to-base64.d.ts","sourceRoot":"","sources":["../src/uint8array-to-base64.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAIvD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,UAAU;QAClB;;WAEG;QACH,QAAQ,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE;YACpB,wBAAwB;YACxB,QAAQ,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAA;YACjC,WAAW,CAAC,EAAE,OAAO,CAAA;SACtB,KAAK,MAAM,CAAA;KACb;CACF;AAED,eAAO,MAAM,cAAc,WAGZ,UAAU,aACP,cAAc,KACvB,MAAM,
|
|
1
|
+
{"version":3,"file":"uint8array-to-base64.d.ts","sourceRoot":"","sources":["../src/uint8array-to-base64.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAIvD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,UAAU;QAClB;;WAEG;QACH,QAAQ,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE;YACpB,wBAAwB;YACxB,QAAQ,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAA;YACjC,WAAW,CAAC,EAAE,OAAO,CAAA;SACtB,KAAK,MAAM,CAAA;KACb;CACF;AAED,eAAO,MAAM,cAAc,WAGZ,UAAU,aACP,cAAc,KACvB,MAAM,QAG6B,CAAA;AAE5C,eAAO,MAAM,YAAY,WAEZ,UAAU,aACP,cAAc,KACvB,MAAM,QAc6B,CAAA;AAE1C,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,UAAU,EACjB,QAAQ,GAAE,cAAyB,GAClC,MAAM,CAER"}
|
|
@@ -9,7 +9,7 @@ exports.toBase64Native = typeof Uint8Array.prototype.toBase64 === 'function'
|
|
|
9
9
|
? function toBase64Native(bytes, alphabet = 'base64') {
|
|
10
10
|
return bytes.toBase64({ alphabet, omitPadding: true });
|
|
11
11
|
}
|
|
12
|
-
: null;
|
|
12
|
+
: /* v8 ignore next -- @preserve */ null;
|
|
13
13
|
exports.toBase64Node = Buffer
|
|
14
14
|
? function toBase64Node(bytes, alphabet = 'base64') {
|
|
15
15
|
const buffer = bytes instanceof Buffer ? bytes : Buffer.from(bytes);
|
|
@@ -24,7 +24,7 @@ exports.toBase64Node = Buffer
|
|
|
24
24
|
: b64.slice(0, -1) // '='
|
|
25
25
|
: b64;
|
|
26
26
|
}
|
|
27
|
-
: null;
|
|
27
|
+
: /* v8 ignore next -- @preserve */ null;
|
|
28
28
|
function toBase64Ponyfill(bytes, alphabet = 'base64') {
|
|
29
29
|
return (0, to_string_1.toString)(bytes, alphabet);
|
|
30
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uint8array-to-base64.js","sourceRoot":"","sources":["../src/uint8array-to-base64.ts"],"names":[],"mappings":";;;AAiDA,4CAKC;AAtDD,qDAAgD;AAChD,6DAAqD;AAGrD,MAAM,MAAM,GAAG,+BAAY,CAAA;AAed,QAAA,cAAc,GACzB,OAAO,UAAU,CAAC,SAAS,CAAC,QAAQ,KAAK,UAAU;IACjD,CAAC,CAAC,SAAS,cAAc,CACrB,KAAiB,EACjB,WAA2B,QAAQ;QAEnC,OAAO,KAAK,CAAC,QAAS,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAA;IACzD,CAAC;IACH,CAAC,CAAC,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"uint8array-to-base64.js","sourceRoot":"","sources":["../src/uint8array-to-base64.ts"],"names":[],"mappings":";;;AAiDA,4CAKC;AAtDD,qDAAgD;AAChD,6DAAqD;AAGrD,MAAM,MAAM,GAAG,+BAAY,CAAA;AAed,QAAA,cAAc,GACzB,OAAO,UAAU,CAAC,SAAS,CAAC,QAAQ,KAAK,UAAU;IACjD,CAAC,CAAC,SAAS,cAAc,CACrB,KAAiB,EACjB,WAA2B,QAAQ;QAEnC,OAAO,KAAK,CAAC,QAAS,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAA;IACzD,CAAC;IACH,CAAC,CAAC,iCAAiC,CAAC,IAAI,CAAA;AAE/B,QAAA,YAAY,GAAG,MAAM;IAChC,CAAC,CAAC,SAAS,YAAY,CACnB,KAAiB,EACjB,WAA2B,QAAQ;QAEnC,MAAM,MAAM,GAAG,KAAK,YAAY,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACnE,MAAM,GAAG,GAAG,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;QAErC,uDAAuD;QACvD,0EAA0E;QAC1E,sEAAsE;QACtE,6DAA6D;QAC7D,OAAO,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,SAAS,CAAC,IAAI;YACtD,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,SAAS,CAAC,IAAI;gBACjD,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO;gBAC1B,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM;YAC3B,CAAC,CAAC,GAAG,CAAA;IACT,CAAC;IACH,CAAC,CAAC,iCAAiC,CAAC,IAAI,CAAA;AAE1C,SAAgB,gBAAgB,CAC9B,KAAiB,EACjB,WAA2B,QAAQ;IAEnC,OAAO,IAAA,oBAAQ,EAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;AAClC,CAAC","sourcesContent":["import { toString } from 'uint8arrays/to-string'\nimport { NodeJSBuffer } from './lib/nodejs-buffer.js'\nimport { Base64Alphabet } from './uint8array-base64.js'\n\nconst Buffer = NodeJSBuffer\n\ndeclare global {\n interface Uint8Array {\n /**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64 Uint8Array.prototype.toBase64()}\n */\n toBase64?: (options?: {\n /** @default 'base64' */\n alphabet?: 'base64' | 'base64url'\n omitPadding?: boolean\n }) => string\n }\n}\n\nexport const toBase64Native =\n typeof Uint8Array.prototype.toBase64 === 'function'\n ? function toBase64Native(\n bytes: Uint8Array,\n alphabet: Base64Alphabet = 'base64',\n ): string {\n return bytes.toBase64!({ alphabet, omitPadding: true })\n }\n : /* v8 ignore next -- @preserve */ null\n\nexport const toBase64Node = Buffer\n ? function toBase64Node(\n bytes: Uint8Array,\n alphabet: Base64Alphabet = 'base64',\n ): string {\n const buffer = bytes instanceof Buffer ? bytes : Buffer.from(bytes)\n const b64 = buffer.toString(alphabet)\n\n // @NOTE We strip padding for strict compatibility with\n // uint8arrays.toString behavior. Tests failing because of the presence of\n // padding are not really synonymous with an actual error and we might\n // (should?) actually want to keep the padding at some point.\n return b64.charCodeAt(b64.length - 1) === /* '=' */ 0x3d\n ? b64.charCodeAt(b64.length - 2) === /* '=' */ 0x3d\n ? b64.slice(0, -2) // '=='\n : b64.slice(0, -1) // '='\n : b64\n }\n : /* v8 ignore next -- @preserve */ null\n\nexport function toBase64Ponyfill(\n bytes: Uint8Array,\n alphabet: Base64Alphabet = 'base64',\n): string {\n return toString(bytes, alphabet)\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Base64Alphabet } from './uint8array-base64.js';
|
|
2
|
+
export declare const utf8FromBase64Node: ((b64: string, alphabet?: Base64Alphabet) => string) | null;
|
|
3
|
+
export declare function utf8FromBase64Ponyfill(b64: string, alphabet?: Base64Alphabet): string;
|
|
4
|
+
//# sourceMappingURL=utf8-from-base64.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utf8-from-base64.d.ts","sourceRoot":"","sources":["../src/utf8-from-base64.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAIvD,eAAO,MAAM,kBAAkB,SAEpB,MAAM,aACD,cAAc,KACvB,MAAM,QAG6B,CAAA;AAG1C,wBAAgB,sBAAsB,CACpC,GAAG,EAAE,MAAM,EACX,QAAQ,CAAC,EAAE,cAAc,GACxB,MAAM,CAGR"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.utf8FromBase64Node = void 0;
|
|
4
|
+
exports.utf8FromBase64Ponyfill = utf8FromBase64Ponyfill;
|
|
5
|
+
const from_string_1 = require("uint8arrays/from-string");
|
|
6
|
+
const nodejs_buffer_js_1 = require("./lib/nodejs-buffer.js");
|
|
7
|
+
const Buffer = nodejs_buffer_js_1.NodeJSBuffer;
|
|
8
|
+
exports.utf8FromBase64Node = Buffer
|
|
9
|
+
? function utf8FromBase64Node(b64, alphabet = 'base64') {
|
|
10
|
+
return Buffer.from(b64, alphabet).toString('utf8');
|
|
11
|
+
}
|
|
12
|
+
: /* v8 ignore next -- @preserve */ null;
|
|
13
|
+
const textDecoder = /*#__PURE__*/ new TextDecoder();
|
|
14
|
+
function utf8FromBase64Ponyfill(b64, alphabet) {
|
|
15
|
+
const bytes = (0, from_string_1.fromString)(b64, alphabet);
|
|
16
|
+
return textDecoder.decode(bytes);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=utf8-from-base64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utf8-from-base64.js","sourceRoot":"","sources":["../src/utf8-from-base64.ts"],"names":[],"mappings":";;;AAgBA,wDAMC;AAtBD,yDAAoD;AACpD,6DAAqD;AAGrD,MAAM,MAAM,GAAG,+BAAY,CAAA;AAEd,QAAA,kBAAkB,GAAG,MAAM;IACtC,CAAC,CAAC,SAAS,kBAAkB,CACzB,GAAW,EACX,WAA2B,QAAQ;QAEnC,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IACpD,CAAC;IACH,CAAC,CAAC,iCAAiC,CAAC,IAAI,CAAA;AAE1C,MAAM,WAAW,GAAG,aAAa,CAAC,IAAI,WAAW,EAAE,CAAA;AACnD,SAAgB,sBAAsB,CACpC,GAAW,EACX,QAAyB;IAEzB,MAAM,KAAK,GAAG,IAAA,wBAAU,EAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IACvC,OAAO,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAClC,CAAC","sourcesContent":["import { fromString } from 'uint8arrays/from-string'\nimport { NodeJSBuffer } from './lib/nodejs-buffer.js'\nimport { Base64Alphabet } from './uint8array-base64.js'\n\nconst Buffer = NodeJSBuffer\n\nexport const utf8FromBase64Node = Buffer\n ? function utf8FromBase64Node(\n b64: string,\n alphabet: Base64Alphabet = 'base64',\n ): string {\n return Buffer.from(b64, alphabet).toString('utf8')\n }\n : /* v8 ignore next -- @preserve */ null\n\nconst textDecoder = /*#__PURE__*/ new TextDecoder()\nexport function utf8FromBase64Ponyfill(\n b64: string,\n alphabet?: Base64Alphabet,\n): string {\n const bytes = fromString(b64, alphabet)\n return textDecoder.decode(bytes)\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Base64Alphabet } from './uint8array-base64.js';
|
|
2
|
+
export declare const utf8ToBase64Node: ((text: string, alphabet?: Base64Alphabet) => string) | null;
|
|
3
|
+
export declare function utf8ToBase64Ponyfill(text: string, alphabet?: Base64Alphabet): string;
|
|
4
|
+
//# sourceMappingURL=utf8-to-base64.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utf8-to-base64.d.ts","sourceRoot":"","sources":["../src/utf8-to-base64.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAKvD,eAAO,MAAM,gBAAgB,UACO,MAAM,aAAa,cAAc,KAAG,MAAM,QAIpC,CAAA;AAG1C,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,QAAQ,CAAC,EAAE,cAAc,GACxB,MAAM,CAGR"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.utf8ToBase64Node = void 0;
|
|
4
|
+
exports.utf8ToBase64Ponyfill = utf8ToBase64Ponyfill;
|
|
5
|
+
const to_string_1 = require("uint8arrays/to-string");
|
|
6
|
+
const nodejs_buffer_js_1 = require("./lib/nodejs-buffer.js");
|
|
7
|
+
const uint8array_to_base64_js_1 = require("./uint8array-to-base64.js");
|
|
8
|
+
const Buffer = nodejs_buffer_js_1.NodeJSBuffer;
|
|
9
|
+
exports.utf8ToBase64Node = Buffer
|
|
10
|
+
? function utf8ToBase64Node(text, alphabet) {
|
|
11
|
+
const buffer = Buffer.from(text, 'utf8');
|
|
12
|
+
return uint8array_to_base64_js_1.toBase64Node(buffer, alphabet);
|
|
13
|
+
}
|
|
14
|
+
: /* v8 ignore next -- @preserve */ null;
|
|
15
|
+
const textEncoder = /*#__PURE__*/ new TextEncoder();
|
|
16
|
+
function utf8ToBase64Ponyfill(text, alphabet) {
|
|
17
|
+
const bytes = textEncoder.encode(text);
|
|
18
|
+
return (0, to_string_1.toString)(bytes, alphabet);
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=utf8-to-base64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utf8-to-base64.js","sourceRoot":"","sources":["../src/utf8-to-base64.ts"],"names":[],"mappings":";;;AAeA,oDAMC;AArBD,qDAAgD;AAChD,6DAAqD;AAErD,uEAAwD;AAExD,MAAM,MAAM,GAAG,+BAAY,CAAA;AAEd,QAAA,gBAAgB,GAAG,MAAM;IACpC,CAAC,CAAC,SAAS,gBAAgB,CAAC,IAAY,EAAE,QAAyB;QAC/D,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;QACxC,OAAO,sCAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IACxC,CAAC;IACH,CAAC,CAAC,iCAAiC,CAAC,IAAI,CAAA;AAE1C,MAAM,WAAW,GAAG,aAAa,CAAC,IAAI,WAAW,EAAE,CAAA;AACnD,SAAgB,oBAAoB,CAClC,IAAY,EACZ,QAAyB;IAEzB,MAAM,KAAK,GAAG,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACtC,OAAO,IAAA,oBAAQ,EAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;AAClC,CAAC","sourcesContent":["import { toString } from 'uint8arrays/to-string'\nimport { NodeJSBuffer } from './lib/nodejs-buffer.js'\nimport { Base64Alphabet } from './uint8array-base64.js'\nimport { toBase64Node } from './uint8array-to-base64.js'\n\nconst Buffer = NodeJSBuffer\n\nexport const utf8ToBase64Node = Buffer\n ? function utf8ToBase64Node(text: string, alphabet?: Base64Alphabet): string {\n const buffer = Buffer.from(text, 'utf8')\n return toBase64Node!(buffer, alphabet)\n }\n : /* v8 ignore next -- @preserve */ null\n\nconst textEncoder = /*#__PURE__*/ new TextEncoder()\nexport function utf8ToBase64Ponyfill(\n text: string,\n alphabet?: Base64Alphabet,\n): string {\n const bytes = textEncoder.encode(text)\n return toString(bytes, alphabet)\n}\n"]}
|
package/dist/utf8.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { Base64Alphabet } from './uint8array.js';
|
|
1
2
|
export declare const graphemeLen: (str: string) => number;
|
|
2
3
|
export declare const utf8Len: (string: string) => number;
|
|
4
|
+
export declare const utf8ToBase64: (str: string, alphabet?: Base64Alphabet) => string;
|
|
5
|
+
export declare const utf8FromBase64: (b64: string, alphabet?: Base64Alphabet) => string;
|
|
3
6
|
//# sourceMappingURL=utf8.d.ts.map
|
package/dist/utf8.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utf8.d.ts","sourceRoot":"","sources":["../src/utf8.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utf8.d.ts","sourceRoot":"","sources":["../src/utf8.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAShD,eAAO,MAAM,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAEY,CAAA;AAUvD,eAAO,MAAM,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAEQ,CAAA;AAElD,eAAO,MAAM,YAAY,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,cAAc,KAAK,MAEf,CAAA;AAExD,eAAO,MAAM,cAAc,EAAE,CAC3B,GAAG,EAAE,MAAM,EACX,QAAQ,CAAC,EAAE,cAAc,KACtB,MAEqD,CAAA"}
|
package/dist/utf8.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.utf8Len = exports.graphemeLen = void 0;
|
|
3
|
+
exports.utf8FromBase64 = exports.utf8ToBase64 = exports.utf8Len = exports.graphemeLen = void 0;
|
|
4
|
+
const utf8_from_base64_js_1 = require("./utf8-from-base64.js");
|
|
4
5
|
const utf8_grapheme_len_js_1 = require("./utf8-grapheme-len.js");
|
|
5
6
|
const utf8_len_js_1 = require("./utf8-len.js");
|
|
6
|
-
|
|
7
|
+
const utf8_to_base64_js_1 = require("./utf8-to-base64.js");
|
|
8
|
+
exports.graphemeLen =
|
|
9
|
+
/* v8 ignore next -- @preserve */ utf8_grapheme_len_js_1.graphemeLenNative ??
|
|
10
|
+
/* v8 ignore next -- @preserve */ utf8_grapheme_len_js_1.graphemeLenPonyfill;
|
|
11
|
+
/* v8 ignore next -- @preserve */
|
|
7
12
|
if (exports.graphemeLen === utf8_grapheme_len_js_1.graphemeLenPonyfill) {
|
|
8
13
|
/*#__PURE__*/
|
|
9
14
|
console.warn('[@atproto/lex-data]: Intl.Segmenter is not available in this environment. Falling back to ponyfill implementation.');
|
|
10
15
|
}
|
|
11
|
-
exports.utf8Len =
|
|
16
|
+
exports.utf8Len =
|
|
17
|
+
/* v8 ignore next -- @preserve */ utf8_len_js_1.utf8LenNode ??
|
|
18
|
+
/* v8 ignore next -- @preserve */ utf8_len_js_1.utf8LenCompute;
|
|
19
|
+
exports.utf8ToBase64 =
|
|
20
|
+
/* v8 ignore next -- @preserve */ utf8_to_base64_js_1.utf8ToBase64Node ??
|
|
21
|
+
/* v8 ignore next -- @preserve */ utf8_to_base64_js_1.utf8ToBase64Ponyfill;
|
|
22
|
+
exports.utf8FromBase64 =
|
|
23
|
+
/* v8 ignore next -- @preserve */ utf8_from_base64_js_1.utf8FromBase64Node ??
|
|
24
|
+
/* v8 ignore next -- @preserve */ utf8_from_base64_js_1.utf8FromBase64Ponyfill;
|
|
12
25
|
//# sourceMappingURL=utf8.js.map
|
package/dist/utf8.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utf8.js","sourceRoot":"","sources":["../src/utf8.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"utf8.js","sourceRoot":"","sources":["../src/utf8.ts"],"names":[],"mappings":";;;AACA,+DAG8B;AAC9B,iEAA+E;AAC/E,+CAA2D;AAC3D,2DAA4E;AAE/D,QAAA,WAAW;AACtB,iCAAiC,CAAC,wCAAiB;IACnD,iCAAiC,CAAC,0CAAmB,CAAA;AAEvD,iCAAiC;AACjC,IAAI,mBAAW,KAAK,0CAAmB,EAAE,CAAC;IACxC,aAAa;IACb,OAAO,CAAC,IAAI,CACV,oHAAoH,CACrH,CAAA;AACH,CAAC;AAEY,QAAA,OAAO;AAClB,iCAAiC,CAAC,yBAAW;IAC7C,iCAAiC,CAAC,4BAAc,CAAA;AAErC,QAAA,YAAY;AACvB,iCAAiC,CAAC,oCAAgB;IAClD,iCAAiC,CAAC,wCAAoB,CAAA;AAE3C,QAAA,cAAc;AAIzB,iCAAiC,CAAC,wCAAkB;IACpD,iCAAiC,CAAC,4CAAsB,CAAA","sourcesContent":["import { Base64Alphabet } from './uint8array.js'\nimport {\n utf8FromBase64Node,\n utf8FromBase64Ponyfill,\n} from './utf8-from-base64.js'\nimport { graphemeLenNative, graphemeLenPonyfill } from './utf8-grapheme-len.js'\nimport { utf8LenCompute, utf8LenNode } from './utf8-len.js'\nimport { utf8ToBase64Node, utf8ToBase64Ponyfill } from './utf8-to-base64.js'\n\nexport const graphemeLen: (str: string) => number =\n /* v8 ignore next -- @preserve */ graphemeLenNative ??\n /* v8 ignore next -- @preserve */ graphemeLenPonyfill\n\n/* v8 ignore next -- @preserve */\nif (graphemeLen === graphemeLenPonyfill) {\n /*#__PURE__*/\n console.warn(\n '[@atproto/lex-data]: Intl.Segmenter is not available in this environment. Falling back to ponyfill implementation.',\n )\n}\n\nexport const utf8Len: (string: string) => number =\n /* v8 ignore next -- @preserve */ utf8LenNode ??\n /* v8 ignore next -- @preserve */ utf8LenCompute\n\nexport const utf8ToBase64: (str: string, alphabet?: Base64Alphabet) => string =\n /* v8 ignore next -- @preserve */ utf8ToBase64Node ??\n /* v8 ignore next -- @preserve */ utf8ToBase64Ponyfill\n\nexport const utf8FromBase64: (\n b64: string,\n alphabet?: Base64Alphabet,\n) => string =\n /* v8 ignore next -- @preserve */ utf8FromBase64Node ??\n /* v8 ignore next -- @preserve */ utf8FromBase64Ponyfill\n"]}
|
package/package.json
CHANGED
package/src/blob.test.ts
CHANGED
|
@@ -1,29 +1,39 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
BlobRef,
|
|
4
|
+
LegacyBlobRef,
|
|
5
|
+
enumBlobRefs,
|
|
6
|
+
isBlobRef,
|
|
7
|
+
isLegacyBlobRef,
|
|
8
|
+
} from './blob.js'
|
|
9
|
+
import { RawCid, parseCid } from './cid.js'
|
|
10
|
+
import { LexArray, LexMap, LexValue } from './lex.js'
|
|
4
11
|
|
|
5
12
|
// await cidForRawBytes(Buffer.from('Hello, World!'))
|
|
6
|
-
const
|
|
13
|
+
const validBlobCid = parseCid(
|
|
7
14
|
'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
|
|
15
|
+
{ flavor: 'raw' },
|
|
8
16
|
)
|
|
17
|
+
|
|
9
18
|
// await cidForLex(Buffer.from('Hello, World!'))
|
|
10
|
-
const
|
|
19
|
+
const invalidBlobCid = parseCid(
|
|
11
20
|
'bafyreic52vzks7wdklat4evp3vimohl55i2unzqpshz2ytka5omzr7exdy',
|
|
21
|
+
{ flavor: 'cbor' },
|
|
12
22
|
)
|
|
13
23
|
|
|
14
24
|
describe(isBlobRef, () => {
|
|
15
25
|
it('tests valid blobCid and lexCid', () => {
|
|
16
|
-
expect(
|
|
17
|
-
expect(
|
|
18
|
-
expect(
|
|
19
|
-
expect(
|
|
26
|
+
expect(validBlobCid.code).toBe(0x55) // raw
|
|
27
|
+
expect(validBlobCid.multihash.code).toBe(0x12) // sha2-256
|
|
28
|
+
expect(invalidBlobCid.code).toBe(0x71) // dag-cbor
|
|
29
|
+
expect(invalidBlobCid.multihash.code).toBe(0x12) // sha2-256
|
|
20
30
|
})
|
|
21
31
|
|
|
22
32
|
it('parses valid blob', () => {
|
|
23
33
|
expect(
|
|
24
34
|
isBlobRef({
|
|
25
35
|
$type: 'blob',
|
|
26
|
-
ref:
|
|
36
|
+
ref: validBlobCid,
|
|
27
37
|
mimeType: 'image/jpeg',
|
|
28
38
|
size: 10000,
|
|
29
39
|
}),
|
|
@@ -33,7 +43,7 @@ describe(isBlobRef, () => {
|
|
|
33
43
|
isBlobRef(
|
|
34
44
|
{
|
|
35
45
|
$type: 'blob',
|
|
36
|
-
ref:
|
|
46
|
+
ref: invalidBlobCid,
|
|
37
47
|
mimeType: 'image/jpeg',
|
|
38
48
|
size: 10000,
|
|
39
49
|
},
|
|
@@ -43,11 +53,22 @@ describe(isBlobRef, () => {
|
|
|
43
53
|
).toBe(true)
|
|
44
54
|
})
|
|
45
55
|
|
|
56
|
+
it('performs strict validation by default', () => {
|
|
57
|
+
expect(
|
|
58
|
+
isBlobRef({
|
|
59
|
+
$type: 'blob',
|
|
60
|
+
ref: invalidBlobCid,
|
|
61
|
+
mimeType: 'image/jpeg',
|
|
62
|
+
size: 10000,
|
|
63
|
+
}),
|
|
64
|
+
).toBe(false)
|
|
65
|
+
})
|
|
66
|
+
|
|
46
67
|
it('rejects invalid inputs', () => {
|
|
47
68
|
expect(
|
|
48
69
|
isBlobRef({
|
|
49
70
|
$type: 'blob',
|
|
50
|
-
ref: { $link:
|
|
71
|
+
ref: { $link: validBlobCid.toString() },
|
|
51
72
|
mimeType: 'image/jpeg',
|
|
52
73
|
size: '10000',
|
|
53
74
|
}),
|
|
@@ -56,7 +77,7 @@ describe(isBlobRef, () => {
|
|
|
56
77
|
expect(
|
|
57
78
|
isBlobRef({
|
|
58
79
|
// $type: 'blob',
|
|
59
|
-
ref:
|
|
80
|
+
ref: validBlobCid,
|
|
60
81
|
mimeType: 'image/jpeg',
|
|
61
82
|
size: 10000,
|
|
62
83
|
}),
|
|
@@ -65,7 +86,7 @@ describe(isBlobRef, () => {
|
|
|
65
86
|
expect(
|
|
66
87
|
isBlobRef({
|
|
67
88
|
$type: 'blob',
|
|
68
|
-
ref:
|
|
89
|
+
ref: validBlobCid,
|
|
69
90
|
mimeType: { toString: () => 'image/jpeg' },
|
|
70
91
|
size: 10000,
|
|
71
92
|
}),
|
|
@@ -75,7 +96,7 @@ describe(isBlobRef, () => {
|
|
|
75
96
|
isBlobRef(
|
|
76
97
|
{
|
|
77
98
|
$type: 'blob',
|
|
78
|
-
ref: { $link:
|
|
99
|
+
ref: { $link: validBlobCid.toString() },
|
|
79
100
|
mimeType: 'image/jpeg',
|
|
80
101
|
size: '10000',
|
|
81
102
|
},
|
|
@@ -112,7 +133,7 @@ describe(isBlobRef, () => {
|
|
|
112
133
|
expect(
|
|
113
134
|
isBlobRef({
|
|
114
135
|
$type: 'blob',
|
|
115
|
-
ref:
|
|
136
|
+
ref: validBlobCid,
|
|
116
137
|
mimeType: 'image/jpeg',
|
|
117
138
|
size: 10000.5,
|
|
118
139
|
}),
|
|
@@ -124,7 +145,7 @@ describe(isBlobRef, () => {
|
|
|
124
145
|
isBlobRef(
|
|
125
146
|
{
|
|
126
147
|
$type: 'blob',
|
|
127
|
-
ref:
|
|
148
|
+
ref: validBlobCid,
|
|
128
149
|
mimeType: 'image/jpeg',
|
|
129
150
|
size: 10000,
|
|
130
151
|
},
|
|
@@ -136,7 +157,7 @@ describe(isBlobRef, () => {
|
|
|
136
157
|
isBlobRef(
|
|
137
158
|
{
|
|
138
159
|
$type: 'blob',
|
|
139
|
-
ref:
|
|
160
|
+
ref: invalidBlobCid,
|
|
140
161
|
mimeType: 'image/jpeg',
|
|
141
162
|
size: 10000,
|
|
142
163
|
},
|
|
@@ -149,7 +170,7 @@ describe(isBlobRef, () => {
|
|
|
149
170
|
expect(
|
|
150
171
|
isBlobRef({
|
|
151
172
|
$type: 'blob',
|
|
152
|
-
ref:
|
|
173
|
+
ref: validBlobCid,
|
|
153
174
|
mimeType: 'image/jpeg',
|
|
154
175
|
size: 10000,
|
|
155
176
|
extra: 'not allowed',
|
|
@@ -160,7 +181,7 @@ describe(isBlobRef, () => {
|
|
|
160
181
|
isBlobRef(
|
|
161
182
|
{
|
|
162
183
|
$type: 'blob',
|
|
163
|
-
ref:
|
|
184
|
+
ref: validBlobCid,
|
|
164
185
|
mimeType: 'image/jpeg',
|
|
165
186
|
size: 10000,
|
|
166
187
|
extra: 'not allowed',
|
|
@@ -194,14 +215,14 @@ describe(isLegacyBlobRef, () => {
|
|
|
194
215
|
it('parses valid legacy blob', () => {
|
|
195
216
|
expect(
|
|
196
217
|
isLegacyBlobRef({
|
|
197
|
-
cid:
|
|
218
|
+
cid: validBlobCid.toString(),
|
|
198
219
|
mimeType: 'image/jpeg',
|
|
199
220
|
}),
|
|
200
221
|
).toBe(true)
|
|
201
222
|
|
|
202
223
|
expect(
|
|
203
224
|
isLegacyBlobRef({
|
|
204
|
-
cid:
|
|
225
|
+
cid: invalidBlobCid.toString(),
|
|
205
226
|
mimeType: 'image/jpeg',
|
|
206
227
|
}),
|
|
207
228
|
).toBe(true)
|
|
@@ -230,21 +251,21 @@ describe(isLegacyBlobRef, () => {
|
|
|
230
251
|
|
|
231
252
|
expect(
|
|
232
253
|
isLegacyBlobRef({
|
|
233
|
-
cid:
|
|
254
|
+
cid: invalidBlobCid.toString(),
|
|
234
255
|
mimeType: { toString: () => 'image/jpeg' },
|
|
235
256
|
}),
|
|
236
257
|
).toBe(false)
|
|
237
258
|
|
|
238
259
|
expect(
|
|
239
260
|
isLegacyBlobRef({
|
|
240
|
-
cid:
|
|
261
|
+
cid: invalidBlobCid.toString(),
|
|
241
262
|
mimeType: 3,
|
|
242
263
|
}),
|
|
243
264
|
).toBe(false)
|
|
244
265
|
|
|
245
266
|
expect(
|
|
246
267
|
isLegacyBlobRef({
|
|
247
|
-
cid:
|
|
268
|
+
cid: invalidBlobCid.toString(),
|
|
248
269
|
mimeType: '',
|
|
249
270
|
}),
|
|
250
271
|
).toBe(false)
|
|
@@ -258,10 +279,114 @@ describe(isLegacyBlobRef, () => {
|
|
|
258
279
|
it('rejects extra keys', () => {
|
|
259
280
|
expect(
|
|
260
281
|
isLegacyBlobRef({
|
|
261
|
-
cid:
|
|
282
|
+
cid: validBlobCid.toString(),
|
|
262
283
|
mimeType: 'image/jpeg',
|
|
263
284
|
extra: 'not allowed',
|
|
264
285
|
}),
|
|
265
286
|
).toBe(false)
|
|
266
287
|
})
|
|
267
288
|
})
|
|
289
|
+
|
|
290
|
+
describe(enumBlobRefs, () => {
|
|
291
|
+
const valid1: BlobRef<RawCid> = {
|
|
292
|
+
$type: 'blob',
|
|
293
|
+
ref: validBlobCid,
|
|
294
|
+
mimeType: 'image/png',
|
|
295
|
+
size: 2048,
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const valid2: BlobRef<RawCid> = {
|
|
299
|
+
$type: 'blob',
|
|
300
|
+
ref: validBlobCid,
|
|
301
|
+
mimeType: 'image/jpeg',
|
|
302
|
+
size: 1024,
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const invalid: BlobRef = {
|
|
306
|
+
$type: 'blob',
|
|
307
|
+
ref: invalidBlobCid,
|
|
308
|
+
mimeType: 'image/jpeg',
|
|
309
|
+
size: 1024,
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const legacy: LegacyBlobRef = {
|
|
313
|
+
cid: validBlobCid.toString(),
|
|
314
|
+
mimeType: 'image/gif',
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const data: LexValue = {
|
|
318
|
+
name: 'example',
|
|
319
|
+
file: { deeply: { nested: { in: { object: { valid1 } } } } },
|
|
320
|
+
attachments: [valid2, invalid, legacy, { description: 'not a blob' }],
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
it('enumerates valid BlobRefs by default', () => {
|
|
324
|
+
const refs = Array.from(enumBlobRefs(data))
|
|
325
|
+
expect(refs).toHaveLength(2)
|
|
326
|
+
expect(refs.includes(valid1)).toBe(true)
|
|
327
|
+
expect(refs.includes(valid2)).toBe(true)
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
describe('strict support', () => {
|
|
331
|
+
it('enumerates valid BlobRefs in strict mode', () => {
|
|
332
|
+
const refs = Array.from(enumBlobRefs(data, { strict: true }))
|
|
333
|
+
expect(refs).toHaveLength(2)
|
|
334
|
+
expect(refs.includes(valid1)).toBe(true)
|
|
335
|
+
expect(refs.includes(valid2)).toBe(true)
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
it('enumerates all BlobRefs in non-strict mode', () => {
|
|
339
|
+
const refs = Array.from(enumBlobRefs(data, { strict: false }))
|
|
340
|
+
expect(refs).toHaveLength(3)
|
|
341
|
+
expect(refs.includes(valid1)).toBe(true)
|
|
342
|
+
expect(refs.includes(valid2)).toBe(true)
|
|
343
|
+
expect(refs.includes(invalid)).toBe(true)
|
|
344
|
+
})
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
describe('legacy support', () => {
|
|
348
|
+
it('returns LegacyBlobRefs when legacy option is enabled', () => {
|
|
349
|
+
const refs = Array.from(enumBlobRefs(data, { allowLegacy: true }))
|
|
350
|
+
expect(refs).toHaveLength(3)
|
|
351
|
+
expect(refs.includes(valid1)).toBe(true)
|
|
352
|
+
expect(refs.includes(valid2)).toBe(true)
|
|
353
|
+
expect(refs.includes(legacy)).toBe(true)
|
|
354
|
+
})
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
describe('safety', () => {
|
|
358
|
+
it('handles cyclic structures without infinite loops', () => {
|
|
359
|
+
const cyclicArray: LexArray = [valid2]
|
|
360
|
+
const cyclicObject: LexMap = {
|
|
361
|
+
name: 'cyclic',
|
|
362
|
+
blob: valid1,
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Creating a cycle
|
|
366
|
+
cyclicArray.push(cyclicArray)
|
|
367
|
+
cyclicObject.self = cyclicObject
|
|
368
|
+
|
|
369
|
+
const refs = Array.from(
|
|
370
|
+
enumBlobRefs({
|
|
371
|
+
cyclicObject,
|
|
372
|
+
cyclicArray,
|
|
373
|
+
}),
|
|
374
|
+
)
|
|
375
|
+
expect(refs).toHaveLength(2)
|
|
376
|
+
expect(refs.includes(valid1)).toBe(true)
|
|
377
|
+
expect(refs.includes(valid2)).toBe(true)
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
it('handles deep structures without exceeding call stack', () => {
|
|
381
|
+
// Creating a deep nested structure
|
|
382
|
+
let deepData: LexMap = { blob: valid1 }
|
|
383
|
+
for (let i = 0; i < 100_000; i++) {
|
|
384
|
+
deepData = { nested: deepData }
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const refs = Array.from(enumBlobRefs(deepData))
|
|
388
|
+
expect(refs).toHaveLength(1)
|
|
389
|
+
expect(refs[0]).toBe(valid1)
|
|
390
|
+
})
|
|
391
|
+
})
|
|
392
|
+
})
|