Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,3 +26,4 @@ jobs:
- run: npm run lint
- run: npm test
- run: npm run build
- run: npm run test-imports
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<CityResponse>(db);
const reader = new mmdb.Reader<CityResponse>(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]`
```
Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@
"geoip2"
],
"author": "Dmitry Shirokov <deadrunk@gmail.com>",
"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",
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
13 changes: 6 additions & 7 deletions src/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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 =>
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ReaderOptions } from './types';

const DATA_SECTION_SEPARATOR_SIZE = 16;

export default class Reader<T extends Response> {
export class Reader<T extends Response> {
public metadata: Metadata;
private decoder: Decoder;
private db: Buffer;
Expand Down
4 changes: 2 additions & 2 deletions src/reader/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/imports/commonjs.js
Original file line number Diff line number Diff line change
@@ -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');
9 changes: 9 additions & 0 deletions test/imports/esm.mjs
Original file line number Diff line number Diff line change
@@ -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');
20 changes: 6 additions & 14 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down