@milaboratories/pl-model-common 1.47.3 → 1.48.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/dist/bmodel/block_kind_ref.cjs +45 -0
- package/dist/bmodel/block_kind_ref.cjs.map +1 -0
- package/dist/bmodel/block_kind_ref.d.ts +57 -0
- package/dist/bmodel/block_kind_ref.d.ts.map +1 -0
- package/dist/bmodel/block_kind_ref.js +43 -0
- package/dist/bmodel/block_kind_ref.js.map +1 -0
- package/dist/bmodel/container.d.ts +8 -0
- package/dist/bmodel/container.d.ts.map +1 -1
- package/dist/bmodel/index.cjs +4 -0
- package/dist/bmodel/index.d.ts +2 -1
- package/dist/bmodel/index.js +2 -1
- package/dist/columns/dedup.cjs +1 -1
- package/dist/columns/dedup.cjs.map +1 -1
- package/dist/columns/dedup.d.ts +1 -1
- package/dist/columns/dedup.js +1 -1
- package/dist/columns/dedup.js.map +1 -1
- package/dist/columns/providers.cjs +1 -1
- package/dist/columns/providers.cjs.map +1 -1
- package/dist/columns/providers.d.ts +1 -1
- package/dist/columns/providers.js +1 -1
- package/dist/columns/providers.js.map +1 -1
- package/dist/drivers/index.cjs +4 -0
- package/dist/drivers/index.d.ts +2 -2
- package/dist/drivers/index.js +2 -2
- package/dist/drivers/pframe/index.cjs +4 -0
- package/dist/drivers/pframe/index.d.ts +2 -2
- package/dist/drivers/pframe/index.js +2 -2
- package/dist/drivers/pframe/spec/ids.cjs +151 -0
- package/dist/drivers/pframe/spec/ids.cjs.map +1 -1
- package/dist/drivers/pframe/spec/ids.d.ts +53 -1
- package/dist/drivers/pframe/spec/ids.d.ts.map +1 -1
- package/dist/drivers/pframe/spec/ids.js +150 -3
- package/dist/drivers/pframe/spec/ids.js.map +1 -1
- package/dist/drivers/pframe/spec/index.cjs +4 -0
- package/dist/drivers/pframe/spec/index.d.ts +2 -2
- package/dist/drivers/pframe/spec/index.js +2 -2
- package/dist/index.cjs +29 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +8 -2
- package/dist/plid.cjs +1 -1
- package/dist/plid.cjs.map +1 -1
- package/dist/plid.d.ts +3 -2
- package/dist/plid.d.ts.map +1 -1
- package/dist/plid.js +1 -1
- package/dist/plid.js.map +1 -1
- package/dist/template/index.cjs +20 -0
- package/dist/template/index.d.ts +5 -0
- package/dist/template/index.js +5 -0
- package/dist/template/kind_selector.cjs +92 -0
- package/dist/template/kind_selector.cjs.map +1 -0
- package/dist/template/kind_selector.d.ts +80 -0
- package/dist/template/kind_selector.d.ts.map +1 -0
- package/dist/template/kind_selector.js +87 -0
- package/dist/template/kind_selector.js.map +1 -0
- package/dist/template/project_template_v1.cjs +231 -0
- package/dist/template/project_template_v1.cjs.map +1 -0
- package/dist/template/project_template_v1.d.ts +215 -0
- package/dist/template/project_template_v1.d.ts.map +1 -0
- package/dist/template/project_template_v1.js +225 -0
- package/dist/template/project_template_v1.js.map +1 -0
- package/dist/template/template_ref_form.cjs +73 -0
- package/dist/template/template_ref_form.cjs.map +1 -0
- package/dist/template/template_ref_form.d.ts +74 -0
- package/dist/template/template_ref_form.d.ts.map +1 -0
- package/dist/template/template_ref_form.js +72 -0
- package/dist/template/template_ref_form.js.map +1 -0
- package/dist/template/template_relocate.cjs +46 -0
- package/dist/template/template_relocate.cjs.map +1 -0
- package/dist/template/template_relocate.d.ts +32 -0
- package/dist/template/template_relocate.d.ts.map +1 -0
- package/dist/template/template_relocate.js +46 -0
- package/dist/template/template_relocate.js.map +1 -0
- package/package.json +5 -5
- package/src/bmodel/block_kind_ref.ts +59 -0
- package/src/bmodel/container.ts +9 -0
- package/src/bmodel/index.ts +1 -0
- package/src/columns/dedup.ts +1 -1
- package/src/columns/providers.ts +1 -1
- package/src/drivers/pframe/spec/ids.test.ts +90 -0
- package/src/drivers/pframe/spec/ids.ts +191 -1
- package/src/index.ts +1 -0
- package/src/plid.ts +5 -5
- package/src/template/index.ts +4 -0
- package/src/template/kind_selector.ts +126 -0
- package/src/template/project_template_v1.test.ts +315 -0
- package/src/template/project_template_v1.ts +444 -0
- package/src/template/template_ref_form.test.ts +86 -0
- package/src/template/template_ref_form.ts +108 -0
- package/src/template/template_relocate.test.ts +182 -0
- package/src/template/template_relocate.ts +61 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isColumnUniversalKey, remapColumnIdBlockIds } from "../drivers/pframe/spec/ids.js";
|
|
2
|
+
import "../drivers/index.js";
|
|
3
|
+
//#region src/template/template_relocate.ts
|
|
4
|
+
/**
|
|
5
|
+
* Point every column identifier in a block's params at the blocks of the project being built.
|
|
6
|
+
*
|
|
7
|
+
* The whole of what a template does about references, and it lives here — in the package the
|
|
8
|
+
* block's own bundle imports — because knowing which values carry block ids is knowing the
|
|
9
|
+
* reference system. The engine carrying the params neither marks them, reads them, nor
|
|
10
|
+
* rewrites them: it hands the block its params and this map, and takes back what comes out.
|
|
11
|
+
*
|
|
12
|
+
* Params travel verbatim precisely so that this is possible. A file holds a `PlRef` as the
|
|
13
|
+
* object the block stored and a column id as the canonical string the block stored, with no
|
|
14
|
+
* marker of any kind, and the identifiers are found here by recognizing them — the same way
|
|
15
|
+
* the project's own dependency detector finds them in live args.
|
|
16
|
+
*
|
|
17
|
+
* Rewriting is structural, never textual: an identifier is taken apart, its `blockId` fields
|
|
18
|
+
* are replaced, and it is rebuilt canonically. That is what keeps a value that merely *looks*
|
|
19
|
+
* like an id — a `domain` entry, an axis filter — from being rewritten along with it, and
|
|
20
|
+
* what re-sorts a qualifications map whose keys are identifiers.
|
|
21
|
+
*
|
|
22
|
+
* An id the map does not mention is left as it is. That is the ordering rule doing its work:
|
|
23
|
+
* a caller building the map as it creates blocks passes only the entries already created, so
|
|
24
|
+
* a reference to an entry further down the file stays pointing at a block that does not
|
|
25
|
+
* exist, and the applied block reports itself as missing references rather than being wired
|
|
26
|
+
* to something below it.
|
|
27
|
+
*
|
|
28
|
+
* @param params Whatever the block projected, as the document stored it
|
|
29
|
+
* @param blockIds template-local entry id → the block id that entry was given
|
|
30
|
+
*/
|
|
31
|
+
function relocateBlockIds(params, blockIds) {
|
|
32
|
+
if (blockIds.size === 0) return params;
|
|
33
|
+
const remapBlockId = (blockId) => blockIds.get(blockId) ?? blockId;
|
|
34
|
+
const walk = (node) => {
|
|
35
|
+
if (typeof node === "string") return remapColumnIdBlockIds(node, remapBlockId);
|
|
36
|
+
if (isColumnUniversalKey(node)) return remapColumnIdBlockIds(node, remapBlockId);
|
|
37
|
+
if (Array.isArray(node)) return node.map(walk);
|
|
38
|
+
if (typeof node === "object" && node !== null) return Object.fromEntries(Object.entries(node).map(([key, value]) => [remapColumnIdBlockIds(key, remapBlockId), walk(value)]));
|
|
39
|
+
return node;
|
|
40
|
+
};
|
|
41
|
+
return walk(params);
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { relocateBlockIds };
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=template_relocate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template_relocate.js","names":[],"sources":["../../src/template/template_relocate.ts"],"sourcesContent":["import { isColumnUniversalKey, remapColumnIdBlockIds } from \"../drivers\";\n\n/**\n * Point every column identifier in a block's params at the blocks of the project being built.\n *\n * The whole of what a template does about references, and it lives here — in the package the\n * block's own bundle imports — because knowing which values carry block ids is knowing the\n * reference system. The engine carrying the params neither marks them, reads them, nor\n * rewrites them: it hands the block its params and this map, and takes back what comes out.\n *\n * Params travel verbatim precisely so that this is possible. A file holds a `PlRef` as the\n * object the block stored and a column id as the canonical string the block stored, with no\n * marker of any kind, and the identifiers are found here by recognizing them — the same way\n * the project's own dependency detector finds them in live args.\n *\n * Rewriting is structural, never textual: an identifier is taken apart, its `blockId` fields\n * are replaced, and it is rebuilt canonically. That is what keeps a value that merely *looks*\n * like an id — a `domain` entry, an axis filter — from being rewritten along with it, and\n * what re-sorts a qualifications map whose keys are identifiers.\n *\n * An id the map does not mention is left as it is. That is the ordering rule doing its work:\n * a caller building the map as it creates blocks passes only the entries already created, so\n * a reference to an entry further down the file stays pointing at a block that does not\n * exist, and the applied block reports itself as missing references rather than being wired\n * to something below it.\n *\n * @param params Whatever the block projected, as the document stored it\n * @param blockIds template-local entry id → the block id that entry was given\n */\nexport function relocateBlockIds<T>(params: T, blockIds: ReadonlyMap<string, string>): T {\n if (blockIds.size === 0) return params;\n const remapBlockId = (blockId: string) => blockIds.get(blockId) ?? blockId;\n\n const walk = (node: unknown): unknown => {\n // Any string may be an identifier under any amount of escaping; one that is not comes\n // back as the very same string, so this needs no test of its own here.\n if (typeof node === \"string\") return remapColumnIdBlockIds(node, remapBlockId);\n\n // Before the generic object case: an identifier IS an object, and descending into one\n // would rewrite the strings nested in it piecemeal instead of rebuilding the whole id —\n // losing the bottom-up canonicalization that keeps the result a valid identifier.\n if (isColumnUniversalKey(node)) return remapColumnIdBlockIds(node, remapBlockId);\n\n if (Array.isArray(node)) return node.map(walk);\n\n if (typeof node === \"object\" && node !== null) {\n // Keys as well as values: params may be keyed by column id — per-column settings, say\n // — and a key is exactly as much of a reference as a value is.\n return Object.fromEntries(\n Object.entries(node).map(([key, value]) => [\n remapColumnIdBlockIds(key, remapBlockId),\n walk(value),\n ]),\n );\n }\n\n return node;\n };\n\n return walk(params) as T;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,iBAAoB,QAAW,UAA0C;CACvF,IAAI,SAAS,SAAS,GAAG,OAAO;CAChC,MAAM,gBAAgB,YAAoB,SAAS,IAAI,OAAO,KAAK;CAEnE,MAAM,QAAQ,SAA2B;EAGvC,IAAI,OAAO,SAAS,UAAU,OAAO,sBAAsB,MAAM,YAAY;EAK7E,IAAI,qBAAqB,IAAI,GAAG,OAAO,sBAAsB,MAAM,YAAY;EAE/E,IAAI,MAAM,QAAQ,IAAI,GAAG,OAAO,KAAK,IAAI,IAAI;EAE7C,IAAI,OAAO,SAAS,YAAY,SAAS,MAGvC,OAAO,OAAO,YACZ,OAAO,QAAQ,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW,CACzC,sBAAsB,KAAK,YAAY,GACvC,KAAK,KAAK,CACZ,CAAC,CACH;EAGF,OAAO;CACT;CAEA,OAAO,KAAK,MAAM;AACpB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milaboratories/pl-model-common",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.48.0",
|
|
4
4
|
"description": "Platforma SDK Model",
|
|
5
5
|
"files": [
|
|
6
6
|
"./dist/**/*",
|
|
@@ -20,16 +20,16 @@
|
|
|
20
20
|
"canonicalize": "~2.1.0",
|
|
21
21
|
"es-toolkit": "^1.39.10",
|
|
22
22
|
"zod": "~3.25.76",
|
|
23
|
-
"@milaboratories/
|
|
24
|
-
"@milaboratories/
|
|
23
|
+
"@milaboratories/pl-error-like": "1.12.10",
|
|
24
|
+
"@milaboratories/helpers": "1.14.5"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@vitest/coverage-istanbul": "^4.1.3",
|
|
28
28
|
"typescript": "~5.9.3",
|
|
29
29
|
"vitest": "^4.1.3",
|
|
30
|
+
"@milaboratories/ts-configs": "1.4.0",
|
|
30
31
|
"@milaboratories/build-configs": "2.0.0",
|
|
31
|
-
"@milaboratories/ts-builder": "1.
|
|
32
|
-
"@milaboratories/ts-configs": "1.3.1"
|
|
32
|
+
"@milaboratories/ts-builder": "1.7.0"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "ts-builder build --target node",
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Branded } from "@milaboratories/helpers";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* On-wire reference to a block kind, canonically the string `{name}@{version}`.
|
|
5
|
+
*
|
|
6
|
+
* A branded string: readers overwhelmingly need identity equality ("does block
|
|
7
|
+
* X implement kind Y?"), for which an opaque canonical string is ideal. Any
|
|
8
|
+
* reader that needs the parts calls {@link parseKindRef}; any writer composes
|
|
9
|
+
* the reference through {@link formatKindRef}. Keeping composition in a single
|
|
10
|
+
* function localizes the one open decision — whether the name segment has to be
|
|
11
|
+
* org-qualified for global uniqueness — to one place.
|
|
12
|
+
*/
|
|
13
|
+
export type BlockKindReference = Branded<string, "BlockKindReference">;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Compose a {@link BlockKindReference} from a kind's `name`/`version`.
|
|
17
|
+
*
|
|
18
|
+
* The single place that decides how the reference is assembled. If global
|
|
19
|
+
* uniqueness later requires the name segment to be org-qualified, this is the
|
|
20
|
+
* one line that changes.
|
|
21
|
+
*/
|
|
22
|
+
export const formatKindRef = (k: { name: string; version: string }): BlockKindReference =>
|
|
23
|
+
`${k.name}@${k.version}` as BlockKindReference;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Split a `{name}@{version}` string on its version separator.
|
|
27
|
+
*
|
|
28
|
+
* The one place that decides where the name ends. Uses the LAST `@` so an
|
|
29
|
+
* org-qualified npm name that itself starts with `@` (e.g.
|
|
30
|
+
* `@platforma-open/pkg.kind`) keeps its whole name. A leading/absent separator
|
|
31
|
+
* (`lastIndexOf("@") <= 0`) means the string carries no version segment —
|
|
32
|
+
* malformed — so this throws rather than returning a silently version-less
|
|
33
|
+
* result.
|
|
34
|
+
*
|
|
35
|
+
* Shared with the template layer, whose `{name}@{selector}` references use the
|
|
36
|
+
* same split and differ only in how the right half is interpreted (see
|
|
37
|
+
* `parseKindSelectorReference`). `what` names the thing being parsed so the
|
|
38
|
+
* error message stays specific to the caller's reference type.
|
|
39
|
+
*/
|
|
40
|
+
export const splitVersionedName = (
|
|
41
|
+
ref: string,
|
|
42
|
+
what = "block kind reference",
|
|
43
|
+
expected = "{name}@{version}",
|
|
44
|
+
): { name: string; version: string } => {
|
|
45
|
+
const at = ref.lastIndexOf("@");
|
|
46
|
+
if (at <= 0) {
|
|
47
|
+
throw new Error(`Malformed ${what} (expected '${expected}'): ${ref}`);
|
|
48
|
+
}
|
|
49
|
+
return { name: ref.slice(0, at), version: ref.slice(at + 1) };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Split a {@link BlockKindReference} back into `{ name, version }`.
|
|
54
|
+
*
|
|
55
|
+
* Throws on a reference with no version segment — see
|
|
56
|
+
* {@link splitVersionedName}, which owns the split rule.
|
|
57
|
+
*/
|
|
58
|
+
export const parseKindRef = (ref: BlockKindReference): { name: string; version: string } =>
|
|
59
|
+
splitVersionedName(ref);
|
package/src/bmodel/container.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { BlockConfigV3Generic, BlockConfigV4Generic } from "./block_config";
|
|
2
|
+
import type { BlockKindReference } from "./block_kind_ref";
|
|
2
3
|
import type { Code } from "./code";
|
|
3
4
|
import type { BlockRenderingMode } from "./types";
|
|
4
5
|
|
|
@@ -11,6 +12,14 @@ export type BlockConfigContainer = {
|
|
|
11
12
|
/** Config code bundle. Actually is required, but we keep it optional for backward compatibility */
|
|
12
13
|
readonly code?: Code;
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Reference to the block kind this config implements, in `{name}@{version}`
|
|
17
|
+
* form. Version-independent block identity — lives at the container level
|
|
18
|
+
* beside {@link code}, orthogonal to which render envelope (`v3`/`v4`)
|
|
19
|
+
* applies. Optional for backward compatibility with kind-less blocks.
|
|
20
|
+
*/
|
|
21
|
+
readonly kind?: BlockKindReference;
|
|
22
|
+
|
|
14
23
|
//
|
|
15
24
|
// Fields below are used to read previous config versions
|
|
16
25
|
//
|
package/src/bmodel/index.ts
CHANGED
package/src/columns/dedup.ts
CHANGED
|
@@ -20,7 +20,7 @@ import { isPObjectId } from "../pool";
|
|
|
20
20
|
*
|
|
21
21
|
* Shared by sandbox-side `extractColumns` (column_providers) and host-side
|
|
22
22
|
* `ColumnsCollectionDriverImpl.getColumns` — both layers need identical
|
|
23
|
-
* dedup semantics, but operate on different concrete item types (
|
|
23
|
+
* dedup semantics, but operate on different concrete item types (DataColumnRecipe
|
|
24
24
|
* vs. raw ColumnUniversalId).
|
|
25
25
|
*/
|
|
26
26
|
export function dedupColumns<T>(
|
package/src/columns/providers.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type { AccessorLike, ColumnEntriesProvider, LeafEntry, UpstreamBlockCtx }
|
|
|
8
8
|
* exposes `isFinal()` via the root's `getInputsLocked()`.
|
|
9
9
|
*
|
|
10
10
|
* Used directly on the host side; sandbox extends it with `getColumns()`
|
|
11
|
-
* returning {@link
|
|
11
|
+
* returning {@link DataColumnRecipe}s — see `AccessorColumnsProvider` in
|
|
12
12
|
* `@platforma-sdk/model`.
|
|
13
13
|
*/
|
|
14
14
|
export class AccessorEntriesProvider<
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { createGlobalPObjectId, createLocalPObjectId } from "../../../pool";
|
|
3
|
+
import { createColumnDiscoveredId } from "./discovered_column";
|
|
4
|
+
import { createColumnFilteredId } from "./filtered_column";
|
|
5
|
+
import { peelJsonLayers, type ColumnUniversalId } from "./ids";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The escape-layer peeler.
|
|
9
|
+
*
|
|
10
|
+
* It is the one definition of "how a value can be hiding inside a string", and the reference
|
|
11
|
+
* detector in `pl-middle-layer` (`inferAllReferencedBlocks`) is built on it — a block id can
|
|
12
|
+
* sit under any number of `JSON.stringify` passes, and a walk over object properties reaches
|
|
13
|
+
* none of them. It deliberately says nothing about which values count as identifiers; the
|
|
14
|
+
* cases below are identifiers only because that is what the callers care about.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const leaf = (blockId: string, name: string) =>
|
|
18
|
+
createGlobalPObjectId(blockId, name) as ColumnUniversalId;
|
|
19
|
+
|
|
20
|
+
describe("peelJsonLayers", () => {
|
|
21
|
+
test("a canonical id is layer zero — encoded once, and that once is the id itself", () => {
|
|
22
|
+
const id = leaf("samples", "reads");
|
|
23
|
+
|
|
24
|
+
expect(peelJsonLayers(id)).toEqual({
|
|
25
|
+
value: { __isRef: true, blockId: "samples", name: "reads" },
|
|
26
|
+
layers: 0,
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("each extra stringify pass is one more layer", () => {
|
|
31
|
+
const id = leaf("samples", "reads");
|
|
32
|
+
|
|
33
|
+
expect(peelJsonLayers(JSON.stringify(id))?.layers).toBe(1);
|
|
34
|
+
expect(peelJsonLayers(JSON.stringify(JSON.stringify(id)))?.layers).toBe(2);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("the value at the bottom is the same however deep it was", () => {
|
|
38
|
+
const id = leaf("samples", "reads");
|
|
39
|
+
const bottom = { __isRef: true, blockId: "samples", name: "reads" };
|
|
40
|
+
|
|
41
|
+
expect(peelJsonLayers(JSON.stringify(JSON.stringify(id)))?.value).toEqual(bottom);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("a nested identifier peels to its own outer key, not to the leaf", () => {
|
|
45
|
+
// Wrapper forms nest by *string*, so peeling reaches the outermost key and stops. Walking
|
|
46
|
+
// further in is the caller's business, and no caller does — which is the point.
|
|
47
|
+
const filtered = createColumnFilteredId({
|
|
48
|
+
source: createColumnDiscoveredId({ column: leaf("samples", "clonotypes") }),
|
|
49
|
+
axisFilters: [[0, "IGH"]],
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const peeled = peelJsonLayers(filtered);
|
|
53
|
+
|
|
54
|
+
expect(peeled?.layers).toBe(0);
|
|
55
|
+
expect(peeled?.value).toMatchObject({ __isFiltered: true });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("a local leaf peels too, though it carries no marker at all", () => {
|
|
59
|
+
// The gate must not demand `__isRef`: a filtered id whose innermost leaf is local has
|
|
60
|
+
// none, and a peeler that required one would miss the whole chain.
|
|
61
|
+
const id = createLocalPObjectId(["pf", "byChain"], "abundance");
|
|
62
|
+
|
|
63
|
+
expect(peelJsonLayers(id)?.value).toEqual({
|
|
64
|
+
resolvePath: ["pf", "byChain"],
|
|
65
|
+
name: "abundance",
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("an ordinary string is not JSON and stops at the first character", () => {
|
|
70
|
+
for (const value of ["samples", "", "not json", "1.0", "yes"]) {
|
|
71
|
+
expect(peelJsonLayers(value)).toBeUndefined();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("a quoted string that only ever yields strings is refused", () => {
|
|
76
|
+
// `"\"abc\""` peels to `abc`, which is not JSON — there is no encoded value in there, so
|
|
77
|
+
// there is nothing for a caller to look at.
|
|
78
|
+
expect(peelJsonLayers(JSON.stringify("abc"))).toBeUndefined();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("malformed JSON is refused rather than thrown", () => {
|
|
82
|
+
expect(peelJsonLayers('{"__isRef": true')).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("a JSON array or scalar is a value like any other", () => {
|
|
86
|
+
// Nothing here is identifier-specific: the peeler answers "what was encoded", full stop.
|
|
87
|
+
expect(peelJsonLayers("[1,2]")).toBeUndefined();
|
|
88
|
+
expect(peelJsonLayers('{"a":1}')).toEqual({ value: { a: 1 }, layers: 0 });
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
} from "./filtered_column";
|
|
9
9
|
import {
|
|
10
10
|
createPObjectId,
|
|
11
|
+
isGlobalPObjectKey,
|
|
12
|
+
isLocalPObjectKey,
|
|
11
13
|
isPObjectId,
|
|
12
14
|
isPObjectKey,
|
|
13
15
|
LocalPObjectKey,
|
|
@@ -28,7 +30,7 @@ import {
|
|
|
28
30
|
type ColumnOverriddenId,
|
|
29
31
|
type ColumnOverriddenKey,
|
|
30
32
|
} from "./overridden";
|
|
31
|
-
import { canonicalizeJson } from "../../../json";
|
|
33
|
+
import { canonicalizeJson, parseJsonSafely } from "../../../json";
|
|
32
34
|
import { AxisSpec, PColumnSpec } from "./spec";
|
|
33
35
|
import { isString } from "es-toolkit";
|
|
34
36
|
|
|
@@ -97,6 +99,194 @@ export function parseColumnIdSafely(
|
|
|
97
99
|
}
|
|
98
100
|
}
|
|
99
101
|
|
|
102
|
+
/** Whether `value` is any of the five key forms a {@link ColumnUniversalId} serializes. */
|
|
103
|
+
export function isColumnUniversalKey(value: unknown): value is ColumnUniversalKey {
|
|
104
|
+
return (
|
|
105
|
+
isPObjectKey(value) ||
|
|
106
|
+
isColumnFilteredKey(value) ||
|
|
107
|
+
isColumnDiscoveredKey(value) ||
|
|
108
|
+
isColumnOverriddenKey(value)
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function isColumnUniversalId(value: unknown): value is ColumnUniversalId {
|
|
113
|
+
const key = isString(value) ? parseJsonSafely(value, false) : false;
|
|
114
|
+
return key === false ? false : isColumnUniversalKey(key);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A JSON string with its escape padding taken off, and how many passes that took.
|
|
119
|
+
*
|
|
120
|
+
* `layers` counts the `JSON.stringify` passes *above* the encoded value: a canonical id
|
|
121
|
+
* is `layers: 0`, the same id run through `JSON.stringify` once more is `layers: 1`. Keep
|
|
122
|
+
* it to put the value back the way it was found.
|
|
123
|
+
*/
|
|
124
|
+
export type PeeledJsonLayers = {
|
|
125
|
+
readonly value: unknown;
|
|
126
|
+
readonly layers: number;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Take a value out of however many `JSON.stringify` passes wrapped it, or `undefined`
|
|
131
|
+
* when `s` is not JSON at all.
|
|
132
|
+
*
|
|
133
|
+
* The one definition of "how a value can be hiding inside a string" — a block id can sit
|
|
134
|
+
* under several layers of escaping, and a walk over object properties reaches none of
|
|
135
|
+
* them. Callers differ in what they do at the bottom (this deliberately says nothing
|
|
136
|
+
* about which values count as identifiers), but they must agree on the mechanics, or
|
|
137
|
+
* "what carries a block id" ends up with two answers that drift.
|
|
138
|
+
*
|
|
139
|
+
* The gate is cheap and does NOT require any marker in the body: a filtered id whose
|
|
140
|
+
* innermost leaf is a {@link LocalPObjectKey} carries no `__isRef`, so demanding one
|
|
141
|
+
* would miss it.
|
|
142
|
+
*/
|
|
143
|
+
export function peelJsonLayers(s: string): PeeledJsonLayers | undefined {
|
|
144
|
+
let current = s;
|
|
145
|
+
let layers = 0;
|
|
146
|
+
for (;;) {
|
|
147
|
+
const c0 = current.charCodeAt(0);
|
|
148
|
+
if (c0 !== 0x7b /* { */ && c0 !== 0x22 /* " */) return undefined;
|
|
149
|
+
let parsed: unknown;
|
|
150
|
+
try {
|
|
151
|
+
parsed = JSON.parse(current);
|
|
152
|
+
} catch {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
// A pass that yielded another string was escape padding, so peel again. The string
|
|
156
|
+
// is strictly shorter each time, which is what bounds the loop.
|
|
157
|
+
if (isString(parsed)) {
|
|
158
|
+
if (parsed.length >= current.length) return undefined;
|
|
159
|
+
current = parsed;
|
|
160
|
+
layers++;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
return { value: parsed, layers };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Rewrite every block id buried inside a column id.
|
|
169
|
+
*
|
|
170
|
+
* A {@link GlobalPObjectKey} leaf names its upstream by block id, and the wrapper key forms
|
|
171
|
+
* nest by *string* id rather than by object — so a block id can sit under several layers of
|
|
172
|
+
* JSON escaping, and `queriesQualifications` carries one in a map *key*. A caller that only
|
|
173
|
+
* walks object properties never reaches any of them, which is why moving a column id between
|
|
174
|
+
* projects needs this rather than a generic walk.
|
|
175
|
+
*
|
|
176
|
+
* Recursion re-canonicalizes bottom-up, so every level is canonical afterwards — including
|
|
177
|
+
* the rebuilt `queriesQualifications`, whose keys the canonical form sorts. That is the
|
|
178
|
+
* property a textual rewrite cannot have: redirecting an id that is a map key changes what
|
|
179
|
+
* the sorted order should be, and only rebuilding restores it.
|
|
180
|
+
*
|
|
181
|
+
* Returns the input itself when no block id changed, so a caller mapping ids to themselves
|
|
182
|
+
* gets its value back byte-for-byte and never re-serializes a stored id. Any `string` is
|
|
183
|
+
* accepted for the same reason: a caller sweeping a params object cannot know which of its
|
|
184
|
+
* strings are ids, and one that is not is returned as-is.
|
|
185
|
+
*
|
|
186
|
+
* @param remapBlockId old block id → new block id. Throw from it to reject an id that cannot
|
|
187
|
+
* be mapped.
|
|
188
|
+
*/
|
|
189
|
+
export function remapColumnIdBlockIds<T extends string | ColumnUniversalKey>(
|
|
190
|
+
id: T,
|
|
191
|
+
remapBlockId: (blockId: string) => string,
|
|
192
|
+
): T {
|
|
193
|
+
const remapped = isString(id) ? remapIdString(id, remapBlockId) : remapKey(id, remapBlockId);
|
|
194
|
+
return (remapped ?? id) as T;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The string half of {@link remapColumnIdBlockIds}. `undefined` means "nothing to change",
|
|
199
|
+
* which is what keeps an unaffected id from being re-serialized.
|
|
200
|
+
*
|
|
201
|
+
* Escape padding is peeled and put back, so an id that reached params through an extra
|
|
202
|
+
* `JSON.stringify` is rewritten in place and comes back wrapped as it was found. A string
|
|
203
|
+
* that does not peel to a column key is left alone: params hold ordinary strings too.
|
|
204
|
+
*/
|
|
205
|
+
function remapIdString(id: string, remapBlockId: (blockId: string) => string): string | undefined {
|
|
206
|
+
const peeled = peelJsonLayers(id);
|
|
207
|
+
if (peeled === undefined || !isColumnUniversalKey(peeled.value)) return undefined;
|
|
208
|
+
|
|
209
|
+
const remappedKey = remapKey(peeled.value, remapBlockId);
|
|
210
|
+
if (remappedKey === undefined) return undefined;
|
|
211
|
+
|
|
212
|
+
let rebuilt: string = stringifyColumnId(remappedKey);
|
|
213
|
+
for (let layer = 0; layer < peeled.layers; layer++) rebuilt = JSON.stringify(rebuilt);
|
|
214
|
+
return rebuilt;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The key half of {@link remapColumnIdBlockIds}. `undefined` means "nothing to change". */
|
|
218
|
+
function remapKey(
|
|
219
|
+
key: ColumnUniversalKey,
|
|
220
|
+
remapBlockId: (blockId: string) => string,
|
|
221
|
+
): ColumnUniversalKey | undefined {
|
|
222
|
+
if (isGlobalPObjectKey(key)) {
|
|
223
|
+
const blockId = remapBlockId(key.blockId);
|
|
224
|
+
return blockId === key.blockId ? undefined : { ...key, blockId };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// A local leaf names its column by a path inside its own block — no block id.
|
|
228
|
+
if (isLocalPObjectKey(key)) return undefined;
|
|
229
|
+
|
|
230
|
+
if (isColumnFilteredKey(key)) {
|
|
231
|
+
const source = remapIdString(key.source, remapBlockId);
|
|
232
|
+
return source === undefined ? undefined : { ...key, source: source as ColumnUniversalId };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (isColumnOverriddenKey(key)) {
|
|
236
|
+
const source = remapIdString(key.source, remapBlockId);
|
|
237
|
+
// Remapping preserves the id's shape, so `source` is still not an Overridden id.
|
|
238
|
+
return source === undefined
|
|
239
|
+
? undefined
|
|
240
|
+
: { ...key, source: source as ColumnOverriddenKey["source"] };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (isColumnDiscoveredKey(key)) return remapDiscoveredKey(key, remapBlockId);
|
|
244
|
+
|
|
245
|
+
throw new Error(
|
|
246
|
+
`remapColumnIdBlockIds: unrecognized column id structure: ${JSON.stringify(key)}`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Discovered is the only key form carrying more than one nested id: the column it
|
|
252
|
+
* discovered, one per linker hop, and one per entry in `queriesQualifications` — where the
|
|
253
|
+
* id is the map key, not the value.
|
|
254
|
+
*/
|
|
255
|
+
function remapDiscoveredKey(
|
|
256
|
+
key: ColumnDiscoveredKey,
|
|
257
|
+
remapBlockId: (blockId: string) => string,
|
|
258
|
+
): ColumnDiscoveredKey | undefined {
|
|
259
|
+
const column = remapIdString(key.column, remapBlockId);
|
|
260
|
+
|
|
261
|
+
let pathChanged = false;
|
|
262
|
+
const path = key.path?.map((item) => {
|
|
263
|
+
const itemColumn = remapIdString(item.column, remapBlockId);
|
|
264
|
+
if (itemColumn === undefined) return item;
|
|
265
|
+
pathChanged = true;
|
|
266
|
+
return { ...item, column: itemColumn as ColumnUniversalId };
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
let queriesChanged = false;
|
|
270
|
+
const queriesQualifications =
|
|
271
|
+
key.queriesQualifications &&
|
|
272
|
+
(Object.fromEntries(
|
|
273
|
+
Object.entries(key.queriesQualifications).map(([queryId, qualifications]) => {
|
|
274
|
+
const remappedId = remapIdString(queryId, remapBlockId);
|
|
275
|
+
if (remappedId === undefined) return [queryId, qualifications];
|
|
276
|
+
queriesChanged = true;
|
|
277
|
+
return [remappedId, qualifications];
|
|
278
|
+
}),
|
|
279
|
+
) as ColumnDiscoveredKey["queriesQualifications"]);
|
|
280
|
+
|
|
281
|
+
if (column === undefined && !pathChanged && !queriesChanged) return undefined;
|
|
282
|
+
return {
|
|
283
|
+
...key,
|
|
284
|
+
...(column !== undefined ? { column: column as ColumnUniversalId } : {}),
|
|
285
|
+
...(pathChanged ? { path } : {}),
|
|
286
|
+
...(queriesChanged ? { queriesQualifications } : {}),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
100
290
|
/**
|
|
101
291
|
* Walk a rich column id down to its terminal leaf {@link PObjectId}.
|
|
102
292
|
*/
|
package/src/index.ts
CHANGED
package/src/plid.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { base32Encode } from "./base32_encode";
|
|
3
|
+
import { Branded } from "@milaboratories/helpers";
|
|
3
4
|
|
|
4
5
|
/** Number of raw bytes in the PlId. */
|
|
5
6
|
export const PlIdBytes = 15;
|
|
@@ -9,19 +10,18 @@ export const PlIdLength = 24; // = 15 bytes * 8 bits / 5 bits per char in base32
|
|
|
9
10
|
export const PlId = z
|
|
10
11
|
.string()
|
|
11
12
|
.length(PlIdLength)
|
|
12
|
-
.regex(/[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]/) // RFC4648
|
|
13
|
-
|
|
14
|
-
export type PlId = z.infer<typeof PlId>;
|
|
13
|
+
.regex(/[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]/); // RFC4648
|
|
14
|
+
export type PlId = Branded<z.infer<typeof PlId>, "PlId">;
|
|
15
15
|
|
|
16
16
|
export function uniquePlId(): PlId {
|
|
17
17
|
const data = new Uint8Array(PlIdBytes);
|
|
18
18
|
crypto.getRandomValues(data);
|
|
19
|
-
return PlId.parse(base32Encode(data, "RFC4648"));
|
|
19
|
+
return PlId.parse(base32Encode(data, "RFC4648")) as PlId;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export function plId(bytes: Uint8Array): PlId {
|
|
23
23
|
if (bytes.length !== PlIdBytes) throw new Error(`Wrong number of bytes: ${bytes.length}`);
|
|
24
|
-
return PlId.parse(base32Encode(bytes, "RFC4648"));
|
|
24
|
+
return PlId.parse(base32Encode(bytes, "RFC4648")) as PlId;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export async function digestPlId(data: string): Promise<PlId> {
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { Branded } from "@milaboratories/helpers";
|
|
2
|
+
import type { BlockKindReference } from "../bmodel/block_kind_ref";
|
|
3
|
+
import { parseKindRef, splitVersionedName } from "../bmodel/block_kind_ref";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Version-selection tier of a template entry's `kind` field.
|
|
7
|
+
*
|
|
8
|
+
* - `exact` — `X.Y.Z`: this version and no other.
|
|
9
|
+
* - `patch` — `~X.Y.Z`: patch floor, behavior frozen.
|
|
10
|
+
* - `minor` — `^X.Y.Z`: minor floor, behavior floats.
|
|
11
|
+
*/
|
|
12
|
+
export type KindSelectorOp = "exact" | "patch" | "minor";
|
|
13
|
+
|
|
14
|
+
/** The version half of a `{name}@{selector}` kind reference, split into parts. */
|
|
15
|
+
export type KindSelector = {
|
|
16
|
+
readonly op: KindSelectorOp;
|
|
17
|
+
readonly version: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* On-wire reference to a *set* of block kind versions: `{name}@{selector}`, e.g.
|
|
22
|
+
* `@platforma-open/milaboratories.mixcr-clonotyping.kind@~1.2.0`.
|
|
23
|
+
*
|
|
24
|
+
* The template-file form of a kind reference, and the only form the
|
|
25
|
+
* `template-v1` schema accepts in an entry's `kind` field. It is the same string
|
|
26
|
+
* shape as {@link BlockKindReference} widened by the `~`/`^` tiers, but branded
|
|
27
|
+
* separately so a *resolved* kind reference is never silently passed where a
|
|
28
|
+
* selector is expected, or vice versa. Widen an exact reference explicitly with
|
|
29
|
+
* {@link kindReferenceToSelectorReference}.
|
|
30
|
+
*/
|
|
31
|
+
export type BlockKindSelectorReference = Branded<string, "BlockKindSelectorReference">;
|
|
32
|
+
|
|
33
|
+
/** `X.Y.Z` with optional semver prerelease and build metadata. */
|
|
34
|
+
const semVerRegex =
|
|
35
|
+
/^\d+\.\d+\.\d+(?:-[\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*)?(?:\+[\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*)?$/;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Split a raw selector string (`1.2.0`, `~1.2.0`, `^1.2.0`) into its parts.
|
|
39
|
+
*
|
|
40
|
+
* The version is validated as `X.Y.Z`, so a range that is legal npm but not part
|
|
41
|
+
* of the kind grammar (`>=1.0.0`, `1.x`, `latest`) is rejected here rather than
|
|
42
|
+
* reaching resolution. Note the deliberate divergence from
|
|
43
|
+
* `tools/block-tools`'s `parseSelector`, which additionally tolerates a leading
|
|
44
|
+
* `@` as `exact`: after the `{name}@{selector}` split a leading `@` can only
|
|
45
|
+
* come from a doubled separator, which is malformed.
|
|
46
|
+
*
|
|
47
|
+
* Mapping a selector onto a concrete version is resolution, not parsing, and
|
|
48
|
+
* lives with the resolver (`kind_resolver.selectorToRange`).
|
|
49
|
+
*
|
|
50
|
+
* @throws if the version part is not `X.Y.Z`
|
|
51
|
+
*/
|
|
52
|
+
export function parseKindSelector(raw: string): KindSelector {
|
|
53
|
+
const s = raw.trim();
|
|
54
|
+
const op: KindSelectorOp = s.startsWith("~") ? "patch" : s.startsWith("^") ? "minor" : "exact";
|
|
55
|
+
const version = op === "exact" ? s : s.slice(1);
|
|
56
|
+
if (!semVerRegex.test(version)) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Malformed kind version selector (expected 'X.Y.Z', '~X.Y.Z' or '^X.Y.Z'): ${raw}`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return { op, version };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Render a {@link KindSelector} back to its on-wire string. */
|
|
65
|
+
export function formatKindSelector(sel: KindSelector): string {
|
|
66
|
+
switch (sel.op) {
|
|
67
|
+
case "exact":
|
|
68
|
+
return sel.version;
|
|
69
|
+
case "patch":
|
|
70
|
+
return `~${sel.version}`;
|
|
71
|
+
case "minor":
|
|
72
|
+
return `^${sel.version}`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Split a {@link BlockKindSelectorReference} into `{ name, selector }`.
|
|
78
|
+
*
|
|
79
|
+
* @throws if the reference carries no version segment, or the selector is
|
|
80
|
+
* outside the `X.Y.Z` / `~X.Y.Z` / `^X.Y.Z` grammar
|
|
81
|
+
*/
|
|
82
|
+
export function parseKindSelectorReference(ref: BlockKindSelectorReference): {
|
|
83
|
+
name: string;
|
|
84
|
+
selector: KindSelector;
|
|
85
|
+
} {
|
|
86
|
+
const { name, version } = splitVersionedName(ref, "kind selector reference", "{name}@{selector}");
|
|
87
|
+
return { name, selector: parseKindSelector(version) };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Compose a {@link BlockKindSelectorReference} from a name and selector.
|
|
92
|
+
*
|
|
93
|
+
* A formatter, not a validator — pass a selector that came from
|
|
94
|
+
* {@link parseKindSelector} or that you constructed from a known-good version.
|
|
95
|
+
*/
|
|
96
|
+
export function formatKindSelectorReference(k: {
|
|
97
|
+
name: string;
|
|
98
|
+
selector: KindSelector;
|
|
99
|
+
}): BlockKindSelectorReference {
|
|
100
|
+
return `${k.name}@${formatKindSelector(k.selector)}` as BlockKindSelectorReference;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Widen a resolved {@link BlockKindReference} to its `exact`-tier selector form.
|
|
105
|
+
*
|
|
106
|
+
* The export direction: a block implements exactly one kind version, so export
|
|
107
|
+
* always emits `{name}@X.Y.Z`. Validates on the way through, so a
|
|
108
|
+
* malformed stored reference fails at the boundary rather than in the file.
|
|
109
|
+
*/
|
|
110
|
+
export function kindReferenceToSelectorReference(
|
|
111
|
+
ref: BlockKindReference,
|
|
112
|
+
): BlockKindSelectorReference {
|
|
113
|
+
const { name, version } = parseKindRef(ref);
|
|
114
|
+
return formatKindSelectorReference({ name, selector: parseKindSelector(version) });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Whether `value` is a well-formed `{name}@{selector}` string. */
|
|
118
|
+
export function isBlockKindSelectorReference(value: unknown): value is BlockKindSelectorReference {
|
|
119
|
+
if (typeof value !== "string") return false;
|
|
120
|
+
try {
|
|
121
|
+
parseKindSelectorReference(value as BlockKindSelectorReference);
|
|
122
|
+
return true;
|
|
123
|
+
} catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|