From 595d64e307452b805c4d209a6f77916e54c031ab Mon Sep 17 00:00:00 2001 From: scagood <2230835+scagood@users.noreply.github.com> Date: Fri, 1 Mar 2024 11:13:58 +0000 Subject: [PATCH] [Fix] Add "isWritable" before writing to properties This fixes a crash in node 0.8 and 0.10 --- index.js | 15 ++++++++++++++- package.json | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 797c3b1..ce507da 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ var whichTypedArray = require('which-typed-array'); var taSlice = require('typedarray.prototype.slice'); +var gopd = require('gopd'); // TODO: use call-bind, is-date, is-regex, is-string, is-boolean-object, is-number-object function toS(obj) { return Object.prototype.toString.call(obj); } @@ -57,6 +58,14 @@ var hasOwnProperty = Object.prototype.hasOwnProperty || function (obj, key) { return key in obj; }; +function isWritable(object, key) { + if (typeof gopd !== 'function') { + return true; + } + + return !gopd(object, key).writable; +} + function copy(src) { if (typeof src === 'object' && src !== null) { var dst; @@ -200,7 +209,11 @@ function walk(root, cb) { if (modifiers.pre) { modifiers.pre.call(state, state.node[key], key); } var child = walker(state.node[key]); - if (immutable && hasOwnProperty.call(state.node, key)) { + if ( + immutable + && hasOwnProperty.call(state.node, key) + && !isWritable(state.node, key) + ) { state.node[key] = child.node; } diff --git a/package.json b/package.json index 88305b1..223335e 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ ] }, "dependencies": { + "gopd": "^1.0.1", "typedarray.prototype.slice": "^1.0.2", "which-typed-array": "^1.1.15" },