Skip to content

Commit 2f873ff

Browse files
author
Daniele Briggi
committed
fix(blob): serialize commands as binary
Blobs have to be serialized as binary and thus all the parameters have to be converted to binary as well.
1 parent bb15e13 commit 2f873ff

16 files changed

+67
-44
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ dist
118118
# Compiled code
119119
coverage/
120120
docs/
121-
lib/
121+
#lib/
122122

123123
# Mac files
124124
.DS_Store

lib/drivers/connection-tls.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ class SQLiteCloudTlsConnection extends connection_1.SQLiteCloudConnection {
156156
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.destroy();
157157
this.socket = undefined;
158158
}, timeoutMs);
159-
(_d = this.socket) === null || _d === void 0 ? void 0 : _d.write(formattedCommands, 'utf-8', () => {
159+
(_d = this.socket) === null || _d === void 0 ? void 0 : _d.write(formattedCommands, () => {
160160
clearTimeout(timeout); // Clear the timeout on successful write
161161
});
162162
}
163163
else {
164-
(_e = this.socket) === null || _e === void 0 ? void 0 : _e.write(formattedCommands, 'utf-8');
164+
(_e = this.socket) === null || _e === void 0 ? void 0 : _e.write(formattedCommands);
165165
}
166166
return this;
167167
}

lib/drivers/connection.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* connection.ts - base abstract class for sqlitecloud server connections
33
*/
4-
import { SQLiteCloudConfig, ErrorCallback, ResultsCallback, SQLiteCloudCommand } from './types';
4+
import { SQLiteCloudConfig, ErrorCallback, ResultsCallback, SQLiteCloudCommand, SQLiteCloudDataTypes } from './types';
55
import { OperationsQueue } from './queue';
66
/**
77
* Base class for SQLiteCloudConnection handles basics and defines methods.
@@ -39,7 +39,7 @@ export declare abstract class SQLiteCloudConnection {
3939
* @returns An array of rows in case of selections or an object with
4040
* metadata in case of insert, update, delete.
4141
*/
42-
sql(sql: TemplateStringsArray | string | SQLiteCloudCommand, ...values: any[]): Promise<any>;
42+
sql(sql: TemplateStringsArray | string | SQLiteCloudCommand, ...values: SQLiteCloudDataTypes[]): Promise<any>;
4343
/** Disconnect from server, release transport. */
4444
abstract close(): this;
4545
}

lib/drivers/database.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import EventEmitter from 'eventemitter3';
22
import { PubSub } from './pubsub';
33
import { Statement } from './statement';
4-
import { ErrorCallback as ConnectionCallback, ResultsCallback, RowCallback, RowCountCallback, RowsCallback, SQLiteCloudCommand, SQLiteCloudConfig } from './types';
4+
import { ErrorCallback as ConnectionCallback, ResultsCallback, RowCallback, RowCountCallback, RowsCallback, SQLiteCloudCommand, SQLiteCloudConfig, SQLiteCloudDataTypes } from './types';
55
/**
66
* Creating a Database object automatically opens a connection to the SQLite database.
77
* When the connection is established the Database object emits an open event and calls
@@ -153,7 +153,7 @@ export declare class Database extends EventEmitter {
153153
* @returns An array of rows in case of selections or an object with
154154
* metadata in case of insert, update, delete.
155155
*/
156-
sql(sql: TemplateStringsArray | string | SQLiteCloudCommand, ...values: any[]): Promise<any>;
156+
sql(sql: TemplateStringsArray | string | SQLiteCloudCommand, ...values: SQLiteCloudDataTypes[]): Promise<any>;
157157
/**
158158
* Returns true if the database connection is open.
159159
*/

lib/drivers/protocol.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ export declare function popData(buffer: Buffer): {
5050
fwdBuffer: Buffer;
5151
};
5252
/** Format a command to be sent via SCSP protocol */
53-
export declare function formatCommand(command: SQLiteCloudCommand): string;
53+
export declare function formatCommand(command: SQLiteCloudCommand): Buffer;

lib/drivers/protocol.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,15 @@ function formatCommand(command) {
314314
}
315315
function serializeCommand(data, zeroString = false) {
316316
const n = data.length;
317-
let serializedData = `${n} `;
317+
let serializedData = buffer_1.Buffer.from(`${n} `);
318318
for (let i = 0; i < n; i++) {
319319
// the first string is the sql and it must be zero-terminated
320320
const zs = i == 0 || zeroString;
321-
serializedData += serializeData(data[i], zs);
321+
serializedData = buffer_1.Buffer.concat([serializedData, serializeData(data[i], zs)]);
322322
}
323-
const bytesTotal = buffer_1.Buffer.byteLength(serializedData, 'utf-8');
324-
const header = `${exports.CMD_ARRAY}${bytesTotal} `;
325-
return header + serializedData;
323+
const bytesTotal = serializedData.byteLength;
324+
const header = buffer_1.Buffer.from(`${exports.CMD_ARRAY}${bytesTotal} `);
325+
return buffer_1.Buffer.concat([header, serializedData]);
326326
}
327327
function serializeData(data, zeroString = false) {
328328
if (typeof data === 'string') {
@@ -332,22 +332,22 @@ function serializeData(data, zeroString = false) {
332332
data += '\0';
333333
}
334334
const header = `${cmd}${buffer_1.Buffer.byteLength(data, 'utf-8')} `;
335-
return header + data;
335+
return buffer_1.Buffer.from(header + data);
336336
}
337337
if (typeof data === 'number') {
338338
if (Number.isInteger(data)) {
339-
return `${exports.CMD_INT}${data} `;
339+
return buffer_1.Buffer.from(`${exports.CMD_INT}${data} `);
340340
}
341341
else {
342-
return `${exports.CMD_FLOAT}${data} `;
342+
return buffer_1.Buffer.from(`${exports.CMD_FLOAT}${data} `);
343343
}
344344
}
345345
if (buffer_1.Buffer.isBuffer(data)) {
346-
const header = `${exports.CMD_BLOB}${data.length} `;
347-
return header + data.toString('utf-8');
346+
const header = `${exports.CMD_BLOB}${data.byteLength} `;
347+
return buffer_1.Buffer.concat([buffer_1.Buffer.from(header), data]);
348348
}
349349
if (data === null || data === undefined) {
350-
return `${exports.CMD_NULL} `;
350+
return buffer_1.Buffer.from(`${exports.CMD_NULL} `);
351351
}
352352
if (Array.isArray(data)) {
353353
return serializeCommand(data, zeroString);

lib/sqlitecloud.drivers.dev.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/sqlitecloud.drivers.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.491",
3+
"version": "1.0.492",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/connection-tls.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ export class SQLiteCloudTlsConnection extends SQLiteCloudConnection {
141141
this.socket = undefined
142142
}, timeoutMs)
143143

144-
this.socket?.write(formattedCommands, 'utf-8', () => {
144+
this.socket?.write(formattedCommands, () => {
145145
clearTimeout(timeout) // Clear the timeout on successful write
146146
})
147147
} else {
148-
this.socket?.write(formattedCommands, 'utf-8')
148+
this.socket?.write(formattedCommands)
149149
}
150150

151151
return this

0 commit comments

Comments
 (0)