@vrtmrz/livesync-commonlib 0.4.2 → 0.5.0
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/README.md +7 -5
- package/dist/API/DirectFileManipulatorV2.d.ts +1 -0
- package/dist/API/DirectFileManipulatorV2.js +3 -1
- package/dist/API/processSetting.d.ts +1 -0
- package/dist/API/processSetting.js +3 -0
- package/dist/common/models/setting.const.defaults.js +1 -0
- package/dist/common/models/setting.const.qr.js +2 -1
- package/dist/common/models/setting.encryption.d.ts +2 -0
- package/dist/common/models/setting.encryption.js +6 -0
- package/dist/common/models/setting.lifecycle.js +7 -2
- package/dist/common/models/setting.policy.d.ts +4 -0
- package/dist/common/models/setting.policy.js +9 -0
- package/dist/common/models/setting.type.d.ts +2 -0
- package/dist/common/models/shared.definition.configNames.js +5 -0
- package/dist/common/models/tweak.definition.d.ts +1 -0
- package/dist/common/settingConstants.d.ts +1 -0
- package/dist/common/utils.js +5 -4
- package/dist/diagnostics/couchdb.js +4 -2
- package/dist/interfaces/StorageAccess.d.ts +8 -2
- package/dist/managers/ChunkFetcher.js +27 -4
- package/dist/managers/chunkFetcher.helper.js +1 -1
- package/dist/platform/FilePathIndex.d.ts +110 -0
- package/dist/platform/FilePathIndex.js +146 -0
- package/dist/pouchdb/StreamingFetch.d.ts +1 -2
- package/dist/pouchdb/encryption.d.ts +5 -10
- package/dist/pouchdb/encryption.eden.d.ts +11 -0
- package/dist/pouchdb/encryption.eden.js +37 -0
- package/dist/pouchdb/encryption.js +67 -63
- package/dist/pouchdb/encryption.policy.d.ts +9 -0
- package/dist/pouchdb/encryption.policy.js +125 -0
- package/dist/replication/UnencryptedContentRejectedError.d.ts +16 -0
- package/dist/replication/UnencryptedContentRejectedError.js +44 -0
- package/dist/replication/couchdb/LiveSyncReplicator.js +41 -4
- package/dist/replication/index.d.ts +2 -0
- package/dist/replication/index.js +3 -0
- package/dist/replication/trystero/LiveSyncTrysteroReplicator.d.ts +3 -3
- package/dist/replication/trystero/P2PAutomationCoordinator.d.ts +1 -1
- package/dist/replication/trystero/P2PReplicationWire.d.ts +1 -1
- package/dist/replication/trystero/TrysteroReplicator/changeRelay.d.ts +133 -0
- package/dist/replication/trystero/TrysteroReplicator/changeRelay.js +176 -0
- package/dist/replication/trystero/TrysteroReplicator/configurationExchange.d.ts +55 -0
- package/dist/replication/trystero/TrysteroReplicator/configurationExchange.js +99 -0
- package/dist/replication/trystero/TrysteroReplicator/configuredTargets.d.ts +83 -0
- package/dist/replication/trystero/TrysteroReplicator/configuredTargets.js +121 -0
- package/dist/replication/trystero/TrysteroReplicator/peerAgreement.d.ts +75 -0
- package/dist/replication/trystero/TrysteroReplicator/peerAgreement.js +131 -0
- package/dist/replication/trystero/TrysteroReplicator/progress.d.ts +57 -0
- package/dist/replication/trystero/TrysteroReplicator/progress.js +84 -0
- package/dist/replication/trystero/TrysteroReplicator/relayControl.d.ts +20 -0
- package/dist/replication/trystero/TrysteroReplicator/relayControl.js +25 -0
- package/dist/replication/trystero/TrysteroReplicator/transfers.d.ts +125 -0
- package/dist/replication/trystero/TrysteroReplicator/transfers.js +278 -0
- package/dist/replication/trystero/TrysteroReplicator.d.ts +65 -481
- package/dist/replication/trystero/TrysteroReplicator.js +240 -673
- package/dist/replication/trystero/TrysteroReplicatorP2PClient.d.ts +19 -6
- package/dist/replication/trystero/TrysteroReplicatorP2PClient.js +29 -25
- package/dist/replication/trystero/TrysteroReplicatorP2PServer/peerAdmission.d.ts +66 -0
- package/dist/replication/trystero/TrysteroReplicatorP2PServer/peerAdmission.js +142 -0
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.d.ts +19 -16
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.js +33 -93
- package/dist/replication/trystero/p2pReplicationDiagnostics.d.ts +1 -1
- package/dist/replication/trystero/p2pReplicationProgress.d.ts +2 -11
- package/dist/replication/trystero/replicationTypes.d.ts +88 -0
- package/dist/replication/trystero/replicationTypes.js +0 -0
- package/dist/replication/trystero/rpcSessionProvider.d.ts +13 -0
- package/dist/replication/trystero/rpcSessionProvider.js +0 -0
- package/dist/replication/unencryptedContentMessages.d.ts +8 -0
- package/dist/replication/unencryptedContentMessages.js +12 -0
- package/dist/rpc/RpcRoom/pendingInvocations.d.ts +141 -0
- package/dist/rpc/RpcRoom/pendingInvocations.js +225 -0
- package/dist/rpc/RpcRoom.d.ts +9 -4
- package/dist/rpc/RpcRoom.js +73 -118
- package/dist/rpc/RpcSession.d.ts +6 -0
- package/dist/rpc/RpcSession.js +11 -0
- package/dist/rpc/types.d.ts +2 -0
- package/dist/serviceFeatures/targetFilter.d.ts +1 -1
- package/dist/serviceFeatures/targetFilter.js +8 -38
- package/dist/serviceModules/FileAccessBase.d.ts +4 -0
- package/dist/serviceModules/FileAccessBase.js +10 -0
- package/dist/serviceModules/Rebuilder.js +29 -5
- package/dist/serviceModules/ServiceFileAccessBase/fileNames.d.ts +54 -0
- package/dist/serviceModules/ServiceFileAccessBase/fileNames.js +57 -0
- package/dist/serviceModules/ServiceFileAccessBase.d.ts +5 -1
- package/dist/serviceModules/ServiceFileAccessBase.js +18 -3
- package/dist/serviceModules/adapters/IFileSystemAdapter.d.ts +3 -0
- package/dist/services/base/RemoteConnection.d.ts +2 -0
- package/dist/services/base/RemoteService.js +2 -1
- package/dist/services/base/SettingService.js +2 -0
- package/dist/settings.d.ts +2 -1
- package/dist/settings.js +2 -0
- package/dist/storage.d.ts +2 -0
- package/dist/storage.js +2 -0
- package/dist/worker/bgWorker.js +1 -1
- package/docs/development.md +103 -0
- package/docs/offline-scanner.md +2 -1
- package/docs/p2p-transport-lifecycle.md +24 -0
- package/docs/platform-storage.md +22 -0
- package/docs/releasing.md +6 -0
- package/docs/settings-lifecycle.md +6 -0
- package/package.json +1 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared replication data contracts. Keep these independent of the facade,
|
|
3
|
+
* operation factories, and transport implementations so adapters can use the
|
|
4
|
+
* same types without importing the code they support.
|
|
5
|
+
*/
|
|
6
|
+
import type { ProgressInfo, ReplicationStatistics } from "../../pouchdb/ReplicatorShim.js";
|
|
7
|
+
/**
|
|
8
|
+
* Local replication outcome. Error-compatible values remain available to callers;
|
|
9
|
+
* the wire adapter converts them to JSON-safe data before transport.
|
|
10
|
+
*/
|
|
11
|
+
export type P2PReplicationResult = {
|
|
12
|
+
readonly status: "completed";
|
|
13
|
+
readonly ok: true;
|
|
14
|
+
readonly error?: never;
|
|
15
|
+
} | {
|
|
16
|
+
readonly status: "cancelled";
|
|
17
|
+
readonly ok?: false;
|
|
18
|
+
readonly error?: never;
|
|
19
|
+
} | {
|
|
20
|
+
readonly status: "failed";
|
|
21
|
+
readonly ok?: false;
|
|
22
|
+
readonly error: unknown;
|
|
23
|
+
};
|
|
24
|
+
/** Sequence progress retained for compatibility, with independently observed work counts. */
|
|
25
|
+
export type P2PReplicationDirectionProgress = {
|
|
26
|
+
max: number;
|
|
27
|
+
current: number;
|
|
28
|
+
isActive: boolean;
|
|
29
|
+
statistics?: ReplicationStatistics;
|
|
30
|
+
};
|
|
31
|
+
export type P2PReplicationProgress = {
|
|
32
|
+
peerId: string;
|
|
33
|
+
peerName: string;
|
|
34
|
+
fetching: P2PReplicationDirectionProgress;
|
|
35
|
+
sending: P2PReplicationDirectionProgress;
|
|
36
|
+
};
|
|
37
|
+
export type P2PReplicationReport = {
|
|
38
|
+
peerId: string;
|
|
39
|
+
peerName: string;
|
|
40
|
+
} & ({
|
|
41
|
+
fetching: P2PReplicationDirectionProgress;
|
|
42
|
+
} | {
|
|
43
|
+
sending: P2PReplicationDirectionProgress;
|
|
44
|
+
});
|
|
45
|
+
export type P2PConfiguredTargetResult = {
|
|
46
|
+
readonly name: string;
|
|
47
|
+
readonly peerId?: string;
|
|
48
|
+
} & ({
|
|
49
|
+
readonly status: "completed";
|
|
50
|
+
} | {
|
|
51
|
+
readonly status: "cancelled";
|
|
52
|
+
} | {
|
|
53
|
+
readonly status: "missing";
|
|
54
|
+
} | {
|
|
55
|
+
readonly status: "rejected";
|
|
56
|
+
} | {
|
|
57
|
+
readonly status: "undecided";
|
|
58
|
+
} | {
|
|
59
|
+
readonly status: "failed";
|
|
60
|
+
readonly error: unknown;
|
|
61
|
+
});
|
|
62
|
+
/** Explicit result of configured, unattended P2P target synchronisation. */
|
|
63
|
+
export type P2PConfiguredReplicationResult = {
|
|
64
|
+
readonly status: "completed";
|
|
65
|
+
readonly targets: readonly P2PConfiguredTargetResult[];
|
|
66
|
+
} | {
|
|
67
|
+
readonly status: "cancelled";
|
|
68
|
+
readonly targets: readonly P2PConfiguredTargetResult[];
|
|
69
|
+
} | {
|
|
70
|
+
readonly status: "blocked";
|
|
71
|
+
readonly reason: "no-targets";
|
|
72
|
+
readonly targets: readonly [];
|
|
73
|
+
} | {
|
|
74
|
+
readonly status: "partial";
|
|
75
|
+
readonly targets: readonly P2PConfiguredTargetResult[];
|
|
76
|
+
};
|
|
77
|
+
/** Transient attempt correlation; no replication checkpoint is stored here. */
|
|
78
|
+
export type ProgressAcknowledgement = {
|
|
79
|
+
attempt: number;
|
|
80
|
+
finalProgress?: ProgressInfo;
|
|
81
|
+
};
|
|
82
|
+
export type AcknowledgedProgressAttempt = {
|
|
83
|
+
attempt: number;
|
|
84
|
+
isActive: boolean;
|
|
85
|
+
checkedChanges: number;
|
|
86
|
+
};
|
|
87
|
+
/** Non-interactive result of evaluating the persisted and automatic peer policy. */
|
|
88
|
+
export type P2PPeerAcceptance = "accepted" | "rejected" | "undecided" | "unknown";
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RpcRoom } from "../../rpc/RpcRoom.js";
|
|
2
|
+
import type { RpcSession } from "../../rpc/RpcSession.js";
|
|
3
|
+
type RpcSessionCall = (...args: Parameters<RpcSession["call"]>) => ReturnType<RpcRoom["invoke"]>;
|
|
4
|
+
/** The current RPC room capability required by a P2P client. */
|
|
5
|
+
export type RpcSessionProvider = {
|
|
6
|
+
/** Resolve the active room for each operation; it may be replaced during the client's lifetime. */
|
|
7
|
+
rpcRoom?: {
|
|
8
|
+
session(peerId: string): Pick<RpcSession, "notify"> & {
|
|
9
|
+
call: RpcSessionCall;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare const rejectionConsequences: {
|
|
2
|
+
readonly replication: "Synchronisation stopped";
|
|
3
|
+
readonly "on-demand": "File content could not be retrieved";
|
|
4
|
+
readonly "fast-fetch": "Fast Fetch stopped without completing";
|
|
5
|
+
};
|
|
6
|
+
/** Explain a refused operation and its recovery action without including document content. */
|
|
7
|
+
export declare function getUnencryptedContentRejectionMessage(operation: keyof typeof rejectionConsequences): string;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const rejectionConsequences = {
|
|
2
|
+
replication: "Synchronisation stopped",
|
|
3
|
+
"on-demand": "File content could not be retrieved",
|
|
4
|
+
"fast-fetch": "Fast Fetch stopped without completing"
|
|
5
|
+
};
|
|
6
|
+
function getUnencryptedContentRejectionMessage(operation) {
|
|
7
|
+
const nextAction = operation === "on-demand" ? ", then retry the file retrieval." : ".";
|
|
8
|
+
return `${rejectionConsequences[operation]} because unencrypted content was rejected. Review the remote data and 'Accept unencrypted content' in the E2EE Advanced settings${nextAction}`;
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
getUnencryptedContentRejectionMessage
|
|
12
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { type TimeoutScheduler } from "octagonal-wheels/concurrency/deferredAction";
|
|
2
|
+
import { RpcError } from "../errors.js";
|
|
3
|
+
import type { JsonLike, RpcCallOptions, RpcEnvelope, RpcResponseErrorEnvelope, RpcResponseEnvelope } from "../types.js";
|
|
4
|
+
type PendingInvocation = {
|
|
5
|
+
peerId: string;
|
|
6
|
+
resolve: (value: JsonLike) => void;
|
|
7
|
+
reject: (reason?: unknown) => void;
|
|
8
|
+
releaseWait: () => void;
|
|
9
|
+
/** Set only after the request send completes, so cancellation can follow it on the wire. */
|
|
10
|
+
requestSent: boolean;
|
|
11
|
+
/** Kept on the send task's captured record after map removal until cancellation sequencing is complete. */
|
|
12
|
+
cancelRequested: boolean;
|
|
13
|
+
};
|
|
14
|
+
type PendingInvocationsOptions = {
|
|
15
|
+
/** Calls RpcRoom's current envelope sender at invocation time; the Room owns wire serialisation and chunking. */
|
|
16
|
+
sendEnvelope: (peerId: string, envelope: RpcEnvelope) => Promise<void>;
|
|
17
|
+
/** Uses RpcRoom's existing request-ID generator, shared with its other request-envelope producers. */
|
|
18
|
+
createRequestId: () => string;
|
|
19
|
+
/** Uses RpcRoom's compatibility timer scheduler; non-positive timeout values do not schedule a deadline. */
|
|
20
|
+
scheduleTimeout: TimeoutScheduler;
|
|
21
|
+
/** Reports failures from background cancellation sends. */
|
|
22
|
+
onProtocolWarning?: (message: string, peerId: string) => void;
|
|
23
|
+
};
|
|
24
|
+
type PendingInvocationState = {
|
|
25
|
+
pending: Map<string, PendingInvocation>;
|
|
26
|
+
};
|
|
27
|
+
type RemovePendingContext = Pick<PendingInvocationState, "pending">;
|
|
28
|
+
type RemovePendingOperation = (requestId: string) => PendingInvocation | undefined;
|
|
29
|
+
type NotifyRemoteCancellationContext = Pick<PendingInvocationsOptions, "sendEnvelope" | "onProtocolWarning">;
|
|
30
|
+
type NotifyRemoteCancellationOperation = (peerId: string, requestId: string) => void;
|
|
31
|
+
type RejectPendingContext = Pick<PendingInvocationState, "pending"> & {
|
|
32
|
+
removePending: RemovePendingOperation;
|
|
33
|
+
notifyRemoteCancellation: NotifyRemoteCancellationOperation;
|
|
34
|
+
};
|
|
35
|
+
type RejectPendingOperation = (requestId: string, error: RpcError, notifyRemote?: boolean) => void;
|
|
36
|
+
type RegisterPendingWaitContext = Pick<PendingInvocationState, "pending"> & Pick<PendingInvocationsOptions, "scheduleTimeout"> & {
|
|
37
|
+
rejectPending: RejectPendingOperation;
|
|
38
|
+
};
|
|
39
|
+
type RegisterPendingWaitOperation = (requestId: string, peerId: string, method: string, options: RpcCallOptions, timeoutMs: number, onInvocationCreated: (invocation: PendingInvocation) => void, resolve: PendingInvocation["resolve"], reject: PendingInvocation["reject"]) => void;
|
|
40
|
+
type InvokePendingInvocationContext = Pick<PendingInvocationState, "pending"> & Pick<PendingInvocationsOptions, "sendEnvelope" | "createRequestId"> & {
|
|
41
|
+
registerPendingWait: RegisterPendingWaitOperation;
|
|
42
|
+
removePending: RemovePendingOperation;
|
|
43
|
+
notifyRemoteCancellation: NotifyRemoteCancellationOperation;
|
|
44
|
+
};
|
|
45
|
+
type CancelPendingInvocationContext = Pick<PendingInvocationState, "pending"> & Pick<PendingInvocationsOptions, "sendEnvelope"> & {
|
|
46
|
+
rejectPending: RejectPendingOperation;
|
|
47
|
+
};
|
|
48
|
+
type ReceivePendingResponseContext = {
|
|
49
|
+
removePending: RemovePendingOperation;
|
|
50
|
+
};
|
|
51
|
+
type RejectPendingForPeerDepartureContext = Pick<PendingInvocationState, "pending"> & {
|
|
52
|
+
rejectPending: RejectPendingOperation;
|
|
53
|
+
};
|
|
54
|
+
type ClosePendingInvocationsContext = RejectPendingForPeerDepartureContext;
|
|
55
|
+
/**
|
|
56
|
+
* Removes a wait and synchronously releases its deadline and abort listener
|
|
57
|
+
* before returning the invocation to its caller.
|
|
58
|
+
*/
|
|
59
|
+
export declare function removePending(context: RemovePendingContext, requestId: string): PendingInvocation | undefined;
|
|
60
|
+
/** Sends cancellation without delaying the local outcome; reports a failed background send. */
|
|
61
|
+
export declare function notifyRemoteCancellation(context: NotifyRemoteCancellationContext, peerId: string, requestId: string): void;
|
|
62
|
+
/**
|
|
63
|
+
* Rejects a live wait. When remote cancellation is requested, records that
|
|
64
|
+
* intent even if the request send has not completed yet.
|
|
65
|
+
*/
|
|
66
|
+
export declare function rejectPending(context: RejectPendingContext, requestId: string, error: RpcError, notifyRemote?: boolean): void;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a wait record, deadline, and abort listener. The creation callback
|
|
69
|
+
* retains the current Promise executor boundary before deadline setup can throw.
|
|
70
|
+
*/
|
|
71
|
+
export declare function registerPendingWait(context: RegisterPendingWaitContext, requestId: string, peerId: string, method: string, options: RpcCallOptions, timeoutMs: number, onInvocationCreated: (invocation: PendingInvocation) => void, resolve: PendingInvocation["resolve"], reject: PendingInvocation["reject"]): void;
|
|
72
|
+
/**
|
|
73
|
+
* Registers and sends one response-bearing request, then waits for its
|
|
74
|
+
* matching response. The returned promise resolves with response data and
|
|
75
|
+
* rejects with remote RPC errors, a request-send failure, or local RpcError
|
|
76
|
+
* outcomes: CANCELLED for abort or explicit cancellation, TIMEOUT for the
|
|
77
|
+
* deadline, and NOT_CONNECTED for peer or room disconnection.
|
|
78
|
+
*
|
|
79
|
+
* Transport send completion only establishes whether cancellation can be
|
|
80
|
+
* sent immediately; it is not the remote operation's result. A non-positive
|
|
81
|
+
* timeout disables the deadline while keeping abort and other settlement
|
|
82
|
+
* paths active. An already-aborted signal rejects before allocating an ID,
|
|
83
|
+
* scheduling a deadline, registering a listener, or sending the request.
|
|
84
|
+
*
|
|
85
|
+
* @param timeoutOrOptions - A timeout in milliseconds or RPC call options.
|
|
86
|
+
* Non-positive timeouts disable only the deadline; an AbortSignal remains
|
|
87
|
+
* active.
|
|
88
|
+
*/
|
|
89
|
+
export declare function invokePendingInvocation(context: InvokePendingInvocationContext, peerId: string, method: string, args: JsonLike[], timeoutOrOptions?: number | RpcCallOptions): Promise<JsonLike>;
|
|
90
|
+
/**
|
|
91
|
+
* Rejects a matching local wait and requests remote cancellation.
|
|
92
|
+
*
|
|
93
|
+
* If its request send is still pending, returns after local rejection; the
|
|
94
|
+
* send-completion path will preserve request-before-cancel wire order. If the
|
|
95
|
+
* request has already been sent, this call waits for the cancel send.
|
|
96
|
+
* Failure from that immediate send rejects this call; a deferred send uses
|
|
97
|
+
* the background warning path. An unknown ID or an ID owned by another peer
|
|
98
|
+
* still sends the supplied cancellation envelope, preserving
|
|
99
|
+
* RpcRoom.cancel behaviour.
|
|
100
|
+
*/
|
|
101
|
+
export declare function cancelPendingInvocation(context: CancelPendingInvocationContext, peerId: string, requestId: string): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Settles the wait for a matching request ID, resolving successful data
|
|
104
|
+
* or rejecting with the remote error's code, message, and details.
|
|
105
|
+
* Unknown IDs are ignored, including unsolicited or surplus replies from
|
|
106
|
+
* peers that answer one-way notifications.
|
|
107
|
+
*/
|
|
108
|
+
export declare function receivePendingResponse(context: ReceivePendingResponseContext, envelope: RpcResponseEnvelope | RpcResponseErrorEnvelope): void;
|
|
109
|
+
/**
|
|
110
|
+
* Rejects this peer's local waits without attempting remote cancellation:
|
|
111
|
+
* a departed peer is no longer available to receive it.
|
|
112
|
+
*/
|
|
113
|
+
export declare function rejectPendingForPeerDeparture(context: RejectPendingForPeerDepartureContext, peerId: string): void;
|
|
114
|
+
/**
|
|
115
|
+
* Rejects all local waits with NOT_CONNECTED and requests remote
|
|
116
|
+
* cancellation for sent requests. RpcRoom calls this before clearing its
|
|
117
|
+
* state, while the send callback is still usable. Requests still being sent
|
|
118
|
+
* carry cancellation intent until their send completes.
|
|
119
|
+
*/
|
|
120
|
+
export declare function closePendingInvocations(context: ClosePendingInvocationsContext): void;
|
|
121
|
+
/**
|
|
122
|
+
* Creates the owner for outbound response-bearing invocation waits.
|
|
123
|
+
*
|
|
124
|
+
* RpcRoom checks method names and room-open state, and owns envelope
|
|
125
|
+
* serialisation, transport, chunking, and inbound requests. This component
|
|
126
|
+
* owns pending request IDs, wait resources, cancellation sequencing, and
|
|
127
|
+
* response-to-promise settlement.
|
|
128
|
+
*
|
|
129
|
+
* The Room supplies narrow callbacks rather than its full state. Completing a
|
|
130
|
+
* request send records wire progress but does not complete the invocation;
|
|
131
|
+
* only a matching response supplies its remote result. The returned operations
|
|
132
|
+
* also handle explicit cancellation, response dispatch, peer departure, and close.
|
|
133
|
+
*/
|
|
134
|
+
export declare function createPendingInvocations({ sendEnvelope, createRequestId, scheduleTimeout, onProtocolWarning, }: PendingInvocationsOptions): {
|
|
135
|
+
invoke: (peerId: string, method: string, args: JsonLike[], timeoutOrOptions?: number | RpcCallOptions) => Promise<JsonLike>;
|
|
136
|
+
cancel: (peerId: string, requestId: string) => Promise<void>;
|
|
137
|
+
receiveResponse: (envelope: RpcResponseEnvelope | RpcResponseErrorEnvelope) => void;
|
|
138
|
+
peerLeft: (peerId: string) => void;
|
|
139
|
+
close: () => void;
|
|
140
|
+
};
|
|
141
|
+
export {};
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { bindContext } from "octagonal-wheels/context";
|
|
2
|
+
import { createDeferredAction } from "octagonal-wheels/concurrency/deferredAction";
|
|
3
|
+
import { RpcError } from "../errors.js";
|
|
4
|
+
function removePending(context, requestId) {
|
|
5
|
+
const { pending } = context;
|
|
6
|
+
const invocation = pending.get(requestId);
|
|
7
|
+
if (!invocation) {
|
|
8
|
+
return void 0;
|
|
9
|
+
}
|
|
10
|
+
pending.delete(requestId);
|
|
11
|
+
invocation.releaseWait();
|
|
12
|
+
return invocation;
|
|
13
|
+
}
|
|
14
|
+
function notifyRemoteCancellation(context, peerId, requestId) {
|
|
15
|
+
const { sendEnvelope, onProtocolWarning } = context;
|
|
16
|
+
void sendEnvelope(peerId, { kind: "cancel", requestId }).catch((error) => {
|
|
17
|
+
onProtocolWarning?.("Failed to send RPC cancellation: " + String(error), peerId);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function rejectPending(context, requestId, error, notifyRemote = false) {
|
|
21
|
+
const { pending, removePending: removePending2, notifyRemoteCancellation: notifyRemoteCancellation2 } = context;
|
|
22
|
+
const invocation = pending.get(requestId);
|
|
23
|
+
if (!invocation) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (notifyRemote) {
|
|
27
|
+
invocation.cancelRequested = true;
|
|
28
|
+
}
|
|
29
|
+
const removed = removePending2(requestId);
|
|
30
|
+
if (!removed) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
removed.reject(error);
|
|
34
|
+
if (notifyRemote && removed.requestSent) {
|
|
35
|
+
notifyRemoteCancellation2(removed.peerId, requestId);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function registerPendingWait(context, requestId, peerId, method, options, timeoutMs, onInvocationCreated, resolve, reject) {
|
|
39
|
+
const { pending, scheduleTimeout, rejectPending: rejectPending2 } = context;
|
|
40
|
+
const signal = options.signal;
|
|
41
|
+
const onAbort = () => {
|
|
42
|
+
const current = pending.get(requestId);
|
|
43
|
+
if (!current) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
rejectPending2(requestId, new RpcError("CANCELLED", "RPC cancelled: " + method), true);
|
|
47
|
+
};
|
|
48
|
+
const deadline = timeoutMs > 0 ? createDeferredAction(() => {
|
|
49
|
+
rejectPending2(requestId, new RpcError("TIMEOUT", "RPC timed out: " + method), true);
|
|
50
|
+
}, scheduleTimeout) : void 0;
|
|
51
|
+
const invocation = {
|
|
52
|
+
peerId,
|
|
53
|
+
resolve,
|
|
54
|
+
reject,
|
|
55
|
+
releaseWait: () => {
|
|
56
|
+
deadline?.dispose();
|
|
57
|
+
signal?.removeEventListener("abort", onAbort);
|
|
58
|
+
},
|
|
59
|
+
requestSent: false,
|
|
60
|
+
cancelRequested: false
|
|
61
|
+
};
|
|
62
|
+
onInvocationCreated(invocation);
|
|
63
|
+
deadline?.schedule(timeoutMs);
|
|
64
|
+
pending.set(requestId, invocation);
|
|
65
|
+
if (signal) {
|
|
66
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
67
|
+
if (signal.aborted) {
|
|
68
|
+
onAbort();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function invokePendingInvocation(context, peerId, method, args, timeoutOrOptions = 3e4) {
|
|
73
|
+
const { pending, sendEnvelope, createRequestId, registerPendingWait: registerPendingWait2, removePending: removePending2, notifyRemoteCancellation: notifyRemoteCancellation2 } = context;
|
|
74
|
+
const options = typeof timeoutOrOptions === "number" ? { timeoutMs: timeoutOrOptions } : timeoutOrOptions ?? {};
|
|
75
|
+
const timeoutMs = options.timeoutMs ?? 3e4;
|
|
76
|
+
if (options.signal?.aborted) {
|
|
77
|
+
throw new RpcError("CANCELLED", "RPC cancelled: " + method);
|
|
78
|
+
}
|
|
79
|
+
const requestId = createRequestId();
|
|
80
|
+
let invocation;
|
|
81
|
+
const result = new Promise((resolve, reject) => {
|
|
82
|
+
registerPendingWait2(
|
|
83
|
+
requestId,
|
|
84
|
+
peerId,
|
|
85
|
+
method,
|
|
86
|
+
options,
|
|
87
|
+
timeoutMs,
|
|
88
|
+
(created) => {
|
|
89
|
+
invocation = created;
|
|
90
|
+
},
|
|
91
|
+
resolve,
|
|
92
|
+
reject
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
const sendTask = sendEnvelope(peerId, {
|
|
96
|
+
kind: "request",
|
|
97
|
+
requestId,
|
|
98
|
+
method,
|
|
99
|
+
args
|
|
100
|
+
});
|
|
101
|
+
void sendTask.then(
|
|
102
|
+
() => {
|
|
103
|
+
const current = pending.get(requestId);
|
|
104
|
+
invocation.requestSent = true;
|
|
105
|
+
if (!current && invocation.cancelRequested) {
|
|
106
|
+
notifyRemoteCancellation2(peerId, requestId);
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
(error) => {
|
|
110
|
+
const current = pending.get(requestId);
|
|
111
|
+
if (!current) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const failed = removePending2(requestId);
|
|
115
|
+
if (failed) {
|
|
116
|
+
failed.reject(error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
return await result;
|
|
121
|
+
}
|
|
122
|
+
async function cancelPendingInvocation(context, peerId, requestId) {
|
|
123
|
+
const { pending, sendEnvelope, rejectPending: rejectPending2 } = context;
|
|
124
|
+
const invocation = pending.get(requestId);
|
|
125
|
+
if (invocation?.peerId === peerId) {
|
|
126
|
+
const requestSent = invocation.requestSent;
|
|
127
|
+
invocation.cancelRequested = true;
|
|
128
|
+
rejectPending2(requestId, new RpcError("CANCELLED", "Invocation cancelled"));
|
|
129
|
+
if (!requestSent) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
await sendEnvelope(peerId, {
|
|
134
|
+
kind: "cancel",
|
|
135
|
+
requestId
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function receivePendingResponse(context, envelope) {
|
|
139
|
+
const { removePending: removePending2 } = context;
|
|
140
|
+
const settled = removePending2(envelope.requestId);
|
|
141
|
+
if (!settled) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (envelope.ok === true) {
|
|
145
|
+
settled.resolve(envelope.data);
|
|
146
|
+
} else {
|
|
147
|
+
settled.reject(new RpcError(envelope.error.code, envelope.error.message, envelope.error.details));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function rejectPendingForPeerDeparture(context, peerId) {
|
|
151
|
+
const { pending, rejectPending: rejectPending2 } = context;
|
|
152
|
+
pending.forEach((invocation, requestId) => {
|
|
153
|
+
if (invocation.peerId !== peerId) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
rejectPending2(requestId, new RpcError("NOT_CONNECTED", "Peer disconnected"));
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function closePendingInvocations(context) {
|
|
160
|
+
const { pending, rejectPending: rejectPending2 } = context;
|
|
161
|
+
pending.forEach((_invocation, requestId) => {
|
|
162
|
+
rejectPending2(requestId, new RpcError("NOT_CONNECTED", "Room closed"), true);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
function createPendingInvocations({
|
|
166
|
+
sendEnvelope,
|
|
167
|
+
createRequestId,
|
|
168
|
+
scheduleTimeout,
|
|
169
|
+
onProtocolWarning
|
|
170
|
+
}) {
|
|
171
|
+
const state = {
|
|
172
|
+
pending: /* @__PURE__ */ new Map()
|
|
173
|
+
};
|
|
174
|
+
const removeOperation = bindContext(state, removePending);
|
|
175
|
+
const notifyRemoteCancellationOperation = bindContext(
|
|
176
|
+
{ sendEnvelope, onProtocolWarning },
|
|
177
|
+
notifyRemoteCancellation
|
|
178
|
+
);
|
|
179
|
+
const rejectOperation = bindContext(
|
|
180
|
+
{
|
|
181
|
+
pending: state.pending,
|
|
182
|
+
removePending: removeOperation,
|
|
183
|
+
notifyRemoteCancellation: notifyRemoteCancellationOperation
|
|
184
|
+
},
|
|
185
|
+
rejectPending
|
|
186
|
+
);
|
|
187
|
+
const registerPendingWaitOperation = bindContext(
|
|
188
|
+
{ pending: state.pending, scheduleTimeout, rejectPending: rejectOperation },
|
|
189
|
+
registerPendingWait
|
|
190
|
+
);
|
|
191
|
+
const invoke = bindContext(
|
|
192
|
+
{
|
|
193
|
+
pending: state.pending,
|
|
194
|
+
sendEnvelope,
|
|
195
|
+
createRequestId,
|
|
196
|
+
registerPendingWait: registerPendingWaitOperation,
|
|
197
|
+
removePending: removeOperation,
|
|
198
|
+
notifyRemoteCancellation: notifyRemoteCancellationOperation
|
|
199
|
+
},
|
|
200
|
+
invokePendingInvocation
|
|
201
|
+
);
|
|
202
|
+
const cancel = bindContext(
|
|
203
|
+
{ pending: state.pending, sendEnvelope, rejectPending: rejectOperation },
|
|
204
|
+
cancelPendingInvocation
|
|
205
|
+
);
|
|
206
|
+
const receiveResponse = bindContext({ removePending: removeOperation }, receivePendingResponse);
|
|
207
|
+
const peerLeft = bindContext(
|
|
208
|
+
{ pending: state.pending, rejectPending: rejectOperation },
|
|
209
|
+
rejectPendingForPeerDeparture
|
|
210
|
+
);
|
|
211
|
+
const close = bindContext({ pending: state.pending, rejectPending: rejectOperation }, closePendingInvocations);
|
|
212
|
+
return { invoke, cancel, receiveResponse, peerLeft, close };
|
|
213
|
+
}
|
|
214
|
+
export {
|
|
215
|
+
cancelPendingInvocation,
|
|
216
|
+
closePendingInvocations,
|
|
217
|
+
createPendingInvocations,
|
|
218
|
+
invokePendingInvocation,
|
|
219
|
+
notifyRemoteCancellation,
|
|
220
|
+
receivePendingResponse,
|
|
221
|
+
registerPendingWait,
|
|
222
|
+
rejectPending,
|
|
223
|
+
rejectPendingForPeerDeparture,
|
|
224
|
+
removePending
|
|
225
|
+
};
|
package/dist/rpc/RpcRoom.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { RpcSession } from "./RpcSession.js";
|
|
|
2
2
|
import { type JsonLike, type RpcCallOptions, type RpcCancellationAwareMethodHandler, type RpcMethodHandler, type RpcRegisterOptions, type RpcRoomOptions } from "./types.js";
|
|
3
3
|
export declare class RpcRoom {
|
|
4
4
|
private options;
|
|
5
|
-
private
|
|
5
|
+
private pendingInvocations;
|
|
6
6
|
private inboundCalls;
|
|
7
7
|
private methods;
|
|
8
8
|
private sessions;
|
|
@@ -26,14 +26,19 @@ export declare class RpcRoom {
|
|
|
26
26
|
invoke(peerId: string, method: string, args: JsonLike[], timeoutMs?: number): Promise<JsonLike>;
|
|
27
27
|
invoke(peerId: string, method: string, args: JsonLike[], options?: RpcCallOptions): Promise<JsonLike>;
|
|
28
28
|
invoke(peerId: string, method: string, args: JsonLike[], timeoutOrOptions?: number | RpcCallOptions): Promise<JsonLike>;
|
|
29
|
+
/**
|
|
30
|
+
* Send a notification to a peer without creating an RPC response wait.
|
|
31
|
+
* Resolves when the local transport send completes; it does not report
|
|
32
|
+
* whether the peer accepted or completed the handler.
|
|
33
|
+
*/
|
|
34
|
+
notify(peerId: string, method: string, args: JsonLike[]): Promise<void>;
|
|
29
35
|
cancel(peerId: string, requestId: string): Promise<void>;
|
|
30
|
-
private removePending;
|
|
31
|
-
private rejectPending;
|
|
32
|
-
private notifyRemoteCancellation;
|
|
33
36
|
private abortInboundCall;
|
|
34
37
|
private sendEnvelope;
|
|
35
38
|
private sendChunk;
|
|
36
39
|
private sendErrorResponse;
|
|
40
|
+
private reportRequestFailure;
|
|
41
|
+
private warnNotificationIssue;
|
|
37
42
|
private scheduleMissingAck;
|
|
38
43
|
private onWireMessage;
|
|
39
44
|
private onEnvelopePayload;
|