Skip to content

Commit

Permalink
fix: make it compatible with node's --disable-proto=throw (#78)
Browse files Browse the repository at this point in the history
* 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 <castarco@coderspirit.xyz>

---------

Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
  • Loading branch information
castarco authored Feb 18, 2023
1 parent b4c1c37 commit 74dca1d
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any[]>;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 74dca1d

Please sign in to comment.