@dittolive/ditto 4.7.4 → 4.8.0-rc.2

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.
Files changed (126) hide show
  1. package/DittoReactNative.podspec +33 -2
  2. package/README.md +2 -2
  3. package/node/ditto.cjs.js +341 -376
  4. package/node/ditto.darwin-arm64.node +0 -0
  5. package/node/ditto.darwin-x64.node +0 -0
  6. package/node/ditto.linux-arm.node +0 -0
  7. package/node/ditto.linux-arm64.node +0 -0
  8. package/node/ditto.linux-x64.node +0 -0
  9. package/node/ditto.win32-x64.node +0 -0
  10. package/package.json +2 -2
  11. package/react-native/android/build.gradle +24 -64
  12. package/react-native/android/cpp-adapter.cpp +37 -104
  13. package/react-native/android/src/main/AndroidManifestNew.xml +2 -0
  14. package/react-native/android/src/main/java/com/dittolive/rnsdk/DittoRNSDKModule.java +5 -70
  15. package/react-native/cpp/include/ConnectionRequest.h +1 -1
  16. package/react-native/cpp/include/IO.h +2 -0
  17. package/react-native/cpp/include/Logger.h +2 -1
  18. package/react-native/cpp/include/Misc.h +1 -0
  19. package/react-native/cpp/include/main.h +4 -2
  20. package/react-native/cpp/src/Attachment.cpp +1 -3
  21. package/react-native/cpp/src/ConnectionRequest.cpp +1 -1
  22. package/react-native/cpp/src/IO.cpp +79 -0
  23. package/react-native/cpp/src/Logger.cpp +63 -0
  24. package/react-native/cpp/src/Misc.cpp +21 -0
  25. package/react-native/cpp/src/main.cpp +10 -4
  26. package/react-native/ditto.es6.js +2 -0
  27. package/react-native/dittoffi/dittoffi.h +137 -40
  28. package/react-native/ios/DittoRNSDK.mm +19 -124
  29. package/react-native.config.js +2 -1
  30. package/types/ditto.d.ts +2412 -2032
  31. package/web/ditto.es6.js +1 -1
  32. package/web/ditto.umd.js +1 -1
  33. package/web/ditto.wasm +0 -0
  34. package/node/transports.darwin-arm64.node +0 -0
  35. package/node/transports.darwin-x64.node +0 -0
  36. package/react-native/android/.gradle/8.9/checksums/checksums.lock +0 -0
  37. package/react-native/android/.gradle/8.9/dependencies-accessors/gc.properties +0 -0
  38. package/react-native/android/.gradle/8.9/fileChanges/last-build.bin +0 -0
  39. package/react-native/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
  40. package/react-native/android/.gradle/8.9/gc.properties +0 -0
  41. package/react-native/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  42. package/react-native/android/.gradle/buildOutputCleanup/cache.properties +0 -2
  43. package/react-native/android/.gradle/vcs-1/gc.properties +0 -0
  44. package/react-native/lib/commonjs/ditto.rn.js +0 -93
  45. package/react-native/lib/commonjs/ditto.rn.js.map +0 -1
  46. package/react-native/lib/commonjs/index.js +0 -61
  47. package/react-native/lib/commonjs/index.js.map +0 -1
  48. package/react-native/lib/module/ditto.rn.js +0 -89
  49. package/react-native/lib/module/ditto.rn.js.map +0 -1
  50. package/react-native/lib/module/index.js +0 -27
  51. package/react-native/lib/module/index.js.map +0 -1
  52. package/react-native/lib/typescript/ditto.rn.d.ts +0 -15
  53. package/react-native/lib/typescript/ditto.rn.d.ts.map +0 -1
  54. package/react-native/lib/typescript/index.d.ts +0 -1
  55. package/react-native/lib/typescript/index.d.ts.map +0 -1
  56. package/react-native/src/ditto.rn.ts +0 -123
  57. package/react-native/src/environment/environment.fallback.ts +0 -4
  58. package/react-native/src/index.ts +0 -29
  59. package/react-native/src/sources/@cbor-redux.ts +0 -2
  60. package/react-native/src/sources/@ditto.core.ts +0 -1
  61. package/react-native/src/sources/@environment.ts +0 -1
  62. package/react-native/src/sources/attachment-fetch-event.ts +0 -54
  63. package/react-native/src/sources/attachment-fetcher-manager.ts +0 -145
  64. package/react-native/src/sources/attachment-fetcher.ts +0 -265
  65. package/react-native/src/sources/attachment-token.ts +0 -129
  66. package/react-native/src/sources/attachment.ts +0 -121
  67. package/react-native/src/sources/augment.ts +0 -108
  68. package/react-native/src/sources/authenticator.ts +0 -314
  69. package/react-native/src/sources/base-pending-cursor-operation.ts +0 -255
  70. package/react-native/src/sources/base-pending-id-specific-operation.ts +0 -112
  71. package/react-native/src/sources/bridge.ts +0 -557
  72. package/react-native/src/sources/build-time-constants.ts +0 -8
  73. package/react-native/src/sources/cbor.ts +0 -20
  74. package/react-native/src/sources/collection-interface.ts +0 -73
  75. package/react-native/src/sources/collection.ts +0 -219
  76. package/react-native/src/sources/collections-event.ts +0 -99
  77. package/react-native/src/sources/connection-request.ts +0 -142
  78. package/react-native/src/sources/counter.ts +0 -82
  79. package/react-native/src/sources/ditto.ts +0 -991
  80. package/react-native/src/sources/document-id.ts +0 -163
  81. package/react-native/src/sources/document-path.ts +0 -308
  82. package/react-native/src/sources/document.ts +0 -237
  83. package/react-native/src/sources/epilogue.ts +0 -32
  84. package/react-native/src/sources/error-codes.ts +0 -114
  85. package/react-native/src/sources/error.ts +0 -256
  86. package/react-native/src/sources/essentials.ts +0 -81
  87. package/react-native/src/sources/ffi-error.ts +0 -134
  88. package/react-native/src/sources/ffi.ts +0 -2190
  89. package/react-native/src/sources/identity.ts +0 -163
  90. package/react-native/src/sources/init.ts +0 -71
  91. package/react-native/src/sources/internal.ts +0 -143
  92. package/react-native/src/sources/keep-alive.ts +0 -73
  93. package/react-native/src/sources/key-path.ts +0 -198
  94. package/react-native/src/sources/live-query-event.ts +0 -208
  95. package/react-native/src/sources/live-query-manager.ts +0 -110
  96. package/react-native/src/sources/live-query.ts +0 -167
  97. package/react-native/src/sources/logger.ts +0 -196
  98. package/react-native/src/sources/main.ts +0 -61
  99. package/react-native/src/sources/observer-manager.ts +0 -185
  100. package/react-native/src/sources/observer.ts +0 -79
  101. package/react-native/src/sources/pending-collections-operation.ts +0 -241
  102. package/react-native/src/sources/pending-cursor-operation.ts +0 -218
  103. package/react-native/src/sources/pending-id-specific-operation.ts +0 -218
  104. package/react-native/src/sources/presence-manager.ts +0 -170
  105. package/react-native/src/sources/presence.ts +0 -427
  106. package/react-native/src/sources/query-result-item.ts +0 -131
  107. package/react-native/src/sources/query-result.ts +0 -55
  108. package/react-native/src/sources/register.ts +0 -95
  109. package/react-native/src/sources/small-peer-info.ts +0 -166
  110. package/react-native/src/sources/static-tcp-client.ts +0 -8
  111. package/react-native/src/sources/store-observer.ts +0 -170
  112. package/react-native/src/sources/store.ts +0 -630
  113. package/react-native/src/sources/subscription-manager.ts +0 -99
  114. package/react-native/src/sources/subscription.ts +0 -89
  115. package/react-native/src/sources/sync-subscription.ts +0 -90
  116. package/react-native/src/sources/sync.ts +0 -561
  117. package/react-native/src/sources/test-helpers.ts +0 -24
  118. package/react-native/src/sources/transport-conditions-manager.ts +0 -104
  119. package/react-native/src/sources/transport-config.ts +0 -430
  120. package/react-native/src/sources/update-result.ts +0 -66
  121. package/react-native/src/sources/update-results-map.ts +0 -65
  122. package/react-native/src/sources/websocket-client.ts +0 -7
  123. package/react-native/src/sources/write-transaction-collection.ts +0 -122
  124. package/react-native/src/sources/write-transaction-pending-cursor-operation.ts +0 -101
  125. package/react-native/src/sources/write-transaction-pending-id-specific-operation.ts +0 -74
  126. package/react-native/src/sources/write-transaction.ts +0 -121
@@ -1,73 +0,0 @@
1
- //
2
- // Copyright © 2023 DittoLive Incorporated. All rights reserved.
3
- //
4
-
5
- import type { DocumentValue } from './document'
6
- import type { DocumentID, DocumentIDValue } from './document-id'
7
- import type { WriteStrategy, QueryArguments } from './essentials'
8
- import type { Store } from './store'
9
- import type { BasePendingCursorOperation } from './base-pending-cursor-operation'
10
- import type { BasePendingIDSpecificOperation } from './base-pending-id-specific-operation'
11
-
12
- export type UpsertOptions = {
13
- /**
14
- * Specifies the desired strategy for inserting a document. The default
15
- * strategy is `merge`. See {@link WriteStrategy} for more information.
16
- */
17
- writeStrategy?: WriteStrategy
18
- }
19
-
20
- export interface CollectionInterface {
21
- /** The name of the collection. */
22
- readonly name: string
23
-
24
- /**
25
- * The store this collection belongs to.
26
- * @internal
27
- */
28
- readonly store: Store
29
-
30
- /**
31
- * Search for documents in this collection using the provided query string.
32
- *
33
- * @param query The query to run against the collection.
34
- * @param queryArgs These arguments replace placeholders in the provided
35
- * query.
36
- */
37
- find(query: string, queryArgs?: QueryArguments): BasePendingCursorOperation
38
-
39
- /**
40
- * Convenience method, equivalent to calling {@link find | find()} and passing
41
- * the query `"true"`.
42
- */
43
- findAll(): BasePendingCursorOperation
44
-
45
- /**
46
- * Find documents given a specific ID.
47
- *
48
- * Use the returned cursor instance to chain operations on the search result.
49
- *
50
- * @param id The document's identifier
51
- */
52
- findByID(id: DocumentID | DocumentIDValue): BasePendingIDSpecificOperation
53
-
54
- /**
55
- * TEMPORARY: helper to deal with non-canonical IDs.
56
- *
57
- * @internal
58
- */
59
- findByIDCBOR(idCBOR: Uint8Array): BasePendingIDSpecificOperation
60
-
61
- /**
62
- * Inserts a new document into the collection and returns its ID.
63
- *
64
- * If the document already exists, the contents of both are merged by default.
65
- * You can change this by providing a different `writeStrategy` via `options`.
66
- *
67
- * @param value The content of the document to be inserted or updated. Must
68
- * not contain any non-finite numbers (NaN, Infinity, -Infinity).
69
- * @param options Change defaults for the behavior of the operation, such as
70
- * the write strategy.
71
- */
72
- upsert(value: DocumentValue, options: UpsertOptions): Promise<DocumentID>
73
- }
@@ -1,219 +0,0 @@
1
- //
2
- // Copyright © 2021 DittoLive Incorporated. All rights reserved.
3
- //
4
-
5
- import * as FFI from './ffi'
6
- import * as Environment from './@environment'
7
-
8
- import { QueryArguments } from './essentials'
9
- import { CBOR } from './cbor'
10
- import { DocumentID } from './document-id'
11
- import { Bridge } from './bridge'
12
- import { Attachment } from './attachment'
13
- import { AttachmentToken } from './attachment-token'
14
- import { AttachmentFetcher } from './attachment-fetcher'
15
- import { PendingCursorOperation } from './pending-cursor-operation'
16
- import { PendingIDSpecificOperation } from './pending-id-specific-operation'
17
-
18
- import { performAsyncToWorkaroundNonAsyncFFIAPI } from './internal'
19
- import { desugarJSObject } from './augment'
20
-
21
- import type { TypedAttachmentToken } from './attachment-token'
22
- import type { Store } from './store'
23
- import type { DocumentValue } from './document'
24
- import type { DocumentIDValue } from './document-id'
25
- import type { CollectionInterface, UpsertOptions } from './collection-interface'
26
- import type { AttachmentFetchEvent } from './attachment-fetch-event'
27
-
28
- /**
29
- * Represents a collection of a Ditto store.
30
- *
31
- * This is the entrypoint for inserting documents into a collection, as well as
32
- * querying a collection. You can get a collection by calling
33
- * {@link Store.collection | collection()} on a {@link Store} of a {@link Ditto}
34
- * object.
35
- */
36
- export class Collection implements CollectionInterface {
37
- /** The name of the collection. */
38
- readonly name: string
39
-
40
- /** The store this collection belongs to. */
41
- readonly store: Store
42
-
43
- /**
44
- * Generates a {@link PendingCursorOperation} using the provided query.
45
- *
46
- * The returned object can be used to find and return the documents or you can
47
- * chain a call to `observeLocal()` or `subscribe()` if you want to get
48
- * updates about the list of matching documents over time. It can also be used
49
- * to update, remove or evict the matching documents.
50
- *
51
- * You can incorporate dynamic data into the query string with placeholders in
52
- * the form of `$args.my_arg_name`, along with providing an accompanying
53
- * dictionary in the form of `{ "my_arg_name": "some value" }`. The
54
- * placeholders will be appropriately replaced by the corresponding argument
55
- * contained in `queryArgs`. This includes handling things like wrapping
56
- * strings in quotation marks and arrays in square brackets, for example.
57
- *
58
- * Find more information about the query string format in the documentation's
59
- * section on {@link https://docs.ditto.live/javascript/common/concepts/querying Querying}
60
- *
61
- * @param query The query to run against the collection.
62
- * @param queryArgs The arguments to use to replace placeholders in the
63
- * provided query.
64
- */
65
- find(query: string, queryArgs?: QueryArguments): PendingCursorOperation {
66
- return new PendingCursorOperation(query, queryArgs ?? null, this)
67
- }
68
-
69
- findAll(): PendingCursorOperation {
70
- return this.find('true')
71
- }
72
-
73
- /**
74
- * Generates a {@link PendingIDSpecificOperation} with the provided document
75
- * ID.
76
- *
77
- * The returned object can be used to find and return the document or you can
78
- * chain a call to
79
- * {@link PendingIDSpecificOperation.observeLocal | observeLocal()}, or
80
- * {@link PendingIDSpecificOperation.subscribe | subscribe()} if you want to
81
- * get updates about the document over time. It can also be used to update,
82
- * remove or evict the document.
83
- *
84
- * @param id The ID of the document to find.
85
- */
86
- findByID(id: DocumentID | DocumentIDValue): PendingIDSpecificOperation {
87
- const documentID: DocumentID = id instanceof DocumentID ? id : new DocumentID(id)
88
- return new PendingIDSpecificOperation(documentID, this)
89
- }
90
-
91
- async upsert(value: DocumentValue, options: UpsertOptions = {}): Promise<DocumentID> {
92
- const ditto = this.store.ditto
93
- const dittoHandle = Bridge.ditto.handleFor(ditto)
94
-
95
- return ditto.deferCloseAsync(async () => {
96
- const writeStrategy = options.writeStrategy ?? 'merge'
97
-
98
- const documentValueJSON = desugarJSObject(value)
99
- const documentValueCBOR = CBOR.encode(documentValueJSON)
100
-
101
- const idCBOR = await performAsyncToWorkaroundNonAsyncFFIAPI(async () => {
102
- return await FFI.collectionInsertValue(dittoHandle.deref(), this.name, documentValueCBOR, writeStrategy, undefined)
103
- })
104
-
105
- return new DocumentID(idCBOR, true)
106
- })
107
- }
108
-
109
- /**
110
- * Creates a new {@link Attachment} object, which can then be inserted into a
111
- * document. Node only, throws when running in the web browser.
112
- *
113
- * The file residing at the provided path will be copied into Ditto's store.
114
- * The {@link Attachment} object that is returned is what you can then use to
115
- * insert an attachment into a document.
116
- *
117
- * You can provide metadata about the attachment, which will be replicated to
118
- * other peers alongside the file attachment.
119
- *
120
- * Below is a snippet to show how you can use the
121
- * {@link newAttachment | newAttachment()} functionality to insert an
122
- * attachment into a document.
123
- *
124
- * ``` JavaScript
125
- * const attachment = await collection.newAttachment('/path/to/my/file.pdf')
126
- * await collection.upsert({ _id: '123', attachment, other: 'some-string' })
127
- * }
128
- * ```
129
- *
130
- * @param pathOrData The path to the file that you want to create an
131
- * attachment with or the raw data.
132
- *
133
- * @param metadata Metadata relating to the attachment.
134
- *
135
- * @deprecated Use {@link Store.newAttachment | ditto.store.newAttachment() }
136
- * instead.
137
- */
138
- async newAttachment(pathOrData: string | Uint8Array, metadata: { [key: string]: string } = {}): Promise<Attachment> {
139
- const ditto = this.store.ditto
140
- const dittoHandle = Bridge.ditto.handleFor(ditto)
141
-
142
- return ditto.deferCloseAsync(async () => {
143
- const { id, len, handle } = await (async () => {
144
- if (typeof pathOrData === 'string') {
145
- if (Environment.isWebBuild) {
146
- throw new Error(`Can't create attachment from file when running in the browser. Please pass the raw data (as a Uint8Array) instead.`)
147
- }
148
-
149
- if (Environment.isNodeBuild) {
150
- return FFI.dittoNewAttachmentFromFile(dittoHandle.deref(), pathOrData as string, 'Copy')
151
- }
152
- }
153
-
154
- if (pathOrData instanceof Uint8Array) {
155
- return await FFI.dittoNewAttachmentFromBytes(dittoHandle.deref(), pathOrData)
156
- }
157
-
158
- throw new Error(`Can't create new attachment, only file path as string or raw data as Uint8Array are supported, but got: ${typeof pathOrData}, ${pathOrData}`)
159
- })()
160
-
161
- const attachmentTokenJSON = { _id: id, _len: len, _meta: { ...metadata } }
162
- attachmentTokenJSON[FFI.DittoCRDTTypeKey] = FFI.DittoCRDTType.attachment
163
-
164
- const attachmentToken = new AttachmentToken(attachmentTokenJSON as TypedAttachmentToken)
165
- const attachment = new Attachment(ditto, attachmentToken)
166
-
167
- return Bridge.attachment.bridge(handle, () => attachment)
168
- })
169
- }
170
-
171
- /**
172
- * Trigger an attachment to be downloaded locally to the device and observe
173
- * its progress as it does so.
174
- *
175
- * When you encounter a document that contains an attachment the attachment
176
- * will not automatically be downloaded along with the document. You trigger
177
- * an attachment to be downloaded locally to a device by calling this method.
178
- * It will report events relating to the attachment fetch attempt as it tries
179
- * to download it. The `eventHandler` block may be called multiple times with
180
- * progress events. It will then be called with either a `Completed` event or
181
- * a `Deleted` event. If downloading the attachment succeeds then the
182
- * `Completed` event that the `eventHandler` will be called with will hold a
183
- * reference to the downloaded attachment.
184
- *
185
- * @param token The {@link AttachmentToken} relevant to the attachment that
186
- * you wish to download and observe. Throws if token is invalid.
187
- *
188
- * @param eventHandler An optional callback that will be called when there is
189
- * an update to the status of the attachment fetch attempt.
190
- *
191
- * @return An `AttachmentFetcher` object, which must be kept alive for the
192
- * fetch request to proceed and for you to be notified about the attachment's
193
- * fetch status changes.
194
- *
195
- * @deprecated Use
196
- * {@link Store.fetchAttachment | ditto.store.fetchAttachment() } instead.
197
- */
198
- fetchAttachment(token: AttachmentToken, eventHandler?: (event: AttachmentFetchEvent) => void): AttachmentFetcher {
199
- if (token == null || !(token instanceof AttachmentToken)) {
200
- throw new Error(`Invalid attachment token: ${token}`)
201
- }
202
- const ditto = this.store.ditto
203
- return ditto.deferClose(() => {
204
- return ditto.attachmentFetcherManager.startAttachmentFetcher(token, eventHandler)
205
- })
206
- }
207
-
208
- /** @internal */
209
- constructor(name: string, store: Store) {
210
- this.name = name
211
- this.store = store
212
- }
213
-
214
- /** @internal */
215
- findByIDCBOR(idCBOR: Uint8Array): PendingIDSpecificOperation {
216
- const documentID = new DocumentID(idCBOR, true, true)
217
- return new PendingIDSpecificOperation(documentID, this)
218
- }
219
- }
@@ -1,99 +0,0 @@
1
- //
2
- // Copyright (c) 2020 - 2021 DittoLive Inc. All rights reserved.
3
- //
4
-
5
- import type { Collection } from './collection'
6
- import type { LiveQueryMove } from './live-query-event'
7
-
8
- // -----------------------------------------------------------------------------
9
-
10
- /** @internal */
11
- export interface CollectionsEventParams {
12
- isInitial: boolean
13
- collections: Collection[]
14
- oldCollections: Collection[]
15
- insertions: number[]
16
- deletions: number[]
17
- updates: number[]
18
- moves: LiveQueryMove[]
19
- }
20
-
21
- // -----------------------------------------------------------------------------
22
-
23
- /**
24
- * Provides information about the changes that have occurred in relation to an
25
- * event delivered when observing the collections in a {@link Store}.
26
- *
27
- * It contains information about the collections that are known about as well as
28
- * the collections that were previously known about in the previous event, along
29
- * with information about what collections have been inserted, deleted, updated,
30
- * or moved since the last event.
31
- */
32
- export class CollectionsEvent {
33
- /**
34
- * Indicates whether or not this is the first event to be delivered when
35
- * observing collections in the store.
36
- */
37
- readonly isInitial: boolean
38
-
39
- /**
40
- * A list of all of the known collections.
41
- */
42
- readonly collections: Collection[]
43
-
44
- /**
45
- * A list of all of the known collections at the time the previous event was
46
- * delivered.
47
- */
48
- readonly oldCollections: Collection[]
49
-
50
- /**
51
- * A list of the indexes in the list of currently known about collections at
52
- * which new collections have been inserted.
53
- */
54
- readonly insertions: number[]
55
-
56
- /**
57
- * A list of the indexes in the list of previously known about collections at
58
- * which collections have been removed.
59
- */
60
- readonly deletions: number[]
61
-
62
- /**
63
- * A list of the indexes in the list of currently known about collections at
64
- * which pre-existing collections have been updated.
65
- */
66
- readonly updates: number[]
67
-
68
- /**
69
- * A list of the tuples that provides the indexes, in relation to the list of
70
- * previously known about collections, that already known about collections
71
- * have moved from and the indexes, in relation to the list of currently known
72
- * about collections, that the collections have moved to.
73
- */
74
- readonly moves: LiveQueryMove[]
75
-
76
- /** @internal */
77
- static initial(collections: Collection[]): CollectionsEvent {
78
- return new CollectionsEvent({
79
- isInitial: true,
80
- collections: collections,
81
- oldCollections: [],
82
- insertions: [],
83
- deletions: [],
84
- updates: [],
85
- moves: [],
86
- })
87
- }
88
-
89
- /** @internal */
90
- constructor(params: CollectionsEventParams) {
91
- this.isInitial = params.isInitial
92
- this.collections = params.collections
93
- this.oldCollections = params.oldCollections
94
- this.insertions = params.insertions
95
- this.deletions = params.deletions
96
- this.updates = params.updates
97
- this.moves = params.moves
98
- }
99
- }
@@ -1,142 +0,0 @@
1
- //
2
- // Copyright © 2024 DittoLive Incorporated. All rights reserved.
3
- //
4
-
5
- import { Bridge } from './bridge'
6
- import * as FFI from './ffi'
7
-
8
- import type { ConnectionType, Peer, Presence } from './presence'
9
-
10
- /**
11
- * Indicates whether a connection request should be authorized.
12
- *
13
- * `allow` if the request should be authorized, `deny` otherwise.
14
- */
15
- export type ConnectionRequestAuthorization = 'allow' | 'deny'
16
-
17
- /**
18
- * A handler for connection requests from other peers.
19
- *
20
- * Set a `ConnectionRequestHandler` on
21
- * {@link Presence.connectionRequestHandler | `ditto.presence.connectionRequestHandler`}
22
- * to allow or deny connection requests from other peers.
23
- *
24
- * @param connectionRequest - Contains information about the remote peer that
25
- * can be used to make an authorization decision.
26
- *
27
- * @returns `allow` if the request should be authorized, `deny` otherwise.
28
- *
29
- * @see
30
- * {@link Presence.connectionRequestHandler | `ditto.presence.connectionRequestHandler`}
31
- * for details on the connection request API.
32
- */
33
- export type ConnectionRequestHandler = (connectionRequest: ConnectionRequest) => Promise<ConnectionRequestAuthorization>
34
-
35
- /**
36
- * Contains information about a remote peer that has requested a connection.
37
- *
38
- * Connection requests and their authorization are scoped to a specific Ditto
39
- * peer and connection type.
40
- */
41
- export class ConnectionRequest {
42
- /**
43
- * The unique peer key of the remote peer.
44
- *
45
- * @see field `peerKeyString` on {@link Peer} for more information on peer
46
- * keys.
47
- */
48
- get peerKeyString(): string {
49
- return FFI.connectionRequestPeerKeyString(this.deref())
50
- }
51
-
52
- /**
53
- * Metadata associated with the remote peer.
54
- *
55
- * This is an empty object if the remote peer has not set any metadata, or
56
- * when this request is for a WebSocket connection (which does not currently
57
- * support peer metadata).
58
- *
59
- * Set peer metadata for the local peer using {@link Presence.peerMetadata} or
60
- * {@link Presence.peerMetadataJSONString}.
61
- *
62
- * This is a convenience property that wraps
63
- * {@link peerMetadataJSONString | `peerMetadataJSONString()`}.
64
- */
65
- get peerMetadata(): Record<string, any> {
66
- return JSON.parse(this.peerMetadataJSONString)
67
- }
68
-
69
- /**
70
- * JSON-encoded metadata associated with the remote peer.
71
- *
72
- * This is a JSON string representing an empty dictionary if the remote peer
73
- * has not set any metadata or when this request is for a WebSocket
74
- * connection.
75
- *
76
- * Set peer metadata for the local peer using {@link Presence.peerMetadata} or
77
- * {@link Presence.peerMetadataJSONString}.
78
- *
79
- * Uses UTF-8 encoding.
80
- */
81
- get peerMetadataJSONString(): string {
82
- return FFI.connectionRequestPeerMetadataJSON(this.deref())
83
- }
84
-
85
- /**
86
- * Metadata for the remote peer that is provided by the identity service.
87
- *
88
- * Use an authentication webhook to set this value. See Ditto's online
89
- * documentation for more information on how to configure an authentication
90
- * webhook.
91
- *
92
- * Convenience property that wraps {@link identityServiceMetadataJSONString}.
93
- */
94
- get identityServiceMetadata(): Record<string, any> {
95
- return JSON.parse(this.identityServiceMetadataJSONString)
96
- }
97
-
98
- /**
99
- * JSON-encoded metadata for the remote peer that is provided by the
100
- * identity service.
101
- *
102
- * Use an authentication webhook to set this value. See Ditto's online
103
- * documentation for more information on how to configure an authentication
104
- * webhook.
105
- *
106
- * Uses UTF-8 encoding.
107
- */
108
- get identityServiceMetadataJSONString(): string {
109
- return FFI.connectionRequestIdentityServiceMetadataJSON(this.deref())
110
- }
111
-
112
- /**
113
- * The network transport of this connection request.
114
- *
115
- * Expect to receive separate connection requests for each network
116
- * transport that connects the local and remote peer.
117
- */
118
- get connectionType(): ConnectionType {
119
- return FFI.connectionRequestConnectionType(this.deref())
120
- }
121
-
122
- /** @internal */
123
- toString(): string {
124
- return `ConnectionRequest(${this.peerKeyString} via ${this.connectionType})`
125
- }
126
-
127
- /**
128
- * Defines a custom inspect function for Node.js that will be used when the
129
- * object is inspected with console.log() or util.inspect().
130
- *
131
- * @internal
132
- */
133
- [Symbol.for('nodejs.util.inspect.custom')](_depth: number, _inspectOptions: any, inspect: any): string {
134
- return this.toString()
135
- }
136
-
137
- // --------------------------------------------------------------------------
138
-
139
- private deref(): FFI.Pointer<FFI.FFIConnectionRequest> {
140
- return Bridge.connectionRequest.handleFor(this).deref()
141
- }
142
- }
@@ -1,82 +0,0 @@
1
- //
2
- // Copyright © 2021 DittoLive Incorporated. All rights reserved.
3
- //
4
-
5
- // NOTE: we use a token to detect private invocation of the constructor. This is
6
- // not secure and just to prevent accidental private invocation on the client
7
- // side.
8
- const privateToken = Symbol('privateConstructorToken')
9
-
10
- /**
11
- * Represents a CRDT counter that can be upserted as part of a document or
12
- * assigned to a property during an update of a document.
13
- */
14
- export class Counter {
15
- /** The value of the counter. */
16
- get value(): number {
17
- return this._value
18
- }
19
-
20
- /**
21
- * Creates a new counter that can be used as part of a document's content.
22
- */
23
- constructor() {
24
- this._value = 0.0
25
- }
26
-
27
- /** @internal */
28
- static '@ditto.create'(mutDoc, path, value) {
29
- // @ts-expect-error - using hidden argument
30
- const counter = mutDoc ? new MutableCounter(privateToken) : new Counter()
31
- counter.mutDoc = mutDoc
32
- counter.path = path
33
- counter._value = value
34
- return counter
35
- }
36
-
37
- protected mutDoc: any
38
- protected path: any
39
- protected _value: number
40
- }
41
-
42
- // -----------------------------------------------------------------------------
43
-
44
- /**
45
- * Represents a mutable CRDT counter that can be incremented by a specific
46
- * amount while updating a document.
47
- *
48
- * This class can't be instantiated directly, it's returned automatically for
49
- * any counter property within an update block via {@link MutableDocumentPath.counter}.
50
- */
51
- export class MutableCounter extends Counter {
52
- /**
53
- * Increments the counter by `amount`, which can be any valid number.
54
- *
55
- * Only valid within the `update` closure of
56
- * {@link PendingCursorOperation.update | PendingCursorOperation.update()} and
57
- * {@link PendingIDSpecificOperation.update | PendingIDSpecificOperation.update()},
58
- * otherwise an exception is thrown.
59
- */
60
- increment(amount: number) {
61
- const mutDoc = this.mutDoc
62
- const path = this.path
63
-
64
- if (!mutDoc) {
65
- throw new Error(`Can't increment counter, only possible within the closure of a collection's update() method.`)
66
- }
67
-
68
- mutDoc.at(path)['@ditto.increment'](amount)
69
- // We also increment the local value to make sure that the change is
70
- // reflected locally as well as in the underlying document
71
- this._value += amount
72
- }
73
-
74
- /** @internal */
75
- protected constructor() {
76
- if (arguments[0] === privateToken) {
77
- super()
78
- } else {
79
- throw new Error(`MutableCounter constructor is for internal use only.`)
80
- }
81
- }
82
- }