@dittolive/ditto 4.5.1-experimental.aarch64-linux.1.aarch64 → 4.5.2-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.
- package/DittoReactNative.podspec +25 -0
- package/README.md +2 -2
- package/node/ditto.cjs.js +1 -1
- package/node/ditto.cjs.js.map +1 -0
- package/node/ditto.cjs.pretty.js +9655 -0
- package/node/ditto.cjs.pretty.js.map +1 -0
- package/node/ditto.darwin-arm64.node +0 -0
- package/node/ditto.darwin-x64.node +0 -0
- package/node/{ditto.linux-arm64.node → ditto.linux-x64.node} +0 -0
- package/node/transports.darwin-arm64.node +0 -0
- package/node/transports.darwin-x64.node +0 -0
- package/package.json +2 -1
- package/react-native/android/CMakeLists.txt +37 -0
- package/react-native/android/build.gradle +186 -0
- package/react-native/android/cpp-adapter.cpp +254 -0
- package/react-native/android/gradle.properties +5 -0
- package/react-native/android/src/main/AndroidManifest.xml +4 -0
- package/react-native/android/src/main/java/com/dittolive/rnsdk/DittoRNSDKModule.java +85 -0
- package/react-native/android/src/main/java/com/dittolive/rnsdk/DittoRNSDKPackage.java +28 -0
- package/react-native/cpp/include/Arc.hpp +141 -0
- package/react-native/cpp/include/Attachment.h +16 -0
- package/react-native/cpp/include/Authentication.h +23 -0
- package/react-native/cpp/include/Collection.h +13 -0
- package/react-native/cpp/include/DQL.h +21 -0
- package/react-native/cpp/include/Document.h +17 -0
- package/react-native/cpp/include/Identity.h +17 -0
- package/react-native/cpp/include/Lifecycle.h +17 -0
- package/react-native/cpp/include/LiveQuery.h +17 -0
- package/react-native/cpp/include/Logger.h +22 -0
- package/react-native/cpp/include/Misc.h +27 -0
- package/react-native/cpp/include/Presence.h +14 -0
- package/react-native/cpp/include/SmallPeerInfo.h +19 -0
- package/react-native/cpp/include/Transports.h +25 -0
- package/react-native/cpp/include/TypedArray.hpp +167 -0
- package/react-native/cpp/include/Utils.h +61 -0
- package/react-native/cpp/include/main.h +10 -0
- package/react-native/cpp/src/Attachment.cpp +86 -0
- package/react-native/cpp/src/Authentication.cpp +227 -0
- package/react-native/cpp/src/Collection.cpp +54 -0
- package/react-native/cpp/src/DQL.cpp +256 -0
- package/react-native/cpp/src/Document.cpp +146 -0
- package/react-native/cpp/src/Identity.cpp +123 -0
- package/react-native/cpp/src/Lifecycle.cpp +110 -0
- package/react-native/cpp/src/LiveQuery.cpp +63 -0
- package/react-native/cpp/src/Logger.cpp +200 -0
- package/react-native/cpp/src/Misc.cpp +283 -0
- package/react-native/cpp/src/Presence.cpp +79 -0
- package/react-native/cpp/src/SmallPeerInfo.cpp +142 -0
- package/react-native/cpp/src/Transports.cpp +270 -0
- package/react-native/cpp/src/TypedArray.cpp +303 -0
- package/react-native/cpp/src/Utils.cpp +138 -0
- package/react-native/cpp/src/main.cpp +152 -0
- package/react-native/dittoffi/dittoffi.h +4700 -0
- package/react-native/dittoffi/ifaddrs.cpp +385 -0
- package/react-native/dittoffi/ifaddrs.h +206 -0
- package/react-native/ios/DittoRNSDK.h +7 -0
- package/react-native/ios/DittoRNSDK.mm +107 -0
- package/react-native/ios/YeetJSIUtils.h +60 -0
- package/react-native/ios/YeetJSIUtils.mm +196 -0
- package/react-native/lib/commonjs/ditto.rn.js +92 -0
- package/react-native/lib/commonjs/ditto.rn.js.map +1 -0
- package/react-native/lib/commonjs/index.js +61 -0
- package/react-native/lib/commonjs/index.js.map +1 -0
- package/react-native/lib/module/ditto.rn.js +88 -0
- package/react-native/lib/module/ditto.rn.js.map +1 -0
- package/react-native/lib/module/index.js +27 -0
- package/react-native/lib/module/index.js.map +1 -0
- package/react-native/lib/typescript/ditto.rn.d.ts +15 -0
- package/react-native/lib/typescript/ditto.rn.d.ts.map +1 -0
- package/react-native/lib/typescript/index.d.ts +1 -0
- package/react-native/lib/typescript/index.d.ts.map +1 -0
- package/react-native/src/ditto.rn.ts +91 -0
- package/react-native/src/environment/environment.fallback.ts +4 -0
- package/react-native/src/index.ts +26 -0
- package/react-native/src/sources/@cbor-redux.ts +2 -0
- package/react-native/src/sources/@ditto.core.ts +1 -0
- package/react-native/src/sources/@environment.ts +1 -0
- package/react-native/src/sources/attachment-fetch-event.ts +54 -0
- package/react-native/src/sources/attachment-fetcher-manager.ts +144 -0
- package/react-native/src/sources/attachment-fetcher.ts +134 -0
- package/react-native/src/sources/attachment-token.ts +48 -0
- package/react-native/src/sources/attachment.ts +74 -0
- package/react-native/src/sources/augment.ts +101 -0
- package/react-native/src/sources/authenticator.ts +314 -0
- package/react-native/src/sources/base-pending-cursor-operation.ts +239 -0
- package/react-native/src/sources/base-pending-id-specific-operation.ts +109 -0
- package/react-native/src/sources/bridge.ts +553 -0
- package/react-native/src/sources/build-time-constants.ts +8 -0
- package/react-native/src/sources/cbor.ts +35 -0
- package/react-native/src/sources/collection-interface.ts +67 -0
- package/react-native/src/sources/collection.ts +212 -0
- package/react-native/src/sources/collections-event.ts +99 -0
- package/react-native/src/sources/counter.ts +82 -0
- package/react-native/src/sources/ditto.ts +979 -0
- package/react-native/src/sources/document-id.ts +159 -0
- package/react-native/src/sources/document-path.ts +306 -0
- package/react-native/src/sources/document.ts +193 -0
- package/react-native/src/sources/epilogue.ts +30 -0
- package/react-native/src/sources/error-codes.ts +52 -0
- package/react-native/src/sources/error.ts +208 -0
- package/react-native/src/sources/essentials.ts +53 -0
- package/react-native/src/sources/ffi-error.ts +122 -0
- package/react-native/src/sources/ffi.ts +2012 -0
- package/react-native/src/sources/identity.ts +163 -0
- package/react-native/src/sources/init.ts +71 -0
- package/react-native/src/sources/internal.ts +109 -0
- package/react-native/src/sources/keep-alive.ts +73 -0
- package/react-native/src/sources/key-path.ts +198 -0
- package/react-native/src/sources/live-query-event.ts +208 -0
- package/react-native/src/sources/live-query-manager.ts +102 -0
- package/react-native/src/sources/live-query.ts +166 -0
- package/react-native/src/sources/logger.ts +196 -0
- package/react-native/src/sources/main.ts +60 -0
- package/react-native/src/sources/observer-manager.ts +178 -0
- package/react-native/src/sources/observer.ts +79 -0
- package/react-native/src/sources/pending-collections-operation.ts +232 -0
- package/react-native/src/sources/pending-cursor-operation.ts +218 -0
- package/react-native/src/sources/pending-id-specific-operation.ts +218 -0
- package/react-native/src/sources/presence-manager.ts +161 -0
- package/react-native/src/sources/presence.ts +233 -0
- package/react-native/src/sources/query-result-item.ts +116 -0
- package/react-native/src/sources/query-result.ts +55 -0
- package/react-native/src/sources/register.ts +95 -0
- package/react-native/src/sources/small-peer-info.ts +177 -0
- package/react-native/src/sources/static-tcp-client.ts +6 -0
- package/react-native/src/sources/store-observer.ts +177 -0
- package/react-native/src/sources/store.ts +385 -0
- package/react-native/src/sources/subscription-manager.ts +99 -0
- package/react-native/src/sources/subscription.ts +89 -0
- package/react-native/src/sources/sync-subscription.ts +90 -0
- package/react-native/src/sources/sync.ts +559 -0
- package/react-native/src/sources/test-helpers.ts +24 -0
- package/react-native/src/sources/transport-conditions-manager.ts +104 -0
- package/react-native/src/sources/transport-config.ts +430 -0
- package/react-native/src/sources/update-result.ts +66 -0
- package/react-native/src/sources/update-results-map.ts +57 -0
- package/react-native/src/sources/websocket-client.ts +7 -0
- package/react-native/src/sources/write-transaction-collection.ts +122 -0
- package/react-native/src/sources/write-transaction-pending-cursor-operation.ts +101 -0
- package/react-native/src/sources/write-transaction-pending-id-specific-operation.ts +74 -0
- package/react-native/src/sources/write-transaction.ts +121 -0
- package/react-native.config.js +9 -0
- package/types/ditto.d.ts.map +1 -0
- package/web/ditto.es6.js +1 -1
- package/web/ditto.es6.js.map +1 -0
- package/web/ditto.es6.pretty.js +12600 -0
- package/web/ditto.es6.pretty.js.map +1 -0
- package/web/ditto.umd.js +1 -1
- package/web/ditto.umd.js.map +1 -0
- package/web/ditto.umd.pretty.js +12669 -0
- package/web/ditto.umd.pretty.js.map +1 -0
- package/web/ditto.wasm +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Permission } from 'react-native';
|
|
2
|
+
import 'fastestsmallesttextencoderdecoder';
|
|
3
|
+
export declare function ditto_sdk_transports_init(transportsErrorPointer: any): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Get the list of missing Android permissions for syncing. The returned value
|
|
6
|
+
* can be passed directly to `PermissionsAndroid.requestMultiple()`.
|
|
7
|
+
*
|
|
8
|
+
* Returns an empty array on other platforms.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getMissingAndroidPermissions(): Permission[];
|
|
11
|
+
export declare function boxCStringIntoString(something: any): any;
|
|
12
|
+
export declare function boxCBytesIntoBuffer(cborBytes: any): Uint8Array | null;
|
|
13
|
+
export declare function getDeadlockTimeout(): number;
|
|
14
|
+
export declare function setDeadlockTimeout(something: any): any;
|
|
15
|
+
//# sourceMappingURL=ditto.rn.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ditto.rn.d.ts","sourceRoot":"","sources":["../../src/ditto.rn.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAOxE,OAAO,mCAAmC,CAAC;AAuE3C,wBAAgB,yBAAyB,CACvC,sBAAsB,EAAE,GAAG,GAC1B,OAAO,CAUT;AAED;;;;;GAKG;AACH,wBAAgB,4BAA4B,IAAI,UAAU,EAAE,CAK3D;AAMD,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,GAAG,OAElD;AAED,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,GAAG,qBAGjD;AAED,wBAAgB,kBAAkB,IAAI,MAAM,CAE3C;AAED,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,GAAG,OAEhD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { NativeModules, Platform, type Permission } from 'react-native';
|
|
2
|
+
import { FinalizationGroup } from '@ungap/weakrefs';
|
|
3
|
+
import 'fastestsmallesttextencoderdecoder';
|
|
4
|
+
|
|
5
|
+
interface JSSDKAPI {
|
|
6
|
+
ditto_sdk_transports_init(transportsErrorPointer: any): boolean;
|
|
7
|
+
}
|
|
8
|
+
interface PlatformSpecificAPI {
|
|
9
|
+
getRandomValues<T extends ArrayBufferView>(array: T): T;
|
|
10
|
+
ditto_tick(): void;
|
|
11
|
+
createDirectory(path: string): string;
|
|
12
|
+
checkDirectoryExists(path: string): string;
|
|
13
|
+
getDeviceName(): string;
|
|
14
|
+
|
|
15
|
+
ditto_sdk_transports_set_android_context(): boolean;
|
|
16
|
+
ditto_sdk_transports_android_missing_permissions(): string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type DittoCoreType = JSSDKAPI & PlatformSpecificAPI;
|
|
20
|
+
|
|
21
|
+
const dittoCore = global as unknown as DittoCoreType;
|
|
22
|
+
const isLoaded = typeof dittoCore.getDeviceName === 'function';
|
|
23
|
+
if (!isLoaded) {
|
|
24
|
+
const result = NativeModules.DittoRNSDK?.install();
|
|
25
|
+
|
|
26
|
+
if (!result) {
|
|
27
|
+
throw new Error('JSI bindings were not installed for: DittoRNSDK Module');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setInterval(() => {
|
|
31
|
+
dittoCore.ditto_tick();
|
|
32
|
+
}, 50);
|
|
33
|
+
|
|
34
|
+
if (
|
|
35
|
+
typeof global.FinalizationRegistry === 'undefined' &&
|
|
36
|
+
typeof FinalizationGroup !== 'undefined'
|
|
37
|
+
) {
|
|
38
|
+
global.FinalizationRegistry = FinalizationGroup;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
Object.keys(dittoCore)
|
|
43
|
+
.filter((key) => key !== 'ditto_sdk_transports_init')
|
|
44
|
+
.forEach((key) => {
|
|
45
|
+
exports[key] = dittoCore[key as keyof DittoCoreType];
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export function ditto_sdk_transports_init(
|
|
49
|
+
transportsErrorPointer: any
|
|
50
|
+
): boolean {
|
|
51
|
+
if (Platform.OS === 'android') {
|
|
52
|
+
const res = dittoCore.ditto_sdk_transports_set_android_context();
|
|
53
|
+
if (!res) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
"Couldn't manage to set Android context with `ditto_sdk_transports_set_android_context()`"
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return dittoCore.ditto_sdk_transports_init(transportsErrorPointer);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get the list of missing Android permissions for syncing. The returned value
|
|
65
|
+
* can be passed directly to `PermissionsAndroid.requestMultiple()`.
|
|
66
|
+
*
|
|
67
|
+
* Returns an empty array on other platforms.
|
|
68
|
+
*/
|
|
69
|
+
export function getMissingAndroidPermissions(): Permission[] {
|
|
70
|
+
if (Platform.OS !== 'android') {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
return dittoCore.ditto_sdk_transports_android_missing_permissions() as Permission[];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function boxCStringIntoString(something: any) {
|
|
77
|
+
return something;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function boxCBytesIntoBuffer(cborBytes: any) {
|
|
81
|
+
const bytes = dittoCore.sliceBoxedToUInt8Array(cborBytes);
|
|
82
|
+
return bytes ? new Uint8Array(bytes) : null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function getDeadlockTimeout(): number {
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function setDeadlockTimeout(something: any) {
|
|
90
|
+
return something;
|
|
91
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
export { getMissingAndroidPermissions } from './ditto.rn';
|
|
3
|
+
|
|
4
|
+
export * from './sources/essentials';
|
|
5
|
+
|
|
6
|
+
export * from './sources/logger';
|
|
7
|
+
export * from './sources/document-id';
|
|
8
|
+
export * from './sources/observer';
|
|
9
|
+
|
|
10
|
+
export * from './sources/authenticator';
|
|
11
|
+
export * from './sources/identity';
|
|
12
|
+
export * from './sources/ditto';
|
|
13
|
+
export * from './sources/store-observer';
|
|
14
|
+
export * from './sources/sync';
|
|
15
|
+
export * from './sources/sync-subscription';
|
|
16
|
+
export * from './sources/error';
|
|
17
|
+
export * from './sources/error-codes';
|
|
18
|
+
export * from './sources/query-result-item';
|
|
19
|
+
export * from './sources/query-result';
|
|
20
|
+
|
|
21
|
+
export * from './sources/small-peer-info';
|
|
22
|
+
export * from './sources/store';
|
|
23
|
+
export * from './sources/presence';
|
|
24
|
+
export * from './sources/transport-config';
|
|
25
|
+
|
|
26
|
+
export * from './sources/epilogue';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../ditto.rn';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../environment/environment.fallback';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright © 2021 DittoLive Incorporated. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { Attachment } from './attachment'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The types of attachment fetch events that can be delivered to an attachment
|
|
9
|
+
* fetcher's `callback`.
|
|
10
|
+
*/
|
|
11
|
+
export type AttachmentFetchEventType = 'Completed' | 'Progress' | 'Deleted'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* An attachment fetch event used when the attachment's download has completed.
|
|
15
|
+
*/
|
|
16
|
+
export type AttachmentFetchEventCompleted = {
|
|
17
|
+
type: 'Completed'
|
|
18
|
+
attachment: Attachment
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* An attachment fetch event used when the attachment's download progressed but
|
|
23
|
+
* is not yet complete.
|
|
24
|
+
*/
|
|
25
|
+
export type AttachmentFetchEventProgress = {
|
|
26
|
+
type: 'Progress'
|
|
27
|
+
totalBytes: number | BigInt
|
|
28
|
+
downloadedBytes: number | BigInt
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* An attachment fetch event used when the attachment is deleted.
|
|
33
|
+
*/
|
|
34
|
+
export type AttachmentFetchEventDeleted = {
|
|
35
|
+
type: 'Deleted'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A representation of the events that can occur in relation to an attachment
|
|
40
|
+
* fetch.
|
|
41
|
+
*
|
|
42
|
+
* There are three different attachment fetch events: `Completed`, `Progress`,
|
|
43
|
+
* or `Deleted`.
|
|
44
|
+
*
|
|
45
|
+
* There will be at most one `Completed` or `Deleted` event per attachment
|
|
46
|
+
* fetch. There can be many `Progress` events delivered for each attachment
|
|
47
|
+
* fetch.
|
|
48
|
+
*
|
|
49
|
+
* Updates relating to an attachment fetch are delivered by registering an
|
|
50
|
+
* {@link AttachmentFetcher} through a call to
|
|
51
|
+
* {@link Collection.fetchAttachment | fetchAttachment()} on a
|
|
52
|
+
* {@link Collection} instance.
|
|
53
|
+
*/
|
|
54
|
+
export type AttachmentFetchEvent = AttachmentFetchEventCompleted | AttachmentFetchEventProgress | AttachmentFetchEventDeleted
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright © 2023 DittoLive Incorporated. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as FFI from './ffi'
|
|
6
|
+
|
|
7
|
+
import { AttachmentFetcher } from './attachment-fetcher'
|
|
8
|
+
import { AttachmentToken } from './attachment-token'
|
|
9
|
+
import { Bridge } from './bridge'
|
|
10
|
+
import { Ditto } from './ditto'
|
|
11
|
+
|
|
12
|
+
import type { AttachmentFetchEvent } from './attachment-fetch-event'
|
|
13
|
+
|
|
14
|
+
/** @internal */
|
|
15
|
+
type AttachmentFetcherContextInfo = {
|
|
16
|
+
attachmentFetcher: WeakRef<AttachmentFetcher>
|
|
17
|
+
// Attachment token is unique per attachment.
|
|
18
|
+
attachmentTokenID: Uint8Array
|
|
19
|
+
// This id is unique per attachment fetcher.
|
|
20
|
+
id: string
|
|
21
|
+
// Promise for a token that can be used to cancel the fetch. If `null`, the
|
|
22
|
+
// fetch has already completed, errored, or been canceled.
|
|
23
|
+
cancelTokenPromise: Promise<unknown | null> | null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Manages attachment fetchers to make sure we free all resources when the
|
|
28
|
+
* fetcher is garbage collected and to allow us to wait for freeing of
|
|
29
|
+
* ressources to be finished before the ditto instance is closed.
|
|
30
|
+
*
|
|
31
|
+
* @internal */
|
|
32
|
+
export class AttachmentFetcherManager {
|
|
33
|
+
readonly ditto: Ditto
|
|
34
|
+
|
|
35
|
+
/** @internal */
|
|
36
|
+
constructor(ditto: Ditto) {
|
|
37
|
+
this.ditto = ditto
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Start an attachment fetcher.
|
|
42
|
+
*
|
|
43
|
+
* @internal */
|
|
44
|
+
startAttachmentFetcher(token: AttachmentToken, eventHandler?: (attachmentFetchEvent: AttachmentFetchEvent) => void): AttachmentFetcher {
|
|
45
|
+
return this.ditto.deferClose(() => {
|
|
46
|
+
const attachmentFetcher = new AttachmentFetcher(this.ditto, token, this, eventHandler)
|
|
47
|
+
|
|
48
|
+
// Register in finalization registry.
|
|
49
|
+
const contextInfo: AttachmentFetcherContextInfo = {
|
|
50
|
+
id: attachmentFetcher.id,
|
|
51
|
+
attachmentTokenID: token.id,
|
|
52
|
+
cancelTokenPromise: attachmentFetcher.cancelTokenPromise,
|
|
53
|
+
attachmentFetcher: new WeakRef(attachmentFetcher),
|
|
54
|
+
}
|
|
55
|
+
this.finalizationRegistry.register(attachmentFetcher, contextInfo, attachmentFetcher)
|
|
56
|
+
|
|
57
|
+
// Keep a reference to the context info so that we can stop the fetcher.
|
|
58
|
+
this.contextInfoByID[attachmentFetcher.id] = contextInfo
|
|
59
|
+
|
|
60
|
+
// Prevent cancellation of the fetch once it was fulfilled or rejected.
|
|
61
|
+
const resetCancelToken = () => {
|
|
62
|
+
if (this.contextInfoByID[attachmentFetcher.id] != null) {
|
|
63
|
+
this.contextInfoByID[attachmentFetcher.id].cancelTokenPromise = null
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
attachmentFetcher.attachment.then(
|
|
67
|
+
(value) => {
|
|
68
|
+
resetCancelToken()
|
|
69
|
+
return value
|
|
70
|
+
},
|
|
71
|
+
(reason) => {
|
|
72
|
+
resetCancelToken()
|
|
73
|
+
return reason
|
|
74
|
+
},
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
// Keep the attachment fetcher alive until it is stopped.
|
|
78
|
+
this.ditto.keepAlive.retain(`AttachmentFetcher.${attachmentFetcher.id})`)
|
|
79
|
+
|
|
80
|
+
return attachmentFetcher
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Stop an attachment fetcher and wait for it to be stopped.
|
|
86
|
+
*
|
|
87
|
+
* @internal */
|
|
88
|
+
async stopAttachmentFetcher(attachmentFetcher: AttachmentFetcher): Promise<void> {
|
|
89
|
+
this.finalizationRegistry.unregister(attachmentFetcher)
|
|
90
|
+
const contextInfo = this.contextInfoByID[attachmentFetcher.id]
|
|
91
|
+
if (contextInfo == null) {
|
|
92
|
+
throw new Error(`Internal inconsistency: cannot stop attachment fetcher ${attachmentFetcher.id}, which is not registered.`)
|
|
93
|
+
}
|
|
94
|
+
await this.stopWithContextInfo(contextInfo)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Closing the manager will cancel all attachment fetchers.
|
|
99
|
+
*
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
close() {
|
|
103
|
+
this.ditto.deferCloseAsync(async () => {
|
|
104
|
+
const contextInfos = Object.values(this.contextInfoByID)
|
|
105
|
+
const stopped = contextInfos.map(async (contextInfo) => {
|
|
106
|
+
const attachmentFetcher = contextInfo.attachmentFetcher.deref()
|
|
107
|
+
if (attachmentFetcher != null) {
|
|
108
|
+
await this.stopAttachmentFetcher(attachmentFetcher)
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
await Promise.all(stopped)
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// --- Private ---
|
|
116
|
+
|
|
117
|
+
private contextInfoByID: { [attachmentFetcherID: string]: AttachmentFetcherContextInfo } = {}
|
|
118
|
+
|
|
119
|
+
private finalizationRegistry: FinalizationRegistry<AttachmentFetcherContextInfo> = new FinalizationRegistry(this.stopWithContextInfo)
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Stop the attachment fetcher without unregistering it from the finalization
|
|
123
|
+
* registry.
|
|
124
|
+
*/
|
|
125
|
+
private stopWithContextInfo(contextInfo: AttachmentFetcherContextInfo): Promise<void> {
|
|
126
|
+
const dittoHandle = Bridge.ditto.handleFor(this.ditto)
|
|
127
|
+
return this.ditto.deferCloseAsync(async () => {
|
|
128
|
+
// Remove the manager's own record of the context info.
|
|
129
|
+
if (this.contextInfoByID[contextInfo.id] == null) {
|
|
130
|
+
throw new Error(`Internal inconsistency: attachment fetcher ${contextInfo.id} not found in active attachment fetchers.`)
|
|
131
|
+
}
|
|
132
|
+
delete this.contextInfoByID[contextInfo.id]
|
|
133
|
+
|
|
134
|
+
// Release the keep-alive.
|
|
135
|
+
this.ditto.keepAlive.release(`AttachmentFetcher.${contextInfo.id})`)
|
|
136
|
+
|
|
137
|
+
// Cancel the fetcher if it is still running.
|
|
138
|
+
const cancelToken = await contextInfo.cancelTokenPromise
|
|
139
|
+
if (cancelToken) {
|
|
140
|
+
FFI.dittoCancelResolveAttachment(dittoHandle.deref(), contextInfo.attachmentTokenID, cancelToken)
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright © 2021 DittoLive Incorporated. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as FFI from './ffi'
|
|
6
|
+
import { Bridge } from './bridge'
|
|
7
|
+
import { Attachment } from './attachment'
|
|
8
|
+
import { AttachmentToken } from './attachment-token'
|
|
9
|
+
import { generateEphemeralToken, step } from './internal'
|
|
10
|
+
|
|
11
|
+
import type { AttachmentFetchEvent } from './attachment-fetch-event'
|
|
12
|
+
import type { Ditto } from './ditto'
|
|
13
|
+
import type { AttachmentFetcherManager } from './attachment-fetcher-manager'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* These objects are returned by calls to
|
|
17
|
+
* {@link Collection.fetchAttachment | fetchAttachment()} on {@link Collection}
|
|
18
|
+
* and allow you to stop an in-flight attachment fetch.
|
|
19
|
+
*/
|
|
20
|
+
export class AttachmentFetcher implements PromiseLike<Attachment | null> {
|
|
21
|
+
/**
|
|
22
|
+
* Returns a promise for the attachment that you can `await`. The promise
|
|
23
|
+
* resolves to either an attachment or `null` if the attachment has been
|
|
24
|
+
* deleted meanwhile. The promise is rejected if an error occurs during
|
|
25
|
+
* the fetch. Note that the `AttachmentFetcher` itself implementes
|
|
26
|
+
* `PromiseLike`, so you can `await` it directly.
|
|
27
|
+
*/
|
|
28
|
+
readonly attachment: Promise<Attachment | null>
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Stops fetching the associated attachment and cleans up any associated
|
|
32
|
+
* resources.
|
|
33
|
+
*
|
|
34
|
+
* Note that you are not required to call `stop()` once your attachment fetch
|
|
35
|
+
* operation has finished. The method primarily exists to allow you to cancel
|
|
36
|
+
* an attachment fetch request while it is ongoing if you no longer wish for
|
|
37
|
+
* the attachment to be made available locally to the device.
|
|
38
|
+
*/
|
|
39
|
+
stop() {
|
|
40
|
+
// No need for synchronicity here: we let the "stop promise" float / run in
|
|
41
|
+
// a detached fashion since there is no point in awaiting the "stop
|
|
42
|
+
// signaling" itself.
|
|
43
|
+
step(async () => {
|
|
44
|
+
await this.manager.stopAttachmentFetcher(this)
|
|
45
|
+
if (this.rejectPendingFetch != null) {
|
|
46
|
+
this.rejectPendingFetch()
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// --------------------------------------- Internal ------------------------
|
|
52
|
+
|
|
53
|
+
/** @internal */
|
|
54
|
+
readonly cancelTokenPromise: Promise<unknown | null> | null = null
|
|
55
|
+
|
|
56
|
+
/** @internal */
|
|
57
|
+
readonly ditto: Ditto
|
|
58
|
+
|
|
59
|
+
/** @internal */
|
|
60
|
+
readonly id: string
|
|
61
|
+
|
|
62
|
+
/** @internal */
|
|
63
|
+
readonly manager: AttachmentFetcherManager
|
|
64
|
+
|
|
65
|
+
/** @internal */
|
|
66
|
+
readonly token: AttachmentToken
|
|
67
|
+
|
|
68
|
+
/** @internal */
|
|
69
|
+
then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2> {
|
|
70
|
+
return this.attachment.then(onfulfilled, onrejected)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** @internal */
|
|
74
|
+
constructor(ditto: Ditto, token: AttachmentToken, manager: AttachmentFetcherManager, eventHandler?: (attachmentFetchEvent: AttachmentFetchEvent) => void) {
|
|
75
|
+
this.ditto = ditto
|
|
76
|
+
this.token = token
|
|
77
|
+
this.manager = manager
|
|
78
|
+
this.id = generateEphemeralToken()
|
|
79
|
+
|
|
80
|
+
const eventHandlerOrNoOp = eventHandler || function () {}
|
|
81
|
+
const dittoHandle = Bridge.ditto.handleFor(ditto)
|
|
82
|
+
|
|
83
|
+
this.attachment = new Promise((resolve, reject) => {
|
|
84
|
+
const onComplete = (attachmentHandlePointer: FFI.Pointer<FFI.AttachmentHandle>) => {
|
|
85
|
+
const attachment = new Attachment(this.ditto, this.token)
|
|
86
|
+
Bridge.attachment.bridge(attachmentHandlePointer, () => attachment)
|
|
87
|
+
|
|
88
|
+
eventHandlerOrNoOp({ type: 'Completed', attachment })
|
|
89
|
+
|
|
90
|
+
this.rejectPendingFetch = null
|
|
91
|
+
resolve(attachment)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const onProgress = (downloaded: number | BigInt, toDownload: number | BigInt) => {
|
|
95
|
+
eventHandlerOrNoOp({ type: 'Progress', totalBytes: toDownload, downloadedBytes: downloaded })
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const onDelete = () => {
|
|
99
|
+
eventHandlerOrNoOp({ type: 'Deleted' })
|
|
100
|
+
|
|
101
|
+
this.rejectPendingFetch = null
|
|
102
|
+
resolve(null)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const onError = () => {
|
|
106
|
+
;(err: any) => {
|
|
107
|
+
this.rejectPendingFetch = null
|
|
108
|
+
reject(err)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// The core doesn't call any of the handlers defined above when a fetch is
|
|
113
|
+
// cancelled through `this.stop()` so we use this function to reject the
|
|
114
|
+
// promise from the outside.
|
|
115
|
+
this.rejectPendingFetch = () => {
|
|
116
|
+
reject(new Error('Attachment fetch was canceled'))
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Not awaited yet; only needs to be fully resolved (and `await`ed over)
|
|
120
|
+
// when using the actual `cancelTokenPromise`, which only happens in
|
|
121
|
+
// `AttachmentFetcherManager.stopWithContextInfo()`.
|
|
122
|
+
// @ts-expect-error setting readonly property
|
|
123
|
+
this.cancelTokenPromise = FFI.dittoResolveAttachment(dittoHandle.deref(), token.id, { onComplete, onProgress, onDelete }, onError)
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* This function is defined while a fetch is in progress and is used to reject
|
|
129
|
+
* the promise `this.attachment` when the fetch is canceled.
|
|
130
|
+
*
|
|
131
|
+
* @internal
|
|
132
|
+
*/
|
|
133
|
+
private rejectPendingFetch: (() => void) | null = null
|
|
134
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright © 2021 DittoLive Incorporated. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as FFI from './ffi'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Serves as a token for a specific attachment that you can pass to a call to
|
|
9
|
+
* {@link Collection.fetchAttachment | fetchAttachment()} on a
|
|
10
|
+
* {@link Collection}.
|
|
11
|
+
*/
|
|
12
|
+
export class AttachmentToken {
|
|
13
|
+
/** @internal */
|
|
14
|
+
readonly id: Uint8Array
|
|
15
|
+
|
|
16
|
+
/** @internal */
|
|
17
|
+
readonly len: number
|
|
18
|
+
|
|
19
|
+
/** @internal */
|
|
20
|
+
readonly metadata: { [key: string]: string }
|
|
21
|
+
|
|
22
|
+
/** @internal */
|
|
23
|
+
constructor(jsObj: object) {
|
|
24
|
+
const type = jsObj[FFI.DittoCRDTTypeKey]
|
|
25
|
+
if (type !== FFI.DittoCRDTType.attachment) {
|
|
26
|
+
throw new Error('Invalid attachment token')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const id = jsObj['_id']
|
|
30
|
+
if (!(id instanceof Uint8Array)) {
|
|
31
|
+
throw new Error('Invalid attachment token id')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const len = jsObj['_len']
|
|
35
|
+
if (typeof len !== 'number' || len < 0) {
|
|
36
|
+
throw new Error('Invalid attachment token length')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const meta = jsObj['_meta']
|
|
40
|
+
if (typeof meta !== 'object') {
|
|
41
|
+
throw new Error('Invalid attachment token meta')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this.id = id
|
|
45
|
+
this.len = len
|
|
46
|
+
this.metadata = meta
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright © 2021 DittoLive Incorporated. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as FFI from './ffi'
|
|
6
|
+
import { Bridge } from './bridge'
|
|
7
|
+
import * as Environment from './@environment'
|
|
8
|
+
|
|
9
|
+
import type { Ditto } from './ditto'
|
|
10
|
+
import type { AttachmentToken } from './attachment-token'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Represents an attachment and can be used to insert the associated attachment
|
|
14
|
+
* into a document at a specific key-path. You can't instantiate an attachment
|
|
15
|
+
* directly, please use the {@link Collection.newAttachment | newAttachment()}
|
|
16
|
+
* method of {@link Collection} instead.
|
|
17
|
+
*/
|
|
18
|
+
export class Attachment {
|
|
19
|
+
/** @internal */
|
|
20
|
+
readonly ditto: Ditto
|
|
21
|
+
|
|
22
|
+
/** @internal */
|
|
23
|
+
readonly token: AttachmentToken
|
|
24
|
+
|
|
25
|
+
/** The attachment's metadata. */
|
|
26
|
+
get metadata(): { [key: string]: string } {
|
|
27
|
+
return this.token.metadata
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the attachment's data.
|
|
32
|
+
*/
|
|
33
|
+
getData(): Promise<Uint8Array> {
|
|
34
|
+
const ditto = this.ditto
|
|
35
|
+
const dittoHandle = Bridge.ditto.handleFor(ditto)
|
|
36
|
+
return this.ditto.deferCloseAsync(async () => {
|
|
37
|
+
// if (Environment.isWebBuild) {
|
|
38
|
+
// const attachmentHandle = Bridge.attachment.handleFor(this)
|
|
39
|
+
// return await FFI.dittoGetCompleteAttachmentData(dittoHandle.deref(), attachmentHandle.deref())
|
|
40
|
+
// }
|
|
41
|
+
|
|
42
|
+
// if (Environment.isNodeBuild) {
|
|
43
|
+
// const attachmentHandle = Bridge.attachment.handleFor(this)
|
|
44
|
+
// const attachmentPath = FFI.dittoGetCompleteAttachmentPath(dittoHandle.deref(), attachmentHandle.deref())
|
|
45
|
+
// const fs = require('fs').promises
|
|
46
|
+
// return await fs.readFile(attachmentPath)
|
|
47
|
+
// }
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Copies the attachment to the specified file path. Node-only,
|
|
53
|
+
* throws in the browser.
|
|
54
|
+
*
|
|
55
|
+
* @param path The path that the attachment should be copied to.
|
|
56
|
+
*/
|
|
57
|
+
copyToPath(path: string): Promise<void> {
|
|
58
|
+
const ditto = this.ditto
|
|
59
|
+
const dittoHandle = Bridge.ditto.handleFor(ditto)
|
|
60
|
+
return this.ditto.deferCloseAsync(async () => {
|
|
61
|
+
if (Environment.isWebBuild) {
|
|
62
|
+
throw new Error(`Can't copy attachment to path, not available when running in the browser.`)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @internal */
|
|
70
|
+
constructor(ditto: Ditto, token: AttachmentToken) {
|
|
71
|
+
this.ditto = ditto
|
|
72
|
+
this.token = token
|
|
73
|
+
}
|
|
74
|
+
}
|