@tik-choco/mistvault 0.3.0 → 0.3.1
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.ja.md +2 -0
- package/README.md +4 -0
- package/README.zh.md +2 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +1 -0
- package/dist/testing/memory.d.ts +27 -0
- package/dist/testing/memory.js +110 -0
- package/package.json +10 -2
package/README.ja.md
CHANGED
|
@@ -12,6 +12,8 @@ npm install @tik-choco/mistvault
|
|
|
12
12
|
|
|
13
13
|
`dist/` は Git 管理対象外です。開発時は `npm install`、`npm run build`、`npm test` を実行してください。
|
|
14
14
|
|
|
15
|
+
アプリのテストでは `import { MemoryProvider, memoryStorage } from '@tik-choco/mistvault/testing'` でインメモリーの通信とストレージを利用できます。Vitest が必要です。
|
|
16
|
+
|
|
15
17
|
```ts
|
|
16
18
|
import { createVault, createMistVaultTransport } from '@tik-choco/mistvault'
|
|
17
19
|
|
package/README.md
CHANGED
|
@@ -12,6 +12,10 @@ npm install @tik-choco/mistvault
|
|
|
12
12
|
|
|
13
13
|
Generated `dist/` files are excluded from Git. For development, run `npm install`, `npm run build`, and `npm test`.
|
|
14
14
|
|
|
15
|
+
## Testing helpers
|
|
16
|
+
|
|
17
|
+
For application tests, use `import { MemoryProvider, memoryStorage } from '@tik-choco/mistvault/testing'` for in-memory transport and storage. Vitest is required.
|
|
18
|
+
|
|
15
19
|
## Connect an application
|
|
16
20
|
|
|
17
21
|
```ts
|
package/README.zh.md
CHANGED
|
@@ -12,6 +12,8 @@ npm install @tik-choco/mistvault
|
|
|
12
12
|
|
|
13
13
|
`dist/` 不纳入版本控制。开发时运行 `npm install`、`npm run build` 和 `npm test`。
|
|
14
14
|
|
|
15
|
+
应用测试可通过 `import { MemoryProvider, memoryStorage } from '@tik-choco/mistvault/testing'` 使用内存传输和存储。需要 Vitest。
|
|
16
|
+
|
|
15
17
|
```ts
|
|
16
18
|
import { createVault, createMistVaultTransport } from '@tik-choco/mistvault'
|
|
17
19
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './memory.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './memory.js';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { EscrowTransport } from '../core/client.js';
|
|
2
|
+
import type { EscrowHead } from '../core/head.js';
|
|
3
|
+
import type { EscrowMessage, EscrowPut } from '../core/protocol.js';
|
|
4
|
+
export declare function memoryStorage(): Storage;
|
|
5
|
+
/** A protocol-level provider with real encrypted blobs, signatures and compare-and-swap. */
|
|
6
|
+
export declare class MemoryProvider implements EscrowTransport {
|
|
7
|
+
readonly heads: Map<string, EscrowHead>;
|
|
8
|
+
readonly tombstones: Map<string, EscrowHead>;
|
|
9
|
+
readonly blobs: Map<string, Uint8Array<ArrayBufferLike>>;
|
|
10
|
+
readonly requests: EscrowMessage[];
|
|
11
|
+
readonly messages: Map<string, Set<(from: string, bytes: Uint8Array) => void>>;
|
|
12
|
+
readonly peers: Map<string, Set<(id: string, joined: boolean) => void>>;
|
|
13
|
+
online: boolean;
|
|
14
|
+
quota: Set<string>;
|
|
15
|
+
beforePut?: (message: EscrowPut) => Promise<void>;
|
|
16
|
+
join: import("vitest").Mock<(_room: string) => Promise<void>>;
|
|
17
|
+
leave: import("vitest").Mock<(_room: string) => void>;
|
|
18
|
+
storageAdd: import("vitest").Mock<(_name: string, bytes: Uint8Array) => Promise<string>>;
|
|
19
|
+
storageGet: import("vitest").Mock<(cid: string) => Promise<Uint8Array<ArrayBufferLike>>>;
|
|
20
|
+
onMessage(room: string, cb: (from: string, bytes: Uint8Array) => void): () => void;
|
|
21
|
+
onPeer(room: string, cb: (id: string, joined: boolean) => void): () => void;
|
|
22
|
+
send(room: string, _to: string | null, bytes: Uint8Array): void;
|
|
23
|
+
failures: unknown[];
|
|
24
|
+
reply(room: string, message: EscrowMessage): void;
|
|
25
|
+
advertise(): void;
|
|
26
|
+
answer(room: string, m: EscrowMessage): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
import { headHash, verifyHead, deleteSigningPayload } from '../core/head.js';
|
|
3
|
+
import { fromBase64url } from '../core/crypto.js';
|
|
4
|
+
import { encodeEscrowMessage, parseEscrowMessage } from '../core/protocol.js';
|
|
5
|
+
export function memoryStorage() {
|
|
6
|
+
const values = new Map();
|
|
7
|
+
return { get length() { return values.size; }, clear: () => values.clear(), key: n => [...values.keys()][n] ?? null,
|
|
8
|
+
getItem: k => values.get(k) ?? null, removeItem: k => { values.delete(k); }, setItem: (k, v) => { values.set(k, v); } };
|
|
9
|
+
}
|
|
10
|
+
/** A protocol-level provider with real encrypted blobs, signatures and compare-and-swap. */
|
|
11
|
+
export class MemoryProvider {
|
|
12
|
+
heads = new Map();
|
|
13
|
+
tombstones = new Map();
|
|
14
|
+
blobs = new Map();
|
|
15
|
+
requests = [];
|
|
16
|
+
messages = new Map();
|
|
17
|
+
peers = new Map();
|
|
18
|
+
online = true;
|
|
19
|
+
quota = new Set();
|
|
20
|
+
beforePut;
|
|
21
|
+
join = vi.fn(async (_room) => { });
|
|
22
|
+
leave = vi.fn((_room) => { });
|
|
23
|
+
storageAdd = vi.fn(async (_name, bytes) => {
|
|
24
|
+
const cid = 'cid-' + this.blobs.size;
|
|
25
|
+
this.blobs.set(cid, new Uint8Array(bytes));
|
|
26
|
+
return cid;
|
|
27
|
+
});
|
|
28
|
+
storageGet = vi.fn(async (cid) => {
|
|
29
|
+
const bytes = this.blobs.get(cid);
|
|
30
|
+
if (!bytes)
|
|
31
|
+
throw new Error('Missing blob');
|
|
32
|
+
return bytes;
|
|
33
|
+
});
|
|
34
|
+
onMessage(room, cb) {
|
|
35
|
+
const set = this.messages.get(room) ?? new Set();
|
|
36
|
+
this.messages.set(room, set);
|
|
37
|
+
set.add(cb);
|
|
38
|
+
return () => { set.delete(cb); };
|
|
39
|
+
}
|
|
40
|
+
onPeer(room, cb) {
|
|
41
|
+
const set = this.peers.get(room) ?? new Set();
|
|
42
|
+
this.peers.set(room, set);
|
|
43
|
+
set.add(cb);
|
|
44
|
+
return () => { set.delete(cb); };
|
|
45
|
+
}
|
|
46
|
+
send(room, _to, bytes) {
|
|
47
|
+
const message = parseEscrowMessage(bytes);
|
|
48
|
+
if (!message)
|
|
49
|
+
throw new Error('Invalid request');
|
|
50
|
+
this.requests.push(message);
|
|
51
|
+
if (this.online)
|
|
52
|
+
void this.answer(room, message).catch(e => { this.failures.push(e); });
|
|
53
|
+
}
|
|
54
|
+
failures = [];
|
|
55
|
+
reply(room, message) { for (const cb of this.messages.get(room) ?? [])
|
|
56
|
+
cb('provider', encodeEscrowMessage(message)); }
|
|
57
|
+
advertise() { for (const room of this.messages.keys())
|
|
58
|
+
this.reply(room, { v: 1, type: 'escrow_provider', version: 1, maxBlob: 33554432, features: ['delete', 'delete-continue'] }); }
|
|
59
|
+
async answer(room, m) {
|
|
60
|
+
if (m.type === 'escrow_hello') {
|
|
61
|
+
this.advertise();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (m.type === 'escrow_get') {
|
|
65
|
+
this.reply(room, { v: 1, type: 'escrow_head', id: m.id, slot: m.slot, head: this.heads.get(m.slot) ?? null });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (m.type === 'escrow_put') {
|
|
69
|
+
await this.beforePut?.(m);
|
|
70
|
+
const h = m.head, old = this.heads.get(h.slot), tombstone = this.tombstones.get(h.slot);
|
|
71
|
+
if (!await verifyHead(h, { slot: h.slot, writer: h.writer }))
|
|
72
|
+
throw new Error('Invalid signature');
|
|
73
|
+
if (this.quota.has(h.slot)) {
|
|
74
|
+
this.reply(room, { v: 1, type: 'escrow_error', id: m.id, code: 'quota', message: 'Full' });
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (!old && tombstone && (h.seq !== tombstone.seq + 1 || h.parent !== await headHash(tombstone))) {
|
|
78
|
+
this.reply(room, { v: 1, type: 'escrow_error', id: m.id, code: 'deleted', head: tombstone, message: 'Deleted' });
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const hash = await headHash(h);
|
|
82
|
+
if (old && hash !== await headHash(old) && (h.writer !== old.writer || h.seq !== old.seq + 1 || h.parent !== await headHash(old)) || !old && !tombstone && h.parent !== null) {
|
|
83
|
+
this.reply(room, { v: 1, type: 'escrow_error', id: m.id, code: 'conflict', head: old ?? null, message: 'Changed' });
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const blob = this.blobs.get(h.cid);
|
|
87
|
+
if (!blob || blob.length !== h.size)
|
|
88
|
+
throw new Error('Invalid blob');
|
|
89
|
+
this.heads.set(h.slot, h);
|
|
90
|
+
this.tombstones.delete(h.slot);
|
|
91
|
+
this.reply(room, { v: 1, type: 'escrow_ack', id: m.id, slot: h.slot, seq: h.seq, hash });
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (m.type === 'escrow_delete') {
|
|
95
|
+
const old = this.heads.get(m.slot);
|
|
96
|
+
if (old) {
|
|
97
|
+
const key = await crypto.subtle.importKey('raw', fromBase64url(old.writer), 'Ed25519', false, ['verify']);
|
|
98
|
+
if (!await crypto.subtle.verify('Ed25519', key, fromBase64url(m.sig), deleteSigningPayload(m)))
|
|
99
|
+
throw new Error('Invalid delete signature');
|
|
100
|
+
if (old.seq !== m.seq || await headHash(old) !== m.head) {
|
|
101
|
+
this.reply(room, { v: 1, type: 'escrow_error', id: m.id, code: 'conflict', head: old, message: 'Changed' });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.heads.delete(m.slot);
|
|
105
|
+
this.tombstones.set(m.slot, old);
|
|
106
|
+
}
|
|
107
|
+
this.reply(room, { v: 1, type: 'escrow_deleted', id: m.id, slot: m.slot });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tik-choco/mistvault",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Browser-encrypted distributed vaults with portable transport and optional framework-independent Web Components UI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -23,6 +23,10 @@
|
|
|
23
23
|
"types": "./dist/elements/index.d.ts",
|
|
24
24
|
"default": "./dist/elements/index.js"
|
|
25
25
|
},
|
|
26
|
+
"./testing": {
|
|
27
|
+
"types": "./dist/testing/index.d.ts",
|
|
28
|
+
"default": "./dist/testing/index.js"
|
|
29
|
+
},
|
|
26
30
|
"./elements.css": "./dist/preact/ui.css",
|
|
27
31
|
"./ui.css": "./dist/preact/ui.css"
|
|
28
32
|
},
|
|
@@ -46,11 +50,15 @@
|
|
|
46
50
|
"vitest": "^4.0.0"
|
|
47
51
|
},
|
|
48
52
|
"peerDependencies": {
|
|
49
|
-
"preact": "^10.0.0"
|
|
53
|
+
"preact": "^10.0.0",
|
|
54
|
+
"vitest": ">=1"
|
|
50
55
|
},
|
|
51
56
|
"peerDependenciesMeta": {
|
|
52
57
|
"preact": {
|
|
53
58
|
"optional": true
|
|
59
|
+
},
|
|
60
|
+
"vitest": {
|
|
61
|
+
"optional": true
|
|
54
62
|
}
|
|
55
63
|
},
|
|
56
64
|
"publishConfig": {
|