Skip to content

Commit 0c7919c

Browse files
committed
Enable thread safe calls so async transactions can run on Bun and Deno now that they are properly implemented, #259
1 parent 2878282 commit 0c7919c

File tree

2 files changed

+113
-93
lines changed

2 files changed

+113
-93
lines changed

node-index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { createRequire } from 'module';
22
import { setRequire } from './open.js';
33
import { nativeAddon, setNativeFunctions } from './native.js';
4-
export { nativeAddon } from './native.js'
4+
export { nativeAddon } from './native.js';
55
let require = createRequire(import.meta.url);
66
setRequire(require);
7-
export let v8AccelerationEnabled = false
7+
export let v8AccelerationEnabled = false;
88

99
let versions = process.versions;
1010
if (!versions.deno && !process.isBun) {
11-
let [ majorVersion, minorVersion ] = versions.node.split('.');
11+
let [majorVersion, minorVersion] = versions.node.split('.');
1212
if (versions.v8 && +majorVersion == nativeAddon.version.nodeCompiledVersion) {
1313
let v8Funcs = {};
14-
let fastApiCalls = (majorVersion == 17 || majorVersion == 18 || majorVersion == 16 && minorVersion > 8) && !process.env.DISABLE_TURBO_CALLS;
14+
let fastApiCalls =
15+
(majorVersion == 17 ||
16+
majorVersion == 18 ||
17+
(majorVersion == 16 && minorVersion > 8)) &&
18+
!process.env.DISABLE_TURBO_CALLS;
1519
if (fastApiCalls) {
1620
require('v8').setFlagsFromString('--turbo-fast-api-calls');
1721
}
@@ -24,9 +28,9 @@ if (!versions.deno && !process.isBun) {
2428
nativeAddon.enableDirectV8(v8Funcs, false);
2529
nativeAddon.clearKeptObjects = v8Funcs.clearKeptObjects;
2630
}
27-
nativeAddon.enableThreadSafeCalls();
2831
}
32+
nativeAddon.enableThreadSafeCalls();
2933
setNativeFunctions(nativeAddon);
3034

31-
export * from './index.js'
32-
export { default } from './index.js'
35+
export * from './index.js';
36+
export { default } from './index.js';

test/bun.js

Lines changed: 102 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { open, IF_EXISTS, asBinary } from '../index.js';
2-
import { assert, should as shouldLoad} from "chai";
1+
import { open, IF_EXISTS, asBinary } from '../node-index.js';
2+
import { assert, should as shouldLoad } from 'chai';
33
//const { assert, should: shouldLoad } = chai;
44
const should = shouldLoad();
55
let db = open('test/testdata', {
@@ -20,7 +20,12 @@ let tests = [];
2020
let test = (name, test) => {
2121
tests.push({ name, test });
2222
};
23-
test('query of keys', async function() {
23+
test('async txn', async function () {
24+
await db.transaction(() => {
25+
db.put('key1', 'Hello world!');
26+
});
27+
});
28+
test('query of keys', async function () {
2429
let keys = [
2530
Symbol.for('test'),
2631
false,
@@ -29,86 +34,97 @@ test('query of keys', async function() {
2934
-1.1,
3035
3.3,
3136
5,
32-
[5,4],
33-
[5,55],
37+
[5, 4],
38+
[5, 55],
3439
[5, 'words after number'],
3540
[6, 'abc'],
36-
[ 'Test', null, 1 ],
37-
[ 'Test', Symbol.for('test'), 2 ],
38-
[ 'Test', 'not null', 3 ],
41+
['Test', null, 1],
42+
['Test', Symbol.for('test'), 2],
43+
['Test', 'not null', 3],
3944
'hello',
4045
['hello', 3],
4146
['hello', 'world'],
42-
[ 'uid', 'I-7l9ySkD-wAOULIjOEnb', 'Rwsu6gqOw8cqdCZG5_YNF' ],
43-
'z'
44-
]
47+
['uid', 'I-7l9ySkD-wAOULIjOEnb', 'Rwsu6gqOw8cqdCZG5_YNF'],
48+
'z',
49+
];
4550
for (let key of keys) {
4651
await db.put(key, 3);
4752
}
48-
let returnedKeys = []
53+
let returnedKeys = [];
4954
for (let { key, value } of db.getRange({
50-
start: Symbol.for('A')
55+
start: Symbol.for('A'),
5156
})) {
52-
returnedKeys.push(key)
53-
should.equal(db.get(key), value)
57+
returnedKeys.push(key);
58+
should.equal(db.get(key), value);
5459
}
55-
keys.should.deep.equal(returnedKeys)
60+
keys.should.deep.equal(returnedKeys);
5661

57-
returnedKeys = []
62+
returnedKeys = [];
5863
for (let { key, value } of db.getRange({
5964
reverse: true,
6065
})) {
61-
returnedKeys.unshift(key)
62-
should.equal(db.get(key), value)
66+
returnedKeys.unshift(key);
67+
should.equal(db.get(key), value);
6368
}
64-
keys.should.deep.equal(returnedKeys)
69+
keys.should.deep.equal(returnedKeys);
6570
});
66-
test('reverse query range', async function() {
71+
test('reverse query range', async function () {
6772
const keys = [
68-
[ 'Test', 100, 1 ],
69-
[ 'Test', 10010, 2 ],
70-
[ 'Test', 10010, 3 ]
71-
]
72-
for (let key of keys)
73-
await db.put(key, 3);
73+
['Test', 100, 1],
74+
['Test', 10010, 2],
75+
['Test', 10010, 3],
76+
];
77+
for (let key of keys) await db.put(key, 3);
7478
for (let { key, value } of db.getRange({
7579
start: ['Test', null],
7680
end: ['Test', null],
77-
reverse: true
81+
reverse: true,
7882
})) {
79-
throw new Error('Should not return any results')
83+
throw new Error('Should not return any results');
8084
}
81-
})
82-
test('more reverse query range', async function() {
83-
db.putSync('0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdtsud6g8YGhPwUK04fRVKhuTywhnx8', 1, 1, null);
84-
db.putSync('0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdu0mnkm8lS38yIZa4Xte3Q3JUoD84V', 1, 1, null);
85-
const options =
86-
{
85+
});
86+
test('more reverse query range', async function () {
87+
db.putSync(
88+
'0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdtsud6g8YGhPwUK04fRVKhuTywhnx8',
89+
1,
90+
1,
91+
null,
92+
);
93+
db.putSync(
94+
'0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdu0mnkm8lS38yIZa4Xte3Q3JUoD84V',
95+
1,
96+
1,
97+
null,
98+
);
99+
const options = {
87100
start: '0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0SdvKaMkMNPoydWV6HxZbFtKeQm5sqz3',
88101
end: '0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/00000000dKZzSn03pte5dWbaYfrZl4hG',
89-
reverse: true
102+
reverse: true,
90103
};
91-
let returnedKeys = Array.from(db.getKeys(options))
92-
returnedKeys.should.deep.equal(['0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdu0mnkm8lS38yIZa4Xte3Q3JUoD84V', '0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdtsud6g8YGhPwUK04fRVKhuTywhnx8'])
104+
let returnedKeys = Array.from(db.getKeys(options));
105+
returnedKeys.should.deep.equal([
106+
'0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdu0mnkm8lS38yIZa4Xte3Q3JUoD84V',
107+
'0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdtsud6g8YGhPwUK04fRVKhuTywhnx8',
108+
]);
93109
});
94-
test('clear between puts', async function() {
95-
db.put('key0', 'zero')
96-
db.clearAsync()
97-
await db.put('key1', 'one')
98-
assert.equal(db.get('key0'), undefined)
99-
assert.equal(db.get('hello'), undefined)
100-
assert.equal(db.get('key1'), 'one')
110+
test('clear between puts', async function () {
111+
db.put('key0', 'zero');
112+
db.clearAsync();
113+
await db.put('key1', 'one');
114+
assert.equal(db.get('key0'), undefined);
115+
assert.equal(db.get('hello'), undefined);
116+
assert.equal(db.get('key1'), 'one');
101117
});
102118

103-
test('string', async function() {
119+
test('string', async function () {
104120
await db.put('key1', 'Hello world!');
105121
let data = db.get('key1');
106122
data.should.equal('Hello world!');
107-
await db.remove('key1')
123+
await db.remove('key1');
108124
let data2 = db.get('key1');
109125
assert.equal(data2, undefined);
110126
});
111-
test('string with version', async function() {
127+
test('string with version', async function () {
112128
await db.put('key1', 'Hello world!', 53252);
113129
let entry = db.getEntry('key1');
114130
entry.value.should.equal('Hello world!');
@@ -121,54 +137,56 @@ test('string with version', async function() {
121137
entry = db.getEntry('key1');
122138
assert.equal(entry, undefined);
123139
});
124-
test('string with version branching', async function() {
140+
test('string with version branching', async function () {
125141
await db.put('key1', 'Hello world!', 53252);
126142
let entry = db.getEntry('key1');
127143
entry.value.should.equal('Hello world!');
128144
entry.version.should.equal(53252);
129-
(await db.ifVersion('key1', 777, () => {
130-
db.put('newKey', 'test', 6);
131-
db2.put('keyB', 'test', 6);
132-
})).should.equal(false);
145+
(
146+
await db.ifVersion('key1', 777, () => {
147+
db.put('newKey', 'test', 6);
148+
db2.put('keyB', 'test', 6);
149+
})
150+
).should.equal(false);
133151
assert.equal(db.get('newKey'), undefined);
134152
assert.equal(db2.get('keyB'), undefined);
135-
let result = (await db.ifVersion('key1', 53252, () => {
136-
db.put('newKey', 'test', 6);
137-
db2.put('keyB', 'test', 6);
138-
}))
139-
assert.equal(db.get('newKey'), 'test')
140-
assert.equal(db2.get('keyB'), 'test')
153+
let result = await db.ifVersion('key1', 53252, () => {
154+
db.put('newKey', 'test', 6);
155+
db2.put('keyB', 'test', 6);
156+
});
157+
assert.equal(db.get('newKey'), 'test');
158+
assert.equal(db2.get('keyB'), 'test');
141159
assert.equal(result, true);
142160
result = await db.ifNoExists('key1', () => {
143-
db.put('newKey', 'changed', 7);
144-
})
161+
db.put('newKey', 'changed', 7);
162+
});
145163
assert.equal(db.get('newKey'), 'test');
146164
assert.equal(result, false);
147165
result = await db.ifNoExists('key-no-exist', () => {
148-
db.put('newKey', 'changed', 7);
149-
})
150-
assert.equal(db.get('newKey'), 'changed')
166+
db.put('newKey', 'changed', 7);
167+
});
168+
assert.equal(db.get('newKey'), 'changed');
151169
assert.equal(result, true);
152170

153171
result = await db2.ifVersion('key-no-exist', IF_EXISTS, () => {
154-
db.put('newKey', 'changed again', 7);
155-
})
156-
assert.equal(db.get('newKey'), 'changed')
172+
db.put('newKey', 'changed again', 7);
173+
});
174+
assert.equal(db.get('newKey'), 'changed');
157175
assert.equal(result, false);
158176

159177
result = await db2.ifVersion('keyB', IF_EXISTS, () => {
160-
db.put('newKey', 'changed again', 7);
161-
})
162-
assert.equal(db.get('newKey'), 'changed again')
178+
db.put('newKey', 'changed again', 7);
179+
});
180+
assert.equal(db.get('newKey'), 'changed again');
163181
assert.equal(result, true);
164182

165183
result = await db2.remove('key-no-exists');
166184
assert.equal(result, true);
167185
result = await db2.remove('key-no-exists', IF_EXISTS);
168186
assert.equal(result, false);
169187
});
170-
test('string with compression and versions', async function() {
171-
let str = expand('Hello world!')
188+
test('string with compression and versions', async function () {
189+
let str = expand('Hello world!');
172190
await db.put('key1', str, 53252);
173191
let entry = db.getEntry('key1');
174192
entry.value.should.equal(str);
@@ -180,8 +198,8 @@ test('string with compression and versions', async function() {
180198
data = db.get('key1');
181199
assert.equal(data, undefined);
182200
});
183-
test('repeated compressions', async function() {
184-
let str = expand('Hello world!')
201+
test('repeated compressions', async function () {
202+
let str = expand('Hello world!');
185203
db.put('key1', str, 53252);
186204
db.put('key1', str, 53253);
187205
db.put('key1', str, 53254);
@@ -192,7 +210,7 @@ test('repeated compressions', async function() {
192210
(await db.remove('key1')).should.equal(true);
193211
});
194212

195-
test('forced compression due to starting with 255', async function() {
213+
test('forced compression due to starting with 255', async function () {
196214
await db.put('key1', asBinary(new Uint8Array([255])));
197215
let entry = db.getBinary('key1');
198216
entry.length.should.equal(1);
@@ -208,17 +226,16 @@ test('forced compression due to starting with 255', async function() {
208226
});*/
209227

210228
function expand(str) {
211-
str = '(' + str + ')';
212-
str = str + str;
213-
str = str + str;
214-
str = str + str;
215-
str = str + str;
216-
str = str + str;
217-
return str;
229+
str = '(' + str + ')';
230+
str = str + str;
231+
str = str + str;
232+
str = str + str;
233+
str = str + str;
234+
str = str + str;
235+
return str;
218236
}
219237

220-
221-
let hasErrors
238+
let hasErrors;
222239
for (let { name, test } of tests) {
223240
try {
224241
await test();
@@ -228,5 +245,4 @@ for (let { name, test } of tests) {
228245
console.error('Failed:', name, error);
229246
}
230247
}
231-
if (hasErrors)
232-
throw new Error('Unit tests failed');
248+
if (hasErrors) throw new Error('Unit tests failed');

0 commit comments

Comments
 (0)