From 74dca1de8655c7d7c6bdb1a9aacf0d1d05b84e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Correa=20Casablanca?= Date: Sat, 18 Feb 2023 03:15:47 +0100 Subject: [PATCH] fix: make it compatible with node's --disable-proto=throw (#78) * fix: make it compatible with node's --disable-proto Many people use the option `--disable-proto` in NodeJS to minimise the probability of prototype pollution attacks. Because of that, `__proto__` might not be available. * refactor: move conditional outside hotpath * refactor: avoid _ prefix for getPrototypeOf * fix: add missing parenthesis Signed-off-by: Andres Correa Casablanca --------- Signed-off-by: Andres Correa Casablanca --- src/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6f84bc4..8720c96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,7 +22,8 @@ import type { InternalCopier, State } from './copier'; export type { State } from './copier'; const { isArray } = Array; -const { assign, getPrototypeOf } = Object; +const { assign } = Object; +const getPrototypeOf = Object.getPrototypeOf || ((obj) => obj.__proto__) export interface CreateCopierOptions { array?: InternalCopier; @@ -116,7 +117,7 @@ export function createCopier(options: CreateCopierOptions) { return state.cache.get(value); } - state.prototype = value.__proto__ || getPrototypeOf(value); + state.prototype = getPrototypeOf(value); state.Constructor = state.prototype && state.prototype.constructor; // plain objects