@based/db 0.0.62 → 0.0.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
Binary file
@@ -67,10 +67,10 @@ export declare class DbServer extends DbShared {
67
67
  getSortIndex(typeId: number, field: number, start: number, lang: number): SortIndex;
68
68
  createSortIndexBuffer(typeId: number, field: number, start: number, lang: number): SortIndex;
69
69
  setSchema(strictSchema: StrictSchema, transformFns?: TransformFns): Promise<SchemaChecksum>;
70
- modify(buf: Uint8Array): Record<number, number> | null;
71
- addToQueryQueue(resolve: any, buf: any): void;
70
+ modify(bufWithHash: Uint8Array): Record<number, number> | null;
71
+ addToQueryQueue(resolve: any, buf: any): Promise<Uint8Array>;
72
72
  getQueryBuf(buf: Uint8Array, fromQueue?: boolean): Promise<Uint8Array>;
73
- onQueryEnd(): void;
73
+ onQueryEnd(): any;
74
74
  stop(noSave?: boolean): Promise<void>;
75
75
  destroy(): Promise<void>;
76
76
  }
@@ -242,14 +242,13 @@ export class DbServer extends DbShared {
242
242
  });
243
243
  return schema.hash;
244
244
  }
245
- modify(buf) {
246
- const schemaHash = readUint64(buf, 0);
247
- // if !schema
245
+ modify(bufWithHash) {
246
+ const schemaHash = readUint64(bufWithHash, 0);
248
247
  if (schemaHash !== this.schema?.hash) {
249
248
  this.emit('info', 'Schema mismatch in modify');
250
249
  return null;
251
250
  }
252
- buf = buf.subarray(8);
251
+ const buf = bufWithHash.subarray(8);
253
252
  const offsets = {};
254
253
  const dataLen = readUint32(buf, buf.length - 4);
255
254
  let typesSize = readUint16(buf, dataLen);
@@ -277,7 +276,7 @@ export class DbServer extends DbShared {
277
276
  offsets[typeId] = offset;
278
277
  }
279
278
  if (this.processingQueries) {
280
- this.modifyQueue.push(new Uint8Array(buf));
279
+ this.modifyQueue.push(new Uint8Array(bufWithHash));
281
280
  }
282
281
  else {
283
282
  this.#modify(buf);
@@ -334,6 +333,11 @@ export class DbServer extends DbShared {
334
333
  resolve(new Error('Query queue exceeded'));
335
334
  return;
336
335
  }
336
+ // TODO should we check here as well? Already will check in DbWorker
337
+ const schemaChecksum = readUint64(buf, buf.byteLength - 8);
338
+ if (schemaChecksum !== this.schema?.hash) {
339
+ return Promise.resolve(new Uint8Array(1));
340
+ }
337
341
  this.queryQueue.set(resolve, buf);
338
342
  }
339
343
  getQueryBuf(buf, fromQueue = false) {
@@ -341,10 +345,6 @@ export class DbServer extends DbShared {
341
345
  console.error('Db is stopped - trying to query', buf.byteLength);
342
346
  return Promise.resolve(new Uint8Array(8));
343
347
  }
344
- const schemaChecksum = readUint64(buf, buf.byteLength - 8);
345
- if (schemaChecksum !== this.schema?.hash) {
346
- return Promise.resolve(new Uint8Array(1));
347
- }
348
348
  if (this.modifyQueue.length) {
349
349
  return new Promise((resolve) => {
350
350
  this.addToQueryQueue(resolve, buf);
@@ -393,7 +393,13 @@ export class DbServer extends DbShared {
393
393
  if (this.modifyQueue.length) {
394
394
  const modifyQueue = this.modifyQueue;
395
395
  this.modifyQueue = [];
396
- for (const buf of modifyQueue) {
396
+ for (const bufWithHash of modifyQueue) {
397
+ const schemaHash = readUint64(bufWithHash, 0);
398
+ if (schemaHash !== this.schema?.hash) {
399
+ this.emit('info', 'Schema mismatch in modify');
400
+ return null;
401
+ }
402
+ const buf = bufWithHash.subarray(8);
397
403
  this.#modify(buf);
398
404
  }
399
405
  }
@@ -13,6 +13,7 @@ else if (workerData?.isDbMigrateWorker) {
13
13
  const { from, to, fromSchema, toSchema, channel, workerState, transformFns } = workerData;
14
14
  const fromCtx = native.externalFromInt(from);
15
15
  const toCtx = native.externalFromInt(to);
16
+ native.workerCtxInit();
16
17
  const fromDb = new BasedDb({ path: null });
17
18
  const toDb = new BasedDb({ path: null });
18
19
  const cp = (obj) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.62",
3
+ "version": "0.0.63",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",