-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-binding.ts
More file actions
135 lines (117 loc) · 4.44 KB
/
load-binding.ts
File metadata and controls
135 lines (117 loc) · 4.44 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { dirname, join, resolve } from 'node:path';
import { readdirSync } from 'node:fs';
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import type { BufferWithDataView, Key } from './encoding.js';
import type { IteratorOptions, RangeOptions } from './dbi.js';
import type { Context } from './store.js';
export type TransactionOptions = {
/**
* Whether to disable snapshots.
*
* @default false
*/
disableSnapshot?: boolean;
};
export type NativeTransaction = {
id: number;
new(context: NativeDatabase, options?: TransactionOptions): NativeTransaction;
abort(): void;
commit(resolve: (timestamp: number) => void, reject: (err: Error) => void): void;
commitSync(): number;
get(key: Key, resolve: (value: Buffer) => void, reject: (err: Error) => void): number;
getCount(options?: RangeOptions): number;
getSync(key: Key): Buffer;
putSync(key: Key, value: Buffer | Uint8Array, txnId?: number): void;
removeSync(key: Key): void;
};
export declare class NativeIteratorCls<T> implements Iterator<T> {
constructor(context: Context, options: IteratorOptions);
next(): IteratorResult<T>;
return(): IteratorResult<T>;
throw(): IteratorResult<T>;
}
export type NativeDatabaseMode = 'optimistic' | 'pessimistic';
export type NativeDatabaseOptions = {
name?: string;
noBlockCache?: boolean;
parallelismThreads?: number;
mode?: NativeDatabaseMode;
};
type ResolveCallback<T> = (value: T) => void;
type RejectCallback = (err: Error) => void;
export type UserSharedBufferCallback = () => void;
export type NativeDatabase = {
new(): NativeDatabase;
addListener(event: string, callback: (...args: any[]) => void): void;
clear(resolve: ResolveCallback<number>, reject: RejectCallback, batchSize?: number): void;
clearSync(batchSize?: number): number;
close(): void;
notify(event: string | BufferWithDataView, args?: any[]): boolean;
get(key: BufferWithDataView, resolve: ResolveCallback<Buffer>, reject: RejectCallback, txnId?: number): number;
getCount(options?: RangeOptions, txnId?: number): number;
getOldestSnapshotTimestamp(): number;
getSync(key: BufferWithDataView, txnId?: number): Buffer;
getUserSharedBuffer(key: BufferWithDataView, defaultBuffer: ArrayBuffer, callback?: UserSharedBufferCallback): ArrayBuffer;
hasLock(key: BufferWithDataView): boolean;
listeners(event: string | BufferWithDataView): number;
opened: boolean;
open(
path: string,
options?: NativeDatabaseOptions
): void;
putSync(key: BufferWithDataView, value: any, txnId?: number): void;
removeListener(event: string | BufferWithDataView, callback: () => void): boolean;
removeSync(key: BufferWithDataView, txnId?: number): void;
tryLock(key: BufferWithDataView, callback?: () => void): boolean;
unlock(key: BufferWithDataView): void;
withLock(key: BufferWithDataView, callback: () => void | Promise<void>): Promise<void>;
};
export type RocksDatabaseConfig = {
blockCacheSize?: number;
};
const nativeExtRE = /\.node$/;
/**
* Locates the native binding in the `build` directory, then the `prebuilds`
* directory.
*
* @returns The path to the native binding.
*/
function locateBinding(): string {
const baseDir = dirname(dirname(fileURLToPath(import.meta.url)));
for (const type of ['Release', 'Debug'] as const) {
try {
const dir = join(baseDir, 'build', type);
const files = readdirSync(dir);
for (const file of files) {
if (nativeExtRE.test(file)) {
return resolve(dir, file);
}
}
/* v8 ignore next -- @preserve */
} catch {}
}
// the following lines are non-trivial to test, so we'll ignore them
/* v8 ignore next 17 -- @preserve */
// check prebuilds
try {
for (const target of readdirSync(join(baseDir, 'prebuilds'))) {
const [platform, arch] = target.split('-');
if (platform === process.platform && arch === process.arch) {
for (const binding of readdirSync(join(baseDir, 'prebuilds', target))) {
if (nativeExtRE.test(binding)) {
return resolve(join(baseDir, 'prebuilds', target, binding));
}
}
}
}
} catch {}
throw new Error('Unable to locate rocksdb-js native binding');
}
const req = createRequire(import.meta.url);
const binding = req(locateBinding());
export const config: (options: RocksDatabaseConfig) => void = binding.config;
export const NativeDatabase: NativeDatabase = binding.Database;
export const NativeIterator: typeof NativeIteratorCls = binding.Iterator;
export const NativeTransaction: NativeTransaction = binding.Transaction;
export const version = binding.version;