@atolljs/core 0.1.0 → 0.1.1

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.
Files changed (51) hide show
  1. package/README.md +159 -59
  2. package/dist/contract/listSchema.d.ts +47 -0
  3. package/dist/contract/msgpackCodec.d.ts +7 -0
  4. package/dist/contract/msgpackrCodec.d.ts +2 -0
  5. package/dist/contract/mz.d.ts +43 -0
  6. package/dist/contract/sharedMemory.d.ts +350 -0
  7. package/dist/contract/types.d.ts +131 -0
  8. package/dist/contract/zod.d.ts +53 -0
  9. package/dist/index.d.ts +30 -0
  10. package/dist/index.js +18 -18
  11. package/dist/index10.js +14 -178
  12. package/dist/index11.js +188 -47
  13. package/dist/index12.js +45 -59
  14. package/dist/index13.js +59 -20
  15. package/dist/index14.js +24 -60
  16. package/dist/index15.js +56 -43
  17. package/dist/index16.js +45 -41
  18. package/dist/index17.js +42 -38
  19. package/dist/index18.js +36 -55
  20. package/dist/index19.js +52 -47
  21. package/dist/index20.js +48 -51
  22. package/dist/index21.js +59 -0
  23. package/dist/index3.js +24 -117
  24. package/dist/index4.js +116 -34
  25. package/dist/index5.js +32 -473
  26. package/dist/index6.js +489 -19
  27. package/dist/index7.js +19 -6
  28. package/dist/index8.js +6 -33
  29. package/dist/index9.js +29 -14
  30. package/dist/log.d.ts +35 -0
  31. package/dist/observable.d.ts +30 -0
  32. package/dist/pool/errors.d.ts +16 -0
  33. package/dist/pool/memory.d.ts +19 -0
  34. package/dist/pool/workerClient.d.ts +80 -0
  35. package/dist/pool/workerPool.d.ts +115 -0
  36. package/dist/reactive.d.ts +46 -0
  37. package/dist/service.d.ts +181 -0
  38. package/dist/shared/sharedWorkerClient.d.ts +35 -0
  39. package/dist/shared/sharedWorkerHost.d.ts +21 -0
  40. package/dist/task.d.ts +40 -0
  41. package/dist/worker/defineWorker.d.ts +63 -0
  42. package/dist/worker/registry.d.ts +8 -0
  43. package/dist/worker/workerBootstrap.d.ts +1 -0
  44. package/package.json +14 -2
  45. package/src/contract/listSchema.ts +13 -8
  46. package/src/contract/mz.ts +1 -1
  47. package/src/contract/sharedMemory.ts +28 -1
  48. package/src/contract/types.ts +21 -0
  49. package/src/contract/zod.ts +44 -0
  50. package/src/index.ts +2 -1
  51. package/src/pool/workerPool.ts +27 -1
package/dist/index5.js CHANGED
@@ -1,481 +1,40 @@
1
- import { msgpackrCodec as e } from "./index2.js";
2
- import { memberToSpec as t, zodArrayInfo as n, zodObjectShape as r, zodStringBytes as i } from "./index3.js";
3
- import { fmtBytes as a, scoped as o } from "./index4.js";
4
- import { z as s } from "zod";
5
- //#region src/contract/sharedMemory.ts
6
- var c = o("memory"), l = 8, u = (e, t) => e + t - 1 & ~(t - 1), d = (e) => u(e, l), f = (e) => typeof e == "object" && !!e && typeof e.kind == "string" && typeof e.byteLength == "number";
7
- function p(e) {
8
- let t = [];
9
- for (let [n, r] of Object.entries(e)) if (f(r)) t.push({
10
- path: n,
11
- descriptor: r
12
- });
13
- else for (let [e, i] of Object.entries(r)) {
14
- if (!f(i)) throw TypeError(`shared memory "${n}.${e}" is not a field descriptor — intent groups nest one level only`);
15
- t.push({
16
- path: `${n}.${e}`,
17
- descriptor: i
18
- });
19
- }
20
- return t;
21
- }
22
- var m = new TextEncoder(), h = new TextDecoder(), g = {
23
- encode: (e) => m.encode(JSON.stringify(e)),
24
- decode: (e) => JSON.parse(h.decode(e))
25
- }, _ = /* @__PURE__ */ new Map();
26
- function v(e, t) {
27
- _.set(e, t);
28
- }
29
- var y = {
30
- i8: 1,
31
- u8: 1,
32
- i16: 2,
33
- u16: 2,
34
- i32: 4,
35
- u32: 4,
36
- f32: 4,
37
- f64: 8,
38
- i64: 8,
39
- u64: 8
40
- }, b = (e) => typeof e == "string" ? y[e] : "string" in e ? e.string : 1, x = (e) => typeof e == "string" ? y[e] : 1;
41
- function S(e) {
42
- let n = {}, r = {}, i = 0;
43
- for (let a of Object.keys(e)) {
44
- let o = t(e[a], String(a));
45
- r[a] = o, i = u(i, x(o)), n[a] = i, i += b(o);
46
- }
47
- return {
48
- layout: r,
49
- offsets: n,
50
- recordSize: d(i)
51
- };
52
- }
53
- var C = {
54
- number: () => ({
55
- kind: "number",
56
- byteLength: 8
57
- }),
58
- boolean: () => ({
59
- kind: "boolean",
60
- byteLength: 8
61
- }),
62
- string: (e) => {
63
- let { schema: t, maxBytes: n } = e;
64
- if (n !== void 0) return {
65
- kind: "string",
66
- byteLength: 4 + n,
67
- schema: t
68
- };
69
- let r = t ? i(t) : null;
70
- if (r !== null) return {
71
- kind: "string",
72
- byteLength: 4 + r,
73
- schema: t
74
- };
75
- throw TypeError("field.string: pass { maxBytes } or a schema carrying a byte budget — mz.string(n) / z.string().meta({ bytes: n })");
76
- },
77
- object: (e) => {
78
- let { schema: t, maxBytes: n } = e;
79
- if (n === void 0 && t) {
80
- let e = r(t);
81
- if (e) {
82
- let { layout: n, offsets: r, recordSize: i } = S(e);
83
- return {
84
- kind: "object",
85
- byteLength: 8 + i,
86
- schema: t,
87
- layout: n,
88
- offsets: r,
89
- recordSize: i
90
- };
91
- }
92
- throw TypeError("field.object: schema can't derive a fixed byte width (not a zod object of fixed-width members) — pass { maxBytes } for codec-encoded storage");
93
- }
94
- if (n === void 0) throw TypeError("field.object: pass { schema } for a fixed-layout field or { maxBytes } for a codec-encoded field");
95
- return {
96
- kind: "object",
97
- byteLength: 4 + n,
98
- schema: t
99
- };
100
- },
101
- array: (e) => {
102
- let { schema: r, maxBytes: i } = e;
103
- if (i === void 0 && r) {
104
- let e = n(r);
105
- if (e) {
106
- if (e.capacity === null) throw TypeError("field.array: z.array() needs .max(n) or .length(n) — bounded arrays are fixed-width");
107
- let n;
108
- try {
109
- n = t(e.element, "array element");
110
- } catch (e) {
111
- throw TypeError(`field.array: element isn't a fixed-width member — use field.list for arrays of records (${e.message})`);
112
- }
113
- let i = u(b(n), x(n));
114
- return {
115
- kind: "array",
116
- byteLength: 8 + e.capacity * i,
117
- schema: r,
118
- element: n,
119
- elementStride: i,
120
- capacity: e.capacity
121
- };
122
- }
123
- throw TypeError("field.array: schema can't derive a fixed byte width (not a bounded zod array) — pass { maxBytes } for codec-encoded storage");
124
- }
125
- if (i === void 0) throw TypeError("field.array: pass { schema } for a fixed-layout field or { maxBytes } for a codec-encoded field");
126
- return {
127
- kind: "array",
128
- byteLength: 4 + i,
129
- schema: r
130
- };
131
- },
132
- int32Array: ({ length: e }) => ({
133
- kind: "Int32",
134
- byteLength: e * 4
135
- }),
136
- float64Array: ({ length: e }) => ({
137
- kind: "Float64",
138
- byteLength: e * 8
139
- }),
140
- bigInt64Array: ({ length: e }) => ({
141
- kind: "BigInt64",
142
- byteLength: e * 8
143
- }),
144
- uint8Array: ({ length: e }) => ({
145
- kind: "Uint8",
146
- byteLength: e
147
- }),
148
- list: ({ schema: e, count: t }) => {
149
- let n = r(e);
150
- if (!n) throw TypeError("field.list: schema must be an mz.object() of fixed-width members");
151
- let { layout: i, offsets: a, recordSize: o } = S(n);
152
- return {
153
- kind: "list",
154
- byteLength: o * t,
155
- schema: e,
156
- layout: i,
157
- offsets: a,
158
- recordSize: o,
159
- count: t
160
- };
161
- }
162
- }, w = {
163
- Int32: Int32Array,
164
- Float64: Float64Array,
165
- BigInt64: BigInt64Array,
166
- Uint8: Uint8Array
1
+ //#region src/log.ts
2
+ var e = {
3
+ trace: 0,
4
+ debug: 1,
5
+ info: 2,
6
+ warn: 3,
7
+ error: 4,
8
+ off: 5
9
+ }, t = typeof WorkerGlobalScope < "u" && self instanceof WorkerGlobalScope ? "worker" : "main", n = ({ level: e, scope: t, message: n, data: r, thread: i, at: a }) => {
10
+ let o = `[${(a / 1e3).toFixed(2)}s ${i}:${t}]`, s = e === "error" ? console.error : e === "warn" ? console.warn : e === "info" ? console.info : console.debug;
11
+ r === void 0 ? s(`${o} ${n}`) : s(`${o} ${n}`, r);
12
+ }, r = "info", i = n, a = (e) => {
13
+ r = e;
14
+ }, o = () => r, s = (e) => {
15
+ i = e ?? n;
167
16
  };
168
- v("number", (e, t, n) => {
169
- let r = new Float64Array(t.buffer, n, 1);
170
- return {
171
- byteOffset: n,
172
- byteLength: e.byteLength,
173
- read: () => r[0],
174
- write: (e) => {
175
- r[0] = e;
176
- }
177
- };
178
- }), v("boolean", (e, t, n) => {
179
- let r = new Uint8Array(t.buffer, n, 1);
180
- return {
181
- byteOffset: n,
182
- byteLength: e.byteLength,
183
- read: () => r[0] === 1,
184
- write: (e) => {
185
- r[0] = +!!e;
186
- }
187
- };
188
- });
189
- for (let e of Object.keys(w)) v(e, (t, n, r) => {
190
- let i = w[e], a = new i(n.buffer, r, t.byteLength / i.BYTES_PER_ELEMENT);
191
- return {
192
- byteOffset: r,
193
- byteLength: t.byteLength,
194
- read: () => a,
195
- write: (e) => a.set(e)
196
- };
197
- });
198
- v("string", (e, t, n) => {
199
- let r = new TextEncoder(), i = new TextDecoder(), a = e.byteLength - 4, o = new Uint32Array(t.buffer, n, 1), s = (e) => {
200
- if (e.byteLength > a) throw Error(`Encoded value of ${e.byteLength} bytes exceeds field capacity of ${a} bytes at offset ${n}`);
201
- o[0] = e.byteLength, new Uint8Array(t.buffer, n + 4, e.byteLength).set(e);
202
- }, c = () => new Uint8Array(t.buffer, n + 4, o[0]).slice();
203
- return {
204
- byteOffset: n,
205
- byteLength: e.byteLength,
206
- read: () => i.decode(c()),
207
- write: (t) => s(r.encode(e.schema ? e.schema.parse(t) : t))
208
- };
209
- });
210
- function T(e) {
211
- let t = new DataView(e.buffer), n = new Uint8Array(e.buffer), r = new TextEncoder(), i = new TextDecoder(), a = {
212
- i8: {
213
- get: (e) => t.getInt8(e),
214
- set: (e, n) => t.setInt8(e, n)
215
- },
216
- u8: {
217
- get: (e) => t.getUint8(e),
218
- set: (e, n) => t.setUint8(e, n)
219
- },
220
- i16: {
221
- get: (e) => t.getInt16(e),
222
- set: (e, n) => t.setInt16(e, n)
223
- },
224
- u16: {
225
- get: (e) => t.getUint16(e),
226
- set: (e, n) => t.setUint16(e, n)
227
- },
228
- i32: {
229
- get: (e) => t.getInt32(e),
230
- set: (e, n) => t.setInt32(e, n)
231
- },
232
- u32: {
233
- get: (e) => t.getUint32(e),
234
- set: (e, n) => t.setUint32(e, n)
235
- },
236
- f32: {
237
- get: (e) => t.getFloat32(e),
238
- set: (e, n) => t.setFloat32(e, n)
239
- },
240
- f64: {
241
- get: (e) => t.getFloat64(e),
242
- set: (e, n) => t.setFloat64(e, n)
243
- },
244
- i64: {
245
- get: (e) => t.getBigInt64(e),
246
- set: (e, n) => t.setBigInt64(e, n)
247
- },
248
- u64: {
249
- get: (e) => t.getBigUint64(e),
250
- set: (e, n) => t.setBigUint64(e, n)
251
- }
252
- }, o = (e, t) => {
253
- let r = n.indexOf(0, e), a = r === -1 || r - e >= t ? t : r - e;
254
- return i.decode(n.slice(e, e + a));
255
- }, s = /* @__PURE__ */ new Uint8Array(64), c = (e, t, i) => {
256
- s.length < t && (s = new Uint8Array(t));
257
- let { read: a, written: o } = r.encodeInto(i, s);
258
- if (a < i.length || o > t) throw Error(`String exceeds inline field capacity of ${t} bytes`);
259
- n.set(s.subarray(0, o), e), o < t && n.fill(0, e + o, e + t);
260
- }, l = (e) => {
261
- if (typeof e == "string") {
262
- let t = a[e];
263
- return {
264
- get: (e) => t.get(e),
265
- set: (e, n) => t.set(e, n)
266
- };
267
- }
268
- return "bool" in e ? {
269
- get: (e) => !!t.getUint8(e),
270
- set: (e, n) => t.setUint8(e, +!!n)
271
- } : {
272
- get: (t) => o(t, e.string),
273
- set: (t, n) => c(t, e.string, n)
274
- };
275
- };
276
- return {
277
- ops: l,
278
- compile: (e, t) => {
279
- let n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
280
- for (let i of Object.keys(e)) {
281
- let { get: a, set: o } = l(e[i]), s = t[i];
282
- n.set(i, (e, t) => {
283
- t[i] = a(e + s);
284
- }), r.set(i, (e, t) => o(e + s, t[i]));
285
- }
286
- let i = [...n.values()], a = [...r.values()];
287
- return {
288
- read(e, t = {}, r) {
289
- if (r) for (let i of r) n.get(i)(e, t);
290
- else for (let n of i) n(e, t);
291
- return t;
292
- },
293
- write(e, t, n) {
294
- if (n) for (let i of n) r.get(i)(e, t);
295
- else for (let n of a) n(e, t);
296
- }
297
- };
298
- }
299
- };
17
+ function c(n, a, o, s) {
18
+ e[n] < e[r] || i({
19
+ level: n,
20
+ scope: a,
21
+ message: o,
22
+ data: s,
23
+ thread: t,
24
+ at: performance.now()
25
+ });
300
26
  }
301
- var E = (e, t, n) => {
302
- let { schema: r, byteLength: i, kind: a } = e, o = e;
303
- if (o.layout && o.offsets && o.recordSize !== void 0) {
304
- let e = new Uint32Array(t.buffer, n, 2), a = n + 8, s = T(t).compile(o.layout, o.offsets);
305
- return {
306
- byteOffset: n,
307
- byteLength: i,
308
- read: () => e[0] ? s.read(a, {}) : void 0,
309
- write: (t) => {
310
- s.write(a, r ? r.parse(t) : t), e[0] = 1;
311
- }
312
- };
313
- }
314
- if (o.element !== void 0 && o.elementStride !== void 0 && o.capacity !== void 0) {
315
- let e = new Uint32Array(t.buffer, n, 1), a = new Uint32Array(t.buffer, n + 4, 1), s = T(t).ops(o.element), c = o.elementStride, l = o.capacity, u = n + 8;
316
- return {
317
- byteOffset: n,
318
- byteLength: i,
319
- read: () => {
320
- if (!e[0]) return;
321
- let t = a[0];
322
- return Array.from({ length: t }, (e, t) => s.get(u + t * c));
323
- },
324
- write: (t) => {
325
- let n = r ? r.parse(t) : t;
326
- if (n.length > l) throw Error(`${n.length} elements exceeds array capacity of ${l}`);
327
- n.forEach((e, t) => s.set(u + t * c, e)), a[0] = n.length, e[0] = 1;
328
- }
329
- };
330
- }
331
- let s = i - 4, c = new Uint32Array(t.buffer, n, 1), l = (e) => {
332
- if (e.byteLength > s) throw Error(`Encoded value of ${e.byteLength} bytes exceeds field capacity of ${s} bytes at offset ${n}`);
333
- c[0] = e.byteLength, new Uint8Array(t.buffer, n + 4, e.byteLength).set(e);
334
- }, u = () => new Uint8Array(t.buffer, n + 4, c[0]).slice();
335
- return {
336
- byteOffset: n,
337
- byteLength: i,
338
- read: () => {
339
- let e = u();
340
- if (e.byteLength === 0) return;
341
- let n = t.codec.decode(e);
342
- return r ? r.parse(n) : n;
343
- },
344
- write: (e) => l(t.codec.encode(r ? r.parse(e) : e))
345
- };
346
- };
347
- v("object", E), v("array", E);
348
- function D(e, t, n) {
349
- let r = e, { offsets: i, recordSize: a, count: o } = r, s = T(t).compile(r.layout, i), c = (e) => {
350
- if (e < 0 || e >= o) throw RangeError(`Record index ${e} out of bounds (0..${o - 1})`);
351
- }, l = (e, t = {}, r) => (c(e), s.read(n + e * a, t, r)), u = (e, t, r) => {
352
- c(e), s.write(n + e * a, t, r);
353
- };
27
+ function l(e) {
354
28
  return {
355
- byteOffset: n,
356
- byteLength: e.byteLength,
357
- recordCount: o,
358
- recordSize: a,
359
- readAt: l,
360
- writeAt: u,
361
- commit: () => {
362
- t.version && (Atomics.add(t.version.view, t.version.index, 1), Atomics.notify(t.version.view, t.version.index));
363
- },
364
- read: () => Array.from({ length: o }, (e, t) => l(t)),
365
- write: (e) => {
366
- if (e.length > o) throw Error(`${e.length} records exceeds list array capacity of ${o}`);
367
- e.forEach((e, t) => u(t, e));
368
- }
29
+ trace: (t, n) => c("trace", e, t, n),
30
+ debug: (t, n) => c("debug", e, t, n),
31
+ info: (t, n) => c("info", e, t, n),
32
+ warn: (t, n) => c("warn", e, t, n),
33
+ error: (t, n) => c("error", e, t, n)
369
34
  };
370
35
  }
371
- v("list", D);
372
- var O = [];
373
- function k(e) {
374
- let t = {};
375
- for (let [n, r] of Object.entries(e)) t[n] = f(r) ? A(r) : Object.fromEntries(Object.entries(r).map(([e, t]) => [e, A(t)]));
376
- return t;
377
- }
378
- function A(e) {
379
- if (e.schema) return e.schema;
380
- switch (e.kind) {
381
- case "number": return s.number();
382
- case "boolean": return s.boolean();
383
- case "string": return s.string();
384
- case "Int32": return s.instanceof(Int32Array);
385
- case "Float64": return s.instanceof(Float64Array);
386
- case "BigInt64": return s.instanceof(BigInt64Array);
387
- case "Uint8": return s.instanceof(Uint8Array);
388
- default: return s.unknown();
389
- }
390
- }
391
- var j = class {
392
- totalBytes;
393
- spec;
394
- schemas;
395
- codec;
396
- plugins;
397
- entries;
398
- offsets = /* @__PURE__ */ new Map();
399
- versionOffset;
400
- connectors = /* @__PURE__ */ new Map();
401
- boundListeners = /* @__PURE__ */ new Set();
402
- isBound = !1;
403
- constructor(t, n = {}) {
404
- this.spec = t, this.codec = n.codec ?? e, this.plugins = n.plugins, this.entries = p(t), this.schemas = k(t);
405
- let r = 0;
406
- for (let { path: e, descriptor: t } of this.entries) r = d(r), this.offsets.set(e, r), r += t.byteLength;
407
- this.versionOffset = d(r), this.totalBytes = this.versionOffset + d(this.entries.length * 4), O.push(this), c.info(`contract defined: ${this.entries.length} field(s), ${a(this.totalBytes)}`, {
408
- fields: Object.fromEntries(this.entries.map((e) => [e.path, `${e.descriptor.kind}@${this.offsets.get(e.path)?.toLocaleString()}+${a(e.descriptor.byteLength)}`])),
409
- pluginOverrides: Object.keys(this.plugins ?? {})
410
- });
411
- }
412
- bind(e) {
413
- this.connectors.clear();
414
- let t = new Int32Array(e, this.versionOffset, this.entries.length);
415
- this.entries.forEach(({ path: n, descriptor: r }, i) => {
416
- let o = this.plugins?.[r.kind] ?? _.get(r.kind);
417
- if (!o) throw Error(`Unknown shared memory field kind: ${r.kind}`);
418
- c.debug(`bind "${n}" (${r.kind}, ${a(r.byteLength)}) via ${this.plugins?.[r.kind] ? "plugin" : "built-in"} factory`);
419
- let s = o(r, {
420
- buffer: e,
421
- codec: this.codec,
422
- version: {
423
- view: t,
424
- index: i
425
- }
426
- }, this.offsets.get(n));
427
- this.connectors.set(n, {
428
- ...s,
429
- write: (e) => {
430
- s.write(e), Atomics.add(t, i, 1), Atomics.notify(t, i);
431
- },
432
- _version: {
433
- view: t,
434
- index: i
435
- }
436
- });
437
- }), this.isBound = !0;
438
- for (let e of [...this.boundListeners]) e();
439
- }
440
- get bound() {
441
- return this.isBound;
442
- }
443
- onBound(e) {
444
- return this.isBound ? (e(), () => {}) : (this.boundListeners.add(e), () => this.boundListeners.delete(e));
445
- }
446
- connector(e) {
447
- let t = String(e), n = this.connectors.get(t);
448
- if (!n) throw Error(`Shared memory field "${t}" was accessed before the buffer was bound on this thread.`);
449
- return n;
450
- }
451
- };
452
- function M(e, t = {}) {
453
- let n = new j(e, t);
454
- for (let [t, r] of Object.entries(e)) if (f(r)) Object.defineProperty(n, t, {
455
- enumerable: !0,
456
- get: () => n.connector(t)
457
- });
458
- else {
459
- let e = {};
460
- for (let i of Object.keys(r)) Object.defineProperty(e, i, {
461
- enumerable: !0,
462
- get: () => n.connector(`${t}.${i}`)
463
- });
464
- Object.defineProperty(n, t, {
465
- enumerable: !0,
466
- get: () => e
467
- });
468
- }
469
- return n;
470
- }
471
- function N(e) {
472
- for (let t of O) t.bind(e);
473
- c.info(`bound ${O.length} contract(s) — ${a(e.byteLength)} buffer`);
474
- }
475
- function P() {
476
- return O.length;
477
- }
36
+ var u = (e) => e >= 1 << 20 ? `${(e / (1 << 20)).toFixed(1)}MB` : e >= 1024 ? `${(e / 1024).toFixed(1)}KB` : `${e}B`;
478
37
  //#endregion
479
- export { j as SharedMemory, N as bindSharedMemories, M as defineSharedMemory, C as field, P as getDefinedSharedMemoryCount, g as jsonCodec, v as registerConnectorFactory };
38
+ export { u as fmtBytes, o as getLogLevel, c as log, l as scoped, a as setLogLevel, s as setLogSink };
480
39
 
481
40
  //# sourceMappingURL=index5.js.map