@electric-sql/client 1.0.3 → 1.0.5
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/cjs/index.cjs +301 -140
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +29 -8
- package/dist/index.browser.mjs +5 -5
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.d.ts +29 -8
- package/dist/index.legacy-esm.js +287 -140
- package/dist/index.legacy-esm.js.map +1 -1
- package/dist/index.mjs +303 -140
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
- package/src/client.ts +432 -207
- package/src/constants.ts +2 -0
- package/src/fetch.ts +12 -2
- package/src/helpers.ts +13 -1
- package/src/types.ts +4 -1
package/src/constants.ts
CHANGED
|
@@ -12,4 +12,6 @@ export const TABLE_QUERY_PARAM = `table`
|
|
|
12
12
|
export const WHERE_QUERY_PARAM = `where`
|
|
13
13
|
export const REPLICA_PARAM = `replica`
|
|
14
14
|
export const WHERE_PARAMS_PARAM = `params`
|
|
15
|
+
export const EXPERIMENTAL_LIVE_SSE_QUERY_PARAM = `experimental_live_sse`
|
|
15
16
|
export const FORCE_DISCONNECT_AND_REFRESH = `force-disconnect-and-refresh`
|
|
17
|
+
export const PAUSE_STREAM = `pause-stream`
|
package/src/fetch.ts
CHANGED
|
@@ -38,7 +38,8 @@ export const BackoffDefaults = {
|
|
|
38
38
|
|
|
39
39
|
export function createFetchWithBackoff(
|
|
40
40
|
fetchClient: typeof fetch,
|
|
41
|
-
backoffOptions: BackoffOptions = BackoffDefaults
|
|
41
|
+
backoffOptions: BackoffOptions = BackoffDefaults,
|
|
42
|
+
sseMode: boolean = false
|
|
42
43
|
): typeof fetch {
|
|
43
44
|
const {
|
|
44
45
|
initialDelay,
|
|
@@ -64,7 +65,16 @@ export function createFetchWithBackoff(
|
|
|
64
65
|
try {
|
|
65
66
|
const result = await fetchClient(...args)
|
|
66
67
|
if (result.ok) return result
|
|
67
|
-
|
|
68
|
+
|
|
69
|
+
const err = await FetchError.fromResponse(result, url.toString())
|
|
70
|
+
if (err.status === 409 && sseMode) {
|
|
71
|
+
// The json body is [ { headers: { control: 'must-refetch' } } ] in normal mode
|
|
72
|
+
// and is { headers: { control: 'must-refetch' } } in SSE mode
|
|
73
|
+
// So in SSE mode we need to wrap it in an array
|
|
74
|
+
err.json = [err.json]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
throw err
|
|
68
78
|
} catch (e) {
|
|
69
79
|
onFailedAttempt?.()
|
|
70
80
|
if (options?.signal?.aborted) {
|
package/src/helpers.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChangeMessage, ControlMessage, Message, Row } from './types'
|
|
1
|
+
import { ChangeMessage, ControlMessage, Message, Offset, Row } from './types'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Type guard for checking {@link Message} is {@link ChangeMessage}.
|
|
@@ -51,3 +51,15 @@ export function isUpToDateMessage<T extends Row<unknown> = Row>(
|
|
|
51
51
|
): message is ControlMessage & { up_to_date: true } {
|
|
52
52
|
return isControlMessage(message) && message.headers.control === `up-to-date`
|
|
53
53
|
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Parses the LSN from the up-to-date message and turns it into an offset.
|
|
57
|
+
* The LSN is only present in the up-to-date control message when in SSE mode.
|
|
58
|
+
* If we are not in SSE mode this function will return undefined.
|
|
59
|
+
*/
|
|
60
|
+
export function getOffset(message: ControlMessage): Offset | undefined {
|
|
61
|
+
const lsn = Number(message.headers.global_last_seen_lsn)
|
|
62
|
+
if (lsn && !isNaN(lsn)) {
|
|
63
|
+
return `${lsn}_0`
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -26,7 +26,10 @@ interface Header {
|
|
|
26
26
|
export type Operation = `insert` | `update` | `delete`
|
|
27
27
|
|
|
28
28
|
export type ControlMessage = {
|
|
29
|
-
headers: Header & {
|
|
29
|
+
headers: Header & {
|
|
30
|
+
control: `up-to-date` | `must-refetch`
|
|
31
|
+
global_last_seen_lsn?: string
|
|
32
|
+
}
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
export type ChangeMessage<T extends Row<unknown> = Row> = {
|