-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
memory.ts
138 lines (110 loc) · 3.55 KB
/
memory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Crypto } from "../../crypto/crypto.ts";
import { DocAttachment } from "../../util/doc-types.ts";
import { ReplicaIsClosedError, ValidationError } from "../../util/errors.ts";
import { streamToBytes } from "../../util/streams.ts";
import { IReplicaAttachmentDriver } from "../replica-types.ts";
/** An attachment driver which persists attachments in memory.
* Works everywhere.
*/
export class AttachmentDriverMemory implements IReplicaAttachmentDriver {
private stagingMap = new Map<string, Blob>();
private attachmentMap = new Map<string, Blob>();
private closed = false;
private getKey(formatName: string, attachmentHash: string) {
return `${formatName}___${attachmentHash}`;
}
getAttachment(
formatName: string,
attachmentHash: string,
): Promise<DocAttachment | undefined> {
if (this.closed) throw new ReplicaIsClosedError();
const key = this.getKey(formatName, attachmentHash);
const attachment = this.attachmentMap.get(key);
if (!attachment) {
return Promise.resolve(undefined);
}
return Promise.resolve({
bytes: async () => new Uint8Array(await attachment.arrayBuffer()),
stream: () =>
Promise.resolve(
// Need to do this for Node's sake.
attachment.stream() as unknown as ReadableStream<Uint8Array>,
),
});
}
async stage(
formatName: string,
attachment: ReadableStream<Uint8Array> | Uint8Array,
) {
if (this.closed) throw new ReplicaIsClosedError();
const bytes = attachment instanceof Uint8Array
? attachment
: await streamToBytes(attachment);
const hash = await Crypto.sha256base32(bytes);
const newAttachment = new Blob([bytes]);
const key = this.getKey(formatName, hash);
this.stagingMap.set(key, newAttachment);
return Promise.resolve({
hash,
size: bytes.byteLength,
commit: () => {
this.attachmentMap.set(key, newAttachment);
this.stagingMap.delete(key);
return Promise.resolve();
},
reject: () => {
this.stagingMap.delete(key);
return Promise.resolve();
},
});
}
erase(formatName: string, attachmentHash: string) {
if (this.closed) throw new ReplicaIsClosedError();
const key = this.getKey(formatName, attachmentHash);
if (this.attachmentMap.has(key)) {
this.attachmentMap.delete(key);
return Promise.resolve(true as true);
}
return Promise.resolve(
new ValidationError("No attachment with that signature found."),
);
}
wipe() {
if (this.closed) throw new ReplicaIsClosedError();
this.attachmentMap.clear();
return Promise.resolve();
}
async filter(
hashes: Record<string, Set<string>>,
): Promise<{ format: string; hash: string }[]> {
if (this.closed) throw new ReplicaIsClosedError();
const erasedAttachments = [];
for (const key of this.attachmentMap.keys()) {
const [format, hash] = key.split("___");
const hashesToKeep = hashes[format];
if (hashesToKeep && !hashesToKeep.has(hash)) {
const result = await this.erase(format, hash);
if (result) {
erasedAttachments.push({ format, hash });
}
}
}
return erasedAttachments;
}
clearStaging() {
if (this.closed) throw new ReplicaIsClosedError();
this.stagingMap.clear();
return Promise.resolve();
}
isClosed(): boolean {
return this.closed;
}
async close(erase: boolean) {
if (this.closed) throw new ReplicaIsClosedError();
if (erase) {
await this.wipe();
}
this.closed = true;
return;
}
}