diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 15fe1b9..a6f406f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x, 14.x] + node-version: [12.x, 14.x, 16.x] steps: - uses: actions/checkout@v2 @@ -26,3 +26,4 @@ jobs: - run: npm run lint - run: npm test - run: npm run build + - run: npm run test-imports diff --git a/README.md b/README.md index f5ccdd1..9fd6644 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,12 @@ npm i mmdb-lib ```typescript import fs from 'fs'; -import Reader from 'mmdb-lib'; +import * as mmdb from 'mmdb-lib'; // Get a buffer with mmdb database, from file system or whereever. const db = fs.readFileSync('/path/to/GeoLite2-City.mmdb'); -const reader = new Reader(db); +const reader = new mmdb.Reader(db); console.log(reader.get('66.6.44.4')); // inferred type `CityResponse` console.log(reader.getWithPrefixLength('66.6.44.4')); // tuple with inferred type `[CityResponse|null, number]` ``` diff --git a/package.json b/package.json index f784e62..514c87e 100644 --- a/package.json +++ b/package.json @@ -11,23 +11,24 @@ "geoip2" ], "author": "Dmitry Shirokov ", - "contributors": [], - "dependencies": {}, + "contributors": [ + "William Storey @horgh" + ], "devDependencies": { "@types/ip-address": "5.8.2", - "@types/jest": "26.0.4", - "@types/node": "10.17.5", + "@types/jest": "27.0.0", + "@types/node": "16.0.0", "@types/sinon": "7.5.2", "@typescript-eslint/eslint-plugin": "^4.4.0", "@typescript-eslint/parser": "^4.4.0", "eslint": "^7.10.0", "ip-address": "6.2.0", - "jest": "26.1.0", + "jest": "^27.4.7", "prettier": "^2.1.2", - "semantic-release": "17.1.1", + "semantic-release": "^18.0.1", "sinon": "9.0.1", - "ts-jest": "26.1.1", - "typescript": "3.9.6" + "ts-jest": "^27.1.2", + "typescript": "4.5.4" }, "repository": { "type": "git", @@ -52,6 +53,7 @@ "lint": "eslint . --ext .ts", "lint:types": "tsc --noEmit", "test": "jest", + "test-imports": "node test/imports/commonjs.js && node test/imports/esm.mjs", "format": "prettier --write src", "prepublish": "npm run build", "semantic-release": "semantic-release" diff --git a/src/__test__/integration.test.ts b/src/__test__/integration.test.ts index 31e2caf..df8ad36 100755 --- a/src/__test__/integration.test.ts +++ b/src/__test__/integration.test.ts @@ -2,7 +2,7 @@ import fs from 'fs'; import assert from 'assert'; import ipAddr from 'ip-address'; import path from 'path'; -import Reader from '../'; +import { Reader } from '../'; import { Cache } from '../types'; import { Response } from '../reader/response'; diff --git a/src/decoder.ts b/src/decoder.ts index 31e0c32..4d9aa4b 100755 --- a/src/decoder.ts +++ b/src/decoder.ts @@ -163,7 +163,7 @@ export default class Decoder { // If the value is 30, then the size is 285 + *the next two bytes after the type // specifying bytes as a single unsigned integer*. if (size === 30) { - return cursor(285 + this.db.readUInt16BE(offset, false), offset + 2); + return cursor(285 + this.db.readUInt16BE(offset), offset + 2); } // At this point `size` is always 31. @@ -227,7 +227,7 @@ export default class Decoder { // bytes as a 32-bit value. In this case, the last three bits of the control byte // are ignored. } else { - packed = this.db.readUInt32BE(offset, true); + packed = this.db.readUInt32BE(offset); } offset += pointerSize + 1; @@ -252,11 +252,11 @@ export default class Decoder { } private decodeDouble(offset: number) { - return this.db.readDoubleBE(offset, true); + return this.db.readDoubleBE(offset); } private decodeFloat(offset: number) { - return this.db.readFloatBE(offset, true); + return this.db.readFloatBE(offset); } private decodeMap(size: number, offset: number) { @@ -280,7 +280,7 @@ export default class Decoder { if (size === 0) { return 0; } - return this.db.readInt32BE(offset, true); + return this.db.readInt32BE(offset); } private decodeUint(offset: number, size: number) { @@ -325,8 +325,7 @@ export default class Decoder { const numberOfLongs = size / 4; for (let i = 0; i < numberOfLongs; i++) { integer = - integer * BigInt(4294967296) + - BigInt(buffer.readUInt32BE(i << 2, true)); + integer * BigInt(4294967296) + BigInt(buffer.readUInt32BE(i << 2)); } return integer.toString(); diff --git a/src/index.test.ts b/src/index.test.ts index 8e0e9bb..5a656c5 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,7 +1,7 @@ import assert from 'assert'; import fs from 'fs'; import path from 'path'; -import Reader from '.'; +import { Reader } from '.'; const dataDir = path.join(__dirname, '../test/data/test-data'); const read = (dir: string, filepath: string): Buffer => diff --git a/src/index.ts b/src/index.ts index 16ea84e..831a684 100755 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import { ReaderOptions } from './types'; const DATA_SECTION_SEPARATOR_SIZE = 16; -export default class Reader { +export class Reader { public metadata: Metadata; private decoder: Decoder; private db: Buffer; diff --git a/src/reader/walker.ts b/src/reader/walker.ts index 996d4da..27884e0 100644 --- a/src/reader/walker.ts +++ b/src/reader/walker.ts @@ -40,12 +40,12 @@ const readNodeRight28 = const readNodeLeft32 = (db: Buffer): NodeReader => (offset: number): number => - db.readUInt32BE(offset, true); + db.readUInt32BE(offset); const readNodeRight32 = (db: Buffer): NodeReader => (offset: number): number => - db.readUInt32BE(offset + 4, true); + db.readUInt32BE(offset + 4); export default (db: Buffer, recordSize: number): Walker => { switch (recordSize) { diff --git a/test/imports/commonjs.js b/test/imports/commonjs.js new file mode 100644 index 0000000..904868d --- /dev/null +++ b/test/imports/commonjs.js @@ -0,0 +1,10 @@ +const fs = require('fs'); +const assert = require('assert'); + +const mmdb = require('../../lib/index.js'); +const db = fs.readFileSync('test/data/test-data/GeoIP2-City-Test.mmdb') + +const reader = new mmdb.Reader(db) +const res = reader.get('175.16.199.255'); +assert.strictEqual(res.city.geoname_id, 2038180); +console.log('commonjs: OK'); diff --git a/test/imports/esm.mjs b/test/imports/esm.mjs new file mode 100644 index 0000000..0e5d345 --- /dev/null +++ b/test/imports/esm.mjs @@ -0,0 +1,9 @@ +import fs from 'fs'; +import assert from 'assert'; +import * as mmdb from '../../lib/index.js'; +const db = fs.readFileSync('test/data/test-data/GeoIP2-City-Test.mmdb') + +const reader = new mmdb.Reader(db) +const res = reader.get('175.16.199.255'); +assert.strictEqual(res.city.geoname_id, 2038180); +console.log('esm: OK'); diff --git a/tsconfig.json b/tsconfig.json index 6b1ecbf..dfbf40a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,22 +1,14 @@ { "compilerOptions": { - "forceConsistentCasingInFileNames": true, - "outDir": "lib", - "rootDir": "src", - "allowJs": false, - "allowSyntheticDefaultImports": true, - "declaration": true, - "diagnostics": true, - "esModuleInterop": true, - "extendedDiagnostics": true, - "listEmittedFiles": true, + "lib": ["es2021"], "module": "commonjs", - "removeComments": true, - "sourceMap": true, + "target": "es2021", + "outDir": "lib", + "strict": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, "strictPropertyInitialization": false, - "lib": ["ES2019"], - "target": "ES2019" }, "exclude": [ "node_modules",