@based/db 0.0.44 → 0.0.45

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
@@ -142,7 +142,7 @@ export function create(db, type, obj, opts) {
142
142
  ctx.prefix0 = -1; // Force a new cursor
143
143
  ctx.len = pos;
144
144
  if (err === RANGE_ERR) {
145
- if (pos === 0) {
145
+ if (pos === 8) {
146
146
  throw new Error('!No range available');
147
147
  }
148
148
  flushBuffer(db);
@@ -67,7 +67,7 @@ export const update = (db, type, id, obj, opts) => {
67
67
  ctx.prefix0 = -1; // force a new cursor
68
68
  ctx.len = pos;
69
69
  if (err === RANGE_ERR) {
70
- if (pos === 0) {
70
+ if (pos === 8) {
71
71
  throw new Error('out of range');
72
72
  }
73
73
  flushBuffer(db);
@@ -285,6 +285,7 @@ export class BasedDbQuery extends QueryBranch {
285
285
  throw new Error('Query: No schema yet - use await db.schemaIsSet()');
286
286
  }
287
287
  const def = createQueryDef(db, QueryDefType.Root, target, skipValidation);
288
+ def.schemaChecksum = db.schema?.hash || 0;
288
289
  super(db, def);
289
290
  this.db = db;
290
291
  this.skipValidation = skipValidation;
@@ -295,6 +296,7 @@ export class BasedDbQuery extends QueryBranch {
295
296
  this.id = undefined;
296
297
  this.buffer = undefined;
297
298
  const def = createQueryDef(this.db, QueryDefType.Root, this.target, this.skipValidation);
299
+ def.schemaChecksum = this.db.schema?.hash || 0;
298
300
  this.def = def;
299
301
  const q = this.queryCommands;
300
302
  this.queryCommands = [];
@@ -316,8 +318,20 @@ export class BasedDbQuery extends QueryBranch {
316
318
  }
317
319
  const d = performance.now();
318
320
  await this.db.isModified();
321
+ if (this.db.schema?.hash !== this.def.schemaChecksum) {
322
+ this.reBuildQuery();
323
+ return this.#getInternal(resolve, reject);
324
+ }
319
325
  const res = await this.db.hooks.getQueryBuf(buf);
320
- if (res instanceof Error) {
326
+ if (res.byteLength === 1) {
327
+ if (res[0] === 0) {
328
+ reject(new Error('schema mismatch'));
329
+ }
330
+ else {
331
+ reject(new Error('unexpected error'));
332
+ }
333
+ }
334
+ else if (res instanceof Error) {
321
335
  reject(res);
322
336
  }
323
337
  else {
@@ -4,7 +4,6 @@ import { defToBuffer } from './toBuffer.js';
4
4
  import { handleErrors } from './validation.js';
5
5
  export const registerQuery = (q) => {
6
6
  if (!q.id) {
7
- q.def.schemaChecksum = q.db.schema.hash;
8
7
  const b = defToBuffer(q.db, q.def);
9
8
  const buf = concatUint8Arr(b);
10
9
  let id = native.crc32(buf);
package/dist/src/index.js CHANGED
@@ -53,6 +53,7 @@ export class BasedDb {
53
53
  maxModifySize,
54
54
  hooks: {
55
55
  subscribe(q, onData, onError) {
56
+ let killed = false;
56
57
  let timer;
57
58
  let prevChecksum;
58
59
  let lastLen = 0;
@@ -63,8 +64,16 @@ export class BasedDb {
63
64
  q.reBuildQuery();
64
65
  registerQuery(q);
65
66
  response = undefined;
67
+ timer = setTimeout(get, 0);
68
+ return;
69
+ }
70
+ if (killed) {
71
+ return;
66
72
  }
67
73
  const res = await server.getQueryBuf(q.buffer);
74
+ if (killed) {
75
+ return;
76
+ }
68
77
  if (!response) {
69
78
  response = new BasedQueryResponse(q.id, q.def, res, 0);
70
79
  }
@@ -82,6 +91,7 @@ export class BasedDb {
82
91
  };
83
92
  get();
84
93
  return () => {
94
+ killed = true;
85
95
  clearTimeout(timer);
86
96
  };
87
97
  },
@@ -469,6 +469,10 @@ export class DbServer {
469
469
  console.error('Db is stopped - trying to query', buf.byteLength);
470
470
  return Promise.resolve(new Uint8Array(8));
471
471
  }
472
+ const schemaChecksum = readUint64(buf, buf.byteLength - 8);
473
+ if (schemaChecksum !== this.schema?.hash) {
474
+ return Promise.resolve(new Uint8Array(1));
475
+ }
472
476
  if (this.modifyQueue.length) {
473
477
  return new Promise((resolve) => {
474
478
  this.addToQueryQueue(resolve, buf);
@@ -1,13 +1,12 @@
1
1
  import { isMainThread, receiveMessageOnPort, workerData, } from 'node:worker_threads';
2
2
  import native from '../native.js';
3
- let workerCtx;
4
3
  if (isMainThread) {
5
4
  console.warn('running query worker.ts in mainthread');
6
5
  }
7
6
  else {
8
7
  let { address, channel } = workerData;
9
8
  let dbCtx = native.externalFromInt(address);
10
- workerCtx = native.workerCtxInit();
9
+ native.workerCtxInit();
11
10
  // const transferList = new Array(1)
12
11
  const handleMsg = (msg) => {
13
12
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.44",
3
+ "version": "0.0.45",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",