From 2e4bd0518d3617bb06be3944835f76bec3a17654 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 7 Jan 2022 14:44:32 +1100 Subject: [PATCH 1/6] fix: Maintenance --- package.json | 15 ++++++++------- src/decoder.ts | 12 ++++++------ src/reader/walker.ts | 4 ++-- test.jsm | 3 +++ tsconfig.json | 9 ++++----- 5 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 test.jsm diff --git a/package.json b/package.json index f784e62..2a7b5fb 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": "^17.0.8", "@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", "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", diff --git a/src/decoder.ts b/src/decoder.ts index 31e0c32..b2632c9 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) { @@ -326,7 +326,7 @@ export default class Decoder { for (let i = 0; i < numberOfLongs; i++) { integer = integer * BigInt(4294967296) + - BigInt(buffer.readUInt32BE(i << 2, true)); + BigInt(buffer.readUInt32BE(i << 2)); } return integer.toString(); 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.jsm b/test.jsm new file mode 100644 index 0000000..63f903e --- /dev/null +++ b/test.jsm @@ -0,0 +1,3 @@ +import Reader from './lib'; // Reader is equal to {default: ActuallyReader} + +// import {default as Reader} from 'mmdb-lib'; // Reader is equal to {default: ActuallyReader} because default is a special keyword here \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 6b1ecbf..52ec2d1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,21 +1,20 @@ { "compilerOptions": { - "forceConsistentCasingInFileNames": true, - "outDir": "lib", - "rootDir": "src", "allowJs": false, - "allowSyntheticDefaultImports": true, "declaration": true, "diagnostics": true, "esModuleInterop": true, "extendedDiagnostics": true, + "forceConsistentCasingInFileNames": true, + "lib": ["ES2019"], "listEmittedFiles": true, "module": "commonjs", + "outDir": "lib", "removeComments": true, + "rootDir": "src", "sourceMap": true, "strict": true, "strictPropertyInitialization": false, - "lib": ["ES2019"], "target": "ES2019" }, "exclude": [ From 63a435139cf222f2f8594741b85ef94f1e504d71 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 7 Jan 2022 15:27:58 +1100 Subject: [PATCH 2/6] fix: Maintenance --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2a7b5fb..bf0b841 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "devDependencies": { "@types/ip-address": "5.8.2", "@types/jest": "27.0.0", - "@types/node": "^17.0.8", + "@types/node": "16.0.0", "@types/sinon": "7.5.2", "@typescript-eslint/eslint-plugin": "^4.4.0", "@typescript-eslint/parser": "^4.4.0", From 59b520d789fb3d64c6bde0f6e85416fcb72af3fd Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 7 Jan 2022 15:52:10 +1100 Subject: [PATCH 3/6] fix: commonjs --- .gitignore | 2 +- README.md | 4 ++-- package.json | 6 ++++-- src/__test__/integration.test.ts | 2 +- src/index.test.ts | 2 +- src/index.ts | 2 +- tsconfig.json | 19 ++++++------------- 7 files changed, 16 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 29bd1c1..af4ed01 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,6 @@ node_modules coverage npm-debug.log .nyc_output -lib +lib-* package-lock.json local 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 bf0b841..5cc4f79 100644 --- a/package.json +++ b/package.json @@ -39,9 +39,11 @@ "url": "http://github.com/runk/mmdb-lib/issues" }, "files": [ - "lib" + "lib-commonjs", + "lib-esm" ], - "main": "lib/index.js", + "main": "lib-commonjs/index.js", + "module": "lib-esm/index.js", "typings": "lib/index.d.ts", "engines": { "node": ">=10", 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/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/tsconfig.json b/tsconfig.json index 52ec2d1..79e11e1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,21 +1,14 @@ { "compilerOptions": { - "allowJs": false, - "declaration": true, - "diagnostics": true, - "esModuleInterop": true, - "extendedDiagnostics": true, - "forceConsistentCasingInFileNames": true, - "lib": ["ES2019"], - "listEmittedFiles": true, + "lib": ["es2021"], "module": "commonjs", - "outDir": "lib", - "removeComments": true, - "rootDir": "src", - "sourceMap": true, + "target": "es2021", + "outDir": "lib-commonjs", + "strict": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, "strictPropertyInitialization": false, - "target": "ES2019" }, "exclude": [ "node_modules", From 4ef00ff9a6dbaee293ce8036dff3e3e4e80b253b Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 7 Jan 2022 16:15:16 +1100 Subject: [PATCH 4/6] feat: make Reader a named export BREAKING CHANGE: Reader class is no longer a default export: ```diff - import Reader from 'mmdb-lib'; + import { Reader } from 'mmdb-lib'; const reader = new Reader(db); ``` --- .github/workflows/build.yml | 1 + .gitignore | 2 +- package.json | 7 +++---- src/decoder.ts | 3 +-- test.jsm | 3 --- test/imports/commonjs.js | 10 ++++++++++ test/imports/esm.mjs | 9 +++++++++ tsconfig.json | 2 +- 8 files changed, 26 insertions(+), 11 deletions(-) delete mode 100644 test.jsm create mode 100644 test/imports/commonjs.js create mode 100644 test/imports/esm.mjs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 15fe1b9..7eac162 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,3 +26,4 @@ jobs: - run: npm run lint - run: npm test - run: npm run build + - run: npm run test-imports diff --git a/.gitignore b/.gitignore index af4ed01..29bd1c1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,6 @@ node_modules coverage npm-debug.log .nyc_output -lib-* +lib package-lock.json local diff --git a/package.json b/package.json index 5cc4f79..1f46543 100644 --- a/package.json +++ b/package.json @@ -39,11 +39,9 @@ "url": "http://github.com/runk/mmdb-lib/issues" }, "files": [ - "lib-commonjs", - "lib-esm" + "lib" ], - "main": "lib-commonjs/index.js", - "module": "lib-esm/index.js", + "main": "lib/index.js", "typings": "lib/index.d.ts", "engines": { "node": ">=10", @@ -55,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/decoder.ts b/src/decoder.ts index b2632c9..4d9aa4b 100755 --- a/src/decoder.ts +++ b/src/decoder.ts @@ -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)); + integer * BigInt(4294967296) + BigInt(buffer.readUInt32BE(i << 2)); } return integer.toString(); diff --git a/test.jsm b/test.jsm deleted file mode 100644 index 63f903e..0000000 --- a/test.jsm +++ /dev/null @@ -1,3 +0,0 @@ -import Reader from './lib'; // Reader is equal to {default: ActuallyReader} - -// import {default as Reader} from 'mmdb-lib'; // Reader is equal to {default: ActuallyReader} because default is a special keyword here \ No newline at end of file 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 79e11e1..dfbf40a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "lib": ["es2021"], "module": "commonjs", "target": "es2021", - "outDir": "lib-commonjs", + "outDir": "lib", "strict": true, "esModuleInterop": true, From c88d033471a572796bc20d18f6e8c18cc449cc1c Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 7 Jan 2022 16:22:26 +1100 Subject: [PATCH 5/6] fix: Stop testing for node 10 --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7eac162..d72a8dd 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 From 1f56a96ba1bb84884d0291d770af208d2da5a5b5 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 7 Jan 2022 16:24:31 +1100 Subject: [PATCH 6/6] latest sem ver --- .github/workflows/build.yml | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d72a8dd..a6f406f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - node-version: [12.x, 14.x., 16.x] + node-version: [12.x, 14.x, 16.x] steps: - uses: actions/checkout@v2 diff --git a/package.json b/package.json index 1f46543..514c87e 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "ip-address": "6.2.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": "^27.1.2", "typescript": "4.5.4"