@livestore/sync-cf 0.3.2-dev.0 → 0.3.2-dev.10
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/.tsbuildinfo +1 -1
- package/dist/cf-worker/durable-object.d.ts +61 -7
- package/dist/cf-worker/durable-object.d.ts.map +1 -1
- package/dist/cf-worker/durable-object.js +12 -14
- package/dist/cf-worker/durable-object.js.map +1 -1
- package/dist/cf-worker/mod.d.ts +2 -2
- package/dist/cf-worker/mod.js +2 -2
- package/dist/cf-worker/worker.d.ts +14 -4
- package/dist/cf-worker/worker.d.ts.map +1 -1
- package/dist/cf-worker/worker.js +4 -3
- package/dist/cf-worker/worker.js.map +1 -1
- package/dist/common/mod.d.ts +1 -1
- package/dist/common/mod.js +1 -1
- package/dist/sync-impl/mod.d.ts +1 -1
- package/dist/sync-impl/mod.js +1 -1
- package/dist/sync-impl/ws-impl.d.ts +1 -1
- package/dist/sync-impl/ws-impl.js +1 -3
- package/dist/sync-impl/ws-impl.js.map +1 -1
- package/package.json +7 -19
- package/src/cf-worker/durable-object.ts +21 -17
- package/src/cf-worker/mod.ts +2 -2
- package/src/cf-worker/worker.ts +12 -6
- package/src/common/mod.ts +1 -1
- package/src/sync-impl/mod.ts +1 -1
- package/src/sync-impl/ws-impl.ts +4 -4
package/src/cf-worker/worker.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { UnexpectedError } from '@livestore/common'
|
|
|
3
3
|
import type { Schema } from '@livestore/utils/effect'
|
|
4
4
|
import { Effect, UrlParams } from '@livestore/utils/effect'
|
|
5
5
|
|
|
6
|
-
import { SearchParamsSchema } from '../common/mod.
|
|
7
|
-
import type { Env } from './durable-object.
|
|
6
|
+
import { SearchParamsSchema } from '../common/mod.ts'
|
|
7
|
+
import type { Env } from './durable-object.ts'
|
|
8
8
|
|
|
9
9
|
// Redeclaring Response to Cloudflare Worker Response type to avoid lib.dom type clashing
|
|
10
10
|
declare const Response: typeof CfWorker.Response
|
|
@@ -46,7 +46,12 @@ export type CFWorker<TEnv extends Env = Env, _T extends CfWorker.Rpc.DurableObje
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export type MakeWorkerOptions<TEnv extends Env = Env> = {
|
|
49
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Validates the payload during WebSocket connection establishment.
|
|
51
|
+
* Note: This runs only at connection time, not for individual push events.
|
|
52
|
+
* For push event validation, use the `onPush` callback in the durable object.
|
|
53
|
+
*/
|
|
54
|
+
validatePayload?: (payload: Schema.JsonValue | undefined, context: { storeId: string }) => void | Promise<void>
|
|
50
55
|
/** @default false */
|
|
51
56
|
enableCORS?: boolean
|
|
52
57
|
durableObject?: {
|
|
@@ -120,7 +125,8 @@ export const makeWorker = <
|
|
|
120
125
|
*
|
|
121
126
|
* @example
|
|
122
127
|
* ```ts
|
|
123
|
-
* const validatePayload = (payload: Schema.JsonValue | undefined) => {
|
|
128
|
+
* const validatePayload = (payload: Schema.JsonValue | undefined, context: { storeId: string }) => {
|
|
129
|
+
* console.log(`Validating connection for store: ${context.storeId}`)
|
|
124
130
|
* if (payload?.authToken !== 'insecure-token-change-me') {
|
|
125
131
|
* throw new Error('Invalid auth token')
|
|
126
132
|
* }
|
|
@@ -150,7 +156,7 @@ export const handleWebSocket = <
|
|
|
150
156
|
options: {
|
|
151
157
|
headers?: CfWorker.HeadersInit
|
|
152
158
|
durableObject?: MakeWorkerOptions<TEnv>['durableObject']
|
|
153
|
-
validatePayload?: (payload: Schema.JsonValue | undefined) => void | Promise<void>
|
|
159
|
+
validatePayload?: (payload: Schema.JsonValue | undefined, context: { storeId: string }) => void | Promise<void>
|
|
154
160
|
},
|
|
155
161
|
): Promise<CfWorker.Response> =>
|
|
156
162
|
Effect.gen(function* () {
|
|
@@ -169,7 +175,7 @@ export const handleWebSocket = <
|
|
|
169
175
|
const { storeId, payload } = paramsResult.right
|
|
170
176
|
|
|
171
177
|
if (options.validatePayload !== undefined) {
|
|
172
|
-
const result = yield* Effect.promise(async () => options.validatePayload!(payload)).pipe(
|
|
178
|
+
const result = yield* Effect.promise(async () => options.validatePayload!(payload, { storeId })).pipe(
|
|
173
179
|
UnexpectedError.mapToUnexpectedError,
|
|
174
180
|
Effect.either,
|
|
175
181
|
)
|
package/src/common/mod.ts
CHANGED
package/src/sync-impl/mod.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './ws-impl.
|
|
1
|
+
export * from './ws-impl.ts'
|
package/src/sync-impl/ws-impl.ts
CHANGED
|
@@ -19,8 +19,8 @@ import {
|
|
|
19
19
|
} from '@livestore/utils/effect'
|
|
20
20
|
import { nanoid } from '@livestore/utils/nanoid'
|
|
21
21
|
|
|
22
|
-
import { SearchParamsSchema, WSMessage } from '../common/mod.
|
|
23
|
-
import type { SyncMetadata } from '../common/ws-message-types.
|
|
22
|
+
import { SearchParamsSchema, WSMessage } from '../common/mod.ts'
|
|
23
|
+
import type { SyncMetadata } from '../common/ws-message-types.ts'
|
|
24
24
|
|
|
25
25
|
export interface WsSyncOptions {
|
|
26
26
|
url: string
|
|
@@ -235,10 +235,10 @@ const connect = (wsUrl: string) =>
|
|
|
235
235
|
|
|
236
236
|
// NOTE it seems that this callback doesn't work reliably on a worker but only via `window.addEventListener`
|
|
237
237
|
// We might need to proxy the event from the main thread to the worker if we want this to work reliably.
|
|
238
|
-
|
|
238
|
+
|
|
239
239
|
if (typeof self !== 'undefined' && typeof self.addEventListener === 'function') {
|
|
240
240
|
// TODO support an Expo equivalent for this
|
|
241
|
-
|
|
241
|
+
|
|
242
242
|
yield* Effect.eventListener(self, 'offline', () => Deferred.succeed(connectionClosed, void 0))
|
|
243
243
|
}
|
|
244
244
|
|