-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathnative.js
85 lines (82 loc) · 3.01 KB
/
native.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
import { dirname, join, default as pathModule } from 'path';
import { fileURLToPath } from 'url';
import loadNAPI from 'node-gyp-build-optional-packages';
export let Env, Txn, Dbi, Compression, Cursor, getAddress, createBufferForAddress, clearKeptObjects, globalBuffer, setGlobalBuffer, arch, fs, os, onExit, tmpdir, lmdbError, path, EventEmitter, orderedBinary, MsgpackrEncoder, WeakLRUCache, setEnvMap, getEnvMap, getByBinary, detachBuffer, startRead, write, position, iterate, prefetch, resetTxn, getCurrentValue, getCurrentShared, getStringByBinary, getSharedByBinary, getSharedBuffer, compress;
path = pathModule;
let dirName = (typeof __dirname == 'string' ? __dirname : // for bun, which doesn't have fileURLToPath
dirname(fileURLToPath(import.meta.url))).replace(/dist$/, ''); // for node, which doesn't have __dirname in ESM
export let nativeAddon = loadNAPI(dirName);
if (process.isBun) {
const { linkSymbols, FFIType } = require('bun:ffi');
let lmdbLib = linkSymbols({
getByBinary: {
args: [FFIType.f64, FFIType.u32],
returns: FFIType.u32,
ptr: nativeAddon.getByBinaryPtr
},
iterate: {
args: [FFIType.f64],
returns: FFIType.i32,
ptr: nativeAddon.iteratePtr,
},
position: {
args: [FFIType.f64, FFIType.u32, FFIType.u32, FFIType.u32, FFIType.f64],
returns: FFIType.i32,
ptr: nativeAddon.positionPtr,
},
write: {
args: [FFIType.f64, FFIType.f64],
returns: FFIType.i32,
ptr: nativeAddon.writePtr,
},
resetTxn: {
args: [FFIType.f64],
returns: FFIType.void,
ptr: nativeAddon.resetTxnPtr,
}
});
for (let key in lmdbLib.symbols) {
nativeAddon[key] = lmdbLib.symbols[key].native;
}
}
setNativeFunctions(nativeAddon);
export function setNativeFunctions(externals) {
Env = externals.Env;
Txn = externals.Txn;
Dbi = externals.Dbi;
Compression = externals.Compression;
getAddress = externals.getAddress;
createBufferForAddress = externals.createBufferForAddress;
clearKeptObjects = externals.clearKeptObjects || function() {};
getByBinary = externals.getByBinary;
detachBuffer = externals.detachBuffer;
startRead = externals.startRead;
setGlobalBuffer = externals.setGlobalBuffer;
globalBuffer = externals.globalBuffer;
getSharedBuffer = externals.getSharedBuffer;
prefetch = externals.prefetch;
iterate = externals.iterate;
position = externals.position;
resetTxn = externals.resetTxn;
getCurrentValue = externals.getCurrentValue;
getCurrentShared = externals.getCurrentShared;
getStringByBinary = externals.getStringByBinary;
getSharedByBinary = externals.getSharedByBinary;
write = externals.write;
compress = externals.compress;
Cursor = externals.Cursor;
lmdbError = externals.lmdbError;
if (externals.tmpdir)
tmpdir = externals.tmpdir
}
export function setExternals(externals) {
arch = externals.arch;
fs = externals.fs;
EventEmitter = externals.EventEmitter;
orderedBinary = externals.orderedBinary;
MsgpackrEncoder = externals.MsgpackrEncoder;
WeakLRUCache = externals.WeakLRUCache;
tmpdir = externals.tmpdir;
os = externals.os;
onExit = externals.onExit;
}