@supacloud/js 0.30.0 → 0.32.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/dist/index.d.ts +15 -0
- package/dist/index.js +32 -1
- package/dist/task-events.d.ts +66 -0
- package/dist/task-events.js +311 -0
- package/package.json +8 -3
package/dist/index.d.ts
CHANGED
|
@@ -42,6 +42,21 @@ export type SupaCloudTaskDetail<TResult = unknown> = {
|
|
|
42
42
|
id: string;
|
|
43
43
|
project_ref: string;
|
|
44
44
|
status: SupaCloudTaskStatus | string;
|
|
45
|
+
task_type?: string;
|
|
46
|
+
executor?: {
|
|
47
|
+
kind: string;
|
|
48
|
+
version: string;
|
|
49
|
+
definition: string;
|
|
50
|
+
run_id: string;
|
|
51
|
+
native_status: string;
|
|
52
|
+
};
|
|
53
|
+
capabilities?: {
|
|
54
|
+
cancel: boolean;
|
|
55
|
+
retry: boolean;
|
|
56
|
+
};
|
|
57
|
+
blocked_reason?: string | null;
|
|
58
|
+
total_steps?: number;
|
|
59
|
+
finished_steps?: number;
|
|
45
60
|
function_slug?: string | null;
|
|
46
61
|
function_version?: string | null;
|
|
47
62
|
attempt?: number | null;
|
package/dist/index.js
CHANGED
|
@@ -2639,6 +2639,25 @@ function decodeTaskAttempt(value) {
|
|
|
2639
2639
|
}
|
|
2640
2640
|
function decodeTaskDetail(value) {
|
|
2641
2641
|
const record = responseRecord(value, "task");
|
|
2642
|
+
let executor;
|
|
2643
|
+
let capabilities;
|
|
2644
|
+
if (Object.hasOwn(record, "executor")) {
|
|
2645
|
+
const source = responseRecord(record.executor, "task executor");
|
|
2646
|
+
executor = {
|
|
2647
|
+
kind: taskText(source.kind),
|
|
2648
|
+
version: taskText(source.version),
|
|
2649
|
+
definition: taskText(source.definition),
|
|
2650
|
+
run_id: taskText(source.run_id),
|
|
2651
|
+
native_status: taskText(source.native_status)
|
|
2652
|
+
};
|
|
2653
|
+
}
|
|
2654
|
+
if (Object.hasOwn(record, "capabilities")) {
|
|
2655
|
+
const source = responseRecord(record.capabilities, "task capabilities");
|
|
2656
|
+
if (typeof source.cancel !== "boolean" || typeof source.retry !== "boolean") {
|
|
2657
|
+
throw new Error("Invalid task capabilities");
|
|
2658
|
+
}
|
|
2659
|
+
capabilities = { cancel: source.cancel, retry: source.retry };
|
|
2660
|
+
}
|
|
2642
2661
|
for (const key of [
|
|
2643
2662
|
"function_slug",
|
|
2644
2663
|
"function_version",
|
|
@@ -2647,7 +2666,8 @@ function decodeTaskDetail(value) {
|
|
|
2647
2666
|
"correlation_id",
|
|
2648
2667
|
"business_task_id",
|
|
2649
2668
|
"updated_at",
|
|
2650
|
-
"created_at"
|
|
2669
|
+
"created_at",
|
|
2670
|
+
"blocked_reason"
|
|
2651
2671
|
]) {
|
|
2652
2672
|
if (Object.hasOwn(record, key))
|
|
2653
2673
|
taskNullable(record[key], taskText);
|
|
@@ -2656,6 +2676,15 @@ function decodeTaskDetail(value) {
|
|
|
2656
2676
|
if (Object.hasOwn(record, key))
|
|
2657
2677
|
taskNullable(record[key], taskNumber);
|
|
2658
2678
|
}
|
|
2679
|
+
if (Object.hasOwn(record, "task_type"))
|
|
2680
|
+
taskText(record.task_type);
|
|
2681
|
+
for (const key of ["total_steps", "finished_steps"]) {
|
|
2682
|
+
if (Object.hasOwn(record, key)) {
|
|
2683
|
+
const count = taskNumber(record[key]);
|
|
2684
|
+
if (!Number.isSafeInteger(count) || count < 0)
|
|
2685
|
+
throw new Error("Invalid task step count");
|
|
2686
|
+
}
|
|
2687
|
+
}
|
|
2659
2688
|
if (Object.hasOwn(record, "payload"))
|
|
2660
2689
|
responseRecord(record.payload, "task payload");
|
|
2661
2690
|
if (Object.hasOwn(record, "metadata") && record.metadata !== null)
|
|
@@ -2665,6 +2694,8 @@ function decodeTaskDetail(value) {
|
|
|
2665
2694
|
id: responseString(record, "id", "task"),
|
|
2666
2695
|
project_ref: responseString(record, "project_ref", "task"),
|
|
2667
2696
|
status: responseString(record, "status", "task"),
|
|
2697
|
+
...executor === undefined ? {} : { executor },
|
|
2698
|
+
...capabilities === undefined ? {} : { capabilities },
|
|
2668
2699
|
...Object.hasOwn(record, "attempts") ? { attempts: taskArray(record.attempts, decodeTaskAttempt) } : {},
|
|
2669
2700
|
...Object.hasOwn(record, "latest_logs") ? { latest_logs: taskArray(record.latest_logs, decodeTaskLog) } : {}
|
|
2670
2701
|
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** Opt-in SupaCloud task output extension. Does not wrap or modify supabase-js. */
|
|
2
|
+
export interface TaskOutputEvent {
|
|
3
|
+
schema_version: 1;
|
|
4
|
+
project_ref: string;
|
|
5
|
+
task_id: string;
|
|
6
|
+
event_id: string;
|
|
7
|
+
sequence: string;
|
|
8
|
+
attempt: number;
|
|
9
|
+
type: string;
|
|
10
|
+
payload: Record<string, unknown>;
|
|
11
|
+
created_at: string;
|
|
12
|
+
}
|
|
13
|
+
export interface TaskOutputPage {
|
|
14
|
+
schema_version: 1;
|
|
15
|
+
project_ref: string;
|
|
16
|
+
task_id: string;
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
task_status: string;
|
|
19
|
+
attempt: number;
|
|
20
|
+
events: TaskOutputEvent[];
|
|
21
|
+
next_cursor: string;
|
|
22
|
+
last_sequence: string;
|
|
23
|
+
retained_after: string;
|
|
24
|
+
has_more: boolean;
|
|
25
|
+
replay_available: true;
|
|
26
|
+
}
|
|
27
|
+
export interface TaskOutputAppend {
|
|
28
|
+
attempt: number;
|
|
29
|
+
/** Generate once per logical output event; reuse when confirming an uncertain write. */
|
|
30
|
+
event_id: string;
|
|
31
|
+
type: "output.delta" | "output.snapshot" | "progress" | "warning";
|
|
32
|
+
payload: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
export interface TaskEventClientOptions {
|
|
35
|
+
baseUrl: string;
|
|
36
|
+
projectRef: string;
|
|
37
|
+
/** Read the current JWT on each request; never put credentials in the URL. */
|
|
38
|
+
getHeaders: () => HeadersInit | Promise<HeadersInit>;
|
|
39
|
+
fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
40
|
+
}
|
|
41
|
+
export interface TaskEventListOptions {
|
|
42
|
+
after?: string;
|
|
43
|
+
limit?: number;
|
|
44
|
+
signal?: AbortSignal;
|
|
45
|
+
}
|
|
46
|
+
export interface TaskEventWatchOptions extends TaskEventListOptions {
|
|
47
|
+
pollIntervalMs?: number;
|
|
48
|
+
maxRetries?: number;
|
|
49
|
+
/** Called AFTER the consumer resumes the generator, not when a message arrives. */
|
|
50
|
+
onCursor?: (cursor: string) => void | Promise<void>;
|
|
51
|
+
/** Optional native Realtime notification hook. Messages only request a fresh HTTP read. */
|
|
52
|
+
subscribe?: (wake: () => void) => (() => void | Promise<void>);
|
|
53
|
+
}
|
|
54
|
+
export declare class TaskEventError extends Error {
|
|
55
|
+
readonly status: number;
|
|
56
|
+
readonly code: string;
|
|
57
|
+
readonly details: Record<string, unknown>;
|
|
58
|
+
constructor(status: number, code: string, details?: Record<string, unknown>);
|
|
59
|
+
}
|
|
60
|
+
export declare function createTaskEventClient(options: TaskEventClientOptions): {
|
|
61
|
+
list: (id: string, settings?: TaskEventListOptions) => Promise<TaskOutputPage>;
|
|
62
|
+
append: (id: string, input: TaskOutputAppend, settings?: {
|
|
63
|
+
signal?: AbortSignal;
|
|
64
|
+
}) => Promise<TaskOutputEvent>;
|
|
65
|
+
watch: (id: string, settings?: TaskEventWatchOptions) => AsyncGenerator<TaskOutputEvent, void, void>;
|
|
66
|
+
};
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
// src/task-events.ts
|
|
2
|
+
class TaskEventError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
details;
|
|
6
|
+
constructor(status, code, details = {}) {
|
|
7
|
+
super(code);
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.details = details;
|
|
11
|
+
this.name = "TaskEventError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
15
|
+
var MAX_CURSOR = 9223372036854775807n;
|
|
16
|
+
var MAX_RESPONSE_BYTES = 2 * 1024 * 1024;
|
|
17
|
+
var STATES = new Set(["pending", "leased", "running", "retry_scheduled", "succeeded", "failed", "dead_lettered", "cancelled"]);
|
|
18
|
+
var TERMINAL = new Set(["succeeded", "failed", "dead_lettered", "cancelled"]);
|
|
19
|
+
var OUTPUTS = new Set(["output.delta", "output.snapshot", "progress", "warning"]);
|
|
20
|
+
function invalid() {
|
|
21
|
+
throw new TaskEventError(0, "TASK_OUTPUT_INVALID_RESPONSE");
|
|
22
|
+
}
|
|
23
|
+
function record(value) {
|
|
24
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
function cursor(value) {
|
|
27
|
+
if (typeof value !== "string" || !/^(0|[1-9][0-9]{0,18})$/.test(value) || BigInt(value) > MAX_CURSOR) {
|
|
28
|
+
throw new TaskEventError(400, "TASK_OUTPUT_INVALID_CURSOR");
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
function taskId(value) {
|
|
33
|
+
if (!UUID.test(value))
|
|
34
|
+
throw new TaskEventError(400, "TASK_OUTPUT_INVALID_TASK");
|
|
35
|
+
return value.toLowerCase();
|
|
36
|
+
}
|
|
37
|
+
function integer(value, minimum, maximum) {
|
|
38
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= minimum && value <= maximum;
|
|
39
|
+
}
|
|
40
|
+
function event(value, ref, id) {
|
|
41
|
+
if (!record(value) || value.schema_version !== 1 || value.project_ref !== ref || value.task_id !== id || typeof value.event_id !== "string" || !UUID.test(value.event_id) || !integer(value.attempt, 0, 2147483647) || typeof value.type !== "string" || !(OUTPUTS.has(value.type) || value.type.startsWith("task.") && STATES.has(value.type.slice(5))) || !record(value.payload) || typeof value.created_at !== "string" || !Number.isFinite(Date.parse(value.created_at)))
|
|
42
|
+
invalid();
|
|
43
|
+
const sequence = cursor(value.sequence);
|
|
44
|
+
if (sequence === "0" || OUTPUTS.has(value.type) && value.attempt < 1)
|
|
45
|
+
invalid();
|
|
46
|
+
return {
|
|
47
|
+
schema_version: 1,
|
|
48
|
+
project_ref: ref,
|
|
49
|
+
task_id: id,
|
|
50
|
+
event_id: value.event_id,
|
|
51
|
+
sequence,
|
|
52
|
+
attempt: value.attempt,
|
|
53
|
+
type: value.type,
|
|
54
|
+
payload: value.payload,
|
|
55
|
+
created_at: value.created_at
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function page(value, ref, id, after, limit) {
|
|
59
|
+
if (!record(value) || value.schema_version !== 1 || value.project_ref !== ref || value.task_id !== id || typeof value.enabled !== "boolean" || typeof value.task_status !== "string" || !STATES.has(value.task_status) || !integer(value.attempt, 0, 2147483647) || !Array.isArray(value.events) || value.events.length > limit || typeof value.has_more !== "boolean" || value.replay_available !== true)
|
|
60
|
+
invalid();
|
|
61
|
+
const next = cursor(value.next_cursor), last = cursor(value.last_sequence), retained = cursor(value.retained_after);
|
|
62
|
+
if (BigInt(retained) > BigInt(after) || BigInt(after) > BigInt(last))
|
|
63
|
+
invalid();
|
|
64
|
+
let position = BigInt(after);
|
|
65
|
+
const events = value.events.map((raw) => {
|
|
66
|
+
const item = event(raw, ref, id);
|
|
67
|
+
if (BigInt(item.sequence) !== position + 1n)
|
|
68
|
+
invalid();
|
|
69
|
+
position = BigInt(item.sequence);
|
|
70
|
+
return item;
|
|
71
|
+
});
|
|
72
|
+
if (next !== position.toString() || position > BigInt(last) || value.has_more !== position < BigInt(last) || value.has_more && events.length !== limit || !value.enabled && last !== "0")
|
|
73
|
+
invalid();
|
|
74
|
+
return {
|
|
75
|
+
schema_version: 1,
|
|
76
|
+
project_ref: ref,
|
|
77
|
+
task_id: id,
|
|
78
|
+
enabled: value.enabled,
|
|
79
|
+
task_status: value.task_status,
|
|
80
|
+
attempt: value.attempt,
|
|
81
|
+
events,
|
|
82
|
+
next_cursor: next,
|
|
83
|
+
last_sequence: last,
|
|
84
|
+
retained_after: retained,
|
|
85
|
+
has_more: value.has_more,
|
|
86
|
+
replay_available: true
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function abortError(signal) {
|
|
90
|
+
return signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
91
|
+
}
|
|
92
|
+
function abortable(work, signal) {
|
|
93
|
+
if (signal.aborted) {
|
|
94
|
+
work.catch(() => {});
|
|
95
|
+
return Promise.reject(abortError(signal));
|
|
96
|
+
}
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
const abort = () => {
|
|
99
|
+
cleanup();
|
|
100
|
+
reject(abortError(signal));
|
|
101
|
+
};
|
|
102
|
+
const cleanup = () => signal.removeEventListener("abort", abort);
|
|
103
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
104
|
+
work.then((value) => {
|
|
105
|
+
cleanup();
|
|
106
|
+
resolve(value);
|
|
107
|
+
}, (error) => {
|
|
108
|
+
cleanup();
|
|
109
|
+
reject(error);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async function jsonBody(response, signal) {
|
|
114
|
+
if (!response.body)
|
|
115
|
+
invalid();
|
|
116
|
+
const reader = response.body.getReader();
|
|
117
|
+
const chunks = [];
|
|
118
|
+
let length = 0;
|
|
119
|
+
try {
|
|
120
|
+
for (;; ) {
|
|
121
|
+
const { value, done } = await abortable(reader.read(), signal);
|
|
122
|
+
if (done)
|
|
123
|
+
break;
|
|
124
|
+
length += value.byteLength;
|
|
125
|
+
if (length > MAX_RESPONSE_BYTES || chunks.length >= 32768)
|
|
126
|
+
throw new TaskEventError(0, "TASK_OUTPUT_RESPONSE_TOO_LARGE");
|
|
127
|
+
chunks.push(value);
|
|
128
|
+
}
|
|
129
|
+
const bytes = new Uint8Array(length);
|
|
130
|
+
let offset = 0;
|
|
131
|
+
for (const chunk of chunks) {
|
|
132
|
+
bytes.set(chunk, offset);
|
|
133
|
+
offset += chunk.byteLength;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
137
|
+
} catch {
|
|
138
|
+
invalid();
|
|
139
|
+
}
|
|
140
|
+
} finally {
|
|
141
|
+
reader.cancel().catch(() => {});
|
|
142
|
+
reader.releaseLock();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function createTaskEventClient(options) {
|
|
146
|
+
const base = new URL(options.baseUrl);
|
|
147
|
+
if (!["http:", "https:"].includes(base.protocol) || base.username || base.password || base.search || base.hash || !/^[a-zA-Z0-9_-]{1,20}$/.test(options.projectRef))
|
|
148
|
+
throw new TaskEventError(400, "TASK_OUTPUT_INVALID_CLIENT");
|
|
149
|
+
const ref = options.projectRef;
|
|
150
|
+
const basePath = base.pathname.replace(/\/$/, "");
|
|
151
|
+
const fetchImpl = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
152
|
+
async function request(id, query, input, signal) {
|
|
153
|
+
signal?.throwIfAborted();
|
|
154
|
+
const controller = new AbortController;
|
|
155
|
+
const stop = () => controller.abort(signal?.reason);
|
|
156
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
157
|
+
const timeout = setTimeout(() => controller.abort(new TaskEventError(408, "TASK_OUTPUT_REQUEST_TIMEOUT")), 30000);
|
|
158
|
+
try {
|
|
159
|
+
let headers;
|
|
160
|
+
try {
|
|
161
|
+
headers = new Headers(await abortable(Promise.resolve().then(options.getHeaders), controller.signal));
|
|
162
|
+
} catch (error) {
|
|
163
|
+
if (controller.signal.aborted)
|
|
164
|
+
throw error;
|
|
165
|
+
throw new TaskEventError(0, "TASK_OUTPUT_HEADERS_ERROR");
|
|
166
|
+
}
|
|
167
|
+
headers.set("accept", "application/json");
|
|
168
|
+
if (input)
|
|
169
|
+
headers.set("content-type", "application/json");
|
|
170
|
+
const url = new URL(`${basePath}/v1/projects/${encodeURIComponent(ref)}/tasks/${id}/events${query}`, base.origin);
|
|
171
|
+
const response = await abortable(fetchImpl(url, {
|
|
172
|
+
method: input ? "POST" : "GET",
|
|
173
|
+
headers,
|
|
174
|
+
body: input ? JSON.stringify(input) : undefined,
|
|
175
|
+
signal: controller.signal,
|
|
176
|
+
redirect: "error",
|
|
177
|
+
credentials: "omit",
|
|
178
|
+
cache: "no-store"
|
|
179
|
+
}), controller.signal);
|
|
180
|
+
let data;
|
|
181
|
+
try {
|
|
182
|
+
data = await jsonBody(response, controller.signal);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (!response.ok && error instanceof TaskEventError && error.code === "TASK_OUTPUT_INVALID_RESPONSE") {
|
|
185
|
+
throw new TaskEventError(response.status, "TASK_OUTPUT_HTTP_ERROR");
|
|
186
|
+
}
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
if (!response.ok)
|
|
190
|
+
throw new TaskEventError(response.status, record(data) && typeof data.code === "string" ? data.code : "TASK_OUTPUT_HTTP_ERROR", record(data) ? data : {});
|
|
191
|
+
return data;
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (controller.signal.aborted)
|
|
194
|
+
throw abortError(controller.signal);
|
|
195
|
+
if (error instanceof TaskEventError)
|
|
196
|
+
throw error;
|
|
197
|
+
throw new TaskEventError(0, "TASK_OUTPUT_NETWORK_ERROR");
|
|
198
|
+
} finally {
|
|
199
|
+
clearTimeout(timeout);
|
|
200
|
+
signal?.removeEventListener("abort", stop);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
async function list(id, settings = {}) {
|
|
204
|
+
id = taskId(id);
|
|
205
|
+
const after = cursor(settings.after ?? "0"), limit = settings.limit ?? 50;
|
|
206
|
+
if (!integer(limit, 1, 100))
|
|
207
|
+
throw new TaskEventError(400, "TASK_OUTPUT_INVALID_LIMIT");
|
|
208
|
+
const query = `?${new URLSearchParams({ after, limit: String(limit) })}`;
|
|
209
|
+
return page(await request(id, query, undefined, settings.signal), ref, id, after, limit);
|
|
210
|
+
}
|
|
211
|
+
async function append(id, input, settings = {}) {
|
|
212
|
+
id = taskId(id);
|
|
213
|
+
if (!record(input) || !integer(input.attempt, 1, 2147483647) || !UUID.test(input.event_id) || !OUTPUTS.has(input.type) || !record(input.payload))
|
|
214
|
+
throw new TaskEventError(400, "TASK_OUTPUT_INVALID_INPUT");
|
|
215
|
+
let encoded;
|
|
216
|
+
try {
|
|
217
|
+
encoded = JSON.stringify({ attempt: input.attempt, event_id: input.event_id, type: input.type, payload: input.payload });
|
|
218
|
+
} catch {
|
|
219
|
+
throw new TaskEventError(400, "TASK_OUTPUT_INVALID_INPUT");
|
|
220
|
+
}
|
|
221
|
+
if (new TextEncoder().encode(encoded).byteLength > 20 * 1024)
|
|
222
|
+
throw new TaskEventError(413, "TASK_OUTPUT_LIMIT");
|
|
223
|
+
const snapshot = JSON.parse(encoded);
|
|
224
|
+
if (!record(snapshot.payload))
|
|
225
|
+
throw new TaskEventError(400, "TASK_OUTPUT_INVALID_INPUT");
|
|
226
|
+
const output = event(await request(id, "", snapshot, settings.signal), ref, id);
|
|
227
|
+
if (output.attempt !== snapshot.attempt || output.event_id !== snapshot.event_id.toLowerCase() || output.type !== snapshot.type)
|
|
228
|
+
invalid();
|
|
229
|
+
return output;
|
|
230
|
+
}
|
|
231
|
+
async function* watch(id, settings = {}) {
|
|
232
|
+
id = taskId(id);
|
|
233
|
+
let after = cursor(settings.after ?? "0");
|
|
234
|
+
const interval = settings.pollIntervalMs ?? 1000, maxRetries = settings.maxRetries ?? 5;
|
|
235
|
+
if (!integer(interval, 10, 60000) || !integer(maxRetries, 0, 20))
|
|
236
|
+
throw new TaskEventError(400, "TASK_OUTPUT_INVALID_INPUT");
|
|
237
|
+
settings.signal?.throwIfAborted();
|
|
238
|
+
let pending = false;
|
|
239
|
+
let wakeWaiter;
|
|
240
|
+
let unsubscribe;
|
|
241
|
+
let retries = 0;
|
|
242
|
+
const wake = () => {
|
|
243
|
+
pending = true;
|
|
244
|
+
wakeWaiter?.();
|
|
245
|
+
};
|
|
246
|
+
async function wait(ms, notifications) {
|
|
247
|
+
settings.signal?.throwIfAborted();
|
|
248
|
+
if (notifications && pending) {
|
|
249
|
+
pending = false;
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
await new Promise((resolve, reject) => {
|
|
253
|
+
const finish = () => {
|
|
254
|
+
cleanup();
|
|
255
|
+
resolve();
|
|
256
|
+
};
|
|
257
|
+
const stop = () => {
|
|
258
|
+
cleanup();
|
|
259
|
+
reject(abortError(settings.signal));
|
|
260
|
+
};
|
|
261
|
+
const timeout = setTimeout(finish, ms);
|
|
262
|
+
const cleanup = () => {
|
|
263
|
+
clearTimeout(timeout);
|
|
264
|
+
wakeWaiter = undefined;
|
|
265
|
+
settings.signal?.removeEventListener("abort", stop);
|
|
266
|
+
};
|
|
267
|
+
if (notifications)
|
|
268
|
+
wakeWaiter = finish;
|
|
269
|
+
settings.signal?.addEventListener("abort", stop, { once: true });
|
|
270
|
+
});
|
|
271
|
+
if (notifications)
|
|
272
|
+
pending = false;
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
unsubscribe = settings.subscribe?.(wake);
|
|
276
|
+
for (;; ) {
|
|
277
|
+
settings.signal?.throwIfAborted();
|
|
278
|
+
let current;
|
|
279
|
+
try {
|
|
280
|
+
current = await list(id, { after, limit: settings.limit, signal: settings.signal });
|
|
281
|
+
retries = 0;
|
|
282
|
+
} catch (error) {
|
|
283
|
+
settings.signal?.throwIfAborted();
|
|
284
|
+
const retryable = error instanceof TaskEventError && ([408, 429, 502, 503, 504].includes(error.status) || error.code === "TASK_OUTPUT_NETWORK_ERROR");
|
|
285
|
+
if (!retryable || retries++ >= maxRetries)
|
|
286
|
+
throw error;
|
|
287
|
+
await wait(Math.min(30000, interval * 2 ** retries), false);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
for (const item of current.events) {
|
|
291
|
+
settings.signal?.throwIfAborted();
|
|
292
|
+
yield item;
|
|
293
|
+
await settings.onCursor?.(item.sequence);
|
|
294
|
+
after = item.sequence;
|
|
295
|
+
}
|
|
296
|
+
if (current.has_more)
|
|
297
|
+
continue;
|
|
298
|
+
if (TERMINAL.has(current.task_status))
|
|
299
|
+
return;
|
|
300
|
+
await wait(interval, true);
|
|
301
|
+
}
|
|
302
|
+
} finally {
|
|
303
|
+
await unsubscribe?.();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return { list, append, watch };
|
|
307
|
+
}
|
|
308
|
+
export {
|
|
309
|
+
TaskEventError,
|
|
310
|
+
createTaskEventClient
|
|
311
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.0",
|
|
4
4
|
"description": "Platform SDK for SupaCloud, built on top of supabase-js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.js",
|
|
13
13
|
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./task-events": {
|
|
16
|
+
"types": "./dist/task-events.d.ts",
|
|
17
|
+
"import": "./dist/task-events.js",
|
|
18
|
+
"default": "./dist/task-events.js"
|
|
14
19
|
}
|
|
15
20
|
},
|
|
16
21
|
"files": [
|
|
@@ -20,7 +25,7 @@
|
|
|
20
25
|
"scripts": {
|
|
21
26
|
"test": "bun test",
|
|
22
27
|
"build": "bun run clean && bun run build:js && bun run build:types",
|
|
23
|
-
"build:js": "bun build src/index.ts --outdir dist --target node --external @supabase/supabase-js --external @supacloud/contracts",
|
|
28
|
+
"build:js": "bun build src/index.ts src/task-events.ts --outdir dist --target node --external @supabase/supabase-js --external @supacloud/contracts",
|
|
24
29
|
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
|
|
25
30
|
"clean": "rm -rf dist",
|
|
26
31
|
"prepublishOnly": "bun run build",
|
|
@@ -45,7 +50,7 @@
|
|
|
45
50
|
"@supabase/supabase-js": "^2.115.0"
|
|
46
51
|
},
|
|
47
52
|
"dependencies": {
|
|
48
|
-
"@supacloud/contracts": "0.
|
|
53
|
+
"@supacloud/contracts": "0.4.0"
|
|
49
54
|
},
|
|
50
55
|
"devDependencies": {
|
|
51
56
|
"@types/bun": "^1.4.2",
|