k2hash 1.1.34 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/binding.gyp +106 -0
- package/build/cjs/index.js +296 -0
- package/build/esm/index.js +293 -0
- package/buildutils/make_node_prebuild_variables.sh +359 -0
- package/buildutils/node_prebuild.sh +371 -0
- package/buildutils/node_prebuild_install.sh +307 -0
- package/index.js +4 -1
- package/index.mjs +154 -0
- package/package.json +76 -20
- package/src/index.ts +378 -0
- package/src/k2h_cbs.cc +27 -41
- package/src/k2h_cbs.h +17 -13
- package/src/k2h_common.h +1 -1
- package/src/k2h_keyqueue.cc +513 -339
- package/src/k2h_keyqueue.h +38 -30
- package/src/k2h_keyqueue_async.h +222 -184
- package/src/k2h_queue.cc +483 -304
- package/src/k2h_queue.h +38 -30
- package/src/k2h_queue_async.h +218 -173
- package/src/k2h_shm.cc +1963 -1221
- package/src/k2h_shm.h +109 -104
- package/src/k2h_shm_async.h +722 -560
- package/src/k2hash.cc +22 -8
- package/src/k2hkeyqueue.cc +21 -8
- package/src/k2hqueue.cc +21 -8
- package/types/index.d.ts +570 -0
- package/ChangeLog +0 -137
- package/src/binding.gyp +0 -146
package/binding.gyp
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
{
|
|
2
|
+
"variables": {
|
|
3
|
+
"coverage": "false",
|
|
4
|
+
"openssl_fips": ""
|
|
5
|
+
},
|
|
6
|
+
"targets": [
|
|
7
|
+
{
|
|
8
|
+
"target_name": "k2hash",
|
|
9
|
+
"sources": [
|
|
10
|
+
"src/k2hash.cc",
|
|
11
|
+
"src/k2h_shm.cc",
|
|
12
|
+
"src/k2h_queue.cc",
|
|
13
|
+
"src/k2h_keyqueue.cc",
|
|
14
|
+
"src/k2h_cbs.cc"
|
|
15
|
+
],
|
|
16
|
+
"include_dirs": [
|
|
17
|
+
"<!(node -p \"require('node-addon-api').include\")",
|
|
18
|
+
"<(module_root_dir)/node_modules/node-addon-api"
|
|
19
|
+
],
|
|
20
|
+
"dependencies": [
|
|
21
|
+
"<!(node -p \"require('node-addon-api').gyp\")"
|
|
22
|
+
],
|
|
23
|
+
"cflags!": [
|
|
24
|
+
"-fno-exceptions"
|
|
25
|
+
],
|
|
26
|
+
"cflags_cc!": [
|
|
27
|
+
"-fno-exceptions"
|
|
28
|
+
],
|
|
29
|
+
"cflags_cc": [
|
|
30
|
+
"-std=c++17"
|
|
31
|
+
],
|
|
32
|
+
"defines": [
|
|
33
|
+
"NAPI_CPP_EXCEPTIONS"
|
|
34
|
+
],
|
|
35
|
+
"link_settings": {
|
|
36
|
+
"libraries": [
|
|
37
|
+
"-lk2hash"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"target_name": "k2hqueue",
|
|
43
|
+
"sources": [
|
|
44
|
+
"src/k2hqueue.cc",
|
|
45
|
+
"src/k2h_queue.cc",
|
|
46
|
+
"src/k2h_cbs.cc"
|
|
47
|
+
],
|
|
48
|
+
"include_dirs": [
|
|
49
|
+
"<!(node -p \"require('node-addon-api').include\")",
|
|
50
|
+
"<(module_root_dir)/node_modules/node-addon-api"
|
|
51
|
+
],
|
|
52
|
+
"dependencies": [
|
|
53
|
+
"<!(node -p \"require('node-addon-api').gyp\")"
|
|
54
|
+
],
|
|
55
|
+
"cflags!": [
|
|
56
|
+
"-fno-exceptions"
|
|
57
|
+
],
|
|
58
|
+
"cflags_cc!": [
|
|
59
|
+
"-fno-exceptions"
|
|
60
|
+
],
|
|
61
|
+
"cflags_cc": [
|
|
62
|
+
"-std=c++17"
|
|
63
|
+
],
|
|
64
|
+
"defines": [
|
|
65
|
+
"NAPI_CPP_EXCEPTIONS"
|
|
66
|
+
],
|
|
67
|
+
"link_settings": {
|
|
68
|
+
"libraries": [
|
|
69
|
+
"-lk2hash"
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"target_name": "k2hkeyqueue",
|
|
75
|
+
"sources": [
|
|
76
|
+
"src/k2hkeyqueue.cc",
|
|
77
|
+
"src/k2h_keyqueue.cc",
|
|
78
|
+
"src/k2h_cbs.cc"
|
|
79
|
+
],
|
|
80
|
+
"include_dirs": [
|
|
81
|
+
"<!(node -p \"require('node-addon-api').include\")",
|
|
82
|
+
"<(module_root_dir)/node_modules/node-addon-api"
|
|
83
|
+
],
|
|
84
|
+
"dependencies": [
|
|
85
|
+
"<!(node -p \"require('node-addon-api').gyp\")"
|
|
86
|
+
],
|
|
87
|
+
"cflags!": [
|
|
88
|
+
"-fno-exceptions"
|
|
89
|
+
],
|
|
90
|
+
"cflags_cc!": [
|
|
91
|
+
"-fno-exceptions"
|
|
92
|
+
],
|
|
93
|
+
"cflags_cc": [
|
|
94
|
+
"-std=c++17"
|
|
95
|
+
],
|
|
96
|
+
"defines": [
|
|
97
|
+
"NAPI_CPP_EXCEPTIONS"
|
|
98
|
+
],
|
|
99
|
+
"link_settings": {
|
|
100
|
+
"libraries": [
|
|
101
|
+
"-lk2hash"
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* K2HASH
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2015 Yahoo Japan Corporation.
|
|
6
|
+
*
|
|
7
|
+
* K2HASH is key-valuew store base libraries.
|
|
8
|
+
* K2HASH is made for the purpose of the construction of
|
|
9
|
+
* original KVS system and the offer of the library.
|
|
10
|
+
* The characteristic is this KVS library which Key can
|
|
11
|
+
* layer. And can support multi-processing and multi-thread,
|
|
12
|
+
* and is provided safely as available KVS.
|
|
13
|
+
*
|
|
14
|
+
* For the full copyright and license information, please view
|
|
15
|
+
* the license file that was distributed with this source code.
|
|
16
|
+
*
|
|
17
|
+
* AUTHOR: Takeshi Nakatani
|
|
18
|
+
* CREATE: Wed 19 Nov 2025
|
|
19
|
+
* REVISION:
|
|
20
|
+
*/
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.K2hKeyQueue = exports.K2hQueue = exports.K2hNode = void 0;
|
|
23
|
+
let _native = undefined;
|
|
24
|
+
let _nativeLoaded = false;
|
|
25
|
+
// [NOTE]
|
|
26
|
+
// Copy properties from native onto the factory(mirrors original runtime
|
|
27
|
+
// normalization)
|
|
28
|
+
// We try to preserve descriptors when possible.
|
|
29
|
+
//
|
|
30
|
+
function copyPropsToFactory(k2hashFactory, factory_native) {
|
|
31
|
+
if (!factory_native || (typeof factory_native !== 'object' && typeof factory_native !== 'function')) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
Object.getOwnPropertyNames(factory_native).forEach((name) => {
|
|
36
|
+
if (name === 'prototype') {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const desc = Object.getOwnPropertyDescriptor(factory_native, name);
|
|
41
|
+
if (desc) {
|
|
42
|
+
Object.defineProperty(k2hashFactory, name, desc);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
try {
|
|
47
|
+
k2hashFactory[name] = factory_native[name];
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// ignore
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
Object.getOwnPropertySymbols(factory_native).forEach((sym) => {
|
|
55
|
+
try {
|
|
56
|
+
const desc = Object.getOwnPropertyDescriptor(factory_native, sym);
|
|
57
|
+
if (desc) {
|
|
58
|
+
Object.defineProperty(k2hashFactory, sym, desc);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
try {
|
|
63
|
+
k2hashFactory[sym] = factory_native[sym];
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// ignore
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// ignore odd cases(native undefined or primitive)
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
if (factory_native && factory_native.K2hNode) {
|
|
76
|
+
k2hashFactory.K2hNode = factory_native.K2hNode;
|
|
77
|
+
}
|
|
78
|
+
if (factory_native && factory_native.K2hQueue) {
|
|
79
|
+
k2hashFactory.K2hQueue = factory_native.K2hQueue;
|
|
80
|
+
}
|
|
81
|
+
if (factory_native && factory_native.K2hKeyQueue) {
|
|
82
|
+
k2hashFactory.K2hKeyQueue = factory_native.K2hKeyQueue;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
// ignore
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// [NOTE]
|
|
90
|
+
// Load native on first need. Also call copyPropsToFactory once after
|
|
91
|
+
// loading.
|
|
92
|
+
// require('bindings') may throw if native not present at build/test time
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
94
|
+
//
|
|
95
|
+
function ensureNative() {
|
|
96
|
+
if (_nativeLoaded) {
|
|
97
|
+
return _native;
|
|
98
|
+
}
|
|
99
|
+
_nativeLoaded = true;
|
|
100
|
+
try {
|
|
101
|
+
const bindings = require('bindings');
|
|
102
|
+
_native = bindings('k2hash');
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
_native = undefined;
|
|
106
|
+
}
|
|
107
|
+
// [NOTE]
|
|
108
|
+
// If we successfully loaded native, copy properties onto the factory so
|
|
109
|
+
// default export retains the same shape as before (callable + named
|
|
110
|
+
// properties).
|
|
111
|
+
//
|
|
112
|
+
if (_native) {
|
|
113
|
+
try {
|
|
114
|
+
// [NOTE]
|
|
115
|
+
// k2hashFactory is hoisted(function declaration), safe to
|
|
116
|
+
// reference here
|
|
117
|
+
copyPropsToFactory(k2hashFactory, _native);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// swallow copy errors to preserve robustness
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return _native;
|
|
124
|
+
}
|
|
125
|
+
// [NOTE]
|
|
126
|
+
// Default factory that mirrors previous behavior
|
|
127
|
+
//
|
|
128
|
+
function k2hashFactory(...args) {
|
|
129
|
+
const _factory_native = ensureNative();
|
|
130
|
+
if (typeof _factory_native === 'function') {
|
|
131
|
+
try {
|
|
132
|
+
const retobj = _factory_native.apply(null, args);
|
|
133
|
+
if (retobj !== undefined) {
|
|
134
|
+
return retobj;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// fallthrough to constructor fallback
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (_factory_native && typeof _factory_native.K2hNode === 'function') {
|
|
142
|
+
try {
|
|
143
|
+
return new _factory_native.K2hNode(...args);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
// fallthrough
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// fallback: return native itself (could be an object with properties)
|
|
150
|
+
return _factory_native;
|
|
151
|
+
}
|
|
152
|
+
// [NOTE]
|
|
153
|
+
// Returns a callable & constructible Proxy that forwards to native export
|
|
154
|
+
//
|
|
155
|
+
function createLazyProxy(name) {
|
|
156
|
+
let _cached = undefined;
|
|
157
|
+
function loadActual() {
|
|
158
|
+
if (_cached !== undefined) {
|
|
159
|
+
return _cached;
|
|
160
|
+
}
|
|
161
|
+
const _lazy_native = ensureNative();
|
|
162
|
+
const _actual = _lazy_native && _lazy_native[name] ? _lazy_native[name] : undefined;
|
|
163
|
+
if (!_actual) {
|
|
164
|
+
throw new Error("Native export " + JSON.stringify(name) + " is not available");
|
|
165
|
+
}
|
|
166
|
+
_cached = _actual;
|
|
167
|
+
return _cached;
|
|
168
|
+
}
|
|
169
|
+
const target = function (...args) {
|
|
170
|
+
const _actual = loadActual();
|
|
171
|
+
return _actual.apply(this, args);
|
|
172
|
+
};
|
|
173
|
+
const handler = {
|
|
174
|
+
apply(_t, thisArg, args) {
|
|
175
|
+
const _actual = loadActual();
|
|
176
|
+
return _actual.apply(thisArg, args);
|
|
177
|
+
},
|
|
178
|
+
construct(_t, args, newTarget) {
|
|
179
|
+
const _actual = loadActual();
|
|
180
|
+
return Reflect.construct(_actual, args, newTarget);
|
|
181
|
+
},
|
|
182
|
+
get(_t, prop, receiver) {
|
|
183
|
+
const _actual = loadActual();
|
|
184
|
+
return Reflect.get(_actual, prop, receiver);
|
|
185
|
+
},
|
|
186
|
+
set(_t, prop, value, receiver) {
|
|
187
|
+
const _actual = loadActual();
|
|
188
|
+
return Reflect.set(_actual, prop, value, receiver);
|
|
189
|
+
},
|
|
190
|
+
has(_t, prop) {
|
|
191
|
+
const _actual = loadActual();
|
|
192
|
+
return prop in _actual;
|
|
193
|
+
},
|
|
194
|
+
ownKeys(_t) {
|
|
195
|
+
const _actual = loadActual();
|
|
196
|
+
return Reflect.ownKeys(_actual);
|
|
197
|
+
},
|
|
198
|
+
getOwnPropertyDescriptor(_t, prop) {
|
|
199
|
+
const _actual = loadActual();
|
|
200
|
+
return Object.getOwnPropertyDescriptor(_actual, prop) || undefined;
|
|
201
|
+
},
|
|
202
|
+
getPrototypeOf(_t) {
|
|
203
|
+
const _actual = loadActual();
|
|
204
|
+
return Object.getPrototypeOf(_actual);
|
|
205
|
+
},
|
|
206
|
+
setPrototypeOf(_t, proto) {
|
|
207
|
+
const _actual = loadActual();
|
|
208
|
+
return Object.setPrototypeOf(_actual, proto);
|
|
209
|
+
},
|
|
210
|
+
defineProperty(_t, prop, descriptor) {
|
|
211
|
+
const _actual = loadActual();
|
|
212
|
+
return Reflect.defineProperty(_actual, prop, descriptor);
|
|
213
|
+
},
|
|
214
|
+
deleteProperty(_t, prop) {
|
|
215
|
+
const _actual = loadActual();
|
|
216
|
+
return Reflect.deleteProperty(_actual, prop);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
return new Proxy(target, handler);
|
|
220
|
+
}
|
|
221
|
+
//
|
|
222
|
+
// Export named proxies to keep compatibility with existing consumers
|
|
223
|
+
//
|
|
224
|
+
// Named convenience exports (runtime values).
|
|
225
|
+
// Types are provided by types/index.d.ts.
|
|
226
|
+
//
|
|
227
|
+
exports.K2hNode = createLazyProxy('K2hNode');
|
|
228
|
+
exports.K2hQueue = createLazyProxy('K2hQueue');
|
|
229
|
+
exports.K2hKeyQueue = createLazyProxy('K2hKeyQueue');
|
|
230
|
+
exports.default = k2hashFactory;
|
|
231
|
+
try {
|
|
232
|
+
if (typeof module !== 'undefined' && module && module.exports) {
|
|
233
|
+
const own_module_export = module.exports;
|
|
234
|
+
//
|
|
235
|
+
// If module.exports is exactly { default: fn }, replace module.exports with fn.
|
|
236
|
+
//
|
|
237
|
+
if (own_module_export &&
|
|
238
|
+
typeof own_module_export === 'object' &&
|
|
239
|
+
Object.prototype.hasOwnProperty.call(own_module_export, 'default') &&
|
|
240
|
+
typeof own_module_export.default === 'function' &&
|
|
241
|
+
Object.keys(own_module_export).length === 1) {
|
|
242
|
+
module.exports = own_module_export.default;
|
|
243
|
+
}
|
|
244
|
+
else if (own_module_export && typeof own_module_export === 'object' && typeof own_module_export.default === 'function') {
|
|
245
|
+
//
|
|
246
|
+
// If default is a function but other named exports exist,
|
|
247
|
+
// prefer a top-level callable while preserving named props.
|
|
248
|
+
//
|
|
249
|
+
try {
|
|
250
|
+
const defaultFn = own_module_export.default;
|
|
251
|
+
// If top-level is not already callable, make it so.
|
|
252
|
+
if (typeof own_module_export !== 'function') {
|
|
253
|
+
// create a callable wrapper that forwards to defaultFn
|
|
254
|
+
const callable = function (...args) {
|
|
255
|
+
return defaultFn.apply(this, args);
|
|
256
|
+
};
|
|
257
|
+
// copy named properties from original exports except 'default'
|
|
258
|
+
Object.keys(own_module_export).forEach((key) => {
|
|
259
|
+
if (key === 'default') {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
try {
|
|
263
|
+
callable[key] = own_module_export[key];
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
// ignore
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
// preserve reference to original module
|
|
270
|
+
try {
|
|
271
|
+
callable.__orig_module = own_module_export;
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// ignore
|
|
275
|
+
}
|
|
276
|
+
// replace module.exports with callable that also carries named props
|
|
277
|
+
module.exports = callable;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
// swallow normalization errors - not critical
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
// ignore
|
|
288
|
+
}
|
|
289
|
+
/*
|
|
290
|
+
* Local variables:
|
|
291
|
+
* tab-width: 4
|
|
292
|
+
* c-basic-offset: 4
|
|
293
|
+
* End:
|
|
294
|
+
* vim600: noexpandtab sw=4 ts=4 fdm=marker
|
|
295
|
+
* vim<600: noexpandtab sw=4 ts=4
|
|
296
|
+
*/
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* K2HASH
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2015 Yahoo Japan Corporation.
|
|
5
|
+
*
|
|
6
|
+
* K2HASH is key-valuew store base libraries.
|
|
7
|
+
* K2HASH is made for the purpose of the construction of
|
|
8
|
+
* original KVS system and the offer of the library.
|
|
9
|
+
* The characteristic is this KVS library which Key can
|
|
10
|
+
* layer. And can support multi-processing and multi-thread,
|
|
11
|
+
* and is provided safely as available KVS.
|
|
12
|
+
*
|
|
13
|
+
* For the full copyright and license information, please view
|
|
14
|
+
* the license file that was distributed with this source code.
|
|
15
|
+
*
|
|
16
|
+
* AUTHOR: Takeshi Nakatani
|
|
17
|
+
* CREATE: Wed 19 Nov 2025
|
|
18
|
+
* REVISION:
|
|
19
|
+
*/
|
|
20
|
+
let _native = undefined;
|
|
21
|
+
let _nativeLoaded = false;
|
|
22
|
+
// [NOTE]
|
|
23
|
+
// Copy properties from native onto the factory(mirrors original runtime
|
|
24
|
+
// normalization)
|
|
25
|
+
// We try to preserve descriptors when possible.
|
|
26
|
+
//
|
|
27
|
+
function copyPropsToFactory(k2hashFactory, factory_native) {
|
|
28
|
+
if (!factory_native || (typeof factory_native !== 'object' && typeof factory_native !== 'function')) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
Object.getOwnPropertyNames(factory_native).forEach((name) => {
|
|
33
|
+
if (name === 'prototype') {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const desc = Object.getOwnPropertyDescriptor(factory_native, name);
|
|
38
|
+
if (desc) {
|
|
39
|
+
Object.defineProperty(k2hashFactory, name, desc);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
try {
|
|
44
|
+
k2hashFactory[name] = factory_native[name];
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// ignore
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
Object.getOwnPropertySymbols(factory_native).forEach((sym) => {
|
|
52
|
+
try {
|
|
53
|
+
const desc = Object.getOwnPropertyDescriptor(factory_native, sym);
|
|
54
|
+
if (desc) {
|
|
55
|
+
Object.defineProperty(k2hashFactory, sym, desc);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
try {
|
|
60
|
+
k2hashFactory[sym] = factory_native[sym];
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// ignore
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// ignore odd cases(native undefined or primitive)
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
if (factory_native && factory_native.K2hNode) {
|
|
73
|
+
k2hashFactory.K2hNode = factory_native.K2hNode;
|
|
74
|
+
}
|
|
75
|
+
if (factory_native && factory_native.K2hQueue) {
|
|
76
|
+
k2hashFactory.K2hQueue = factory_native.K2hQueue;
|
|
77
|
+
}
|
|
78
|
+
if (factory_native && factory_native.K2hKeyQueue) {
|
|
79
|
+
k2hashFactory.K2hKeyQueue = factory_native.K2hKeyQueue;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
// ignore
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// [NOTE]
|
|
87
|
+
// Load native on first need. Also call copyPropsToFactory once after
|
|
88
|
+
// loading.
|
|
89
|
+
// require('bindings') may throw if native not present at build/test time
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
91
|
+
//
|
|
92
|
+
function ensureNative() {
|
|
93
|
+
if (_nativeLoaded) {
|
|
94
|
+
return _native;
|
|
95
|
+
}
|
|
96
|
+
_nativeLoaded = true;
|
|
97
|
+
try {
|
|
98
|
+
const bindings = require('bindings');
|
|
99
|
+
_native = bindings('k2hash');
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
_native = undefined;
|
|
103
|
+
}
|
|
104
|
+
// [NOTE]
|
|
105
|
+
// If we successfully loaded native, copy properties onto the factory so
|
|
106
|
+
// default export retains the same shape as before (callable + named
|
|
107
|
+
// properties).
|
|
108
|
+
//
|
|
109
|
+
if (_native) {
|
|
110
|
+
try {
|
|
111
|
+
// [NOTE]
|
|
112
|
+
// k2hashFactory is hoisted(function declaration), safe to
|
|
113
|
+
// reference here
|
|
114
|
+
copyPropsToFactory(k2hashFactory, _native);
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
// swallow copy errors to preserve robustness
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return _native;
|
|
121
|
+
}
|
|
122
|
+
// [NOTE]
|
|
123
|
+
// Default factory that mirrors previous behavior
|
|
124
|
+
//
|
|
125
|
+
function k2hashFactory(...args) {
|
|
126
|
+
const _factory_native = ensureNative();
|
|
127
|
+
if (typeof _factory_native === 'function') {
|
|
128
|
+
try {
|
|
129
|
+
const retobj = _factory_native.apply(null, args);
|
|
130
|
+
if (retobj !== undefined) {
|
|
131
|
+
return retobj;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// fallthrough to constructor fallback
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (_factory_native && typeof _factory_native.K2hNode === 'function') {
|
|
139
|
+
try {
|
|
140
|
+
return new _factory_native.K2hNode(...args);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// fallthrough
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// fallback: return native itself (could be an object with properties)
|
|
147
|
+
return _factory_native;
|
|
148
|
+
}
|
|
149
|
+
// [NOTE]
|
|
150
|
+
// Returns a callable & constructible Proxy that forwards to native export
|
|
151
|
+
//
|
|
152
|
+
function createLazyProxy(name) {
|
|
153
|
+
let _cached = undefined;
|
|
154
|
+
function loadActual() {
|
|
155
|
+
if (_cached !== undefined) {
|
|
156
|
+
return _cached;
|
|
157
|
+
}
|
|
158
|
+
const _lazy_native = ensureNative();
|
|
159
|
+
const _actual = _lazy_native && _lazy_native[name] ? _lazy_native[name] : undefined;
|
|
160
|
+
if (!_actual) {
|
|
161
|
+
throw new Error("Native export " + JSON.stringify(name) + " is not available");
|
|
162
|
+
}
|
|
163
|
+
_cached = _actual;
|
|
164
|
+
return _cached;
|
|
165
|
+
}
|
|
166
|
+
const target = function (...args) {
|
|
167
|
+
const _actual = loadActual();
|
|
168
|
+
return _actual.apply(this, args);
|
|
169
|
+
};
|
|
170
|
+
const handler = {
|
|
171
|
+
apply(_t, thisArg, args) {
|
|
172
|
+
const _actual = loadActual();
|
|
173
|
+
return _actual.apply(thisArg, args);
|
|
174
|
+
},
|
|
175
|
+
construct(_t, args, newTarget) {
|
|
176
|
+
const _actual = loadActual();
|
|
177
|
+
return Reflect.construct(_actual, args, newTarget);
|
|
178
|
+
},
|
|
179
|
+
get(_t, prop, receiver) {
|
|
180
|
+
const _actual = loadActual();
|
|
181
|
+
return Reflect.get(_actual, prop, receiver);
|
|
182
|
+
},
|
|
183
|
+
set(_t, prop, value, receiver) {
|
|
184
|
+
const _actual = loadActual();
|
|
185
|
+
return Reflect.set(_actual, prop, value, receiver);
|
|
186
|
+
},
|
|
187
|
+
has(_t, prop) {
|
|
188
|
+
const _actual = loadActual();
|
|
189
|
+
return prop in _actual;
|
|
190
|
+
},
|
|
191
|
+
ownKeys(_t) {
|
|
192
|
+
const _actual = loadActual();
|
|
193
|
+
return Reflect.ownKeys(_actual);
|
|
194
|
+
},
|
|
195
|
+
getOwnPropertyDescriptor(_t, prop) {
|
|
196
|
+
const _actual = loadActual();
|
|
197
|
+
return Object.getOwnPropertyDescriptor(_actual, prop) || undefined;
|
|
198
|
+
},
|
|
199
|
+
getPrototypeOf(_t) {
|
|
200
|
+
const _actual = loadActual();
|
|
201
|
+
return Object.getPrototypeOf(_actual);
|
|
202
|
+
},
|
|
203
|
+
setPrototypeOf(_t, proto) {
|
|
204
|
+
const _actual = loadActual();
|
|
205
|
+
return Object.setPrototypeOf(_actual, proto);
|
|
206
|
+
},
|
|
207
|
+
defineProperty(_t, prop, descriptor) {
|
|
208
|
+
const _actual = loadActual();
|
|
209
|
+
return Reflect.defineProperty(_actual, prop, descriptor);
|
|
210
|
+
},
|
|
211
|
+
deleteProperty(_t, prop) {
|
|
212
|
+
const _actual = loadActual();
|
|
213
|
+
return Reflect.deleteProperty(_actual, prop);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
return new Proxy(target, handler);
|
|
217
|
+
}
|
|
218
|
+
//
|
|
219
|
+
// Export named proxies to keep compatibility with existing consumers
|
|
220
|
+
//
|
|
221
|
+
// Named convenience exports (runtime values).
|
|
222
|
+
// Types are provided by types/index.d.ts.
|
|
223
|
+
//
|
|
224
|
+
export const K2hNode = createLazyProxy('K2hNode');
|
|
225
|
+
export const K2hQueue = createLazyProxy('K2hQueue');
|
|
226
|
+
export const K2hKeyQueue = createLazyProxy('K2hKeyQueue');
|
|
227
|
+
export default k2hashFactory;
|
|
228
|
+
try {
|
|
229
|
+
if (typeof module !== 'undefined' && module && module.exports) {
|
|
230
|
+
const own_module_export = module.exports;
|
|
231
|
+
//
|
|
232
|
+
// If module.exports is exactly { default: fn }, replace module.exports with fn.
|
|
233
|
+
//
|
|
234
|
+
if (own_module_export &&
|
|
235
|
+
typeof own_module_export === 'object' &&
|
|
236
|
+
Object.prototype.hasOwnProperty.call(own_module_export, 'default') &&
|
|
237
|
+
typeof own_module_export.default === 'function' &&
|
|
238
|
+
Object.keys(own_module_export).length === 1) {
|
|
239
|
+
module.exports = own_module_export.default;
|
|
240
|
+
}
|
|
241
|
+
else if (own_module_export && typeof own_module_export === 'object' && typeof own_module_export.default === 'function') {
|
|
242
|
+
//
|
|
243
|
+
// If default is a function but other named exports exist,
|
|
244
|
+
// prefer a top-level callable while preserving named props.
|
|
245
|
+
//
|
|
246
|
+
try {
|
|
247
|
+
const defaultFn = own_module_export.default;
|
|
248
|
+
// If top-level is not already callable, make it so.
|
|
249
|
+
if (typeof own_module_export !== 'function') {
|
|
250
|
+
// create a callable wrapper that forwards to defaultFn
|
|
251
|
+
const callable = function (...args) {
|
|
252
|
+
return defaultFn.apply(this, args);
|
|
253
|
+
};
|
|
254
|
+
// copy named properties from original exports except 'default'
|
|
255
|
+
Object.keys(own_module_export).forEach((key) => {
|
|
256
|
+
if (key === 'default') {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
callable[key] = own_module_export[key];
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
// ignore
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
// preserve reference to original module
|
|
267
|
+
try {
|
|
268
|
+
callable.__orig_module = own_module_export;
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
// ignore
|
|
272
|
+
}
|
|
273
|
+
// replace module.exports with callable that also carries named props
|
|
274
|
+
module.exports = callable;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
// swallow normalization errors - not critical
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
// ignore
|
|
285
|
+
}
|
|
286
|
+
/*
|
|
287
|
+
* Local variables:
|
|
288
|
+
* tab-width: 4
|
|
289
|
+
* c-basic-offset: 4
|
|
290
|
+
* End:
|
|
291
|
+
* vim600: noexpandtab sw=4 ts=4 fdm=marker
|
|
292
|
+
* vim<600: noexpandtab sw=4 ts=4
|
|
293
|
+
*/
|