@powersync/shared-internals 1.3.1 → 1.5.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/websockets.mjs +29 -1
- package/dist/websockets.mjs.map +1 -1
- package/dist/websockets.node.mjs +29 -1
- package/dist/websockets.node.mjs.map +1 -1
- package/lib/client/BasePowerSyncDatabase.d.ts +12 -6
- package/lib/client/BasePowerSyncDatabase.js +11 -15
- package/lib/client/BasePowerSyncDatabase.js.map +1 -1
- package/lib/client/ConnectionManager.d.ts +7 -5
- package/lib/client/ConnectionManager.js +29 -15
- package/lib/client/ConnectionManager.js.map +1 -1
- package/lib/client/InternalConnector.d.ts +9 -0
- package/lib/client/InternalConnector.js +42 -0
- package/lib/client/InternalConnector.js.map +1 -0
- package/lib/client/sync/options.d.ts +4 -0
- package/lib/client/sync/options.js +7 -1
- package/lib/client/sync/options.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +7 -9
- package/lib/client/sync/stream/AbstractRemote.js +13 -5
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +12 -14
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +147 -82
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/client/sync/stream/WebSocketSupport.js +29 -1
- package/lib/client/sync/stream/WebSocketSupport.js.map +1 -1
- package/lib/client/sync/stream/core-instruction.d.ts +28 -1
- package/lib/client/sync/stream/core-instruction.js.map +1 -1
- package/lib/client/watched/DifferentialQueryProcessor.js +5 -0
- package/lib/client/watched/DifferentialQueryProcessor.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +2 -5
- package/lib/db/crud/SyncStatus.js +5 -18
- package/lib/db/crud/SyncStatus.js.map +1 -1
- package/lib/index.d.ts +3 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/utils/async.d.ts +6 -0
- package/lib/utils/async.js +31 -0
- package/lib/utils/async.js.map +1 -1
- package/lib/utils/error-serialization.d.ts +27 -0
- package/lib/utils/error-serialization.js +90 -0
- package/lib/utils/error-serialization.js.map +1 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +3 -3
- package/src/client/BasePowerSyncDatabase.ts +24 -19
- package/src/client/ConnectionManager.ts +32 -19
- package/src/client/InternalConnector.ts +68 -0
- package/src/client/sync/options.ts +8 -1
- package/src/client/sync/stream/AbstractRemote.ts +33 -25
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +177 -106
- package/src/client/sync/stream/WebSocketSupport.ts +32 -1
- package/src/client/sync/stream/core-instruction.ts +25 -2
- package/src/client/watched/DifferentialQueryProcessor.ts +5 -0
- package/src/db/crud/SyncStatus.ts +6 -19
- package/src/index.ts +3 -0
- package/src/utils/async.ts +36 -0
- package/src/utils/error-serialization.ts +116 -0
- package/src/version.ts +1 -1
package/src/utils/async.ts
CHANGED
|
@@ -183,3 +183,39 @@ export class EventQueue<T> {
|
|
|
183
183
|
};
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Runs `action` with a signal that aborts once `signal` aborts or after `timeoutMs`, whichever comes first.
|
|
189
|
+
*
|
|
190
|
+
* @param description Names the operation in the error thrown when it times out.
|
|
191
|
+
*/
|
|
192
|
+
export async function runWithTimeout<T>(
|
|
193
|
+
signal: AbortSignal | undefined,
|
|
194
|
+
timeoutMs: number,
|
|
195
|
+
description: string,
|
|
196
|
+
action: (signal: AbortSignal) => Promise<T>
|
|
197
|
+
): Promise<T> {
|
|
198
|
+
const controller = new AbortController();
|
|
199
|
+
const timeoutError = new Error(`${description} timed out after ${timeoutMs}ms`);
|
|
200
|
+
const onAbort = () => controller.abort(signal?.reason);
|
|
201
|
+
|
|
202
|
+
if (signal?.aborted) {
|
|
203
|
+
onAbort();
|
|
204
|
+
} else {
|
|
205
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
206
|
+
}
|
|
207
|
+
const timeout = setTimeout(() => controller.abort(timeoutError), timeoutMs);
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
return await action(controller.signal);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
// Some fetch implementations reject with a generic abort error instead of the signal's reason.
|
|
213
|
+
if (controller.signal.reason === timeoutError) {
|
|
214
|
+
throw timeoutError;
|
|
215
|
+
}
|
|
216
|
+
throw error;
|
|
217
|
+
} finally {
|
|
218
|
+
clearTimeout(timeout);
|
|
219
|
+
signal?.removeEventListener('abort', onAbort);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Clone-safe representation of errors crossing a worker boundary.
|
|
2
|
+
|
|
3
|
+
export const RELAY_ERROR_MARKER = '__powersyncError';
|
|
4
|
+
|
|
5
|
+
export type ErrorRelayState = 'serialized' | 'partial' | 'missing';
|
|
6
|
+
|
|
7
|
+
/** Structurally Error-like, but `instanceof Error` is false. */
|
|
8
|
+
export interface SerializedRelayError {
|
|
9
|
+
[RELAY_ERROR_MARKER]: true;
|
|
10
|
+
name: string;
|
|
11
|
+
message: string;
|
|
12
|
+
stack?: string;
|
|
13
|
+
/** SQLite errors, e.g. wa-sqlite's `SQLiteError`. */
|
|
14
|
+
code?: string | number;
|
|
15
|
+
/** HTTP/network error status, e.g. 401. */
|
|
16
|
+
status?: number;
|
|
17
|
+
cause?: SerializedRelayError | string;
|
|
18
|
+
state: ErrorRelayState;
|
|
19
|
+
relay?: { origin: string; sequence: number; errorState: ErrorRelayState; relayedAt: string };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const MAX_CAUSE_DEPTH = 5;
|
|
23
|
+
|
|
24
|
+
interface RelayContext {
|
|
25
|
+
lossy: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isErrorLike(value: unknown): value is Error {
|
|
29
|
+
return (
|
|
30
|
+
typeof value === 'object' &&
|
|
31
|
+
value !== null &&
|
|
32
|
+
typeof (value as { name?: unknown }).name === 'string' &&
|
|
33
|
+
typeof (value as { message?: unknown }).message === 'string'
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function missing(reason: unknown): SerializedRelayError {
|
|
38
|
+
return {
|
|
39
|
+
[RELAY_ERROR_MARKER]: true,
|
|
40
|
+
name: 'PowerSyncMissingRejectionReason',
|
|
41
|
+
message: `No rejection reason was provided (${reason}).`,
|
|
42
|
+
state: 'missing'
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function serializeCause(value: unknown, depth: number, ctx: RelayContext): SerializedRelayError | string {
|
|
47
|
+
if (isErrorLike(value)) {
|
|
48
|
+
if (depth <= 0) {
|
|
49
|
+
ctx.lossy = true;
|
|
50
|
+
return '[Max depth reached]';
|
|
51
|
+
}
|
|
52
|
+
return serialize(value, depth, ctx);
|
|
53
|
+
}
|
|
54
|
+
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
55
|
+
return String(value);
|
|
56
|
+
}
|
|
57
|
+
ctx.lossy = true;
|
|
58
|
+
return `[Unserializable ${typeof value}]`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function serialize(value: unknown, depth: number, ctx: RelayContext): SerializedRelayError {
|
|
62
|
+
if (isSerializedRelayError(value)) return value;
|
|
63
|
+
if (!isErrorLike(value)) return serializePlain(value);
|
|
64
|
+
|
|
65
|
+
const source = value as Error & { code?: unknown; status?: unknown; cause?: unknown };
|
|
66
|
+
const result: SerializedRelayError = {
|
|
67
|
+
[RELAY_ERROR_MARKER]: true,
|
|
68
|
+
name: source.name || 'Error',
|
|
69
|
+
message: source.message,
|
|
70
|
+
state: 'serialized'
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const stack = source.stack;
|
|
74
|
+
if (typeof stack === 'string') result.stack = stack;
|
|
75
|
+
// Not standard Error fields, but wa-sqlite errors (code) and HTTP errors (status) carry them and consumers need them as context.
|
|
76
|
+
if (typeof source.code === 'string' || typeof source.code === 'number') result.code = source.code;
|
|
77
|
+
if (typeof source.status === 'number') result.status = source.status;
|
|
78
|
+
|
|
79
|
+
if (source.cause !== undefined) result.cause = serializeCause(source.cause, depth - 1, ctx);
|
|
80
|
+
|
|
81
|
+
if (ctx.lossy && result.state !== 'missing') result.state = 'partial';
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Never stringifies an arbitrary object: it may hold credentials or a request payload. */
|
|
86
|
+
function serializePlain(value: unknown): SerializedRelayError {
|
|
87
|
+
const printable = typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
|
|
88
|
+
return {
|
|
89
|
+
[RELAY_ERROR_MARKER]: true,
|
|
90
|
+
name: typeof value === 'string' ? 'String' : 'Error',
|
|
91
|
+
message: printable ? String(value) : 'Non-Error rejection',
|
|
92
|
+
state: 'serialized'
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Serializes a caught value into a clone-safe shape. Never throws and never
|
|
98
|
+
* returns `undefined` — a missing rejection reason becomes `state: 'missing'`.
|
|
99
|
+
*/
|
|
100
|
+
export function serializeErrorForRelay(value: unknown): SerializedRelayError {
|
|
101
|
+
try {
|
|
102
|
+
if (value === undefined || value === null) return missing(value);
|
|
103
|
+
return serialize(value, MAX_CAUSE_DEPTH, { lossy: false });
|
|
104
|
+
} catch {
|
|
105
|
+
return {
|
|
106
|
+
[RELAY_ERROR_MARKER]: true,
|
|
107
|
+
name: 'Error',
|
|
108
|
+
message: 'Unable to serialize rejection reason',
|
|
109
|
+
state: 'partial'
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function isSerializedRelayError(value: unknown): value is SerializedRelayError {
|
|
115
|
+
return typeof value === 'object' && value !== null && (value as SerializedRelayError)[RELAY_ERROR_MARKER] === true;
|
|
116
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// Note: This file gets updated by tool/update_version.js script as part of the Changesets release workflow.
|
|
2
2
|
// The update happens in the changesets PR, before the package is built.
|
|
3
|
-
export const POWERSYNC_JS_VERSION = '1.
|
|
3
|
+
export const POWERSYNC_JS_VERSION = '1.5.0';
|