-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (84 loc) · 3.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const { ClassicLevel } = require("classic-level");
const { RaveLevel } = require("rave-level");
const crypto = require("crypto");
const sha1 = (data) => {
return crypto.createHash("sha1").update(data).digest("hex");
};
let kDb = null;
// ClassicRave wrapper includes static methods and methods missing from RaveLevel but present on classic-level
class ClassicRave extends RaveLevel {
constructor(location, options = {}) {
let locationHash = null;
// We need to override the rave socket path using a hash of the location, as unix sockets
// have a max length of 108 characters and the location can very easily be too long and cause the
// sockets to fail to connect and enter an infinite loop when rave-level tries to open/connect to the socket.
if (process.platform === "win32") {
/*
Only relevant in Windows testing environments.
Create a location hash relative to Data, so that the named pipe can be consistent per package in Data.
*/
const normalizedPath = (location || "").replace(/\//g, "\\");
const dataPath = "\\Data\\";
const lastIndexOfData = normalizedPath.lastIndexOf(dataPath);
if (lastIndexOfData > -1) {
locationHash = sha1(location.slice(lastIndexOfData));
options.raveSocketPath = `\\\\.\\pipe\\rave-level\\${locationHash}`;
}
} else {
locationHash = sha1(location);
options.raveSocketPath = `/tmp/rave-level-${locationHash}.sock`;
}
super(location, options);
// Find and keep track of the symbol that references the ClassicLevel database
if (!kDb) {
for (const symbol of Object.getOwnPropertySymbols(this)) {
if (symbol.description === "db") {
// If found, store for future lookups
kDb = symbol;
break;
}
}
}
}
compactRange(start, end, options, callback) {
if (this[kDb]) {
return this[kDb].compactRange(start, end, options, callback);
}
if (typeof options === "function" && !callback) {
callback = options;
options = undefined;
} else if (typeof options !== "object") {
options = null;
}
if (callback) {
// Call the callback with null error
return callback(null);
}
return Promise.resolve(0);
}
approximateSize(start, end, options, callback) {
if (this[kDb]) {
return this[kDb].approximateSize(start, end, options, callback);
}
if (typeof options === "function" && !callback) {
callback = options;
options = undefined;
} else if (typeof options !== "object") {
options = null;
}
if (callback) {
// Call the callback with null error and size of 0 as default
return callback(null, 0);
}
return Promise.resolve(0);
}
static destroy(location, callback) {
return ClassicLevel.destroy(location, callback);
}
static repair(location, callback) {
return ClassicLevel.repair(location, callback);
}
}
module.exports = {
ClassicLevel: ClassicRave,
};