durable-actors 0.17.1 → 0.18.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.
- package/dist/actor/schema.d.ts.map +1 -1
- package/dist/actor/schema.js.map +1 -1
- package/dist/actor/socket.d.ts +6 -6
- package/dist/actor/socket.d.ts.map +1 -1
- package/dist/actor/socket.js +10 -3
- package/dist/actor/socket.js.map +1 -1
- package/dist/actor/socketProtocol.d.ts +16 -15
- package/dist/actor/socketProtocol.d.ts.map +1 -1
- package/dist/actor/socketProtocol.js +1 -58
- package/dist/actor/socketProtocol.js.map +1 -1
- package/dist/actor/socketValidation.d.ts.map +1 -1
- package/dist/actor/socketValidation.js +12 -12
- package/dist/actor/socketValidation.js.map +1 -1
- package/dist/client/telemetry.d.ts +0 -1
- package/dist/client/telemetry.d.ts.map +1 -1
- package/dist/client/telemetry.js.map +1 -1
- package/dist/client-runtime/json.d.ts +2 -1
- package/dist/client-runtime/json.d.ts.map +1 -1
- package/dist/client-runtime/json.js +5 -2
- package/dist/client-runtime/json.js.map +1 -1
- package/dist/generated/client-runtime.d.ts.map +1 -1
- package/dist/generated/client-runtime.js +1 -1
- package/dist/generated/client-runtime.js.map +1 -1
- package/dist/host/actor-entrypoint.d.ts +4 -0
- package/dist/host/actor-entrypoint.d.ts.map +1 -0
- package/dist/host/actor-entrypoint.js +41 -0
- package/dist/host/actor-entrypoint.js.map +1 -0
- package/dist/host/actor-host.d.ts +5 -3
- package/dist/host/actor-host.d.ts.map +1 -1
- package/dist/host/actor-host.js +98 -90
- package/dist/host/actor-host.js.map +1 -1
- package/dist/host/actor-runtime.d.ts +2 -2
- package/dist/host/actor-runtime.d.ts.map +1 -1
- package/dist/host/actor-runtime.js +26 -13
- package/dist/host/actor-runtime.js.map +1 -1
- package/dist/host/console-capture.d.ts +5 -0
- package/dist/host/console-capture.d.ts.map +1 -0
- package/dist/host/console-capture.js +27 -0
- package/dist/host/console-capture.js.map +1 -0
- package/dist/host/database.d.ts +11 -25
- package/dist/host/database.d.ts.map +1 -1
- package/dist/host/database.js +25 -32
- package/dist/host/database.js.map +1 -1
- package/dist/host/generic-host.d.ts +1 -0
- package/dist/host/generic-host.d.ts.map +1 -1
- package/dist/host/generic-host.js +12 -29
- package/dist/host/generic-host.js.map +1 -1
- package/dist/host/invocation.d.ts +15 -0
- package/dist/host/invocation.d.ts.map +1 -0
- package/dist/host/invocation.js +11 -0
- package/dist/host/invocation.js.map +1 -0
- package/dist/host/persisted-fields.d.ts.map +1 -1
- package/dist/host/persisted-fields.js +1 -3
- package/dist/host/persisted-fields.js.map +1 -1
- package/dist/host/protocol.d.ts +35 -110
- package/dist/host/protocol.d.ts.map +1 -1
- package/dist/host/protocol.js +36 -36
- package/dist/host/protocol.js.map +1 -1
- package/dist/host/resident-actor.d.ts +42 -0
- package/dist/host/resident-actor.d.ts.map +1 -0
- package/dist/host/resident-actor.js +218 -0
- package/dist/host/resident-actor.js.map +1 -0
- package/dist/host/types.d.ts +13 -28
- package/dist/host/types.d.ts.map +1 -1
- package/dist/host/validate-artifact.js +4 -8
- package/dist/host/validate-artifact.js.map +1 -1
- package/dist/json.d.ts +7 -2
- package/dist/json.d.ts.map +1 -1
- package/dist/json.js +46 -10
- package/dist/json.js.map +1 -1
- package/dist/payload/blocking-stream.d.ts +25 -0
- package/dist/payload/blocking-stream.d.ts.map +1 -0
- package/dist/payload/blocking-stream.js +39 -0
- package/dist/payload/blocking-stream.js.map +1 -0
- package/dist/payload/database-channel.d.ts +30 -0
- package/dist/payload/database-channel.d.ts.map +1 -0
- package/dist/payload/database-channel.js +220 -0
- package/dist/payload/database-channel.js.map +1 -0
- package/dist/payload/garbage-collection.d.ts +8 -0
- package/dist/payload/garbage-collection.d.ts.map +1 -0
- package/dist/payload/garbage-collection.js +24 -0
- package/dist/payload/garbage-collection.js.map +1 -0
- package/dist/payload/json-text.d.ts +17 -0
- package/dist/payload/json-text.d.ts.map +1 -0
- package/dist/payload/json-text.js +211 -0
- package/dist/payload/json-text.js.map +1 -0
- package/dist/payload/session-lines.d.ts +8 -0
- package/dist/payload/session-lines.d.ts.map +1 -0
- package/dist/payload/session-lines.js +31 -0
- package/dist/payload/session-lines.js.map +1 -0
- package/dist/payload/session-writer.d.ts +22 -0
- package/dist/payload/session-writer.d.ts.map +1 -0
- package/dist/payload/session-writer.js +87 -0
- package/dist/payload/session-writer.js.map +1 -0
- package/dist/payload/stream-reader.d.ts +27 -0
- package/dist/payload/stream-reader.d.ts.map +1 -0
- package/dist/payload/stream-reader.js +86 -0
- package/dist/payload/stream-reader.js.map +1 -0
- package/dist/payload/stream-writer.d.ts +25 -0
- package/dist/payload/stream-writer.d.ts.map +1 -0
- package/dist/payload/stream-writer.js +83 -0
- package/dist/payload/stream-writer.js.map +1 -0
- package/dist/payload/text-slice.d.ts +7 -0
- package/dist/payload/text-slice.d.ts.map +1 -0
- package/dist/payload/text-slice.js +13 -0
- package/dist/payload/text-slice.js.map +1 -0
- package/dist/templates/actor/package.json +1 -1
- package/dist/templates/ai-chat/package.json +1 -1
- package/dist/templates/chat/package.json +1 -1
- package/dist/templates/documents/package.json +1 -1
- package/package.json +2 -2
- package/dist/host/actor-worker.d.ts +0 -2
- package/dist/host/actor-worker.d.ts.map +0 -1
- package/dist/host/actor-worker.js +0 -181
- package/dist/host/actor-worker.js.map +0 -1
- package/dist/host/database-bridge.d.ts +0 -16
- package/dist/host/database-bridge.d.ts.map +0 -1
- package/dist/host/database-bridge.js +0 -29
- package/dist/host/database-bridge.js.map +0 -1
- package/dist/host/worker-supervisor.d.ts +0 -72
- package/dist/host/worker-supervisor.d.ts.map +0 -1
- package/dist/host/worker-supervisor.js +0 -442
- package/dist/host/worker-supervisor.js.map +0 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Socket } from "node:net";
|
|
2
|
+
import type { GarbageCollection } from "./garbage-collection.js";
|
|
3
|
+
/** A string, or bytes that are already UTF-8. */
|
|
4
|
+
type SessionText = string | Uint8Array;
|
|
5
|
+
declare class SessionWriter {
|
|
6
|
+
private readonly socket;
|
|
7
|
+
private readonly collection;
|
|
8
|
+
private readonly texts;
|
|
9
|
+
private start;
|
|
10
|
+
private bound;
|
|
11
|
+
private writing;
|
|
12
|
+
constructor(socket: Pick<Socket, "write">, collection: GarbageCollection);
|
|
13
|
+
write(texts: readonly SessionText[]): void;
|
|
14
|
+
private drain;
|
|
15
|
+
private nextWrite;
|
|
16
|
+
private fill;
|
|
17
|
+
private encodeInto;
|
|
18
|
+
private take;
|
|
19
|
+
}
|
|
20
|
+
export { SessionWriter };
|
|
21
|
+
export type { SessionText };
|
|
22
|
+
//# sourceMappingURL=session-writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-writer.d.ts","sourceRoot":"","sources":["../../src/payload/session-writer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAEtC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAA;AAOhE,iDAAiD;AACjD,KAAK,WAAW,GAAG,MAAM,GAAG,UAAU,CAAA;AAItC,cAAM,aAAa;IAQX,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAR/B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAoB;IAC1C,OAAO,CAAC,KAAK,CAAI;IAEjB,OAAO,CAAC,KAAK,CAAI;IACjB,OAAO,CAAC,OAAO,CAAQ;gBAGF,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,UAAU,EAAE,iBAAiB;IAGlD,KAAK,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,IAAI;IAS1C,OAAO,CAAC,KAAK;IAab,OAAO,CAAC,SAAS;IAcjB,OAAO,CAAC,IAAI;IAQZ,OAAO,CAAC,UAAU;IAYlB,OAAO,CAAC,IAAI;CAKf;AAQD,OAAO,EAAE,aAAa,EAAE,CAAA;AACxB,YAAY,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { sliceEnd } from "./text-slice.js";
|
|
2
|
+
const WRITE_BYTES = 64 * 1024;
|
|
3
|
+
// A UTF-16 code unit encodes to at most three bytes of UTF-8.
|
|
4
|
+
const BYTES_PER_UNIT = 3;
|
|
5
|
+
// The socket is given one bounded buffer at a time. A string is encoded as the socket takes it, so a large one is
|
|
6
|
+
// never held encoded whole, and given 13 MB in one buffer the socket takes three times as long to send it.
|
|
7
|
+
class SessionWriter {
|
|
8
|
+
socket;
|
|
9
|
+
collection;
|
|
10
|
+
texts = [];
|
|
11
|
+
start = 0;
|
|
12
|
+
// No fewer than the bytes still to write, which is what sizes the next buffer.
|
|
13
|
+
bound = 0;
|
|
14
|
+
writing = false;
|
|
15
|
+
constructor(socket, collection) {
|
|
16
|
+
this.socket = socket;
|
|
17
|
+
this.collection = collection;
|
|
18
|
+
}
|
|
19
|
+
write(texts) {
|
|
20
|
+
for (const text of texts) {
|
|
21
|
+
if (text.length === 0)
|
|
22
|
+
continue;
|
|
23
|
+
this.texts.push(text);
|
|
24
|
+
this.bound += mostBytes(text);
|
|
25
|
+
}
|
|
26
|
+
this.drain();
|
|
27
|
+
}
|
|
28
|
+
drain() {
|
|
29
|
+
if (this.writing || this.texts.length === 0)
|
|
30
|
+
return;
|
|
31
|
+
this.writing = true;
|
|
32
|
+
const buffer = this.nextWrite();
|
|
33
|
+
this.socket.write(buffer, () => {
|
|
34
|
+
this.writing = false;
|
|
35
|
+
this.collection.payloadPassed(buffer.byteLength);
|
|
36
|
+
this.drain();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
// Short texts share a buffer, so a burst of short messages costs one system call. Bytes that fill most of a
|
|
40
|
+
// buffer leave as they are, without a copy.
|
|
41
|
+
nextWrite() {
|
|
42
|
+
const first = this.texts[0];
|
|
43
|
+
if (typeof first !== "string" && first.byteLength > WRITE_BYTES / 2)
|
|
44
|
+
return this.take(first);
|
|
45
|
+
const buffer = Buffer.allocUnsafe(Math.min(WRITE_BYTES, this.bound));
|
|
46
|
+
let used = 0;
|
|
47
|
+
while (this.texts.length > 0 && used < buffer.length) {
|
|
48
|
+
const written = this.fill(buffer.subarray(used));
|
|
49
|
+
if (written === 0)
|
|
50
|
+
break;
|
|
51
|
+
used += written;
|
|
52
|
+
}
|
|
53
|
+
return buffer.subarray(0, used);
|
|
54
|
+
}
|
|
55
|
+
// Writes the first text into `room`: as much of a string as fits, and bytes only whole.
|
|
56
|
+
fill(room) {
|
|
57
|
+
const text = this.texts[0];
|
|
58
|
+
if (typeof text === "string")
|
|
59
|
+
return this.encodeInto(text, room);
|
|
60
|
+
if (text.byteLength > room.length)
|
|
61
|
+
return 0;
|
|
62
|
+
room.set(this.take(text));
|
|
63
|
+
return text.byteLength;
|
|
64
|
+
}
|
|
65
|
+
encodeInto(text, room) {
|
|
66
|
+
const end = sliceEnd(text, this.start, room.length);
|
|
67
|
+
const { read, written } = encoder.encodeInto(text.substring(this.start, end), room);
|
|
68
|
+
this.start += read;
|
|
69
|
+
this.bound -= read * BYTES_PER_UNIT;
|
|
70
|
+
if (this.start === text.length) {
|
|
71
|
+
this.texts.shift();
|
|
72
|
+
this.start = 0;
|
|
73
|
+
}
|
|
74
|
+
return written;
|
|
75
|
+
}
|
|
76
|
+
take(bytes) {
|
|
77
|
+
this.texts.shift();
|
|
78
|
+
this.bound -= bytes.byteLength;
|
|
79
|
+
return bytes;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function mostBytes(text) {
|
|
83
|
+
return typeof text === "string" ? text.length * BYTES_PER_UNIT : text.byteLength;
|
|
84
|
+
}
|
|
85
|
+
const encoder = new TextEncoder();
|
|
86
|
+
export { SessionWriter };
|
|
87
|
+
//# sourceMappingURL=session-writer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-writer.js","sourceRoot":"","sources":["../../src/payload/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IAAgB;QAC7C,MAAM,GAAG,GAAG,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;QACnD,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAA;QACnF,IAAI,CAAC,KAAK,IAAI,IAAI,CAAA;QAClB,IAAI,CAAC,KAAK,IAAI,IAAI,GAAG,cAAc,CAAA;QACnC,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;YAC7B,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;YAClB,IAAI,CAAC,KAAK,GAAG,CAAC,CAAA;QAClB,CAAC;QACD,OAAO,OAAO,CAAA;IAClB,CAAC;IAEO,IAAI,CAAC,KAAiB;QAC1B,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;QAClB,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,UAAU,CAAA;QAC9B,OAAO,KAAK,CAAA;IAChB,CAAC;CACJ;AAED,SAAS,SAAS,CAAC,IAAiB;IAChC,OAAO,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,cAAc,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAA;AACpF,CAAC;AAED,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC,OAAO,EAAE,aAAa,EAAE,CAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { BlockingStream } from "./blocking-stream.js";
|
|
2
|
+
/** Reads little-endian values from a blocking stream. Nothing is held beyond the value being read. */
|
|
3
|
+
declare class StreamReader {
|
|
4
|
+
private readonly stream;
|
|
5
|
+
private readonly buffer;
|
|
6
|
+
private start;
|
|
7
|
+
private end;
|
|
8
|
+
private scratch;
|
|
9
|
+
constructor(stream: BlockingStream);
|
|
10
|
+
u8(): number;
|
|
11
|
+
u32(): number;
|
|
12
|
+
/** A 64-bit integer, as a number when a number holds it exactly. */
|
|
13
|
+
integer(): number | bigint;
|
|
14
|
+
f64(): number;
|
|
15
|
+
/** Text after its `u32` size. Bytes that are not UTF-8 decode to U+FFFD. */
|
|
16
|
+
text(): string;
|
|
17
|
+
/** Bytes after their `u32` size, in a buffer of their own. */
|
|
18
|
+
bytes(): Uint8Array;
|
|
19
|
+
/** Gives up the buffer that text larger than the read buffer was decoded from. */
|
|
20
|
+
release(): void;
|
|
21
|
+
private fillScratch;
|
|
22
|
+
private readInto;
|
|
23
|
+
private need;
|
|
24
|
+
private read;
|
|
25
|
+
}
|
|
26
|
+
export { StreamReader };
|
|
27
|
+
//# sourceMappingURL=stream-reader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-reader.d.ts","sourceRoot":"","sources":["../../src/payload/stream-reader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAI1D,sGAAsG;AACtG,cAAM,YAAY;IAMF,OAAO,CAAC,QAAQ,CAAC,MAAM;IALnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmC;IAC1D,OAAO,CAAC,KAAK,CAAI;IACjB,OAAO,CAAC,GAAG,CAAI;IACf,OAAO,CAAC,OAAO,CAAoB;gBAEN,MAAM,EAAE,cAAc;IAEnD,EAAE,IAAI,MAAM;IAKZ,GAAG,IAAI,MAAM;IAOb,oEAAoE;IACpE,OAAO,IAAI,MAAM,GAAG,MAAM;IAQ1B,GAAG,IAAI,MAAM;IAOb,4EAA4E;IAC5E,IAAI,IAAI,MAAM;IASd,8DAA8D;IAC9D,KAAK,IAAI,UAAU;IAMnB,kFAAkF;IAClF,OAAO,IAAI,IAAI;IAKf,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,QAAQ;IAMhB,OAAO,CAAC,IAAI;IAQZ,OAAO,CAAC,IAAI;CAKf;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const BUFFER_BYTES = 64 * 1024;
|
|
2
|
+
/** Reads little-endian values from a blocking stream. Nothing is held beyond the value being read. */
|
|
3
|
+
class StreamReader {
|
|
4
|
+
stream;
|
|
5
|
+
buffer = Buffer.allocUnsafe(BUFFER_BYTES);
|
|
6
|
+
start = 0;
|
|
7
|
+
end = 0;
|
|
8
|
+
scratch;
|
|
9
|
+
constructor(stream) {
|
|
10
|
+
this.stream = stream;
|
|
11
|
+
}
|
|
12
|
+
u8() {
|
|
13
|
+
this.need(1);
|
|
14
|
+
return this.buffer[this.start++];
|
|
15
|
+
}
|
|
16
|
+
u32() {
|
|
17
|
+
this.need(4);
|
|
18
|
+
const value = this.buffer.readUInt32LE(this.start);
|
|
19
|
+
this.start += 4;
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
/** A 64-bit integer, as a number when a number holds it exactly. */
|
|
23
|
+
integer() {
|
|
24
|
+
this.need(8);
|
|
25
|
+
const at = this.start;
|
|
26
|
+
this.start += 8;
|
|
27
|
+
const value = this.buffer.readInt32LE(at + 4) * 0x1_0000_0000 + this.buffer.readUInt32LE(at);
|
|
28
|
+
return Number.isSafeInteger(value) ? value : this.buffer.readBigInt64LE(at);
|
|
29
|
+
}
|
|
30
|
+
f64() {
|
|
31
|
+
this.need(8);
|
|
32
|
+
const value = this.buffer.readDoubleLE(this.start);
|
|
33
|
+
this.start += 8;
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
/** Text after its `u32` size. Bytes that are not UTF-8 decode to U+FFFD. */
|
|
37
|
+
text() {
|
|
38
|
+
const size = this.u32();
|
|
39
|
+
if (size > BUFFER_BYTES)
|
|
40
|
+
return this.fillScratch(size).toString("utf8", 0, size);
|
|
41
|
+
this.need(size);
|
|
42
|
+
const at = this.start;
|
|
43
|
+
this.start += size;
|
|
44
|
+
return this.buffer.toString("utf8", at, at + size);
|
|
45
|
+
}
|
|
46
|
+
/** Bytes after their `u32` size, in a buffer of their own. */
|
|
47
|
+
bytes() {
|
|
48
|
+
const bytes = new Uint8Array(this.u32());
|
|
49
|
+
this.readInto(bytes);
|
|
50
|
+
return bytes;
|
|
51
|
+
}
|
|
52
|
+
/** Gives up the buffer that text larger than the read buffer was decoded from. */
|
|
53
|
+
release() {
|
|
54
|
+
this.scratch = undefined;
|
|
55
|
+
}
|
|
56
|
+
// One buffer serves every large text of an answer, so they do not each leave one behind to be collected.
|
|
57
|
+
fillScratch(size) {
|
|
58
|
+
if (this.scratch === undefined || this.scratch.byteLength < size)
|
|
59
|
+
this.scratch = Buffer.allocUnsafe(size);
|
|
60
|
+
this.readInto(this.scratch.subarray(0, size));
|
|
61
|
+
return this.scratch;
|
|
62
|
+
}
|
|
63
|
+
readInto(target) {
|
|
64
|
+
let filled = this.buffer.copy(target, 0, this.start, this.end);
|
|
65
|
+
this.start += filled;
|
|
66
|
+
while (filled < target.byteLength)
|
|
67
|
+
filled += this.read(target, filled);
|
|
68
|
+
}
|
|
69
|
+
need(bytes) {
|
|
70
|
+
if (this.end - this.start >= bytes)
|
|
71
|
+
return;
|
|
72
|
+
this.buffer.copyWithin(0, this.start, this.end);
|
|
73
|
+
this.end -= this.start;
|
|
74
|
+
this.start = 0;
|
|
75
|
+
while (this.end < bytes)
|
|
76
|
+
this.end += this.read(this.buffer, this.end);
|
|
77
|
+
}
|
|
78
|
+
read(into, offset) {
|
|
79
|
+
const read = this.stream.read(into, offset);
|
|
80
|
+
if (read === 0)
|
|
81
|
+
throw new Error("the stream closed inside a value");
|
|
82
|
+
return read;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export { StreamReader };
|
|
86
|
+
//# sourceMappingURL=stream-reader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-reader.js","sourceRoot":"","sources":["../../src/payload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little-endian values to a blocking stream through one fixed buffer, so text is never encoded whole. */
|
|
4
|
+
declare class StreamWriter {
|
|
5
|
+
private readonly stream;
|
|
6
|
+
private readonly buffer;
|
|
7
|
+
private used;
|
|
8
|
+
constructor(stream: BlockingStream);
|
|
9
|
+
u8(value: number): void;
|
|
10
|
+
u32(value: number): void;
|
|
11
|
+
i64(value: bigint): void;
|
|
12
|
+
f64(value: number): void;
|
|
13
|
+
/** Text as UTF-8 after its `u32` size. */
|
|
14
|
+
text(text: string): void;
|
|
15
|
+
/** Bytes after their `u32` size. */
|
|
16
|
+
bytes(bytes: Uint8Array): void;
|
|
17
|
+
/** Text that is already UTF-8, after its `u32` size. */
|
|
18
|
+
encoded(text: JsonText): void;
|
|
19
|
+
flush(): void;
|
|
20
|
+
private raw;
|
|
21
|
+
private encode;
|
|
22
|
+
private reserve;
|
|
23
|
+
}
|
|
24
|
+
export { StreamWriter };
|
|
25
|
+
//# sourceMappingURL=stream-writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-writer.d.ts","sourceRoot":"","sources":["../../src/payload/stream-writer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAC1D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAO9C,iHAAiH;AACjH,cAAM,YAAY;IAIF,OAAO,CAAC,QAAQ,CAAC,MAAM;IAHnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmC;IAC1D,OAAO,CAAC,IAAI,CAAI;gBAEa,MAAM,EAAE,cAAc;IAEnD,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKvB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKxB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKxB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKxB,0CAA0C;IAC1C,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAYxB,oCAAoC;IACpC,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IAK9B,wDAAwD;IACxD,OAAO,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI;IAK7B,KAAK,IAAI,IAAI;IAOb,OAAO,CAAC,GAAG;IAUX,OAAO,CAAC,MAAM;IAOd,OAAO,CAAC,OAAO;CAGlB;AAID,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { sliceEnd } from "./text-slice.js";
|
|
2
|
+
const BUFFER_BYTES = 64 * 1024;
|
|
3
|
+
// A UTF-16 code unit encodes to at most three bytes of UTF-8.
|
|
4
|
+
const UNITS_PER_BUFFER = Math.floor(BUFFER_BYTES / 3);
|
|
5
|
+
/** Writes little-endian values to a blocking stream through one fixed buffer, so text is never encoded whole. */
|
|
6
|
+
class StreamWriter {
|
|
7
|
+
stream;
|
|
8
|
+
buffer = Buffer.allocUnsafe(BUFFER_BYTES);
|
|
9
|
+
used = 0;
|
|
10
|
+
constructor(stream) {
|
|
11
|
+
this.stream = stream;
|
|
12
|
+
}
|
|
13
|
+
u8(value) {
|
|
14
|
+
this.reserve(1);
|
|
15
|
+
this.buffer[this.used++] = value;
|
|
16
|
+
}
|
|
17
|
+
u32(value) {
|
|
18
|
+
this.reserve(4);
|
|
19
|
+
this.used = this.buffer.writeUInt32LE(value, this.used);
|
|
20
|
+
}
|
|
21
|
+
i64(value) {
|
|
22
|
+
this.reserve(8);
|
|
23
|
+
this.used = this.buffer.writeBigInt64LE(value, this.used);
|
|
24
|
+
}
|
|
25
|
+
f64(value) {
|
|
26
|
+
this.reserve(8);
|
|
27
|
+
this.used = this.buffer.writeDoubleLE(value, this.used);
|
|
28
|
+
}
|
|
29
|
+
/** Text as UTF-8 after its `u32` size. */
|
|
30
|
+
text(text) {
|
|
31
|
+
const size = Buffer.byteLength(text);
|
|
32
|
+
this.u32(size);
|
|
33
|
+
let written = 0;
|
|
34
|
+
for (let start = 0; start < text.length;) {
|
|
35
|
+
const end = sliceEnd(text, start, UNITS_PER_BUFFER);
|
|
36
|
+
written += this.encode(text.substring(start, end));
|
|
37
|
+
start = end;
|
|
38
|
+
}
|
|
39
|
+
if (written !== size)
|
|
40
|
+
throw new Error(`text announced as ${size} bytes encoded to ${written}`);
|
|
41
|
+
}
|
|
42
|
+
/** Bytes after their `u32` size. */
|
|
43
|
+
bytes(bytes) {
|
|
44
|
+
this.u32(bytes.byteLength);
|
|
45
|
+
this.raw(bytes);
|
|
46
|
+
}
|
|
47
|
+
/** Text that is already UTF-8, after its `u32` size. */
|
|
48
|
+
encoded(text) {
|
|
49
|
+
this.u32(text.byteLength);
|
|
50
|
+
for (const chunk of text.chunks)
|
|
51
|
+
this.raw(chunk);
|
|
52
|
+
}
|
|
53
|
+
flush() {
|
|
54
|
+
if (this.used === 0)
|
|
55
|
+
return;
|
|
56
|
+
const used = this.used;
|
|
57
|
+
this.used = 0;
|
|
58
|
+
this.stream.write(this.buffer.subarray(0, used));
|
|
59
|
+
}
|
|
60
|
+
raw(bytes) {
|
|
61
|
+
if (bytes.byteLength > BUFFER_BYTES - this.used) {
|
|
62
|
+
this.flush();
|
|
63
|
+
this.stream.write(bytes);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
this.buffer.set(bytes, this.used);
|
|
67
|
+
this.used += bytes.byteLength;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
encode(slice) {
|
|
71
|
+
this.reserve(slice.length * 3);
|
|
72
|
+
const { written } = encoder.encodeInto(slice, this.buffer.subarray(this.used));
|
|
73
|
+
this.used += written;
|
|
74
|
+
return written;
|
|
75
|
+
}
|
|
76
|
+
reserve(bytes) {
|
|
77
|
+
if (bytes > BUFFER_BYTES - this.used)
|
|
78
|
+
this.flush();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const encoder = new TextEncoder();
|
|
82
|
+
export { StreamWriter };
|
|
83
|
+
//# sourceMappingURL=stream-writer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-writer.js","sourceRoot":"","sources":["../../src/payload/stream-writer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAE1C,MAAM,YAAY,GAAG,EAAE,GAAG,IAAI,CAAA;AAC9B,8DAA8D;AAC9D,MAAM,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC,CAAA;AAErD,iHAAiH;AACjH,MAAM,YAAY;IAIe;IAHZ,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,YAAY,CAAC,CAAA;IAClD,IAAI,GAAG,CAAC,CAAA;IAEhB,YAA6B,MAAsB;QAAtB,WAAM,GAAN,MAAM,CAAgB;IAAG,CAAC;IAEvD,EAAE,CAAC,KAAa;QACZ,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;QACf,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,KAAK,CAAA;IACpC,CAAC;IAED,GAAG,CAAC,KAAa;QACb,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;IAC3D,CAAC;IAED,GAAG,CAAC,KAAa;QACb,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;IAC7D,CAAC;IAED,GAAG,CAAC,KAAa;QACb,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,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AM,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAA;IACpD,CAAC;IAEO,GAAG,CAAC,KAAiB;QACzB,IAAI,KAAK,CAAC,UAAU,GAAG,YAAY,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;YAC9C,IAAI,CAAC,KAAK,EAAE,CAAA;YACZ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;QAC5B,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;YACjC,IAAI,CAAC,IAAI,IAAI,KAAK,CAAC,UAAU,CAAA;QACjC,CAAC;IACL,CAAC;IAEO,MAAM,CAAC,KAAa;QACxB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;QAC9B,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;QAC9E,IAAI,CAAC,IAAI,IAAI,OAAO,CAAA;QACpB,OAAO,OAAO,CAAA;IAClB,CAAC;IAEO,OAAO,CAAC,KAAa;QACzB,IAAI,KAAK,GAAG,YAAY,GAAG,IAAI,CAAC,IAAI;YAAE,IAAI,CAAC,KAAK,EAAE,CAAA;IACtD,CAAC;CACJ;AAED,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a slice of `text` that starts at `start` and holds at most `units` UTF-16 code units ends. A slice never
|
|
3
|
+
* ends between the two halves of a surrogate pair, which would encode each half as U+FFFD.
|
|
4
|
+
*/
|
|
5
|
+
declare function sliceEnd(text: string, start: number, units: number): number;
|
|
6
|
+
export { sliceEnd };
|
|
7
|
+
//# sourceMappingURL=text-slice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-slice.d.ts","sourceRoot":"","sources":["../../src/payload/text-slice.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,iBAAS,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAGpE;AAMD,OAAO,EAAE,QAAQ,EAAE,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a slice of `text` that starts at `start` and holds at most `units` UTF-16 code units ends. A slice never
|
|
3
|
+
* ends between the two halves of a surrogate pair, which would encode each half as U+FFFD.
|
|
4
|
+
*/
|
|
5
|
+
function sliceEnd(text, start, units) {
|
|
6
|
+
const end = Math.min(start + units, text.length);
|
|
7
|
+
return end < text.length && isHighSurrogate(text.charCodeAt(end - 1)) ? end - 1 : end;
|
|
8
|
+
}
|
|
9
|
+
function isHighSurrogate(unit) {
|
|
10
|
+
return unit >= 0xd800 && unit <= 0xdbff;
|
|
11
|
+
}
|
|
12
|
+
export { sliceEnd };
|
|
13
|
+
//# sourceMappingURL=text-slice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-slice.js","sourceRoot":"","sources":["../../src/payload/text-slice.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,SAAS,QAAQ,CAAC,IAAY,EAAE,KAAa,EAAE,KAAa;IACxD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IAChD,OAAO,GAAG,GAAG,IAAI,CAAC,MAAM,IAAI,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;AACzF,CAAC;AAED,SAAS,eAAe,CAAC,IAAY;IACjC,OAAO,IAAI,IAAI,MAAM,IAAI,IAAI,IAAI,MAAM,CAAA;AAC3C,CAAC;AAED,OAAO,EAAE,QAAQ,EAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "durable-actors",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.1",
|
|
4
4
|
"description": "Provider-neutral actors for sandbox compute",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"@types/json-schema": "^7.0.15",
|
|
105
105
|
"ajv": "^8.20.0",
|
|
106
106
|
"commander": "^14.0.3",
|
|
107
|
-
"durable-actors-observer": "0.
|
|
107
|
+
"durable-actors-observer": "0.18.1",
|
|
108
108
|
"esbuild": "^0.28.2",
|
|
109
109
|
"import-meta-resolve": "^4.2.0",
|
|
110
110
|
"open": "^10.2.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"actor-worker.d.ts","sourceRoot":"","sources":["../../src/host/actor-worker.ts"],"names":[],"mappings":""}
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
-
import { format } from "node:util";
|
|
3
|
-
import { parentPort, workerData } from "node:worker_threads";
|
|
4
|
-
import { Actor, findActorDefinition, registerActorClass } from "../actor/actor.js";
|
|
5
|
-
import { ACTOR_ARTIFACT_VERSION } from "../actor/schema.js";
|
|
6
|
-
import { remoteActorClient, runWithActorClient } from "../client/client.js";
|
|
7
|
-
import { ActorConfigurationError, ActorDefinitionError, errorMessage } from "../errors.js";
|
|
8
|
-
import { ActorRuntime } from "./actor-runtime.js";
|
|
9
|
-
import { requestDatabaseCall } from "./database-bridge.js";
|
|
10
|
-
import { DatabaseProxy } from "./database.js";
|
|
11
|
-
import { HostActorClient } from "./host-actor-client.js";
|
|
12
|
-
import { failedReply } from "./protocol.js";
|
|
13
|
-
const port = parentPort;
|
|
14
|
-
if (port === null)
|
|
15
|
-
throw new Error("actor Worker requires a parent message port");
|
|
16
|
-
let exiting = false;
|
|
17
|
-
let runtime;
|
|
18
|
-
// Bun 1.3 can drain microtasks after process.exit().
|
|
19
|
-
process.once("exit", () => {
|
|
20
|
-
exiting = true;
|
|
21
|
-
});
|
|
22
|
-
let assigned = false;
|
|
23
|
-
const invocation = new AsyncLocalStorage();
|
|
24
|
-
const LOG_LIMIT_BYTES = 256 * 1024;
|
|
25
|
-
captureConsole();
|
|
26
|
-
const loadingConnections = new Map();
|
|
27
|
-
const actorCalls = new Map();
|
|
28
|
-
let nextActorCallId = 0;
|
|
29
|
-
const actorClient = new HostActorClient(callActor, remoteActorClient);
|
|
30
|
-
if (workerData !== undefined && workerData !== null)
|
|
31
|
-
void initialize(workerData);
|
|
32
|
-
else {
|
|
33
|
-
post({ type: "warm" });
|
|
34
|
-
port.once("message", (message) => {
|
|
35
|
-
if (message.type !== "load")
|
|
36
|
-
throw new Error("generic executor requires a code assignment");
|
|
37
|
-
void initialize(message.data);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
async function initialize(data) {
|
|
41
|
-
try {
|
|
42
|
-
if (assigned)
|
|
43
|
-
throw new Error("customer code already assigned");
|
|
44
|
-
assigned = true;
|
|
45
|
-
Object.assign(process.env, data.environment);
|
|
46
|
-
const actorNames = await loadActorEntrypoint(data.moduleUrl);
|
|
47
|
-
port.on("message", (message) => {
|
|
48
|
-
if (message.type === "load")
|
|
49
|
-
throw new Error("customer code already assigned");
|
|
50
|
-
if (message.type === "actor_result") {
|
|
51
|
-
actorCalls.get(message.callId)?.(message.outcome);
|
|
52
|
-
actorCalls.delete(message.callId);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
if (message.type === "socket_connections") {
|
|
56
|
-
const pending = loadingConnections.get(message.messageId);
|
|
57
|
-
loadingConnections.delete(message.messageId);
|
|
58
|
-
if (message.error === undefined)
|
|
59
|
-
pending?.resolve(message.connections);
|
|
60
|
-
else
|
|
61
|
-
pending?.reject(new Error(message.error));
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
const definition = findActorDefinition(message.command.actor.actor_name);
|
|
65
|
-
if (definition === undefined) {
|
|
66
|
-
post({
|
|
67
|
-
type: "reply",
|
|
68
|
-
messageId: message.messageId,
|
|
69
|
-
reply: failedReply("actor_name_not_found", `actor entrypoint ${data.moduleUrl} does not export ${message.command.actor.actor_name}`)
|
|
70
|
-
});
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
runtime ??= new ActorRuntime(definition, allowNextInvocation, new DatabaseProxy(callDatabase), publish, getConnections);
|
|
74
|
-
invocation.run(message.messageId, () => {
|
|
75
|
-
void runWithActorClient(actorClient, () => runtime.handle(message.command)).then(reply => post({ type: "reply", messageId: message.messageId, reply }), error => post({
|
|
76
|
-
type: "reply",
|
|
77
|
-
messageId: message.messageId,
|
|
78
|
-
reply: failedReply("actor_worker_failed", errorMessage(error))
|
|
79
|
-
}));
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
post({ type: "ready", actorNames });
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
post(failedReply("actor_worker_failed", errorMessage(error)));
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
function post(message, transfer) {
|
|
89
|
-
if (!exiting)
|
|
90
|
-
port.postMessage(message, transfer);
|
|
91
|
-
}
|
|
92
|
-
// Console output travels to the host tagged with the invocation that wrote it, instead of stdout.
|
|
93
|
-
function captureConsole() {
|
|
94
|
-
const levels = [
|
|
95
|
-
["debug", "debug"],
|
|
96
|
-
["log", "info"],
|
|
97
|
-
["info", "info"],
|
|
98
|
-
["warn", "warn"],
|
|
99
|
-
["error", "error"]
|
|
100
|
-
];
|
|
101
|
-
for (const [method, level] of levels)
|
|
102
|
-
console[method] = (...args) => post({ type: "log", messageId: invocation.getStore(), level, message: truncateLog(format(...args)) });
|
|
103
|
-
}
|
|
104
|
-
function truncateLog(message) {
|
|
105
|
-
if (message.length * 3 <= LOG_LIMIT_BYTES)
|
|
106
|
-
return message;
|
|
107
|
-
const bytes = Buffer.from(message);
|
|
108
|
-
return bytes.length <= LOG_LIMIT_BYTES ? message : bytes.subarray(0, LOG_LIMIT_BYTES).toString();
|
|
109
|
-
}
|
|
110
|
-
function allowNextInvocation() {
|
|
111
|
-
const messageId = invocation.getStore();
|
|
112
|
-
if (messageId === undefined)
|
|
113
|
-
throw new Error("invocation admission has no active invocation");
|
|
114
|
-
post({ type: "ready_for_invocation", messageId });
|
|
115
|
-
}
|
|
116
|
-
function callDatabase(call) {
|
|
117
|
-
if (exiting)
|
|
118
|
-
throw new Error("actor Worker is exiting");
|
|
119
|
-
const messageId = invocation.getStore();
|
|
120
|
-
return requestDatabaseCall(request => post({ type: "database_call", messageId, call, request }, [request.port]));
|
|
121
|
-
}
|
|
122
|
-
function callActor(call) {
|
|
123
|
-
return new Promise(resolve => {
|
|
124
|
-
const callId = ++nextActorCallId;
|
|
125
|
-
actorCalls.set(callId, resolve);
|
|
126
|
-
post({ type: "actor_call", callId, call });
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
function publish(effects) {
|
|
130
|
-
const messageId = invocation.getStore();
|
|
131
|
-
if (messageId === undefined)
|
|
132
|
-
throw new Error("socket output has no active invocation");
|
|
133
|
-
post({ type: "socket_effects", messageId, effects });
|
|
134
|
-
}
|
|
135
|
-
function getConnections(query) {
|
|
136
|
-
return new Promise((resolve, reject) => {
|
|
137
|
-
const messageId = invocation.getStore();
|
|
138
|
-
if (messageId === undefined)
|
|
139
|
-
throw new Error("connection lookup has no active invocation");
|
|
140
|
-
if (loadingConnections.has(messageId))
|
|
141
|
-
throw new Error("actor connections are already being loaded");
|
|
142
|
-
loadingConnections.set(messageId, { resolve, reject });
|
|
143
|
-
post({ type: "get_connections", messageId, query });
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
async function loadActorEntrypoint(moduleUrl) {
|
|
147
|
-
const artifact = await import(moduleUrl);
|
|
148
|
-
if (artifact.version !== ACTOR_ARTIFACT_VERSION || !Array.isArray(artifact.schemas) || !artifact.actors)
|
|
149
|
-
throw new ActorConfigurationError("invalid actor artifact; redeploy using matching SDK and runtime versions");
|
|
150
|
-
return registerActors(artifact.actors, artifact.schemas);
|
|
151
|
-
}
|
|
152
|
-
function registerActors(actorModule, schemas) {
|
|
153
|
-
const actorNames = [];
|
|
154
|
-
for (const [exportName, value] of Object.entries(actorModule)) {
|
|
155
|
-
if (!isActorClass(value))
|
|
156
|
-
continue;
|
|
157
|
-
if (exportName === "default") {
|
|
158
|
-
throw new ActorDefinitionError("actor entrypoint must use named exports, not a default export");
|
|
159
|
-
}
|
|
160
|
-
if (Object.getPrototypeOf(value.prototype) !== Actor.prototype) {
|
|
161
|
-
throw new ActorDefinitionError(`actor entrypoint export ${exportName} must be a class that directly extends Actor`);
|
|
162
|
-
}
|
|
163
|
-
if (value.name !== exportName) {
|
|
164
|
-
throw new ActorDefinitionError(`actor entrypoint export ${exportName} must have the same class name`);
|
|
165
|
-
}
|
|
166
|
-
const schema = schemas.find(schema => schema.actorName === exportName);
|
|
167
|
-
if (schema === undefined)
|
|
168
|
-
throw new ActorDefinitionError(`actor ${exportName} has no validated schema; restart the actor host`);
|
|
169
|
-
actorNames.push(registerActorClass(value, schema).actorName);
|
|
170
|
-
}
|
|
171
|
-
if (actorNames.length === 0)
|
|
172
|
-
throw new ActorDefinitionError("actor entrypoint has no named actor exports");
|
|
173
|
-
if (actorNames.length !== schemas.length)
|
|
174
|
-
throw new ActorDefinitionError("actor exports do not match validated schemas; restart the actor host");
|
|
175
|
-
actorNames.sort();
|
|
176
|
-
return actorNames;
|
|
177
|
-
}
|
|
178
|
-
function isActorClass(value) {
|
|
179
|
-
return typeof value === "function" && value.prototype instanceof Actor;
|
|
180
|
-
}
|
|
181
|
-
//# sourceMappingURL=actor-worker.js.map
|