@colixsystems/actions-client 0.4.1 → 0.4.2
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 +2 -0
- package/dist/transport.js +40 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,8 @@ An Action is a server-side script the workspace owns. The app may run one only w
|
|
|
10
10
|
|
|
11
11
|
## Status
|
|
12
12
|
|
|
13
|
+
`v0.4.2` — internal: carries the newer transport the clients share, which gained options other clients use. This client's requests, retries and errors are unchanged.
|
|
14
|
+
|
|
13
15
|
`v0.4.1` — the factory option checks, URL joining and request headers now come from the transport the clients share. Two small changes: a `getRequestHeaders` value of `null` or `undefined` is no longer sent as the text "null", and a `multipartFetchImpl` option that is not a function is now refused with a `TypeError`, as in the other clients. No export or request shape changed.
|
|
14
16
|
|
|
15
17
|
`v0.4.0` — adds `enqueue`, which queues an Action run instead of waiting for it. `v0.3.0` added `createConnectorsClient`, which calls a workspace's REST connectors through the operations it listed for apps.
|
package/dist/transport.js
CHANGED
|
@@ -13,7 +13,7 @@ const DEFAULT_TIMEOUT_MS = 10_000;
|
|
|
13
13
|
// An upload lasts as long as the file takes to cross a mobile link.
|
|
14
14
|
const MULTIPART_TIMEOUT_MS = 5 * 60_000;
|
|
15
15
|
const BACKOFF_MS = [200, 400, 800];
|
|
16
|
-
const RETRY_VERBS =
|
|
16
|
+
const RETRY_VERBS = ["GET", "HEAD"];
|
|
17
17
|
// A longer server hint surfaces the 429 at once instead of hanging a widget.
|
|
18
18
|
const MAX_RETRY_AFTER_MS = DEFAULT_TIMEOUT_MS;
|
|
19
19
|
|
|
@@ -82,8 +82,8 @@ function retryAfterMs(body, header) {
|
|
|
82
82
|
return Number.isNaN(at) ? null : Math.max(0, at - Date.now());
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
function isRetryable(err) {
|
|
86
|
-
if (!err) return false;
|
|
85
|
+
function isRetryable(err, neverRetryCodes) {
|
|
86
|
+
if (!err || neverRetryCodes.includes(err.code)) return false;
|
|
87
87
|
if (err.code === "NETWORK" || err.status === 429 || err.status >= 500) return true;
|
|
88
88
|
// A caller's own `withRetry` attempt may reject with fetch's raw TypeError.
|
|
89
89
|
// A raw AbortError is a caller's cancel, which must stay final.
|
|
@@ -143,23 +143,34 @@ function parseBody(text) {
|
|
|
143
143
|
* One client's retry contract.
|
|
144
144
|
*
|
|
145
145
|
* @param {{ BaseError: new (message: string, init: { code: string, status: number }) => Error,
|
|
146
|
-
* fromResponse: (status: number, body: unknown) => Error
|
|
146
|
+
* fromResponse: (status: number, body: unknown, retryAfterHeader: string | null) => Error,
|
|
147
|
+
* retryVerbs?: string[] | "all",
|
|
148
|
+
* neverRetryCodes?: string[] }} policy
|
|
149
|
+
* `retryVerbs` defaults to GET and HEAD. An error whose `code` is in
|
|
150
|
+
* `neverRetryCodes` is final.
|
|
147
151
|
* @returns {{ BaseError, fromResponse, withRetry }} `withRetry` is the client's public export.
|
|
148
152
|
*/
|
|
149
|
-
export function defineTransportPolicy({
|
|
153
|
+
export function defineTransportPolicy({
|
|
154
|
+
BaseError,
|
|
155
|
+
fromResponse,
|
|
156
|
+
retryVerbs = RETRY_VERBS,
|
|
157
|
+
neverRetryCodes = [],
|
|
158
|
+
}) {
|
|
150
159
|
/**
|
|
151
|
-
* Run `attempt` with retries:
|
|
152
|
-
* failure, a 429 or a 5xx; other verbs once. Each
|
|
153
|
-
* AbortSignal, aborted after `timeoutMs` (default
|
|
154
|
-
* timed out is not resent.
|
|
160
|
+
* Run `attempt` with retries: a retried verb up to `retries` (default 3)
|
|
161
|
+
* more times on a network failure, a 429 or a 5xx; other verbs once. Each
|
|
162
|
+
* attempt gets its own AbortSignal, aborted after `timeoutMs` (default
|
|
163
|
+
* 10 s); an attempt that timed out is not resent.
|
|
155
164
|
*
|
|
156
|
-
* @param {{ method?: string, timeoutMs?: number }} opts
|
|
165
|
+
* @param {{ method?: string, timeoutMs?: number, retries?: number }} opts
|
|
157
166
|
* @param {(signal: AbortSignal) => Promise<any>} attempt
|
|
158
167
|
*/
|
|
159
168
|
async function withRetry(opts, attempt) {
|
|
160
169
|
const method = (opts?.method || "GET").toUpperCase();
|
|
161
170
|
const timeoutMs = opts?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
162
|
-
const
|
|
171
|
+
const retries = Number.isInteger(opts?.retries) ? Math.max(0, opts.retries) : BACKOFF_MS.length;
|
|
172
|
+
const retried = retryVerbs === "all" || retryVerbs.includes(method);
|
|
173
|
+
const maxAttempts = retried ? retries + 1 : 1;
|
|
163
174
|
for (let i = 0; ; i++) {
|
|
164
175
|
const outcome = await timedAttempt(attempt, timeoutMs);
|
|
165
176
|
if (outcome.ok) return outcome.value;
|
|
@@ -167,8 +178,8 @@ export function defineTransportPolicy({ BaseError, fromResponse }) {
|
|
|
167
178
|
const hintMs = retryAfterMsByError.get(error) ?? 0;
|
|
168
179
|
// The server may still be working on a request we timed out.
|
|
169
180
|
const final = i === maxAttempts - 1 || outcome.timedOut || hintMs > MAX_RETRY_AFTER_MS;
|
|
170
|
-
if (final || !isRetryable(error)) throw error;
|
|
171
|
-
await sleep(Math.max(BACKOFF_MS[i], hintMs));
|
|
181
|
+
if (final || !isRetryable(error, neverRetryCodes)) throw error;
|
|
182
|
+
await sleep(Math.max(BACKOFF_MS[Math.min(i, BACKOFF_MS.length - 1)], hintMs));
|
|
172
183
|
}
|
|
173
184
|
}
|
|
174
185
|
return { BaseError, fromResponse, withRetry };
|
|
@@ -190,6 +201,8 @@ export function createRequester(factory, opts, policy, { namespace: defaultNames
|
|
|
190
201
|
/**
|
|
191
202
|
* `body` is sent as JSON; `formData` verbatim over `multipartFetchImpl`, with
|
|
192
203
|
* no content-type so the runtime sets the multipart boundary.
|
|
204
|
+
* `responseType: "arrayBuffer"` resolves a successful response to its bytes.
|
|
205
|
+
* `accept` replaces the accept header after the host's headers.
|
|
193
206
|
*/
|
|
194
207
|
async function request(
|
|
195
208
|
method,
|
|
@@ -201,6 +214,8 @@ export function createRequester(factory, opts, policy, { namespace: defaultNames
|
|
|
201
214
|
namespace = defaultNamespace,
|
|
202
215
|
operation,
|
|
203
216
|
timeoutMs,
|
|
217
|
+
responseType = "json",
|
|
218
|
+
accept,
|
|
204
219
|
onResponse,
|
|
205
220
|
} = {},
|
|
206
221
|
) {
|
|
@@ -211,6 +226,8 @@ export function createRequester(factory, opts, policy, { namespace: defaultNames
|
|
|
211
226
|
namespace,
|
|
212
227
|
operation,
|
|
213
228
|
});
|
|
229
|
+
// The format this call parses outranks a host's accept header.
|
|
230
|
+
if (accept) headers.accept = accept;
|
|
214
231
|
|
|
215
232
|
const isMultipart = formData !== undefined;
|
|
216
233
|
const send = isMultipart ? multipartFetchImpl : fetchImpl;
|
|
@@ -219,25 +236,28 @@ export function createRequester(factory, opts, policy, { namespace: defaultNames
|
|
|
219
236
|
|
|
220
237
|
return withRetry({ method, timeoutMs: deadline }, async (signal) => {
|
|
221
238
|
let res;
|
|
222
|
-
let
|
|
239
|
+
let content;
|
|
223
240
|
// The body read can fail like the fetch, so both are classified here.
|
|
224
241
|
try {
|
|
225
242
|
res = await send(url, { method, headers, body: payload, signal });
|
|
226
|
-
|
|
243
|
+
// An error still comes back as the JSON envelope.
|
|
244
|
+
content =
|
|
245
|
+
res.ok && responseType === "arrayBuffer"
|
|
246
|
+
? await res.arrayBuffer()
|
|
247
|
+
: parseBody(await res.text());
|
|
227
248
|
} catch (err) {
|
|
228
249
|
const reason = signal.aborted ? "request timed out" : err?.message;
|
|
229
250
|
throw new BaseError(`Network failure: ${reason}`, { code: "NETWORK", status: 0 });
|
|
230
251
|
}
|
|
231
|
-
const parsed = parseBody(text);
|
|
232
252
|
if (!res.ok) {
|
|
233
|
-
const
|
|
234
|
-
const
|
|
235
|
-
|
|
253
|
+
const retryAfterHeader = res.headers?.get?.("retry-after") ?? null;
|
|
254
|
+
const err = fromResponse(res.status, content, retryAfterHeader);
|
|
255
|
+
const hintMs = res.status === 429 ? retryAfterMs(content, retryAfterHeader) : null;
|
|
236
256
|
if (hintMs != null) retryAfterMsByError.set(err, hintMs);
|
|
237
257
|
throw err;
|
|
238
258
|
}
|
|
239
259
|
onResponse?.(res);
|
|
240
|
-
return
|
|
260
|
+
return content;
|
|
241
261
|
});
|
|
242
262
|
}
|
|
243
263
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@colixsystems/actions-client",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Scoped clients for running a workspace's Actions and calling its REST connectors from an AppStudio app. Used by widgets through the injected WidgetContext.actions and WidgetContext.connectors.",
|
|
5
5
|
"homepage": "https://github.com/Colix-AB/AppStudio",
|
|
6
6
|
"type": "module",
|