From 2d30e1a45f61dc70d737e39687d242efaf9fa9f4 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Reina Date: Wed, 17 Aug 2022 18:36:19 +0200 Subject: [PATCH 1/4] Add test for issue #196 --- src/index.test.ts | 101 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/src/index.test.ts b/src/index.test.ts index 174ed8c..90735ea 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1001,6 +1001,107 @@ test('regression: `Object.create(null)` / object without prototype', () => { expect(parsed.date).toBeInstanceOf(Date); }); +test.only("issue #196: TypeError: Cannot read property 'queries' of undefined", () => { + const input: Record = { + props: { + dehydratedState: { + mutations: [], + queries: [ + { + state: { + data: [ + { + id: 'cl6w4xsy300010gl10j1mcu5q', + label: 'first template', + ownerId: 'cl6cdyozp0007jbul3o4oqoox', + createdAt: new Date('2022-08-16T12:02:33.291Z'), + updatedAt: new Date('2022-08-16T12:02:33.294Z'), + fields: [ + { + id: 'cl6w4xsy400020gl1o22qhkzv', + label: 'years', + type: 'NUMBER', + templateId: 'cl6w4xsy300010gl10j1mcu5q', + createdAt: new Date('2022-08-16T12:02:33.291Z'), + updatedAt: new Date('2022-08-16T12:02:33.294Z'), + }, + { + id: 'cl6w4xsy400030gl1ermljulb', + label: 'date', + type: 'DATE', + templateId: 'cl6w4xsy300010gl10j1mcu5q', + createdAt: new Date('2022-08-16T12:02:33.291Z'), + updatedAt: new Date('2022-08-16T12:02:33.294Z'), + }, + ], + }, + ], + dataUpdateCount: 1, + dataUpdatedAt: 1660753910208, + error: null, + errorUpdateCount: 0, + errorUpdatedAt: 0, + fetchFailureCount: 0, + fetchMeta: null, + isInvalidated: false, + status: 'success', + fetchStatus: 'idle', + }, + queryKey: ['templates'], + queryHash: '["templates"]', + }, + { + state: { + data: { + id: 'cl6w4xsy300010gl10j1mcu5q', + label: 'first template', + ownerId: 'cl6cdyozp0007jbul3o4oqoox', + createdAt: new Date('2022-08-16T12:02:33.291Z'), + updatedAt: new Date('2022-08-16T12:02:33.294Z'), + fields: [ + { + id: 'cl6w4xsy400020gl1o22qhkzv', + label: 'years', + type: 'NUMBER', + templateId: 'cl6w4xsy300010gl10j1mcu5q', + createdAt: new Date('2022-08-16T12:02:33.291Z'), + updatedAt: new Date('2022-08-16T12:02:33.294Z'), + }, + { + id: 'cl6w4xsy400030gl1ermljulb', + label: 'date', + type: 'DATE', + templateId: 'cl6w4xsy300010gl10j1mcu5q', + createdAt: new Date('2022-08-16T12:02:33.291Z'), + updatedAt: new Date('2022-08-16T12:02:33.294Z'), + }, + ], + }, + dataUpdateCount: 1, + dataUpdatedAt: 1660753910208, + error: null, + errorUpdateCount: 0, + errorUpdatedAt: 0, + fetchFailureCount: 0, + fetchMeta: null, + isInvalidated: false, + status: 'success', + fetchStatus: 'idle', + }, + queryKey: ['templates', 'cl6w4xsy300010gl10j1mcu5q'], + queryHash: '["templates","cl6w4xsy300010gl10j1mcu5q"]', + }, + ], + }, + }, + }; + + const stringified = SuperJSON.stringify(input); + const parsed: any = SuperJSON.parse(stringified); + + expect(parsed).toEqual(input); +}); + test('prototype pollution - __proto__', () => { expect(() => { SuperJSON.parse( From dde1bcc58017eae417dc8055730bc4999c288af4 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Reina Date: Wed, 17 Aug 2022 18:38:43 +0200 Subject: [PATCH 2/4] Remove '.only' --- src/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index 90735ea..916a65a 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1001,7 +1001,7 @@ test('regression: `Object.create(null)` / object without prototype', () => { expect(parsed.date).toBeInstanceOf(Date); }); -test.only("issue #196: TypeError: Cannot read property 'queries' of undefined", () => { +test("issue #196: TypeError: Cannot read property 'queries' of undefined", () => { const input: Record = { props: { dehydratedState: { From 55394be48062aa7117de2f4d443a2441d17e6008 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Reina Date: Thu, 25 Aug 2022 11:55:49 +0200 Subject: [PATCH 3/4] Add tests using the actual --- package.json | 2 + src/index.test.ts | 148 +++++++++++++++++----------------------------- yarn.lock | 33 ++++++++++- 3 files changed, 87 insertions(+), 96 deletions(-) diff --git a/package.json b/package.json index bc98acd..b5309ac 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "url": "https://github.com/blitz-js/superjson" }, "devDependencies": { + "@tanstack/react-query": "^4.2.1", "@types/debug": "^4.1.5", "@types/mongodb": "^3.6.12", "benchmark": "^2.1.4", @@ -64,6 +65,7 @@ "husky": "^6.0.0", "mongodb": "^3.6.6", "publish-please": "^5.5.2", + "react": "^18.2.0", "tsdx": "^0.14.1", "typescript": "^4.2.4" }, diff --git a/src/index.test.ts b/src/index.test.ts index 916a65a..9506899 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -14,6 +14,7 @@ import { import { ObjectID } from 'mongodb'; import { Decimal } from 'decimal.js'; +import { dehydrate, QueryClient } from '@tanstack/react-query'; const isNode10 = process.version.indexOf('v10') === 0; @@ -1001,105 +1002,62 @@ test('regression: `Object.create(null)` / object without prototype', () => { expect(parsed.date).toBeInstanceOf(Date); }); -test("issue #196: TypeError: Cannot read property 'queries' of undefined", () => { - const input: Record = { - props: { - dehydratedState: { - mutations: [], - queries: [ - { - state: { - data: [ - { - id: 'cl6w4xsy300010gl10j1mcu5q', - label: 'first template', - ownerId: 'cl6cdyozp0007jbul3o4oqoox', - createdAt: new Date('2022-08-16T12:02:33.291Z'), - updatedAt: new Date('2022-08-16T12:02:33.294Z'), - fields: [ - { - id: 'cl6w4xsy400020gl1o22qhkzv', - label: 'years', - type: 'NUMBER', - templateId: 'cl6w4xsy300010gl10j1mcu5q', - createdAt: new Date('2022-08-16T12:02:33.291Z'), - updatedAt: new Date('2022-08-16T12:02:33.294Z'), - }, - { - id: 'cl6w4xsy400030gl1ermljulb', - label: 'date', - type: 'DATE', - templateId: 'cl6w4xsy300010gl10j1mcu5q', - createdAt: new Date('2022-08-16T12:02:33.291Z'), - updatedAt: new Date('2022-08-16T12:02:33.294Z'), - }, - ], - }, - ], - dataUpdateCount: 1, - dataUpdatedAt: 1660753910208, - error: null, - errorUpdateCount: 0, - errorUpdatedAt: 0, - fetchFailureCount: 0, - fetchMeta: null, - isInvalidated: false, - status: 'success', - fetchStatus: 'idle', - }, - queryKey: ['templates'], - queryHash: '["templates"]', - }, - { - state: { - data: { - id: 'cl6w4xsy300010gl10j1mcu5q', - label: 'first template', - ownerId: 'cl6cdyozp0007jbul3o4oqoox', - createdAt: new Date('2022-08-16T12:02:33.291Z'), - updatedAt: new Date('2022-08-16T12:02:33.294Z'), - fields: [ - { - id: 'cl6w4xsy400020gl1o22qhkzv', - label: 'years', - type: 'NUMBER', - templateId: 'cl6w4xsy300010gl10j1mcu5q', - createdAt: new Date('2022-08-16T12:02:33.291Z'), - updatedAt: new Date('2022-08-16T12:02:33.294Z'), - }, - { - id: 'cl6w4xsy400030gl1ermljulb', - label: 'date', - type: 'DATE', - templateId: 'cl6w4xsy300010gl10j1mcu5q', - createdAt: new Date('2022-08-16T12:02:33.291Z'), - updatedAt: new Date('2022-08-16T12:02:33.294Z'), - }, - ], - }, - dataUpdateCount: 1, - dataUpdatedAt: 1660753910208, - error: null, - errorUpdateCount: 0, - errorUpdatedAt: 0, - fetchFailureCount: 0, - fetchMeta: null, - isInvalidated: false, - status: 'success', - fetchStatus: 'idle', - }, - queryKey: ['templates', 'cl6w4xsy300010gl10j1mcu5q'], - queryHash: '["templates","cl6w4xsy300010gl10j1mcu5q"]', - }, - ], +describe("ReactQuery. Issue #196: TypeError: Cannot read property 'queries' of undefined", () => { + it('works with an empty dehydrated QueryClient', () => { + const queryClient = new QueryClient(); + const input = { props: { dehydratedState: dehydrate(queryClient) } }; + + const stringified = SuperJSON.stringify(input); + const parsed: any = SuperJSON.parse(stringified); + + expect(parsed).toEqual(input); + }); + + const template = { + id: 'cl6mlkijv00000glajl1xljzl', + label: 'foo', + ownerId: 'cl6cdyozp0007jbul3o4oqoox', + createdAt: new Date('2022-08-09T19:50:25.003Z'), + updatedAt: new Date('2022-08-09T19:50:25.019Z'), + fields: [ + { + id: 'cl6mlkik800010glagppvr24i', + label: 'Property', + type: 'NUMBER', + templateId: 'cl6mlkijv00000glajl1xljzl', + createdAt: new Date('2022-08-09T19:50:25.003Z'), + updatedAt: new Date('2022-08-09T19:50:25.019Z'), }, - }, + ], }; - const stringified = SuperJSON.stringify(input); - const parsed: any = SuperJSON.parse(stringified); + it('works with simple-key dehydrated QueryClient', () => { + const queryClient = new QueryClient(); + queryClient.setQueryData(['templates'], [template]); + const input = { props: { dehydratedState: dehydrate(queryClient) } }; - expect(parsed).toEqual(input); + const stringified = SuperJSON.stringify(input); + const parsed: any = SuperJSON.parse(stringified); + + expect(parsed).toEqual(input); + }); + + it('works with double-key dehydrated QueryClient', () => { + const queryClient = new QueryClient(); + queryClient.setQueryData(['templates'], [template]); + + queryClient.setQueryData(['templates', template.id], template); + const input = { props: { dehydratedState: dehydrate(queryClient) } }; + + // INFO: The following code produces the same kind of log described + // on https://github.com/blitz-js/superjson/issues/196 + // console.log(JSON.stringify(input, null, 2)); + + const stringified = SuperJSON.stringify(input); + const parsed: any = SuperJSON.parse(stringified); + + expect(parsed).toEqual(input); + }); }); test('prototype pollution - __proto__', () => { diff --git a/yarn.lock b/yarn.lock index d4c8dde..8aac2d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1412,6 +1412,20 @@ dependencies: "@sinonjs/commons" "^1.7.0" +"@tanstack/query-core@^4.0.0-beta.1": + version "4.2.1" + resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-4.2.1.tgz#21ff3a33f27bf038c990ea53af89cf7c7e8078fc" + integrity sha512-UOyOhHKLS/5i9qG2iUnZNVV3R9riJJmG9eG+hnMFIPT/oRh5UzAfjxCtBneNgPQZLDuP8y6YtRYs/n4qVAD5Ng== + +"@tanstack/react-query@^4.2.1": + version "4.2.1" + resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-4.2.1.tgz#1f00f03573b35a353e62fa64f904bbb0286a1808" + integrity sha512-w02oTOYpoxoBzD/onAGRQNeLAvggLn7WZjS811cT05WAE/4Q3br0PTp388M7tnmyYGbgOOhFq0MkhH0wIfAKqA== + dependencies: + "@tanstack/query-core" "^4.0.0-beta.1" + "@types/use-sync-external-store" "^0.0.3" + use-sync-external-store "^1.2.0" + "@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7": version "7.1.12" resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.12.tgz#4d8e9e51eb265552a7e4f1ff2219ab6133bdfb2d" @@ -1584,6 +1598,11 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff" integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw== +"@types/use-sync-external-store@^0.0.3": + version "0.0.3" + resolved "https://registry.yarnpkg.com/@types/use-sync-external-store/-/use-sync-external-store-0.0.3.tgz#b6725d5f4af24ace33b36fafd295136e75509f43" + integrity sha512-EwmlvuaxPNej9+T4v5AuBPJa2x2UOJVdjCtDHgcDqitUeOtjnJKJ+apYjVcAoBEMjKW1VVFGZLUb5+qqa09XFA== + "@types/yargs-parser@*": version "15.0.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz" @@ -4722,7 +4741,7 @@ log-update@^2.3.0: cli-cursor "^2.0.0" wrap-ansi "^3.0.1" -loose-envify@^1.0.0, loose-envify@^1.4.0: +loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz" integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q== @@ -5520,6 +5539,13 @@ react-is@^17.0.1: resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.1.tgz#5b3531bd76a645a4c9fb6e693ed36419e3301339" integrity sha512-NAnt2iGDXohE5LI7uBnLnqvLQMtzhkiAOLXTmv+qnF9Ky7xAPcX8Up/xWIhxvLVGJvuLiNc4xQLtuqDRzb4fSA== +react@^18.2.0: + version "18.2.0" + resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5" + integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ== + dependencies: + loose-envify "^1.1.0" + read-pkg-up@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz" @@ -6798,6 +6824,11 @@ urix@^0.1.0: resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz" integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI= +use-sync-external-store@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a" + integrity sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA== + use@^3.1.0: version "3.1.1" resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz" From c02c0c7c618f7125dfb585848c7485ad627672f8 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Reina Date: Fri, 26 Aug 2022 12:58:50 +0200 Subject: [PATCH 4/4] Update dependencies to avoid using react --- package.json | 3 +-- src/index.test.ts | 2 +- yarn.lock | 36 +++++------------------------------- 3 files changed, 7 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index b5309ac..be72cd4 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "url": "https://github.com/blitz-js/superjson" }, "devDependencies": { - "@tanstack/react-query": "^4.2.1", + "@tanstack/query-core": "^4.2.2", "@types/debug": "^4.1.5", "@types/mongodb": "^3.6.12", "benchmark": "^2.1.4", @@ -65,7 +65,6 @@ "husky": "^6.0.0", "mongodb": "^3.6.6", "publish-please": "^5.5.2", - "react": "^18.2.0", "tsdx": "^0.14.1", "typescript": "^4.2.4" }, diff --git a/src/index.test.ts b/src/index.test.ts index 9506899..1865d0d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -14,7 +14,7 @@ import { import { ObjectID } from 'mongodb'; import { Decimal } from 'decimal.js'; -import { dehydrate, QueryClient } from '@tanstack/react-query'; +import { dehydrate, QueryClient } from '@tanstack/query-core'; const isNode10 = process.version.indexOf('v10') === 0; diff --git a/yarn.lock b/yarn.lock index 8aac2d7..e8028d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1412,19 +1412,10 @@ dependencies: "@sinonjs/commons" "^1.7.0" -"@tanstack/query-core@^4.0.0-beta.1": - version "4.2.1" - resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-4.2.1.tgz#21ff3a33f27bf038c990ea53af89cf7c7e8078fc" - integrity sha512-UOyOhHKLS/5i9qG2iUnZNVV3R9riJJmG9eG+hnMFIPT/oRh5UzAfjxCtBneNgPQZLDuP8y6YtRYs/n4qVAD5Ng== - -"@tanstack/react-query@^4.2.1": - version "4.2.1" - resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-4.2.1.tgz#1f00f03573b35a353e62fa64f904bbb0286a1808" - integrity sha512-w02oTOYpoxoBzD/onAGRQNeLAvggLn7WZjS811cT05WAE/4Q3br0PTp388M7tnmyYGbgOOhFq0MkhH0wIfAKqA== - dependencies: - "@tanstack/query-core" "^4.0.0-beta.1" - "@types/use-sync-external-store" "^0.0.3" - use-sync-external-store "^1.2.0" +"@tanstack/query-core@^4.2.2": + version "4.2.2" + resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-4.2.2.tgz#134a45887ff3fc8667e81936028c746564c90882" + integrity sha512-cA+hTvYtZU6cTpY1gmfokLWK76mciEkTW1vN2CJ4CZf7xhpO9HmvmzQo9mTygV96a9EGQUuYPt37fYFrsnzmww== "@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7": version "7.1.12" @@ -1598,11 +1589,6 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff" integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw== -"@types/use-sync-external-store@^0.0.3": - version "0.0.3" - resolved "https://registry.yarnpkg.com/@types/use-sync-external-store/-/use-sync-external-store-0.0.3.tgz#b6725d5f4af24ace33b36fafd295136e75509f43" - integrity sha512-EwmlvuaxPNej9+T4v5AuBPJa2x2UOJVdjCtDHgcDqitUeOtjnJKJ+apYjVcAoBEMjKW1VVFGZLUb5+qqa09XFA== - "@types/yargs-parser@*": version "15.0.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz" @@ -4741,7 +4727,7 @@ log-update@^2.3.0: cli-cursor "^2.0.0" wrap-ansi "^3.0.1" -loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.4.0: +loose-envify@^1.0.0, loose-envify@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz" integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q== @@ -5539,13 +5525,6 @@ react-is@^17.0.1: resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.1.tgz#5b3531bd76a645a4c9fb6e693ed36419e3301339" integrity sha512-NAnt2iGDXohE5LI7uBnLnqvLQMtzhkiAOLXTmv+qnF9Ky7xAPcX8Up/xWIhxvLVGJvuLiNc4xQLtuqDRzb4fSA== -react@^18.2.0: - version "18.2.0" - resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5" - integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ== - dependencies: - loose-envify "^1.1.0" - read-pkg-up@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz" @@ -6824,11 +6803,6 @@ urix@^0.1.0: resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz" integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI= -use-sync-external-store@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a" - integrity sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA== - use@^3.1.0: version "3.1.1" resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz"