@abraca/dabra 2.90.0 → 2.92.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/package.json +5 -11
- package/src/AbracadabraBaseProvider.ts +0 -963
- package/src/AbracadabraClient.ts +0 -1960
- package/src/AbracadabraProvider.ts +0 -915
- package/src/AbracadabraWS.ts +0 -695
- package/src/BackgroundSyncManager.ts +0 -856
- package/src/BackgroundSyncPersistence.ts +0 -140
- package/src/ChatClient.ts +0 -314
- package/src/CloseEvents.ts +0 -49
- package/src/ContentManager.ts +0 -246
- package/src/CoverReconcile.ts +0 -168
- package/src/CryptoIdentityKeystore.ts +0 -754
- package/src/DeviceRegistrationService.ts +0 -257
- package/src/DocConverters.ts +0 -1859
- package/src/DocKeyManager.ts +0 -163
- package/src/DocTypes.ts +0 -628
- package/src/DocUtils.ts +0 -251
- package/src/DocumentCache.ts +0 -210
- package/src/DocumentManager.ts +0 -410
- package/src/E2EAbracadabraProvider.ts +0 -426
- package/src/E2EOfflineStore.ts +0 -55
- package/src/EncryptedChatClient.ts +0 -173
- package/src/EncryptedY.ts +0 -145
- package/src/EventEmitter.ts +0 -59
- package/src/FileBlobStore.ts +0 -569
- package/src/IdentityDoc.ts +0 -711
- package/src/IncomingMessage.ts +0 -63
- package/src/MessageReceiver.ts +0 -151
- package/src/MessageSender.ts +0 -22
- package/src/MetaManager.ts +0 -194
- package/src/MnemonicKeyDerivation.ts +0 -171
- package/src/NodeConnect.ts +0 -63
- package/src/NodeIdentity.ts +0 -378
- package/src/NotificationsClient.ts +0 -207
- package/src/OfflineStore.ts +0 -294
- package/src/OutgoingMessage.ts +0 -25
- package/src/OutgoingMessages/AuthenticationMessage.ts +0 -23
- package/src/OutgoingMessages/AwarenessMessage.ts +0 -41
- package/src/OutgoingMessages/CloseMessage.ts +0 -17
- package/src/OutgoingMessages/QueryAwarenessMessage.ts +0 -17
- package/src/OutgoingMessages/StatelessMessage.ts +0 -18
- package/src/OutgoingMessages/SubdocMessage.ts +0 -35
- package/src/OutgoingMessages/SyncStepOneMessage.ts +0 -25
- package/src/OutgoingMessages/SyncStepTwoMessage.ts +0 -25
- package/src/OutgoingMessages/UpdateMessage.ts +0 -20
- package/src/QueryClient.ts +0 -209
- package/src/RpcClient.ts +0 -659
- package/src/SchemaTypes.ts +0 -179
- package/src/SearchIndex.ts +0 -250
- package/src/ServerRegistry.ts +0 -220
- package/src/ServerReset.ts +0 -222
- package/src/TokenManager.ts +0 -337
- package/src/TreeManager.ts +0 -894
- package/src/TreeTimestamps.ts +0 -101
- package/src/auth.ts +0 -62
- package/src/awarenessStatesToArray.ts +0 -10
- package/src/index.ts +0 -219
- package/src/messageRecord.ts +0 -121
- package/src/sync/BroadcastChannelSync.ts +0 -235
- package/src/types.ts +0 -790
- package/src/webrtc/AbracadabraWebRTC.ts +0 -683
- package/src/webrtc/DataChannelRouter.ts +0 -179
- package/src/webrtc/DevicePairingChannel.ts +0 -496
- package/src/webrtc/E2EEChannel.ts +0 -195
- package/src/webrtc/FileTransferChannel.ts +0 -367
- package/src/webrtc/ManualSignaling.ts +0 -204
- package/src/webrtc/PeerConnection.ts +0 -133
- package/src/webrtc/SignalingSocket.ts +0 -378
- package/src/webrtc/YjsDataChannel.ts +0 -203
- package/src/webrtc/index.ts +0 -26
- package/src/webrtc/types.ts +0 -177
package/src/IncomingMessage.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import type { Decoder } from "lib0/decoding";
|
|
2
|
-
import {
|
|
3
|
-
createDecoder,
|
|
4
|
-
peekVarString,
|
|
5
|
-
readVarUint,
|
|
6
|
-
readVarUint8Array,
|
|
7
|
-
readVarString,
|
|
8
|
-
} from "lib0/decoding";
|
|
9
|
-
import type { Encoder } from "lib0/encoding";
|
|
10
|
-
import {
|
|
11
|
-
createEncoder,
|
|
12
|
-
writeVarUint,
|
|
13
|
-
writeVarUint8Array,
|
|
14
|
-
writeVarString,
|
|
15
|
-
length,
|
|
16
|
-
} from "lib0/encoding";
|
|
17
|
-
import type { MessageType } from "./types.ts";
|
|
18
|
-
|
|
19
|
-
export class IncomingMessage {
|
|
20
|
-
data: any;
|
|
21
|
-
|
|
22
|
-
encoder: Encoder;
|
|
23
|
-
|
|
24
|
-
decoder: Decoder;
|
|
25
|
-
|
|
26
|
-
constructor(data: any) {
|
|
27
|
-
this.data = data;
|
|
28
|
-
this.encoder = createEncoder();
|
|
29
|
-
this.decoder = createDecoder(new Uint8Array(this.data));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
peekVarString(): string {
|
|
33
|
-
return peekVarString(this.decoder);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
readVarUint(): MessageType {
|
|
37
|
-
return readVarUint(this.decoder);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
readVarString(): string {
|
|
41
|
-
return readVarString(this.decoder);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
readVarUint8Array() {
|
|
45
|
-
return readVarUint8Array(this.decoder);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
writeVarUint(type: MessageType) {
|
|
49
|
-
return writeVarUint(this.encoder, type);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
writeVarString(string: string) {
|
|
53
|
-
return writeVarString(this.encoder, string);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
writeVarUint8Array(data: Uint8Array) {
|
|
57
|
-
return writeVarUint8Array(this.encoder, data);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
length() {
|
|
61
|
-
return length(this.encoder);
|
|
62
|
-
}
|
|
63
|
-
}
|
package/src/MessageReceiver.ts
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { readAuthMessage } from "./auth.ts";
|
|
2
|
-
import { readVarInt, readVarString } from "lib0/decoding";
|
|
3
|
-
import type { CloseEvent } from "ws";
|
|
4
|
-
import * as awarenessProtocol from "y-protocols/awareness";
|
|
5
|
-
import { messageYjsSyncStep2, readSyncMessage } from "y-protocols/sync";
|
|
6
|
-
import type { AbracadabraBaseProvider } from "./AbracadabraBaseProvider.ts";
|
|
7
|
-
import type { IncomingMessage } from "./IncomingMessage.ts";
|
|
8
|
-
import { OutgoingMessage } from "./OutgoingMessage.ts";
|
|
9
|
-
import { MessageType } from "./types.ts";
|
|
10
|
-
|
|
11
|
-
export class MessageReceiver {
|
|
12
|
-
message: IncomingMessage;
|
|
13
|
-
|
|
14
|
-
constructor(message: IncomingMessage) {
|
|
15
|
-
this.message = message;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public apply(provider: AbracadabraBaseProvider, emitSynced: boolean) {
|
|
19
|
-
const { message } = this;
|
|
20
|
-
const type = message.readVarUint();
|
|
21
|
-
|
|
22
|
-
const emptyMessageLength = message.length();
|
|
23
|
-
|
|
24
|
-
switch (type) {
|
|
25
|
-
case MessageType.Sync:
|
|
26
|
-
this.applySyncMessage(provider, emitSynced);
|
|
27
|
-
break;
|
|
28
|
-
|
|
29
|
-
case MessageType.Awareness:
|
|
30
|
-
this.applyAwarenessMessage(provider);
|
|
31
|
-
break;
|
|
32
|
-
|
|
33
|
-
case MessageType.Auth:
|
|
34
|
-
this.applyAuthMessage(provider);
|
|
35
|
-
break;
|
|
36
|
-
|
|
37
|
-
case MessageType.QueryAwareness:
|
|
38
|
-
this.applyQueryAwarenessMessage(provider);
|
|
39
|
-
break;
|
|
40
|
-
|
|
41
|
-
case MessageType.Stateless:
|
|
42
|
-
provider.receiveStateless(readVarString(message.decoder));
|
|
43
|
-
break;
|
|
44
|
-
|
|
45
|
-
case MessageType.SyncStatus:
|
|
46
|
-
this.applySyncStatusMessage(provider, readVarInt(message.decoder) === 1);
|
|
47
|
-
break;
|
|
48
|
-
|
|
49
|
-
case MessageType.CLOSE:
|
|
50
|
-
// eslint-disable-next-line no-case-declarations
|
|
51
|
-
const event: CloseEvent = {
|
|
52
|
-
code: 1000,
|
|
53
|
-
reason: readVarString(message.decoder),
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
target: provider.configuration.websocketProvider.webSocket!,
|
|
56
|
-
type: "close",
|
|
57
|
-
};
|
|
58
|
-
provider.onClose();
|
|
59
|
-
provider.configuration.onClose({ event });
|
|
60
|
-
provider.forwardClose({ event });
|
|
61
|
-
break;
|
|
62
|
-
|
|
63
|
-
default:
|
|
64
|
-
throw new Error(`Can’t apply message of unknown type: ${type}`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Reply
|
|
68
|
-
if (message.length() > emptyMessageLength + 1) {
|
|
69
|
-
// length of documentName (considered in emptyMessageLength plus length of yjs sync type, set in applySyncMessage)
|
|
70
|
-
// @ts-ignore
|
|
71
|
-
provider.send(OutgoingMessage, { encoder: message.encoder });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
private applySyncMessage(provider: AbracadabraBaseProvider, emitSynced: boolean) {
|
|
76
|
-
const { message } = this;
|
|
77
|
-
|
|
78
|
-
message.writeVarUint(MessageType.Sync);
|
|
79
|
-
|
|
80
|
-
// Apply update
|
|
81
|
-
const syncMessageType = readSyncMessage(
|
|
82
|
-
message.decoder,
|
|
83
|
-
message.encoder,
|
|
84
|
-
provider.document,
|
|
85
|
-
provider,
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
// Synced once we receive Step2
|
|
89
|
-
if (emitSynced && syncMessageType === messageYjsSyncStep2) {
|
|
90
|
-
provider.synced = true;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
applySyncStatusMessage(provider: AbracadabraBaseProvider, applied: boolean) {
|
|
95
|
-
if (applied) {
|
|
96
|
-
provider.decrementUnsyncedChanges();
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// applied = 0 is the server saying "I did not take that update". It is
|
|
101
|
-
// sent for a write the caller's role isn't allowed to make and for one a
|
|
102
|
-
// server-side gate rejected (schema validation, size/pattern guards).
|
|
103
|
-
//
|
|
104
|
-
// This branch used to be empty. Not decrementing is correct — the change
|
|
105
|
-
// must stay queued rather than be counted as landed — but silence is not:
|
|
106
|
-
// nothing in the SDK, and therefore nothing in any app built on it, could
|
|
107
|
-
// tell that the edit sitting on screen had already been refused. That is
|
|
108
|
-
// the whole "it saved, and then it wasn't there after reload" class.
|
|
109
|
-
provider.reportWriteRejected(
|
|
110
|
-
"server did not apply the update (permission or validation)",
|
|
111
|
-
"validation",
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
private applyAwarenessMessage(provider: AbracadabraBaseProvider) {
|
|
116
|
-
if (!provider.awareness) return;
|
|
117
|
-
|
|
118
|
-
const { message } = this;
|
|
119
|
-
|
|
120
|
-
awarenessProtocol.applyAwarenessUpdate(
|
|
121
|
-
provider.awareness,
|
|
122
|
-
message.readVarUint8Array(),
|
|
123
|
-
provider,
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
private applyAuthMessage(provider: AbracadabraBaseProvider) {
|
|
128
|
-
const { message } = this;
|
|
129
|
-
|
|
130
|
-
readAuthMessage(
|
|
131
|
-
message.decoder,
|
|
132
|
-
provider.sendToken.bind(provider),
|
|
133
|
-
provider.permissionDeniedHandler.bind(provider),
|
|
134
|
-
provider.authenticatedHandler.bind(provider),
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
private applyQueryAwarenessMessage(provider: AbracadabraBaseProvider) {
|
|
139
|
-
if (!provider.awareness) return;
|
|
140
|
-
|
|
141
|
-
const { message } = this;
|
|
142
|
-
|
|
143
|
-
message.writeVarUint(MessageType.Awareness);
|
|
144
|
-
message.writeVarUint8Array(
|
|
145
|
-
awarenessProtocol.encodeAwarenessUpdate(
|
|
146
|
-
provider.awareness,
|
|
147
|
-
Array.from(provider.awareness.getStates().keys()),
|
|
148
|
-
),
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
}
|
package/src/MessageSender.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { Encoder } from "lib0/encoding";
|
|
2
|
-
import { toUint8Array } from "lib0/encoding";
|
|
3
|
-
import type { ConstructableOutgoingMessage } from "./types.ts";
|
|
4
|
-
|
|
5
|
-
export class MessageSender {
|
|
6
|
-
encoder: Encoder;
|
|
7
|
-
|
|
8
|
-
message: any;
|
|
9
|
-
|
|
10
|
-
constructor(Message: ConstructableOutgoingMessage, args: any = {}) {
|
|
11
|
-
this.message = new Message();
|
|
12
|
-
this.encoder = this.message.get(args);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
create() {
|
|
16
|
-
return toUint8Array(this.encoder);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
send(webSocket: any) {
|
|
20
|
-
webSocket?.send(this.create());
|
|
21
|
-
}
|
|
22
|
-
}
|
package/src/MetaManager.ts
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MetaManager — read/write PageMeta on tree entries.
|
|
3
|
-
*
|
|
4
|
-
* Extracted from `mcp/tools/meta.ts` and `cli/commands/meta.ts`.
|
|
5
|
-
*
|
|
6
|
-
* Optionally validates writes against a JSON-Schema-backed validator
|
|
7
|
-
* (e.g. `@abraca/schema`'s `SchemaRegistry`). The validator is supplied
|
|
8
|
-
* via {@link MetaManager.setSchema} as a structural value matching
|
|
9
|
-
* {@link SchemaValidatorLike}; the provider package never imports
|
|
10
|
-
* `@abraca/schema` directly so core stays schema-free
|
|
11
|
-
* (`feedback_schema_free_core` Rule 1). When no schema is set,
|
|
12
|
-
* `update`/`set` behave exactly as before (Rule 4).
|
|
13
|
-
*/
|
|
14
|
-
import type { PageMeta } from "./DocTypes.ts";
|
|
15
|
-
import { toPlain, patchEntry } from "./DocUtils.ts";
|
|
16
|
-
import type { DocumentManager } from "./DocumentManager.ts";
|
|
17
|
-
|
|
18
|
-
export interface DocumentMetaInfo {
|
|
19
|
-
id: string;
|
|
20
|
-
label: string;
|
|
21
|
-
type?: string;
|
|
22
|
-
meta: PageMeta;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Structural shape of a schema validator MetaManager can consult before
|
|
27
|
-
* writes. Compatible with `@abraca/schema`'s `SchemaRegistry`.
|
|
28
|
-
*
|
|
29
|
-
* Implementations MUST treat unknown doc-types as unconstrained
|
|
30
|
-
* (return `{ ok: true }`) — Rule 4: existing-app traffic for doc-types
|
|
31
|
-
* not in the validator's bundle is never rejected.
|
|
32
|
-
*/
|
|
33
|
-
export interface SchemaValidatorLike {
|
|
34
|
-
validateMeta(
|
|
35
|
-
docType: string,
|
|
36
|
-
meta: unknown,
|
|
37
|
-
): { ok: true; value?: unknown } | {
|
|
38
|
-
ok: false;
|
|
39
|
-
errors: ReadonlyArray<{
|
|
40
|
-
path: ReadonlyArray<PropertyKey>;
|
|
41
|
-
message: string;
|
|
42
|
-
code?: string;
|
|
43
|
-
}>;
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export class MetaValidationError extends Error {
|
|
48
|
-
readonly docId: string;
|
|
49
|
-
readonly docType: string;
|
|
50
|
-
readonly errors: ReadonlyArray<{
|
|
51
|
-
path: ReadonlyArray<PropertyKey>;
|
|
52
|
-
message: string;
|
|
53
|
-
code?: string;
|
|
54
|
-
}>;
|
|
55
|
-
|
|
56
|
-
constructor(
|
|
57
|
-
docId: string,
|
|
58
|
-
docType: string,
|
|
59
|
-
errors: ReadonlyArray<{
|
|
60
|
-
path: ReadonlyArray<PropertyKey>;
|
|
61
|
-
message: string;
|
|
62
|
-
code?: string;
|
|
63
|
-
}>,
|
|
64
|
-
) {
|
|
65
|
-
const detail = errors
|
|
66
|
-
.map((e) => `${e.path.join(".") || "(root)"}: ${e.message}`)
|
|
67
|
-
.join("; ");
|
|
68
|
-
super(
|
|
69
|
-
`Meta validation failed for document ${docId} of type "${docType}": ${detail}`,
|
|
70
|
-
);
|
|
71
|
-
this.name = "MetaValidationError";
|
|
72
|
-
this.docId = docId;
|
|
73
|
-
this.docType = docType;
|
|
74
|
-
this.errors = errors;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export class MetaManager {
|
|
79
|
-
private schema: SchemaValidatorLike | null = null;
|
|
80
|
-
|
|
81
|
-
constructor(private dm: DocumentManager) {}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Attach (or detach with `null`) a schema validator. When set, every
|
|
85
|
-
* `update` / `set` validates the post-write meta against the entry's
|
|
86
|
-
* declared `type` before writing to the doc-tree. Entries without a
|
|
87
|
-
* `type` field pass through unconditionally.
|
|
88
|
-
*/
|
|
89
|
-
setSchema(schema: SchemaValidatorLike | null): void {
|
|
90
|
-
this.schema = schema;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** Read metadata for a document. Returns null if not found. */
|
|
94
|
-
get(docId: string): DocumentMetaInfo | null {
|
|
95
|
-
const treeMap = this.dm.getTreeMap();
|
|
96
|
-
if (!treeMap) return null;
|
|
97
|
-
|
|
98
|
-
const raw = treeMap.get(docId);
|
|
99
|
-
if (!raw) return null;
|
|
100
|
-
|
|
101
|
-
const entry = toPlain(raw) as Record<string, unknown>;
|
|
102
|
-
return {
|
|
103
|
-
id: docId,
|
|
104
|
-
label: (entry.label as string) || "Untitled",
|
|
105
|
-
type: entry.type as string | undefined,
|
|
106
|
-
meta: (entry.meta as PageMeta) ?? {},
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Merge fields into a document's metadata.
|
|
112
|
-
* Existing keys not in the update are preserved.
|
|
113
|
-
*
|
|
114
|
-
* @throws {MetaValidationError} when a schema is attached and the
|
|
115
|
-
* merged meta fails validation for the entry's declared type.
|
|
116
|
-
*/
|
|
117
|
-
update(docId: string, meta: Partial<PageMeta>): void {
|
|
118
|
-
const treeMap = this.dm.getTreeMap();
|
|
119
|
-
if (!treeMap) throw new Error("Not connected");
|
|
120
|
-
|
|
121
|
-
const raw = treeMap.get(docId);
|
|
122
|
-
if (!raw) throw new Error(`Document ${docId} not found`);
|
|
123
|
-
|
|
124
|
-
const entry = toPlain(raw) as Record<string, unknown>;
|
|
125
|
-
const mergedMeta = { ...((entry.meta as PageMeta) ?? {}), ...meta };
|
|
126
|
-
this.validateOrThrow(docId, entry, mergedMeta);
|
|
127
|
-
patchEntry(treeMap, docId, {
|
|
128
|
-
meta: mergedMeta,
|
|
129
|
-
updatedAt: Date.now(),
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Replace all metadata on a document.
|
|
135
|
-
* This overwrites the entire meta object.
|
|
136
|
-
*
|
|
137
|
-
* @throws {MetaValidationError} when a schema is attached and the
|
|
138
|
-
* replacement meta fails validation for the entry's declared type.
|
|
139
|
-
*/
|
|
140
|
-
set(docId: string, meta: PageMeta): void {
|
|
141
|
-
const treeMap = this.dm.getTreeMap();
|
|
142
|
-
if (!treeMap) throw new Error("Not connected");
|
|
143
|
-
|
|
144
|
-
const raw = treeMap.get(docId);
|
|
145
|
-
if (!raw) throw new Error(`Document ${docId} not found`);
|
|
146
|
-
|
|
147
|
-
const entry = toPlain(raw) as Record<string, unknown>;
|
|
148
|
-
this.validateOrThrow(docId, entry, meta);
|
|
149
|
-
patchEntry(treeMap, docId, {
|
|
150
|
-
meta,
|
|
151
|
-
updatedAt: Date.now(),
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Clear specific metadata keys (set them to null/undefined).
|
|
157
|
-
*/
|
|
158
|
-
clear(docId: string, keys: string[]): void {
|
|
159
|
-
const treeMap = this.dm.getTreeMap();
|
|
160
|
-
if (!treeMap) throw new Error("Not connected");
|
|
161
|
-
|
|
162
|
-
const raw = treeMap.get(docId);
|
|
163
|
-
if (!raw) throw new Error(`Document ${docId} not found`);
|
|
164
|
-
|
|
165
|
-
const entry = toPlain(raw) as Record<string, unknown>;
|
|
166
|
-
const existingMeta = ((entry.meta as PageMeta) ?? {}) as Record<
|
|
167
|
-
string,
|
|
168
|
-
unknown
|
|
169
|
-
>;
|
|
170
|
-
const updated = { ...existingMeta };
|
|
171
|
-
for (const key of keys) {
|
|
172
|
-
delete updated[key];
|
|
173
|
-
}
|
|
174
|
-
this.validateOrThrow(docId, entry, updated as PageMeta);
|
|
175
|
-
patchEntry(treeMap, docId, {
|
|
176
|
-
meta: updated,
|
|
177
|
-
updatedAt: Date.now(),
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
private validateOrThrow(
|
|
182
|
-
docId: string,
|
|
183
|
-
entry: Record<string, unknown>,
|
|
184
|
-
meta: PageMeta,
|
|
185
|
-
): void {
|
|
186
|
-
if (!this.schema) return;
|
|
187
|
-
const docType = entry.type as string | undefined;
|
|
188
|
-
if (!docType) return;
|
|
189
|
-
const result = this.schema.validateMeta(docType, meta);
|
|
190
|
-
if (!result.ok) {
|
|
191
|
-
throw new MetaValidationError(docId, docType, result.errors);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MnemonicKeyDerivation
|
|
3
|
-
*
|
|
4
|
-
* Pure, stateless functions for BIP-39 mnemonic-based Ed25519 identity
|
|
5
|
-
* derivation and AES-256-GCM seed wrapping. No IndexedDB, no WebAuthn,
|
|
6
|
-
* no DOM access — fully unit-testable.
|
|
7
|
-
*
|
|
8
|
-
* Derivation chain:
|
|
9
|
-
* BIP-39 Mnemonic (+optional passphrase)
|
|
10
|
-
* → PBKDF2-HMAC-SHA512 (2048 rounds, per BIP-39 spec)
|
|
11
|
-
* → first 32 bytes as IKM
|
|
12
|
-
* → HKDF-SHA256(salt="abracadabra-mnemonic-v1", info="abracadabra-identity-v1")
|
|
13
|
-
* → Ed25519 seed (32 bytes)
|
|
14
|
-
* → Ed25519 keypair + X25519 (Montgomery conversion)
|
|
15
|
-
*
|
|
16
|
-
* The HKDF salt differs from the passkey PRF path (which uses PRF_SALT),
|
|
17
|
-
* providing domain separation: identical raw input bytes can never produce
|
|
18
|
-
* the same Ed25519 seed across the two derivation methods.
|
|
19
|
-
*
|
|
20
|
-
* Dependencies: @scure/bip39, @noble/ed25519, @noble/hashes, @noble/curves
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import * as ed from "@noble/ed25519";
|
|
24
|
-
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
25
|
-
import { sha256, sha512 } from "@noble/hashes/sha2.js";
|
|
26
|
-
import { ed25519 as nobleEd25519Curves } from "@noble/curves/ed25519.js";
|
|
27
|
-
import { generateMnemonic as _generateMnemonic, validateMnemonic as _validateMnemonic, mnemonicToSeedSync } from "@scure/bip39";
|
|
28
|
-
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
29
|
-
|
|
30
|
-
// @noble/ed25519 v3 hash hook (idempotent across modules).
|
|
31
|
-
ed.hashes.sha512 = sha512;
|
|
32
|
-
ed.hashes.sha512Async = (m: Uint8Array) => Promise.resolve(sha512(m));
|
|
33
|
-
|
|
34
|
-
// ── Constants ───────────────────────────────────────────────────────────────
|
|
35
|
-
|
|
36
|
-
/** HKDF salt for mnemonic → Ed25519 seed derivation. */
|
|
37
|
-
const MNEMONIC_HKDF_SALT = /* @__PURE__ */ new TextEncoder().encode("abracadabra-mnemonic-v1");
|
|
38
|
-
|
|
39
|
-
/** HKDF info string — intentionally matches the passkey path's HKDF_INFO. */
|
|
40
|
-
const MNEMONIC_HKDF_INFO = /* @__PURE__ */ new TextEncoder().encode("abracadabra-identity-v1");
|
|
41
|
-
|
|
42
|
-
/** HKDF info string for deriving the AES-GCM seed-wrapping key from PRF output. */
|
|
43
|
-
export const SEED_WRAP_INFO = /* @__PURE__ */ new TextEncoder().encode("abracadabra-seed-wrap-v1");
|
|
44
|
-
|
|
45
|
-
// ── Helpers ─────────────────────────────────────────────────────────────────
|
|
46
|
-
|
|
47
|
-
function toBase64url(bytes: Uint8Array): string {
|
|
48
|
-
return btoa(String.fromCharCode(...bytes))
|
|
49
|
-
.replace(/\+/g, "-")
|
|
50
|
-
.replace(/\//g, "_")
|
|
51
|
-
.replace(/=/g, "");
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// ── Mnemonic generation & validation ────────────────────────────────────────
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Generate a new BIP-39 mnemonic phrase.
|
|
58
|
-
* @param strength 128 for 12 words (default), 256 for 24 words.
|
|
59
|
-
*/
|
|
60
|
-
export function generateMnemonic(strength: 128 | 256 = 128): string {
|
|
61
|
-
return _generateMnemonic(wordlist, strength);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Validate a BIP-39 mnemonic (wordlist + checksum).
|
|
66
|
-
*/
|
|
67
|
-
export function validateMnemonic(mnemonic: string): boolean {
|
|
68
|
-
return _validateMnemonic(mnemonic, wordlist);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Re-export the English wordlist for UI auto-complete.
|
|
73
|
-
*/
|
|
74
|
-
export { wordlist as bip39Wordlist };
|
|
75
|
-
|
|
76
|
-
// ── Key derivation ──────────────────────────────────────────────────────────
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Derive a 32-byte Ed25519 seed from a BIP-39 mnemonic.
|
|
80
|
-
*
|
|
81
|
-
* Chain: mnemonic → PBKDF2-HMAC-SHA512 (2048 rounds) → first 32 bytes →
|
|
82
|
-
* HKDF-SHA256(salt, info) → 32-byte seed.
|
|
83
|
-
*
|
|
84
|
-
* @param mnemonic Valid BIP-39 mnemonic (12 or 24 words).
|
|
85
|
-
* @param passphrase Optional BIP-39 passphrase ("25th word").
|
|
86
|
-
* @returns 32-byte Ed25519 seed. Caller MUST wipe after use: `seed.fill(0)`.
|
|
87
|
-
*/
|
|
88
|
-
export function mnemonicToEd25519Seed(mnemonic: string, passphrase?: string): Uint8Array {
|
|
89
|
-
// BIP-39: PBKDF2-HMAC-SHA512(mnemonic, "mnemonic" + passphrase, 2048) → 64 bytes
|
|
90
|
-
const bip39Seed = mnemonicToSeedSync(mnemonic, passphrase ?? "");
|
|
91
|
-
// Take first 32 bytes as input key material
|
|
92
|
-
const ikm = bip39Seed.subarray(0, 32);
|
|
93
|
-
// HKDF-SHA256 with our domain-specific salt
|
|
94
|
-
const seed = hkdf(sha256, ikm, MNEMONIC_HKDF_SALT, MNEMONIC_HKDF_INFO, 32);
|
|
95
|
-
// Wipe intermediate material
|
|
96
|
-
bip39Seed.fill(0);
|
|
97
|
-
return seed;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Derive the full Ed25519 + X25519 keypair from a BIP-39 mnemonic.
|
|
102
|
-
*
|
|
103
|
-
* @param mnemonic Valid BIP-39 mnemonic.
|
|
104
|
-
* @param passphrase Optional BIP-39 passphrase.
|
|
105
|
-
* @returns Keys and seed. Caller MUST wipe `seed` after use.
|
|
106
|
-
*/
|
|
107
|
-
export async function mnemonicToKeyPair(
|
|
108
|
-
mnemonic: string,
|
|
109
|
-
passphrase?: string,
|
|
110
|
-
): Promise<{
|
|
111
|
-
seed: Uint8Array;
|
|
112
|
-
publicKey: Uint8Array;
|
|
113
|
-
publicKeyB64: string;
|
|
114
|
-
x25519PublicKey: Uint8Array;
|
|
115
|
-
x25519PublicKeyB64: string;
|
|
116
|
-
}> {
|
|
117
|
-
const seed = mnemonicToEd25519Seed(mnemonic, passphrase);
|
|
118
|
-
const publicKey = await ed.getPublicKeyAsync(seed);
|
|
119
|
-
const publicKeyB64 = toBase64url(publicKey);
|
|
120
|
-
const x25519PublicKey = nobleEd25519Curves.utils.toMontgomery(publicKey);
|
|
121
|
-
const x25519PublicKeyB64 = toBase64url(x25519PublicKey);
|
|
122
|
-
return { seed, publicKey, publicKeyB64, x25519PublicKey, x25519PublicKeyB64 };
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// ── Seed wrapping (AES-256-GCM via WebCrypto) ──────────────────────────────
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Derive a 32-byte AES-256-GCM key from WebAuthn PRF output for seed wrapping.
|
|
129
|
-
*
|
|
130
|
-
* @param prfOutput Raw PRF output from WebAuthn assertion (32 bytes typical).
|
|
131
|
-
* @param wrapSalt Random 32-byte salt, stored alongside the ciphertext.
|
|
132
|
-
*/
|
|
133
|
-
export function deriveSeedWrappingKey(prfOutput: ArrayBuffer, wrapSalt: Uint8Array): Uint8Array {
|
|
134
|
-
return hkdf(sha256, new Uint8Array(prfOutput), wrapSalt, SEED_WRAP_INFO, 32);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Encrypt an Ed25519 seed with AES-256-GCM.
|
|
139
|
-
*
|
|
140
|
-
* @param seed 32-byte Ed25519 seed to protect.
|
|
141
|
-
* @param wrappingKeyBytes 32-byte AES key from `deriveSeedWrappingKey`.
|
|
142
|
-
* @returns Ciphertext (48 bytes: 32 plaintext + 16 auth tag) and 12-byte IV.
|
|
143
|
-
*/
|
|
144
|
-
export async function wrapSeed(
|
|
145
|
-
seed: Uint8Array,
|
|
146
|
-
wrappingKeyBytes: Uint8Array,
|
|
147
|
-
): Promise<{ ciphertext: ArrayBuffer; iv: Uint8Array }> {
|
|
148
|
-
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
149
|
-
const key = await crypto.subtle.importKey("raw", wrappingKeyBytes as BufferSource, "AES-GCM", false, ["encrypt"]);
|
|
150
|
-
const ciphertext = await crypto.subtle.encrypt({ name: "AES-GCM", iv: iv as BufferSource }, key, seed as BufferSource);
|
|
151
|
-
return { ciphertext, iv };
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Decrypt an Ed25519 seed from AES-256-GCM ciphertext.
|
|
156
|
-
*
|
|
157
|
-
* @param ciphertext Encrypted seed (48 bytes).
|
|
158
|
-
* @param iv 12-byte GCM nonce.
|
|
159
|
-
* @param wrappingKeyBytes 32-byte AES key from `deriveSeedWrappingKey`.
|
|
160
|
-
* @returns 32-byte Ed25519 seed. Caller MUST wipe after use.
|
|
161
|
-
* @throws If the auth tag is invalid (wrong key or tampered data).
|
|
162
|
-
*/
|
|
163
|
-
export async function unwrapSeed(
|
|
164
|
-
ciphertext: ArrayBuffer,
|
|
165
|
-
iv: Uint8Array,
|
|
166
|
-
wrappingKeyBytes: Uint8Array,
|
|
167
|
-
): Promise<Uint8Array> {
|
|
168
|
-
const key = await crypto.subtle.importKey("raw", wrappingKeyBytes as BufferSource, "AES-GCM", false, ["decrypt"]);
|
|
169
|
-
const plaintext = await crypto.subtle.decrypt({ name: "AES-GCM", iv: iv as BufferSource }, key, ciphertext);
|
|
170
|
-
return new Uint8Array(plaintext);
|
|
171
|
-
}
|
package/src/NodeConnect.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Node connection helpers — shared bootstrap primitives for the Node consumers
|
|
3
|
-
* (`@abraca/mcp`, `@abraca/cli`, `@abraca/orchestrator`, `@abraca/resend`).
|
|
4
|
-
*
|
|
5
|
-
* Each of those packages opened a synced root/space/inbox provider with the
|
|
6
|
-
* exact same incantation (`new Y.Doc({ guid }) → new AbracadabraProvider({
|
|
7
|
-
* disableOfflineStore, subdocLoading:'lazy' }) → waitForSync`) and selected the
|
|
8
|
-
* entry space with the same "first Kind.Space, else first top-level doc" rule.
|
|
9
|
-
* Both live here now so there is one implementation.
|
|
10
|
-
*
|
|
11
|
-
* These are deliberately small, faithful extractions — they do NOT own auth
|
|
12
|
-
* (see {@link loginOrRegisterWithKey}), awareness, child-provider caching, or
|
|
13
|
-
* the reconnect/heal loop. Those stay with each consumer.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import * as Y from "yjs";
|
|
17
|
-
import { AbracadabraProvider } from "./AbracadabraProvider.ts";
|
|
18
|
-
import { AbracadabraClient } from "./AbracadabraClient.ts";
|
|
19
|
-
import { waitForSync } from "./DocUtils.ts";
|
|
20
|
-
import { Kind } from "./types.ts";
|
|
21
|
-
import type { DocumentMeta } from "./types.ts";
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Open a fresh `AbracadabraProvider` bound to `docId` on `client` and wait for
|
|
25
|
-
* its first sync. Returns the new `Y.Doc` + provider. Used for the root space
|
|
26
|
-
* doc, child-space docs, and the inbox doc alike — every Node consumer opened
|
|
27
|
-
* them with these exact options.
|
|
28
|
-
*
|
|
29
|
-
* Offline persistence is disabled (Node daemons/CLIs are online-only) and
|
|
30
|
-
* subdocs load lazily.
|
|
31
|
-
*/
|
|
32
|
-
export async function openSyncedProvider(opts: {
|
|
33
|
-
client: AbracadabraClient;
|
|
34
|
-
docId: string;
|
|
35
|
-
syncTimeoutMs?: number;
|
|
36
|
-
}): Promise<{ doc: Y.Doc; provider: AbracadabraProvider }> {
|
|
37
|
-
const doc = new Y.Doc({ guid: opts.docId });
|
|
38
|
-
const provider = new AbracadabraProvider({
|
|
39
|
-
name: opts.docId,
|
|
40
|
-
document: doc,
|
|
41
|
-
client: opts.client,
|
|
42
|
-
disableOfflineStore: true,
|
|
43
|
-
subdocLoading: "lazy",
|
|
44
|
-
});
|
|
45
|
-
await waitForSync(provider, opts.syncTimeoutMs);
|
|
46
|
-
return { doc, provider };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Pick the entry-point document from a server root's top-level children: the
|
|
51
|
-
* first `Kind.Space`, falling back to the first top-level doc of any kind.
|
|
52
|
-
* Returns `null` when the server has no top-level documents.
|
|
53
|
-
*
|
|
54
|
-
* `spaces` is returned alongside so callers that track the space list (most do)
|
|
55
|
-
* don't have to re-filter.
|
|
56
|
-
*/
|
|
57
|
-
export function pickEntrySpace(roots: DocumentMeta[]): {
|
|
58
|
-
entry: DocumentMeta | null;
|
|
59
|
-
spaces: DocumentMeta[];
|
|
60
|
-
} {
|
|
61
|
-
const spaces = roots.filter((d) => d.kind === Kind.Space);
|
|
62
|
-
return { entry: spaces[0] ?? roots[0] ?? null, spaces };
|
|
63
|
-
}
|