libfx 0.0.7 → 0.0.8-dev.820.g1d9d3b63d6ea

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/core-output.js ADDED
@@ -0,0 +1,58 @@
1
+ export const maxCoreMessageBytes = 64 * 1024 * 1024;
2
+
3
+ // One ordered writer per core; a pending handler holds admission of the next message.
4
+ export class CoreOutput {
5
+ constructor(handler) {
6
+ this.handler = handler;
7
+ this.decoder = new TextDecoder("utf-8", { fatal: true });
8
+ this.buffer = new Uint8Array(0);
9
+ this.bytes = 0;
10
+ this.closed = false;
11
+ }
12
+
13
+ write(chunk) {
14
+ let offset = 0;
15
+ const consume = () => {
16
+ while (offset < chunk.length) {
17
+ if (this.closed) throw new Error("core output is closed");
18
+ const newline = chunk.indexOf(10, offset);
19
+ const end = newline < 0 ? chunk.length : newline;
20
+ const fragment = chunk.subarray(offset, end);
21
+ const length = this.bytes + fragment.length;
22
+ if (length + (newline < 0 ? 0 : 1) > maxCoreMessageBytes) throw new RangeError("core output message exceeds 64 MiB");
23
+ let data = fragment;
24
+ if (this.bytes || newline < 0) {
25
+ if (length > this.buffer.length) {
26
+ const next = new Uint8Array(Math.min(maxCoreMessageBytes, Math.max(length, this.buffer.length * 2, 4096)));
27
+ next.set(this.buffer.subarray(0, this.bytes));
28
+ this.buffer = next;
29
+ }
30
+ this.buffer.set(fragment, this.bytes);
31
+ data = this.buffer.subarray(0, length);
32
+ }
33
+ this.bytes = length;
34
+ offset = newline < 0 ? end : end + 1;
35
+ if (newline < 0) return;
36
+ const line = this.decoder.decode(data);
37
+ const size = length + 1;
38
+ this.bytes = 0;
39
+ if (this.buffer.length > 1024 * 1024) this.buffer = new Uint8Array(0);
40
+ if (line) {
41
+ const pending = this.handler(JSON.parse(line), size);
42
+ if (pending) return Promise.resolve(pending).then(consume);
43
+ }
44
+ }
45
+ };
46
+ return consume();
47
+ }
48
+
49
+ finish() {
50
+ if (this.bytes) throw new Error("core output ended within a message");
51
+ }
52
+
53
+ close() {
54
+ this.closed = true;
55
+ this.buffer = new Uint8Array(0);
56
+ this.bytes = 0;
57
+ }
58
+ }
package/fx-core.wasm CHANGED
Binary file