@m4ike1/ion-server 0.1.0

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 (67) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/README.md +99 -0
  3. package/dist/connection.d.ts +32 -0
  4. package/dist/connection.d.ts.map +1 -0
  5. package/dist/connection.js +4 -0
  6. package/dist/connection.js.map +1 -0
  7. package/dist/errors.d.ts +25 -0
  8. package/dist/errors.d.ts.map +1 -0
  9. package/dist/errors.js +41 -0
  10. package/dist/errors.js.map +1 -0
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +5 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/listener.d.ts +8 -0
  16. package/dist/listener.d.ts.map +1 -0
  17. package/dist/listener.js +2 -0
  18. package/dist/listener.js.map +1 -0
  19. package/dist/server.d.ts +46 -0
  20. package/dist/server.d.ts.map +1 -0
  21. package/dist/server.js +547 -0
  22. package/dist/server.js.map +1 -0
  23. package/dist/session-router.d.ts +40 -0
  24. package/dist/session-router.d.ts.map +1 -0
  25. package/dist/session-router.js +264 -0
  26. package/dist/session-router.js.map +1 -0
  27. package/dist/testing/client.d.ts +35 -0
  28. package/dist/testing/client.d.ts.map +1 -0
  29. package/dist/testing/client.js +138 -0
  30. package/dist/testing/client.js.map +1 -0
  31. package/dist/testing/host.d.ts +57 -0
  32. package/dist/testing/host.d.ts.map +1 -0
  33. package/dist/testing/host.js +189 -0
  34. package/dist/testing/host.js.map +1 -0
  35. package/dist/testing/index.d.ts +6 -0
  36. package/dist/testing/index.d.ts.map +1 -0
  37. package/dist/testing/index.js +4 -0
  38. package/dist/testing/index.js.map +1 -0
  39. package/dist/testing/server.d.ts +13 -0
  40. package/dist/testing/server.d.ts.map +1 -0
  41. package/dist/testing/server.js +17 -0
  42. package/dist/testing/server.js.map +1 -0
  43. package/dist/transports/unix/address.d.ts +3 -0
  44. package/dist/transports/unix/address.d.ts.map +1 -0
  45. package/dist/transports/unix/address.js +9 -0
  46. package/dist/transports/unix/address.js.map +1 -0
  47. package/dist/transports/unix/index.d.ts +5 -0
  48. package/dist/transports/unix/index.d.ts.map +1 -0
  49. package/dist/transports/unix/index.js +4 -0
  50. package/dist/transports/unix/index.js.map +1 -0
  51. package/dist/transports/unix/listener.d.ts +24 -0
  52. package/dist/transports/unix/listener.d.ts.map +1 -0
  53. package/dist/transports/unix/listener.js +421 -0
  54. package/dist/transports/unix/listener.js.map +1 -0
  55. package/dist/transports/unix/preset.d.ts +7 -0
  56. package/dist/transports/unix/preset.d.ts.map +1 -0
  57. package/dist/transports/unix/preset.js +22 -0
  58. package/dist/transports/unix/preset.js.map +1 -0
  59. package/dist/transports/unix/types.d.ts +15 -0
  60. package/dist/transports/unix/types.d.ts.map +1 -0
  61. package/dist/transports/unix/types.js +2 -0
  62. package/dist/transports/unix/types.js.map +1 -0
  63. package/dist/types.d.ts +49 -0
  64. package/dist/types.d.ts.map +1 -0
  65. package/dist/types.js +2 -0
  66. package/dist/types.js.map +1 -0
  67. package/package.json +58 -0
@@ -0,0 +1,57 @@
1
+ import type { JsonValue, ServiceCall } from "@m4ike1/chord";
2
+ import type { Context, Session, SessionMetadata } from "@m4ike1/ion-agent-core";
3
+ import { MemorySessionRepo } from "@m4ike1/ion-agent-core";
4
+ import type { RoutedServerServiceHost, RoutedSessionHandle, ServerHost } from "../types.ts";
5
+ export declare class Deferred<T> {
6
+ readonly promise: Promise<T>;
7
+ private resolvePromise;
8
+ constructor();
9
+ resolve(value: T): void;
10
+ }
11
+ interface OpenGate {
12
+ entered: Deferred<void>;
13
+ release: Deferred<void>;
14
+ }
15
+ export declare class TestHarness {
16
+ #private;
17
+ readonly session: Session;
18
+ readonly closed: Deferred<void>;
19
+ readonly terminated: Promise<Error | undefined>;
20
+ attachedClients: number;
21
+ attachmentReleaseCount: number;
22
+ closeCount: number;
23
+ readonly serviceCalls: ServiceCall[];
24
+ failAttachmentRelease?: Error;
25
+ failClose?: Error;
26
+ nextServiceError?: Error;
27
+ nextServiceResult: JsonValue | undefined;
28
+ private nextCloseGate?;
29
+ private nextServiceGate?;
30
+ constructor(session: Session);
31
+ attachClient(_context: Context): {
32
+ invokeService: TestHarness["invokeService"];
33
+ release(context: Context): void;
34
+ };
35
+ invokeService(call: ServiceCall): Promise<JsonValue | undefined>;
36
+ close(context: Context): Promise<void>;
37
+ terminate(error: Error): Promise<void>;
38
+ gateNextClose(): OpenGate;
39
+ gateNextServiceCall(): OpenGate;
40
+ }
41
+ export declare function createTestServerServices(): RoutedServerServiceHost;
42
+ export declare class TestServerHost implements ServerHost {
43
+ readonly serverServices: RoutedServerServiceHost;
44
+ readonly repo: MemorySessionRepo;
45
+ readonly harnesses: Map<string, TestHarness[]>;
46
+ openSessionCount: number;
47
+ nextOpenSessionError?: Error;
48
+ nextHarnessCloseError?: Error;
49
+ private nextOpenSessionGate?;
50
+ resolveSession(sessionId: string, context: Context): Promise<SessionMetadata>;
51
+ openSession(metadata: SessionMetadata, context: Context): Promise<RoutedSessionHandle>;
52
+ seed(id?: string, parentSessionId?: string): Promise<SessionMetadata>;
53
+ gateNextOpenSession(): OpenGate;
54
+ latestHarness(id: string): TestHarness;
55
+ }
56
+ export {};
57
+ //# sourceMappingURL=host.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../src/testing/host.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAsB,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE/E,OAAO,KAAK,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE5F,qBAAa,QAAQ,CAAC,CAAC;IACtB,QAAQ,CAAC,O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9B;CACD;AAED,wBAAgB,wBAAwB,IAAI,uBAAuB,CA8BlE;AAED,qBAAa,cAAe,YAAW,UAAU;IAChD,QAAQ,CAAC,cAAc,0BAA8B;IACrD,QAAQ,CAAC,IAAI,oBAA2C;IACxD,QAAQ,CAAC,SAAS,6BAAoC;IACtD,gBAAgB,SAAK;IACrB,oBAAoB,CAAC,EAAE,KAAK,CAAC;IAC7B,qBAAqB,CAAC,EAAE,KAAK,CAAC;IAC9B,OAAO,CAAC,mBAAmB,CAAC,CAAW;IAEjC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,eAAe,CAAC,CAKlF;IAEK,WAAW,CAAC,QAAQ,EAAE,eAAe,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,mBAAmB,CAAC,CA4B3F;IAEK,IAAI,CAAC,EAAE,SAAc,EAAE,eAAe,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAK/E;IAED,mBAAmB,IAAI,QAAQ,CAI9B;IAED,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,WAAW,CAIrC;CACD","sourcesContent":["import type { JsonValue, ServiceCall } from \"@m4ike1/chord\";\nimport type { Context, Session, SessionMetadata } from \"@m4ike1/ion-agent-core\";\nimport { BACKGROUND_CONTEXT, MemorySessionRepo } from \"@m4ike1/ion-agent-core\";\nimport { SessionAmbiguousError, SessionNotFoundError } from \"../errors.ts\";\nimport type { RoutedServerServiceHost, RoutedSessionHandle, ServerHost } from \"../types.ts\";\n\nexport class Deferred<T> {\n\treadonly promise: Promise<T>;\n\tprivate resolvePromise!: (value: T) => void;\n\n\tconstructor() {\n\t\tthis.promise = new Promise<T>((resolve) => {\n\t\t\tthis.resolvePromise = resolve;\n\t\t});\n\t}\n\n\tresolve(value: T): void {\n\t\tthis.resolvePromise(value);\n\t}\n}\n\ninterface OpenGate {\n\tentered: Deferred<void>;\n\trelease: Deferred<void>;\n}\n\nexport class TestHarness {\n\treadonly session: Session;\n\treadonly closed = new Deferred<void>();\n\treadonly #termination = new Deferred<Error | undefined>();\n\treadonly terminated = this.#termination.promise;\n\tattachedClients = 0;\n\tattachmentReleaseCount = 0;\n\tcloseCount = 0;\n\treadonly serviceCalls: ServiceCall[] = [];\n\tfailAttachmentRelease?: Error;\n\tfailClose?: Error;\n\tnextServiceError?: Error;\n\tnextServiceResult: JsonValue | undefined = { ok: true };\n\tprivate nextCloseGate?: OpenGate;\n\tprivate nextServiceGate?: OpenGate;\n\n\tconstructor(session: Session) {\n\t\tthis.session = session;\n\t}\n\n\tattachClient(_context: Context): {\n\t\tinvokeService: TestHarness[\"invokeService\"];\n\t\trelease(context: Context): void;\n\t} {\n\t\tthis.attachedClients += 1;\n\t\tlet released = false;\n\t\treturn {\n\t\t\tinvokeService: (call) => this.invokeService(call),\n\t\t\trelease: (_context) => {\n\t\t\t\tif (released) return;\n\t\t\t\tthis.attachmentReleaseCount += 1;\n\t\t\t\tif (this.failAttachmentRelease) throw this.failAttachmentRelease;\n\t\t\t\treleased = true;\n\t\t\t\tthis.attachedClients -= 1;\n\t\t\t},\n\t\t};\n\t}\n\n\tasync invokeService(call: ServiceCall): Promise<JsonValue | undefined> {\n\t\tthis.serviceCalls.push(call);\n\t\tif (this.nextServiceError) {\n\t\t\tconst error = this.nextServiceError;\n\t\t\tthis.nextServiceError = undefined;\n\t\t\tthrow error;\n\t\t}\n\t\tconst gate = this.nextServiceGate;\n\t\tif (gate) {\n\t\t\tthis.nextServiceGate = undefined;\n\t\t\tgate.entered.resolve(undefined);\n\t\t\tawait gate.release.promise;\n\t\t}\n\t\tconst result = this.nextServiceResult;\n\t\tthis.nextServiceResult = { ok: true };\n\t\treturn result;\n\t}\n\n\tasync close(context: Context): Promise<void> {\n\t\tthis.closeCount += 1;\n\t\tconst gate = this.nextCloseGate;\n\t\tif (gate) {\n\t\t\tthis.nextCloseGate = undefined;\n\t\t\tgate.entered.resolve(undefined);\n\t\t\tawait gate.release.promise;\n\t\t}\n\t\tif (this.failClose) {\n\t\t\tconst error = this.failClose;\n\t\t\tthis.failClose = undefined;\n\t\t\tthrow error;\n\t\t}\n\t\tawait this.session.close(context);\n\t\tthis.closed.resolve(undefined);\n\t\tthis.#termination.resolve(undefined);\n\t}\n\n\tasync terminate(error: Error): Promise<void> {\n\t\tawait this.session.close(BACKGROUND_CONTEXT);\n\t\tthis.#termination.resolve(error);\n\t}\n\n\tgateNextClose(): OpenGate {\n\t\tconst gate = { entered: new Deferred<void>(), release: new Deferred<void>() };\n\t\tthis.nextCloseGate = gate;\n\t\treturn gate;\n\t}\n\n\tgateNextServiceCall(): OpenGate {\n\t\tconst gate = { entered: new Deferred<void>(), release: new Deferred<void>() };\n\t\tthis.nextServiceGate = gate;\n\t\treturn gate;\n\t}\n}\n\nexport function createTestServerServices(): RoutedServerServiceHost {\n\treturn {\n\t\tattachClient(presentation) {\n\t\t\treturn {\n\t\t\t\tasync invokeService(call, _publish, context) {\n\t\t\t\t\tif (\n\t\t\t\t\t\tcall.instance === undefined &&\n\t\t\t\t\t\tcall.serviceId === \"ion.session-management\" &&\n\t\t\t\t\t\tcall.member === \"attach\" &&\n\t\t\t\t\t\tcall.args.length === 1 &&\n\t\t\t\t\t\ttypeof call.args[0] === \"string\"\n\t\t\t\t\t) {\n\t\t\t\t\t\tawait presentation.attachSession(call.args[0], context);\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t\tif (\n\t\t\t\t\t\tcall.instance === undefined &&\n\t\t\t\t\t\tcall.serviceId === \"ion.session-management\" &&\n\t\t\t\t\t\tcall.member === \"detach\" &&\n\t\t\t\t\t\tcall.args.length === 0\n\t\t\t\t\t) {\n\t\t\t\t\t\tawait presentation.detachSession(context);\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t\tthrow new Error(`Unsupported test server service ${call.serviceId}.${call.member}`);\n\t\t\t\t},\n\t\t\t\trelease() {},\n\t\t\t};\n\t\t},\n\t};\n}\n\nexport class TestServerHost implements ServerHost {\n\treadonly serverServices = createTestServerServices();\n\treadonly repo = new MemorySessionRepo({ now: () => 1 });\n\treadonly harnesses = new Map<string, TestHarness[]>();\n\topenSessionCount = 0;\n\tnextOpenSessionError?: Error;\n\tnextHarnessCloseError?: Error;\n\tprivate nextOpenSessionGate?: OpenGate;\n\n\tasync resolveSession(sessionId: string, context: Context): Promise<SessionMetadata> {\n\t\tconst matches = (await this.repo.list(undefined, context)).filter(({ id }) => id === sessionId);\n\t\tif (matches.length === 0) throw new SessionNotFoundError(`Unknown session: ${sessionId}`);\n\t\tif (matches.length > 1) throw new SessionAmbiguousError();\n\t\treturn matches[0]!;\n\t}\n\n\tasync openSession(metadata: SessionMetadata, context: Context): Promise<RoutedSessionHandle> {\n\t\tthis.openSessionCount += 1;\n\t\tconst gate = this.nextOpenSessionGate;\n\t\tif (gate) {\n\t\t\tthis.nextOpenSessionGate = undefined;\n\t\t\tgate.entered.resolve(undefined);\n\t\t\tawait gate.release.promise;\n\t\t}\n\t\tconst session = await this.repo.open(metadata, context);\n\t\ttry {\n\t\t\tif (this.nextOpenSessionError) {\n\t\t\t\tconst error = this.nextOpenSessionError;\n\t\t\t\tthis.nextOpenSessionError = undefined;\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst harness = new TestHarness(session);\n\t\t\tif (this.nextHarnessCloseError) {\n\t\t\t\tharness.failClose = this.nextHarnessCloseError;\n\t\t\t\tthis.nextHarnessCloseError = undefined;\n\t\t\t}\n\t\t\tconst harnesses = this.harnesses.get(metadata.id) ?? [];\n\t\t\tharnesses.push(harness);\n\t\t\tthis.harnesses.set(metadata.id, harnesses);\n\t\t\treturn harness;\n\t\t} catch (error) {\n\t\t\tawait session.close(context);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tasync seed(id = \"session-1\", parentSessionId?: string): Promise<SessionMetadata> {\n\t\tconst session = await this.repo.create({ id, parentSessionId }, BACKGROUND_CONTEXT);\n\t\tconst metadata = session.metadata;\n\t\tawait session.close(BACKGROUND_CONTEXT);\n\t\treturn metadata;\n\t}\n\n\tgateNextOpenSession(): OpenGate {\n\t\tconst gate = { entered: new Deferred<void>(), release: new Deferred<void>() };\n\t\tthis.nextOpenSessionGate = gate;\n\t\treturn gate;\n\t}\n\n\tlatestHarness(id: string): TestHarness {\n\t\tconst harnesses = this.harnesses.get(id);\n\t\tif (!harnesses?.length) throw new Error(`No harness for ${id}`);\n\t\treturn harnesses.at(-1)!;\n\t}\n}\n"]}
@@ -0,0 +1,189 @@
1
+ import { BACKGROUND_CONTEXT, MemorySessionRepo } from "@m4ike1/ion-agent-core";
2
+ import { SessionAmbiguousError, SessionNotFoundError } from "../errors.js";
3
+ export class Deferred {
4
+ promise;
5
+ resolvePromise;
6
+ constructor() {
7
+ this.promise = new Promise((resolve) => {
8
+ this.resolvePromise = resolve;
9
+ });
10
+ }
11
+ resolve(value) {
12
+ this.resolvePromise(value);
13
+ }
14
+ }
15
+ export class TestHarness {
16
+ session;
17
+ closed = new Deferred();
18
+ #termination = new Deferred();
19
+ terminated = this.#termination.promise;
20
+ attachedClients = 0;
21
+ attachmentReleaseCount = 0;
22
+ closeCount = 0;
23
+ serviceCalls = [];
24
+ failAttachmentRelease;
25
+ failClose;
26
+ nextServiceError;
27
+ nextServiceResult = { ok: true };
28
+ nextCloseGate;
29
+ nextServiceGate;
30
+ constructor(session) {
31
+ this.session = session;
32
+ }
33
+ attachClient(_context) {
34
+ this.attachedClients += 1;
35
+ let released = false;
36
+ return {
37
+ invokeService: (call) => this.invokeService(call),
38
+ release: (_context) => {
39
+ if (released)
40
+ return;
41
+ this.attachmentReleaseCount += 1;
42
+ if (this.failAttachmentRelease)
43
+ throw this.failAttachmentRelease;
44
+ released = true;
45
+ this.attachedClients -= 1;
46
+ },
47
+ };
48
+ }
49
+ async invokeService(call) {
50
+ this.serviceCalls.push(call);
51
+ if (this.nextServiceError) {
52
+ const error = this.nextServiceError;
53
+ this.nextServiceError = undefined;
54
+ throw error;
55
+ }
56
+ const gate = this.nextServiceGate;
57
+ if (gate) {
58
+ this.nextServiceGate = undefined;
59
+ gate.entered.resolve(undefined);
60
+ await gate.release.promise;
61
+ }
62
+ const result = this.nextServiceResult;
63
+ this.nextServiceResult = { ok: true };
64
+ return result;
65
+ }
66
+ async close(context) {
67
+ this.closeCount += 1;
68
+ const gate = this.nextCloseGate;
69
+ if (gate) {
70
+ this.nextCloseGate = undefined;
71
+ gate.entered.resolve(undefined);
72
+ await gate.release.promise;
73
+ }
74
+ if (this.failClose) {
75
+ const error = this.failClose;
76
+ this.failClose = undefined;
77
+ throw error;
78
+ }
79
+ await this.session.close(context);
80
+ this.closed.resolve(undefined);
81
+ this.#termination.resolve(undefined);
82
+ }
83
+ async terminate(error) {
84
+ await this.session.close(BACKGROUND_CONTEXT);
85
+ this.#termination.resolve(error);
86
+ }
87
+ gateNextClose() {
88
+ const gate = { entered: new Deferred(), release: new Deferred() };
89
+ this.nextCloseGate = gate;
90
+ return gate;
91
+ }
92
+ gateNextServiceCall() {
93
+ const gate = { entered: new Deferred(), release: new Deferred() };
94
+ this.nextServiceGate = gate;
95
+ return gate;
96
+ }
97
+ }
98
+ export function createTestServerServices() {
99
+ return {
100
+ attachClient(presentation) {
101
+ return {
102
+ async invokeService(call, _publish, context) {
103
+ if (call.instance === undefined &&
104
+ call.serviceId === "ion.session-management" &&
105
+ call.member === "attach" &&
106
+ call.args.length === 1 &&
107
+ typeof call.args[0] === "string") {
108
+ await presentation.attachSession(call.args[0], context);
109
+ return null;
110
+ }
111
+ if (call.instance === undefined &&
112
+ call.serviceId === "ion.session-management" &&
113
+ call.member === "detach" &&
114
+ call.args.length === 0) {
115
+ await presentation.detachSession(context);
116
+ return null;
117
+ }
118
+ throw new Error(`Unsupported test server service ${call.serviceId}.${call.member}`);
119
+ },
120
+ release() { },
121
+ };
122
+ },
123
+ };
124
+ }
125
+ export class TestServerHost {
126
+ serverServices = createTestServerServices();
127
+ repo = new MemorySessionRepo({ now: () => 1 });
128
+ harnesses = new Map();
129
+ openSessionCount = 0;
130
+ nextOpenSessionError;
131
+ nextHarnessCloseError;
132
+ nextOpenSessionGate;
133
+ async resolveSession(sessionId, context) {
134
+ const matches = (await this.repo.list(undefined, context)).filter(({ id }) => id === sessionId);
135
+ if (matches.length === 0)
136
+ throw new SessionNotFoundError(`Unknown session: ${sessionId}`);
137
+ if (matches.length > 1)
138
+ throw new SessionAmbiguousError();
139
+ return matches[0];
140
+ }
141
+ async openSession(metadata, context) {
142
+ this.openSessionCount += 1;
143
+ const gate = this.nextOpenSessionGate;
144
+ if (gate) {
145
+ this.nextOpenSessionGate = undefined;
146
+ gate.entered.resolve(undefined);
147
+ await gate.release.promise;
148
+ }
149
+ const session = await this.repo.open(metadata, context);
150
+ try {
151
+ if (this.nextOpenSessionError) {
152
+ const error = this.nextOpenSessionError;
153
+ this.nextOpenSessionError = undefined;
154
+ throw error;
155
+ }
156
+ const harness = new TestHarness(session);
157
+ if (this.nextHarnessCloseError) {
158
+ harness.failClose = this.nextHarnessCloseError;
159
+ this.nextHarnessCloseError = undefined;
160
+ }
161
+ const harnesses = this.harnesses.get(metadata.id) ?? [];
162
+ harnesses.push(harness);
163
+ this.harnesses.set(metadata.id, harnesses);
164
+ return harness;
165
+ }
166
+ catch (error) {
167
+ await session.close(context);
168
+ throw error;
169
+ }
170
+ }
171
+ async seed(id = "session-1", parentSessionId) {
172
+ const session = await this.repo.create({ id, parentSessionId }, BACKGROUND_CONTEXT);
173
+ const metadata = session.metadata;
174
+ await session.close(BACKGROUND_CONTEXT);
175
+ return metadata;
176
+ }
177
+ gateNextOpenSession() {
178
+ const gate = { entered: new Deferred(), release: new Deferred() };
179
+ this.nextOpenSessionGate = gate;
180
+ return gate;
181
+ }
182
+ latestHarness(id) {
183
+ const harnesses = this.harnesses.get(id);
184
+ if (!harnesses?.length)
185
+ throw new Error(`No harness for ${id}`);
186
+ return harnesses.at(-1);
187
+ }
188
+ }
189
+ //# sourceMappingURL=host.js.map
@@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,EAAE,CAAC,CAAC;QAChE,OAAO,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,CAAE,CAAC;IAAA,CACzB;CACD","sourcesContent":["import type { JsonValue, ServiceCall } from \"@m4ike1/chord\";\nimport type { Context, Session, SessionMetadata } from \"@m4ike1/ion-agent-core\";\nimport { BACKGROUND_CONTEXT, MemorySessionRepo } from \"@m4ike1/ion-agent-core\";\nimport { SessionAmbiguousError, SessionNotFoundError } from \"../errors.ts\";\nimport type { RoutedServerServiceHost, RoutedSessionHandle, ServerHost } from \"../types.ts\";\n\nexport class Deferred<T> {\n\treadonly promise: Promise<T>;\n\tprivate resolvePromise!: (value: T) => void;\n\n\tconstructor() {\n\t\tthis.promise = new Promise<T>((resolve) => {\n\t\t\tthis.resolvePromise = resolve;\n\t\t});\n\t}\n\n\tresolve(value: T): void {\n\t\tthis.resolvePromise(value);\n\t}\n}\n\ninterface OpenGate {\n\tentered: Deferred<void>;\n\trelease: Deferred<void>;\n}\n\nexport class TestHarness {\n\treadonly session: Session;\n\treadonly closed = new Deferred<void>();\n\treadonly #termination = new Deferred<Error | undefined>();\n\treadonly terminated = this.#termination.promise;\n\tattachedClients = 0;\n\tattachmentReleaseCount = 0;\n\tcloseCount = 0;\n\treadonly serviceCalls: ServiceCall[] = [];\n\tfailAttachmentRelease?: Error;\n\tfailClose?: Error;\n\tnextServiceError?: Error;\n\tnextServiceResult: JsonValue | undefined = { ok: true };\n\tprivate nextCloseGate?: OpenGate;\n\tprivate nextServiceGate?: OpenGate;\n\n\tconstructor(session: Session) {\n\t\tthis.session = session;\n\t}\n\n\tattachClient(_context: Context): {\n\t\tinvokeService: TestHarness[\"invokeService\"];\n\t\trelease(context: Context): void;\n\t} {\n\t\tthis.attachedClients += 1;\n\t\tlet released = false;\n\t\treturn {\n\t\t\tinvokeService: (call) => this.invokeService(call),\n\t\t\trelease: (_context) => {\n\t\t\t\tif (released) return;\n\t\t\t\tthis.attachmentReleaseCount += 1;\n\t\t\t\tif (this.failAttachmentRelease) throw this.failAttachmentRelease;\n\t\t\t\treleased = true;\n\t\t\t\tthis.attachedClients -= 1;\n\t\t\t},\n\t\t};\n\t}\n\n\tasync invokeService(call: ServiceCall): Promise<JsonValue | undefined> {\n\t\tthis.serviceCalls.push(call);\n\t\tif (this.nextServiceError) {\n\t\t\tconst error = this.nextServiceError;\n\t\t\tthis.nextServiceError = undefined;\n\t\t\tthrow error;\n\t\t}\n\t\tconst gate = this.nextServiceGate;\n\t\tif (gate) {\n\t\t\tthis.nextServiceGate = undefined;\n\t\t\tgate.entered.resolve(undefined);\n\t\t\tawait gate.release.promise;\n\t\t}\n\t\tconst result = this.nextServiceResult;\n\t\tthis.nextServiceResult = { ok: true };\n\t\treturn result;\n\t}\n\n\tasync close(context: Context): Promise<void> {\n\t\tthis.closeCount += 1;\n\t\tconst gate = this.nextCloseGate;\n\t\tif (gate) {\n\t\t\tthis.nextCloseGate = undefined;\n\t\t\tgate.entered.resolve(undefined);\n\t\t\tawait gate.release.promise;\n\t\t}\n\t\tif (this.failClose) {\n\t\t\tconst error = this.failClose;\n\t\t\tthis.failClose = undefined;\n\t\t\tthrow error;\n\t\t}\n\t\tawait this.session.close(context);\n\t\tthis.closed.resolve(undefined);\n\t\tthis.#termination.resolve(undefined);\n\t}\n\n\tasync terminate(error: Error): Promise<void> {\n\t\tawait this.session.close(BACKGROUND_CONTEXT);\n\t\tthis.#termination.resolve(error);\n\t}\n\n\tgateNextClose(): OpenGate {\n\t\tconst gate = { entered: new Deferred<void>(), release: new Deferred<void>() };\n\t\tthis.nextCloseGate = gate;\n\t\treturn gate;\n\t}\n\n\tgateNextServiceCall(): OpenGate {\n\t\tconst gate = { entered: new Deferred<void>(), release: new Deferred<void>() };\n\t\tthis.nextServiceGate = gate;\n\t\treturn gate;\n\t}\n}\n\nexport function createTestServerServices(): RoutedServerServiceHost {\n\treturn {\n\t\tattachClient(presentation) {\n\t\t\treturn {\n\t\t\t\tasync invokeService(call, _publish, context) {\n\t\t\t\t\tif (\n\t\t\t\t\t\tcall.instance === undefined &&\n\t\t\t\t\t\tcall.serviceId === \"ion.session-management\" &&\n\t\t\t\t\t\tcall.member === \"attach\" &&\n\t\t\t\t\t\tcall.args.length === 1 &&\n\t\t\t\t\t\ttypeof call.args[0] === \"string\"\n\t\t\t\t\t) {\n\t\t\t\t\t\tawait presentation.attachSession(call.args[0], context);\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t\tif (\n\t\t\t\t\t\tcall.instance === undefined &&\n\t\t\t\t\t\tcall.serviceId === \"ion.session-management\" &&\n\t\t\t\t\t\tcall.member === \"detach\" &&\n\t\t\t\t\t\tcall.args.length === 0\n\t\t\t\t\t) {\n\t\t\t\t\t\tawait presentation.detachSession(context);\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t\tthrow new Error(`Unsupported test server service ${call.serviceId}.${call.member}`);\n\t\t\t\t},\n\t\t\t\trelease() {},\n\t\t\t};\n\t\t},\n\t};\n}\n\nexport class TestServerHost implements ServerHost {\n\treadonly serverServices = createTestServerServices();\n\treadonly repo = new MemorySessionRepo({ now: () => 1 });\n\treadonly harnesses = new Map<string, TestHarness[]>();\n\topenSessionCount = 0;\n\tnextOpenSessionError?: Error;\n\tnextHarnessCloseError?: Error;\n\tprivate nextOpenSessionGate?: OpenGate;\n\n\tasync resolveSession(sessionId: string, context: Context): Promise<SessionMetadata> {\n\t\tconst matches = (await this.repo.list(undefined, context)).filter(({ id }) => id === sessionId);\n\t\tif (matches.length === 0) throw new SessionNotFoundError(`Unknown session: ${sessionId}`);\n\t\tif (matches.length > 1) throw new SessionAmbiguousError();\n\t\treturn matches[0]!;\n\t}\n\n\tasync openSession(metadata: SessionMetadata, context: Context): Promise<RoutedSessionHandle> {\n\t\tthis.openSessionCount += 1;\n\t\tconst gate = this.nextOpenSessionGate;\n\t\tif (gate) {\n\t\t\tthis.nextOpenSessionGate = undefined;\n\t\t\tgate.entered.resolve(undefined);\n\t\t\tawait gate.release.promise;\n\t\t}\n\t\tconst session = await this.repo.open(metadata, context);\n\t\ttry {\n\t\t\tif (this.nextOpenSessionError) {\n\t\t\t\tconst error = this.nextOpenSessionError;\n\t\t\t\tthis.nextOpenSessionError = undefined;\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst harness = new TestHarness(session);\n\t\t\tif (this.nextHarnessCloseError) {\n\t\t\t\tharness.failClose = this.nextHarnessCloseError;\n\t\t\t\tthis.nextHarnessCloseError = undefined;\n\t\t\t}\n\t\t\tconst harnesses = this.harnesses.get(metadata.id) ?? [];\n\t\t\tharnesses.push(harness);\n\t\t\tthis.harnesses.set(metadata.id, harnesses);\n\t\t\treturn harness;\n\t\t} catch (error) {\n\t\t\tawait session.close(context);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tasync seed(id = \"session-1\", parentSessionId?: string): Promise<SessionMetadata> {\n\t\tconst session = await this.repo.create({ id, parentSessionId }, BACKGROUND_CONTEXT);\n\t\tconst metadata = session.metadata;\n\t\tawait session.close(BACKGROUND_CONTEXT);\n\t\treturn metadata;\n\t}\n\n\tgateNextOpenSession(): OpenGate {\n\t\tconst gate = { entered: new Deferred<void>(), release: new Deferred<void>() };\n\t\tthis.nextOpenSessionGate = gate;\n\t\treturn gate;\n\t}\n\n\tlatestHarness(id: string): TestHarness {\n\t\tconst harnesses = this.harnesses.get(id);\n\t\tif (!harnesses?.length) throw new Error(`No harness for ${id}`);\n\t\treturn harnesses.at(-1)!;\n\t}\n}\n"]}
@@ -0,0 +1,6 @@
1
+ export type { WireChannel } from "./client.ts";
2
+ export { connectUnixTestClient, ProtocolTestClient } from "./client.ts";
3
+ export { createTestServerServices, Deferred, TestHarness, TestServerHost } from "./host.ts";
4
+ export type { TestServer, TestServerOptions } from "./server.ts";
5
+ export { createTestServer } from "./server.ts";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,wBAAwB,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC5F,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC","sourcesContent":["export type { WireChannel } from \"./client.ts\";\nexport { connectUnixTestClient, ProtocolTestClient } from \"./client.ts\";\nexport { createTestServerServices, Deferred, TestHarness, TestServerHost } from \"./host.ts\";\nexport type { TestServer, TestServerOptions } from \"./server.ts\";\nexport { createTestServer } from \"./server.ts\";\n"]}
@@ -0,0 +1,4 @@
1
+ export { connectUnixTestClient, ProtocolTestClient } from "./client.js";
2
+ export { createTestServerServices, Deferred, TestHarness, TestServerHost } from "./host.js";
3
+ export { createTestServer } from "./server.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,wBAAwB,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE5F,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC","sourcesContent":["export type { WireChannel } from \"./client.ts\";\nexport { connectUnixTestClient, ProtocolTestClient } from \"./client.ts\";\nexport { createTestServerServices, Deferred, TestHarness, TestServerHost } from \"./host.ts\";\nexport type { TestServer, TestServerOptions } from \"./server.ts\";\nexport { createTestServer } from \"./server.ts\";\n"]}
@@ -0,0 +1,13 @@
1
+ import { Server } from "../server.ts";
2
+ import type { ServerHost, ServerOptions } from "../types.ts";
3
+ export interface TestServerOptions extends Omit<ServerOptions, "serverId"> {
4
+ host?: ServerHost;
5
+ serverId?: string;
6
+ }
7
+ export interface TestServer {
8
+ server: Server;
9
+ host: ServerHost;
10
+ }
11
+ /** Create an unstarted Server with deterministic defaults for transport conformance tests. */
12
+ export declare function createTestServer(options: TestServerOptions): TestServer;
13
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/testing/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG7D,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,aAAa,EAAE,UAAU,CAAC;IACzE,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,UAAU;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,UAAU,CAAC;CACjB;AAED,8FAA8F;AAC9F,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,GAAG,UAAU,CAYvE","sourcesContent":["import { Server } from \"../server.ts\";\nimport type { ServerHost, ServerOptions } from \"../types.ts\";\nimport { TestServerHost } from \"./host.ts\";\n\nexport interface TestServerOptions extends Omit<ServerOptions, \"serverId\"> {\n\thost?: ServerHost;\n\tserverId?: string;\n}\n\nexport interface TestServer {\n\tserver: Server;\n\thost: ServerHost;\n}\n\n/** Create an unstarted Server with deterministic defaults for transport conformance tests. */\nexport function createTestServer(options: TestServerOptions): TestServer {\n\tconst host = options.host ?? new TestServerHost();\n\treturn {\n\t\tserver: new Server(host, {\n\t\t\tlisteners: options.listeners,\n\t\t\tmaxFrameLength: options.maxFrameLength,\n\t\t\thandshakeTimeoutMs: options.handshakeTimeoutMs,\n\t\t\tserverId: options.serverId ?? \"00000000-0000-4000-8000-000000000001\",\n\t\t\tonError: options.onError,\n\t\t}),\n\t\thost,\n\t};\n}\n"]}
@@ -0,0 +1,17 @@
1
+ import { Server } from "../server.js";
2
+ import { TestServerHost } from "./host.js";
3
+ /** Create an unstarted Server with deterministic defaults for transport conformance tests. */
4
+ export function createTestServer(options) {
5
+ const host = options.host ?? new TestServerHost();
6
+ return {
7
+ server: new Server(host, {
8
+ listeners: options.listeners,
9
+ maxFrameLength: options.maxFrameLength,
10
+ handshakeTimeoutMs: options.handshakeTimeoutMs,
11
+ serverId: options.serverId ?? "00000000-0000-4000-8000-000000000001",
12
+ onError: options.onError,
13
+ }),
14
+ host,
15
+ };
16
+ }
17
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/testing/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAY3C,8FAA8F;AAC9F,MAAM,UAAU,gBAAgB,CAAC,OAA0B,EAAc;IACxE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,cAAc,EAAE,CAAC;IAClD,OAAO;QACN,MAAM,EAAE,IAAI,MAAM,CAAC,IAAI,EAAE;YACxB,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;YAC9C,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,sCAAsC;YACpE,OAAO,EAAE,OAAO,CAAC,OAAO;SACxB,CAAC;QACF,IAAI;KACJ,CAAC;AAAA,CACF","sourcesContent":["import { Server } from \"../server.ts\";\nimport type { ServerHost, ServerOptions } from \"../types.ts\";\nimport { TestServerHost } from \"./host.ts\";\n\nexport interface TestServerOptions extends Omit<ServerOptions, \"serverId\"> {\n\thost?: ServerHost;\n\tserverId?: string;\n}\n\nexport interface TestServer {\n\tserver: Server;\n\thost: ServerHost;\n}\n\n/** Create an unstarted Server with deterministic defaults for transport conformance tests. */\nexport function createTestServer(options: TestServerOptions): TestServer {\n\tconst host = options.host ?? new TestServerHost();\n\treturn {\n\t\tserver: new Server(host, {\n\t\t\tlisteners: options.listeners,\n\t\t\tmaxFrameLength: options.maxFrameLength,\n\t\t\thandshakeTimeoutMs: options.handshakeTimeoutMs,\n\t\t\tserverId: options.serverId ?? \"00000000-0000-4000-8000-000000000001\",\n\t\t\tonError: options.onError,\n\t\t}),\n\t\thost,\n\t};\n}\n"]}
@@ -0,0 +1,3 @@
1
+ /** Derive the local Unix socket path for one logical server identity. */
2
+ export declare function getUnixSocketPath(serverId: string, serverDirectory: string): string;
3
+ //# sourceMappingURL=address.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../../src/transports/unix/address.ts"],"names":[],"mappings":"AAEA,yEAAyE;AACzE,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,MAAM,CAKnF","sourcesContent":["import { join } from \"node:path\";\n\n/** Derive the local Unix socket path for one logical server identity. */\nexport function getUnixSocketPath(serverId: string, serverDirectory: string): string {\n\tif (!/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(serverId)) {\n\t\tthrow new TypeError(\"Unix serverId must be a canonical lowercase UUIDv4\");\n\t}\n\treturn join(serverDirectory, `${serverId}.sock`);\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import { join } from "node:path";
2
+ /** Derive the local Unix socket path for one logical server identity. */
3
+ export function getUnixSocketPath(serverId, serverDirectory) {
4
+ if (!/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(serverId)) {
5
+ throw new TypeError("Unix serverId must be a canonical lowercase UUIDv4");
6
+ }
7
+ return join(serverDirectory, `${serverId}.sock`);
8
+ }
9
+ //# sourceMappingURL=address.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.js","sourceRoot":"","sources":["../../../src/transports/unix/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,yEAAyE;AACzE,MAAM,UAAU,iBAAiB,CAAC,QAAgB,EAAE,eAAuB,EAAU;IACpF,IAAI,CAAC,uEAAuE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC7F,MAAM,IAAI,SAAS,CAAC,oDAAoD,CAAC,CAAC;IAC3E,CAAC;IACD,OAAO,IAAI,CAAC,eAAe,EAAE,GAAG,QAAQ,OAAO,CAAC,CAAC;AAAA,CACjD","sourcesContent":["import { join } from \"node:path\";\n\n/** Derive the local Unix socket path for one logical server identity. */\nexport function getUnixSocketPath(serverId: string, serverDirectory: string): string {\n\tif (!/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(serverId)) {\n\t\tthrow new TypeError(\"Unix serverId must be a canonical lowercase UUIDv4\");\n\t}\n\treturn join(serverDirectory, `${serverId}.sock`);\n}\n"]}
@@ -0,0 +1,5 @@
1
+ export { getUnixSocketPath } from "./address.ts";
2
+ export { createUnixListener } from "./listener.ts";
3
+ export { createUnixServer } from "./preset.ts";
4
+ export type { UnixListenerOptions, UnixServerOptions } from "./types.ts";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transports/unix/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export { getUnixSocketPath } from \"./address.ts\";\nexport { createUnixListener } from \"./listener.ts\";\nexport { createUnixServer } from \"./preset.ts\";\nexport type { UnixListenerOptions, UnixServerOptions } from \"./types.ts\";\n"]}
@@ -0,0 +1,4 @@
1
+ export { getUnixSocketPath } from "./address.js";
2
+ export { createUnixListener } from "./listener.js";
3
+ export { createUnixServer } from "./preset.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transports/unix/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC","sourcesContent":["export { getUnixSocketPath } from \"./address.ts\";\nexport { createUnixListener } from \"./listener.ts\";\nexport { createUnixServer } from \"./preset.ts\";\nexport type { UnixListenerOptions, UnixServerOptions } from \"./types.ts\";\n"]}
@@ -0,0 +1,24 @@
1
+ import { type Socket } from "node:net";
2
+ import type { ByteConnection } from "../../connection.ts";
3
+ import type { ServerListener } from "../../listener.ts";
4
+ import type { UnixListenerOptions } from "./types.ts";
5
+ /** @internal Exported only for transport-level verification. */
6
+ export declare class UnixByteConnection implements ByteConnection {
7
+ private readonly socket;
8
+ private readonly gracefulCloseTimeoutMs;
9
+ private readonly maxPendingBytes;
10
+ private pendingBytes;
11
+ private closedValue;
12
+ private closing;
13
+ private writeTail;
14
+ private closePromise?;
15
+ private resolveClose?;
16
+ constructor(socket: Socket, gracefulCloseTimeoutMs: number, maxPendingBytes: number);
17
+ get closed(): boolean;
18
+ send(chunk: Uint8Array): Promise<void>;
19
+ close(finalChunk?: Uint8Array): Promise<void>;
20
+ markClosed(): void;
21
+ private write;
22
+ }
23
+ export declare function createUnixListener(options: UnixListenerOptions): ServerListener;
24
+ //# sourceMappingURL=listener.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"listener.d.ts","sourceRoot":"","sources":["../../../src/transports/unix/listener.ts"],"names":[],"mappings":"AAGA,OAAO,EAA+C,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AAGpF,OAAO,KAAK,EAAE,cAAc,EAA0B,MAAM,qBAAqB,CAAC;AAClF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAqLtD,gEAAgE;AAChE,qBAAa,kBAAmB,YAAW,cAAc;IACxD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAS;IAChD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IACzC,OAAO,CAAC,YAAY,CAAK;IACzB,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAoC;IACrD,OAAO,CAAC,YAAY,CAAC,CAAgB;IACrC,OAAO,CAAC,YAAY,CAAC,CAAa;IAElC,YAAY,MAAM,EAAE,MAAM,EAAE,sBAAsB,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAIlF;IAED,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAgBrC;IAED,KAAK,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CA8B5C;IAED,UAAU,IAAI,IAAI,CAMjB;IAED,OAAO,CAAC,KAAK;CAsBb;AAgGD,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,cAAc,CAE/E","sourcesContent":["import { createHash, randomUUID } from \"node:crypto\";\nimport type { Stats } from \"node:fs\";\nimport { chmod, link, lstat, mkdir, rename, unlink } from \"node:fs/promises\";\nimport { createConnection, createServer, type Server, type Socket } from \"node:net\";\nimport { dirname, join } from \"node:path\";\nimport { DEFAULT_MAX_FRAME_LENGTH } from \"@m4ike1/ion-protocol\";\nimport type { ByteConnection, ByteConnectionAcceptor } from \"../../connection.ts\";\nimport type { ServerListener } from \"../../listener.ts\";\nimport type { UnixListenerOptions } from \"./types.ts\";\n\nconst DEFAULT_SOCKET_MODE = 0o600;\nconst DEFAULT_GRACEFUL_CLOSE_TIMEOUT_MS = 5_000;\nconst MAX_UINT32 = 0xffff_ffff;\nconst MAX_TIMER_DELAY_MS = 2_147_483_647;\nconst SOCKET_PROBE_TIMEOUT_MS = 1_000;\n\ninterface ResolvedUnixListenerOptions {\n\tpath: string;\n\tmode: number;\n\tgracefulCloseTimeoutMs: number;\n\tmaxPendingBytes: number;\n\tonError?: (error: Error) => void;\n}\n\ninterface FileIdentity {\n\tdev: number;\n\tino: number;\n}\nclass UnixListener implements ServerListener {\n\tprivate readonly options: ResolvedUnixListenerOptions;\n\tprivate readonly path: string;\n\tprivate readonly mode: number;\n\tprivate readonly connections = new Set<UnixByteConnection>();\n\tprivate server?: Server;\n\tprivate socketIdentity?: FileIdentity;\n\tprivate ownedBindPath?: string;\n\tprivate closing = false;\n\tprivate closePromise?: Promise<void>;\n\tprivate accept?: ByteConnectionAcceptor;\n\n\tconstructor(options: UnixListenerOptions) {\n\t\tthis.options = resolveUnixListenerOptions(options);\n\t\tthis.path = this.options.path;\n\t\tthis.mode = this.options.mode;\n\t}\n\n\tasync start(accept: ByteConnectionAcceptor): Promise<void> {\n\t\tif (this.server) throw new Error(\"Unix listener is already started\");\n\t\tif (this.closing) throw new Error(\"Unix listener is closing or closed\");\n\t\tthis.accept = accept;\n\n\t\tconst ownedBindPath = getOwnedBindPath(this.path);\n\t\tawait mkdir(dirname(this.path), { recursive: true, mode: 0o700 });\n\t\tawait removeStaleSocket(this.path);\n\t\tawait removeStaleSocket(ownedBindPath);\n\t\tthis.ownedBindPath = ownedBindPath;\n\t\tconst server = createServer((socket) => this.acceptSocket(socket));\n\t\tserver.on(\"error\", (error) => this.reportError(error));\n\t\tthis.server = server;\n\t\ttry {\n\t\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\t\tconst onError = (error: Error): void => {\n\t\t\t\t\tserver.off(\"listening\", onListening);\n\t\t\t\t\treject(error);\n\t\t\t\t};\n\t\t\t\tconst onListening = (): void => {\n\t\t\t\t\tserver.off(\"error\", onError);\n\t\t\t\t\tresolve();\n\t\t\t\t};\n\t\t\t\tserver.once(\"error\", onError);\n\t\t\t\tserver.once(\"listening\", onListening);\n\t\t\t\tserver.listen(ownedBindPath);\n\t\t\t});\n\t\t\tconst stats = await lstat(ownedBindPath);\n\t\t\tif (!stats.isSocket()) throw new Error(`Unix listener path is not a socket after binding: ${ownedBindPath}`);\n\t\t\tthis.socketIdentity = { dev: stats.dev, ino: stats.ino };\n\t\t\tawait link(ownedBindPath, this.path);\n\t\t\tawait setSocketMode(this.path, this.mode);\n\t\t\tawait removePath(ownedBindPath);\n\t\t\tthis.ownedBindPath = undefined;\n\t\t} catch (error) {\n\t\t\tawait this.closeServerAndCleanup(server);\n\t\t\tthis.server = undefined;\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tasync close(): Promise<void> {\n\t\tif (this.closePromise) return this.closePromise;\n\t\tthis.closing = true;\n\t\tthis.closePromise = this.closeInternal();\n\t\treturn this.closePromise;\n\t}\n\n\tprivate acceptSocket(socket: Socket): void {\n\t\tif (this.closing) {\n\t\t\tsocket.destroy();\n\t\t\treturn;\n\t\t}\n\t\tconst connection = new UnixByteConnection(\n\t\t\tsocket,\n\t\t\tthis.options.gracefulCloseTimeoutMs,\n\t\t\tthis.options.maxPendingBytes,\n\t\t);\n\t\tthis.connections.add(connection);\n\t\tconst accept = this.accept;\n\t\tif (!accept) {\n\t\t\tsocket.destroy();\n\t\t\treturn;\n\t\t}\n\t\tconst handler = accept(connection);\n\t\tsocket.on(\"data\", (chunk) => {\n\t\t\thandler.onData(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));\n\t\t});\n\t\tsocket.on(\"error\", (error) => {\n\t\t\thandler.onError(error);\n\t\t\tsocket.destroy();\n\t\t});\n\t\tsocket.once(\"close\", () => {\n\t\t\tconnection.markClosed();\n\t\t\tthis.connections.delete(connection);\n\t\t\thandler.onClose();\n\t\t});\n\t}\n\n\tprivate async closeInternal(): Promise<void> {\n\t\tconst serverClosed = this.server ? this.closeServerAndCleanup(this.server) : this.cleanupOwnedSocket();\n\t\tawait Promise.all([...this.connections].map((connection) => connection.close()));\n\t\tawait serverClosed;\n\t\tif (this.ownedBindPath) await removePath(this.ownedBindPath);\n\t\tthis.ownedBindPath = undefined;\n\t\tthis.connections.clear();\n\t\tthis.server = undefined;\n\t}\n\n\tprivate async closeServerAndCleanup(server: Server): Promise<void> {\n\t\ttry {\n\t\t\tawait closeNetServer(server, (error) => this.reportError(error));\n\t\t} finally {\n\t\t\t// Remove an unpublished startup bind path before the public route.\n\t\t\tif (this.ownedBindPath) await removePath(this.ownedBindPath);\n\t\t\tthis.ownedBindPath = undefined;\n\t\t\tawait this.cleanupOwnedSocket();\n\t\t}\n\t}\n\n\tprivate async cleanupOwnedSocket(): Promise<void> {\n\t\tconst identity = this.socketIdentity;\n\t\tthis.socketIdentity = undefined;\n\t\tif (!identity) return;\n\t\tlet current: Stats;\n\t\ttry {\n\t\t\tcurrent = await lstat(this.path);\n\t\t} catch (error) {\n\t\t\tif (isErrorCode(error, \"ENOENT\")) return;\n\t\t\tthrow error;\n\t\t}\n\t\tif (!current.isSocket() || current.dev !== identity.dev || current.ino !== identity.ino) return;\n\n\t\tconst preserved = join(dirname(this.path), `cleanup-${randomUUID().slice(0, 6)}`);\n\t\ttry {\n\t\t\tawait rename(this.path, preserved);\n\t\t} catch (error) {\n\t\t\tif (isErrorCode(error, \"ENOENT\")) return;\n\t\t\tthrow error;\n\t\t}\n\t\tconst moved = await lstat(preserved);\n\t\tif (moved.isSocket() && moved.dev === identity.dev && moved.ino === identity.ino) {\n\t\t\tawait removePath(preserved);\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tawait lstat(this.path);\n\t\t} catch (error) {\n\t\t\tif (isErrorCode(error, \"ENOENT\")) await rename(preserved, this.path);\n\t\t\telse throw error;\n\t\t}\n\t\tthrow new Error(`Unix listener path changed during cleanup; preserved replacement at ${preserved}`);\n\t}\n\n\tprivate reportError(error: unknown): void {\n\t\ttry {\n\t\t\tthis.options.onError?.(error instanceof Error ? error : new Error(String(error)));\n\t\t} catch {\n\t\t\t// Error observers cannot affect listener state.\n\t\t}\n\t}\n}\n\n/** @internal Exported only for transport-level verification. */\nexport class UnixByteConnection implements ByteConnection {\n\tprivate readonly socket: Socket;\n\tprivate readonly gracefulCloseTimeoutMs: number;\n\tprivate readonly maxPendingBytes: number;\n\tprivate pendingBytes = 0;\n\tprivate closedValue = false;\n\tprivate closing = false;\n\tprivate writeTail: Promise<void> = Promise.resolve();\n\tprivate closePromise?: Promise<void>;\n\tprivate resolveClose?: () => void;\n\n\tconstructor(socket: Socket, gracefulCloseTimeoutMs: number, maxPendingBytes: number) {\n\t\tthis.socket = socket;\n\t\tthis.gracefulCloseTimeoutMs = gracefulCloseTimeoutMs;\n\t\tthis.maxPendingBytes = maxPendingBytes;\n\t}\n\n\tget closed(): boolean {\n\t\treturn this.closedValue;\n\t}\n\n\tsend(chunk: Uint8Array): Promise<void> {\n\t\tif (!(chunk instanceof Uint8Array)) {\n\t\t\treturn Promise.reject(new TypeError(\"Unix connection chunks must be Uint8Array\"));\n\t\t}\n\t\tif (this.closedValue || this.closing) return Promise.reject(new Error(\"Unix connection is closed\"));\n\t\tif (this.pendingBytes + chunk.byteLength > this.maxPendingBytes) {\n\t\t\treturn Promise.reject(new Error(\"Unix connection exceeded its pending byte limit\"));\n\t\t}\n\t\tthis.pendingBytes += chunk.byteLength;\n\t\tconst bytes = chunk.slice();\n\t\tconst write = this.writeTail.then(() => this.write(bytes));\n\t\tconst tracked = write.finally(() => {\n\t\t\tthis.pendingBytes -= bytes.byteLength;\n\t\t});\n\t\tthis.writeTail = tracked.catch(() => {});\n\t\treturn tracked;\n\t}\n\n\tclose(finalChunk?: Uint8Array): Promise<void> {\n\t\tif (this.closedValue || this.socket.destroyed) {\n\t\t\tthis.markClosed();\n\t\t\treturn Promise.resolve();\n\t\t}\n\t\tif (this.closePromise) return this.closePromise;\n\t\tthis.closing = true;\n\t\tconst finalBytes = finalChunk?.slice();\n\t\tthis.closePromise = new Promise<void>((resolve) => {\n\t\t\tthis.resolveClose = resolve;\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tif (!this.socket.destroyed) this.socket.destroy();\n\t\t\t\tthis.markClosed();\n\t\t\t}, this.gracefulCloseTimeoutMs);\n\t\t\ttimer.unref();\n\t\t\tthis.socket.once(\"close\", () => clearTimeout(timer));\n\t\t\tvoid this.writeTail.then(() => {\n\t\t\t\tif (this.socket.destroyed) {\n\t\t\t\t\tthis.markClosed();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tif (finalBytes) this.socket.end(finalBytes);\n\t\t\t\t\telse this.socket.end();\n\t\t\t\t} catch {\n\t\t\t\t\tthis.socket.destroy();\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\t\treturn this.closePromise;\n\t}\n\n\tmarkClosed(): void {\n\t\tif (this.closedValue) return;\n\t\tthis.closedValue = true;\n\t\tthis.closing = true;\n\t\tthis.resolveClose?.();\n\t\tthis.resolveClose = undefined;\n\t}\n\n\tprivate write(chunk: Uint8Array): Promise<void> {\n\t\tif (this.closedValue || this.closing || !this.socket.writable) {\n\t\t\treturn Promise.reject(new Error(\"Unix connection is closed\"));\n\t\t}\n\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\tlet settled = false;\n\t\t\tconst onClose = (): void => finish(new Error(\"Unix connection closed during write\"));\n\t\t\tconst finish = (error?: Error | null): void => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tthis.socket.off(\"close\", onClose);\n\t\t\t\tif (error) reject(error);\n\t\t\t\telse resolve();\n\t\t\t};\n\t\t\tthis.socket.once(\"close\", onClose);\n\t\t\ttry {\n\t\t\t\tthis.socket.write(chunk, finish);\n\t\t\t} catch (error) {\n\t\t\t\tfinish(error instanceof Error ? error : new Error(String(error)));\n\t\t\t}\n\t\t});\n\t}\n}\n\nfunction getOwnedBindPath(path: string): string {\n\tconst suffix = createHash(\"sha256\").update(path).digest(\"hex\").slice(0, 8);\n\treturn join(dirname(path), `bind-${suffix}`);\n}\n\nasync function removeStaleSocket(path: string): Promise<void> {\n\tlet original: Stats;\n\ttry {\n\t\toriginal = await lstat(path);\n\t} catch (error) {\n\t\tif (isErrorCode(error, \"ENOENT\")) return;\n\t\tthrow error;\n\t}\n\tif (!original.isSocket()) throw new Error(`Refusing to remove non-socket Unix listener path: ${path}`);\n\tif (await isSocketLive(path)) throw new Error(`Unix listener is already running: ${path}`);\n\n\tconst preserved = join(dirname(path), `stale-${randomUUID().slice(0, 6)}`);\n\ttry {\n\t\tawait rename(path, preserved);\n\t} catch (error) {\n\t\tif (isErrorCode(error, \"ENOENT\")) return;\n\t\tthrow error;\n\t}\n\tconst current = await lstat(preserved);\n\tif (!current.isSocket() || current.dev !== original.dev || current.ino !== original.ino) {\n\t\ttry {\n\t\t\tawait lstat(path);\n\t\t} catch (error) {\n\t\t\tif (isErrorCode(error, \"ENOENT\")) await rename(preserved, path);\n\t\t\telse throw error;\n\t\t}\n\t\tthrow new Error(`Unix listener path changed while checking for a stale socket: ${path}`);\n\t}\n\tawait removePath(preserved);\n}\n\nasync function removePath(path: string): Promise<void> {\n\ttry {\n\t\tawait unlink(path);\n\t} catch (error) {\n\t\tif (!isErrorCode(error, \"ENOENT\")) throw error;\n\t}\n}\n\nfunction isSocketLive(path: string): Promise<boolean> {\n\treturn new Promise<boolean>((resolve, reject) => {\n\t\tconst socket = createConnection(path);\n\t\tlet settled = false;\n\t\tlet timer: NodeJS.Timeout | undefined;\n\t\tconst finish = (result: boolean, error?: Error): void => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (timer) clearTimeout(timer);\n\t\t\tsocket.removeAllListeners();\n\t\t\tsocket.destroy();\n\t\t\tif (error) reject(error);\n\t\t\telse resolve(result);\n\t\t};\n\t\tsocket.once(\"connect\", () => finish(true));\n\t\tsocket.once(\"error\", (error: NodeJS.ErrnoException) => {\n\t\t\tif ([\"ECONNREFUSED\", \"ENOENT\", \"EPIPE\", \"ECONNRESET\"].includes(error.code ?? \"\")) {\n\t\t\t\tfinish(false);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfinish(false, error);\n\t\t});\n\t\ttimer = setTimeout(() => finish(true), SOCKET_PROBE_TIMEOUT_MS);\n\t\ttimer.unref();\n\t});\n}\n\nasync function setSocketMode(path: string, mode: number): Promise<void> {\n\tif (process.platform === \"win32\") return;\n\ttry {\n\t\tawait chmod(path, mode);\n\t} catch (error) {\n\t\tif (!isErrorCode(error, \"ENOSYS\") && !isErrorCode(error, \"ENOTSUP\")) throw error;\n\t}\n}\n\nfunction closeNetServer(server: Server, reportError: (error: Error) => void): Promise<void> {\n\tif (!server.listening) return Promise.resolve();\n\treturn new Promise<void>((resolve) => {\n\t\tserver.close((error) => {\n\t\t\tif (error) reportError(error);\n\t\t\tresolve();\n\t\t});\n\t});\n}\n\nfunction isErrorCode(error: unknown, code: string): boolean {\n\treturn error instanceof Error && \"code\" in error && error.code === code;\n}\n\nexport function createUnixListener(options: UnixListenerOptions): ServerListener {\n\treturn new UnixListener(options);\n}\n\nfunction resolveUnixListenerOptions(options: UnixListenerOptions): ResolvedUnixListenerOptions {\n\tif (!options.path) throw new TypeError(\"Server Unix socket path must not be empty\");\n\tconst mode = options.mode ?? DEFAULT_SOCKET_MODE;\n\tif (!Number.isInteger(mode) || mode < 0 || mode > 0o777) {\n\t\tthrow new TypeError(\"Server Unix socket mode must be an integer between 0 and 0o777\");\n\t}\n\tconst maxFrameLength = options.maxFrameLength ?? DEFAULT_MAX_FRAME_LENGTH;\n\tif (!Number.isSafeInteger(maxFrameLength) || maxFrameLength <= 0 || maxFrameLength > MAX_UINT32) {\n\t\tthrow new TypeError(`Server maxFrameLength must be an integer between 1 and ${MAX_UINT32}`);\n\t}\n\tconst maxPendingBytes = options.maxPendingBytes ?? maxFrameLength * 4;\n\tif (!Number.isSafeInteger(maxPendingBytes) || maxPendingBytes < maxFrameLength + 4) {\n\t\tthrow new TypeError(\"Server maxPendingBytes must be a safe integer at least maxFrameLength + 4\");\n\t}\n\tconst gracefulCloseTimeoutMs = options.gracefulCloseTimeoutMs ?? DEFAULT_GRACEFUL_CLOSE_TIMEOUT_MS;\n\tif (\n\t\t!Number.isSafeInteger(gracefulCloseTimeoutMs) ||\n\t\tgracefulCloseTimeoutMs <= 0 ||\n\t\tgracefulCloseTimeoutMs > MAX_TIMER_DELAY_MS\n\t) {\n\t\tthrow new TypeError(`Server gracefulCloseTimeoutMs must be an integer between 1 and ${MAX_TIMER_DELAY_MS}`);\n\t}\n\treturn {\n\t\tpath: options.path,\n\t\tmode,\n\t\tmaxPendingBytes,\n\t\tgracefulCloseTimeoutMs,\n\t\tonError: options.onError,\n\t};\n}\n"]}