From 3032bc587ac7f262deff80ead8f2b6647e7e6315 Mon Sep 17 00:00:00 2001 From: Benjamin Armintor Date: Mon, 27 Jan 2025 16:27:12 -0500 Subject: [PATCH] Replace JSON.parse(JSON.stringify(json)) with structuredClone - see also https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone - fixes #4082 --- __tests__/src/selectors/manifests.test.js | 4 ++-- src/state/selectors/manifests.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/__tests__/src/selectors/manifests.test.js b/__tests__/src/selectors/manifests.test.js index 3c10199d2..68122efb9 100644 --- a/__tests__/src/selectors/manifests.test.js +++ b/__tests__/src/selectors/manifests.test.js @@ -506,7 +506,7 @@ describe('getManifestSearchService', () => { }); it('supports v1 of the search spec', () => { - const v1 = JSON.parse(JSON.stringify(manifestFixtureFg165hz3589)); + const v1 = structuredClone(manifestFixtureFg165hz3589); v1.service[0].profile = 'http://iiif.io/api/search/1/search'; const state = { manifests: { x: { json: v1 } } }; expect(getManifestSearchService(state, { manifestId: 'x' }).id).toEqual('https://contentsearch.stanford.edu/fg165hz3589/search'); @@ -525,7 +525,7 @@ describe('getManifestAutocompleteService', () => { }); it('supports v1 of the search spec', () => { - const v1 = JSON.parse(JSON.stringify(manifestFixtureFg165hz3589)); + const v1 = structuredClone(manifestFixtureFg165hz3589); v1.service[0].profile = 'http://iiif.io/api/search/1/search'; v1.service[0].service.profile = 'http://iiif.io/api/search/1/autocomplete'; const state = { manifests: { x: { json: v1 } } }; diff --git a/src/state/selectors/manifests.js b/src/state/selectors/manifests.js index 04e3661e6..ec7e93ae0 100644 --- a/src/state/selectors/manifests.js +++ b/src/state/selectors/manifests.js @@ -9,8 +9,8 @@ import { getConfig } from './config'; /** */ function createManifestoInstance(json, locale) { if (!json) return undefined; - // Use JSON stringify/parse to create a deep copy and prevent Manifesto from mutating the json - const manifestoObject = Utils.parseManifest(JSON.parse(JSON.stringify(json)), locale ? { locale } : undefined); + // Use structuredClone to create a deep copy and prevent Manifesto from mutating the json + const manifestoObject = Utils.parseManifest(structuredClone(json), locale ? { locale } : undefined); // Local patching of Manifesto so that when its a Collection, it behaves similarly if (typeof manifestoObject.getSequences != 'function') { manifestoObject.getSequences = () => [];