@vrtmrz/livesync-commonlib 0.6.1 → 0.7.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 +29 -19
- package/dist/common/models/shared.definition.d.ts +10 -1
- package/dist/common/models/shared.definition.js +4 -3
- package/dist/events/coreEvents.d.ts +3 -0
- package/dist/events/coreEvents.js +2 -0
- package/dist/managers/EntryManager/EntryManager.d.ts +1 -1
- package/dist/managers/EntryManager/EntryManager.js +2 -2
- package/dist/managers/EntryManager/EntryManagerImpls.d.ts +1 -1
- package/dist/managers/EntryManager/EntryManagerImpls.js +4 -4
- package/dist/managers/StorageEventManager/admission.d.ts +1 -0
- package/dist/managers/StorageEventManager/admission.js +5 -1
- package/dist/managers/StorageEventManager.d.ts +2 -0
- package/dist/managers/StorageEventManager.js +1 -0
- package/dist/pouchdb/LiveSyncLocalDB.d.ts +1 -1
- package/dist/pouchdb/LiveSyncLocalDB.js +2 -2
- package/dist/replication/LiveSyncAbstractReplicator.d.ts +7 -0
- package/dist/replication/LiveSyncAbstractReplicator.js +5 -2
- package/dist/replication/ReplicatorInstance.d.ts +3 -0
- package/dist/replication/ReplicatorProvider.d.ts +23 -2
- package/dist/replication/ReplicatorProvider.js +10 -0
- package/dist/replication/SyncParamsHandler.js +26 -25
- package/dist/replication/couchdb/LiveSyncReplicator.d.ts +14 -2
- package/dist/replication/couchdb/LiveSyncReplicator.js +247 -122
- package/dist/replication/index.d.ts +4 -2
- package/dist/replication/index.js +6 -0
- package/dist/replication/remoteTransportFailure.d.ts +24 -0
- package/dist/replication/remoteTransportFailure.js +80 -0
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.d.ts +1 -1
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.js +2 -2
- package/dist/replication/trystero/TrysteroRpcRoom.d.ts +14 -0
- package/dist/replication/trystero/TrysteroRpcRoom.js +168 -0
- package/dist/replication/trystero/rpcCompat.d.ts +1 -0
- package/dist/replication/trystero/rpcCompat.js +15 -0
- package/dist/rpc/RpcRoom.d.ts +1 -0
- package/dist/rpc/RpcRoom.js +21 -9
- package/dist/rpc/RpcSession.d.ts +8 -13
- package/dist/rpc/RpcSession.js +14 -35
- package/dist/rpc/commonlibRpcRoom.d.ts +1 -0
- package/dist/rpc/commonlibRpcRoom.js +4 -0
- package/dist/rpc/errors.d.ts +9 -3
- package/dist/rpc/errors.js +13 -16
- package/dist/rpc/index.d.ts +10 -1
- package/dist/rpc/index.js +2 -1
- package/dist/rpc/pouchdb/RpcPouchDBProxy.d.ts +1 -1
- package/dist/rpc/pouchdb/RpcPouchDBServer.d.ts +1 -1
- package/dist/serviceFeatures/targetFilter.d.ts +3 -1
- package/dist/serviceFeatures/targetFilter.js +2 -2
- package/dist/services/base/IService.d.ts +9 -6
- package/dist/services/base/ReadinessEvaluator.d.ts +7 -6
- package/dist/services/base/ReadinessEvaluator.js +4 -2
- package/dist/services/base/RemoteConnection.d.ts +4 -0
- package/dist/services/base/RemoteService.d.ts +5 -3
- package/dist/services/base/RemoteService.js +85 -33
- package/dist/services/base/ReplicationService.d.ts +10 -2
- package/dist/services/base/ReplicationService.js +13 -7
- package/dist/services/base/ReplicationService.readiness.d.ts +14 -4
- package/dist/services/base/ReplicationService.readiness.js +29 -2
- package/dist/services/base/ReplicationService.typedReplication.d.ts +5 -2
- package/dist/services/base/ReplicationService.typedReplication.js +67 -6
- package/dist/services/base/ReplicationStaticsOwner.d.ts +18 -0
- package/dist/services/base/ReplicationStaticsOwner.js +33 -0
- package/dist/services/base/ReplicatorService.d.ts +2 -10
- package/dist/services/base/ReplicatorService.js +6 -3
- package/dist/services/base/VaultService.d.ts +1 -0
- package/dist/string_and_binary/chunks.d.ts +1 -1
- package/dist/string_and_binary/chunks.js +63 -50
- package/dist/worker/bg.worker.js +3 -1
- package/dist/worker/bg.worker.splitting.d.ts +1 -0
- package/dist/worker/bg.worker.splitting.js +42 -6
- package/dist/worker/bgWorker.d.ts +4 -0
- package/dist/worker/bgWorker.js +87 -57
- package/dist/worker/bgWorker.splitting.d.ts +2 -3
- package/dist/worker/bgWorker.splitting.js +48 -52
- package/dist/worker/universalTypes.d.ts +13 -1
- package/docs/development.md +69 -32
- package/docs/p2p-transport-lifecycle.md +2 -3
- package/docs/storage-events-and-reflection.md +10 -3
- package/package.json +3 -2
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export { CAPABILITY_NOT_APPLICABLE, CAPABILITY_NOT_IMPLEMENTED, CENTRAL_REMOTE_REPLICATION_READINESS, NO_INTERACTION, PEER_REPLICATION_READINESS, PROVIDER_OWNED_CENTRAL_REMOTE_REPLICATION_READINESS, REPLICATION_CANCELLED, REPLICATION_COMPLETED, REPLICATION_PROGRESS_PRESENTATIONS, USER_INITIATED_REPLICATION_AUTHORITY, defineReplicatorProviderDefinitions, isReplicationCompleted, outcomeFromContinuousOpenReplication, outcomeFromFiniteOpenReplication, replicationBlocked, replicationFailed, supportedOpenReplicationContinuous, supportedOpenReplicationOneShot, supportedOpenReplicationUnattended, supportedStopActiveTransfer, supportedCapability, } from "./ReplicatorProvider.js";
|
|
1
|
+
export { CAPABILITY_NOT_APPLICABLE, CAPABILITY_NOT_IMPLEMENTED, CENTRAL_REMOTE_REPLICATION_READINESS, NO_INTERACTION, PEER_REPLICATION_READINESS, PROVIDER_OWNED_CENTRAL_REMOTE_REPLICATION_READINESS, REPLICATION_CANCELLED, REPLICATION_COMPLETED, REPLICATION_PROGRESS_PRESENTATIONS, USER_INITIATED_REPLICATION_AUTHORITY, defineReplicatorProviderDefinitions, isReplicationCompleted, observedOfflineReplicationFailure, outcomeFromContinuousOpenReplication, outcomeFromFiniteOpenReplication, replicationBlocked, replicationFailed, supportedOpenReplicationContinuous, supportedOpenReplicationOneShot, supportedOpenReplicationUnattended, supportedStopActiveTransfer, supportedCapability, } from "./ReplicatorProvider.js";
|
|
2
|
+
export { EVENT_REPLICATION_ABORT_REQUESTED } from "../events/coreEvents.js";
|
|
3
|
+
export { getReplicationNetworkFailure } from "./remoteTransportFailure.js";
|
|
2
4
|
export type { ReplicatorInstance } from "./ReplicatorInstance.js";
|
|
3
5
|
export { ContentReadError } from "./contentRead.js";
|
|
4
6
|
export type { ContentReadFailure, ContentReadResource } from "./contentRead.js";
|
|
@@ -7,7 +9,7 @@ export type { FileReflectionResult, ReflectionTarget, ReflectionOperationContext
|
|
|
7
9
|
export { isUnencryptedContentRejectedError, UnencryptedContentRejectedError, } from "./UnencryptedContentRejectedError.js";
|
|
8
10
|
export type { UnencryptedContentKind } from "./UnencryptedContentRejectedError.js";
|
|
9
11
|
export { UnsupportedEncryptedDocumentFormatError } from "./UnsupportedEncryptedDocumentFormatError.js";
|
|
10
|
-
export type { ActiveReplicatorContext, CapabilitySupport, ContinuousReplicationRequest, ContinuousReplicationTrigger, ContinuousRunner, InteractionAuthority, InteractionPermissions, NotApplicableCapability, NotImplementedCapability, NoInteraction, PermittedInteractionPermissions, ReplicationBlockReason, ReplicationAttemptFailure, ReplicationCancelled, ReplicationCompleted, ReplicationFailed, ReplicationInteraction, ReplicationOutcome, ReplicationProgressPresentation, ReplicationReadinessRequirements, ReplicationPartial, ReplicationFailureRequest, ReplicatorConfigurationIdentity, ReplicatorProviderDefinition, ReplicatorProviderDefinitionMap, StopActiveTransferRunner, SupportedCapability, UnattendedOneShotRequest, UnattendedReplicationTrigger, UnattendedOneShotRunner, UserInitiatedOneShotRequest, UserInitiatedReplicationAuthority, UserInitiatedOneShotRunner, } from "./ReplicatorProvider.js";
|
|
12
|
+
export type { ActiveReplicatorContext, CapabilitySupport, ContinuousReplicationNetworkFailure, ReplicationPreparationResult, ContinuousReplicationRequest, ContinuousReplicationStartResult, ContinuousReplicationTrigger, ContinuousRunner, InteractionAuthority, InteractionPermissions, NotApplicableCapability, NotImplementedCapability, NoInteraction, PermittedInteractionPermissions, ReplicationBlockReason, ReplicationAttemptFailure, ReplicationCancelled, ReplicationCompleted, ReplicationFailed, ReplicationInteraction, ReplicationOutcome, ReplicationProgressPresentation, ReplicationReadinessRequirements, ReplicationPartial, ReplicationFailureRequest, ReplicatorConfigurationIdentity, ReplicatorProviderDefinition, ReplicatorProviderDefinitionMap, StopActiveTransferRunner, SupportedCapability, UnattendedOneShotRequest, UnattendedReplicationTrigger, UnattendedOneShotRunner, UserInitiatedOneShotRequest, UserInitiatedReplicationAuthority, UserInitiatedOneShotRunner, } from "./ReplicatorProvider.js";
|
|
11
13
|
export { CAPABILITY_SUPPORT_KINDS, CAPABILITY_UNAVAILABLE_REASONS } from "./ProviderCapability.js";
|
|
12
14
|
export { NO_REMOTE_RESOURCE_CAPABILITIES, REMOTE_RESOURCE_KINDS } from "./RemoteResource.js";
|
|
13
15
|
export type { InitialFetchFastForwardResource, InitialFetchFastForwardResourceFactory, ConnectionProbeFactory, JoinedDevicesResource, JoinedDevicesResourceFactory, PreferredTweakProbe, PreferredTweakProbeFactory, RemoteConnectionProbe, RemoteConnectionProbeOptions, RemoteConnectionProbeResult, RemoteResourceCapabilities, RemoteResourceFactoryMap, RemoteResourceKind, RemoteResourceMap, SecuritySeedResource, SecuritySeedResourceFactory, SynchronisationInformationResource, SynchronisationInformationResourceFactory, } from "./RemoteResource.js";
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
USER_INITIATED_REPLICATION_AUTHORITY,
|
|
12
12
|
defineReplicatorProviderDefinitions,
|
|
13
13
|
isReplicationCompleted,
|
|
14
|
+
observedOfflineReplicationFailure,
|
|
14
15
|
outcomeFromContinuousOpenReplication,
|
|
15
16
|
outcomeFromFiniteOpenReplication,
|
|
16
17
|
replicationBlocked,
|
|
@@ -21,6 +22,8 @@ import {
|
|
|
21
22
|
supportedStopActiveTransfer,
|
|
22
23
|
supportedCapability
|
|
23
24
|
} from "./ReplicatorProvider.js";
|
|
25
|
+
import { EVENT_REPLICATION_ABORT_REQUESTED } from "../events/coreEvents.js";
|
|
26
|
+
import { getReplicationNetworkFailure } from "./remoteTransportFailure.js";
|
|
24
27
|
import { ContentReadError } from "./contentRead.js";
|
|
25
28
|
import {
|
|
26
29
|
FILE_REFLECTION_COMPLETED,
|
|
@@ -70,6 +73,7 @@ export {
|
|
|
70
73
|
CENTRAL_REMOTE_REPLICATION_READINESS,
|
|
71
74
|
ContentReadError,
|
|
72
75
|
ENCRYPTED_INTERNAL_METADATA_FEATURE,
|
|
76
|
+
EVENT_REPLICATION_ABORT_REQUESTED,
|
|
73
77
|
FILE_REFLECTION_CANCELLED,
|
|
74
78
|
FILE_REFLECTION_COMPLETED,
|
|
75
79
|
FILE_REFLECTION_POLICY,
|
|
@@ -92,6 +96,7 @@ export {
|
|
|
92
96
|
centralRemoteAdministrationVerified,
|
|
93
97
|
defineReplicatorProviderDefinitions,
|
|
94
98
|
describeRemoteFeatureRejection,
|
|
99
|
+
getReplicationNetworkFailure,
|
|
95
100
|
inspectDatabasePathIdentity,
|
|
96
101
|
isCentralRemoteAdministrationVerified,
|
|
97
102
|
isReflectionCancelled,
|
|
@@ -99,6 +104,7 @@ export {
|
|
|
99
104
|
isReplicationCompleted,
|
|
100
105
|
isUnencryptedContentRejectedError,
|
|
101
106
|
milestoneSatisfiesCentralRemoteAdministration,
|
|
107
|
+
observedOfflineReplicationFailure,
|
|
102
108
|
outcomeFromContinuousOpenReplication,
|
|
103
109
|
outcomeFromFiniteOpenReplication,
|
|
104
110
|
replicationBlocked,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { RemoteRequestError } from "../common/LSError.js";
|
|
2
|
+
import type { ContinuousReplicationNetworkFailure } from "./ReplicatorProvider.js";
|
|
3
|
+
export declare class OneShotConnectivityPreflightTimeoutError extends Error {
|
|
4
|
+
constructor(timeoutMs: number);
|
|
5
|
+
}
|
|
6
|
+
declare class ObservedReplicationNetworkFailure {
|
|
7
|
+
readonly observation: ContinuousReplicationNetworkFailure;
|
|
8
|
+
constructor(observation: ContinuousReplicationNetworkFailure);
|
|
9
|
+
}
|
|
10
|
+
/** Retrieve the observation made at an offline gate or a failed remote request. */
|
|
11
|
+
export declare function getReplicationNetworkFailure(error: unknown): ContinuousReplicationNetworkFailure | undefined;
|
|
12
|
+
/** PouchDB copies own string-keyed properties into its errors and drops the original prototype. */
|
|
13
|
+
export declare class ReplicationNetworkError extends RemoteRequestError {
|
|
14
|
+
readonly replicationNetworkFailure: ObservedReplicationNetworkFailure;
|
|
15
|
+
constructor(message: string, failure: ContinuousReplicationNetworkFailure, options?: {
|
|
16
|
+
cause: unknown;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Preserve a positively identified network failure at the final fetch boundary.
|
|
21
|
+
* PouchDB copies own error properties when emitting a finite transfer failure.
|
|
22
|
+
*/
|
|
23
|
+
export declare function captureRemoteTransportFailure(error: unknown, signal: AbortSignal, isOnline: () => boolean): unknown;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { RemoteRequestError } from "../common/LSError.js";
|
|
2
|
+
class OneShotConnectivityPreflightTimeoutError extends Error {
|
|
3
|
+
constructor(timeoutMs) {
|
|
4
|
+
super(`Remote connectivity preflight exceeded ${timeoutMs} ms.`);
|
|
5
|
+
this.name = "OneShotConnectivityPreflightTimeoutError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
const disconnectedTransportCodes = /* @__PURE__ */ new Set([
|
|
9
|
+
"ECONNABORTED",
|
|
10
|
+
"ECONNREFUSED",
|
|
11
|
+
"ECONNRESET",
|
|
12
|
+
"EHOSTUNREACH",
|
|
13
|
+
"ENETDOWN",
|
|
14
|
+
"ENETRESET",
|
|
15
|
+
"ENETUNREACH",
|
|
16
|
+
"EPIPE",
|
|
17
|
+
"ETIMEDOUT",
|
|
18
|
+
"EAI_AGAIN",
|
|
19
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
20
|
+
"UND_ERR_HEADERS_TIMEOUT",
|
|
21
|
+
"UND_ERR_BODY_TIMEOUT",
|
|
22
|
+
"UND_ERR_SOCKET"
|
|
23
|
+
]);
|
|
24
|
+
class ObservedReplicationNetworkFailure {
|
|
25
|
+
constructor(observation) {
|
|
26
|
+
this.observation = observation;
|
|
27
|
+
}
|
|
28
|
+
observation;
|
|
29
|
+
}
|
|
30
|
+
function* errorChain(error) {
|
|
31
|
+
const seen = /* @__PURE__ */ new Set();
|
|
32
|
+
while (typeof error === "object" && error !== null && !seen.has(error)) {
|
|
33
|
+
seen.add(error);
|
|
34
|
+
const current = error;
|
|
35
|
+
yield current;
|
|
36
|
+
error = current.cause;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function getReplicationNetworkFailure(error) {
|
|
40
|
+
for (const cause of errorChain(error)) {
|
|
41
|
+
const failure = cause.replicationNetworkFailure;
|
|
42
|
+
if (failure instanceof ObservedReplicationNetworkFailure) return failure.observation;
|
|
43
|
+
}
|
|
44
|
+
return void 0;
|
|
45
|
+
}
|
|
46
|
+
class ReplicationNetworkError extends RemoteRequestError {
|
|
47
|
+
replicationNetworkFailure;
|
|
48
|
+
constructor(message, failure, options) {
|
|
49
|
+
super(message, options);
|
|
50
|
+
this.replicationNetworkFailure = new ObservedReplicationNetworkFailure(failure);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function captureRemoteTransportFailure(error, signal, isOnline) {
|
|
54
|
+
if (getReplicationNetworkFailure(error)) return error;
|
|
55
|
+
let disconnected = false;
|
|
56
|
+
if (signal.aborted) {
|
|
57
|
+
disconnected = signal.reason instanceof OneShotConnectivityPreflightTimeoutError || signal.reason?.name === "TimeoutError";
|
|
58
|
+
} else {
|
|
59
|
+
for (const cause of errorChain(error)) {
|
|
60
|
+
if (cause.name === "AbortError" || typeof cause.status === "number" && cause.status >= 400 && cause.status < 600) {
|
|
61
|
+
return error;
|
|
62
|
+
}
|
|
63
|
+
if (cause.name === "TimeoutError" || typeof cause.code === "string" && disconnectedTransportCodes.has(cause.code)) {
|
|
64
|
+
disconnected = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (!disconnected) return error;
|
|
69
|
+
return new ReplicationNetworkError(
|
|
70
|
+
error instanceof Error ? error.message : "Remote request failed",
|
|
71
|
+
{ reason: "remote-transport", hostOnlineAtFailure: isOnline(), failedAt: Date.now() },
|
|
72
|
+
{ cause: error }
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
export {
|
|
76
|
+
OneShotConnectivityPreflightTimeoutError,
|
|
77
|
+
ReplicationNetworkError,
|
|
78
|
+
captureRemoteTransportFailure,
|
|
79
|
+
getReplicationNetworkFailure
|
|
80
|
+
};
|
|
@@ -4,7 +4,7 @@ import { type ReplicatorHostEnv, type FullFilledDeviceInfo, type Request, type R
|
|
|
4
4
|
import { StoredMapLike } from "../../dataobject/StoredMap.js";
|
|
5
5
|
import { TrysteroReplicatorP2PClient } from "./TrysteroReplicatorP2PClient.js";
|
|
6
6
|
import { Computed } from "octagonal-wheels/dataobject/Computed";
|
|
7
|
-
import {
|
|
7
|
+
import type { JsonLike, RpcRequestContext, RpcRoom } from "../../rpc/index.js";
|
|
8
8
|
import { type DiagRTCStats } from "../../rpc/transports/DiagRTCPeerConnections.types.js";
|
|
9
9
|
import type { P2PPeerAcceptance } from "./replicationTypes.js";
|
|
10
10
|
export type { P2PPeerAcceptance } from "./replicationTypes.js";
|
|
@@ -11,10 +11,10 @@ import {
|
|
|
11
11
|
} from "./types.js";
|
|
12
12
|
import { StoredMapLike } from "../../dataobject/StoredMap.js";
|
|
13
13
|
import { TrysteroReplicatorP2PClient } from "./TrysteroReplicatorP2PClient.js";
|
|
14
|
+
import { TrysteroRpcRoom } from "./TrysteroRpcRoom.js";
|
|
14
15
|
import { createHostingDB } from "./ProxiedDB.js";
|
|
15
16
|
import { EVENT_PLATFORM_UNLOADED } from "../../events/coreEvents.js";
|
|
16
17
|
import { Computed } from "octagonal-wheels/dataobject/Computed";
|
|
17
|
-
import { RpcRoom } from "../../rpc/index.js";
|
|
18
18
|
import { resolveTrysteroRpcOptions } from "../../rpc/transports/TrysteroTransport.js";
|
|
19
19
|
import { subscribeTrysteroPeerEvents } from "../../rpc/transports/trysteroRoomEvents.js";
|
|
20
20
|
import { toRpcMethodName } from "./rpcCompat.js";
|
|
@@ -353,7 +353,7 @@ class TrysteroReplicatorP2PServer {
|
|
|
353
353
|
}
|
|
354
354
|
};
|
|
355
355
|
this._rpcRoom?.close();
|
|
356
|
-
this._rpcRoom = new
|
|
356
|
+
this._rpcRoom = new TrysteroRpcRoom({
|
|
357
357
|
...resolveTrysteroRpcOptions(this.settings),
|
|
358
358
|
transport,
|
|
359
359
|
canAcceptRequest: async (peerId, method) => {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RpcRoom, type JsonLike, type RpcCallOptions, type RpcCancellationAwareMethodHandler, type RpcSession as FancyKitRpcSession, type RpcMethodHandler, type RpcRegisterOptions, type RpcRoomOptions } from "@vrtmrz/rpc/p2p";
|
|
2
|
+
import { commonlibRpcRoom } from "../../rpc/commonlibRpcRoom.js";
|
|
3
|
+
import { RpcSession } from "../../rpc/RpcSession.js";
|
|
4
|
+
/** Adapts Fancy Kit's text diagnostics while retaining its wire responses. */
|
|
5
|
+
export declare class TrysteroRpcRoom extends RpcRoom<AbortSignal> {
|
|
6
|
+
readonly [commonlibRpcRoom] = true;
|
|
7
|
+
private readonly commonlibSessions;
|
|
8
|
+
constructor(options: Omit<RpcRoomOptions<AbortSignal>, "codec">);
|
|
9
|
+
session(peerId: string): RpcSession & FancyKitRpcSession;
|
|
10
|
+
invoke(peerId: string, method: string, args: JsonLike[], options?: number | RpcCallOptions): Promise<JsonLike>;
|
|
11
|
+
notify(peerId: string, method: string, args: JsonLike[]): Promise<void>;
|
|
12
|
+
register<Args extends JsonLike[], Result>(method: string, handler: RpcMethodHandler<Args, Result>, options?: RpcRegisterOptions): void;
|
|
13
|
+
registerCancellable<Args extends JsonLike[], Result>(method: string, handler: RpcCancellationAwareMethodHandler<Args, Result, AbortSignal>, options?: RpcRegisterOptions): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createJsonCodec,
|
|
3
|
+
RpcRoom
|
|
4
|
+
} from "@vrtmrz/rpc/p2p";
|
|
5
|
+
import { commonlibRpcRoom } from "../../rpc/commonlibRpcRoom.js";
|
|
6
|
+
import { asRpcErrorShape, RpcError, toCommonlibRpcError } from "../../rpc/errors.js";
|
|
7
|
+
import { RpcSession } from "../../rpc/RpcSession.js";
|
|
8
|
+
import { formatRpcProtocolWarning } from "./rpcCompat.js";
|
|
9
|
+
const responseErrors = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
class HostedRpcError extends RpcError {
|
|
11
|
+
constructor(originalError) {
|
|
12
|
+
super("REMOTE_ERROR", "Commonlib RPC callback failed");
|
|
13
|
+
this.originalError = originalError;
|
|
14
|
+
}
|
|
15
|
+
originalError;
|
|
16
|
+
toShape() {
|
|
17
|
+
const safe = super.toShape();
|
|
18
|
+
responseErrors.set(safe, this.originalError);
|
|
19
|
+
return safe;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function redactCallbackErrors(handler) {
|
|
23
|
+
return async (...args) => {
|
|
24
|
+
try {
|
|
25
|
+
return await handler(...args);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
throw new HostedRpcError(error);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function getResponseError(safe) {
|
|
32
|
+
try {
|
|
33
|
+
const { code, message, details } = asRpcErrorShape(responseErrors.get(safe));
|
|
34
|
+
return { code, message, details };
|
|
35
|
+
} catch {
|
|
36
|
+
return safe;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
class TrysteroRpcRoom extends RpcRoom {
|
|
40
|
+
[commonlibRpcRoom] = true;
|
|
41
|
+
// Follow the underlying cache's lifetime, including peer departure and room closure.
|
|
42
|
+
commonlibSessions = /* @__PURE__ */ new WeakMap();
|
|
43
|
+
constructor(options) {
|
|
44
|
+
const codec = createJsonCodec();
|
|
45
|
+
const {
|
|
46
|
+
canAcceptRequest,
|
|
47
|
+
onProtocolWarning,
|
|
48
|
+
createAbortController = () => {
|
|
49
|
+
if (typeof AbortController === "undefined") {
|
|
50
|
+
throw new RpcError("PROTOCOL_ERROR", "Supply createAbortController in this host");
|
|
51
|
+
}
|
|
52
|
+
return new AbortController();
|
|
53
|
+
}
|
|
54
|
+
} = options;
|
|
55
|
+
let notificationMethod;
|
|
56
|
+
let minorMismatch;
|
|
57
|
+
const emitWarning = async (message, peerId) => {
|
|
58
|
+
try {
|
|
59
|
+
await onProtocolWarning?.(message, peerId);
|
|
60
|
+
} catch {
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
super({
|
|
64
|
+
...options,
|
|
65
|
+
createAbortController() {
|
|
66
|
+
const controller = createAbortController();
|
|
67
|
+
return {
|
|
68
|
+
signal: controller.signal,
|
|
69
|
+
abort(reason) {
|
|
70
|
+
controller.abort(toCommonlibRpcError(reason));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
canAcceptRequest: canAcceptRequest && redactCallbackErrors(canAcceptRequest),
|
|
75
|
+
onProtocolWarning(message, peerId) {
|
|
76
|
+
if (message.startsWith("RPC minor mismatch: ")) {
|
|
77
|
+
const warning = { message, peerId };
|
|
78
|
+
minorMismatch = warning;
|
|
79
|
+
queueMicrotask(() => {
|
|
80
|
+
if (minorMismatch === warning) {
|
|
81
|
+
minorMismatch = void 0;
|
|
82
|
+
void emitWarning(message, peerId);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
return onProtocolWarning?.(formatRpcProtocolWarning(message, notificationMethod), peerId);
|
|
88
|
+
},
|
|
89
|
+
codec: {
|
|
90
|
+
decode(payload) {
|
|
91
|
+
const envelope = codec.decode(payload);
|
|
92
|
+
if (envelope.kind !== "request") {
|
|
93
|
+
return envelope;
|
|
94
|
+
}
|
|
95
|
+
const { method, expectsResponse } = envelope;
|
|
96
|
+
return {
|
|
97
|
+
...envelope,
|
|
98
|
+
get method() {
|
|
99
|
+
notificationMethod = expectsResponse === false ? method : void 0;
|
|
100
|
+
if (minorMismatch) {
|
|
101
|
+
const { message, peerId } = minorMismatch;
|
|
102
|
+
minorMismatch = void 0;
|
|
103
|
+
void emitWarning(
|
|
104
|
+
expectsResponse === false ? `RPC notification ${method}: protocol minor version mismatch` : message,
|
|
105
|
+
peerId
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return method;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
encode(envelope) {
|
|
113
|
+
if (envelope.kind !== "response" || envelope.ok !== false) {
|
|
114
|
+
return codec.encode(envelope);
|
|
115
|
+
}
|
|
116
|
+
if (!responseErrors.has(envelope.error)) {
|
|
117
|
+
return codec.encode(envelope);
|
|
118
|
+
}
|
|
119
|
+
return codec.encode({ ...envelope, error: getResponseError(envelope.error) });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
session(peerId) {
|
|
125
|
+
try {
|
|
126
|
+
const underlying = super.session(peerId);
|
|
127
|
+
let session = this.commonlibSessions.get(underlying);
|
|
128
|
+
if (!session) {
|
|
129
|
+
session = new RpcSession(this, peerId);
|
|
130
|
+
this.commonlibSessions.set(underlying, session);
|
|
131
|
+
}
|
|
132
|
+
return session;
|
|
133
|
+
} catch (error) {
|
|
134
|
+
throw toCommonlibRpcError(error);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async invoke(peerId, method, args, options) {
|
|
138
|
+
try {
|
|
139
|
+
return await super.invoke(peerId, method, args, options);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
throw toCommonlibRpcError(error);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async notify(peerId, method, args) {
|
|
145
|
+
try {
|
|
146
|
+
await super.notify(peerId, method, args);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
throw toCommonlibRpcError(error);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
register(method, handler, options) {
|
|
152
|
+
try {
|
|
153
|
+
super.register(method, redactCallbackErrors(handler), options);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
throw toCommonlibRpcError(error);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
registerCancellable(method, handler, options) {
|
|
159
|
+
try {
|
|
160
|
+
super.registerCancellable(method, redactCallbackErrors(handler), options);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
throw toCommonlibRpcError(error);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
export {
|
|
167
|
+
TrysteroRpcRoom
|
|
168
|
+
};
|
|
@@ -1,9 +1,24 @@
|
|
|
1
1
|
const DB_METHODS = /* @__PURE__ */ new Set(["info", "changes", "revsDiff", "bulkDocs", "bulkGet", "put", "get"]);
|
|
2
|
+
const NOTIFICATION_PREFIX = "RPC notification ";
|
|
2
3
|
function toRpcMethodName(method) {
|
|
3
4
|
if (method.includes(".") || method.includes("/")) return method;
|
|
4
5
|
if (DB_METHODS.has(method)) return `db.${method}`;
|
|
5
6
|
return `legacy/${method}`;
|
|
6
7
|
}
|
|
8
|
+
function formatRpcProtocolWarning(message, notificationMethod) {
|
|
9
|
+
if (message.startsWith("Invalid payload:")) {
|
|
10
|
+
return "Invalid payload";
|
|
11
|
+
}
|
|
12
|
+
if (message.startsWith(NOTIFICATION_PREFIX)) {
|
|
13
|
+
const methodPrefix = `${NOTIFICATION_PREFIX}${notificationMethod}: `;
|
|
14
|
+
if (notificationMethod !== void 0 && message.startsWith(methodPrefix)) {
|
|
15
|
+
return `${methodPrefix}failed`;
|
|
16
|
+
}
|
|
17
|
+
return "RPC notification failed";
|
|
18
|
+
}
|
|
19
|
+
return message;
|
|
20
|
+
}
|
|
7
21
|
export {
|
|
22
|
+
formatRpcProtocolWarning,
|
|
8
23
|
toRpcMethodName
|
|
9
24
|
};
|
package/dist/rpc/RpcRoom.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
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
|
+
static [Symbol.hasInstance](value: unknown): boolean;
|
|
4
5
|
private options;
|
|
5
6
|
private pendingInvocations;
|
|
6
7
|
private inboundCalls;
|
package/dist/rpc/RpcRoom.js
CHANGED
|
@@ -4,7 +4,8 @@ import {
|
|
|
4
4
|
} from "octagonal-wheels/concurrency/deferredAction";
|
|
5
5
|
import { IncomingChunkBuffer, estimateBytes, splitIntoChunks } from "./chunking.js";
|
|
6
6
|
import { createPendingInvocations } from "./RpcRoom/pendingInvocations.js";
|
|
7
|
-
import { asRpcErrorShape, RpcError } from "./errors.js";
|
|
7
|
+
import { asRpcErrorShape, RpcError, toCommonlibRpcError } from "./errors.js";
|
|
8
|
+
import { commonlibRpcRoom } from "./commonlibRpcRoom.js";
|
|
8
9
|
import { RpcSession } from "./RpcSession.js";
|
|
9
10
|
import {
|
|
10
11
|
RPC_VERSION_MAJOR,
|
|
@@ -21,6 +22,9 @@ const scheduleRpcTimeout = (callback, delayMs) => {
|
|
|
21
22
|
return () => compatGlobal.clearTimeout(handle);
|
|
22
23
|
};
|
|
23
24
|
class RpcRoom {
|
|
25
|
+
static [Symbol.hasInstance](value) {
|
|
26
|
+
return Function.prototype[Symbol.hasInstance].call(this, value) || this === RpcRoom && typeof value === "object" && value !== null && commonlibRpcRoom in value && value[commonlibRpcRoom] === true;
|
|
27
|
+
}
|
|
24
28
|
options;
|
|
25
29
|
pendingInvocations;
|
|
26
30
|
inboundCalls = /* @__PURE__ */ new Map();
|
|
@@ -126,7 +130,11 @@ class RpcRoom {
|
|
|
126
130
|
if (this.closed) {
|
|
127
131
|
throw new RpcError("NOT_CONNECTED", "Room is closed");
|
|
128
132
|
}
|
|
129
|
-
|
|
133
|
+
try {
|
|
134
|
+
return await this.pendingInvocations.invoke(peerId, method, args, timeoutOrOptions);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
throw toCommonlibRpcError(error);
|
|
137
|
+
}
|
|
130
138
|
}
|
|
131
139
|
/**
|
|
132
140
|
* Send a notification to a peer without creating an RPC response wait.
|
|
@@ -140,13 +148,17 @@ class RpcRoom {
|
|
|
140
148
|
if (this.closed) {
|
|
141
149
|
throw new RpcError("NOT_CONNECTED", "Room is closed");
|
|
142
150
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
151
|
+
try {
|
|
152
|
+
await this.sendEnvelope(peerId, {
|
|
153
|
+
kind: "request",
|
|
154
|
+
requestId: newId("req"),
|
|
155
|
+
method,
|
|
156
|
+
args,
|
|
157
|
+
expectsResponse: false
|
|
158
|
+
});
|
|
159
|
+
} catch (error) {
|
|
160
|
+
throw toCommonlibRpcError(error);
|
|
161
|
+
}
|
|
150
162
|
}
|
|
151
163
|
async cancel(peerId, requestId) {
|
|
152
164
|
return await this.pendingInvocations.cancel(peerId, requestId);
|
package/dist/rpc/RpcSession.d.ts
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { RpcSession as FancyKitRpcSession } from "@vrtmrz/rpc/p2p";
|
|
2
2
|
import type { RpcRoom } from "./RpcRoom.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
private room;
|
|
6
|
-
constructor(room: RpcRoom, peerId: string);
|
|
3
|
+
import type { JsonLike, RpcCallOptions } from "./types.js";
|
|
4
|
+
export interface RpcSession extends Pick<FancyKitRpcSession, "peerId" | "notify" | "createProxy"> {
|
|
7
5
|
call<T = JsonLike>(method: string, args?: JsonLike[], timeoutMs?: number): Promise<T>;
|
|
8
6
|
call<T = JsonLike>(method: string, args?: JsonLike[], options?: RpcCallOptions): Promise<T>;
|
|
9
|
-
call<T = JsonLike>(method: string, args: JsonLike[] | undefined,
|
|
10
|
-
/**
|
|
11
|
-
* Send a notification to the bound peer without creating an RPC response
|
|
12
|
-
* wait. Resolves when the local transport send completes, not when remote
|
|
13
|
-
* processing finishes.
|
|
14
|
-
*/
|
|
15
|
-
notify(method: string, args?: JsonLike[]): Promise<void>;
|
|
16
|
-
createProxy<T extends object>(namespace: string): T;
|
|
7
|
+
call<T = JsonLike>(method: string, args: JsonLike[] | undefined, options?: number | RpcCallOptions): Promise<T>;
|
|
17
8
|
}
|
|
9
|
+
export declare const RpcSession: {
|
|
10
|
+
new (room: Pick<RpcRoom, "invoke" | "notify">, peerId: string): RpcSession & FancyKitRpcSession;
|
|
11
|
+
readonly prototype: RpcSession;
|
|
12
|
+
};
|
package/dist/rpc/RpcSession.js
CHANGED
|
@@ -1,43 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
async call(method, args = [], timeoutOrOptions) {
|
|
10
|
-
if (!this.peerId) {
|
|
11
|
-
throw new RpcError("NOT_CONNECTED", "Peer is not connected");
|
|
1
|
+
import { RpcSession as FancyKitRpcSession } from "@vrtmrz/rpc/p2p";
|
|
2
|
+
import { toCommonlibRpcError } from "./errors.js";
|
|
3
|
+
class CommonlibRpcSession extends FancyKitRpcSession {
|
|
4
|
+
async call(method, args, options) {
|
|
5
|
+
try {
|
|
6
|
+
return await super.call(method, args, options);
|
|
7
|
+
} catch (error) {
|
|
8
|
+
throw toCommonlibRpcError(error);
|
|
12
9
|
}
|
|
13
|
-
return await this.room.invoke(this.peerId, method, args, timeoutOrOptions);
|
|
14
10
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
async notify(method, args = []) {
|
|
21
|
-
if (!this.peerId) {
|
|
22
|
-
throw new RpcError("NOT_CONNECTED", "Peer is not connected");
|
|
11
|
+
async notify(method, args) {
|
|
12
|
+
try {
|
|
13
|
+
await super.notify(method, args);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
throw toCommonlibRpcError(error);
|
|
23
16
|
}
|
|
24
|
-
await this.room.notify(this.peerId, method, args);
|
|
25
|
-
}
|
|
26
|
-
createProxy(namespace) {
|
|
27
|
-
const session = this;
|
|
28
|
-
return new Proxy(
|
|
29
|
-
{},
|
|
30
|
-
{
|
|
31
|
-
get(_target, propKey) {
|
|
32
|
-
if (typeof propKey !== "string") return void 0;
|
|
33
|
-
return async (...args) => {
|
|
34
|
-
return await session.call(`${namespace}.${propKey}`, args);
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
);
|
|
39
17
|
}
|
|
40
18
|
}
|
|
19
|
+
const RpcSession = CommonlibRpcSession;
|
|
41
20
|
export {
|
|
42
21
|
RpcSession
|
|
43
22
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const commonlibRpcRoom: unique symbol;
|
package/dist/rpc/errors.d.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { JsonLike, RpcErrorCode, RpcErrorShape } from "./types.js";
|
|
2
|
-
|
|
2
|
+
/** The Commonlib error contract used by both RPC implementations. */
|
|
3
|
+
export interface RpcError extends Error {
|
|
3
4
|
code: RpcErrorCode;
|
|
4
5
|
details?: JsonLike;
|
|
5
|
-
constructor(code: RpcErrorCode, message: string, details?: JsonLike);
|
|
6
6
|
toShape(): RpcErrorShape;
|
|
7
7
|
}
|
|
8
|
-
export declare
|
|
8
|
+
export declare const RpcError: {
|
|
9
|
+
new (code: RpcErrorCode, message: string, details?: JsonLike): RpcError;
|
|
10
|
+
readonly prototype: RpcError;
|
|
11
|
+
};
|
|
12
|
+
export declare function toCommonlibRpcError(error: unknown): unknown;
|
|
13
|
+
/** Return the structured error through Commonlib's JSON-compatible type. */
|
|
14
|
+
export declare function asRpcErrorShape(error: unknown): RpcErrorShape;
|
package/dist/rpc/errors.js
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
details;
|
|
1
|
+
import { asRpcErrorShape as asP2pRpcErrorShape, RpcError as P2pRpcError } from "@vrtmrz/rpc/p2p";
|
|
2
|
+
const RpcError = class RpcError2 extends P2pRpcError {
|
|
4
3
|
constructor(code, message, details) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.name = "RpcError";
|
|
7
|
-
this.code = code;
|
|
8
|
-
this.details = details;
|
|
4
|
+
super(code, message, details);
|
|
9
5
|
}
|
|
10
6
|
toShape() {
|
|
11
7
|
return { code: this.code, message: this.message, details: this.details };
|
|
12
8
|
}
|
|
13
|
-
}
|
|
14
|
-
function
|
|
15
|
-
if (
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
if (ex instanceof Error) {
|
|
19
|
-
return { code: "REMOTE_ERROR", message: ex.message };
|
|
9
|
+
};
|
|
10
|
+
function toCommonlibRpcError(error) {
|
|
11
|
+
if (error instanceof P2pRpcError && !(error instanceof RpcError)) {
|
|
12
|
+
return new RpcError(error.code, error.message, error.details);
|
|
20
13
|
}
|
|
21
|
-
return
|
|
14
|
+
return error;
|
|
15
|
+
}
|
|
16
|
+
function asRpcErrorShape(error) {
|
|
17
|
+
return asP2pRpcErrorShape(toCommonlibRpcError(error));
|
|
22
18
|
}
|
|
23
19
|
export {
|
|
24
20
|
RpcError,
|
|
25
|
-
asRpcErrorShape
|
|
21
|
+
asRpcErrorShape,
|
|
22
|
+
toCommonlibRpcError
|
|
26
23
|
};
|
package/dist/rpc/index.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import { RpcRoom as StandaloneRpcRoom } from "./RpcRoom.js";
|
|
2
|
+
import type { RpcSession } from "./RpcSession.js";
|
|
3
|
+
/** Public room operations shared by the standalone and hosted rooms. */
|
|
4
|
+
export type RpcRoom = Omit<StandaloneRpcRoom, "session"> & {
|
|
5
|
+
session(peerId: string): RpcSession;
|
|
6
|
+
};
|
|
7
|
+
export declare const RpcRoom: {
|
|
8
|
+
new (...args: ConstructorParameters<typeof StandaloneRpcRoom>): RpcRoom;
|
|
9
|
+
readonly prototype: RpcRoom;
|
|
10
|
+
};
|
|
2
11
|
export { RpcSession } from "./RpcSession.js";
|
|
3
12
|
export { RpcError } from "./errors.js";
|
|
4
13
|
export { exposeDB } from "./pouchdb/RpcPouchDBServer.js";
|
package/dist/rpc/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { RpcRoom } from "./RpcRoom.js";
|
|
1
|
+
import { RpcRoom as StandaloneRpcRoom } from "./RpcRoom.js";
|
|
2
|
+
const RpcRoom = StandaloneRpcRoom;
|
|
2
3
|
import { RpcSession } from "./RpcSession.js";
|
|
3
4
|
import { RpcError } from "./errors.js";
|
|
4
5
|
import { exposeDB } from "./pouchdb/RpcPouchDBServer.js";
|