@open-conduit/sdk 0.0.0-stage → 7.1.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 +12 -2
- package/dist/actions.d.ts +145 -0
- package/dist/actions.js +281 -0
- package/dist/actiontest.d.ts +80 -0
- package/dist/actiontest.js +190 -0
- package/dist/artifactstore.d.ts +133 -0
- package/dist/artifactstore.js +364 -0
- package/dist/backup.d.ts +99 -0
- package/dist/backup.js +178 -0
- package/dist/containertest.d.ts +71 -0
- package/dist/containertest.js +294 -0
- package/dist/errors.d.ts +127 -0
- package/dist/errors.js +180 -0
- package/dist/executor.d.ts +374 -0
- package/dist/executor.js +556 -0
- package/dist/executortest.d.ts +183 -0
- package/dist/executortest.js +534 -0
- package/dist/extension.d.ts +855 -0
- package/dist/extension.js +1822 -0
- package/dist/feed.d.ts +20 -0
- package/dist/feed.js +88 -0
- package/dist/forward.d.ts +38 -0
- package/dist/forward.js +163 -0
- package/dist/forwardca.d.ts +21 -0
- package/dist/forwardca.js +144 -0
- package/dist/hooks.d.ts +108 -0
- package/dist/hooks.js +13 -0
- package/dist/host.d.ts +295 -0
- package/dist/host.js +486 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +31 -0
- package/dist/jsonrpc.d.ts +39 -0
- package/dist/jsonrpc.js +350 -0
- package/dist/probe.d.ts +61 -0
- package/dist/probe.js +92 -0
- package/dist/providertest.d.ts +59 -0
- package/dist/providertest.js +168 -0
- package/dist/resources.d.ts +208 -0
- package/dist/resources.js +303 -0
- package/dist/sandbox.d.ts +84 -0
- package/dist/sandbox.js +85 -0
- package/dist/schema.d.ts +71 -0
- package/dist/schema.js +252 -0
- package/dist/setup.d.ts +145 -0
- package/dist/setup.js +179 -0
- package/dist/setuptest.d.ts +90 -0
- package/dist/setuptest.js +319 -0
- package/dist/storetest.d.ts +64 -0
- package/dist/storetest.js +740 -0
- package/dist/telemetry.d.ts +86 -0
- package/dist/telemetry.js +46 -0
- package/dist/testing.d.ts +756 -0
- package/dist/testing.js +2165 -0
- package/dist/toolbelt.d.ts +91 -0
- package/dist/toolbelt.js +216 -0
- package/dist/tunnel.d.ts +62 -0
- package/dist/tunnel.js +93 -0
- package/dist/wire.d.ts +5359 -0
- package/dist/wire.js +2211 -0
- package/package.json +29 -4
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
// Backup destinations and artifact stores as the host reaches them, and
|
|
2
|
+
// the checks conformance.RunBackup and conformance.RunArtifacts hold a Go
|
|
3
|
+
// destination or store to, for a TypeScript one. A destination is reached
|
|
4
|
+
// through the test host (TestHost.backupDestination); an artifact provider
|
|
5
|
+
// through testArtifactProvider, which speaks the artifact provider
|
|
6
|
+
// protocol to it over in-memory streams as the host's
|
|
7
|
+
// artifactstore.Command does. Each check resolves when the package keeps
|
|
8
|
+
// the contract and rejects with what it broke. The Go suites, run against
|
|
9
|
+
// the packaged program under a real host, are the contract; these let a
|
|
10
|
+
// package's own tests hold it there first.
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { PassThrough } from "node:stream";
|
|
13
|
+
import { ArtifactCode, ArtifactError, ArtifactStoreChunk, ArtifactStoreVersion, serveArtifactProvider, } from "./artifactstore.js";
|
|
14
|
+
import { BackupManifest } from "./backup.js";
|
|
15
|
+
import { OperationError, ProtocolError } from "./errors.js";
|
|
16
|
+
import { Conn } from "./jsonrpc.js";
|
|
17
|
+
import { matches, t } from "./schema.js";
|
|
18
|
+
import { ArtifactStoreHandle, ArtifactStoreInitializeResult, ArtifactStoreListResult, ArtifactStoreReadResult, ArtifactStoreRef, } from "./wire.js";
|
|
19
|
+
const fail = (message) => {
|
|
20
|
+
throw new Error(message);
|
|
21
|
+
};
|
|
22
|
+
const show = (v) => JSON.stringify(v);
|
|
23
|
+
const text = (b) => Buffer.from(b).toString();
|
|
24
|
+
const bytesOf = (s) => new Uint8Array(Buffer.from(s));
|
|
25
|
+
/** What a chunked read of the whole file returns. */
|
|
26
|
+
const backupChunk = 256 << 10;
|
|
27
|
+
async function readWhole(d, bundle, path) {
|
|
28
|
+
const parts = [];
|
|
29
|
+
let at = 0;
|
|
30
|
+
for (;;) {
|
|
31
|
+
const { chunk, eof } = await d.read(bundle, path, at, backupChunk);
|
|
32
|
+
if (chunk.length > backupChunk)
|
|
33
|
+
fail(`read ${bundle}/${path} at ${at} answered ${chunk.length} bytes; asked for at most ${backupChunk}`);
|
|
34
|
+
parts.push(chunk);
|
|
35
|
+
at += chunk.length;
|
|
36
|
+
if (eof)
|
|
37
|
+
return Buffer.concat(parts);
|
|
38
|
+
if (chunk.length === 0)
|
|
39
|
+
fail(`read ${bundle}/${path} at ${at} answered nothing before the end`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function put(d, bundle, path, body) {
|
|
43
|
+
await d.put(bundle, { path, size: body.length }, body);
|
|
44
|
+
}
|
|
45
|
+
async function paths(d, bundle) {
|
|
46
|
+
return new Map((await d.files(bundle)).map((f) => [f.path, f.size]));
|
|
47
|
+
}
|
|
48
|
+
/** size bytes that differ from offset to offset, so a read from the wrong place doesn't match. */
|
|
49
|
+
function body(size, seed) {
|
|
50
|
+
const b = new Uint8Array(size);
|
|
51
|
+
for (let i = 0; i < size; i++)
|
|
52
|
+
b[i] = ((i * 7 + Math.floor(i / 251)) & 0xff) ^ seed;
|
|
53
|
+
return b;
|
|
54
|
+
}
|
|
55
|
+
async function rejects(p) {
|
|
56
|
+
try {
|
|
57
|
+
await p;
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** The shared backup destination cases (conformance.BackupCases). */
|
|
65
|
+
export const backupCases = [
|
|
66
|
+
{
|
|
67
|
+
name: "put_files_read_back_whole",
|
|
68
|
+
check: async (target) => {
|
|
69
|
+
const d = await target();
|
|
70
|
+
const files = new Map([
|
|
71
|
+
["conduit.db", body(2 * backupChunk + 1234, 1)],
|
|
72
|
+
["artifacts/ab/cd", bytesOf("artifact")],
|
|
73
|
+
["artifacts/empty", new Uint8Array()],
|
|
74
|
+
[BackupManifest, bytesOf('{"files":3}')],
|
|
75
|
+
]);
|
|
76
|
+
for (const [p, b] of files)
|
|
77
|
+
await put(d, "conduit-1", p, b);
|
|
78
|
+
const stored = await paths(d, "conduit-1");
|
|
79
|
+
if (stored.size !== files.size)
|
|
80
|
+
fail(`files lists ${show([...stored])}; want the ${files.size} put`);
|
|
81
|
+
for (const [p, b] of files) {
|
|
82
|
+
if (stored.get(p) !== b.length)
|
|
83
|
+
fail(`files lists ${p} as ${stored.get(p)} bytes; want ${b.length}`);
|
|
84
|
+
const got = await readWhole(d, "conduit-1", p);
|
|
85
|
+
if (!Buffer.from(got).equals(Buffer.from(b)))
|
|
86
|
+
fail(`${p} read back as ${got.length} bytes that differ from the ${b.length} put`);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "read_answers_from_the_offset_and_says_where_the_file_ends",
|
|
92
|
+
check: async (target) => {
|
|
93
|
+
const d = await target();
|
|
94
|
+
const b = body(1000, 2);
|
|
95
|
+
await put(d, "b", "conduit.db", b);
|
|
96
|
+
for (const [offset, length, eofWanted] of [
|
|
97
|
+
[100, 50, false],
|
|
98
|
+
[0, 999, false],
|
|
99
|
+
[950, 100, true],
|
|
100
|
+
[990, 10, true],
|
|
101
|
+
]) {
|
|
102
|
+
const { chunk, eof } = await d.read("b", "conduit.db", offset, length);
|
|
103
|
+
const end = Math.min(offset + length, b.length);
|
|
104
|
+
if (chunk.length === 0 ||
|
|
105
|
+
chunk.length > length ||
|
|
106
|
+
!Buffer.from(chunk).equals(Buffer.from(b.subarray(offset, offset + chunk.length))))
|
|
107
|
+
fail(`read ${length} bytes from ${offset} answered ${chunk.length} bytes that aren't the file's from there`);
|
|
108
|
+
if (eof && offset + chunk.length < b.length)
|
|
109
|
+
fail(`read ${length} bytes from ${offset} of a ${b.length}-byte file answered eof after ${chunk.length} bytes`);
|
|
110
|
+
if (offset + chunk.length === end && eof !== eofWanted)
|
|
111
|
+
fail(`read ${length} bytes from ${offset} of a ${b.length}-byte file answered eof ${eof}; want ${eofWanted}`);
|
|
112
|
+
}
|
|
113
|
+
if (!(await rejects(d.read("b", "missing.db", 0, 10))))
|
|
114
|
+
fail("a file never put was read");
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: "only_bundles_holding_their_manifest_are_listed",
|
|
119
|
+
check: async (target) => {
|
|
120
|
+
const d = await target();
|
|
121
|
+
await put(d, "partial", "conduit.db", bytesOf("abc"));
|
|
122
|
+
for (const p of ["conduit.db", BackupManifest])
|
|
123
|
+
await put(d, "complete", p, bytesOf("{}"));
|
|
124
|
+
const names = await d.list();
|
|
125
|
+
if (show(names) !== show(["complete"]))
|
|
126
|
+
fail(`list = ${show(names)}; want only the bundle holding ${BackupManifest}, ["complete"]`);
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: "an_unknown_bundle_has_no_files",
|
|
131
|
+
check: async (target) => {
|
|
132
|
+
const d = await target();
|
|
133
|
+
const files = await d.files("never-put");
|
|
134
|
+
if (files.length !== 0)
|
|
135
|
+
fail(`a bundle never put has files ${show(files)}`);
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: "putting_a_path_again_replaces_it",
|
|
140
|
+
check: async (target) => {
|
|
141
|
+
const d = await target();
|
|
142
|
+
await put(d, "b", "conduit.db", bytesOf("the first, longer content"));
|
|
143
|
+
await put(d, "b", "conduit.db", bytesOf("second"));
|
|
144
|
+
const got = text(await readWhole(d, "b", "conduit.db"));
|
|
145
|
+
const size = (await paths(d, "b")).get("conduit.db");
|
|
146
|
+
if (got !== "second" || size !== 6)
|
|
147
|
+
fail(`after putting it again the file reads ${show(got)} and is listed as ${size} bytes; want "second", 6`);
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: "a_failed_or_short_put_leaves_nothing",
|
|
152
|
+
check: async (target) => {
|
|
153
|
+
const d = await target();
|
|
154
|
+
await put(d, "b", "conduit.db", bytesOf("db"));
|
|
155
|
+
const b = body(backupChunk + 100, 3);
|
|
156
|
+
const failing = async function* () {
|
|
157
|
+
yield b.subarray(0, backupChunk + 10);
|
|
158
|
+
throw new Error("the reader failed");
|
|
159
|
+
};
|
|
160
|
+
if (!(await rejects(d.put("b", { path: BackupManifest, size: b.length }, failing()))))
|
|
161
|
+
fail("a put whose reader failed succeeded");
|
|
162
|
+
if (!(await rejects(d.put("b", { path: "artifacts/short", size: b.length }, b.subarray(0, b.length / 2)))))
|
|
163
|
+
fail("a put whose reader ended before the file's size succeeded");
|
|
164
|
+
const stored = await paths(d, "b");
|
|
165
|
+
if (stored.size !== 1 || stored.get("conduit.db") !== 2)
|
|
166
|
+
fail(`after two failed puts the bundle holds ${show([...stored])}; want only conduit.db`);
|
|
167
|
+
const names = await d.list();
|
|
168
|
+
if (names.length !== 0)
|
|
169
|
+
fail(`a bundle whose manifest put failed is listed: ${show(names)}`);
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
name: "names_that_leave_the_bundle_are_refused",
|
|
174
|
+
check: async (target) => {
|
|
175
|
+
const d = await target();
|
|
176
|
+
for (const [bundle, path] of [
|
|
177
|
+
["..", "conduit.db"],
|
|
178
|
+
["b/c", "conduit.db"],
|
|
179
|
+
["", "conduit.db"],
|
|
180
|
+
["b", "../other"],
|
|
181
|
+
["b", "/abs"],
|
|
182
|
+
["b", "a//b"],
|
|
183
|
+
["b", "a/./b"],
|
|
184
|
+
["b", ""],
|
|
185
|
+
]) {
|
|
186
|
+
if (!(await rejects(d.put(bundle, { path, size: 1 }, bytesOf("x")))))
|
|
187
|
+
fail(`a put to bundle ${show(bundle)}, path ${show(path)} was accepted`);
|
|
188
|
+
}
|
|
189
|
+
const names = await d.list();
|
|
190
|
+
if (names.length !== 0)
|
|
191
|
+
fail(`refused puts left bundles ${show(names)}`);
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
name: "targets_share_nothing",
|
|
196
|
+
check: async (target) => {
|
|
197
|
+
const a = await target();
|
|
198
|
+
const b = await target();
|
|
199
|
+
for (const p of ["conduit.db", BackupManifest])
|
|
200
|
+
await put(a, "conduit-1", p, bytesOf("{}"));
|
|
201
|
+
const names = await b.list();
|
|
202
|
+
const files = await b.files("conduit-1");
|
|
203
|
+
if (names.length !== 0 || files.length !== 0)
|
|
204
|
+
fail(`another target lists ${show(names)} and files ${show(files)} of what was put to the first`);
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
];
|
|
208
|
+
/** Runs every backup destination case, rejecting with the first a destination breaks (conformance.RunBackup). */
|
|
209
|
+
export async function checkBackupDestination(target) {
|
|
210
|
+
for (const c of backupCases) {
|
|
211
|
+
try {
|
|
212
|
+
await c.check(target);
|
|
213
|
+
}
|
|
214
|
+
catch (err) {
|
|
215
|
+
throw new Error(`${c.name}: ${err.message}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const artifactCodes = new Set(Object.values(ArtifactCode));
|
|
220
|
+
/** An error the provider answered with, as the ArtifactError its code names. */
|
|
221
|
+
function storageError(err) {
|
|
222
|
+
if ((err instanceof ProtocolError || err instanceof OperationError) &&
|
|
223
|
+
artifactCodes.has(err.code)) {
|
|
224
|
+
const message = err instanceof ProtocolError ? err.detail : err.message;
|
|
225
|
+
return new ArtifactError(err.code, message || undefined);
|
|
226
|
+
}
|
|
227
|
+
return err;
|
|
228
|
+
}
|
|
229
|
+
const sha256 = (b) => createHash("sha256").update(b).digest("hex");
|
|
230
|
+
function fromWire(r) {
|
|
231
|
+
return {
|
|
232
|
+
id: r.id,
|
|
233
|
+
size: r.size,
|
|
234
|
+
mediaType: r.media_type,
|
|
235
|
+
...(r.sha256 ? { sha256: r.sha256 } : {}),
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
function toWire(r) {
|
|
239
|
+
return {
|
|
240
|
+
id: r.id,
|
|
241
|
+
size: r.size,
|
|
242
|
+
media_type: r.mediaType,
|
|
243
|
+
...(r.sha256 ? { sha256: r.sha256 } : {}),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
const b64 = (b) => Buffer.from(b).toString("base64");
|
|
247
|
+
/**
|
|
248
|
+
* Serves backend as an artifact provider in this process: each open starts
|
|
249
|
+
* a connection of its own, as the host starts a provider process, and
|
|
250
|
+
* initializes it at the location.
|
|
251
|
+
*/
|
|
252
|
+
export function testArtifactProvider(backend) {
|
|
253
|
+
return {
|
|
254
|
+
open: async (location) => {
|
|
255
|
+
const toProvider = new PassThrough();
|
|
256
|
+
const fromProvider = new PassThrough();
|
|
257
|
+
const served = serveArtifactProvider(backend, toProvider, fromProvider);
|
|
258
|
+
const conn = new Conn(fromProvider, toProvider, () => Promise.reject(new Error("the host serves nothing")));
|
|
259
|
+
const call = async (schema, method, p) => {
|
|
260
|
+
let raw;
|
|
261
|
+
try {
|
|
262
|
+
raw = await conn.call(`artifactstore.${method}`, p);
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
throw storageError(err);
|
|
266
|
+
}
|
|
267
|
+
if (!matches(schema, raw))
|
|
268
|
+
fail(`malformed artifactstore.${method} result: ${show(raw)}`);
|
|
269
|
+
return raw;
|
|
270
|
+
};
|
|
271
|
+
const done = t.object({}, { open: true });
|
|
272
|
+
const close = async () => {
|
|
273
|
+
toProvider.end();
|
|
274
|
+
await served.done;
|
|
275
|
+
};
|
|
276
|
+
let init;
|
|
277
|
+
try {
|
|
278
|
+
init = await call(ArtifactStoreInitializeResult, "initialize", {
|
|
279
|
+
version: ArtifactStoreVersion,
|
|
280
|
+
location,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
catch (err) {
|
|
284
|
+
await close();
|
|
285
|
+
throw err;
|
|
286
|
+
}
|
|
287
|
+
if (init.version !== ArtifactStoreVersion)
|
|
288
|
+
fail(`the provider answered version ${init.version}`);
|
|
289
|
+
return {
|
|
290
|
+
shared: init.shared,
|
|
291
|
+
create: async (mediaType) => {
|
|
292
|
+
const { handle } = await call(ArtifactStoreHandle, "create", {
|
|
293
|
+
media_type: mediaType,
|
|
294
|
+
});
|
|
295
|
+
const hash = createHash("sha256");
|
|
296
|
+
let size = 0;
|
|
297
|
+
return {
|
|
298
|
+
write: async (chunk) => {
|
|
299
|
+
const b = typeof chunk === "string" ? bytesOf(chunk) : chunk;
|
|
300
|
+
for (let at = 0; at < b.length; at += ArtifactStoreChunk) {
|
|
301
|
+
const part = b.subarray(at, at + ArtifactStoreChunk);
|
|
302
|
+
await call(done, "write", { handle, chunk: b64(part) });
|
|
303
|
+
hash.update(part);
|
|
304
|
+
size += part.length;
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
commit: async () => {
|
|
308
|
+
const ref = fromWire(await call(ArtifactStoreRef, "commit", { handle }));
|
|
309
|
+
const digest = hash.copy().digest("hex");
|
|
310
|
+
if (ref.id === "" ||
|
|
311
|
+
ref.size !== size ||
|
|
312
|
+
ref.mediaType !== mediaType ||
|
|
313
|
+
ref.sha256 !== digest)
|
|
314
|
+
throw new ArtifactError(ArtifactCode.Corrupt, `the provider committed ${show(ref)} for ${size} bytes of ${mediaType} with digest ${digest}`);
|
|
315
|
+
return ref;
|
|
316
|
+
},
|
|
317
|
+
close: async () => {
|
|
318
|
+
await call(done, "abort", { handle });
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
},
|
|
322
|
+
read: async (ref) => {
|
|
323
|
+
const { handle } = await call(ArtifactStoreHandle, "open", toWire(ref));
|
|
324
|
+
const parts = [];
|
|
325
|
+
let size = 0;
|
|
326
|
+
try {
|
|
327
|
+
for (;;) {
|
|
328
|
+
const r = await call(ArtifactStoreReadResult, "read", {
|
|
329
|
+
handle,
|
|
330
|
+
});
|
|
331
|
+
const chunk = r.chunk
|
|
332
|
+
? new Uint8Array(Buffer.from(r.chunk, "base64"))
|
|
333
|
+
: new Uint8Array();
|
|
334
|
+
if (chunk.length === 0 && !r.eof)
|
|
335
|
+
fail("artifactstore.read answered neither bytes nor eof");
|
|
336
|
+
if (chunk.length > ArtifactStoreChunk)
|
|
337
|
+
fail(`artifactstore.read answered ${chunk.length} bytes`);
|
|
338
|
+
parts.push(chunk);
|
|
339
|
+
size += chunk.length;
|
|
340
|
+
if (size > ref.size)
|
|
341
|
+
throw new ArtifactError(ArtifactCode.Corrupt);
|
|
342
|
+
if (r.eof)
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
finally {
|
|
347
|
+
await call(done, "close_reader", { handle }).catch(() => { });
|
|
348
|
+
}
|
|
349
|
+
const content = Buffer.concat(parts);
|
|
350
|
+
if (size !== ref.size ||
|
|
351
|
+
(ref.sha256 !== undefined && sha256(content) !== ref.sha256))
|
|
352
|
+
throw new ArtifactError(ArtifactCode.Corrupt);
|
|
353
|
+
return new Uint8Array(content);
|
|
354
|
+
},
|
|
355
|
+
delete: async (ref) => {
|
|
356
|
+
await call(done, "delete", toWire(ref));
|
|
357
|
+
},
|
|
358
|
+
list: async (after, limit) => ((await call(ArtifactStoreListResult, "list", {
|
|
359
|
+
...(after ? { after } : {}),
|
|
360
|
+
limit,
|
|
361
|
+
})).items ?? []).map((it) => ({
|
|
362
|
+
ref: fromWire(it.ref),
|
|
363
|
+
publishedAt: new Date(it.published_at),
|
|
364
|
+
})),
|
|
365
|
+
import: async (ref, source) => {
|
|
366
|
+
const b = typeof source === "string" ? bytesOf(source) : source;
|
|
367
|
+
const { handle } = await call(ArtifactStoreHandle, "import_begin", toWire(ref));
|
|
368
|
+
// The host stops a source that doesn't match before the
|
|
369
|
+
// provider could publish it.
|
|
370
|
+
if (b.length !== ref.size ||
|
|
371
|
+
(ref.sha256 !== undefined && sha256(b) !== ref.sha256)) {
|
|
372
|
+
await call(done, "import_abort", { handle }).catch(() => { });
|
|
373
|
+
throw new ArtifactError(ArtifactCode.Corrupt);
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
for (let at = 0; at < b.length; at += ArtifactStoreChunk)
|
|
377
|
+
await call(done, "import_write", {
|
|
378
|
+
handle,
|
|
379
|
+
chunk: b64(b.subarray(at, at + ArtifactStoreChunk)),
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
catch (err) {
|
|
383
|
+
await call(done, "import_abort", { handle }).catch(() => { });
|
|
384
|
+
throw err;
|
|
385
|
+
}
|
|
386
|
+
await call(done, "import_finish", { handle });
|
|
387
|
+
},
|
|
388
|
+
claim: async (o) => {
|
|
389
|
+
await call(done, "claim", {
|
|
390
|
+
installation: o.installation,
|
|
391
|
+
...(o.takeOver ? { take_over: true } : {}),
|
|
392
|
+
});
|
|
393
|
+
},
|
|
394
|
+
checkOwner: async (installation) => {
|
|
395
|
+
await call(done, "check_owner", { installation });
|
|
396
|
+
},
|
|
397
|
+
close,
|
|
398
|
+
};
|
|
399
|
+
},
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
/** The installations the ownership cases claim as (conformance's). */
|
|
403
|
+
const firstInstallation = "0123456789abcdef0123456789abcdef";
|
|
404
|
+
const secondInstallation = "fedcba9876543210fedcba9876543210";
|
|
405
|
+
async function code(p) {
|
|
406
|
+
try {
|
|
407
|
+
await p;
|
|
408
|
+
return "none";
|
|
409
|
+
}
|
|
410
|
+
catch (err) {
|
|
411
|
+
return err instanceof ArtifactError ? err.code : `${err}`;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
async function want(p, expected, what) {
|
|
415
|
+
const got = await code(p);
|
|
416
|
+
if (got !== expected)
|
|
417
|
+
fail(`${what}: got ${got}, want ${expected}`);
|
|
418
|
+
}
|
|
419
|
+
async function publish(s, mediaType, content) {
|
|
420
|
+
const w = await s.create(mediaType);
|
|
421
|
+
await w.write(content);
|
|
422
|
+
return w.commit();
|
|
423
|
+
}
|
|
424
|
+
async function listAll(s, limit) {
|
|
425
|
+
const out = [];
|
|
426
|
+
let after = "";
|
|
427
|
+
for (;;) {
|
|
428
|
+
const page = await s.list(after, limit);
|
|
429
|
+
if (page.length > limit)
|
|
430
|
+
fail(`a list page of limit ${limit} held ${page.length}`);
|
|
431
|
+
out.push(...page);
|
|
432
|
+
if (page.length < limit)
|
|
433
|
+
return out;
|
|
434
|
+
after = page[page.length - 1].ref.id;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
async function listed(s) {
|
|
438
|
+
return new Map((await listAll(s, 100)).map((a) => [a.ref.id, a.ref]));
|
|
439
|
+
}
|
|
440
|
+
const same = (a, b) => a !== undefined &&
|
|
441
|
+
a.id === b.id &&
|
|
442
|
+
a.size === b.size &&
|
|
443
|
+
a.mediaType === b.mediaType &&
|
|
444
|
+
(a.sha256 ?? "") === (b.sha256 ?? "");
|
|
445
|
+
/** Opens a fresh store, runs fn on it and closes it. */
|
|
446
|
+
async function withStore(f, fn) {
|
|
447
|
+
const location = await f.location();
|
|
448
|
+
const s = await f.provider.open(location);
|
|
449
|
+
try {
|
|
450
|
+
await fn(s, location);
|
|
451
|
+
}
|
|
452
|
+
finally {
|
|
453
|
+
await s.close();
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
async function claimAs(f, location, o) {
|
|
457
|
+
const s = await f.provider.open(location);
|
|
458
|
+
try {
|
|
459
|
+
await s.claim(o);
|
|
460
|
+
}
|
|
461
|
+
finally {
|
|
462
|
+
await s.close();
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/** The shared artifact store cases this test client can run (of conformance.ArtifactCases). */
|
|
466
|
+
export const artifactCases = [
|
|
467
|
+
{
|
|
468
|
+
name: "commit_returns_size_media_type_and_digest",
|
|
469
|
+
check: (f) => withStore(f, async (s) => {
|
|
470
|
+
const ref = await publish(s, "text/plain", "hello artifact");
|
|
471
|
+
const content = "hello artifact";
|
|
472
|
+
if (ref.size !== content.length ||
|
|
473
|
+
ref.mediaType !== "text/plain" ||
|
|
474
|
+
ref.sha256 !== sha256(bytesOf(content)))
|
|
475
|
+
fail(`commit returned ${show(ref)}`);
|
|
476
|
+
const got = text(await s.read(ref));
|
|
477
|
+
if (got !== content)
|
|
478
|
+
fail(`read back ${show(got)}`);
|
|
479
|
+
}),
|
|
480
|
+
},
|
|
481
|
+
{
|
|
482
|
+
name: "abort_publishes_nothing",
|
|
483
|
+
check: (f) => withStore(f, async (s) => {
|
|
484
|
+
const w = await s.create("text/plain");
|
|
485
|
+
await w.write("abandoned");
|
|
486
|
+
await w.close();
|
|
487
|
+
if (!(await rejects(w.commit())))
|
|
488
|
+
fail("an aborted writer committed");
|
|
489
|
+
const ids = await listed(s);
|
|
490
|
+
if (ids.size !== 0)
|
|
491
|
+
fail(`an aborted writer's content was listed: ${show([...ids])}`);
|
|
492
|
+
}),
|
|
493
|
+
},
|
|
494
|
+
{
|
|
495
|
+
name: "uncommitted_content_is_never_read_or_listed",
|
|
496
|
+
check: (f) => withStore(f, async (s) => {
|
|
497
|
+
const w = await s.create("text/plain");
|
|
498
|
+
await w.write("half of it");
|
|
499
|
+
const other = await publish(s, "text/plain", "complete");
|
|
500
|
+
const ids = await listed(s);
|
|
501
|
+
if (ids.size !== 1 || !same(ids.get(other.id), other))
|
|
502
|
+
fail(`with one writer unfinished and one committed, listed ${show([...ids])}, want only ${other.id}`);
|
|
503
|
+
await w.write(", then the rest");
|
|
504
|
+
const ref = await w.commit();
|
|
505
|
+
const got = text(await s.read(ref));
|
|
506
|
+
if (got !== "half of it, then the rest")
|
|
507
|
+
fail(`read back ${show(got)}`);
|
|
508
|
+
}),
|
|
509
|
+
},
|
|
510
|
+
{
|
|
511
|
+
name: "commit_and_close_happen_once",
|
|
512
|
+
check: (f) => withStore(f, async (s) => {
|
|
513
|
+
const w = await s.create("text/plain");
|
|
514
|
+
await w.write("once");
|
|
515
|
+
const ref = await w.commit();
|
|
516
|
+
if (!(await rejects(w.commit())))
|
|
517
|
+
fail("a second commit succeeded");
|
|
518
|
+
if (!(await rejects(w.write("more"))))
|
|
519
|
+
fail("a write after commit succeeded");
|
|
520
|
+
if (!(await rejects(w.close())))
|
|
521
|
+
fail("a close after commit succeeded");
|
|
522
|
+
const got = text(await s.read(ref));
|
|
523
|
+
if (got !== "once")
|
|
524
|
+
fail(`after the extra calls, read ${show(got)}`);
|
|
525
|
+
}),
|
|
526
|
+
},
|
|
527
|
+
{
|
|
528
|
+
name: "open_refuses_a_ref_that_does_not_match",
|
|
529
|
+
check: (f) => withStore(f, async (s) => {
|
|
530
|
+
const ref = await publish(s, "text/plain", "checked");
|
|
531
|
+
for (const [what, wrong] of [
|
|
532
|
+
["size", { ...ref, size: ref.size + 1 }],
|
|
533
|
+
["media type", { ...ref, mediaType: "application/octet-stream" }],
|
|
534
|
+
["digest", { ...ref, sha256: sha256(bytesOf("something else")) }],
|
|
535
|
+
])
|
|
536
|
+
await want(s.read(wrong), ArtifactCode.Invalid, `open with another ${what}`);
|
|
537
|
+
const gone = await publish(s, "text/plain", "to delete");
|
|
538
|
+
await s.delete(gone);
|
|
539
|
+
await want(s.read(gone), ArtifactCode.NotFound, "open of deleted content");
|
|
540
|
+
const { sha256: _, ...legacy } = ref;
|
|
541
|
+
const got = text(await s.read(legacy));
|
|
542
|
+
if (got !== "checked")
|
|
543
|
+
fail(`open of a ref without a digest read ${show(got)}`);
|
|
544
|
+
}),
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
name: "committed_content_survives_reopen",
|
|
548
|
+
check: async (f) => {
|
|
549
|
+
const location = await f.location();
|
|
550
|
+
let s = await f.provider.open(location);
|
|
551
|
+
const ref = await publish(s, "text/plain", "durable");
|
|
552
|
+
const w = await s.create("text/plain");
|
|
553
|
+
await w.write("never committed");
|
|
554
|
+
await s.close();
|
|
555
|
+
s = await f.provider.open(location);
|
|
556
|
+
try {
|
|
557
|
+
const got = text(await s.read(ref));
|
|
558
|
+
if (got !== "durable")
|
|
559
|
+
fail(`after reopen read ${show(got)}`);
|
|
560
|
+
const ids = await listed(s);
|
|
561
|
+
if (ids.size !== 1 || !same(ids.get(ref.id), ref))
|
|
562
|
+
fail(`after reopen listed ${show([...ids])}, want only ${ref.id}`);
|
|
563
|
+
}
|
|
564
|
+
finally {
|
|
565
|
+
await s.close();
|
|
566
|
+
}
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
{
|
|
570
|
+
name: "delete_is_idempotent_and_checks_the_ref",
|
|
571
|
+
check: (f) => withStore(f, async (s) => {
|
|
572
|
+
const ref = await publish(s, "text/plain", "deleted twice");
|
|
573
|
+
await want(s.delete({ ...ref, sha256: sha256(bytesOf("not this")) }), ArtifactCode.Invalid, "delete with another digest");
|
|
574
|
+
const got = text(await s.read(ref));
|
|
575
|
+
if (got !== "deleted twice")
|
|
576
|
+
fail(`a refused delete removed content: read ${show(got)}`);
|
|
577
|
+
await s.delete(ref);
|
|
578
|
+
await s.delete(ref);
|
|
579
|
+
await want(s.read(ref), ArtifactCode.NotFound, "open after delete");
|
|
580
|
+
const ids = await listed(s);
|
|
581
|
+
if (ids.size !== 0)
|
|
582
|
+
fail(`deleted content listed: ${show([...ids])}`);
|
|
583
|
+
}),
|
|
584
|
+
},
|
|
585
|
+
{
|
|
586
|
+
name: "list_pages_committed_content_once_in_id_order",
|
|
587
|
+
check: (f) => withStore(f, async (s) => {
|
|
588
|
+
const committed = new Map();
|
|
589
|
+
for (let i = 0; i < 7; i++) {
|
|
590
|
+
const ref = await publish(s, "text/plain", "x".repeat(i));
|
|
591
|
+
committed.set(ref.id, ref);
|
|
592
|
+
}
|
|
593
|
+
for (const limit of [0, 1001])
|
|
594
|
+
await want(s.list("", limit), ArtifactCode.Invalid, `list with limit ${limit}`);
|
|
595
|
+
for (const limit of [1, 3, 1000]) {
|
|
596
|
+
const all = await listAll(s, limit);
|
|
597
|
+
if (all.length !== committed.size)
|
|
598
|
+
fail(`with limit ${limit} listed ${all.length} artifacts, want ${committed.size}`);
|
|
599
|
+
all.forEach((a, i) => {
|
|
600
|
+
if (i > 0 &&
|
|
601
|
+
!(Buffer.compare(Buffer.from(a.ref.id), Buffer.from(all[i - 1].ref.id)) > 0))
|
|
602
|
+
fail(`with limit ${limit} listed ${a.ref.id} after ${all[i - 1].ref.id}: not in ID order`);
|
|
603
|
+
if (!same(committed.get(a.ref.id), a.ref))
|
|
604
|
+
fail(`listed ${show(a.ref)}, committed ${show(committed.get(a.ref.id))}`);
|
|
605
|
+
if (Number.isNaN(a.publishedAt.getTime()) ||
|
|
606
|
+
a.publishedAt.getTime() <= 0)
|
|
607
|
+
fail(`listed ${a.ref.id} without a publication time`);
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
}),
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
name: "import_publishes_exactly_the_ref_or_nothing",
|
|
614
|
+
check: async (f) => {
|
|
615
|
+
let ref;
|
|
616
|
+
let legacy;
|
|
617
|
+
await withStore(f, async (source) => {
|
|
618
|
+
ref = await publish(source, "text/plain", "restored");
|
|
619
|
+
const { sha256: _, ...old } = await publish(source, "text/plain", "from before digests");
|
|
620
|
+
legacy = old;
|
|
621
|
+
});
|
|
622
|
+
await withStore(f, async (target) => {
|
|
623
|
+
for (const [what, content] of [
|
|
624
|
+
["a short source", "restore"],
|
|
625
|
+
["a long source", "restored!"],
|
|
626
|
+
["another digest", "restorex"],
|
|
627
|
+
])
|
|
628
|
+
await want(target.import(ref, content), ArtifactCode.Corrupt, `import of ${what}`);
|
|
629
|
+
let ids = await listed(target);
|
|
630
|
+
if (ids.size !== 0)
|
|
631
|
+
fail(`failed imports left content listed: ${show([...ids])}`);
|
|
632
|
+
await want(target.read(ref), ArtifactCode.NotFound, "failed imports left content readable");
|
|
633
|
+
await target.import(ref, "restored");
|
|
634
|
+
await want(target.import(ref, "restored"), ArtifactCode.Conflict, "import of an ID the store holds");
|
|
635
|
+
const got = text(await target.read(ref));
|
|
636
|
+
if (got !== "restored")
|
|
637
|
+
fail(`imported content read ${show(got)}`);
|
|
638
|
+
await target.import(legacy, "from before digests");
|
|
639
|
+
ids = await listed(target);
|
|
640
|
+
const full = {
|
|
641
|
+
...legacy,
|
|
642
|
+
sha256: sha256(bytesOf("from before digests")),
|
|
643
|
+
};
|
|
644
|
+
if (!same(ids.get(ref.id), ref) || !same(ids.get(legacy.id), full))
|
|
645
|
+
fail(`after import listed ${show([...ids])}, want ${show(ref)} and ${show(full)}`);
|
|
646
|
+
});
|
|
647
|
+
},
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
name: "identical_content_gets_a_new_id",
|
|
651
|
+
check: (f) => withStore(f, async (s) => {
|
|
652
|
+
const a = await publish(s, "text/plain", "same bytes");
|
|
653
|
+
const b = await publish(s, "text/plain", "same bytes");
|
|
654
|
+
if (a.id === b.id)
|
|
655
|
+
fail(`two commits of the same bytes share the ID ${a.id}`);
|
|
656
|
+
await s.delete(a);
|
|
657
|
+
const got = text(await s.read(b));
|
|
658
|
+
if (got !== "same bytes")
|
|
659
|
+
fail(`after deleting its twin, read ${show(got)}`);
|
|
660
|
+
}),
|
|
661
|
+
},
|
|
662
|
+
{
|
|
663
|
+
name: "a_claimed_location_refuses_another_installation",
|
|
664
|
+
check: async (f) => {
|
|
665
|
+
const location = await f.location();
|
|
666
|
+
let ref;
|
|
667
|
+
const s = await f.provider.open(location);
|
|
668
|
+
try {
|
|
669
|
+
ref = await publish(s, "text/plain", "the first installation's");
|
|
670
|
+
}
|
|
671
|
+
finally {
|
|
672
|
+
await s.close();
|
|
673
|
+
}
|
|
674
|
+
await claimAs(f, location, { installation: firstInstallation });
|
|
675
|
+
await claimAs(f, location, { installation: firstInstallation }).catch((err) => fail(`the same installation claimed again, as a standby would: ${err}`));
|
|
676
|
+
await want(claimAs(f, location, { installation: secondInstallation }), ArtifactCode.OwnedElsewhere, "another installation's claim on a claimed location");
|
|
677
|
+
const x = await f.provider.open(location);
|
|
678
|
+
try {
|
|
679
|
+
const got = text(await x.read(ref));
|
|
680
|
+
if (got !== "the first installation's")
|
|
681
|
+
fail(`the first installation's content reads ${show(got)}`);
|
|
682
|
+
await want(x.checkOwner(secondInstallation), ArtifactCode.OwnedElsewhere, "checkOwner for another installation");
|
|
683
|
+
await want(x.checkOwner(""), ArtifactCode.OwnedElsewhere, "checkOwner for no installation on a claimed location");
|
|
684
|
+
await x.checkOwner(firstInstallation);
|
|
685
|
+
await want(x.claim({ installation: "" }), ArtifactCode.Invalid, "a claim naming no installation");
|
|
686
|
+
}
|
|
687
|
+
finally {
|
|
688
|
+
await x.close();
|
|
689
|
+
}
|
|
690
|
+
},
|
|
691
|
+
},
|
|
692
|
+
{
|
|
693
|
+
name: "a_forced_take_over_replaces_the_claim",
|
|
694
|
+
check: async (f) => {
|
|
695
|
+
const location = await f.location();
|
|
696
|
+
const x = await f.provider.open(location);
|
|
697
|
+
try {
|
|
698
|
+
await x.checkOwner("");
|
|
699
|
+
}
|
|
700
|
+
finally {
|
|
701
|
+
await x.close();
|
|
702
|
+
}
|
|
703
|
+
await claimAs(f, location, { installation: firstInstallation });
|
|
704
|
+
await claimAs(f, location, {
|
|
705
|
+
installation: secondInstallation,
|
|
706
|
+
takeOver: true,
|
|
707
|
+
});
|
|
708
|
+
await want(claimAs(f, location, { installation: firstInstallation }), ArtifactCode.OwnedElsewhere, "the former owner after a takeover");
|
|
709
|
+
await claimAs(f, location, { installation: secondInstallation });
|
|
710
|
+
},
|
|
711
|
+
},
|
|
712
|
+
{
|
|
713
|
+
name: "a_nested_location_is_refused_to_another_installation",
|
|
714
|
+
check: async (f) => {
|
|
715
|
+
if (f.nested === undefined)
|
|
716
|
+
return;
|
|
717
|
+
let outer = await f.location();
|
|
718
|
+
await claimAs(f, outer, { installation: firstInstallation });
|
|
719
|
+
for (const takeOver of [false, true])
|
|
720
|
+
await want(claimAs(f, f.nested(outer), {
|
|
721
|
+
installation: secondInstallation,
|
|
722
|
+
takeOver,
|
|
723
|
+
}), ArtifactCode.OwnedElsewhere, `a location inside another installation's (take over ${takeOver})`);
|
|
724
|
+
outer = await f.location();
|
|
725
|
+
await claimAs(f, f.nested(outer), { installation: secondInstallation });
|
|
726
|
+
await want(claimAs(f, outer, { installation: firstInstallation }), ArtifactCode.OwnedElsewhere, "a location holding another installation's");
|
|
727
|
+
},
|
|
728
|
+
},
|
|
729
|
+
];
|
|
730
|
+
/** Runs every artifact case, rejecting with the first a store breaks (conformance.RunArtifacts). */
|
|
731
|
+
export async function checkArtifactStore(f) {
|
|
732
|
+
for (const c of artifactCases) {
|
|
733
|
+
try {
|
|
734
|
+
await c.check(f);
|
|
735
|
+
}
|
|
736
|
+
catch (err) {
|
|
737
|
+
throw new Error(`${c.name}: ${err.message}`);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|