-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
336 lines (302 loc) · 9.28 KB
/
Copy pathindex.js
File metadata and controls
336 lines (302 loc) · 9.28 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
324
325
326
327
328
329
330
331
332
333
334
335
336
const net = require("net");
const crypto = require("crypto");
const EOM = Buffer.from([0x04]);
const DELIMITER = "\x1E";
const EOM_BYTE = 0x04;
class ONQLClient {
/**
* @param {object} [options]
* @param {number} [options.defaultTimeout=10] - Default timeout in seconds for requests.
*/
constructor(options = {}) {
this._socket = null;
this._buffer = Buffer.alloc(0);
this._pendingRequests = new Map();
this._defaultTimeout = (options.defaultTimeout != null ? options.defaultTimeout : 10);
this._connected = false;
}
/**
* Create and connect an ONQLClient.
* @param {object} [options]
* @param {string} [options.host="localhost"]
* @param {number} [options.port=5656]
* @param {number} [options.defaultTimeout=10] - Default timeout in seconds.
* @returns {Promise<ONQLClient>}
*/
static create(options = {}) {
const host = options.host || "localhost";
const port = options.port || 5656;
const defaultTimeout = options.defaultTimeout != null ? options.defaultTimeout : 10;
const client = new ONQLClient({ defaultTimeout });
return new Promise((resolve, reject) => {
const socket = new net.Socket();
client._socket = socket;
socket.connect(port, host, () => {
client._connected = true;
resolve(client);
});
socket.on("data", (data) => {
client._onData(data);
});
socket.on("error", (err) => {
if (!client._connected) {
reject(err);
return;
}
client._onClose();
});
socket.on("close", () => {
client._onClose();
});
});
}
/**
* Handle incoming data from the socket. Buffer it and extract complete
* messages delimited by EOM (0x04).
* @param {Buffer} data
*/
_onData(data) {
this._buffer = Buffer.concat([this._buffer, data]);
let eomIndex;
while ((eomIndex = this._buffer.indexOf(EOM_BYTE)) !== -1) {
const messageBytes = this._buffer.slice(0, eomIndex);
this._buffer = this._buffer.slice(eomIndex + 1);
const fullResponse = messageBytes.toString("utf-8");
const parts = fullResponse.split(DELIMITER);
if (parts.length !== 3) {
continue;
}
const [responseRid, sourceId, responsePayload] = parts;
const pending = this._pendingRequests.get(responseRid);
if (pending) {
pending.resolve({
request_id: responseRid,
source: sourceId,
payload: responsePayload,
});
}
}
}
/**
* Handle socket close / error: reject all pending requests.
*/
_onClose() {
this._connected = false;
for (const [, pending] of this._pendingRequests) {
pending.reject(new Error("Connection lost."));
}
this._pendingRequests.clear();
}
/**
* Close the connection.
* @returns {Promise<void>}
*/
close() {
return new Promise((resolve) => {
this._pendingRequests.clear();
if (this._socket) {
this._connected = false;
this._socket.destroy();
this._socket = null;
}
resolve();
});
}
/**
* Generate a random 8-character hex request ID.
* @returns {string}
*/
_generateRequestId() {
return crypto.randomBytes(4).toString("hex");
}
/**
* Send a request and wait for a response.
* @param {string} keyword
* @param {string} payload
* @param {number} [timeout] - Timeout in seconds. Defaults to defaultTimeout.
* @returns {Promise<{request_id: string, source: string, payload: string}>}
*/
sendRequest(keyword, payload, timeout) {
if (timeout == null) {
timeout = this._defaultTimeout;
}
if (!this._socket || !this._connected) {
return Promise.reject(new Error("Client is not connected."));
}
const requestId = this._generateRequestId();
const message = Buffer.concat([
Buffer.from(`${requestId}${DELIMITER}${keyword}${DELIMITER}${payload}`, "utf-8"),
EOM,
]);
return new Promise((resolve, reject) => {
let timer = null;
const cleanup = () => {
this._pendingRequests.delete(requestId);
if (timer) {
clearTimeout(timer);
timer = null;
}
};
this._pendingRequests.set(requestId, {
resolve: (value) => {
cleanup();
resolve(value);
},
reject: (err) => {
cleanup();
reject(err);
},
});
if (timeout > 0) {
timer = setTimeout(() => {
const pending = this._pendingRequests.get(requestId);
if (pending) {
cleanup();
reject(new Error("Request timed out."));
}
}, timeout * 1000);
}
this._socket.write(message, (err) => {
if (err) {
cleanup();
reject(err);
}
});
});
}
// ------------------------------------------------------------------
// Direct ORM-style API (insert / update / delete / onql / build)
//
// `query` arguments are ONQL expression *strings*, e.g.
// "mydb.users[id=\"u1\"].id"
// "mydb.orders[status=\"pending\"]"
// Use `client.build(template, ...values)` to substitute $1, $2 ...
// ------------------------------------------------------------------
/**
* Parse the standard `{error, data}` envelope returned by the server.
* Throws when `error` is truthy; otherwise returns `data`.
* @param {string} raw
* @returns {any}
*/
_processResult(raw) {
let parsed;
try {
parsed = JSON.parse(raw);
} catch (_) {
throw new Error(String(raw));
}
if (parsed && parsed.error) {
throw new Error(parsed.error);
}
return parsed ? parsed.data : undefined;
}
/**
* Insert a single record into `db.table`.
* @param {string} db Database name.
* @param {string} table Target table.
* @param {object} data A single record object.
* @returns {Promise<any>} Parsed `data` from the server envelope.
*/
async insert(db, table, data) {
if (data === null || typeof data !== "object" || Array.isArray(data)) {
throw new Error("insert() expects a single record object");
}
const payload = JSON.stringify({
db,
table,
records: data,
});
const res = await this.sendRequest("insert", payload);
return this._processResult(res.payload);
}
/**
* Update records matching `query` (or the explicit `ids`) in `db.table`.
*
* @param {string} db Database name.
* @param {string} table Target table.
* @param {object} data Fields to update.
* @param {string} query ONQL query expression (e.g.
* 'mydb.users[id="u1"].id'). Pass `""` if
* using `opts.ids` instead.
* @param {object} [opts]
* @param {string} [opts.protopass="default"]
* @param {string[]} [opts.ids=[]] Explicit record IDs (alternative to query).
* @returns {Promise<any>}
*/
async update(db, table, data, query, opts = {}) {
const payload = JSON.stringify({
db,
table,
records: data,
query: query || "",
protopass: opts.protopass || "default",
ids: opts.ids || [],
});
const res = await this.sendRequest("update", payload);
return this._processResult(res.payload);
}
/**
* Delete records matching `query` (or the explicit `ids`) in `db.table`.
*
* @param {string} db Database name.
* @param {string} table Target table.
* @param {string} query ONQL query expression, or `""` if using `opts.ids`.
* @param {object} [opts]
* @param {string} [opts.protopass="default"]
* @param {string[]} [opts.ids=[]]
* @returns {Promise<any>}
*/
async delete(db, table, query, opts = {}) {
const payload = JSON.stringify({
db,
table,
query: query || "",
protopass: opts.protopass || "default",
ids: opts.ids || [],
});
const res = await this.sendRequest("delete", payload);
return this._processResult(res.payload);
}
/**
* Execute a raw ONQL query.
* @param {string} query ONQL query expression (e.g. 'mydb.users[active="yes"]').
* @param {object} [opts]
* @param {string} [opts.protopass="default"]
* @param {string} [opts.ctxkey=""]
* @param {string[]} [opts.ctxvalues=[]]
* @returns {Promise<any>} Parsed `data` from the server envelope.
*/
async onql(query, opts = {}) {
const payload = JSON.stringify({
query: query,
protopass: opts.protopass || "default",
ctxkey: opts.ctxkey || "",
ctxvalues: opts.ctxvalues || [],
});
const res = await this.sendRequest("onql", payload);
return this._processResult(res.payload);
}
/**
* Replace `$1`, `$2`, ... placeholders in `query` with the supplied values.
* Strings are double-quoted, numbers and booleans are inlined verbatim.
* @param {string} query
* @param {...any} values
* @returns {string}
*/
build(query, ...values) {
values.forEach((value, i) => {
const placeholder = "$" + (i + 1);
let replacement;
if (typeof value === "string") {
replacement = '"' + value + '"';
} else if (typeof value === "boolean" || typeof value === "number") {
replacement = String(value);
} else {
replacement = String(value);
}
query = query.split(placeholder).join(replacement);
});
return query;
}
}
module.exports = { ONQLClient };