From 93ea1666f6634ac3da74c2784a719fc12905b4b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Mon, 29 Nov 2021 13:45:46 +0100 Subject: [PATCH] perf_hooks: use spec-compliant `structuredClone` Serialize PerformanceMark's `detail` correctly. Fixes: https://github.com/nodejs/node/issues/40840 PR-URL: https://github.com/nodejs/node/pull/40904 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Anna Henningsen --- lib/internal/perf/usertiming.js | 3 ++- lib/internal/util.js | 16 ---------------- test/parallel/test-perf-hooks-usertiming.js | 5 ++++- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/lib/internal/perf/usertiming.js b/lib/internal/perf/usertiming.js index 496c75deb3b78f..7266b14ef40b2e 100644 --- a/lib/internal/perf/usertiming.js +++ b/lib/internal/perf/usertiming.js @@ -26,7 +26,8 @@ const { }, } = require('internal/errors'); -const { structuredClone, lazyDOMException } = require('internal/util'); +const { structuredClone } = require('internal/structured_clone'); +const { lazyDOMException } = require('internal/util'); const markTimings = new SafeMap(); diff --git a/lib/internal/util.js b/lib/internal/util.js index 9a8139cdae34b4..e0f26f35717219 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -477,21 +477,6 @@ const lazyDOMException = hideStackFrames((message, name) => { return new _DOMException(message, name); }); -function structuredClone(value) { - const { - DefaultSerializer, - DefaultDeserializer, - } = require('v8'); - const ser = new DefaultSerializer(); - ser._getDataCloneError = hideStackFrames((message) => - lazyDOMException(message, 'DataCloneError')); - ser.writeValue(value); - const serialized = ser.releaseBuffer(); - - const des = new DefaultDeserializer(serialized); - return des.readValue(); -} - module.exports = { assertCrypto, cachedResult, @@ -515,7 +500,6 @@ module.exports = { promisify, sleep, spliceOne, - structuredClone, toUSVString, removeColors, diff --git a/test/parallel/test-perf-hooks-usertiming.js b/test/parallel/test-perf-hooks-usertiming.js index db83e8db5d79d3..71cc28c0f9c3a3 100644 --- a/test/parallel/test-perf-hooks-usertiming.js +++ b/test/parallel/test-perf-hooks-usertiming.js @@ -44,12 +44,15 @@ assert.throws(() => mark(Symbol('a')), { assert.strictEqual(m.entryType, 'mark'); assert.strictEqual(m.detail, null); }); -[1, 'any', {}, []].forEach((detail) => { +[1, 'any', {}, [], /a/].forEach((detail) => { const m = mark('a', { detail }); assert.strictEqual(m.name, 'a'); assert.strictEqual(m.entryType, 'mark'); // Value of detail is structured cloned. assert.deepStrictEqual(m.detail, detail); + if (typeof detail === 'object') { + assert.notStrictEqual(m.detail, detail); + } }); clearMarks();