From c65e99b54116402696d5c01d654e9abadc231fb5 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Wed, 29 May 2024 11:34:15 +1000 Subject: [PATCH] fix: Warning on assert import in browser env --- README.md | 14 +++++++++++++- src/decoder.ts | 6 +++--- src/metadata.ts | 3 +-- src/utils.ts | 7 +++++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9fd6644..7d1e7a1 100644 --- a/README.md +++ b/README.md @@ -63,4 +63,16 @@ MIT ## Contributing -add a link +All contributions are welcome. Please make sure to add tests for your changes. + +You need to initialise the repository with the following command: + +```shell +git submodule update --init --recursive +``` + +Then you can run tests with: + +```shell +npm test +``` diff --git a/src/decoder.ts b/src/decoder.ts index 81c373a..5d4deb8 100755 --- a/src/decoder.ts +++ b/src/decoder.ts @@ -1,8 +1,7 @@ -import assert from 'assert'; import utils from './utils'; import { Cache } from './types'; -assert( +utils.assert( typeof BigInt !== 'undefined', 'Apparently you are using old version of node. Please upgrade to node 10.4.x or above.' ); @@ -47,7 +46,8 @@ export default class Decoder { private cache: Cache; constructor(db: Buffer, baseOffset = 0, cache: Cache = noCache) { - assert((this.db = db), 'Database buffer is required'); + utils.assert(Boolean(db), 'Database buffer is required'); + this.db = db; this.baseOffset = baseOffset; this.cache = cache; } diff --git a/src/metadata.ts b/src/metadata.ts index a8c9e71..ba1bbec 100755 --- a/src/metadata.ts +++ b/src/metadata.ts @@ -1,4 +1,3 @@ -import assert from 'assert'; import Decoder from './decoder'; import utils from './utils'; @@ -35,7 +34,7 @@ export const parseMetadata = (db: Buffer): Metadata => { ); } - assert( + utils.assert( [24, 28, 32].indexOf(metadata.record_size) > -1, 'Unsupported record size' ); diff --git a/src/utils.ts b/src/utils.ts index afdfb0b..2905baf 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -15,7 +15,14 @@ Upgrade instructions can be found here: \ https://github.com/runk/node-maxmind/wiki/Migration-guide\n\ If you want to use legacy library then explicitly install maxmind@1`; +const assert = (condition: boolean, message: string): void => { + if (!condition) { + throw new Error(message); + } +} + export default { + assert, concat2, concat3, concat4,