diff --git a/src/serialize.ts b/src/serialize.ts index 3acc170..38a5cdb 100644 --- a/src/serialize.ts +++ b/src/serialize.ts @@ -1,4 +1,3 @@ -import invariant from 'assert' import { isInteger, getByteLength } from './helpers' function getClassNamespace(item: any, scope: Record) { @@ -59,7 +58,9 @@ export default function serialize( const constructorName = getClassNamespace(item, scope) if (typeof item.serialize === 'function') { const serialized = item.serialize() - invariant(typeof serialized === 'string', `${item.constructor.name}.serialize should return a string`) + if (typeof serialized !== 'string') { + throw new Error(`${item.constructor.name}.serialize should return a string`); + } return `C:${constructorName.length}:"${constructorName}":${getByteLength(serialized, options)}:{${serialized}}` } return `O:${constructorName.length}:"${constructorName}":${serializeObject(item, scope)}` diff --git a/src/unserialize.ts b/src/unserialize.ts index ca7025d..94d83be 100644 --- a/src/unserialize.ts +++ b/src/unserialize.ts @@ -1,4 +1,3 @@ -import invariant from 'assert' // eslint-disable-next-line import/no-cycle import Parser from './parser' import { isInteger, getClass, getIncompleteClass, __PHP_Incomplete_Class } from './helpers' @@ -11,7 +10,9 @@ export type Options = { function getClassReference(className: string, scope: Record, strict: boolean): any { let container: any const classReference = scope[className] - invariant(classReference || !strict, `Class ${className} not found in given scope`) + if (!classReference && strict) { + throw new Error(`Class ${className} not found in given scope`); + } if (classReference) { // @ts-ignore container = new (getClass(classReference.prototype))() @@ -93,7 +94,9 @@ function unserializeItem(parser: Parser, scope: Record, options: Op const payload = parser.getByLength('{', '}', length => parser.readAhead(length)) const result = getClassReference(name, scope, options.strict) if (!(result instanceof __PHP_Incomplete_Class)) { - invariant(result.unserialize, `unserialize not found on class when processing '${name}'`) + if (!result.unserialize) { + throw new Error(`unserialize not found on class when processing '${name}'`); + } result.unserialize(payload) } return result