Skip to content

Commit cada892

Browse files
committed
src: avoid custom functions in async mode
1 parent 5bec78e commit cada892

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/node_sqlite.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,10 @@ void Database::CustomFunction(const FunctionCallbackInfo<Value>& args) {
13181318
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
13191319
Environment* env = Environment::GetCurrent(args);
13201320
THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open");
1321+
THROW_AND_RETURN_ON_BAD_STATE(
1322+
env,
1323+
db->is_async(),
1324+
"Custom functions are not supported in async mode");
13211325

13221326
if (!args[0]->IsString()) {
13231327
THROW_ERR_INVALID_ARG_TYPE(env->isolate(),
@@ -1491,6 +1495,10 @@ void Database::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
14911495
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
14921496
Environment* env = Environment::GetCurrent(args);
14931497
THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open");
1498+
THROW_AND_RETURN_ON_BAD_STATE(
1499+
env,
1500+
db->is_async(),
1501+
"Aggregate functions are not supported in async mode");
14941502
Utf8Value name(env->isolate(), args[0].As<String>());
14951503
Local<Object> options = args[1].As<Object>();
14961504
Local<Value> start_v;
@@ -2055,6 +2063,10 @@ void Database::SetAuthorizer(const FunctionCallbackInfo<Value>& args) {
20552063
Database* db;
20562064
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
20572065
Environment* env = Environment::GetCurrent(args);
2066+
THROW_AND_RETURN_ON_BAD_STATE(
2067+
env,
2068+
db->is_async(),
2069+
"Authorizer callbacks are not supported in async mode");
20582070
Isolate* isolate = env->isolate();
20592071

20602072
if (args[0]->IsNull()) {

test/parallel/test-sqlite-database-async.mjs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,41 @@ suite('Database.prototype.location()', () => {
524524
t.assert.strictEqual(db.location('other'), otherPath);
525525
});
526526
});
527+
528+
suite('Async mode restrictions', () => {
529+
test('throws when defining a custom function', (t) => {
530+
const db = new Database(':memory:');
531+
t.after(() => { db.close(); });
532+
533+
t.assert.throws(() => {
534+
db.function('test', () => 1);
535+
}, {
536+
code: 'ERR_INVALID_STATE',
537+
message: /Custom functions are not supported in async mode/,
538+
});
539+
});
540+
541+
test('throws when defining an aggregate function', (t) => {
542+
const db = new Database(':memory:');
543+
t.after(() => { db.close(); });
544+
545+
t.assert.throws(() => {
546+
db.aggregate('test', { start: 0, step: (acc) => acc });
547+
}, {
548+
code: 'ERR_INVALID_STATE',
549+
message: /Aggregate functions are not supported in async mode/,
550+
});
551+
});
552+
553+
test('throws when setting an authorizer callback', (t) => {
554+
const db = new Database(':memory:');
555+
t.after(() => { db.close(); });
556+
557+
t.assert.throws(() => {
558+
db.setAuthorizer(() => 0);
559+
}, {
560+
code: 'ERR_INVALID_STATE',
561+
message: /Authorizer callbacks are not supported in async mode/,
562+
});
563+
});
564+
});

0 commit comments

Comments
 (0)