necoherence-http-adapter 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README-ja.md +28 -0
- package/README.md +27 -2
- package/dist/index.d.ts +44 -0
- package/dist/index.js +399 -0
- package/package.json +41 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 barineco
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README-ja.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# TypeScript 向けの Necoherence の HTTP アダプター
|
|
2
|
+
|
|
3
|
+
[プロジェクトの概要](https://github.com/barineco/necoherence/blob/publish/README-ja.md) | [English](README.md)
|
|
4
|
+
|
|
5
|
+
この任意のパッケージは、Necoherence のネットワーク副作用のコマンドを `fetch` で実行します。ライセンスは MIT です。
|
|
6
|
+
核の `necoherence` パッケージは、台帳を持ち、呼び出し側が渡す観測を受け取ります。
|
|
7
|
+
2 つのパッケージは、同じ版で導入します。
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install necoherence@0.2.1 necoherence-http-adapter@0.2.1
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { FetchHttpAdapter } from "necoherence-http-adapter";
|
|
15
|
+
|
|
16
|
+
const adapter = new FetchHttpAdapter();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
開発では、先に `../typescript` をビルドします。開発時の依存は、ローカルの核のパッケージを参照します。公開するパッケージは、核を peer dependency として宣言します。
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm ci
|
|
23
|
+
npm run typecheck
|
|
24
|
+
npm test
|
|
25
|
+
npm pack
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
[MIT ライセンス](LICENSE)
|
package/README.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Necoherence HTTP adapter for TypeScript
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Project overview](https://github.com/barineco/necoherence/blob/publish/README.md) | [日本語](README-ja.md)
|
|
4
|
+
|
|
5
|
+
This optional MIT package executes Necoherence network-effect commands with `fetch`.
|
|
6
|
+
The core `necoherence` package contains the ledger and accepts caller-supplied observations.
|
|
7
|
+
Install both packages at matching versions:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install necoherence@0.2.1 necoherence-http-adapter@0.2.1
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { FetchHttpAdapter } from "necoherence-http-adapter";
|
|
15
|
+
|
|
16
|
+
const adapter = new FetchHttpAdapter();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
For development, build `../typescript` first. The development dependency links that local core package; the published package declares it as a peer dependency.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm ci
|
|
23
|
+
npm run typecheck
|
|
24
|
+
npm test
|
|
25
|
+
npm pack
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
[MIT License](LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type CancelCommand, type HostObservation, type MonotonicClockObservation, type SendCommand, type StartObservation } from "necoherence/network-effect";
|
|
2
|
+
export interface FetchResponseLike {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
readonly headers: {
|
|
5
|
+
readonly entries: () => Iterable<readonly [string, string]>;
|
|
6
|
+
};
|
|
7
|
+
readonly arrayBuffer: () => Promise<ArrayBuffer>;
|
|
8
|
+
}
|
|
9
|
+
export interface FetchRequestOptions {
|
|
10
|
+
readonly method: string;
|
|
11
|
+
readonly headers: readonly (readonly [string, string])[];
|
|
12
|
+
readonly body: Uint8Array;
|
|
13
|
+
readonly signal: unknown;
|
|
14
|
+
}
|
|
15
|
+
export type FetchLike = (target: string, options: FetchRequestOptions) => Promise<FetchResponseLike>;
|
|
16
|
+
export interface AbortControllerLike {
|
|
17
|
+
readonly signal: unknown;
|
|
18
|
+
abort: () => void;
|
|
19
|
+
}
|
|
20
|
+
export type AbortControllerFactory = () => AbortControllerLike;
|
|
21
|
+
export interface MonotonicClockPort {
|
|
22
|
+
observe: () => MonotonicClockObservation;
|
|
23
|
+
}
|
|
24
|
+
export interface CancellationPort {
|
|
25
|
+
request: (command: CancelCommand) => void | boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface FetchHttpAdapterOptions {
|
|
28
|
+
readonly fetch?: FetchLike;
|
|
29
|
+
readonly createAbortController?: AbortControllerFactory;
|
|
30
|
+
readonly monotonicClock?: MonotonicClockPort;
|
|
31
|
+
readonly cancellation?: CancellationPort;
|
|
32
|
+
}
|
|
33
|
+
export declare class FetchHttpAdapter {
|
|
34
|
+
#private;
|
|
35
|
+
constructor(options?: FetchHttpAdapterOptions);
|
|
36
|
+
start(command: SendCommand): StartObservation;
|
|
37
|
+
observe(handle: {
|
|
38
|
+
readonly value: string;
|
|
39
|
+
}): HostObservation;
|
|
40
|
+
cancel(command: CancelCommand): void;
|
|
41
|
+
close(handle: {
|
|
42
|
+
readonly value: string;
|
|
43
|
+
}): void;
|
|
44
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 barineco
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { sourceNone, sourceSome, } from "necoherence/network-effect";
|
|
4
|
+
const unknownEffect = Object.freeze({
|
|
5
|
+
tag: "Unknown",
|
|
6
|
+
reason: "NoResponse",
|
|
7
|
+
});
|
|
8
|
+
const frozen = (value) => Object.freeze(value);
|
|
9
|
+
const defaultFetch = async (target, options) => {
|
|
10
|
+
const headers = new Headers();
|
|
11
|
+
for (const [name, value] of options.headers)
|
|
12
|
+
headers.append(name, value);
|
|
13
|
+
const init = {
|
|
14
|
+
method: options.method,
|
|
15
|
+
headers,
|
|
16
|
+
signal: options.signal,
|
|
17
|
+
};
|
|
18
|
+
if (options.body.length > 0) {
|
|
19
|
+
init.body = options.body;
|
|
20
|
+
}
|
|
21
|
+
const response = await globalThis.fetch(target, init);
|
|
22
|
+
return {
|
|
23
|
+
status: response.status,
|
|
24
|
+
headers: response.headers,
|
|
25
|
+
arrayBuffer: () => response.arrayBuffer(),
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
function failure(command, stage, cause) {
|
|
29
|
+
return frozen({
|
|
30
|
+
attempt: command.prepared.input.attempt,
|
|
31
|
+
stage,
|
|
32
|
+
configuration: command.prepared.input.configuration,
|
|
33
|
+
cause,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function textBytes(value) {
|
|
37
|
+
return Object.freeze(Array.from(new TextEncoder().encode(value)));
|
|
38
|
+
}
|
|
39
|
+
function headersOf(response) {
|
|
40
|
+
const fields = [];
|
|
41
|
+
for (const [name, value] of response.headers.entries()) {
|
|
42
|
+
fields.push(frozen({ name, value: textBytes(value) }));
|
|
43
|
+
}
|
|
44
|
+
return Object.freeze(fields);
|
|
45
|
+
}
|
|
46
|
+
function bodyOf(buffer) {
|
|
47
|
+
return Object.freeze(Array.from(new Uint8Array(buffer)));
|
|
48
|
+
}
|
|
49
|
+
function requestHeadersOf(command) {
|
|
50
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
51
|
+
return Object.freeze(command.prepared.input.instruction.headers.map((header) => {
|
|
52
|
+
const value = decoder.decode(Uint8Array.from(header.value));
|
|
53
|
+
if (/[\u0000\r\n]/.test(value))
|
|
54
|
+
throw new TypeError("request header value is unavailable");
|
|
55
|
+
return Object.freeze([header.name, value]);
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
function property(value, name) {
|
|
59
|
+
return value !== null && typeof value === "object" && name in value
|
|
60
|
+
? value[name]
|
|
61
|
+
: undefined;
|
|
62
|
+
}
|
|
63
|
+
function errorDetail(error, name) {
|
|
64
|
+
return property(error, name) ?? property(property(error, "cause"), name);
|
|
65
|
+
}
|
|
66
|
+
function errorCode(error) {
|
|
67
|
+
const code = errorDetail(error, "code");
|
|
68
|
+
return typeof code === "string" ? code : undefined;
|
|
69
|
+
}
|
|
70
|
+
function classifyTimeout(error, phase) {
|
|
71
|
+
const code = errorCode(error);
|
|
72
|
+
let stage;
|
|
73
|
+
let cause;
|
|
74
|
+
switch (code) {
|
|
75
|
+
case "UND_ERR_CONNECT_TIMEOUT":
|
|
76
|
+
stage = "Start";
|
|
77
|
+
cause = "LibraryReported";
|
|
78
|
+
break;
|
|
79
|
+
case "UND_ERR_HEADERS_TIMEOUT":
|
|
80
|
+
stage = "ResponseHeaders";
|
|
81
|
+
cause = "LibraryReported";
|
|
82
|
+
break;
|
|
83
|
+
case "UND_ERR_BODY_TIMEOUT":
|
|
84
|
+
stage = "ResponseBody";
|
|
85
|
+
cause = "LibraryReported";
|
|
86
|
+
break;
|
|
87
|
+
case "ETIMEDOUT":
|
|
88
|
+
stage = phase === "ResponseBody" ? "ResponseBody" : "Start";
|
|
89
|
+
cause = "SocketReported";
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
if (!(error instanceof DOMException) || error.name !== "TimeoutError") {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
stage = phase === "ResponseBody" ? "ResponseBody" : "ResponseHeaders";
|
|
96
|
+
cause = "HostOperation";
|
|
97
|
+
}
|
|
98
|
+
return frozen({
|
|
99
|
+
stage,
|
|
100
|
+
cause: frozen({ tag: "Timeout", stage, cause }),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function classifyFetchFailure(error, hasRequestBody) {
|
|
104
|
+
const timeout = classifyTimeout(error, "Fetch");
|
|
105
|
+
if (timeout !== undefined)
|
|
106
|
+
return timeout;
|
|
107
|
+
const code = errorCode(error);
|
|
108
|
+
if (code === "ENOTFOUND" || code === "EAI_AGAIN") {
|
|
109
|
+
return frozen({ stage: "Start", cause: frozen({ tag: "NameResolutionFailed" }) });
|
|
110
|
+
}
|
|
111
|
+
if (code === "ECONNREFUSED") {
|
|
112
|
+
return frozen({ stage: "Start", cause: frozen({ tag: "ConnectionFailed" }) });
|
|
113
|
+
}
|
|
114
|
+
const errorName = errorDetail(error, "name");
|
|
115
|
+
const errorMessage = errorDetail(error, "message");
|
|
116
|
+
const cause = property(error, "cause");
|
|
117
|
+
const causeName = property(cause, "name");
|
|
118
|
+
const causeMessage = property(cause, "message");
|
|
119
|
+
if (code?.startsWith("HPE_") === true
|
|
120
|
+
|| errorName === "HTTPParserError"
|
|
121
|
+
|| causeName === "HTTPParserError"
|
|
122
|
+
|| (typeof errorMessage === "string"
|
|
123
|
+
&& errorMessage.includes("HTTP/1.1 protocol"))
|
|
124
|
+
|| (typeof causeMessage === "string"
|
|
125
|
+
&& (causeMessage.includes("HTTP/1.1 protocol")
|
|
126
|
+
|| causeMessage.includes("Invalid status")))) {
|
|
127
|
+
return frozen({
|
|
128
|
+
stage: "ResponseHeaders",
|
|
129
|
+
cause: frozen({ tag: "MalformedResponse" }),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (code === "UND_ERR_SOCKET" || code === "ECONNRESET" || code === "EPIPE") {
|
|
133
|
+
return hasRequestBody
|
|
134
|
+
? frozen({
|
|
135
|
+
stage: "RequestWrite",
|
|
136
|
+
cause: frozen({ tag: "RequestWriteFailed" }),
|
|
137
|
+
})
|
|
138
|
+
: frozen({
|
|
139
|
+
stage: "ResponseHeaders",
|
|
140
|
+
cause: frozen({ tag: "ResponseHeadersFailed" }),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
const message = property(error, "message");
|
|
144
|
+
if (typeof message === "string"
|
|
145
|
+
&& (message.includes("header") || message.includes("GET/HEAD method cannot have body"))) {
|
|
146
|
+
return frozen({
|
|
147
|
+
stage: "RequestWrite",
|
|
148
|
+
cause: frozen({ tag: "RequestHeaderEncodingUnavailable" }),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return frozen({
|
|
152
|
+
stage: "ResponseHeaders",
|
|
153
|
+
cause: frozen({ tag: "HostRuntimeFailure" }),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
export class FetchHttpAdapter {
|
|
157
|
+
#fetch;
|
|
158
|
+
#createAbortController;
|
|
159
|
+
#clock;
|
|
160
|
+
#cancellation;
|
|
161
|
+
#workers = new Map();
|
|
162
|
+
#nextHandle = 0n;
|
|
163
|
+
constructor(options = {}) {
|
|
164
|
+
this.#fetch = options.fetch ?? defaultFetch;
|
|
165
|
+
this.#createAbortController = options.createAbortController
|
|
166
|
+
?? (() => new AbortController());
|
|
167
|
+
this.#clock = options.monotonicClock;
|
|
168
|
+
this.#cancellation = options.cancellation;
|
|
169
|
+
}
|
|
170
|
+
start(command) {
|
|
171
|
+
const input = command.prepared.input;
|
|
172
|
+
if (input.configuration.requestBodyLimit < 0n
|
|
173
|
+
|| input.configuration.responseBodyLimit < 0n
|
|
174
|
+
|| BigInt(input.instruction.body.length) > input.configuration.requestBodyLimit
|
|
175
|
+
|| (input.deadline !== null
|
|
176
|
+
&& (input.configuration.monotonicClock === null
|
|
177
|
+
|| input.deadline.capability.clockBasis.value !== input.deadline.clockBasis.value
|
|
178
|
+
|| input.configuration.monotonicClock.identity.value
|
|
179
|
+
!== input.deadline.capability.identity.value
|
|
180
|
+
|| input.configuration.monotonicClock.clockBasis.value
|
|
181
|
+
!== input.deadline.capability.clockBasis.value))) {
|
|
182
|
+
return frozen({
|
|
183
|
+
tag: "StartFailed",
|
|
184
|
+
failure: failure(command, "Start", frozen({ tag: "InvalidHostConfiguration" })),
|
|
185
|
+
effect: unknownEffect,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (!/^https?:\/\//i.test(input.instruction.target)) {
|
|
189
|
+
return frozen({
|
|
190
|
+
tag: "StartFailed",
|
|
191
|
+
failure: failure(command, "Start", frozen({ tag: "UnsupportedUriScheme" })),
|
|
192
|
+
effect: unknownEffect,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
if (input.deadline !== null && this.#clock === undefined) {
|
|
196
|
+
return frozen({
|
|
197
|
+
tag: "StartFailed",
|
|
198
|
+
failure: failure(command, "Start", frozen({ tag: "DeadlineControlUnavailable" })),
|
|
199
|
+
effect: unknownEffect,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
let controller;
|
|
203
|
+
try {
|
|
204
|
+
controller = this.#createAbortController();
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
return frozen({
|
|
208
|
+
tag: "StartFailed",
|
|
209
|
+
failure: failure(command, "Start", frozen({ tag: "ExecutionStartFailed" })),
|
|
210
|
+
effect: unknownEffect,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
const handle = frozen({
|
|
214
|
+
value: `typescript.worker.${this.#nextHandle.toString()}`,
|
|
215
|
+
});
|
|
216
|
+
this.#nextHandle += 1n;
|
|
217
|
+
const worker = {
|
|
218
|
+
command,
|
|
219
|
+
handle,
|
|
220
|
+
controller,
|
|
221
|
+
queue: [],
|
|
222
|
+
delivery: 0n,
|
|
223
|
+
closed: false,
|
|
224
|
+
sourceSequence: 0n,
|
|
225
|
+
cancelled: false,
|
|
226
|
+
deadlineQueued: false,
|
|
227
|
+
};
|
|
228
|
+
this.#workers.set(handle.value, worker);
|
|
229
|
+
queueMicrotask(() => { void this.#run(worker); });
|
|
230
|
+
return frozen({ tag: "Started", attempt: input.attempt, handle });
|
|
231
|
+
}
|
|
232
|
+
observe(handle) {
|
|
233
|
+
const worker = this.#workers.get(handle.value);
|
|
234
|
+
if (worker === undefined) {
|
|
235
|
+
return frozen({
|
|
236
|
+
tag: "Pending",
|
|
237
|
+
attempt: {
|
|
238
|
+
logical: { kind: "", provenance: "", revision: 0n },
|
|
239
|
+
ordinal: 0n,
|
|
240
|
+
},
|
|
241
|
+
handle,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
this.#queueDeadline(worker);
|
|
245
|
+
const observation = worker.queue.shift();
|
|
246
|
+
if (observation === undefined) {
|
|
247
|
+
return frozen({
|
|
248
|
+
tag: "Pending",
|
|
249
|
+
attempt: worker.command.prepared.input.attempt,
|
|
250
|
+
handle: worker.handle,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
const delivery = worker.delivery;
|
|
254
|
+
worker.delivery += 1n;
|
|
255
|
+
return frozen({ tag: "Delivered", delivery, observation });
|
|
256
|
+
}
|
|
257
|
+
cancel(command) {
|
|
258
|
+
const worker = this.#workers.get(command.handle.value);
|
|
259
|
+
if (worker === undefined || worker.closed)
|
|
260
|
+
return;
|
|
261
|
+
if (this.#cancellation === undefined) {
|
|
262
|
+
this.#queueFailure(worker, "Cancel", frozen({ tag: "CancellationRejected" }));
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
try {
|
|
266
|
+
const accepted = this.#cancellation.request(command);
|
|
267
|
+
if (accepted === false) {
|
|
268
|
+
this.#queueFailure(worker, "Cancel", frozen({ tag: "CancellationRejected" }));
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
worker.cancelled = true;
|
|
272
|
+
worker.controller.abort();
|
|
273
|
+
worker.queue.push(frozen({
|
|
274
|
+
tag: "CancellationObservation",
|
|
275
|
+
attempt: command.attempt,
|
|
276
|
+
handle: command.handle,
|
|
277
|
+
sourceSequence: sourceNone(),
|
|
278
|
+
effect: unknownEffect,
|
|
279
|
+
}));
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
this.#queueFailure(worker, "Cancel", frozen({ tag: "CancellationRejected" }));
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
close(handle) {
|
|
286
|
+
const worker = this.#workers.get(handle.value);
|
|
287
|
+
if (worker === undefined || worker.closed)
|
|
288
|
+
return;
|
|
289
|
+
worker.closed = true;
|
|
290
|
+
this.#queueFailure(worker, "Cancel", frozen({ tag: "HostClosed" }));
|
|
291
|
+
worker.controller.abort();
|
|
292
|
+
}
|
|
293
|
+
#queueFailure(worker, stage, cause) {
|
|
294
|
+
worker.queue.push(frozen({
|
|
295
|
+
tag: "FailureObservation",
|
|
296
|
+
attempt: worker.command.prepared.input.attempt,
|
|
297
|
+
handle: worker.handle,
|
|
298
|
+
sourceSequence: sourceNone(),
|
|
299
|
+
failure: failure(worker.command, stage, cause),
|
|
300
|
+
effect: unknownEffect,
|
|
301
|
+
}));
|
|
302
|
+
}
|
|
303
|
+
#queueDeadline(worker) {
|
|
304
|
+
const deadline = worker.command.prepared.input.deadline;
|
|
305
|
+
if (worker.deadlineQueued || deadline === null || this.#clock === undefined)
|
|
306
|
+
return;
|
|
307
|
+
try {
|
|
308
|
+
const observedAt = this.#clock.observe();
|
|
309
|
+
if (observedAt.clockBasis.value === deadline.clockBasis.value
|
|
310
|
+
&& observedAt.value >= deadline.limit) {
|
|
311
|
+
worker.deadlineQueued = true;
|
|
312
|
+
worker.queue.push(frozen({
|
|
313
|
+
tag: "DeadlineObservation",
|
|
314
|
+
attempt: worker.command.prepared.input.attempt,
|
|
315
|
+
handle: worker.handle,
|
|
316
|
+
sourceSequence: sourceNone(),
|
|
317
|
+
deadline,
|
|
318
|
+
observedAt,
|
|
319
|
+
effect: unknownEffect,
|
|
320
|
+
}));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
worker.deadlineQueued = true;
|
|
325
|
+
this.#queueFailure(worker, "Deadline", frozen({ tag: "DeadlineControlUnavailable" }));
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
async #run(worker) {
|
|
329
|
+
const input = worker.command.prepared.input;
|
|
330
|
+
let headers;
|
|
331
|
+
try {
|
|
332
|
+
headers = requestHeadersOf(worker.command);
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
this.#queueFailure(worker, "RequestWrite", frozen({ tag: "RequestHeaderEncodingUnavailable" }));
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
let response;
|
|
339
|
+
try {
|
|
340
|
+
response = await this.#fetch(input.instruction.target, {
|
|
341
|
+
method: input.instruction.method,
|
|
342
|
+
headers,
|
|
343
|
+
body: Uint8Array.from(input.instruction.body),
|
|
344
|
+
signal: worker.controller.signal,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
catch (error) {
|
|
348
|
+
if (worker.closed || worker.cancelled)
|
|
349
|
+
return;
|
|
350
|
+
const classified = classifyFetchFailure(error, input.instruction.body.length > 0);
|
|
351
|
+
this.#queueFailure(worker, classified.stage, classified.cause);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (!Number.isInteger(response.status) || response.status < 100 || response.status > 999) {
|
|
355
|
+
this.#queueFailure(worker, "ResponseHeaders", frozen({ tag: "MalformedResponse" }));
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
let body;
|
|
359
|
+
try {
|
|
360
|
+
body = bodyOf(await response.arrayBuffer());
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
const timeout = classifyTimeout(error, "ResponseBody");
|
|
364
|
+
if (timeout !== undefined) {
|
|
365
|
+
this.#queueFailure(worker, timeout.stage, timeout.cause);
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
this.#queueFailure(worker, "ResponseBody", frozen({ tag: "ResponseBodyFailed" }));
|
|
369
|
+
}
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (BigInt(body.length) > input.configuration.responseBodyLimit) {
|
|
373
|
+
this.#queueFailure(worker, "ResponseBody", frozen({ tag: "ResponseBodyLimitExceeded" }));
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
let responseHeaders;
|
|
377
|
+
try {
|
|
378
|
+
responseHeaders = headersOf(response);
|
|
379
|
+
}
|
|
380
|
+
catch {
|
|
381
|
+
this.#queueFailure(worker, "ResponseHeaders", frozen({ tag: "HeaderRepresentationUnavailable" }));
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
worker.queue.push(frozen({
|
|
385
|
+
tag: "ResponseObservation",
|
|
386
|
+
attempt: input.attempt,
|
|
387
|
+
handle: worker.handle,
|
|
388
|
+
sourceSequence: sourceSome(worker.sourceSequence),
|
|
389
|
+
response: frozen({
|
|
390
|
+
status: response.status,
|
|
391
|
+
headers: responseHeaders,
|
|
392
|
+
body,
|
|
393
|
+
}),
|
|
394
|
+
provenance: "LibraryNormalized",
|
|
395
|
+
effect: unknownEffect,
|
|
396
|
+
}));
|
|
397
|
+
worker.sourceSequence += 1n;
|
|
398
|
+
}
|
|
399
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "necoherence-http-adapter",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "HTTP host adapter for Necoherence",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/",
|
|
15
|
+
"README.md",
|
|
16
|
+
"README-ja.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc --project tsconfig.build.json",
|
|
21
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
22
|
+
"pretest": "npm run build",
|
|
23
|
+
"test": "vitest run --fileParallelism=false"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"necoherence": "0.2.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"necoherence": "file:../typescript",
|
|
30
|
+
"@types/node": "^26.1.2",
|
|
31
|
+
"typescript": "^5.7.3",
|
|
32
|
+
"vitest": "^2.1.8"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/barineco/necoherence.git",
|
|
37
|
+
"directory": "typescript-http-adapter"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/barineco/necoherence",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/barineco/necoherence/issues"
|
|
42
|
+
}
|
|
43
|
+
}
|