From 87164bfe6b9976cc17b05f2bbd1a3bf1b89c9031 Mon Sep 17 00:00:00 2001 From: ocavue Date: Thu, 1 Feb 2024 23:04:14 +0800 Subject: [PATCH] utils: replace isPlainObject with a robuster implementation --- src/utils.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 9503c62..5be9834 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -162,15 +162,29 @@ export function concatBytes(...arrays: Uint8Array[]): Uint8Array { } // Check if object doens't have custom constructor (like Uint8Array/Array) -const isPlainObject = (obj: any) => - Object.prototype.toString.call(obj) === '[object Object]' && obj.constructor === Object; +// +// Copied from https://www.npmjs.com/package/is-plain-obj v4.1.0, MIT +function isPlainObject(value: unknown): boolean { + if (typeof value !== 'object' || value === null) { + return false; + } + + const prototype = Object.getPrototypeOf(value); + return ( + (prototype === null || + prototype === Object.prototype || + Object.getPrototypeOf(prototype) === null) && + !(Symbol.toStringTag in value) && + !(Symbol.iterator in value) + ); +} type EmptyObj = {}; export function checkOpts( defaults: T1, opts?: T2 ): T1 & T2 { - if (opts !== undefined && (typeof opts !== 'object' || !isPlainObject(opts))) + if (opts !== undefined && !isPlainObject(opts)) throw new Error('options must be object or undefined'); const merged = Object.assign(defaults, opts); return merged as T1 & T2;