-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.ts
More file actions
323 lines (270 loc) · 12.5 KB
/
protocol.ts
File metadata and controls
323 lines (270 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
// protocol.ts - low level protocol handling for SQLiteCloud transport
//
import { SQLiteCloudError, type SQLCloudRowsetMetadata, type SQLiteCloudDataTypes } from './types'
import { SQLiteCloudRowset } from './rowset'
const lz4 = require('lz4js')
// The server communicates with clients via commands defined in
// SQLiteCloud Server Protocol (SCSP), see more at:
// https://github.com/sqlitecloud/sdk/blob/master/PROTOCOL.md
export const CMD_STRING = '+'
export const CMD_ZEROSTRING = '!'
export const CMD_ERROR = '-'
export const CMD_INT = ':'
export const CMD_FLOAT = ','
export const CMD_ROWSET = '*'
export const CMD_ROWSET_CHUNK = '/'
export const CMD_JSON = '#'
export const CMD_NULL = '_'
export const CMD_BLOB = '$'
export const CMD_COMPRESSED = '%'
export const CMD_COMMAND = '^'
export const CMD_ARRAY = '='
// const CMD_RAWJSON = '{'
export const CMD_PUBSUB = '|'
// const CMD_RECONNECT = '@'
// To mark the end of the Rowset, the special string /LEN 0 0 0 is sent (LEN is always 6 in this case)
// https://github.com/sqlitecloud/sdk/blob/master/PROTOCOL.md#scsp-rowset-chunk
export const ROWSET_CHUNKS_END = '/6 0 0 0 '
//
// utility functions
//
/** Analyze first character to check if corresponding data type has LEN */
export function hasCommandLength(firstCharacter: string): boolean {
return firstCharacter == CMD_INT || firstCharacter == CMD_FLOAT || firstCharacter == CMD_NULL ? false : true
}
/** Analyze a command with explict LEN and extract it */
export function parseCommandLength(data: Buffer): number {
return parseInt(data.subarray(1, data.indexOf(' ')).toString('utf8'))
}
/** Receive a compressed buffer, decompress with lz4, return buffer and datatype */
export function decompressBuffer(buffer: Buffer): { buffer: Buffer; dataType: string } {
const spaceIndex = buffer.indexOf(' ')
buffer = buffer.subarray(spaceIndex + 1)
// extract compressed size
const compressedSize = parseInt(buffer.subarray(0, buffer.indexOf(' ') + 1).toString('utf8'))
buffer = buffer.subarray(buffer.indexOf(' ') + 1)
// extract decompressed size
const decompressedSize = parseInt(buffer.subarray(0, buffer.indexOf(' ') + 1).toString('utf8'))
buffer = buffer.subarray(buffer.indexOf(' ') + 1)
// extract compressed dataType
const dataType = buffer.subarray(0, 1).toString('utf8')
const decompressedBuffer = Buffer.alloc(decompressedSize)
const compressedBuffer = buffer.subarray(buffer.length - compressedSize)
// lz4js library is javascript and doesn't have types so we silence the type check
// eslint-disable-next-line
const decompressionResult: number = lz4.decompressBlock(compressedBuffer, decompressedBuffer, 0, compressedSize, 0)
buffer = Buffer.concat([buffer.subarray(0, buffer.length - compressedSize), decompressedBuffer])
if (decompressionResult <= 0 || decompressionResult !== decompressedSize) {
throw new Error(`lz4 decompression error at offset ${decompressionResult}`)
}
return { buffer, dataType }
}
/** Parse error message or extended error message */
export function parseError(buffer: Buffer, spaceIndex: number): never {
const errorBuffer = buffer.subarray(spaceIndex + 1)
const errorString = errorBuffer.toString('utf8')
const parts = errorString.split(' ')
let errorCodeStr = parts.shift() || '0' // Default errorCode is '0' if not present
let extErrCodeStr = '0' // Default extended error code
let offsetCodeStr = '-1' // Default offset code
// Split the errorCode by ':' to check for extended error codes
const errorCodeParts = errorCodeStr.split(':')
errorCodeStr = errorCodeParts[0]
if (errorCodeParts.length > 1) {
extErrCodeStr = errorCodeParts[1]
if (errorCodeParts.length > 2) {
offsetCodeStr = errorCodeParts[2]
}
}
// Rest of the error string is the error message
const errorMessage = parts.join(' ')
// Parse error codes to integers safely, defaulting to 0 if NaN
const errorCode = parseInt(errorCodeStr)
const extErrCode = parseInt(extErrCodeStr)
const offsetCode = parseInt(offsetCodeStr)
// create an Error object and add the custom properties
throw new SQLiteCloudError(errorMessage, {
errorCode: errorCode.toString(),
externalErrorCode: extErrCode.toString(),
offsetCode
})
}
/** Parse an array of items (each of which will be parsed by type separately) */
export function parseArray(buffer: Buffer, spaceIndex: number): SQLiteCloudDataTypes[] {
const parsedData = []
const array = buffer.subarray(spaceIndex + 1, buffer.length)
const numberOfItems = parseInt(array.subarray(0, spaceIndex - 2).toString('utf8'))
let arrayItems = array.subarray(array.indexOf(' ') + 1, array.length)
for (let i = 0; i < numberOfItems; i++) {
const { data, fwdBuffer: buffer } = popData(arrayItems)
parsedData.push(data)
arrayItems = buffer
}
return parsedData as SQLiteCloudDataTypes[]
}
/** Parse header in a rowset or chunk of a chunked rowset */
export function parseRowsetHeader(buffer: Buffer): { index: number; metadata: SQLCloudRowsetMetadata; fwdBuffer: Buffer } {
const index = parseInt(buffer.subarray(0, buffer.indexOf(':') + 1).toString())
buffer = buffer.subarray(buffer.indexOf(':') + 1)
// extract rowset header
const { data, fwdBuffer } = popIntegers(buffer, 3)
return {
index,
metadata: {
version: data[0],
numberOfRows: data[1],
numberOfColumns: data[2],
columns: []
},
fwdBuffer
}
}
/** Extract column names and, optionally, more metadata out of a rowset's header */
function parseRowsetColumnsMetadata(buffer: Buffer, metadata: SQLCloudRowsetMetadata): Buffer {
function popForward() {
const { data, fwdBuffer: fwdBuffer } = popData(buffer) // buffer in parent scope
buffer = fwdBuffer
return data
}
for (let i = 0; i < metadata.numberOfColumns; i++) {
metadata.columns.push({ name: popForward() as string })
}
// extract additional metadata if rowset has version 2
if (metadata.version == 2) {
for (let i = 0; i < metadata.numberOfColumns; i++) metadata.columns[i].type = popForward() as string
for (let i = 0; i < metadata.numberOfColumns; i++) metadata.columns[i].database = popForward() as string
for (let i = 0; i < metadata.numberOfColumns; i++) metadata.columns[i].table = popForward() as string
for (let i = 0; i < metadata.numberOfColumns; i++) metadata.columns[i].column = popForward() as string // original column name
for (let i = 0; i < metadata.numberOfColumns; i++) metadata.columns[i].notNull = popForward() as number
for (let i = 0; i < metadata.numberOfColumns; i++) metadata.columns[i].primaryKey = popForward() as number
for (let i = 0; i < metadata.numberOfColumns; i++) metadata.columns[i].autoIncrement = popForward() as number
}
return buffer
}
/** Parse a regular rowset (no chunks) */
function parseRowset(buffer: Buffer, spaceIndex: number): SQLiteCloudRowset {
buffer = buffer.subarray(spaceIndex + 1, buffer.length)
const { metadata, fwdBuffer } = parseRowsetHeader(buffer)
buffer = parseRowsetColumnsMetadata(fwdBuffer, metadata)
// decode each rowset item
const data = []
for (let j = 0; j < metadata.numberOfRows * metadata.numberOfColumns; j++) {
const { data: rowData, fwdBuffer } = popData(buffer)
data.push(rowData)
buffer = fwdBuffer
}
console.assert(data && data.length === metadata.numberOfRows * metadata.numberOfColumns, 'SQLiteCloudConnection.parseRowset - invalid rowset data')
return new SQLiteCloudRowset(metadata, data)
}
export function bufferStartsWith(buffer: Buffer, prefix: string): boolean {
return buffer.length >= prefix.length && buffer.subarray(0, prefix.length).toString('utf8') === prefix
}
export function bufferEndsWith(buffer: Buffer, suffix: string): boolean {
return buffer.length >= suffix.length && buffer.subarray(buffer.length - suffix.length, buffer.length).toString('utf8') === suffix
}
/**
* Parse a chunk of a chunked rowset command, eg:
* *LEN 0:VERS NROWS NCOLS DATA
* @see https://github.com/sqlitecloud/sdk/blob/master/PROTOCOL.md#scsp-rowset-chunk
*/
export function parseRowsetChunks(buffers: Buffer[]): SQLiteCloudRowset {
let buffer = Buffer.concat(buffers)
if (!bufferStartsWith(buffer, CMD_ROWSET_CHUNK) || !bufferEndsWith(buffer, ROWSET_CHUNKS_END)) {
throw new Error('SQLiteCloudConnection.parseRowsetChunks - invalid chunks buffer')
}
let metadata: SQLCloudRowsetMetadata = { version: 1, numberOfColumns: 0, numberOfRows: 0, columns: [] }
const data: any[] = []
// validate and skip data type
const dataType = buffer.subarray(0, 1).toString()
console.assert(dataType === CMD_ROWSET_CHUNK)
buffer = buffer.subarray(buffer.indexOf(' ') + 1)
while (buffer.length > 0 && !bufferStartsWith(buffer, ROWSET_CHUNKS_END)) {
// chunk header, eg: 0:VERS NROWS NCOLS
const { index: chunkIndex, metadata: chunkMetadata, fwdBuffer } = parseRowsetHeader(buffer)
buffer = fwdBuffer
// first chunk? extract columns metadata
if (chunkIndex === 1) {
metadata = chunkMetadata
buffer = parseRowsetColumnsMetadata(buffer, metadata)
} else {
metadata.numberOfRows += chunkMetadata.numberOfRows
}
// extract single rowset row
for (let k = 0; k < chunkMetadata.numberOfRows * metadata.numberOfColumns; k++) {
const { data: itemData, fwdBuffer } = popData(buffer)
data.push(itemData)
buffer = fwdBuffer
}
}
console.assert(data && data.length === metadata.numberOfRows * metadata.numberOfColumns, 'parseRowsetChunks - invalid rowset data')
const rowset = new SQLiteCloudRowset(metadata, data)
// console.debug(`parseRowsetChunks - ${rowset.numberOfRows} rows, ${rowset.numberOfColumns} columns`)
return rowset
}
/** Pop one or more space separated integers from beginning of buffer, move buffer forward */
function popIntegers(buffer: Buffer, numberOfIntegers = 1): { data: number[]; fwdBuffer: Buffer } {
const data: number[] = []
for (let i = 0; i < numberOfIntegers; i++) {
const spaceIndex = buffer.indexOf(' ')
data[i] = parseInt(buffer.subarray(0, spaceIndex).toString())
buffer = buffer.subarray(spaceIndex + 1)
}
return { data, fwdBuffer: buffer }
}
/** Parse command, extract its data, return the data and the buffer moved to the first byte after the command */
export function popData(buffer: Buffer): { data: SQLiteCloudDataTypes | SQLiteCloudRowset; fwdBuffer: Buffer } {
function popResults(data: any) {
const fwdBuffer = buffer.subarray(commandEnd)
return { data, fwdBuffer }
}
// first character is the data type
console.assert(buffer && buffer instanceof Buffer)
const dataType: string = buffer.subarray(0, 1).toString('utf8')
console.assert(dataType !== CMD_COMPRESSED, "Compressed data shouldn't be decompressed before parsing")
console.assert(dataType !== CMD_ROWSET_CHUNK, 'Chunked data should be parsed by parseRowsetChunks')
let spaceIndex = buffer.indexOf(' ')
if (spaceIndex === -1) {
spaceIndex = buffer.length - 1
}
let commandEnd = -1
if (dataType === CMD_INT || dataType === CMD_FLOAT || dataType === CMD_NULL) {
commandEnd = spaceIndex + 1
} else {
const commandLength = parseInt(buffer.subarray(1, spaceIndex).toString())
commandEnd = spaceIndex + 1 + commandLength
}
switch (dataType) {
case CMD_INT:
return popResults(parseInt(buffer.subarray(1, spaceIndex).toString()))
case CMD_FLOAT:
return popResults(parseFloat(buffer.subarray(1, spaceIndex).toString()))
case CMD_NULL:
return popResults(null)
case CMD_STRING:
return popResults(buffer.subarray(spaceIndex + 1, commandEnd).toString('utf8'))
case CMD_ZEROSTRING:
return popResults(buffer.subarray(spaceIndex + 1, commandEnd - 1).toString('utf8'))
case CMD_COMMAND:
return popResults(buffer.subarray(spaceIndex + 1, commandEnd).toString('utf8'))
case CMD_PUBSUB:
return popResults(buffer.subarray(spaceIndex + 1, commandEnd).toString('utf8'))
case CMD_JSON:
return popResults(JSON.parse(buffer.subarray(spaceIndex + 1, commandEnd).toString('utf8')))
case CMD_BLOB:
return popResults(buffer.subarray(spaceIndex + 1, commandEnd))
case CMD_ARRAY:
return popResults(parseArray(buffer, spaceIndex))
case CMD_ROWSET:
return popResults(parseRowset(buffer, spaceIndex))
case CMD_ERROR:
parseError(buffer, spaceIndex) // throws custom error
break
}
throw new TypeError(`Data type: ${dataType} is not defined in SCSP`)
}
/** Format a command to be sent via SCSP protocol */
export function formatCommand(command: string): string {
const commandLength = Buffer.byteLength(command, 'utf-8')
return `+${commandLength} ${command}`
}