@milaboratories/pl-middle-layer 2.10.0 → 2.10.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/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/middle_layer/build_stamp.cjs +1 -1
- package/dist/middle_layer/build_stamp.js +1 -1
- package/dist/middle_layer/middle_layer.cjs +4 -3
- package/dist/middle_layer/middle_layer.cjs.map +1 -1
- package/dist/middle_layer/middle_layer.d.ts.map +1 -1
- package/dist/middle_layer/middle_layer.js +3 -2
- package/dist/middle_layer/middle_layer.js.map +1 -1
- package/dist/middle_layer/project.cjs +6 -21
- package/dist/middle_layer/project.cjs.map +1 -1
- package/dist/middle_layer/project.d.ts +1 -1
- package/dist/middle_layer/project.d.ts.map +1 -1
- package/dist/middle_layer/project.js +5 -19
- package/dist/middle_layer/project.js.map +1 -1
- package/dist/model/block_pack_spec.cjs.map +1 -1
- package/dist/model/block_pack_spec.d.ts +3 -3
- package/dist/model/block_pack_spec.d.ts.map +1 -1
- package/dist/model/block_pack_spec.js.map +1 -1
- package/dist/model/template_spec.d.ts +18 -0
- package/dist/model/template_spec.d.ts.map +1 -1
- package/dist/mutator/block-pack/block_pack.cjs +89 -2
- package/dist/mutator/block-pack/block_pack.cjs.map +1 -1
- package/dist/mutator/block-pack/block_pack.d.ts +16 -0
- package/dist/mutator/block-pack/block_pack.d.ts.map +1 -1
- package/dist/mutator/block-pack/block_pack.js +90 -4
- package/dist/mutator/block-pack/block_pack.js.map +1 -1
- package/dist/mutator/block-pack/required_capabilities.cjs +16 -0
- package/dist/mutator/block-pack/required_capabilities.cjs.map +1 -1
- package/dist/mutator/block-pack/required_capabilities.js +16 -1
- package/dist/mutator/block-pack/required_capabilities.js.map +1 -1
- package/dist/mutator/template/template_cache.cjs +8 -1
- package/dist/mutator/template/template_cache.cjs.map +1 -1
- package/dist/mutator/template/template_cache.d.ts +7 -3
- package/dist/mutator/template/template_cache.d.ts.map +1 -1
- package/dist/mutator/template/template_cache.js +8 -1
- package/dist/mutator/template/template_cache.js.map +1 -1
- package/package.json +2 -2
- package/src/middle_layer/middle_layer.ts +4 -2
- package/src/middle_layer/project.ts +7 -27
- package/src/model/block_pack_spec.ts +3 -2
- package/src/model/template_spec.ts +21 -0
- package/src/mutator/block-pack/block_pack.test.ts +10 -1
- package/src/mutator/block-pack/block_pack.ts +102 -2
- package/src/mutator/block-pack/block_pack_load.test.ts +52 -0
- package/src/mutator/block-pack/required_capabilities.ts +25 -0
- package/src/mutator/project-v3.test.ts +27 -16
- package/src/mutator/template/template_cache.ts +26 -3
- package/src/test/block_packs.ts +12 -0
|
@@ -10,6 +10,8 @@ export interface TemplateFromRegistry {
|
|
|
10
10
|
export interface ExplicitTemplate {
|
|
11
11
|
readonly type: "explicit";
|
|
12
12
|
content: Uint8Array;
|
|
13
|
+
/** The pack file `content` was read from, for local dev blocks. */
|
|
14
|
+
localPath?: string;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
export interface PreparedTemplate {
|
|
@@ -17,6 +19,25 @@ export interface PreparedTemplate {
|
|
|
17
19
|
data: TemplateData | CompiledTemplateV3;
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
/** A registry pack not yet downloaded: it is fetched from `url` when installed. */
|
|
23
|
+
export interface RegistryPackTemplate {
|
|
24
|
+
readonly type: "registry-pack";
|
|
25
|
+
url: string;
|
|
26
|
+
/** Lowercase hex sha256 of the gzipped pack, from the registry manifest. */
|
|
27
|
+
sha256: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A local dev pack: it is read from `path` when installed. */
|
|
31
|
+
export interface LocalPackTemplate {
|
|
32
|
+
readonly type: "local-pack";
|
|
33
|
+
path: string;
|
|
34
|
+
/** Lowercase hex sha256 of the gzipped pack file. */
|
|
35
|
+
sha256: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A pack not yet loaded: `cacheBlockPackTemplate` installs it. */
|
|
39
|
+
export type InstallablePackTemplate = RegistryPackTemplate | LocalPackTemplate;
|
|
40
|
+
|
|
20
41
|
export interface CachedTemplate {
|
|
21
42
|
readonly type: "cached";
|
|
22
43
|
readonly resourceId: SignedResourceId;
|
|
@@ -10,6 +10,7 @@ import path from "node:path";
|
|
|
10
10
|
import { expect, test } from "vitest";
|
|
11
11
|
import { V2RegistryProvider } from "../../block_registry/registry-v2-provider";
|
|
12
12
|
import { BlockPackSpecAny } from "../../model";
|
|
13
|
+
import { cacheBlockPackTemplate } from "../template/template_cache";
|
|
13
14
|
import { BlockPackPreparer, createBlockPack } from "./block_pack";
|
|
14
15
|
|
|
15
16
|
const preparation = new BlockPackPreparer(
|
|
@@ -29,6 +30,13 @@ test.each([
|
|
|
29
30
|
},
|
|
30
31
|
} as BlockPackSpecAny,
|
|
31
32
|
},
|
|
33
|
+
{
|
|
34
|
+
spec: {
|
|
35
|
+
type: "from-registry-v2",
|
|
36
|
+
registryUrl: "https://blocks.pl-open.science",
|
|
37
|
+
id: { organization: "milaboratories", name: "gpu-test", version: "0.9.0" },
|
|
38
|
+
} as BlockPackSpecAny,
|
|
39
|
+
},
|
|
32
40
|
{
|
|
33
41
|
spec: {
|
|
34
42
|
type: "dev-v1",
|
|
@@ -43,11 +51,12 @@ test.each([
|
|
|
43
51
|
const specPrepared = await preparation.prepare(spec);
|
|
44
52
|
|
|
45
53
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
54
|
+
const cachedBp = await cacheBlockPackTemplate(pl, specPrepared, { preparer: preparation });
|
|
46
55
|
const f0 = { resourceId: pl.clientRoot, fieldName: "test0" };
|
|
47
56
|
|
|
48
57
|
const bp = await pl.withWriteTx("test", async (tx) => {
|
|
49
58
|
tx.createField(f0, "Dynamic");
|
|
50
|
-
const bp = createBlockPack(tx,
|
|
59
|
+
const bp = createBlockPack(tx, cachedBp);
|
|
51
60
|
tx.setField(f0, bp);
|
|
52
61
|
await tx.commit();
|
|
53
62
|
return await toGlobalResourceId(bp);
|
|
@@ -3,9 +3,10 @@ import { field } from "@milaboratories/pl-client";
|
|
|
3
3
|
import { loadTemplate } from "../template/template_loading";
|
|
4
4
|
import type { BlockPackExplicit, BlockPackSpecAny, BlockPackSpecPrepared } from "../../model";
|
|
5
5
|
import type { Signer } from "@milaboratories/ts-helpers";
|
|
6
|
-
import { assertNever } from "@milaboratories/ts-helpers";
|
|
6
|
+
import { assertNever, notEmpty } from "@milaboratories/ts-helpers";
|
|
7
7
|
import type { Branded } from "@milaboratories/pl-model-common";
|
|
8
8
|
import fs from "node:fs";
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
9
10
|
import { fileURLToPath } from "node:url";
|
|
10
11
|
import type { Dispatcher } from "undici";
|
|
11
12
|
import { request } from "undici";
|
|
@@ -19,12 +20,13 @@ import {
|
|
|
19
20
|
RegistryV1,
|
|
20
21
|
} from "@platforma-sdk/block-tools";
|
|
21
22
|
import type { BlockPackInfo } from "../../model/block_pack";
|
|
23
|
+
import type { InstallablePackTemplate, PreparedTemplate } from "../../model/template_spec";
|
|
22
24
|
import { resolveDevPacket } from "../../dev_env";
|
|
23
25
|
import { getDevV2PacketMtime, getFromPackV2Mtime } from "../../block_registry";
|
|
24
26
|
import type { V2RegistryProvider } from "../../block_registry/registry-v2-provider";
|
|
25
27
|
import { LRUCache } from "lru-cache";
|
|
26
28
|
import canonicalize from "canonicalize";
|
|
27
|
-
import type { BlockPackSpec } from "@milaboratories/pl-model-middle-layer";
|
|
29
|
+
import type { BlockPackFromRegistryV2, BlockPackSpec } from "@milaboratories/pl-model-middle-layer";
|
|
28
30
|
import { WorkerManager } from "../../worker/WorkerManager";
|
|
29
31
|
import { z } from "zod";
|
|
30
32
|
|
|
@@ -75,6 +77,9 @@ export class BlockPackPreparer {
|
|
|
75
77
|
sizeCalculation: (value) => value.byteLength,
|
|
76
78
|
});
|
|
77
79
|
|
|
80
|
+
/** Parsed packs by sha256, so adding a block again does not parse its workflow again. */
|
|
81
|
+
private readonly loadedPackCache = new LRUCache<string, LoadedPack>({ max: 50 });
|
|
82
|
+
|
|
78
83
|
/** Cache of prepared block packs for registry specs (immutable by version). */
|
|
79
84
|
private readonly preparedCache = new LRUCache<PreparedCacheKey, BlockPackSpecPrepared>({
|
|
80
85
|
max: 50,
|
|
@@ -156,8 +161,24 @@ export class BlockPackPreparer {
|
|
|
156
161
|
if (cached) return cached;
|
|
157
162
|
}
|
|
158
163
|
|
|
164
|
+
const registryPack =
|
|
165
|
+
spec.type === "from-registry-v2" ? await this.prepareRegistryPack(spec) : undefined;
|
|
166
|
+
if (registryPack !== undefined) {
|
|
167
|
+
if (key) this.preparedCache.set(key, registryPack);
|
|
168
|
+
return registryPack;
|
|
169
|
+
}
|
|
170
|
+
|
|
159
171
|
const explicit = await this.prepareWithoutUnpacking(spec);
|
|
160
172
|
|
|
173
|
+
// Local dev packs are parsed when installed, which also checks their required capabilities.
|
|
174
|
+
const { localPath, content } = explicit.template;
|
|
175
|
+
if (localPath !== undefined)
|
|
176
|
+
return {
|
|
177
|
+
...explicit,
|
|
178
|
+
type: "prepared",
|
|
179
|
+
template: { type: "local-pack", path: localPath, sha256: packSha256(content) },
|
|
180
|
+
};
|
|
181
|
+
|
|
161
182
|
await using workerManager = new WorkerManager();
|
|
162
183
|
|
|
163
184
|
const parsed = await workerManager.process("parseTemplate", explicit.template.content);
|
|
@@ -179,6 +200,73 @@ export class BlockPackPreparer {
|
|
|
179
200
|
return result;
|
|
180
201
|
}
|
|
181
202
|
|
|
203
|
+
/**
|
|
204
|
+
* Prepares a v2 registry block from its manifest alone, without downloading the workflow.
|
|
205
|
+
* Undefined when the manifest does not list the workflow pack's sha256.
|
|
206
|
+
*/
|
|
207
|
+
private async prepareRegistryPack(
|
|
208
|
+
spec: BlockPackFromRegistryV2,
|
|
209
|
+
): Promise<BlockPackSpecPrepared | undefined> {
|
|
210
|
+
const registry = this.v2RegistryProvider.getRegistry(spec.registryUrl);
|
|
211
|
+
const [manifest, components] = await Promise.all([
|
|
212
|
+
registry.getManifest(spec.id),
|
|
213
|
+
registry.getComponents(spec.id),
|
|
214
|
+
]);
|
|
215
|
+
const workflowPath = manifest.description.components.workflow.main.path;
|
|
216
|
+
const workflowFile = manifest.files.find((f) => f.name === workflowPath);
|
|
217
|
+
if (workflowFile === undefined) return undefined;
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
type: "prepared",
|
|
221
|
+
template: {
|
|
222
|
+
type: "registry-pack",
|
|
223
|
+
url: components.workflow.main.url,
|
|
224
|
+
sha256: workflowFile.sha256.toLowerCase(),
|
|
225
|
+
},
|
|
226
|
+
config: parseBufferConfig(await this.remoteContentCache.forceFetch(components.model.url)),
|
|
227
|
+
frontend: { type: "url", url: components.ui.url },
|
|
228
|
+
source: spec,
|
|
229
|
+
requiredCapabilities: manifest.description.meta.requiredCapabilities,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Downloads or reads a pack, checks its sha256 and parses it. */
|
|
234
|
+
public async loadPack(template: InstallablePackTemplate): Promise<LoadedPack> {
|
|
235
|
+
let content: Uint8Array | undefined;
|
|
236
|
+
if (template.type === "local-pack") {
|
|
237
|
+
// A rebuild after prepare would pair this config with another build's workflow.
|
|
238
|
+
content = await fs.promises.readFile(template.path);
|
|
239
|
+
if (packSha256(content) !== template.sha256)
|
|
240
|
+
throw new Error(
|
|
241
|
+
`workflow pack ${template.path} changed after the block was prepared; add the block again`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const cached = this.loadedPackCache.get(template.sha256);
|
|
246
|
+
if (cached !== undefined) return cached;
|
|
247
|
+
|
|
248
|
+
if (content === undefined && template.type === "registry-pack") {
|
|
249
|
+
content = new Uint8Array(await this.remoteContentCache.forceFetch(template.url));
|
|
250
|
+
const sha256 = packSha256(content);
|
|
251
|
+
if (sha256 !== template.sha256) {
|
|
252
|
+
// Do not keep a broken download for the next attempt.
|
|
253
|
+
this.remoteContentCache.delete(template.url);
|
|
254
|
+
throw new Error(
|
|
255
|
+
`workflow pack ${template.url} has sha256 ${sha256}, expected ${template.sha256}`,
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
await using workerManager = new WorkerManager();
|
|
261
|
+
const parsed = await workerManager.process("parseTemplate", notEmpty(content));
|
|
262
|
+
const loaded: LoadedPack = {
|
|
263
|
+
template: { type: "prepared", data: parsed },
|
|
264
|
+
requiredCapabilities: requiredCapabilitiesFromTemplate(parsed),
|
|
265
|
+
};
|
|
266
|
+
this.loadedPackCache.set(template.sha256, loaded);
|
|
267
|
+
return loaded;
|
|
268
|
+
}
|
|
269
|
+
|
|
182
270
|
private async prepareWithoutUnpacking(
|
|
183
271
|
spec: BlockPackExplicit | BlockPackSpec,
|
|
184
272
|
): Promise<BlockPackExplicit> {
|
|
@@ -234,6 +322,7 @@ export class BlockPackPreparer {
|
|
|
234
322
|
template: {
|
|
235
323
|
type: "explicit",
|
|
236
324
|
content: workflowContent,
|
|
325
|
+
localPath: description.components.workflow.main.file,
|
|
237
326
|
},
|
|
238
327
|
config,
|
|
239
328
|
frontend: {
|
|
@@ -276,6 +365,7 @@ export class BlockPackPreparer {
|
|
|
276
365
|
template: {
|
|
277
366
|
type: "explicit",
|
|
278
367
|
content: workflowContent,
|
|
368
|
+
localPath: description.components.workflow.main.file,
|
|
279
369
|
},
|
|
280
370
|
config,
|
|
281
371
|
frontend: {
|
|
@@ -349,7 +439,17 @@ export class BlockPackPreparer {
|
|
|
349
439
|
}
|
|
350
440
|
}
|
|
351
441
|
|
|
442
|
+
/** A pack parsed on the client, with the capabilities it declares. */
|
|
443
|
+
type LoadedPack = { template: PreparedTemplate; requiredCapabilities?: string[] };
|
|
444
|
+
|
|
445
|
+
/** Lowercase hex sha256 of a gzipped workflow pack, the key the backend installs it by. */
|
|
446
|
+
export function packSha256(content: Uint8Array): string {
|
|
447
|
+
return createHash("sha256").update(content).digest("hex");
|
|
448
|
+
}
|
|
449
|
+
|
|
352
450
|
function createCustomBlockPack(tx: PlTransaction, spec: BlockPackSpecPrepared): AnyResourceRef {
|
|
451
|
+
if (spec.template.type === "registry-pack" || spec.template.type === "local-pack")
|
|
452
|
+
throw new Error("BUG: a pack must be installed by cacheBlockPackTemplate first");
|
|
353
453
|
const blockPackInfo: BlockPackInfo = { config: spec.config, source: spec.source };
|
|
354
454
|
const bp = tx.createStruct(BlockPackCustomType, JSON.stringify(blockPackInfo));
|
|
355
455
|
tx.createField(field(bp, BlockPackTemplateField), "Input", loadTemplate(tx, spec.template));
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { defaultHttpDispatcher } from "@milaboratories/pl-http";
|
|
2
|
+
import { HmacSha256Signer } from "@milaboratories/ts-helpers";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import http from "node:http";
|
|
5
|
+
import type { AddressInfo } from "node:net";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { expect, test } from "vitest";
|
|
9
|
+
import { V2RegistryProvider } from "../../block_registry/registry-v2-provider";
|
|
10
|
+
import { BlockPackPreparer, packSha256 } from "./block_pack";
|
|
11
|
+
|
|
12
|
+
const newPreparer = () =>
|
|
13
|
+
new BlockPackPreparer(
|
|
14
|
+
new V2RegistryProvider(defaultHttpDispatcher()),
|
|
15
|
+
new HmacSha256Signer(HmacSha256Signer.generateSecret()),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
test("loadPack drops a download with the wrong sha256, so the next attempt fetches again", async () => {
|
|
19
|
+
let requests = 0;
|
|
20
|
+
const server = http.createServer((_req, res) => {
|
|
21
|
+
requests++;
|
|
22
|
+
res.end("not the pack");
|
|
23
|
+
});
|
|
24
|
+
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
25
|
+
try {
|
|
26
|
+
const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/main.plj.gz`;
|
|
27
|
+
const preparer = newPreparer();
|
|
28
|
+
const template = { type: "registry-pack" as const, url, sha256: "0".repeat(64) };
|
|
29
|
+
|
|
30
|
+
await expect(preparer.loadPack(template)).rejects.toThrow(/expected 0{64}/);
|
|
31
|
+
await expect(preparer.loadPack(template)).rejects.toThrow(/expected 0{64}/);
|
|
32
|
+
expect(requests).toBe(2);
|
|
33
|
+
} finally {
|
|
34
|
+
server.close();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("loadPack refuses a local pack rebuilt after prepare", async () => {
|
|
39
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "pack-"));
|
|
40
|
+
try {
|
|
41
|
+
const packPath = path.join(dir, "main.plj.gz");
|
|
42
|
+
await fs.writeFile(packPath, "build 1");
|
|
43
|
+
const sha256 = packSha256(new TextEncoder().encode("build 1"));
|
|
44
|
+
await fs.writeFile(packPath, "build 2");
|
|
45
|
+
|
|
46
|
+
await expect(
|
|
47
|
+
newPreparer().loadPack({ type: "local-pack", path: packPath, sha256 }),
|
|
48
|
+
).rejects.toThrow(/changed after the block was prepared/);
|
|
49
|
+
} finally {
|
|
50
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
51
|
+
}
|
|
52
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { gunzipSync } from "node:zlib";
|
|
2
2
|
import type { CompiledTemplateV3, TemplateData } from "@milaboratories/pl-model-backend";
|
|
3
|
+
import type { PlClient } from "@milaboratories/pl-client";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Reads the capability tokens a template declares it requires via
|
|
@@ -42,3 +43,27 @@ export function deriveRequiredCapabilities(
|
|
|
42
43
|
}
|
|
43
44
|
return requiredCapabilitiesFromTemplate(parsed as TemplateData | CompiledTemplateV3);
|
|
44
45
|
}
|
|
46
|
+
|
|
47
|
+
/** Throws when the connected backend doesn't advertise every capability the
|
|
48
|
+
* block-pack requires (as declared in `meta.requiredCapabilities`).
|
|
49
|
+
*
|
|
50
|
+
* The matching set is the desktop's install gate moved server-side so both
|
|
51
|
+
* the UI (`AddBlockModal`) and direct middle-layer callers (the MCP server's
|
|
52
|
+
* `add_block` tool, programmatic block installs, tests) reject incompatible
|
|
53
|
+
* blocks consistently. */
|
|
54
|
+
export function throwIfMissingServerCapabilities(
|
|
55
|
+
pl: PlClient,
|
|
56
|
+
requiredCapabilities: readonly string[] | undefined,
|
|
57
|
+
): void {
|
|
58
|
+
if (!requiredCapabilities || requiredCapabilities.length === 0) return;
|
|
59
|
+
const advertised = (pl.serverInfo.capabilities ?? []) as readonly string[];
|
|
60
|
+
const filterFn = (c: string) => !advertised.includes(c);
|
|
61
|
+
if (!requiredCapabilities.some(filterFn)) return;
|
|
62
|
+
const missing = requiredCapabilities.filter(filterFn);
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Block cannot be added: connected backend does not advertise capabilities ` +
|
|
65
|
+
`${JSON.stringify(missing)}. ` +
|
|
66
|
+
`Backend advertises ${JSON.stringify(advertised)}. ` +
|
|
67
|
+
`Upgrade the backend or remove the requirement from the block manifest.`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
@@ -6,7 +6,7 @@ import { outputRef } from "../model/args";
|
|
|
6
6
|
import { argsFingerprint } from "../model/args_derivation";
|
|
7
7
|
import { ProjectHelper } from "../model/project_helper";
|
|
8
8
|
import { blockArgsAuthorKey, projectFieldName } from "../model/project_model";
|
|
9
|
-
import {
|
|
9
|
+
import { prepareInstalled } from "../test/block_packs";
|
|
10
10
|
import { createProject, ProjectMutator } from "./project";
|
|
11
11
|
import type { AuthorMarker, BlockPackSpec } from "@milaboratories/pl-model-middle-layer";
|
|
12
12
|
import { createBlockStorage } from "@platforma-sdk/model";
|
|
@@ -31,6 +31,9 @@ test("v3 blocks: basic test with unified state", async () => {
|
|
|
31
31
|
const quickJs = await getQuickJS();
|
|
32
32
|
|
|
33
33
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
34
|
+
// Installed before any transaction opens: an open transaction cannot see it.
|
|
35
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
36
|
+
const installedBPSpecSumV3 = await prepareInstalled(pl, BPSpecSumV3);
|
|
34
37
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
35
38
|
const prjRef = await createProject(tx);
|
|
36
39
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -46,7 +49,7 @@ test("v3 blocks: basic test with unified state", async () => {
|
|
|
46
49
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
47
50
|
{
|
|
48
51
|
storageMode: "fromModel",
|
|
49
|
-
blockPack:
|
|
52
|
+
blockPack: installedBPSpecEnterV3,
|
|
50
53
|
},
|
|
51
54
|
);
|
|
52
55
|
// Set initial test data
|
|
@@ -79,7 +82,7 @@ test("v3 blocks: basic test with unified state", async () => {
|
|
|
79
82
|
{ id: "enter2", label: "Enter Numbers V3 #2", renderingMode: "Heavy" },
|
|
80
83
|
{
|
|
81
84
|
storageMode: "fromModel",
|
|
82
|
-
blockPack:
|
|
85
|
+
blockPack: installedBPSpecEnterV3,
|
|
83
86
|
},
|
|
84
87
|
);
|
|
85
88
|
mut.setStates([
|
|
@@ -104,7 +107,7 @@ test("v3 blocks: basic test with unified state", async () => {
|
|
|
104
107
|
{ id: "sum1", label: "Sum Numbers V3", renderingMode: "Heavy" },
|
|
105
108
|
{
|
|
106
109
|
storageMode: "fromModel",
|
|
107
|
-
blockPack:
|
|
110
|
+
blockPack: installedBPSpecSumV3,
|
|
108
111
|
},
|
|
109
112
|
);
|
|
110
113
|
// Set sources to reference upstream blocks
|
|
@@ -164,6 +167,7 @@ test("v3 blocks: prerunArgs skip test", async () => {
|
|
|
164
167
|
const quickJs = await getQuickJS();
|
|
165
168
|
|
|
166
169
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
170
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
167
171
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
168
172
|
const prjRef = await createProject(tx);
|
|
169
173
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -178,7 +182,7 @@ test("v3 blocks: prerunArgs skip test", async () => {
|
|
|
178
182
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
179
183
|
{
|
|
180
184
|
storageMode: "fromModel",
|
|
181
|
-
blockPack:
|
|
185
|
+
blockPack: installedBPSpecEnterV3,
|
|
182
186
|
},
|
|
183
187
|
);
|
|
184
188
|
// Set initial state: [3, 1, 2] - has one even number (2)
|
|
@@ -293,6 +297,7 @@ test("v3 blocks: migrateBlockPack preserves state and re-derives args and prerun
|
|
|
293
297
|
const quickJs = await getQuickJS();
|
|
294
298
|
|
|
295
299
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
300
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
296
301
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
297
302
|
const prjRef = await createProject(tx);
|
|
298
303
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -307,7 +312,7 @@ test("v3 blocks: migrateBlockPack preserves state and re-derives args and prerun
|
|
|
307
312
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
308
313
|
{
|
|
309
314
|
storageMode: "fromModel",
|
|
310
|
-
blockPack:
|
|
315
|
+
blockPack: installedBPSpecEnterV3,
|
|
311
316
|
},
|
|
312
317
|
);
|
|
313
318
|
mut.setStates([
|
|
@@ -337,7 +342,7 @@ test("v3 blocks: migrateBlockPack preserves state and re-derives args and prerun
|
|
|
337
342
|
// Call migrateBlockPack without newClearState (state-preserved path)
|
|
338
343
|
await pl.withWriteTx("MigrateBlockPack", async (tx) => {
|
|
339
344
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
340
|
-
mut.migrateBlockPack("enter1",
|
|
345
|
+
mut.migrateBlockPack("enter1", installedBPSpecEnterV3);
|
|
341
346
|
mut.save();
|
|
342
347
|
await tx.commit();
|
|
343
348
|
});
|
|
@@ -365,6 +370,7 @@ test("v3 blocks: migrateBlockPack with storage migration re-derives args and pre
|
|
|
365
370
|
const quickJs = await getQuickJS();
|
|
366
371
|
|
|
367
372
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
373
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
368
374
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
369
375
|
const prjRef = await createProject(tx);
|
|
370
376
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -379,7 +385,7 @@ test("v3 blocks: migrateBlockPack with storage migration re-derives args and pre
|
|
|
379
385
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
380
386
|
{
|
|
381
387
|
storageMode: "fromModel",
|
|
382
|
-
blockPack:
|
|
388
|
+
blockPack: installedBPSpecEnterV3,
|
|
383
389
|
},
|
|
384
390
|
);
|
|
385
391
|
mut.setStates([
|
|
@@ -405,7 +411,7 @@ test("v3 blocks: migrateBlockPack with storage migration re-derives args and pre
|
|
|
405
411
|
// Call migrateBlockPack (state-preserved) — triggers v1→v2→v3 migration
|
|
406
412
|
await pl.withWriteTx("MigrateBlockPack", async (tx) => {
|
|
407
413
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
408
|
-
mut.migrateBlockPack("enter1",
|
|
414
|
+
mut.migrateBlockPack("enter1", installedBPSpecEnterV3);
|
|
409
415
|
mut.save();
|
|
410
416
|
await tx.commit();
|
|
411
417
|
});
|
|
@@ -441,6 +447,7 @@ test("v3 blocks: resetToInitialStorage derives prerunArgs even when args() throw
|
|
|
441
447
|
const quickJs = await getQuickJS();
|
|
442
448
|
|
|
443
449
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
450
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
444
451
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
445
452
|
const prjRef = await createProject(tx);
|
|
446
453
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -455,7 +462,7 @@ test("v3 blocks: resetToInitialStorage derives prerunArgs even when args() throw
|
|
|
455
462
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
456
463
|
{
|
|
457
464
|
storageMode: "fromModel",
|
|
458
|
-
blockPack:
|
|
465
|
+
blockPack: installedBPSpecEnterV3,
|
|
459
466
|
},
|
|
460
467
|
);
|
|
461
468
|
mut.setStates([
|
|
@@ -512,6 +519,7 @@ test("v3 blocks: migrateBlockPack assigns author marker", async () => {
|
|
|
512
519
|
const quickJs = await getQuickJS();
|
|
513
520
|
|
|
514
521
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
522
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
515
523
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
516
524
|
const prjRef = await createProject(tx);
|
|
517
525
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -526,7 +534,7 @@ test("v3 blocks: migrateBlockPack assigns author marker", async () => {
|
|
|
526
534
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
527
535
|
{
|
|
528
536
|
storageMode: "fromModel",
|
|
529
|
-
blockPack:
|
|
537
|
+
blockPack: installedBPSpecEnterV3,
|
|
530
538
|
},
|
|
531
539
|
);
|
|
532
540
|
mut.setStates([
|
|
@@ -544,7 +552,7 @@ test("v3 blocks: migrateBlockPack assigns author marker", async () => {
|
|
|
544
552
|
const testAuthor: AuthorMarker = { authorId: "test-author-123", localVersion: 1 };
|
|
545
553
|
await pl.withWriteTx("MigrateWithAuthor", async (tx) => {
|
|
546
554
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj, testAuthor);
|
|
547
|
-
mut.migrateBlockPack("enter1",
|
|
555
|
+
mut.migrateBlockPack("enter1", installedBPSpecEnterV3);
|
|
548
556
|
mut.save();
|
|
549
557
|
await tx.commit();
|
|
550
558
|
});
|
|
@@ -568,6 +576,7 @@ test("v3 blocks: migrateBlockPack does not crash when args() returns undefined",
|
|
|
568
576
|
const quickJs = await getQuickJS();
|
|
569
577
|
|
|
570
578
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
579
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
571
580
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
572
581
|
const prjRef = await createProject(tx);
|
|
573
582
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -582,7 +591,7 @@ test("v3 blocks: migrateBlockPack does not crash when args() returns undefined",
|
|
|
582
591
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
583
592
|
{
|
|
584
593
|
storageMode: "fromModel",
|
|
585
|
-
blockPack:
|
|
594
|
+
blockPack: installedBPSpecEnterV3,
|
|
586
595
|
},
|
|
587
596
|
);
|
|
588
597
|
mut.setStates([
|
|
@@ -600,7 +609,7 @@ test("v3 blocks: migrateBlockPack does not crash when args() returns undefined",
|
|
|
600
609
|
// args() returns undefined (previously crashed inside createJsonFieldValue).
|
|
601
610
|
await pl.withWriteTx("MigrateBlockPack", async (tx) => {
|
|
602
611
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
603
|
-
mut.migrateBlockPack("enter1",
|
|
612
|
+
mut.migrateBlockPack("enter1", installedBPSpecEnterV3);
|
|
604
613
|
mut.save();
|
|
605
614
|
await tx.commit();
|
|
606
615
|
});
|
|
@@ -629,6 +638,7 @@ test("v3 blocks: every args derivation records the errors found in the args, non
|
|
|
629
638
|
const quickJs = await getQuickJS();
|
|
630
639
|
|
|
631
640
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
641
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
632
642
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
633
643
|
const prjRef = await createProject(tx);
|
|
634
644
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -640,7 +650,7 @@ test("v3 blocks: every args derivation records the errors found in the args, non
|
|
|
640
650
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
641
651
|
mut.addBlock(
|
|
642
652
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
643
|
-
{ storageMode: "fromModel", blockPack:
|
|
653
|
+
{ storageMode: "fromModel", blockPack: installedBPSpecEnterV3 },
|
|
644
654
|
);
|
|
645
655
|
mut.setStates([
|
|
646
656
|
{
|
|
@@ -668,6 +678,7 @@ test("v3 blocks: args without an args errors record of their own are kept and ru
|
|
|
668
678
|
const quickJs = await getQuickJS();
|
|
669
679
|
|
|
670
680
|
await TestHelpers.withTempRoot(async (pl) => {
|
|
681
|
+
const installedBPSpecEnterV3 = await prepareInstalled(pl, BPSpecEnterV3);
|
|
671
682
|
const prj = await pl.withWriteTx("CreatingProject", async (tx) => {
|
|
672
683
|
const prjRef = await createProject(tx);
|
|
673
684
|
tx.createField(field(tx.clientRoot, "prj"), "Dynamic", prjRef);
|
|
@@ -679,7 +690,7 @@ test("v3 blocks: args without an args errors record of their own are kept and ru
|
|
|
679
690
|
const mut = await ProjectMutator.load(new ProjectHelper(quickJs), tx, prj);
|
|
680
691
|
mut.addBlock(
|
|
681
692
|
{ id: "enter1", label: "Enter Numbers V3", renderingMode: "Heavy" },
|
|
682
|
-
{ storageMode: "fromModel", blockPack:
|
|
693
|
+
{ storageMode: "fromModel", blockPack: installedBPSpecEnterV3 },
|
|
683
694
|
);
|
|
684
695
|
mut.setStates([
|
|
685
696
|
{
|
|
@@ -32,7 +32,9 @@ import type {
|
|
|
32
32
|
} from "@milaboratories/pl-model-backend";
|
|
33
33
|
import { notEmpty } from "@milaboratories/ts-helpers";
|
|
34
34
|
import type { BlockPackSpecPrepared } from "../../model";
|
|
35
|
-
import type { TemplateSpecPrepared } from "../../model/template_spec";
|
|
35
|
+
import type { InstallablePackTemplate, TemplateSpecPrepared } from "../../model/template_spec";
|
|
36
|
+
import type { BlockPackPreparer } from "../block-pack/block_pack";
|
|
37
|
+
import { throwIfMissingServerCapabilities } from "../block-pack/required_capabilities";
|
|
36
38
|
import { getDebugFlags } from "../../debug";
|
|
37
39
|
|
|
38
40
|
export const TemplateCacheType = resourceType("TemplateCache", "1");
|
|
@@ -776,6 +778,12 @@ export async function loadTemplateCached(
|
|
|
776
778
|
}
|
|
777
779
|
}
|
|
778
780
|
|
|
781
|
+
type CacheBlockPackOptions = {
|
|
782
|
+
cacheResourceId?: SignedResourceId;
|
|
783
|
+
/** Loads registry and local packs. */
|
|
784
|
+
preparer?: BlockPackPreparer;
|
|
785
|
+
};
|
|
786
|
+
|
|
779
787
|
/**
|
|
780
788
|
* Pre-materialize a block pack's template via cache.
|
|
781
789
|
* Returns a copy of the spec with the template replaced by a cached reference.
|
|
@@ -784,13 +792,28 @@ export async function loadTemplateCached(
|
|
|
784
792
|
export async function cacheBlockPackTemplate(
|
|
785
793
|
pl: PlClient,
|
|
786
794
|
spec: BlockPackSpecPrepared,
|
|
787
|
-
options?:
|
|
795
|
+
options?: CacheBlockPackOptions,
|
|
788
796
|
): Promise<BlockPackSpecPrepared> {
|
|
789
797
|
if (spec.template.type === "cached") return spec;
|
|
790
798
|
|
|
791
|
-
const resourceId =
|
|
799
|
+
const resourceId =
|
|
800
|
+
spec.template.type === "registry-pack" || spec.template.type === "local-pack"
|
|
801
|
+
? await installPackOnClient(pl, spec.template, options)
|
|
802
|
+
: await loadTemplateCached(pl, spec.template, options);
|
|
792
803
|
return {
|
|
793
804
|
...spec,
|
|
794
805
|
template: { type: "cached", resourceId },
|
|
795
806
|
};
|
|
796
807
|
}
|
|
808
|
+
|
|
809
|
+
async function installPackOnClient(
|
|
810
|
+
pl: PlClient,
|
|
811
|
+
template: InstallablePackTemplate,
|
|
812
|
+
options?: CacheBlockPackOptions,
|
|
813
|
+
): Promise<SignedResourceId> {
|
|
814
|
+
if (options?.preparer === undefined) throw new Error("BUG: a pack needs a preparer to load it");
|
|
815
|
+
const loaded = await options.preparer.loadPack(template);
|
|
816
|
+
// The manifest may predate the requiredCapabilities field: check what the pack declares.
|
|
817
|
+
throwIfMissingServerCapabilities(pl, loaded.requiredCapabilities);
|
|
818
|
+
return await loadTemplateCached(pl, loaded.template, options);
|
|
819
|
+
}
|
package/src/test/block_packs.ts
CHANGED
|
@@ -3,12 +3,24 @@ import { HmacSha256Signer } from "@milaboratories/ts-helpers";
|
|
|
3
3
|
import type { BlockPackSpec } from "@milaboratories/pl-model-middle-layer";
|
|
4
4
|
import { V2RegistryProvider } from "../block_registry/registry-v2-provider";
|
|
5
5
|
import { defaultHttpDispatcher } from "@milaboratories/pl-http";
|
|
6
|
+
import type { PlClient } from "@milaboratories/pl-client";
|
|
7
|
+
import type { BlockPackSpecPrepared } from "../model";
|
|
8
|
+
import { cacheBlockPackTemplate } from "../mutator/template/template_cache";
|
|
6
9
|
|
|
7
10
|
export const TestBPPreparer = new BlockPackPreparer(
|
|
8
11
|
new V2RegistryProvider(defaultHttpDispatcher()),
|
|
9
12
|
new HmacSha256Signer(HmacSha256Signer.generateSecret()),
|
|
10
13
|
);
|
|
11
14
|
|
|
15
|
+
/** Prepares a block pack and installs its template, as `Project.addBlock` does. */
|
|
16
|
+
export async function prepareInstalled(
|
|
17
|
+
pl: PlClient,
|
|
18
|
+
spec: BlockPackSpec,
|
|
19
|
+
): Promise<BlockPackSpecPrepared> {
|
|
20
|
+
const prepared = await TestBPPreparer.prepare(spec);
|
|
21
|
+
return await cacheBlockPackTemplate(pl, prepared, { preparer: TestBPPreparer });
|
|
22
|
+
}
|
|
23
|
+
|
|
12
24
|
export const BPSpecEnterV041NotPrepared: BlockPackSpec = {
|
|
13
25
|
type: "from-registry-v1",
|
|
14
26
|
registryUrl: "https://block.registry.platforma.bio/releases",
|