@nocentraldb/sdk 0.2.0-alpha.2 → 0.2.0-alpha.3

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.
package/dist/cbor.js CHANGED
@@ -9,10 +9,38 @@ const text = new TextEncoder();
9
9
  const utf8 = new TextDecoder("utf-8", { fatal: true });
10
10
  /** The canonical bytes of `value`. A key whose value is `undefined` is left out. */
11
11
  export const encode = (value) => {
12
- const out = [];
12
+ const out = new Out();
13
13
  write(out, value);
14
- return Uint8Array.from(out);
14
+ return out.bytes();
15
15
  };
16
+ /** The bytes written so far, in a buffer that doubles as it fills. Bytes are copied in with `set`, never spread as arguments, which overflows the stack from about 125 KiB (NOC-425). */
17
+ class Out {
18
+ buffer = new Uint8Array(64);
19
+ length = 0;
20
+ room(n) {
21
+ if (this.length + n <= this.buffer.length)
22
+ return;
23
+ let size = this.buffer.length * 2;
24
+ while (size < this.length + n)
25
+ size *= 2;
26
+ const grown = new Uint8Array(size);
27
+ grown.set(this.buffer.subarray(0, this.length));
28
+ this.buffer = grown;
29
+ }
30
+ push(...values) {
31
+ this.room(values.length);
32
+ for (const v of values)
33
+ this.buffer[this.length++] = v;
34
+ }
35
+ append(bytes) {
36
+ this.room(bytes.length);
37
+ this.buffer.set(bytes, this.length);
38
+ this.length += bytes.length;
39
+ }
40
+ bytes() {
41
+ return this.buffer.slice(0, this.length);
42
+ }
43
+ }
16
44
  const head = (out, major, n) => {
17
45
  const m = major << 5;
18
46
  const v = BigInt(n);
@@ -46,11 +74,11 @@ const write = (out, value) => {
46
74
  else if (typeof value === "string") {
47
75
  const bytes = text.encode(value);
48
76
  head(out, 3, bytes.length);
49
- out.push(...bytes);
77
+ out.append(bytes);
50
78
  }
51
79
  else if (value instanceof Uint8Array) {
52
80
  head(out, 2, value.length);
53
- out.push(...value);
81
+ out.append(value);
54
82
  }
55
83
  else if (Array.isArray(value)) {
56
84
  head(out, 4, value.length);
@@ -65,7 +93,7 @@ const write = (out, value) => {
65
93
  .sort(([a], [b]) => compare(a, b));
66
94
  head(out, 5, entries.length);
67
95
  for (const [key, v] of entries) {
68
- out.push(...key);
96
+ out.append(key);
69
97
  write(out, v);
70
98
  }
71
99
  }
@@ -102,11 +130,13 @@ const number = (out, n) => {
102
130
  const view = new DataView(new ArrayBuffer(8));
103
131
  view.setFloat32(0, n);
104
132
  if (view.getFloat32(0) === n) {
105
- out.push(0xfa, ...new Uint8Array(view.buffer, 0, 4));
133
+ out.push(0xfa);
134
+ out.append(new Uint8Array(view.buffer, 0, 4));
106
135
  return;
107
136
  }
108
137
  view.setFloat64(0, n);
109
- out.push(0xfb, ...new Uint8Array(view.buffer));
138
+ out.push(0xfb);
139
+ out.append(new Uint8Array(view.buffer));
110
140
  };
111
141
  /** The 16-bit form of `n`, when it keeps `n` exactly. */
112
142
  const toHalf = (n) => {
package/dist/tabs.js CHANGED
@@ -57,7 +57,14 @@ export const tabCore = async (name, platform) => {
57
57
  const me = crypto.randomUUID();
58
58
  const lockName = `nocentraldb/${name}`;
59
59
  const channel = platform.channel(lockName);
60
- const post = (message) => channel.postMessage(message);
60
+ // Once closed, the channel throws on postMessage, while this tab's worker
61
+ // may still answer what it took or report an event (NOC-425): nothing is
62
+ // posted after the close.
63
+ let channelClosed = false;
64
+ const post = (message) => {
65
+ if (!channelClosed)
66
+ channel.postMessage(message);
67
+ };
61
68
  let listener;
62
69
  const emit = (bytes) => listener?.(bytes);
63
70
  // This tab while it owns the database.
@@ -410,6 +417,7 @@ export const tabCore = async (name, platform) => {
410
417
  entry.reject(new NcError("closed", texts.DATABASE_CLOSED));
411
418
  }
412
419
  sent.clear();
420
+ channelClosed = true;
413
421
  channel.close();
414
422
  };
415
423
  return {
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocentraldb/sdk",
3
- "version": "0.2.0-alpha.2",
3
+ "version": "0.2.0-alpha.3",
4
4
  "license": "MIT OR Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",