Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1044,10 +1044,20 @@ void DatabaseSync::CreateTagStore(const FunctionCallbackInfo<Value>& args) {
return;
}
int capacity = 1000;
if (args.Length() > 0 && args[0]->IsNumber()) {
capacity = args[0].As<Number>()->Value();
if (args.Length() > 0 && !args[0]->IsUndefined()) {
if (!args[0]->IsInt32()) {
THROW_ERR_INVALID_ARG_TYPE(
env->isolate(), "The \"maxSize\" argument must be an integer.");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user runs database.createTagStore(Number.MAX_SAFE_INTEGER), that error message is going to be confusing

return;
}
capacity = args[0].As<Int32>()->Value();
if (capacity < 0) {
THROW_ERR_OUT_OF_RANGE(
env->isolate(),
"The \"maxSize\" argument must be a non-negative integer.");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we accept 0, technically that's a negative number

Suggested change
"The \"maxSize\" argument must be a non-negative integer.");
"The \"maxSize\" argument must be a positive integer.");

return;
}
}

BaseObjectPtr<SQLTagStore> session =
SQLTagStore::Create(env, BaseObjectWeakPtr<DatabaseSync>(db), capacity);
if (!session) {
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-sqlite-template-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ test('TagStore capacity, size, and clear', () => {
assert.strictEqual(sql.capacity, 10);
});

test('createTagStore throws on invalid maxSize', () => {
const db = new DatabaseSync(':memory:');

assert.throws(() => db.createTagStore(-1), {
code: 'ERR_OUT_OF_RANGE',
message: /maxSize/,
});

assert.throws(() => db.createTagStore(NaN), {
code: 'ERR_INVALID_ARG_TYPE',
message: /maxSize/,
});

assert.throws(() => db.createTagStore(1.5), {
code: 'ERR_INVALID_ARG_TYPE',
Comment on lines +124 to +129
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should likely be ERR_OUT_OF_RANGE, like e.g. node -e 'child_process.spawn("/dev/null", { uid: 1.3 })' does

message: /maxSize/,
});

assert.throws(() => db.createTagStore('abc'), {
code: 'ERR_INVALID_ARG_TYPE',
message: /maxSize/,
});
});

test('sql.db returns the associated DatabaseSync instance', () => {
assert.strictEqual(sql.db, db);
});
Expand Down
Loading