@based/db 0.0.49 → 0.0.51

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
@@ -1,7 +1,7 @@
1
1
  import { REFERENCES } from '@based/schema/def';
2
2
  import { ModifyError, ModifyState } from '../ModifyRes.js';
3
3
  import { setCursor } from '../setCursor.js';
4
- import { DELETE, EDGE_INDEX_REALID, EDGE_INDEX_TMPID, EDGE_NOINDEX_REALID, EDGE_NOINDEX_TMPID, NOEDGE_INDEX_REALID, NOEDGE_INDEX_TMPID, NOEDGE_NOINDEX_REALID, NOEDGE_NOINDEX_TMPID, RANGE_ERR, REF_OP_UPDATE, } from '../types.js';
4
+ import { DELETE, EDGE_INDEX_REALID, EDGE_INDEX_TMPID, EDGE_NOINDEX_REALID, EDGE_NOINDEX_TMPID, NOEDGE_INDEX_REALID, NOEDGE_INDEX_TMPID, NOEDGE_NOINDEX_REALID, NOEDGE_NOINDEX_TMPID, RANGE_ERR, REF_OP_OVERWRITE, REF_OP_PUT_ADD, REF_OP_PUT_OVERWRITE, REF_OP_UPDATE, } from '../types.js';
5
5
  import { writeEdges } from './edge.js';
6
6
  export function writeReferences(value, ctx, schema, def, res, mod) {
7
7
  if (typeof value !== 'object') {
@@ -266,8 +266,8 @@ function putRefs(def, ctx, modifyOp, refs, op) {
266
266
  ctx.buf[ctx.len++] = size >>>= 8;
267
267
  ctx.buf[ctx.len++] = size >>>= 8;
268
268
  ctx.buf[ctx.len++] = size >>>= 8;
269
- ctx.buf[ctx.len++] = op === 0 ? 3 : 4;
270
- ctx.len = (ctx.len + 3) & ~3;
269
+ ctx.buf[ctx.len++] =
270
+ op === REF_OP_OVERWRITE ? REF_OP_PUT_OVERWRITE : REF_OP_PUT_ADD;
271
271
  let i = 0;
272
272
  for (; i < refs.length; i++) {
273
273
  let ref = refs[i];
@@ -37,4 +37,6 @@ export declare const EDGE_INDEX_TMPID = 6;
37
37
  export declare const NOEDGE_INDEX_TMPID = 7;
38
38
  export declare const REF_OP_UPDATE = 1;
39
39
  export declare const REF_OP_OVERWRITE = 0;
40
+ export declare const REF_OP_PUT_OVERWRITE = 3;
41
+ export declare const REF_OP_PUT_ADD = 4;
40
42
  export type REF_OP = typeof REF_OP_OVERWRITE | typeof REF_OP_UPDATE;
@@ -25,4 +25,6 @@ export const EDGE_INDEX_TMPID = 6;
25
25
  export const NOEDGE_INDEX_TMPID = 7;
26
26
  export const REF_OP_UPDATE = 1;
27
27
  export const REF_OP_OVERWRITE = 0;
28
+ export const REF_OP_PUT_OVERWRITE = 3;
29
+ export const REF_OP_PUT_ADD = 4;
28
30
  //# sourceMappingURL=types.js.map
@@ -12,6 +12,8 @@ import { convertFilter } from './filter/convertFilter.js';
12
12
  import { validateLocale, validateRange } from './validation.js';
13
13
  import { DEF_RANGE_PROP_LIMIT } from './thresholds.js';
14
14
  import { concatUint8Arr } from '@saulx/utils';
15
+ import { displayTarget } from './display.js';
16
+ import picocolors from 'picocolors';
15
17
  export class QueryBranch {
16
18
  db;
17
19
  def;
@@ -357,7 +359,19 @@ export class BasedDbQuery extends QueryBranch {
357
359
  return this;
358
360
  }
359
361
  subscribe(onData, onError) {
360
- return subscribe(this, onData, onError ??
362
+ return subscribe(this, (res) => {
363
+ try {
364
+ onData(res);
365
+ }
366
+ catch (err) {
367
+ // const t = displayTarget(this.def)
368
+ const def = this.def;
369
+ let name = picocolors.red(`QueryError[${displayTarget(def)}]\n`);
370
+ name += ` Error executing onData handler in subscription\n`;
371
+ name += ` ${err.message}\n`;
372
+ console.error(name);
373
+ }
374
+ }, onError ??
361
375
  ((err) => {
362
376
  console.error(err);
363
377
  }));
package/dist/src/hooks.js CHANGED
@@ -1,3 +1,5 @@
1
+ import picocolors from 'picocolors';
2
+ import { displayTarget } from './client/query/display.js';
1
3
  export const getDefaultHooks = (server, subInterval = 200) => {
2
4
  return {
3
5
  subscribe(q, onData, onError) {
@@ -8,7 +10,7 @@ export const getDefaultHooks = (server, subInterval = 200) => {
8
10
  if (killed) {
9
11
  return;
10
12
  }
11
- if (res.byteLength >= 8) {
13
+ if (res.byteLength >= 4) {
12
14
  onData(res);
13
15
  }
14
16
  else if (res.byteLength === 1 && res[0] === 0) {
@@ -17,7 +19,10 @@ export const getDefaultHooks = (server, subInterval = 200) => {
17
19
  return;
18
20
  }
19
21
  else {
20
- onError(new Error('unexpected error'));
22
+ const def = this.def;
23
+ let name = picocolors.red(`QueryError[${displayTarget(def)}]\n`);
24
+ name += ` Incorrect buffer received in subscription (maybe server not started ${res.byteLength}) bytes\n`;
25
+ onError(new Error(name));
21
26
  }
22
27
  timer = setTimeout(poll, subInterval);
23
28
  };
@@ -55,14 +55,15 @@ export const parseSchema = (strictSchema) => {
55
55
  };
56
56
  const props = { ...strictSchema.props };
57
57
  for (const key in props) {
58
- const prop = props[key];
58
+ const prop = { ...props[key] };
59
59
  const propType = getPropType(prop);
60
60
  let refProp;
61
61
  if (propType === 'reference') {
62
62
  refProp = prop;
63
63
  }
64
64
  else if (propType === 'references') {
65
- refProp = prop.items;
65
+ refProp = { ...prop.items };
66
+ prop.items = refProp;
66
67
  }
67
68
  if (refProp) {
68
69
  const type = parsedSchema.types[refProp.ref];
@@ -81,6 +82,7 @@ export const parseSchema = (strictSchema) => {
81
82
  };
82
83
  refProp.prop = inverseKey;
83
84
  }
85
+ props[key] = prop;
84
86
  }
85
87
  // @ts-ignore This creates an internal type to use for root props
86
88
  parsedSchema.types._root = {
@@ -287,7 +287,13 @@ export class DbServer {
287
287
  if (schemaLooseEqual(parsedSchema, this.schema)) {
288
288
  return this.schema;
289
289
  }
290
- return this.migrateSchema(strictSchema, transformFns);
290
+ try {
291
+ return this.migrateSchema(strictSchema, transformFns);
292
+ }
293
+ catch (e) {
294
+ console.error('error migrating schema:', e);
295
+ return this.schema;
296
+ }
291
297
  }
292
298
  this.schema = {
293
299
  lastId: this.schema.lastId,
@@ -2,13 +2,12 @@ import { DbWorker, SCHEMA_FILE, WRITELOG_FILE } from './index.js';
2
2
  import native from '../native.js';
3
3
  import { rm, mkdir, readFile } from 'node:fs/promises';
4
4
  import { join } from 'node:path';
5
- import { hashEq } from './csmt/index.js';
6
- import { foreachBlock, initCsmt, makeCsmtKey } from './tree.js';
5
+ import { destructureCsmtKey, foreachBlock, initCsmt, makeCsmtKey, specialBlock } from './tree.js';
7
6
  import { availableParallelism } from 'node:os';
8
7
  import exitHook from 'exit-hook';
9
8
  import { save } from './save.js';
10
9
  import { DEFAULT_BLOCK_CAPACITY } from '@based/schema/def';
11
- import { bufToHex, hexToBuf } from '@saulx/utils';
10
+ import { bufToHex } from '@saulx/utils';
12
11
  export async function start(db, opts) {
13
12
  const path = db.fileSystemPath;
14
13
  const noop = () => { };
@@ -74,10 +73,27 @@ export async function start(db, opts) {
74
73
  }, true);
75
74
  }
76
75
  if (writelog?.hash) {
77
- const oldHash = hexToBuf(writelog.hash);
78
- const newHash = db.merkleTree.getRoot()?.hash;
79
- if (!hashEq(oldHash, newHash)) {
80
- console.error(`WARN: CSMT hash mismatch. expected: ${writelog.hash} actual: ${bufToHex(newHash)}`);
76
+ // FIXME FDN-1301
77
+ //const oldHash = hexToBuf(writelog.hash)
78
+ //const newHash = db.merkleTree.getRoot()?.hash
79
+ //if (!hashEq(oldHash, newHash)) {
80
+ // console.error(
81
+ // `WARN: CSMT hash mismatch. expected: ${writelog.hash} actual: ${bufToHex(newHash)}`,
82
+ // )
83
+ //}
84
+ const oldHashSet = new Set();
85
+ const newHashSet = new Set();
86
+ for (let k in writelog.rangeDumps)
87
+ writelog.rangeDumps[k].forEach(({ hash }) => oldHashSet.add(hash));
88
+ db.merkleTree.visitLeafNodes(({ key, hash }) => {
89
+ const [_typeId, start] = destructureCsmtKey(key);
90
+ if (start == specialBlock)
91
+ return; // skip the type specialBlock
92
+ newHashSet.add(bufToHex(hash));
93
+ });
94
+ const setEq = (a, b) => a.size === b.size && [...a].every(value => b.has(value));
95
+ if (!setEq(oldHashSet, newHashSet)) {
96
+ console.error(`WARN: CSMT hash mismatch.`);
81
97
  }
82
98
  }
83
99
  // start workers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.49",
3
+ "version": "0.0.51",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -57,4 +57,4 @@
57
57
  "tar": "^7.4.3",
58
58
  "typescript": "^5.6.3"
59
59
  }
60
- }
60
+ }