Skip to content
Open
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
12 changes: 9 additions & 3 deletions src/lib/ipv4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { createDebug, ipv4ToLong } from "./utils";
import { existsSync, readFileSync } from "fs";
import { resolve as pathResolve, isAbsolute } from "path";
import path from "path";
import { isIPv4 } from "net";

const debug = createDebug("ipv4");
Expand Down Expand Up @@ -49,8 +49,14 @@ export default class Ipv4ToRegion {
private totalBlocks: number;

constructor(dbPath?: string) {
const p = dbPath || "../../data/ip2region.db";
this.dbFilePath = isAbsolute(p) ? p : pathResolve(__dirname, p);
if (!dbPath) {
this.dbFilePath = path.join(__dirname, "../../data/ip2region.db");
} else if (!path.isAbsolute(dbPath)) {
this.dbFilePath = path.join(__dirname, dbPath);
} else {
this.dbFilePath = dbPath;
}

if (!existsSync(this.dbFilePath)) {
throw new Error("[Ipv4ToRegion] db file not exists : " + this.dbFilePath);
}
Expand Down
12 changes: 9 additions & 3 deletions src/lib/ipv6.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createDebug, ipv6ToLong } from "./utils";
import { existsSync, readFileSync } from "fs";
import { resolve as pathResolve, isAbsolute } from "path";
import path from "path";
import { isIPv6 } from "net";
import Ipv4ToRegion, { Ipv4ToRegionRes, Ipv4ToRegionResult } from "./ipv4";

Expand Down Expand Up @@ -47,8 +47,14 @@ export default class Ipv6ToRegion {
private ipv4?: Ipv4ToRegion;

constructor(dbPath?: string) {
const p = dbPath || "../../data/ipv6wry.db";
this.dbFilePath = isAbsolute(p) ? p : pathResolve(__dirname, p);
if (!dbPath) {
this.dbFilePath = path.join(__dirname, "../../data/ipv6wry.db");
} else if (!path.isAbsolute(dbPath)) {
this.dbFilePath = path.join(__dirname, dbPath);
} else {
this.dbFilePath = dbPath;
}

if (!existsSync(this.dbFilePath)) {
throw new Error("[Ipv6ToRegion] db file not exists : " + this.dbFilePath);
}
Expand Down