necoherence-http-adapter 0.2.2 → 0.4.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.
- package/README.md +222 -8
- package/dist/index.d.ts +39 -10
- package/dist/index.js +388 -84
- package/dist/records.d.ts +2 -0
- package/dist/records.js +9 -0
- package/package.json +7 -5
- package/README-ja.md +0 -28
package/README.md
CHANGED
|
@@ -1,22 +1,234 @@
|
|
|
1
|
-
# Necoherence HTTP
|
|
1
|
+
# Necoherence の HTTP アダプター (TypeScript)
|
|
2
2
|
|
|
3
|
-
[
|
|
3
|
+
[プロジェクトの概要](../../../../README.md)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
The core `necoherence` package contains the ledger and accepts caller-supplied observations.
|
|
7
|
-
Install both packages at matching versions:
|
|
5
|
+
ネットワーク副作用コマンドを TypeScript で実行する追加アダプターパッケージであり、HTTP クライアントには標準の `fetch` を使用します。コアパッケージ ( `necoherence` ) は副作用状態を管理し、本アダプターから渡された I/O 観測結果を受け取って状態機械を更新します。両パッケージは同一バージョンでインストールして使用します。
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
|
-
npm install necoherence@0.
|
|
8
|
+
npm install necoherence@0.4.0 necoherence-http-adapter@0.4.0
|
|
11
9
|
```
|
|
12
10
|
|
|
11
|
+
## 使い方
|
|
12
|
+
|
|
13
|
+
### `drive` による試行の完了
|
|
14
|
+
|
|
15
|
+
関数 `drive` は、コアパッケージの `semanticExecution.start` が返した最初の進行を受け取り、結果が確定するまで処理を進めます。戻り値は `DriveResult` の Promise であり、成功と失敗の区別は公開 API の節に示します。
|
|
16
|
+
|
|
13
17
|
```typescript
|
|
14
|
-
import {
|
|
18
|
+
import { networkEffect, semanticExecution } from "necoherence";
|
|
19
|
+
import { FetchHttpAdapter, drive } from "necoherence-http-adapter";
|
|
15
20
|
|
|
21
|
+
// admitted: semanticExecution.AdmittedComposition, providers: semanticExecution.Provider の配列,
|
|
22
|
+
// args: 引数の値の配列 ( コアパッケージの手順で用意 )
|
|
16
23
|
const adapter = new FetchHttpAdapter();
|
|
24
|
+
const started = semanticExecution.start(
|
|
25
|
+
admitted,
|
|
26
|
+
providers,
|
|
27
|
+
args,
|
|
28
|
+
networkEffect.emptyNetworkState(),
|
|
29
|
+
);
|
|
30
|
+
if (started.tag !== "Ok") throw new Error("start failed");
|
|
31
|
+
const driven = await drive(adapter, started.value);
|
|
32
|
+
if (driven.tag === "Ok") {
|
|
33
|
+
const { result, retained } = driven.value;
|
|
34
|
+
} else {
|
|
35
|
+
const { cause, retained } = driven.failure;
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
引数 `admitted` と `providers` は、コアパッケージの手順で用意します。関数 `drive` の挙動:
|
|
40
|
+
|
|
41
|
+
- 送信の指示: `adapter.start` の呼び出し
|
|
42
|
+
- 観測の指示: worker からの通知による、観測が届くまでの待機
|
|
43
|
+
- コアパッケージへの反映のたび: 処理した試行の `release` の呼び出しと、記録の破棄
|
|
44
|
+
- 期限を持つ試行の待機: clock の `until` が返した時間で終了
|
|
45
|
+
- 結果の確定時: コアパッケージがまだ観測を要求する試行を `retained` に入れて返却
|
|
46
|
+
|
|
47
|
+
成功の値と失敗の値は、いずれも `retained` フィールドを持ちます。これは、`drive` が戻る時点でコアパッケージがまだ観測を要求している試行の配列です。すでにコアパッケージ側で終端に達した試行は、worker が実行中でも `retained` には入りません。その試行の送信中だった要求は中断済みとなり、記録は worker が終わった時に削除されます。結果が確定した通常の場合は、空の配列になります。
|
|
48
|
+
|
|
49
|
+
失敗の値に含まれる `cause` は次の三種類です。
|
|
50
|
+
|
|
51
|
+
| cause | 意味 |
|
|
52
|
+
| --- | --- |
|
|
53
|
+
| コアパッケージの `RunFailure` | コアパッケージが観測の反映を拒否した |
|
|
54
|
+
| `UnknownExecution` | 観測を待つ handle の記録が削除されていた |
|
|
55
|
+
| `DeadlineWaitUnavailable` | clock の `until` が空の値を返した |
|
|
56
|
+
|
|
57
|
+
原因が `DeadlineWaitUnavailable` の場合、`drive` は即座に制御を返します。このとき、記録は `retained` に入ります。
|
|
58
|
+
|
|
59
|
+
### `start` と `observe` の個別呼び出し
|
|
60
|
+
|
|
61
|
+
試行の進め方を自分で制御する場合、コアパッケージの関数とアダプターの関数は次の順に呼び出します。
|
|
62
|
+
|
|
63
|
+
1. 通信の指示の準備: `networkEffect.prepare`
|
|
64
|
+
2. 送信の命令の作成: `networkEffect.sendCommand`
|
|
65
|
+
3. 送信の開始: `adapter.start`
|
|
66
|
+
4. 開始の観測の反映: `networkEffect.observeStart`
|
|
67
|
+
5. 観測の取り出し: `adapter.observe`
|
|
68
|
+
6. 観測の反映と結果の確定: `networkEffect.observe`
|
|
69
|
+
7. 取り出しを終えた試行の記録の破棄: `adapter.release`
|
|
70
|
+
|
|
71
|
+
記録は `start` で作成され、`release` が破棄を認めるまで保持されます。取り出しを終えた試行は、次のように `release` に渡します。
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const decision = adapter.release(state, startedAttempt);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
引数の `state` にはコアパッケージが返した最新の状態を、`startedAttempt` には開始の観測を反映した結果が持つ試行を渡します。戻り値は、記録を破棄できるか保持し続けるかを表す判定値です。
|
|
78
|
+
|
|
79
|
+
| 判定 | 意味 |
|
|
80
|
+
| --- | --- |
|
|
81
|
+
| `dematerialize` | 破棄を認めた。記録が削除される |
|
|
82
|
+
| `retain` ( 理由は `futureUse` ) | コアパッケージがその試行をまだ待っている |
|
|
83
|
+
| `retain` ( 理由は `outstandingEffect` ) | worker がまだ動いている |
|
|
84
|
+
|
|
85
|
+
判定結果が `retain` だった場合は、状態が進んだ後で同じ試行に対して `release` を呼び直します。関数 `drive` を使う場合は、この呼び出しを `drive` が自動で行います。
|
|
86
|
+
|
|
87
|
+
### 公開 API
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
export class FetchHttpAdapter {
|
|
91
|
+
constructor(options?: FetchHttpAdapterOptions);
|
|
92
|
+
start(command: SendCommand): StartObservation;
|
|
93
|
+
observe(started: StartedAttempt): HostObservation | UnknownExecution;
|
|
94
|
+
cancel(command: CancelCommand): HostObservation | UnknownExecution;
|
|
95
|
+
close(started: StartedAttempt): HostObservation | UnknownExecution;
|
|
96
|
+
release(state: NetworkState, started: StartedAttempt): HostRecordDecision;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function drive(
|
|
100
|
+
adapter: FetchHttpAdapter,
|
|
101
|
+
first: semanticExecution.Progress,
|
|
102
|
+
): Promise<DriveResult>;
|
|
103
|
+
|
|
104
|
+
export type DriveResult =
|
|
105
|
+
| { readonly tag: "Ok"; readonly value: Driven }
|
|
106
|
+
| { readonly tag: "Failed"; readonly failure: DriveFailure };
|
|
107
|
+
|
|
108
|
+
export interface Driven {
|
|
109
|
+
readonly result: semanticExecution.ExecutionResult;
|
|
110
|
+
readonly retained: readonly StartedAttempt[];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface DriveFailure {
|
|
114
|
+
readonly cause: semanticExecution.RunFailure | UnknownExecution | DeadlineWaitUnavailable;
|
|
115
|
+
readonly retained: readonly StartedAttempt[];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface UnknownExecution {
|
|
119
|
+
readonly tag: "UnknownExecution";
|
|
120
|
+
readonly handle: { readonly value: string };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface DeadlineWaitUnavailable {
|
|
124
|
+
readonly tag: "DeadlineWaitUnavailable";
|
|
125
|
+
readonly handle: { readonly value: string };
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## port と capability
|
|
130
|
+
|
|
131
|
+
アダプターは、時刻の port と取り消しの port を、オプションとして受け取ります。
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const adapter = new FetchHttpAdapter({
|
|
135
|
+
monotonicClock: clock,
|
|
136
|
+
cancellation: canceller,
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
オプションには、`fetch` と `createAbortController` も渡せます。各 port のインターフェース:
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
export interface MonotonicClockPort {
|
|
144
|
+
capability: () => MonotonicClockCapability;
|
|
145
|
+
observe: () => MonotonicClockObservation | HostFailure;
|
|
146
|
+
until?: (limit: bigint) => number | undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface CancellationPort {
|
|
150
|
+
capability: () => CancellationCapability;
|
|
151
|
+
request: (handle: ExecutionHandle) => undefined | HostFailure;
|
|
152
|
+
}
|
|
17
153
|
```
|
|
18
154
|
|
|
19
|
-
|
|
155
|
+
既定では clock も取り消しの port も空であるため、期限を持つ試行には clock を、取り消しを使う試行には取り消しの port を渡します。
|
|
156
|
+
|
|
157
|
+
各 port は失敗時にコアパッケージの `HostFailure` ( `tag` を持つ値 ) を返します。時刻の観測は `tag` フィールドを持たないため、戻り値の `tag` の有無で通常の値と失敗を見分けます。一方、取り消しに関する capability の照合はアダプター側で行うため、取り消しの port が受け取る引数は `handle` の 1 個のみです。成功時の戻り値は `undefined` です。
|
|
158
|
+
|
|
159
|
+
時刻の port が失敗の値を返した時と例外を出した時は、アダプターは次の値を持つ失敗の観測を一度だけ追加します。追加した後の `observe` は、すでに追加済みのその観測をそのまま返します。
|
|
160
|
+
|
|
161
|
+
- 段階: `Deadline` ( 失敗の値が `Timeout` の時は、その値が持つ段階 )
|
|
162
|
+
- 原因: 返された失敗の値 ( 例外の時は `HostRuntimeFailure` )
|
|
163
|
+
- 作用が不明になった理由: `HostFailure`
|
|
164
|
+
|
|
165
|
+
メソッド `until` の戻り値は、期限までの待機時間をミリ秒で表す `number` です。値が `undefined` の場合は、clock が待機時間を算出不能であることを表し、`until` 自体は省略可能です。
|
|
166
|
+
|
|
167
|
+
関数 `start` における検査の順序と、失敗時の原因:
|
|
168
|
+
|
|
169
|
+
1. scheme の検査: `UnsupportedUriScheme`
|
|
170
|
+
2. コアパッケージ入力の検査: `InvalidHostConfiguration` ( 設定の clock の capability が期限の capability と異なる入力を含みます )
|
|
171
|
+
3. 期限 clock の検査: `DeadlineControlUnavailable`
|
|
172
|
+
4. 取り消し capability の検査: `InvalidHostConfiguration`
|
|
173
|
+
5. worker の開始: `ExecutionStartFailed`
|
|
174
|
+
|
|
175
|
+
最初に失敗した検査で、開始の失敗が返ります。このとき失敗の段階は `Start`、効果は `Unknown` です。期限 clock の検査は、次の条件で失敗します。
|
|
176
|
+
|
|
177
|
+
- 期限があり、clock が空
|
|
178
|
+
- clock の capability が、期限の capability と異なる
|
|
179
|
+
|
|
180
|
+
取り消しの capability の検査は、次の条件で失敗します。
|
|
181
|
+
|
|
182
|
+
- 設定の取り消しの capability があり、port が空
|
|
183
|
+
- 設定の capability が、port の capability と異なる
|
|
184
|
+
|
|
185
|
+
設定の capability と port の capability が一致する入力のみが、worker の開始へ進みます。ここで worker の開始とは、`createAbortController` の呼び出しです。
|
|
186
|
+
|
|
187
|
+
## 期限、取り消し、close
|
|
188
|
+
|
|
189
|
+
アダプターは、期限到達、clock の失敗、取り消しの結果、close のいずれの契機でも、観測を追加した時点で送信中の要求を止めます。その後に worker が得た結果は破棄されます。各契機の観測:
|
|
190
|
+
|
|
191
|
+
| 契機 | 観測 |
|
|
192
|
+
| --- | --- |
|
|
193
|
+
| clock が期限に達した | `DeadlineObservation` |
|
|
194
|
+
| clock の失敗の値か例外 | `FailureObservation` と、返された失敗の値 ( 例外は `HostRuntimeFailure` ) |
|
|
195
|
+
| 取り消しの受理 | `CancellationObservation` |
|
|
196
|
+
| 取り消しの拒否 | `FailureObservation` と `CancellationRejected` |
|
|
197
|
+
| 取り消しの port の失敗の値か例外 | `FailureObservation` と、返された失敗の値 ( 例外は `HostRuntimeFailure` ) |
|
|
198
|
+
| close | `FailureObservation` と `HostClosed` |
|
|
199
|
+
|
|
200
|
+
関数 `observe` は期限への到達を確かめます。期限に関わる観測 ( `DeadlineObservation` と clock の失敗 ) は、一つの試行で一度だけ返ります。
|
|
201
|
+
|
|
202
|
+
取り消しの port が失敗の値を返した場合、port が空の場合、例外を出した場合にも、要求は止まり応答は破棄されます。失敗の段階は、取り消しの失敗と close では `Cancel`、clock の失敗では `Deadline` です。なお、失敗の値が `Timeout` の時は、その値が持つ段階が優先されます。効果の理由は、期限が `Deadline`、取り消しが `Cancellation` です。
|
|
203
|
+
|
|
204
|
+
取り消しの命令は、その権限が取り消し機構の権限と一致した場合に限り機構へ渡され、不一致の場合は取り消しの拒否と同じ観測が返って要求が止まります。一方、関数 `close` は開始済みの試行を 1 件受け取り、渡された試行の記録と送信中の要求だけを閉じて、`HostClosed` を原因とする失敗の観測を含む観測を返します。記録が削除された handle への応答は `UnknownExecution` です。なお、`cancel` と `close` はいずれも記録の待ち行列の先頭にある観測を返すため、先に届いた応答があればその応答が先に返ります。
|
|
205
|
+
|
|
206
|
+
こうして送信中の要求は、期限、取り消し、または close のいずれかによって終了します。リクエストの中断は、`createAbortController` が返す controller の `abort` で行います。
|
|
207
|
+
|
|
208
|
+
## 記録の寿命
|
|
209
|
+
|
|
210
|
+
アダプターは `start` ごとに記録を一つ作ります。記録が削除される契機:
|
|
211
|
+
|
|
212
|
+
- `release` が `dematerialize` を返した時
|
|
213
|
+
- `release` が `retain` を返した後、コアパッケージでその試行が終端に達しており、かつ worker が終わった時
|
|
214
|
+
|
|
215
|
+
関数 `release` は記録の状態だけを進めます。要求自体は、アダプターが観測を追加した時点で止められています。要求を止めた後の観測は、止めた時に追加された一つが最後です。
|
|
216
|
+
|
|
217
|
+
記録が削除された後の handle に対する応答:
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
adapter.observe(started); // { tag: "UnknownExecution", handle }
|
|
221
|
+
adapter.cancel(command); // { tag: "UnknownExecution", handle }
|
|
222
|
+
adapter.close(started); // { tag: "UnknownExecution", handle }
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
未開始の handle に対しても、同じ応答が返ります。
|
|
226
|
+
|
|
227
|
+
関数 `drive` は処理した試行に `release` を呼ぶため、結果が確定すれば記録は削除されています。試行を自分で進める場合は、`release` が破棄を認めるまで記録が残ります。
|
|
228
|
+
|
|
229
|
+
## 検査
|
|
230
|
+
|
|
231
|
+
開発時には、まず上位ディレクトリ `..` をビルドしてください。本リポジトリ内の開発時依存関係はローカルのコアパッケージを参照しますが、公開 npm パッケージとしてはコアパッケージを peer dependency として宣言しています。
|
|
20
232
|
|
|
21
233
|
```bash
|
|
22
234
|
npm ci
|
|
@@ -25,4 +237,6 @@ npm test
|
|
|
25
237
|
npm pack
|
|
26
238
|
```
|
|
27
239
|
|
|
240
|
+
## ライセンス
|
|
241
|
+
|
|
28
242
|
[MIT License](LICENSE)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type CancelCommand, type HostObservation, type MonotonicClockObservation, type SendCommand, type StartObservation } from "necoherence/network-effect";
|
|
1
|
+
import { type CancelCommand, type CancellationCapability, type ExecutionHandle, type HostFailure, type HostObservation, type HostRecordDecision, type MonotonicClockCapability, type MonotonicClockObservation, type NetworkState, type SendCommand, type StartObservation, type StartedAttempt } from "necoherence/network-effect";
|
|
2
|
+
import { semanticExecution } from "necoherence";
|
|
2
3
|
export interface FetchResponseLike {
|
|
3
4
|
readonly status: number;
|
|
4
5
|
readonly headers: {
|
|
@@ -19,10 +20,13 @@ export interface AbortControllerLike {
|
|
|
19
20
|
}
|
|
20
21
|
export type AbortControllerFactory = () => AbortControllerLike;
|
|
21
22
|
export interface MonotonicClockPort {
|
|
22
|
-
|
|
23
|
+
capability: () => MonotonicClockCapability;
|
|
24
|
+
observe: () => MonotonicClockObservation | HostFailure;
|
|
25
|
+
until?: (limit: bigint) => number | undefined;
|
|
23
26
|
}
|
|
24
27
|
export interface CancellationPort {
|
|
25
|
-
|
|
28
|
+
capability: () => CancellationCapability;
|
|
29
|
+
request: (handle: ExecutionHandle) => undefined | HostFailure;
|
|
26
30
|
}
|
|
27
31
|
export interface FetchHttpAdapterOptions {
|
|
28
32
|
readonly fetch?: FetchLike;
|
|
@@ -30,15 +34,40 @@ export interface FetchHttpAdapterOptions {
|
|
|
30
34
|
readonly monotonicClock?: MonotonicClockPort;
|
|
31
35
|
readonly cancellation?: CancellationPort;
|
|
32
36
|
}
|
|
37
|
+
export interface UnknownExecution {
|
|
38
|
+
readonly tag: "UnknownExecution";
|
|
39
|
+
readonly handle: {
|
|
40
|
+
readonly value: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface DeadlineWaitUnavailable {
|
|
44
|
+
readonly tag: "DeadlineWaitUnavailable";
|
|
45
|
+
readonly handle: {
|
|
46
|
+
readonly value: string;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
33
49
|
export declare class FetchHttpAdapter {
|
|
34
50
|
#private;
|
|
35
51
|
constructor(options?: FetchHttpAdapterOptions);
|
|
36
52
|
start(command: SendCommand): StartObservation;
|
|
37
|
-
observe(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
53
|
+
observe(started: StartedAttempt): HostObservation | UnknownExecution;
|
|
54
|
+
cancel(command: CancelCommand): HostObservation | UnknownExecution;
|
|
55
|
+
close(started: StartedAttempt): HostObservation | UnknownExecution;
|
|
56
|
+
release(state: NetworkState, started: StartedAttempt): HostRecordDecision;
|
|
57
|
+
}
|
|
58
|
+
export interface Driven {
|
|
59
|
+
readonly result: semanticExecution.ExecutionResult;
|
|
60
|
+
readonly retained: readonly StartedAttempt[];
|
|
61
|
+
}
|
|
62
|
+
export interface DriveFailure {
|
|
63
|
+
readonly cause: semanticExecution.RunFailure | UnknownExecution | DeadlineWaitUnavailable;
|
|
64
|
+
readonly retained: readonly StartedAttempt[];
|
|
44
65
|
}
|
|
66
|
+
export type DriveResult = {
|
|
67
|
+
readonly tag: "Ok";
|
|
68
|
+
readonly value: Driven;
|
|
69
|
+
} | {
|
|
70
|
+
readonly tag: "Failed";
|
|
71
|
+
readonly failure: DriveFailure;
|
|
72
|
+
};
|
|
73
|
+
export declare function drive(adapter: FetchHttpAdapter, first: semanticExecution.Progress): Promise<DriveResult>;
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 barineco
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
-
import { sourceNone, sourceSome, } from "necoherence/network-effect";
|
|
4
|
-
|
|
3
|
+
import { emptyNetworkState, hostSettled, prepare, releaseHostRecord, sourceNone, sourceSome, } from "necoherence/network-effect";
|
|
4
|
+
import { semanticExecution } from "necoherence";
|
|
5
|
+
import { registerRecords } from "./records.js";
|
|
6
|
+
const protocolWitnessMissingEffect = Object.freeze({
|
|
5
7
|
tag: "Unknown",
|
|
6
|
-
reason: "
|
|
8
|
+
reason: "ProtocolWitnessMissing",
|
|
9
|
+
});
|
|
10
|
+
const hostFailureEffect = Object.freeze({
|
|
11
|
+
tag: "Unknown",
|
|
12
|
+
reason: "HostFailure",
|
|
13
|
+
});
|
|
14
|
+
const cancellationEffect = Object.freeze({
|
|
15
|
+
tag: "Unknown",
|
|
16
|
+
reason: "Cancellation",
|
|
17
|
+
});
|
|
18
|
+
const deadlineEffect = Object.freeze({
|
|
19
|
+
tag: "Unknown",
|
|
20
|
+
reason: "Deadline",
|
|
7
21
|
});
|
|
8
22
|
const frozen = (value) => Object.freeze(value);
|
|
9
23
|
const defaultFetch = async (target, options) => {
|
|
@@ -33,8 +47,18 @@ function failure(command, stage, cause) {
|
|
|
33
47
|
cause,
|
|
34
48
|
});
|
|
35
49
|
}
|
|
50
|
+
function isHostFailure(value) {
|
|
51
|
+
return value !== null && typeof value === "object" && typeof value.tag === "string";
|
|
52
|
+
}
|
|
53
|
+
function stageForCause(fallback, cause) {
|
|
54
|
+
return cause.tag === "Timeout" ? cause.stage : fallback;
|
|
55
|
+
}
|
|
56
|
+
function sameClockCapability(left, right) {
|
|
57
|
+
return left.identity.value === right.identity.value
|
|
58
|
+
&& left.clockBasis.value === right.clockBasis.value;
|
|
59
|
+
}
|
|
36
60
|
function textBytes(value) {
|
|
37
|
-
return
|
|
61
|
+
return new TextEncoder().encode(value);
|
|
38
62
|
}
|
|
39
63
|
function headersOf(response) {
|
|
40
64
|
const fields = [];
|
|
@@ -44,12 +68,12 @@ function headersOf(response) {
|
|
|
44
68
|
return Object.freeze(fields);
|
|
45
69
|
}
|
|
46
70
|
function bodyOf(buffer) {
|
|
47
|
-
return
|
|
71
|
+
return new Uint8Array(buffer);
|
|
48
72
|
}
|
|
49
73
|
function requestHeadersOf(command) {
|
|
50
74
|
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
51
75
|
return Object.freeze(command.prepared.input.instruction.headers.map((header) => {
|
|
52
|
-
const value = decoder.decode(
|
|
76
|
+
const value = decoder.decode(header.value);
|
|
53
77
|
if (/[\u0000\r\n]/.test(value))
|
|
54
78
|
throw new TypeError("request header value is unavailable");
|
|
55
79
|
return Object.freeze([header.name, value]);
|
|
@@ -100,7 +124,7 @@ function classifyTimeout(error, phase) {
|
|
|
100
124
|
cause: frozen({ tag: "Timeout", stage, cause }),
|
|
101
125
|
});
|
|
102
126
|
}
|
|
103
|
-
function classifyFetchFailure(error, hasRequestBody) {
|
|
127
|
+
function classifyFetchFailure(error, hasRequestBody, current) {
|
|
104
128
|
const timeout = classifyTimeout(error, "Fetch");
|
|
105
129
|
if (timeout !== undefined)
|
|
106
130
|
return timeout;
|
|
@@ -149,10 +173,11 @@ function classifyFetchFailure(error, hasRequestBody) {
|
|
|
149
173
|
});
|
|
150
174
|
}
|
|
151
175
|
return frozen({
|
|
152
|
-
stage:
|
|
176
|
+
stage: current,
|
|
153
177
|
cause: frozen({ tag: "HostRuntimeFailure" }),
|
|
154
178
|
});
|
|
155
179
|
}
|
|
180
|
+
const internals = new WeakMap();
|
|
156
181
|
export class FetchHttpAdapter {
|
|
157
182
|
#fetch;
|
|
158
183
|
#createAbortController;
|
|
@@ -166,39 +191,62 @@ export class FetchHttpAdapter {
|
|
|
166
191
|
?? (() => new AbortController());
|
|
167
192
|
this.#clock = options.monotonicClock;
|
|
168
193
|
this.#cancellation = options.cancellation;
|
|
194
|
+
registerRecords(this, this.#workers);
|
|
195
|
+
internals.set(this, {
|
|
196
|
+
waitObservation: (started) => this.#waitObservation(started),
|
|
197
|
+
});
|
|
169
198
|
}
|
|
170
199
|
start(command) {
|
|
171
200
|
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
201
|
if (!/^https?:\/\//i.test(input.instruction.target)) {
|
|
189
202
|
return frozen({
|
|
190
203
|
tag: "StartFailed",
|
|
191
204
|
failure: failure(command, "Start", frozen({ tag: "UnsupportedUriScheme" })),
|
|
192
|
-
effect:
|
|
205
|
+
effect: hostFailureEffect,
|
|
193
206
|
});
|
|
194
207
|
}
|
|
195
|
-
|
|
208
|
+
const root = { ...input, attempt: { ...input.attempt, ordinal: 0n } };
|
|
209
|
+
if (prepare(emptyNetworkState(), root).tag !== "Prepared") {
|
|
196
210
|
return frozen({
|
|
197
211
|
tag: "StartFailed",
|
|
198
|
-
failure: failure(command, "Start", frozen({ tag: "
|
|
199
|
-
effect:
|
|
212
|
+
failure: failure(command, "Start", frozen({ tag: "InvalidHostConfiguration" })),
|
|
213
|
+
effect: hostFailureEffect,
|
|
200
214
|
});
|
|
201
215
|
}
|
|
216
|
+
if (input.deadline !== null) {
|
|
217
|
+
const clockMatch = this.#clockCapabilitiesMatch(input);
|
|
218
|
+
if (clockMatch === "Failed") {
|
|
219
|
+
return frozen({
|
|
220
|
+
tag: "StartFailed",
|
|
221
|
+
failure: failure(command, "Start", frozen({ tag: "HostRuntimeFailure" })),
|
|
222
|
+
effect: hostFailureEffect,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (!clockMatch) {
|
|
226
|
+
return frozen({
|
|
227
|
+
tag: "StartFailed",
|
|
228
|
+
failure: failure(command, "Start", frozen({ tag: "DeadlineControlUnavailable" })),
|
|
229
|
+
effect: hostFailureEffect,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (input.configuration.cancellation !== null) {
|
|
234
|
+
const cancellationMatch = this.#cancellationCapabilityMatches(input.configuration.cancellation);
|
|
235
|
+
if (cancellationMatch === "Failed") {
|
|
236
|
+
return frozen({
|
|
237
|
+
tag: "StartFailed",
|
|
238
|
+
failure: failure(command, "Start", frozen({ tag: "HostRuntimeFailure" })),
|
|
239
|
+
effect: hostFailureEffect,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
if (!cancellationMatch) {
|
|
243
|
+
return frozen({
|
|
244
|
+
tag: "StartFailed",
|
|
245
|
+
failure: failure(command, "Start", frozen({ tag: "InvalidHostConfiguration" })),
|
|
246
|
+
effect: hostFailureEffect,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
202
250
|
let controller;
|
|
203
251
|
try {
|
|
204
252
|
controller = this.#createAbortController();
|
|
@@ -207,7 +255,7 @@ export class FetchHttpAdapter {
|
|
|
207
255
|
return frozen({
|
|
208
256
|
tag: "StartFailed",
|
|
209
257
|
failure: failure(command, "Start", frozen({ tag: "ExecutionStartFailed" })),
|
|
210
|
-
effect:
|
|
258
|
+
effect: hostFailureEffect,
|
|
211
259
|
});
|
|
212
260
|
}
|
|
213
261
|
const handle = frozen({
|
|
@@ -222,26 +270,123 @@ export class FetchHttpAdapter {
|
|
|
222
270
|
delivery: 0n,
|
|
223
271
|
closed: false,
|
|
224
272
|
sourceSequence: 0n,
|
|
225
|
-
|
|
273
|
+
interrupted: false,
|
|
226
274
|
deadlineQueued: false,
|
|
275
|
+
stage: "RequestWrite",
|
|
276
|
+
lifecycle: "Running",
|
|
277
|
+
waiters: [],
|
|
227
278
|
};
|
|
228
279
|
this.#workers.set(handle.value, worker);
|
|
229
280
|
queueMicrotask(() => { void this.#run(worker); });
|
|
230
281
|
return frozen({ tag: "Started", attempt: input.attempt, handle });
|
|
231
282
|
}
|
|
232
|
-
|
|
233
|
-
|
|
283
|
+
#clockCapabilitiesMatch(input) {
|
|
284
|
+
if (this.#clock === undefined || input.deadline === null)
|
|
285
|
+
return false;
|
|
286
|
+
try {
|
|
287
|
+
const port = this.#clock.capability();
|
|
288
|
+
return sameClockCapability(port, input.deadline.capability);
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
return "Failed";
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
#cancellationCapabilityMatches(configured) {
|
|
295
|
+
if (this.#cancellation === undefined)
|
|
296
|
+
return false;
|
|
297
|
+
try {
|
|
298
|
+
return this.#cancellation.capability().identity.value === configured.identity.value;
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
return "Failed";
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
observe(started) {
|
|
305
|
+
const worker = this.#workers.get(started.handle.value);
|
|
234
306
|
if (worker === undefined) {
|
|
235
|
-
return frozen({
|
|
236
|
-
tag: "Pending",
|
|
237
|
-
attempt: {
|
|
238
|
-
logical: { kind: "", provenance: "", revision: 0n },
|
|
239
|
-
ordinal: 0n,
|
|
240
|
-
},
|
|
241
|
-
handle,
|
|
242
|
-
});
|
|
307
|
+
return frozen({ tag: "UnknownExecution", handle: started.handle });
|
|
243
308
|
}
|
|
244
309
|
this.#queueDeadline(worker);
|
|
310
|
+
return this.#deliver(worker);
|
|
311
|
+
}
|
|
312
|
+
cancel(command) {
|
|
313
|
+
const worker = this.#workers.get(command.handle.value);
|
|
314
|
+
if (worker === undefined) {
|
|
315
|
+
return frozen({ tag: "UnknownExecution", handle: command.handle });
|
|
316
|
+
}
|
|
317
|
+
if (worker.closed)
|
|
318
|
+
return this.#deliver(worker);
|
|
319
|
+
if (this.#cancellation === undefined) {
|
|
320
|
+
this.#publishFailure(worker, "Cancel", frozen({ tag: "CancellationRejected" }));
|
|
321
|
+
return this.#deliver(worker);
|
|
322
|
+
}
|
|
323
|
+
const cancellationMatch = this.#cancellationCapabilityMatches(command.capability);
|
|
324
|
+
if (cancellationMatch === "Failed") {
|
|
325
|
+
this.#publishFailure(worker, "Cancel", frozen({ tag: "HostRuntimeFailure" }));
|
|
326
|
+
return this.#deliver(worker);
|
|
327
|
+
}
|
|
328
|
+
if (!cancellationMatch) {
|
|
329
|
+
this.#publishFailure(worker, "Cancel", frozen({ tag: "CancellationRejected" }));
|
|
330
|
+
return this.#deliver(worker);
|
|
331
|
+
}
|
|
332
|
+
let requested;
|
|
333
|
+
try {
|
|
334
|
+
requested = this.#cancellation.request(command.handle);
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
this.#publishFailure(worker, "Cancel", frozen({ tag: "HostRuntimeFailure" }));
|
|
338
|
+
return this.#deliver(worker);
|
|
339
|
+
}
|
|
340
|
+
if (requested !== undefined) {
|
|
341
|
+
const cause = isHostFailure(requested)
|
|
342
|
+
? requested
|
|
343
|
+
: frozen({ tag: "HostRuntimeFailure" });
|
|
344
|
+
this.#publishFailure(worker, stageForCause("Cancel", cause), cause);
|
|
345
|
+
return this.#deliver(worker);
|
|
346
|
+
}
|
|
347
|
+
this.#publish(worker, frozen({
|
|
348
|
+
tag: "CancellationObservation",
|
|
349
|
+
attempt: command.attempt,
|
|
350
|
+
handle: command.handle,
|
|
351
|
+
sourceSequence: sourceNone(),
|
|
352
|
+
effect: cancellationEffect,
|
|
353
|
+
}));
|
|
354
|
+
return this.#deliver(worker);
|
|
355
|
+
}
|
|
356
|
+
close(started) {
|
|
357
|
+
const worker = this.#workers.get(started.handle.value);
|
|
358
|
+
if (worker === undefined) {
|
|
359
|
+
return frozen({ tag: "UnknownExecution", handle: started.handle });
|
|
360
|
+
}
|
|
361
|
+
if (worker.closed)
|
|
362
|
+
return this.#deliver(worker);
|
|
363
|
+
worker.closed = true;
|
|
364
|
+
this.#publishFailure(worker, "Cancel", frozen({ tag: "HostClosed" }));
|
|
365
|
+
return this.#deliver(worker);
|
|
366
|
+
}
|
|
367
|
+
release(state, started) {
|
|
368
|
+
const worker = this.#workers.get(started.handle.value);
|
|
369
|
+
if (worker === undefined) {
|
|
370
|
+
return releaseHostRecord(state, () => false, started.prepared.input.attempt);
|
|
371
|
+
}
|
|
372
|
+
const decision = releaseHostRecord(state, () => worker.lifecycle === "Running" || worker.lifecycle === "RunningSettled", worker.command.prepared.input.attempt);
|
|
373
|
+
if (decision.kind === "dematerialize") {
|
|
374
|
+
worker.lifecycle = "Released";
|
|
375
|
+
this.#detach(worker);
|
|
376
|
+
}
|
|
377
|
+
else if (decision.kind === "retain"
|
|
378
|
+
&& hostSettled(state, worker.command.prepared.input.attempt)) {
|
|
379
|
+
if (worker.lifecycle === "Running") {
|
|
380
|
+
worker.lifecycle = "RunningSettled";
|
|
381
|
+
}
|
|
382
|
+
else if (worker.lifecycle === "Finished") {
|
|
383
|
+
worker.lifecycle = "Released";
|
|
384
|
+
this.#detach(worker);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return decision;
|
|
388
|
+
}
|
|
389
|
+
#deliver(worker) {
|
|
245
390
|
const observation = worker.queue.shift();
|
|
246
391
|
if (observation === undefined) {
|
|
247
392
|
return frozen({
|
|
@@ -254,51 +399,99 @@ export class FetchHttpAdapter {
|
|
|
254
399
|
worker.delivery += 1n;
|
|
255
400
|
return frozen({ tag: "Delivered", delivery, observation });
|
|
256
401
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
402
|
+
#detach(worker) {
|
|
403
|
+
this.#workers.delete(worker.handle.value);
|
|
404
|
+
this.#wake(worker);
|
|
405
|
+
}
|
|
406
|
+
#push(worker, observation) {
|
|
407
|
+
worker.queue.push(observation);
|
|
408
|
+
this.#wake(worker);
|
|
409
|
+
}
|
|
410
|
+
#publish(worker, observation) {
|
|
411
|
+
worker.queue.push(observation);
|
|
412
|
+
worker.interrupted = true;
|
|
413
|
+
worker.controller.abort();
|
|
414
|
+
this.#wake(worker);
|
|
415
|
+
}
|
|
416
|
+
#publishFailure(worker, stage, cause) {
|
|
417
|
+
this.#publish(worker, this.#failureObservation(worker, stage, cause));
|
|
418
|
+
}
|
|
419
|
+
#wake(worker) {
|
|
420
|
+
for (const resume of worker.waiters.splice(0))
|
|
421
|
+
resume();
|
|
422
|
+
}
|
|
423
|
+
async #waitUntil(worker, ready) {
|
|
424
|
+
while (!ready()) {
|
|
425
|
+
await new Promise((resolve) => { worker.waiters.push(resolve); });
|
|
264
426
|
}
|
|
427
|
+
}
|
|
428
|
+
async #waitFor(worker, ready, limitMs) {
|
|
429
|
+
if (ready())
|
|
430
|
+
return;
|
|
431
|
+
await new Promise((resolve) => {
|
|
432
|
+
const wake = () => {
|
|
433
|
+
clearTimeout(timer);
|
|
434
|
+
const index = worker.waiters.indexOf(wake);
|
|
435
|
+
if (index >= 0)
|
|
436
|
+
worker.waiters.splice(index, 1);
|
|
437
|
+
resolve();
|
|
438
|
+
};
|
|
439
|
+
const timer = setTimeout(wake, limitMs);
|
|
440
|
+
worker.waiters.push(wake);
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
#deadlineWait(worker) {
|
|
444
|
+
const deadline = worker.command.prepared.input.deadline;
|
|
445
|
+
if (deadline === null || worker.deadlineQueued)
|
|
446
|
+
return undefined;
|
|
447
|
+
let length;
|
|
265
448
|
try {
|
|
266
|
-
|
|
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
|
-
}));
|
|
449
|
+
length = this.#clock?.until?.(deadline.limit);
|
|
280
450
|
}
|
|
281
451
|
catch {
|
|
282
|
-
this.#
|
|
452
|
+
this.#queueClockFailure(worker);
|
|
453
|
+
return undefined;
|
|
283
454
|
}
|
|
455
|
+
if (length === undefined)
|
|
456
|
+
return "unavailable";
|
|
457
|
+
return Math.max(length, 1);
|
|
284
458
|
}
|
|
285
|
-
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
459
|
+
async #waitObservation(started) {
|
|
460
|
+
const handle = started.handle;
|
|
461
|
+
for (;;) {
|
|
462
|
+
const observed = this.observe(started);
|
|
463
|
+
if (observed.tag !== "Pending")
|
|
464
|
+
return observed;
|
|
465
|
+
const worker = this.#workers.get(handle.value);
|
|
466
|
+
if (worker === undefined)
|
|
467
|
+
return frozen({ tag: "UnknownExecution", handle });
|
|
468
|
+
const ready = () => worker.queue.length > 0 || worker.lifecycle === "Released";
|
|
469
|
+
const bound = this.#deadlineWait(worker);
|
|
470
|
+
if (bound === "unavailable")
|
|
471
|
+
return frozen({ tag: "DeadlineWaitUnavailable", handle });
|
|
472
|
+
if (bound === undefined)
|
|
473
|
+
await this.#waitUntil(worker, ready);
|
|
474
|
+
else
|
|
475
|
+
await this.#waitFor(worker, ready, bound);
|
|
476
|
+
}
|
|
292
477
|
}
|
|
293
|
-
#
|
|
294
|
-
|
|
478
|
+
#failureObservation(worker, stage, cause) {
|
|
479
|
+
return frozen({
|
|
295
480
|
tag: "FailureObservation",
|
|
296
481
|
attempt: worker.command.prepared.input.attempt,
|
|
297
482
|
handle: worker.handle,
|
|
298
483
|
sourceSequence: sourceNone(),
|
|
299
484
|
failure: failure(worker.command, stage, cause),
|
|
300
|
-
effect:
|
|
301
|
-
})
|
|
485
|
+
effect: hostFailureEffect,
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
#queueResult(worker, observation) {
|
|
489
|
+
if (this.#interrupted(worker))
|
|
490
|
+
return;
|
|
491
|
+
this.#push(worker, observation);
|
|
492
|
+
}
|
|
493
|
+
#queueFailure(worker, stage, cause) {
|
|
494
|
+
this.#queueResult(worker, this.#failureObservation(worker, stage, cause));
|
|
302
495
|
}
|
|
303
496
|
#queueDeadline(worker) {
|
|
304
497
|
const deadline = worker.command.prepared.input.deadline;
|
|
@@ -306,27 +499,63 @@ export class FetchHttpAdapter {
|
|
|
306
499
|
return;
|
|
307
500
|
try {
|
|
308
501
|
const observedAt = this.#clock.observe();
|
|
502
|
+
if (isHostFailure(observedAt)) {
|
|
503
|
+
this.#queueClockFailure(worker, observedAt);
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
309
506
|
if (observedAt.clockBasis.value === deadline.clockBasis.value
|
|
310
507
|
&& observedAt.value >= deadline.limit) {
|
|
311
508
|
worker.deadlineQueued = true;
|
|
312
|
-
worker
|
|
509
|
+
this.#publish(worker, frozen({
|
|
313
510
|
tag: "DeadlineObservation",
|
|
314
511
|
attempt: worker.command.prepared.input.attempt,
|
|
315
512
|
handle: worker.handle,
|
|
316
513
|
sourceSequence: sourceNone(),
|
|
317
514
|
deadline,
|
|
318
515
|
observedAt,
|
|
319
|
-
effect:
|
|
516
|
+
effect: deadlineEffect,
|
|
320
517
|
}));
|
|
321
518
|
}
|
|
322
519
|
}
|
|
323
520
|
catch {
|
|
324
|
-
worker
|
|
325
|
-
this.#queueFailure(worker, "Deadline", frozen({ tag: "DeadlineControlUnavailable" }));
|
|
521
|
+
this.#queueClockFailure(worker);
|
|
326
522
|
}
|
|
327
523
|
}
|
|
524
|
+
#queueClockFailure(worker, cause = frozen({ tag: "HostRuntimeFailure" })) {
|
|
525
|
+
if (worker.deadlineQueued)
|
|
526
|
+
return;
|
|
527
|
+
worker.deadlineQueued = true;
|
|
528
|
+
this.#publishFailure(worker, stageForCause("Deadline", cause), cause);
|
|
529
|
+
}
|
|
530
|
+
#interrupted(worker) {
|
|
531
|
+
return worker.interrupted;
|
|
532
|
+
}
|
|
328
533
|
async #run(worker) {
|
|
534
|
+
try {
|
|
535
|
+
await this.#runRequest(worker);
|
|
536
|
+
}
|
|
537
|
+
catch {
|
|
538
|
+
if (!this.#interrupted(worker)) {
|
|
539
|
+
this.#queueFailure(worker, worker.stage, frozen({ tag: "HostRuntimeFailure" }));
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
finally {
|
|
543
|
+
if (worker.lifecycle === "RunningSettled") {
|
|
544
|
+
worker.lifecycle = "Released";
|
|
545
|
+
this.#detach(worker);
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
if (worker.lifecycle === "Running")
|
|
549
|
+
worker.lifecycle = "Finished";
|
|
550
|
+
this.#wake(worker);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
async #runRequest(worker) {
|
|
329
555
|
const input = worker.command.prepared.input;
|
|
556
|
+
this.#queueDeadline(worker);
|
|
557
|
+
if (worker.deadlineQueued)
|
|
558
|
+
return;
|
|
330
559
|
let headers;
|
|
331
560
|
try {
|
|
332
561
|
headers = requestHeadersOf(worker.command);
|
|
@@ -337,29 +566,36 @@ export class FetchHttpAdapter {
|
|
|
337
566
|
}
|
|
338
567
|
let response;
|
|
339
568
|
try {
|
|
340
|
-
|
|
569
|
+
const options = {
|
|
341
570
|
method: input.instruction.method,
|
|
342
571
|
headers,
|
|
343
|
-
body:
|
|
572
|
+
body: input.instruction.body,
|
|
344
573
|
signal: worker.controller.signal,
|
|
345
|
-
}
|
|
574
|
+
};
|
|
575
|
+
worker.stage = "ResponseHeaders";
|
|
576
|
+
response = await this.#fetch(input.instruction.target, options);
|
|
346
577
|
}
|
|
347
578
|
catch (error) {
|
|
348
|
-
if (worker
|
|
579
|
+
if (this.#interrupted(worker))
|
|
349
580
|
return;
|
|
350
|
-
const classified = classifyFetchFailure(error, input.instruction.body.length > 0);
|
|
581
|
+
const classified = classifyFetchFailure(error, input.instruction.body.length > 0, worker.stage);
|
|
351
582
|
this.#queueFailure(worker, classified.stage, classified.cause);
|
|
352
583
|
return;
|
|
353
584
|
}
|
|
585
|
+
if (this.#interrupted(worker))
|
|
586
|
+
return;
|
|
354
587
|
if (!Number.isInteger(response.status) || response.status < 100 || response.status > 999) {
|
|
355
588
|
this.#queueFailure(worker, "ResponseHeaders", frozen({ tag: "MalformedResponse" }));
|
|
356
589
|
return;
|
|
357
590
|
}
|
|
591
|
+
worker.stage = "ResponseBody";
|
|
358
592
|
let body;
|
|
359
593
|
try {
|
|
360
594
|
body = bodyOf(await response.arrayBuffer());
|
|
361
595
|
}
|
|
362
596
|
catch (error) {
|
|
597
|
+
if (this.#interrupted(worker))
|
|
598
|
+
return;
|
|
363
599
|
const timeout = classifyTimeout(error, "ResponseBody");
|
|
364
600
|
if (timeout !== undefined) {
|
|
365
601
|
this.#queueFailure(worker, timeout.stage, timeout.cause);
|
|
@@ -369,6 +605,8 @@ export class FetchHttpAdapter {
|
|
|
369
605
|
}
|
|
370
606
|
return;
|
|
371
607
|
}
|
|
608
|
+
if (this.#interrupted(worker))
|
|
609
|
+
return;
|
|
372
610
|
if (BigInt(body.length) > input.configuration.responseBodyLimit) {
|
|
373
611
|
this.#queueFailure(worker, "ResponseBody", frozen({ tag: "ResponseBodyLimitExceeded" }));
|
|
374
612
|
return;
|
|
@@ -381,7 +619,7 @@ export class FetchHttpAdapter {
|
|
|
381
619
|
this.#queueFailure(worker, "ResponseHeaders", frozen({ tag: "HeaderRepresentationUnavailable" }));
|
|
382
620
|
return;
|
|
383
621
|
}
|
|
384
|
-
worker
|
|
622
|
+
this.#queueResult(worker, frozen({
|
|
385
623
|
tag: "ResponseObservation",
|
|
386
624
|
attempt: input.attempt,
|
|
387
625
|
handle: worker.handle,
|
|
@@ -392,8 +630,74 @@ export class FetchHttpAdapter {
|
|
|
392
630
|
body,
|
|
393
631
|
}),
|
|
394
632
|
provenance: "LibraryNormalized",
|
|
395
|
-
effect:
|
|
633
|
+
effect: protocolWitnessMissingEffect,
|
|
396
634
|
}));
|
|
397
635
|
worker.sourceSequence += 1n;
|
|
398
636
|
}
|
|
399
637
|
}
|
|
638
|
+
function released(decision) {
|
|
639
|
+
return decision.kind === "dematerialize";
|
|
640
|
+
}
|
|
641
|
+
function sweep(adapter, state, candidates) {
|
|
642
|
+
return candidates.filter((started) => !released(adapter.release(state, started)));
|
|
643
|
+
}
|
|
644
|
+
async function finish(adapter, state, candidates) {
|
|
645
|
+
const remaining = [];
|
|
646
|
+
for (const started of candidates) {
|
|
647
|
+
const decision = adapter.release(state, started);
|
|
648
|
+
if (released(decision))
|
|
649
|
+
continue;
|
|
650
|
+
if (decision.kind === "retain"
|
|
651
|
+
&& hostSettled(state, started.prepared.input.attempt)) {
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
remaining.push(started);
|
|
655
|
+
}
|
|
656
|
+
return remaining;
|
|
657
|
+
}
|
|
658
|
+
export async function drive(adapter, first) {
|
|
659
|
+
const waiting = internals.get(adapter);
|
|
660
|
+
if (waiting === undefined)
|
|
661
|
+
throw new TypeError("drive requires a FetchHttpAdapter");
|
|
662
|
+
let candidates = [];
|
|
663
|
+
let progress = first;
|
|
664
|
+
for (;;) {
|
|
665
|
+
if (progress.tag === "Completed") {
|
|
666
|
+
const retained = await finish(adapter, progress.network, candidates);
|
|
667
|
+
return frozen({ tag: "Ok", value: frozen({ result: progress.result, retained }) });
|
|
668
|
+
}
|
|
669
|
+
const instruction = progress.instruction;
|
|
670
|
+
let observation;
|
|
671
|
+
let handled = null;
|
|
672
|
+
if (instruction.tag === "Send") {
|
|
673
|
+
const start = adapter.start(instruction.command);
|
|
674
|
+
if (start.tag === "Started") {
|
|
675
|
+
handled = { prepared: instruction.command.prepared, handle: start.handle };
|
|
676
|
+
}
|
|
677
|
+
observation = { tag: "Start", observation: start };
|
|
678
|
+
}
|
|
679
|
+
else {
|
|
680
|
+
handled = instruction.started;
|
|
681
|
+
const host = await waiting.waitObservation(instruction.started);
|
|
682
|
+
if ("tag" in host && host.tag === "UnknownExecution") {
|
|
683
|
+
const retained = await finish(adapter, progress.execution.network, candidates);
|
|
684
|
+
return frozen({ tag: "Failed", failure: frozen({ cause: host, retained }) });
|
|
685
|
+
}
|
|
686
|
+
if ("tag" in host && host.tag === "DeadlineWaitUnavailable") {
|
|
687
|
+
const retained = await finish(adapter, progress.execution.network, candidates);
|
|
688
|
+
return frozen({ tag: "Failed", failure: frozen({ cause: host, retained }) });
|
|
689
|
+
}
|
|
690
|
+
observation = { tag: "Host", observation: host };
|
|
691
|
+
}
|
|
692
|
+
const resumed = semanticExecution.resume(progress.execution, observation);
|
|
693
|
+
if (handled !== null && !candidates.some((known) => known.handle.value === handled?.handle.value)) {
|
|
694
|
+
candidates.push(handled);
|
|
695
|
+
}
|
|
696
|
+
if (resumed.tag === "Failed") {
|
|
697
|
+
const retained = await finish(adapter, resumed.failure.network, candidates);
|
|
698
|
+
return frozen({ tag: "Failed", failure: frozen({ cause: resumed.failure, retained }) });
|
|
699
|
+
}
|
|
700
|
+
progress = resumed.value;
|
|
701
|
+
candidates = sweep(adapter, progress.tag === "Completed" ? progress.network : progress.execution.network, candidates);
|
|
702
|
+
}
|
|
703
|
+
}
|
package/dist/records.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 barineco
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
const registries = new WeakMap();
|
|
4
|
+
export function registerRecords(owner, records) {
|
|
5
|
+
registries.set(owner, records);
|
|
6
|
+
}
|
|
7
|
+
export function recordCount(owner) {
|
|
8
|
+
return registries.get(owner)?.size ?? 0;
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "necoherence-http-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "HTTP host adapter for Necoherence",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -13,28 +13,30 @@
|
|
|
13
13
|
"files": [
|
|
14
14
|
"dist/",
|
|
15
15
|
"README.md",
|
|
16
|
-
"README-ja.md",
|
|
17
16
|
"LICENSE"
|
|
18
17
|
],
|
|
19
18
|
"scripts": {
|
|
20
19
|
"build": "tsc --project tsconfig.build.json",
|
|
21
20
|
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
21
|
+
"lint": "eslint src",
|
|
22
22
|
"pretest": "npm run build",
|
|
23
23
|
"test": "vitest run --fileParallelism=false"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"necoherence": "0.
|
|
26
|
+
"necoherence": "0.4.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"necoherence": "file:../typescript",
|
|
30
29
|
"@types/node": "^26.1.2",
|
|
30
|
+
"eslint": "^9.39.5",
|
|
31
|
+
"necoherence": "file:..",
|
|
31
32
|
"typescript": "^5.7.3",
|
|
33
|
+
"typescript-eslint": "^8.72.1",
|
|
32
34
|
"vitest": "^2.1.8"
|
|
33
35
|
},
|
|
34
36
|
"repository": {
|
|
35
37
|
"type": "git",
|
|
36
38
|
"url": "git+https://github.com/barineco/necoherence.git",
|
|
37
|
-
"directory": "typescript
|
|
39
|
+
"directory": "packages/typescript/adapter/http"
|
|
38
40
|
},
|
|
39
41
|
"homepage": "https://github.com/barineco/necoherence",
|
|
40
42
|
"bugs": {
|
package/README-ja.md
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
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.2 necoherence-http-adapter@0.2.2
|
|
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)
|