Skip to content

Commit

Permalink
[Fix] Add "isWritable" before writing to properties
Browse files Browse the repository at this point in the history
 This fixes a crash in node 0.8 and 0.10
  • Loading branch information
scagood authored and ljharb committed Mar 1, 2024
1 parent 0f1e6f1 commit 595d64e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
]
},
"dependencies": {
"gopd": "^1.0.1",
"typedarray.prototype.slice": "^1.0.2",
"which-typed-array": "^1.1.15"
},
Expand Down

0 comments on commit 595d64e

Please sign in to comment.