mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
fix(db): pass the chunk size and the abort options through the NUL-free connection
The wrapper took the chunk size of a stream as optional, which lint:strict refuses, and dropped the abort options Kysely hands executeQuery and streamQuery, so a cancelled query kept running.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { CompiledQuery, DatabaseConnection, Dialect, Driver } from 'kysely';
|
||||
import type { AbortableOperationOptions, CompiledQuery, DatabaseConnection, Dialect, Driver } from 'kysely';
|
||||
|
||||
/**
|
||||
* TREK stores no U+0000 (NUL) in text, on either engine.
|
||||
@@ -87,9 +87,10 @@ function nulFreeDriver(driver: Driver): Driver {
|
||||
let proxy = wrapped.get(connection);
|
||||
if (!proxy) {
|
||||
proxy = overriding(connection, {
|
||||
executeQuery: <R>(query: CompiledQuery) => connection.executeQuery<R>(nulFreeQuery(query)),
|
||||
streamQuery: <R>(query: CompiledQuery, chunkSize?: number) =>
|
||||
connection.streamQuery<R>(nulFreeQuery(query), chunkSize),
|
||||
executeQuery: <R>(query: CompiledQuery, options?: AbortableOperationOptions) =>
|
||||
connection.executeQuery<R>(nulFreeQuery(query), options),
|
||||
streamQuery: <R>(query: CompiledQuery, chunkSize: number, options?: AbortableOperationOptions) =>
|
||||
connection.streamQuery<R>(nulFreeQuery(query), chunkSize, options),
|
||||
});
|
||||
wrapped.set(connection, proxy);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import { TrekPostgreSqlPlatform } from '../../../src/db/engine/postgres/postgres
|
||||
import { nulFreeJson, nulFreeParameter, stripNul, withNulFreeParameters } from '../../../src/db/nul-free';
|
||||
|
||||
import {
|
||||
type AbortableOperationOptions,
|
||||
CompiledQuery,
|
||||
type DatabaseConnection,
|
||||
type Dialect,
|
||||
@@ -25,17 +26,25 @@ const RELEASE = Symbol('release');
|
||||
/** A connection that records what it was sent and keeps a private release, like Kysely's Postgres one. */
|
||||
class RecordingConnection implements DatabaseConnection {
|
||||
readonly sent: CompiledQuery[] = [];
|
||||
/** What came with each query besides itself: the chunk size of a stream, the abort options. */
|
||||
readonly extras: unknown[][] = [];
|
||||
released = false;
|
||||
#open = true;
|
||||
|
||||
async executeQuery<R>(query: CompiledQuery): Promise<QueryResult<R>> {
|
||||
async executeQuery<R>(query: CompiledQuery, options?: AbortableOperationOptions): Promise<QueryResult<R>> {
|
||||
if (!this.#open) throw new Error('closed');
|
||||
this.sent.push(query);
|
||||
this.extras.push([options]);
|
||||
return { rows: [] };
|
||||
}
|
||||
|
||||
async *streamQuery<R>(query: CompiledQuery): AsyncIterableIterator<QueryResult<R>> {
|
||||
async *streamQuery<R>(
|
||||
query: CompiledQuery,
|
||||
chunkSize: number,
|
||||
options?: AbortableOperationOptions,
|
||||
): AsyncIterableIterator<QueryResult<R>> {
|
||||
this.sent.push(query);
|
||||
this.extras.push([chunkSize, options]);
|
||||
yield { rows: [] };
|
||||
}
|
||||
|
||||
@@ -120,6 +129,20 @@ describe('NUL-free values', () => {
|
||||
expect(connection.released).toBe(true);
|
||||
});
|
||||
|
||||
it('NULFREE-005: the chunk size of a stream and the abort options reach the connection as they were given', async () => {
|
||||
const connection = new RecordingConnection();
|
||||
const acquired = await withNulFreeParameters(recordingDialect(connection)).createDriver().acquireConnection();
|
||||
const options: AbortableOperationOptions = { signal: new AbortController().signal };
|
||||
|
||||
await acquired.executeQuery(CompiledQuery.raw('select ?', ['a\u0000']), options);
|
||||
for await (const chunk of acquired.streamQuery(CompiledQuery.raw('select 1'), 25, options)) {
|
||||
expect(chunk.rows).toEqual([]);
|
||||
}
|
||||
expect(connection.extras).toEqual([[options], [25, options]]);
|
||||
expect(connection.extras[0][0]).toBe(options);
|
||||
expect(connection.extras[1][1]).toBe(options);
|
||||
});
|
||||
|
||||
it('NULFREE-004: the Postgres platform inlines strings and serializes JSON without NUL', () => {
|
||||
const platform = new TrekPostgreSqlPlatform();
|
||||
expect(platform.escape("o\u0000'k")).toBe("'o''k'");
|
||||
|
||||
Reference in New Issue
Block a user