@interactive-inc/flume 0.9.4 → 0.10.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/README.md +3 -2
- package/dist/discord.d.ts +15 -1
- package/dist/discord.js +302 -33
- package/dist/flume-source.d.ts +95 -12
- package/dist/flume-source.js +141 -68
- package/dist/github.d.ts +5 -1
- package/dist/github.js +211 -61
- package/dist/http-error.js +4 -0
- package/dist/index.d.ts +138 -23
- package/dist/index.js +358 -72
- package/dist/safe-json-parse.js +1 -1
- package/dist/safe-read-text.js +6 -3
- package/dist/safe-stringify.js +64 -28
- package/dist/slack.d.ts +8 -1
- package/dist/slack.js +227 -30
- package/dist/time.d.ts +46 -4
- package/dist/time.js +342 -40
- package/package.json +1 -1
package/dist/safe-json-parse.js
CHANGED
package/dist/safe-read-text.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as attempt, u as safeErrorMessage } from "./flume-source.js";
|
|
2
2
|
import { t as FlumeHttpError } from "./http-error.js";
|
|
3
3
|
//#region lib/utils/safe-read-text.ts
|
|
4
4
|
/**
|
|
5
5
|
* `response.text()` を保護する。body 読み取り中の reject (接続切断 / 解凍失敗 / 二重消費) を
|
|
6
|
-
* `FlumeHttpError` (status / cause 保持) に変換する。
|
|
6
|
+
* `FlumeHttpError` (status / cause 保持) に変換する。DI モックの `status` getter が throw
|
|
7
|
+
* しても reject しない。log には書かない (呼び出し側で書く)
|
|
7
8
|
*/
|
|
8
9
|
async function safeReadText(props) {
|
|
9
10
|
try {
|
|
10
11
|
return await props.response.text();
|
|
11
12
|
} catch (err) {
|
|
13
|
+
const statusResult = attempt(() => props.response.status);
|
|
14
|
+
const status = typeof statusResult === "number" ? statusResult : 0;
|
|
12
15
|
return new FlumeHttpError({
|
|
13
16
|
message: `${props.context}: failed to read body: ${safeErrorMessage({ error: err })}`,
|
|
14
|
-
status
|
|
17
|
+
status,
|
|
15
18
|
cause: err
|
|
16
19
|
});
|
|
17
20
|
}
|
package/dist/safe-stringify.js
CHANGED
|
@@ -1,32 +1,39 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as attempt, r as safeInvokeCallback, s as FlumeParseError, u as safeErrorMessage } from "./flume-source.js";
|
|
2
2
|
import { t as FlumeConnectionError } from "./connection-error.js";
|
|
3
3
|
//#region lib/utils/safe-random.ts
|
|
4
4
|
/**
|
|
5
|
-
* `deps.random()` を保護する。throw / 範囲外値 / 非数値が返った場合は
|
|
6
|
-
* 0 以上 1 未満 (Math.random と同等)
|
|
5
|
+
* `deps.random()` を保護する。throw / 範囲外値 / 非数値が返った場合は `Math.random()` へ
|
|
6
|
+
* フォールバックする。0 以上 1 未満 (Math.random と同等) の値のみそのまま透過。
|
|
7
|
+
* `Math.random` 自体まで壊れている病的環境でのみ 0.5 を返す
|
|
7
8
|
*/
|
|
8
9
|
function safeRandom(props) {
|
|
9
10
|
try {
|
|
10
11
|
const value = props.deps.random();
|
|
11
|
-
if (typeof value
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0 && value < 1) return value;
|
|
13
|
+
} catch {}
|
|
14
|
+
try {
|
|
15
|
+
const native = Math.random();
|
|
16
|
+
if (typeof native === "number" && Number.isFinite(native) && native >= 0 && native < 1) return native;
|
|
17
|
+
return .5;
|
|
14
18
|
} catch {
|
|
15
19
|
return .5;
|
|
16
20
|
}
|
|
17
21
|
}
|
|
18
22
|
//#endregion
|
|
19
23
|
//#region lib/reconnector.ts
|
|
24
|
+
const MAX_TIMER_DELAY_MS = 2147483647;
|
|
20
25
|
/**
|
|
21
26
|
* 指数バックオフ + ジッタ付きの再接続スケジューラ。
|
|
22
|
-
*
|
|
23
|
-
*
|
|
27
|
+
* setTimeout コールバック内のユーザー fn が throw / reject しても reconnect ループは止めない。
|
|
28
|
+
* `generation` は clearTimeout が throw して古い timer が生き残った場合でも
|
|
29
|
+
* stale な発火を無視するための世代トークン
|
|
24
30
|
*/
|
|
25
31
|
var FlumeReconnector = class {
|
|
26
32
|
props;
|
|
27
33
|
currentAttempt = 0;
|
|
28
34
|
isAborted = false;
|
|
29
35
|
timer = null;
|
|
36
|
+
generation = 0;
|
|
30
37
|
constructor(props) {
|
|
31
38
|
this.props = props;
|
|
32
39
|
}
|
|
@@ -36,12 +43,17 @@ var FlumeReconnector = class {
|
|
|
36
43
|
get aborted() {
|
|
37
44
|
return this.isAborted;
|
|
38
45
|
}
|
|
39
|
-
schedule(fn) {
|
|
40
|
-
if (this.isAborted) return
|
|
41
|
-
if (this.currentAttempt >= this.props.maxAttempts)
|
|
46
|
+
schedule(fn, options) {
|
|
47
|
+
if (this.isAborted) return { kind: "refused" };
|
|
48
|
+
if (this.currentAttempt >= this.props.maxAttempts) {
|
|
49
|
+
this.generation++;
|
|
50
|
+
this.clearTimer();
|
|
51
|
+
return { kind: "exhausted" };
|
|
52
|
+
}
|
|
42
53
|
this.clearTimer();
|
|
43
|
-
const delay = this.computeDelay();
|
|
44
|
-
const
|
|
54
|
+
const delay = this.computeDelay(options?.minDelayMs ?? 0);
|
|
55
|
+
const scheduledGeneration = ++this.generation;
|
|
56
|
+
const timerResult = attempt(() => this.props.deps.setTimeout(() => this.runRetry(fn, scheduledGeneration), delay));
|
|
45
57
|
if (timerResult instanceof Error) {
|
|
46
58
|
this.props.log.error({
|
|
47
59
|
action: "reconnect.timer.schedule.error",
|
|
@@ -49,20 +61,26 @@ var FlumeReconnector = class {
|
|
|
49
61
|
error: timerResult
|
|
50
62
|
});
|
|
51
63
|
this.timer = null;
|
|
52
|
-
return
|
|
64
|
+
return { kind: "refused" };
|
|
53
65
|
}
|
|
54
66
|
this.currentAttempt++;
|
|
55
67
|
this.timer = timerResult;
|
|
56
|
-
return
|
|
68
|
+
return {
|
|
69
|
+
kind: "scheduled",
|
|
70
|
+
delayMs: delay
|
|
71
|
+
};
|
|
57
72
|
}
|
|
58
73
|
reset() {
|
|
59
74
|
this.currentAttempt = 0;
|
|
60
75
|
}
|
|
61
76
|
cancel() {
|
|
62
77
|
this.isAborted = true;
|
|
78
|
+
this.generation++;
|
|
63
79
|
this.clearTimer();
|
|
64
80
|
}
|
|
65
|
-
runRetry(fn) {
|
|
81
|
+
runRetry(fn, scheduledGeneration) {
|
|
82
|
+
if (this.isAborted) return;
|
|
83
|
+
if (scheduledGeneration !== this.generation) return;
|
|
66
84
|
this.timer = null;
|
|
67
85
|
safeInvokeCallback({
|
|
68
86
|
fn,
|
|
@@ -86,19 +104,22 @@ var FlumeReconnector = class {
|
|
|
86
104
|
});
|
|
87
105
|
this.timer = null;
|
|
88
106
|
}
|
|
89
|
-
computeDelay() {
|
|
90
|
-
|
|
107
|
+
computeDelay(minDelayMs) {
|
|
108
|
+
const jittered = Math.min(this.props.baseDelay * 2 ** this.currentAttempt, this.props.maxDelay) * (.5 + safeRandom({ deps: this.props.deps }) * .5);
|
|
109
|
+
return Math.min(Math.max(jittered, Number.isFinite(minDelayMs) && minDelayMs > 0 ? minDelayMs : 0), MAX_TIMER_DELAY_MS);
|
|
91
110
|
}
|
|
92
111
|
};
|
|
93
112
|
//#endregion
|
|
94
113
|
//#region lib/schedule-reconnect.ts
|
|
95
114
|
/**
|
|
96
115
|
* 接続が落ちた際の共通再接続スケジューラ。
|
|
97
|
-
* 再接続の設定状況 (無効 / 中止 / 試行尽き) を見極めてからステータス遷移する。
|
|
116
|
+
* 再接続の設定状況 (無効 / 中止 / 試行尽き / timer 拒否) を見極めてからステータス遷移する。
|
|
98
117
|
* - reconnector が無ければ reconnect.disabled を info ログし disconnected へ
|
|
99
118
|
* - cancel 済みなら reconnect.aborted を info ログし disconnected へ
|
|
100
|
-
* - schedule() が
|
|
101
|
-
* -
|
|
119
|
+
* - schedule() が exhausted なら reconnect.exhausted を error ログし disconnected へ
|
|
120
|
+
* - schedule() が refused (timer 予約失敗) なら reconnect.refused を error ログし disconnected へ
|
|
121
|
+
* ("reconnecting" のまま発火しない timer を待ち続けるハングを防ぐ)
|
|
122
|
+
* - scheduled なら reconnecting へ遷移し reconnect.scheduled を info ログ
|
|
102
123
|
*/
|
|
103
124
|
function scheduleFlumeReconnect(props) {
|
|
104
125
|
if (!props.reconnector) {
|
|
@@ -117,8 +138,8 @@ function scheduleFlumeReconnect(props) {
|
|
|
117
138
|
props.setStatus("disconnected");
|
|
118
139
|
return;
|
|
119
140
|
}
|
|
120
|
-
const
|
|
121
|
-
if (
|
|
141
|
+
const schedule = props.reconnector.schedule(props.retry, { minDelayMs: props.minDelayMs });
|
|
142
|
+
if (schedule.kind === "exhausted") {
|
|
122
143
|
const error = new FlumeConnectionError(`reconnect exhausted after ${props.reconnector.attempt} attempts`);
|
|
123
144
|
props.log.error({
|
|
124
145
|
action: "reconnect.exhausted",
|
|
@@ -128,24 +149,39 @@ function scheduleFlumeReconnect(props) {
|
|
|
128
149
|
props.setStatus("disconnected");
|
|
129
150
|
return;
|
|
130
151
|
}
|
|
152
|
+
if (schedule.kind === "refused") {
|
|
153
|
+
const error = new FlumeConnectionError("reconnect timer could not be scheduled");
|
|
154
|
+
props.log.error({
|
|
155
|
+
action: "reconnect.refused",
|
|
156
|
+
message: safeErrorMessage({ error }),
|
|
157
|
+
error
|
|
158
|
+
});
|
|
159
|
+
props.setStatus("disconnected");
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
131
162
|
props.setStatus("reconnecting");
|
|
132
163
|
props.log.info({
|
|
133
164
|
action: "reconnect.scheduled",
|
|
134
|
-
message: `next attempt in ${Math.round(
|
|
165
|
+
message: `next attempt in ${Math.round(schedule.delayMs)}ms`,
|
|
135
166
|
detail: {
|
|
136
167
|
attempt: props.reconnector.attempt,
|
|
137
|
-
delayMs: Math.round(
|
|
168
|
+
delayMs: Math.round(schedule.delayMs)
|
|
138
169
|
}
|
|
139
170
|
});
|
|
140
171
|
}
|
|
141
172
|
//#endregion
|
|
142
173
|
//#region lib/utils/safe-stringify.ts
|
|
143
174
|
/**
|
|
144
|
-
* `JSON.stringify` を `string | Error`
|
|
145
|
-
* cyclic / BigInt / throwing toJSON など標準が throw するケースを Error
|
|
175
|
+
* `JSON.stringify` を `string | Error` に変換するラッパ。
|
|
176
|
+
* cyclic / BigInt / throwing toJSON など標準が throw するケースを Error として返す。
|
|
177
|
+
* `undefined` / function / symbol は `JSON.stringify` が (型定義に反して) `undefined` を
|
|
178
|
+
* 返すため、これも Error に正規化して戻り値を必ず string にする
|
|
146
179
|
*/
|
|
147
180
|
function safeStringify(value) {
|
|
148
|
-
|
|
181
|
+
const result = attempt(() => JSON.stringify(value));
|
|
182
|
+
if (result instanceof Error) return result;
|
|
183
|
+
if (typeof result !== "string") return new FlumeParseError("value is not JSON-serializable (undefined / function / symbol)");
|
|
184
|
+
return result;
|
|
149
185
|
}
|
|
150
186
|
//#endregion
|
|
151
187
|
export { safeRandom as i, scheduleFlumeReconnect as n, FlumeReconnector as r, safeStringify as t };
|
package/dist/slack.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as FlumeSlackEnvelope, O as FlumeSourceStartContext, T as FlumeSlackSourceOptions, t as FlumeSource } from "./flume-source.js";
|
|
2
2
|
|
|
3
3
|
//#region lib/slack/slack-source.d.ts
|
|
4
4
|
declare class FlumeSlackSource extends FlumeSource {
|
|
@@ -13,7 +13,14 @@ declare class FlumeSlackSource extends FlumeSource {
|
|
|
13
13
|
protected disconnect(): void;
|
|
14
14
|
private hasWebSocket;
|
|
15
15
|
private connectInternal;
|
|
16
|
+
private isTerminalSlackError;
|
|
17
|
+
private minRetryDelayMs;
|
|
16
18
|
private handleMessage;
|
|
19
|
+
/**
|
|
20
|
+
* Events API の再配送は envelope_id が変わり得るため、payload.event_id があれば
|
|
21
|
+
* そちらを重複判定キーとして優先する (再配送をまたいで安定な識別子)
|
|
22
|
+
*/
|
|
23
|
+
private toDedupKey;
|
|
17
24
|
private safeExtractMeta;
|
|
18
25
|
private scheduleReconnect;
|
|
19
26
|
}
|