From 3f99f855182814aeb0003330a72f5be7e52b31b3 Mon Sep 17 00:00:00 2001 From: samsiegart Date: Fri, 22 Sep 2023 10:05:04 -0700 Subject: [PATCH] fix: parse non-sequenced data correctly --- packages/rpc/package.json | 2 +- packages/rpc/src/batchQuery.ts | 24 ++++--- packages/rpc/test/chainStorageWatcher.test.ts | 2 +- yarn.lock | 65 ++++++++++++------- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/packages/rpc/package.json b/packages/rpc/package.json index 6609e9f..3e7aa5f 100644 --- a/packages/rpc/package.json +++ b/packages/rpc/package.json @@ -16,7 +16,7 @@ "coverage": "vitest run --coverage" }, "dependencies": { - "@endo/marshal": "^0.8.8", + "@endo/marshal": "^0.8.9", "vite": "^4.3.2", "vite-tsconfig-paths": "^4.2.0" }, diff --git a/packages/rpc/src/batchQuery.ts b/packages/rpc/src/batchQuery.ts index d4aec00..0274d2f 100644 --- a/packages/rpc/src/batchQuery.ts +++ b/packages/rpc/src/batchQuery.ts @@ -36,7 +36,6 @@ export const batchVstorageQuery = ( Object.fromEntries( (Array.isArray(res) ? res : [res]).map(entry => { const { id: index } = entry; - if (entry.result.response.code) { return [ pathToKey(paths[index]), @@ -64,14 +63,23 @@ export const batchVstorageQuery = ( ]; } - const value = JSON.parse(data.value); - const latestValueStr = Object.hasOwn(value, 'values') - ? value.values[value.values.length - 1] + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const parseIfJSON = (d: any) => { + try { + return JSON.parse(d); + } catch { + return d; + } + }; + const value = parseIfJSON(data.value); + + const latestValue = Object.hasOwn(value, 'values') + ? parseIfJSON(value.values[value.values.length - 1]) : value; - const parsed = JSON.parse(latestValueStr); - const unserialized = Object.hasOwn(parsed, 'slots') - ? unmarshal(parsed) - : parsed; + + const unserialized = Object.hasOwn(latestValue, 'slots') + ? unmarshal(latestValue) + : latestValue; return [ pathToKey(paths[index]), diff --git a/packages/rpc/test/chainStorageWatcher.test.ts b/packages/rpc/test/chainStorageWatcher.test.ts index 8b48a48..d548195 100644 --- a/packages/rpc/test/chainStorageWatcher.test.ts +++ b/packages/rpc/test/chainStorageWatcher.test.ts @@ -90,7 +90,7 @@ describe('makeAgoricChainStorageWatcher', () => { }); it('can handle unserialized values', async () => { - const expected = 126560000000; + const expected = 'abc123'; const path = 'vitest.unserializedValue'; fetch.mockResolvedValue( diff --git a/yarn.lock b/yarn.lock index a11bfb7..e74aa57 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2080,6 +2080,11 @@ resolved "https://registry.yarnpkg.com/@endo/env-options/-/env-options-0.1.3.tgz#430133977de48fb64125535d54f75e0eb0682260" integrity sha512-ljs5E0+Fhe5IQsciCn6jL5bsi1PYysPm4NhrPmNByw136AcjUbfZhOBmOzeXwOhxaL93cI/jTStNdWEA80Q+nQ== +"@endo/env-options@^0.1.4": + version "0.1.4" + resolved "https://registry.yarnpkg.com/@endo/env-options/-/env-options-0.1.4.tgz#e516bc3864f00b154944e444fb8996a9a0c23a45" + integrity sha512-Ol8ct0aW8VK1ZaqntnUJfrYT59P6Xn36XPbHzkqQhsYkpudKDn5ILYEwGmSO/Ff+XJjv/pReNI0lhOyyrDa9mg== + "@endo/eslint-plugin@^0.4.4": version "0.4.4" resolved "https://registry.yarnpkg.com/@endo/eslint-plugin/-/eslint-plugin-0.4.4.tgz#9392bd2de2900197d08bdce147b4e42d8de30e45" @@ -2102,6 +2107,13 @@ dependencies: "@endo/env-options" "^0.1.3" +"@endo/eventual-send@^0.17.6": + version "0.17.6" + resolved "https://registry.yarnpkg.com/@endo/eventual-send/-/eventual-send-0.17.6.tgz#86719e4e3ff76991c49f6680309dc77dff65fe55" + integrity sha512-73cKY2uiWdzMJn7i284NJyD3K0UKjpksBg/EA2GT8YJa0TgeBczFQIm81vC08itK5gHuDDH2vC5COSGR6hxKIg== + dependencies: + "@endo/env-options" "^0.1.4" + "@endo/exo@^0.2.2": version "0.2.2" resolved "https://registry.yarnpkg.com/@endo/exo/-/exo-0.2.2.tgz#eeebe3eeb40dcf9b409fddf8d5ff73821b470515" @@ -2153,25 +2165,25 @@ "@endo/pass-style" "^0.1.3" "@endo/promise-kit" "^0.2.56" -"@endo/marshal@^0.8.8": - version "0.8.8" - resolved "https://registry.yarnpkg.com/@endo/marshal/-/marshal-0.8.8.tgz#d6e51a5c1c4c97f3904a8ee93bb352706391baa7" - integrity sha512-uoGF5Hwmne9y8eT73474YTLjfcvgWQD+9Iq5PrJSy+Ymek4VbOGNv12j5wvA0TTylbHlrakFv0fs7q10OV9B+Q== +"@endo/marshal@^0.8.9": + version "0.8.9" + resolved "https://registry.yarnpkg.com/@endo/marshal/-/marshal-0.8.9.tgz#f6fcaf23ecad828f6d086657f1d1590ea8ef3840" + integrity sha512-wzYlY5/JFzY/wAVxZ6h0BxlRaAS/9KKnhircKO/tGw5bZYHFvLeSeMCBZ4VCSZg5aNgDlhuvB0S6iCwS5MYqcg== dependencies: - "@endo/eventual-send" "^0.17.5" - "@endo/nat" "^4.1.30" - "@endo/pass-style" "^0.1.6" - "@endo/promise-kit" "^0.2.59" + "@endo/eventual-send" "^0.17.6" + "@endo/nat" "^4.1.31" + "@endo/pass-style" "^0.1.7" + "@endo/promise-kit" "^0.2.60" "@endo/nat@^4.1.27": version "4.1.27" resolved "https://registry.yarnpkg.com/@endo/nat/-/nat-4.1.27.tgz#8f1a398b39f994b0769070a3fb36d3397bf86794" integrity sha512-mKRdIc4NvrxZ1qPBcYZH6zaj0RsRwADaCcfPNRnGWcHC9dY8DmZDDcgqNdSBFLiEto1RnXeoKAEGxk6hn253Ow== -"@endo/nat@^4.1.30": - version "4.1.30" - resolved "https://registry.yarnpkg.com/@endo/nat/-/nat-4.1.30.tgz#a52137e4d9dc159dc01f146a3506e7de3cb912f2" - integrity sha512-UN8obyLCGrG4vvEX+LV8ZOQMfwVDDA6z6mnvBRyZXbl+GwAs8QXa7vi9NiscidyCTvxad7jjfuf7IIqlIZUTQg== +"@endo/nat@^4.1.31": + version "4.1.31" + resolved "https://registry.yarnpkg.com/@endo/nat/-/nat-4.1.31.tgz#ca738f472481a572f47749b41529b3261ebb4c1e" + integrity sha512-tz0PnEmzX9BAtKEawYndsx+XC6f+2CKErtrpbpOuX3uct5VNLdw6q6cArSYtnHbxRHR0YaHUdeG0W6okmup4bg== "@endo/netstring@^0.3.26": version "0.3.26" @@ -2190,12 +2202,12 @@ "@endo/promise-kit" "^0.2.56" "@fast-check/ava" "^1.1.3" -"@endo/pass-style@^0.1.6": - version "0.1.6" - resolved "https://registry.yarnpkg.com/@endo/pass-style/-/pass-style-0.1.6.tgz#eb554b6c1b4ff3a4e55a8f3c612866bf27e91b76" - integrity sha512-SkRFW0FqBjjVagBDyhlGr7GmDFtVqZYhH4rN+YBtIjciFugmMT5p8gdnTnns3AUnurBSHH55MjKXIOzJNL+Txw== +"@endo/pass-style@^0.1.7": + version "0.1.7" + resolved "https://registry.yarnpkg.com/@endo/pass-style/-/pass-style-0.1.7.tgz#ea22568e8b86fb2d1a14a5fc042374cc0d8e310b" + integrity sha512-dlB62Ptjcy/+iachy7qzAdgIwaU60rE+XLummLRpE2tDSJF2jSFJlVwa/QuGw1KKO7Rt4vog/51sKev3EbJZQg== dependencies: - "@endo/promise-kit" "^0.2.59" + "@endo/promise-kit" "^0.2.60" "@fast-check/ava" "^1.1.5" "@endo/patterns@^0.2.2": @@ -2214,12 +2226,12 @@ dependencies: ses "^0.18.4" -"@endo/promise-kit@^0.2.59": - version "0.2.59" - resolved "https://registry.yarnpkg.com/@endo/promise-kit/-/promise-kit-0.2.59.tgz#93f61a709660252c541e9ecaf7c138f1cf279f9a" - integrity sha512-l4KK3CE4gb0cqPA2u9Agh+7yIbsExO10eJbnWKDgN9FC6b1oaZPL9NdXf3kOhSjH1OFCPHtc+whH9uMNV9e+KQ== +"@endo/promise-kit@^0.2.60": + version "0.2.60" + resolved "https://registry.yarnpkg.com/@endo/promise-kit/-/promise-kit-0.2.60.tgz#8012ada06970c7eaf965cd856563b34a1790e163" + integrity sha512-6Zp9BqBbc3ywaG+iLRrQRmO/VLKrMnvsbgOKKPMpjEC3sUlksYA09uaH3GrKZgoGChF8m9bXK8eFW39z7wJNUw== dependencies: - ses "^0.18.7" + ses "^0.18.8" "@endo/ses-ava@^0.2.40": version "0.2.40" @@ -10726,7 +10738,7 @@ serialize-error@^7.0.1: dependencies: type-fest "^0.13.1" -ses@0.18.7, ses@^0.18.7: +ses@0.18.7: version "0.18.7" resolved "https://registry.yarnpkg.com/ses/-/ses-0.18.7.tgz#8abeb65732845642c2d1a284e415ef11957fb489" integrity sha512-bcQ3CY8jh+UnumfdDZCPDgRA9hWk6acpL1d7ecQLMOdMkqKkP0yv2b6Sd9AVxuxbu4cXMt158epbpwkV/5sIfw== @@ -10738,6 +10750,13 @@ ses@^0.18.4: resolved "https://registry.yarnpkg.com/ses/-/ses-0.18.4.tgz#28781719870262afc6928b7d6d94dc16318dbd86" integrity sha512-Ph0PC38Q7uutHmMM9XPqA7rp/2taiRwW6pIZJwTr4gz90DtrBvy/x7AmNPH2uqNPhKriZpYKvPi1xKWjM9xJuQ== +ses@^0.18.8: + version "0.18.8" + resolved "https://registry.yarnpkg.com/ses/-/ses-0.18.8.tgz#88036511ac3b3c07e4d82dd8cfc6e5f3788205b6" + integrity sha512-kOH1AhJc6gWDXKURKeU1w7iFUdImAegAljVvBg5EUBgNqjH4bxcEsGVUadVEPtA2PVRMyQp1fiSMDwEZkQNj1g== + dependencies: + "@endo/env-options" "^0.1.4" + set-blocking@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"