@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.
@@ -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.js'
7
- import type { Env } from './durable-object.js'
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
- validatePayload?: (payload: Schema.JsonValue | undefined) => void | Promise<void>
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
@@ -1,6 +1,6 @@
1
1
  import { Schema } from '@livestore/utils/effect'
2
2
 
3
- export * as WSMessage from './ws-message-types.js'
3
+ export * as WSMessage from './ws-message-types.ts'
4
4
 
5
5
  export const SearchParamsSchema = Schema.Struct({
6
6
  storeId: Schema.String,
@@ -1 +1 @@
1
- export * from './ws-impl.js'
1
+ export * from './ws-impl.ts'
@@ -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.js'
23
- import type { SyncMetadata } from '../common/ws-message-types.js'
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
- // eslint-disable-next-line unicorn/prefer-global-this
238
+
239
239
  if (typeof self !== 'undefined' && typeof self.addEventListener === 'function') {
240
240
  // TODO support an Expo equivalent for this
241
- // eslint-disable-next-line unicorn/prefer-global-this
241
+
242
242
  yield* Effect.eventListener(self, 'offline', () => Deferred.succeed(connectionClosed, void 0))
243
243
  }
244
244