@nxtedition/deepstream.io-client-js 32.0.7 → 32.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/deepstream.io-client-js",
3
- "version": "32.0.7",
3
+ "version": "32.0.9",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
package/src/client.d.ts CHANGED
@@ -87,11 +87,12 @@ export interface DeepstreamClient<
87
87
  rpc: RpcHandler<Methods>
88
88
  record: RecordHandler<Records>
89
89
  user: string | null
90
- on: (evt: EventName, callback: (...args: unknown[]) => void) => void
91
- off: (evt: EventName, callback: (...args: unknown[]) => void) => void
90
+ on: (evt: EventName | 'error', callback: (...args: unknown[]) => void) => this
91
+ off: (evt: EventName | 'error', callback: (...args: unknown[]) => void) => this
92
92
  getConnectionState: () => ConnectionStateName
93
93
  close: () => void
94
- login: unknown
94
+ login(callback: (success: boolean, authData: unknown) => void): this
95
+ login(authParams: object, callback: (success: boolean, authData: unknown) => void): this
95
96
  stats: {
96
97
  record: RecordStats
97
98
  rpc: RpcStats
@@ -188,6 +188,16 @@ expectAssignable<Promise<void>>(
188
188
  )
189
189
  expectAssignable<Promise<void>>(ds.record.update('p', 'p1', (data) => data, { timeout: 5000 }))
190
190
 
191
+ // update: updater receives version as second argument
192
+ ds.record.update('p', (data, version) => {
193
+ expectType<string>(version)
194
+ return data
195
+ })
196
+ ds.record.update('p', 'p1', (data, version) => {
197
+ expectType<string>(version)
198
+ return data
199
+ })
200
+
191
201
  // Circular
192
202
  expectAssignable<string | undefined>(await ds.record.get('c', 'a.b1'))
193
203
 
@@ -219,6 +229,19 @@ expectAssignable<Promise<typeof rec>>(rec.when({ state: 2, timeout: 5000 }))
219
229
  expectAssignable<Promise<typeof rec>>(rec.when(2, { timeout: 5000 }))
220
230
  expectAssignable<Promise<typeof rec>>(rec.when(2, { signal: new AbortController().signal }))
221
231
 
232
+ // Record.subscribe: callback receives (record, opaque)
233
+ rec.subscribe((record, opaque) => {
234
+ expectType<typeof rec>(record)
235
+ expectType<unknown>(opaque)
236
+ })
237
+ rec.subscribe((record, opaque) => {}, 'my-opaque-token')
238
+
239
+ // Record.unsubscribe: same callback signature
240
+ rec.unsubscribe((record, opaque) => {
241
+ expectType<typeof rec>(record)
242
+ expectType<unknown>(opaque)
243
+ })
244
+
222
245
  // Record.update with options
223
246
  expectAssignable<Promise<void>>(rec.update((x) => x, { signal: new AbortController().signal }))
224
247
  expectAssignable<Promise<void>>(rec.update((x) => x, { timeout: 5000 }))
@@ -229,7 +252,37 @@ expectAssignable<Promise<void>>(
229
252
  expectAssignable<Promise<void>>(rec.update('o0', (x) => x, { timeout: 5000 }))
230
253
  expectAssignable<Promise<void>>(rec.update('o0', (x) => x, { state: 2 }))
231
254
 
255
+ // Record.update: updater receives version as second argument
256
+ rec.update((x, version) => {
257
+ expectType<string>(version)
258
+ return x
259
+ })
260
+ rec.update('o0', (x, version) => {
261
+ expectType<string>(version)
262
+ return x
263
+ })
264
+
232
265
  const state = 'VOID'
233
266
  expectType<0>(ds.record.STATE[state])
234
267
  const unknownState: string = 'VOID'
235
268
  expectType<number>(ds.record.STATE[unknownState])
269
+
270
+ // record.getRecord: [Symbol.dispose] is present
271
+ const recDispose = ds.record.getRecord('o')
272
+ recDispose[Symbol.dispose]()
273
+
274
+ // record.provide: returns Disposer | void
275
+ expectAssignable<(() => void) | void>(ds.record.provide('pattern*', () => ({})))
276
+ const recordDisposer = ds.record.provide('pattern*', () => ({}))
277
+ if (recordDisposer) {
278
+ recordDisposer()
279
+ recordDisposer[Symbol.dispose]()
280
+ }
281
+
282
+ // event.provide: returns (() => void) | void
283
+ expectAssignable<(() => void) | void>(ds.event.provide('pattern*', () => {}, {}))
284
+
285
+ // client.on/off: 'error' is a valid event name
286
+ ds.on('error', (err) => {})
287
+ ds.off('error', (err) => {})
288
+ expectError(ds.on('unknownEvent', () => {}))
@@ -10,7 +10,11 @@ export default class EventHandler {
10
10
  off: (name: string, callback: (data: unknown) => void) => this
11
11
  observe: <Data>(name: string) => Observable<Data>
12
12
  emit: <Data>(name: string, data?: Data) => void
13
- provide: (pattern: string, callback: (name: string) => void, options: unknown) => () => void
13
+ provide: (
14
+ pattern: string,
15
+ callback: (name: string) => void,
16
+ options: unknown,
17
+ ) => (() => void) | void
14
18
  }
15
19
 
16
20
  export interface EventStats {
@@ -40,7 +40,7 @@ export default class RecordHandler<Records = Record<string, unknown>> {
40
40
  pattern: string,
41
41
  callback: (key: string) => unknown,
42
42
  optionsOrRecursive?: ProvideOptions | boolean,
43
- ) => Disposer
43
+ ) => Disposer | void
44
44
 
45
45
  sync: (options?: SyncOptions) => Promise<void>
46
46
 
@@ -61,14 +61,17 @@ export default class RecordHandler<Records = Record<string, unknown>> {
61
61
  update: {
62
62
  <Name extends string>(
63
63
  name: Name,
64
- updater: (data: Lookup<Records, Name>) => Lookup<Records, Name>,
64
+ updater: (data: Lookup<Records, Name>, version: string) => Lookup<Records, Name>,
65
65
  options?: UpdateOptions,
66
66
  ): Promise<void>
67
67
 
68
68
  <Name extends string, Path extends string | string[]>(
69
69
  name: Name,
70
70
  path: Path,
71
- updater: (data: Get<Lookup<Records, Name>, Path>) => Get<Lookup<Records, Name>, Path>,
71
+ updater: (
72
+ data: Get<Lookup<Records, Name>, Path>,
73
+ version: string,
74
+ ) => Get<Lookup<Records, Name>, Path>,
72
75
  options?: UpdateOptions,
73
76
  ): Promise<void>
74
77
  }
@@ -38,8 +38,15 @@ export default class Record<Data = unknown> {
38
38
 
39
39
  ref(): Record<Data>
40
40
  unref(): Record<Data>
41
- subscribe(callback: (record: Record<Data>) => void, opaque?: unknown): Record<Data>
42
- unsubscribe(callback: (record: Record<Data>) => void, opaque?: unknown): Record<Data>
41
+ [Symbol.dispose](): void
42
+ subscribe(
43
+ callback: (record: Record<Data>, opaque: unknown) => void,
44
+ opaque?: unknown,
45
+ ): Record<Data>
46
+ unsubscribe(
47
+ callback: (record: Record<Data>, opaque: unknown) => void,
48
+ opaque?: unknown,
49
+ ): Record<Data>
43
50
 
44
51
  get: {
45
52
  // with path
@@ -67,11 +74,14 @@ export default class Record<Data = unknown> {
67
74
 
68
75
  update: {
69
76
  // without path
70
- (updater: (data: Readonly<Data>) => Data, options?: UpdateOptions): Promise<void>
77
+ (
78
+ updater: (data: Readonly<Data>, version: string) => Data,
79
+ options?: UpdateOptions,
80
+ ): Promise<void>
71
81
  // with path
72
82
  <P extends string | string[]>(
73
83
  path: P,
74
- updater: (dataAtPath: Readonly<Get<Data, P>>) => Get<Data, P>,
84
+ updater: (dataAtPath: Readonly<Get<Data, P>>, version: string) => Get<Data, P>,
75
85
  options?: UpdateOptions,
76
86
  ): Promise<void>
77
87
  }
@@ -10,8 +10,11 @@ export default class RpcHandler<
10
10
 
11
11
  provide: <Name extends keyof Methods>(
12
12
  name: Name,
13
- callback: (args: Methods[Name][0], response: RpcResponse<Methods[Name][1]>) => void,
14
- ) => UnprovideFn
13
+ callback: (
14
+ args: Methods[Name][0],
15
+ response: RpcResponse<Methods[Name][1]>,
16
+ ) => Methods[Name][1] | Promise<Methods[Name][1]> | void,
17
+ ) => UnprovideFn | void
15
18
 
16
19
  unprovide: <Name extends keyof Methods>(name: Name) => void
17
20
 
@@ -0,0 +1,24 @@
1
+ import make from '../client.js'
2
+ import { expectAssignable, expectError, expectType } from 'tsd'
3
+
4
+ interface Methods extends Record<string, [unknown, unknown]> {
5
+ greet: [{ name: string }, { message: string }]
6
+ }
7
+
8
+ const ds = make<Record<string, unknown>, Methods>('')
9
+
10
+ // provide: callback may return void, a value, or a Promise — all valid
11
+ ds.rpc.provide('greet', (_args, _response) => {})
12
+ ds.rpc.provide('greet', (_args, _response) => ({ message: 'hello' }))
13
+ ds.rpc.provide('greet', async (_args, _response) => ({ message: 'hello' }))
14
+
15
+ // provide: returning the wrong shape is an error
16
+ expectError(ds.rpc.provide('greet', (_args, _response) => ({ notMessage: 'hello' })))
17
+
18
+ // provide: response.completed is boolean
19
+ ds.rpc.provide('greet', (_args, response) => {
20
+ expectType<boolean>(response.completed)
21
+ })
22
+
23
+ // provide: return type is UnprovideFn | void
24
+ expectAssignable<(() => void) | void>(ds.rpc.provide('greet', () => {}))
@@ -1,4 +1,5 @@
1
1
  export default class RpcResponse<Data> {
2
+ completed: boolean
2
3
  reject: () => void
3
4
  error: (error: Error | string) => void
4
5
  send: (data: Data) => void
@@ -6,6 +6,7 @@ export default class Listener {
6
6
  constructor(topic, pattern, callback, handler, { recursive = false, stringify = null } = {}) {
7
7
  this._topic = topic
8
8
  this._pattern = pattern
9
+ this._expr = new RegExp(pattern)
9
10
  this._callback = callback
10
11
  this._handler = handler
11
12
  this._client = this._handler._client
@@ -54,6 +55,11 @@ export default class Listener {
54
55
  return
55
56
  }
56
57
 
58
+ if (!this._expr.test(name)) {
59
+ this._error(name, 'invalid add: name does not match pattern')
60
+ return
61
+ }
62
+
57
63
  // TODO (refactor): Move to class
58
64
  const provider = {
59
65
  name,
@@ -37,6 +37,7 @@ export default class Listener {
37
37
 
38
38
  this._topic = topic
39
39
  this._pattern = pattern
40
+ this._expr = new RegExp(pattern)
40
41
  this._callback = callback
41
42
  this._handler = handler
42
43
  this._client = this._handler._client
@@ -69,6 +70,11 @@ export default class Listener {
69
70
  return
70
71
  }
71
72
 
73
+ if (!this._expr.test(name)) {
74
+ this._error(name, 'invalid accept: name does not match pattern')
75
+ return
76
+ }
77
+
72
78
  let value$
73
79
  try {
74
80
  value$ = this._callback(name)