@friggframework/core 2.0.0--canary.580.1003d8d.0 → 2.0.0--canary.580.4487187.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/modules/requester/requester.js +145 -37
- package/package.json +5 -5
|
@@ -3,6 +3,8 @@ const { Delegate } = require('../../core');
|
|
|
3
3
|
const { FetchError } = require('../../errors');
|
|
4
4
|
const { get } = require('../../assertions');
|
|
5
5
|
|
|
6
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 60_000;
|
|
7
|
+
|
|
6
8
|
class Requester extends Delegate {
|
|
7
9
|
constructor(params) {
|
|
8
10
|
super(params);
|
|
@@ -13,6 +15,30 @@ class Requester extends Delegate {
|
|
|
13
15
|
this.delegateTypes.push(this.DLGT_INVALID_AUTH);
|
|
14
16
|
this.agent = get(params, 'agent', null);
|
|
15
17
|
|
|
18
|
+
// Per-attempt HTTP timeout. Without this the framework called fetch()
|
|
19
|
+
// with no AbortController and no timeout — a silently-hung TCP
|
|
20
|
+
// connection (server accepts but never responds) blocked the calling
|
|
21
|
+
// promise forever, cascading into stalled batches, stalled syncs,
|
|
22
|
+
// and worker-lambda timeouts.
|
|
23
|
+
//
|
|
24
|
+
// Configuration precedence:
|
|
25
|
+
// 1. Instance param: new Requester({ requestTimeoutMs: 30_000 })
|
|
26
|
+
// 2. Class static: static requestTimeoutMs = 30_000
|
|
27
|
+
// 3. Default: DEFAULT_REQUEST_TIMEOUT_MS (60s)
|
|
28
|
+
//
|
|
29
|
+
// Pass 0 (or null) to disable the timeout entirely — reserved for
|
|
30
|
+
// test doubles and documented long-running endpoints.
|
|
31
|
+
// Intentionally NOT using `get(params, ...)` here — the Frigg
|
|
32
|
+
// `get` helper throws RequiredPropertyError if the key is missing
|
|
33
|
+
// and no default is provided, which would collide with the fall-
|
|
34
|
+
// through to the class-level static override.
|
|
35
|
+
const instanceTimeout = params?.requestTimeoutMs;
|
|
36
|
+
this.requestTimeoutMs =
|
|
37
|
+
instanceTimeout !== undefined && instanceTimeout !== null
|
|
38
|
+
? instanceTimeout
|
|
39
|
+
: this.constructor.requestTimeoutMs ??
|
|
40
|
+
DEFAULT_REQUEST_TIMEOUT_MS;
|
|
41
|
+
|
|
16
42
|
// Allow passing in the fetch function
|
|
17
43
|
// Instance methods can use this.fetch without differentiating
|
|
18
44
|
this.fetch = get(params, 'fetch', fetch);
|
|
@@ -48,52 +74,134 @@ class Requester extends Delegate {
|
|
|
48
74
|
|
|
49
75
|
if (this.agent) options.agent = this.agent;
|
|
50
76
|
|
|
51
|
-
|
|
77
|
+
// Per-attempt timeout — fresh AbortController per call so the retry
|
|
78
|
+
// recursion (with its own backoff sleeps) always gets a clean
|
|
79
|
+
// signal. Timer is cleared in the finally block regardless of
|
|
80
|
+
// outcome.
|
|
81
|
+
const timeoutMs = this.requestTimeoutMs;
|
|
82
|
+
const controller = timeoutMs > 0 ? new AbortController() : null;
|
|
83
|
+
const timeoutHandle = controller
|
|
84
|
+
? setTimeout(() => controller.abort(), timeoutMs)
|
|
85
|
+
: null;
|
|
86
|
+
const fetchOptions = controller
|
|
87
|
+
? { ...options, signal: controller.signal }
|
|
88
|
+
: options;
|
|
89
|
+
|
|
90
|
+
// Timer must stay active through body consumption. node-fetch v2
|
|
91
|
+
// resolves the fetch() promise when headers arrive, not when the
|
|
92
|
+
// body is fully read — so a server that sends headers and then
|
|
93
|
+
// stalls the body would still hang parsedBody() or
|
|
94
|
+
// FetchError.create()'s response.text() call. We clear the timer
|
|
95
|
+
// only after the body is fully consumed (success path) or
|
|
96
|
+
// deliberately before each recursive retry so the new attempt
|
|
97
|
+
// starts with its own fresh timer.
|
|
98
|
+
let timerCleared = false;
|
|
99
|
+
const clearRequestTimer = () => {
|
|
100
|
+
if (!timerCleared && timeoutHandle) {
|
|
101
|
+
clearTimeout(timeoutHandle);
|
|
102
|
+
timerCleared = true;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
52
106
|
try {
|
|
53
|
-
response
|
|
54
|
-
|
|
55
|
-
|
|
107
|
+
let response;
|
|
108
|
+
try {
|
|
109
|
+
response = await this.fetch(encodedUrl, fetchOptions);
|
|
110
|
+
} catch (e) {
|
|
111
|
+
// AbortController fires AbortError (name) / ETIMEDOUT-shaped
|
|
112
|
+
// errors (type on node-fetch) when we hit the timeout. No
|
|
113
|
+
// retry on timeout: a slow endpoint is a downstream problem,
|
|
114
|
+
// and each retry would wait another `timeoutMs` before giving
|
|
115
|
+
// up — amplifying the hang into a per-record multi-minute
|
|
116
|
+
// stall at batch scale.
|
|
117
|
+
const isTimeout =
|
|
118
|
+
e?.name === 'AbortError' || e?.type === 'aborted';
|
|
119
|
+
if (e?.code === 'ECONNRESET' && i < this.backOff.length) {
|
|
120
|
+
clearRequestTimer();
|
|
121
|
+
const delay = this.backOff[i] * 1000;
|
|
122
|
+
await new Promise((resolve) =>
|
|
123
|
+
setTimeout(resolve, delay)
|
|
124
|
+
);
|
|
125
|
+
return this._request(url, options, i + 1);
|
|
126
|
+
}
|
|
127
|
+
const fetchError = await FetchError.create({
|
|
128
|
+
resource: encodedUrl,
|
|
129
|
+
init: options,
|
|
130
|
+
responseBody: isTimeout
|
|
131
|
+
? `Request timed out after ${timeoutMs}ms`
|
|
132
|
+
: e,
|
|
133
|
+
});
|
|
134
|
+
if (isTimeout) {
|
|
135
|
+
// Flag + machine-readable fields so callers can
|
|
136
|
+
// distinguish a timeout from a generic network error
|
|
137
|
+
// without parsing the message (which FetchError
|
|
138
|
+
// sanitizes outside of STAGE=dev).
|
|
139
|
+
fetchError.isTimeout = true;
|
|
140
|
+
fetchError.timeoutMs = timeoutMs;
|
|
141
|
+
}
|
|
142
|
+
throw fetchError;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const { status } = response;
|
|
146
|
+
|
|
147
|
+
// If the status is retriable and there are back off requests left, retry the request
|
|
148
|
+
if ((status === 429 || status >= 500) && i < this.backOff.length) {
|
|
149
|
+
clearRequestTimer();
|
|
56
150
|
const delay = this.backOff[i] * 1000;
|
|
57
151
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
58
152
|
return this._request(url, options, i + 1);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if ((status === 429 || status >= 500) && i < this.backOff.length) {
|
|
70
|
-
const delay = this.backOff[i] * 1000;
|
|
71
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
72
|
-
return this._request(url, options, i + 1);
|
|
73
|
-
} else if (status === 401) {
|
|
74
|
-
if (!this.isRefreshable || this.refreshCount > 0) {
|
|
75
|
-
await this.notify(this.DLGT_INVALID_AUTH);
|
|
76
|
-
} else {
|
|
77
|
-
this.refreshCount++;
|
|
78
|
-
const refreshSucceeded = await this.refreshAuth();
|
|
79
|
-
if (refreshSucceeded) {
|
|
80
|
-
return this._request(url, options, i + 1);
|
|
153
|
+
} else if (status === 401) {
|
|
154
|
+
if (!this.isRefreshable || this.refreshCount > 0) {
|
|
155
|
+
await this.notify(this.DLGT_INVALID_AUTH);
|
|
156
|
+
} else {
|
|
157
|
+
this.refreshCount++;
|
|
158
|
+
const refreshSucceeded = await this.refreshAuth();
|
|
159
|
+
if (refreshSucceeded) {
|
|
160
|
+
clearRequestTimer();
|
|
161
|
+
return this._request(url, options, i + 1);
|
|
162
|
+
}
|
|
81
163
|
}
|
|
82
164
|
}
|
|
83
|
-
}
|
|
84
165
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
166
|
+
// If the error wasn't retried, throw. FetchError.create reads
|
|
167
|
+
// the response body (response.text()) — timer must still be
|
|
168
|
+
// alive to catch a stalled body stream.
|
|
169
|
+
if (status >= 400) {
|
|
170
|
+
const fetchError = await FetchError.create({
|
|
171
|
+
resource: encodedUrl,
|
|
172
|
+
init: options,
|
|
173
|
+
response,
|
|
174
|
+
});
|
|
175
|
+
throw this._maybeFlagTimeoutDuringBodyRead(
|
|
176
|
+
fetchError,
|
|
177
|
+
timeoutMs
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// parsedBody consumes the response body stream. If the server
|
|
182
|
+
// stalls mid-stream the timer (still armed) aborts it.
|
|
183
|
+
return options.returnFullRes
|
|
184
|
+
? response
|
|
185
|
+
: await this.parsedBody(response);
|
|
186
|
+
} catch (e) {
|
|
187
|
+
// If the abort fired during body consumption, node-fetch emits
|
|
188
|
+
// the error as an AbortError on the body stream. Surface the
|
|
189
|
+
// same isTimeout flag callers use for header-phase timeouts.
|
|
190
|
+
throw this._maybeFlagTimeoutDuringBodyRead(e, timeoutMs);
|
|
191
|
+
} finally {
|
|
192
|
+
clearRequestTimer();
|
|
92
193
|
}
|
|
194
|
+
}
|
|
93
195
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
196
|
+
_maybeFlagTimeoutDuringBodyRead(err, timeoutMs) {
|
|
197
|
+
if (!err || typeof err !== 'object') return err;
|
|
198
|
+
if (err.isTimeout) return err;
|
|
199
|
+
const isAbort =
|
|
200
|
+
err.name === 'AbortError' || err.type === 'aborted';
|
|
201
|
+
if (!isAbort) return err;
|
|
202
|
+
err.isTimeout = true;
|
|
203
|
+
err.timeoutMs = timeoutMs;
|
|
204
|
+
return err;
|
|
97
205
|
}
|
|
98
206
|
|
|
99
207
|
async _get(options) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0--canary.580.
|
|
4
|
+
"version": "2.0.0--canary.580.4487187.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
|
|
7
7
|
"@aws-sdk/client-kms": "^3.588.0",
|
|
@@ -38,9 +38,9 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@friggframework/eslint-config": "2.0.0--canary.580.
|
|
42
|
-
"@friggframework/prettier-config": "2.0.0--canary.580.
|
|
43
|
-
"@friggframework/test": "2.0.0--canary.580.
|
|
41
|
+
"@friggframework/eslint-config": "2.0.0--canary.580.4487187.0",
|
|
42
|
+
"@friggframework/prettier-config": "2.0.0--canary.580.4487187.0",
|
|
43
|
+
"@friggframework/test": "2.0.0--canary.580.4487187.0",
|
|
44
44
|
"@prisma/client": "^6.17.0",
|
|
45
45
|
"@types/lodash": "4.17.15",
|
|
46
46
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
@@ -80,5 +80,5 @@
|
|
|
80
80
|
"publishConfig": {
|
|
81
81
|
"access": "public"
|
|
82
82
|
},
|
|
83
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "44871876975b9e507ed635e9df4075b2215685d1"
|
|
84
84
|
}
|