@ipab/ipfs-store 0.0.0-stage → 0.1.0-rc.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/LICENSE +21 -0
- package/README.md +245 -2
- package/dist/configuration.d.ts +21 -0
- package/dist/configuration.d.ts.map +1 -0
- package/dist/configuration.js +62 -0
- package/dist/configuration.js.map +1 -0
- package/dist/dag.d.ts +49 -0
- package/dist/dag.d.ts.map +1 -0
- package/dist/dag.js +216 -0
- package/dist/dag.js.map +1 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +417 -0
- package/dist/index.js.map +1 -0
- package/dist/kubo.d.ts +38 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +189 -0
- package/dist/kubo.js.map +1 -0
- package/dist/node.d.ts +11 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +60 -0
- package/dist/node.js.map +1 -0
- package/dist/retention.d.ts +44 -0
- package/dist/retention.d.ts.map +1 -0
- package/dist/retention.js +328 -0
- package/dist/retention.js.map +1 -0
- package/dist/storage.d.ts +28 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +161 -0
- package/dist/storage.js.map +1 -0
- package/dist/store.d.ts +71 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +412 -0
- package/dist/store.js.map +1 -0
- package/package.json +72 -3
- package/src/configuration.ts +93 -0
- package/src/dag.ts +281 -0
- package/src/errors.ts +25 -0
- package/src/index.ts +520 -0
- package/src/kubo.ts +293 -0
- package/src/node.ts +78 -0
- package/src/retention.ts +398 -0
- package/src/storage.ts +181 -0
- package/src/store.ts +577 -0
package/src/dag.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import * as dagPb from "@ipld/dag-pb";
|
|
2
|
+
import * as dagCbor from "@ipld/dag-cbor";
|
|
3
|
+
import {
|
|
4
|
+
asNativeCID,
|
|
5
|
+
encodeDagCbor,
|
|
6
|
+
} from "@silvermoon-work/ipfs-payload/dag-cbor";
|
|
7
|
+
import { CID } from "multiformats/cid";
|
|
8
|
+
import { sha256 } from "multiformats/hashes/sha2";
|
|
9
|
+
import type { IpfsClient } from "./index.js";
|
|
10
|
+
import { abortError, ContentError } from "./errors.js";
|
|
11
|
+
|
|
12
|
+
export interface ContentLimits {
|
|
13
|
+
bytes: number;
|
|
14
|
+
blocks: number;
|
|
15
|
+
blockBytes: number;
|
|
16
|
+
depth: number;
|
|
17
|
+
}
|
|
18
|
+
export const defaultContentLimits: Readonly<ContentLimits> = Object.freeze({
|
|
19
|
+
bytes: 256 * 1024 * 1024,
|
|
20
|
+
blocks: 16_384,
|
|
21
|
+
blockBytes: 2 * 1024 * 1024,
|
|
22
|
+
depth: 64,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/** Enumerate genuine IPLD links, never CID-looking strings or application object shapes. */
|
|
26
|
+
export function contentLinks(
|
|
27
|
+
cid: CID,
|
|
28
|
+
bytes: Uint8Array,
|
|
29
|
+
limits: { depth?: number; nodes?: number } = {},
|
|
30
|
+
): readonly CID[] {
|
|
31
|
+
for (const limit of Object.values(limits))
|
|
32
|
+
if (!Number.isSafeInteger(limit) || limit < 0)
|
|
33
|
+
throw new ContentError(
|
|
34
|
+
"capacity_exceeded",
|
|
35
|
+
"Invalid structured content limit.",
|
|
36
|
+
);
|
|
37
|
+
if (cid.code === 0x55) return [];
|
|
38
|
+
if (cid.code === dagPb.code)
|
|
39
|
+
return dagPb.decode(bytes).Links.map((link) => link.Hash);
|
|
40
|
+
if (cid.code !== dagCbor.code)
|
|
41
|
+
throw new ContentError(
|
|
42
|
+
"unsupported_content",
|
|
43
|
+
"Unsupported content DAG codec.",
|
|
44
|
+
);
|
|
45
|
+
const links: CID[] = [];
|
|
46
|
+
let nodes = 0;
|
|
47
|
+
const value: unknown = dagCbor.decode(bytes);
|
|
48
|
+
const visit = (value: unknown, depth: number): void => {
|
|
49
|
+
if (++nodes > (limits.nodes ?? 100_000) || depth > (limits.depth ?? 64))
|
|
50
|
+
throw new ContentError(
|
|
51
|
+
"capacity_exceeded",
|
|
52
|
+
"Structured content capacity exceeded.",
|
|
53
|
+
);
|
|
54
|
+
const link = asNativeCID(value);
|
|
55
|
+
if (link) links.push(link);
|
|
56
|
+
else if (Array.isArray(value))
|
|
57
|
+
for (const item of value) visit(item, depth + 1);
|
|
58
|
+
else if (
|
|
59
|
+
value &&
|
|
60
|
+
typeof value === "object" &&
|
|
61
|
+
!(value instanceof Uint8Array)
|
|
62
|
+
)
|
|
63
|
+
for (const item of Object.values(value)) visit(item, depth + 1);
|
|
64
|
+
};
|
|
65
|
+
visit(value, 0);
|
|
66
|
+
const canonical = encodeDagCbor(value);
|
|
67
|
+
if (
|
|
68
|
+
canonical.length !== bytes.length ||
|
|
69
|
+
canonical.some((byte, i) => byte !== bytes[i])
|
|
70
|
+
)
|
|
71
|
+
throw new ContentError(
|
|
72
|
+
"unsupported_content",
|
|
73
|
+
"Non-canonical DAG-CBOR content.",
|
|
74
|
+
);
|
|
75
|
+
return links;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function verifyBlock(cid: CID, bytes: Uint8Array): Promise<void> {
|
|
79
|
+
if (cid.multihash.code !== sha256.code)
|
|
80
|
+
throw new Error("Unsupported content hash.");
|
|
81
|
+
const hash = await sha256.digest(bytes);
|
|
82
|
+
if (!Buffer.from(hash.digest).equals(Buffer.from(cid.multihash.digest)))
|
|
83
|
+
throw new Error(`Content hash verification failed: ${cid.toString()}`);
|
|
84
|
+
}
|
|
85
|
+
export async function acquireBlock(
|
|
86
|
+
client: IpfsClient,
|
|
87
|
+
cid: CID,
|
|
88
|
+
options: {
|
|
89
|
+
signal?: AbortSignal;
|
|
90
|
+
blockBytes?: number;
|
|
91
|
+
timeoutMs?: number;
|
|
92
|
+
} = {},
|
|
93
|
+
): Promise<Uint8Array> {
|
|
94
|
+
const timeoutMs = options.timeoutMs ?? 30_000;
|
|
95
|
+
if (
|
|
96
|
+
!Number.isInteger(timeoutMs) ||
|
|
97
|
+
timeoutMs < 0 ||
|
|
98
|
+
timeoutMs > 4_294_967_295
|
|
99
|
+
)
|
|
100
|
+
throw new RangeError("Invalid acquisition timeout.");
|
|
101
|
+
const timeout = new AbortController();
|
|
102
|
+
const signal = AbortSignal.any([
|
|
103
|
+
timeout.signal,
|
|
104
|
+
...(options.signal ? [options.signal] : []),
|
|
105
|
+
]);
|
|
106
|
+
signal.throwIfAborted();
|
|
107
|
+
// Unlike AbortSignal.timeout(), this deadline must keep an otherwise idle
|
|
108
|
+
// process alive while an injected source has no active I/O handles.
|
|
109
|
+
const timer = setTimeout(() => {
|
|
110
|
+
timeout.abort(
|
|
111
|
+
new DOMException("Content acquisition timed out.", "TimeoutError"),
|
|
112
|
+
);
|
|
113
|
+
}, timeoutMs);
|
|
114
|
+
let abort = () => {};
|
|
115
|
+
try {
|
|
116
|
+
const bytes = await Promise.race([
|
|
117
|
+
Promise.resolve().then(() => {
|
|
118
|
+
signal.throwIfAborted();
|
|
119
|
+
return client.block.get(cid.toString(), { signal });
|
|
120
|
+
}),
|
|
121
|
+
new Promise<never>((_, reject) => {
|
|
122
|
+
abort = () => {
|
|
123
|
+
reject(abortError(signal));
|
|
124
|
+
};
|
|
125
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
126
|
+
if (signal.aborted) abort();
|
|
127
|
+
}),
|
|
128
|
+
]);
|
|
129
|
+
signal.throwIfAborted();
|
|
130
|
+
if (!(bytes instanceof Uint8Array))
|
|
131
|
+
throw new Error("Content source must return bytes.");
|
|
132
|
+
if (bytes.byteLength > (options.blockBytes ?? 2 * 1024 * 1024))
|
|
133
|
+
throw new ContentError(
|
|
134
|
+
"capacity_exceeded",
|
|
135
|
+
"Content block capacity exceeded.",
|
|
136
|
+
);
|
|
137
|
+
const snapshot = Uint8Array.from(bytes);
|
|
138
|
+
await verifyBlock(cid, snapshot);
|
|
139
|
+
signal.throwIfAborted();
|
|
140
|
+
return snapshot;
|
|
141
|
+
} finally {
|
|
142
|
+
clearTimeout(timer);
|
|
143
|
+
signal.removeEventListener("abort", abort);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** One owner per scope; immutable bytes, unique-CID storage accounting, no implicit source. */
|
|
148
|
+
export class ContentBlocks implements IpfsClient {
|
|
149
|
+
readonly blocks = new Map<string, Uint8Array>();
|
|
150
|
+
private readonly pending = new Map<string, Promise<Uint8Array>>();
|
|
151
|
+
private readonly lifetime = new AbortController();
|
|
152
|
+
readonly limits: ContentLimits;
|
|
153
|
+
retainedBytes = 0;
|
|
154
|
+
constructor(
|
|
155
|
+
private readonly source?: IpfsClient,
|
|
156
|
+
limits: Partial<ContentLimits> = {},
|
|
157
|
+
) {
|
|
158
|
+
this.limits = { ...defaultContentLimits, ...limits };
|
|
159
|
+
for (const limit of Object.values(this.limits))
|
|
160
|
+
if (!Number.isSafeInteger(limit) || limit <= 0)
|
|
161
|
+
throw new Error("Invalid content capacity.");
|
|
162
|
+
}
|
|
163
|
+
readonly block = {
|
|
164
|
+
get: async (
|
|
165
|
+
value: string,
|
|
166
|
+
options: { signal?: AbortSignal } = {},
|
|
167
|
+
): Promise<Uint8Array> => {
|
|
168
|
+
return this.readBlock(value, options);
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
async readBlock(
|
|
172
|
+
value: string,
|
|
173
|
+
options: { signal?: AbortSignal } = {},
|
|
174
|
+
source = this.source,
|
|
175
|
+
): Promise<Uint8Array> {
|
|
176
|
+
options.signal?.throwIfAborted();
|
|
177
|
+
this.lifetime.signal.throwIfAborted();
|
|
178
|
+
const cid = CID.parse(value),
|
|
179
|
+
key = cid.toV1().toString();
|
|
180
|
+
let bytes = this.blocks.get(key);
|
|
181
|
+
if (!bytes) {
|
|
182
|
+
let operation = this.pending.get(key);
|
|
183
|
+
if (!operation) {
|
|
184
|
+
operation = (async () => {
|
|
185
|
+
if (!source)
|
|
186
|
+
throw new ContentError(
|
|
187
|
+
"missing_block",
|
|
188
|
+
`Missing local content block: ${cid.toString()}`,
|
|
189
|
+
);
|
|
190
|
+
const fetched = await acquireBlock(source, cid, {
|
|
191
|
+
signal: this.lifetime.signal,
|
|
192
|
+
blockBytes: this.limits.blockBytes,
|
|
193
|
+
});
|
|
194
|
+
await this.put(cid, fetched, { signal: this.lifetime.signal });
|
|
195
|
+
return this.blocks.get(key)!;
|
|
196
|
+
})();
|
|
197
|
+
this.pending.set(key, operation);
|
|
198
|
+
void operation.finally(() => this.pending.delete(key)).catch(() => {});
|
|
199
|
+
}
|
|
200
|
+
let abort = () => {};
|
|
201
|
+
try {
|
|
202
|
+
bytes = await Promise.race([
|
|
203
|
+
operation,
|
|
204
|
+
new Promise<never>((_, reject) => {
|
|
205
|
+
abort = () => {
|
|
206
|
+
if (options.signal) reject(abortError(options.signal));
|
|
207
|
+
};
|
|
208
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
209
|
+
if (options.signal?.aborted) abort();
|
|
210
|
+
}),
|
|
211
|
+
]);
|
|
212
|
+
} finally {
|
|
213
|
+
options.signal?.removeEventListener("abort", abort);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
options.signal?.throwIfAborted();
|
|
217
|
+
return Uint8Array.from(bytes);
|
|
218
|
+
}
|
|
219
|
+
async close(): Promise<void> {
|
|
220
|
+
this.lifetime.abort(new Error("Content blocks are closed."));
|
|
221
|
+
await Promise.allSettled([...this.pending.values()]);
|
|
222
|
+
}
|
|
223
|
+
async put(
|
|
224
|
+
cid: CID,
|
|
225
|
+
bytes: Uint8Array,
|
|
226
|
+
options: { signal?: AbortSignal } = {},
|
|
227
|
+
): Promise<CID> {
|
|
228
|
+
options.signal?.throwIfAborted();
|
|
229
|
+
this.lifetime.signal.throwIfAborted();
|
|
230
|
+
if (bytes.byteLength > this.limits.blockBytes)
|
|
231
|
+
throw new ContentError(
|
|
232
|
+
"capacity_exceeded",
|
|
233
|
+
"Content block capacity exceeded.",
|
|
234
|
+
);
|
|
235
|
+
const snapshot = Uint8Array.from(bytes);
|
|
236
|
+
await verifyBlock(cid, snapshot);
|
|
237
|
+
options.signal?.throwIfAborted();
|
|
238
|
+
this.lifetime.signal.throwIfAborted();
|
|
239
|
+
const key = cid.toV1().toString();
|
|
240
|
+
if (this.blocks.has(key)) return cid;
|
|
241
|
+
if (
|
|
242
|
+
this.blocks.size >= this.limits.blocks ||
|
|
243
|
+
this.retainedBytes + bytes.byteLength > this.limits.bytes
|
|
244
|
+
)
|
|
245
|
+
throw new ContentError(
|
|
246
|
+
"capacity_exceeded",
|
|
247
|
+
"Content retention capacity exceeded.",
|
|
248
|
+
);
|
|
249
|
+
this.blocks.set(key, snapshot);
|
|
250
|
+
this.retainedBytes += bytes.byteLength;
|
|
251
|
+
return cid;
|
|
252
|
+
}
|
|
253
|
+
async *get(cid: CID, options: { signal?: AbortSignal } = {}) {
|
|
254
|
+
yield await this.block.get(cid.toString(), options);
|
|
255
|
+
}
|
|
256
|
+
async prepare(
|
|
257
|
+
root: CID | string,
|
|
258
|
+
options: { signal?: AbortSignal } = {},
|
|
259
|
+
): Promise<void> {
|
|
260
|
+
const heights = new Map<string, number>();
|
|
261
|
+
const walk = async (cid: CID, depth: number): Promise<number> => {
|
|
262
|
+
options.signal?.throwIfAborted();
|
|
263
|
+
if (depth > this.limits.depth)
|
|
264
|
+
throw new Error("Content DAG depth capacity exceeded.");
|
|
265
|
+
const key = cid.toV1().toString();
|
|
266
|
+
const known = heights.get(key);
|
|
267
|
+
if (known !== undefined) {
|
|
268
|
+
if (depth + known > this.limits.depth)
|
|
269
|
+
throw new Error("Content DAG depth capacity exceeded.");
|
|
270
|
+
return known;
|
|
271
|
+
}
|
|
272
|
+
const bytes = await this.block.get(cid.toString(), options);
|
|
273
|
+
let height = 0;
|
|
274
|
+
for (const link of contentLinks(cid, bytes))
|
|
275
|
+
height = Math.max(height, 1 + (await walk(link, depth + 1)));
|
|
276
|
+
heights.set(key, height);
|
|
277
|
+
return height;
|
|
278
|
+
};
|
|
279
|
+
await walk(typeof root === "string" ? CID.parse(root) : root, 0);
|
|
280
|
+
}
|
|
281
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class ContentError extends Error {
|
|
2
|
+
constructor(
|
|
3
|
+
readonly code:
|
|
4
|
+
| "capacity_exceeded"
|
|
5
|
+
| "reclamation_unknown"
|
|
6
|
+
| "missing_block"
|
|
7
|
+
| "rpc_unavailable"
|
|
8
|
+
| "rpc_failed"
|
|
9
|
+
| "retention_unfinished"
|
|
10
|
+
| "read_only"
|
|
11
|
+
| "unsupported_content",
|
|
12
|
+
message: string,
|
|
13
|
+
options?: ErrorOptions,
|
|
14
|
+
) {
|
|
15
|
+
super(message, options);
|
|
16
|
+
this.name = "ContentError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function abortError(signal: AbortSignal): Error {
|
|
21
|
+
const reason: unknown = signal.reason;
|
|
22
|
+
return reason instanceof Error
|
|
23
|
+
? reason
|
|
24
|
+
: new Error("Content operation aborted.", { cause: reason });
|
|
25
|
+
}
|