@vizejs/plugin-sdk 0.429.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/README.md +224 -0
- package/facts.d.ts +348 -0
- package/fixes.d.ts +9 -0
- package/fixes.js +42 -0
- package/identity.js +10 -0
- package/index.d.ts +75 -0
- package/index.js +101 -0
- package/package.json +69 -0
- package/sandbox-worker.js +64 -0
- package/sandbox.d.ts +55 -0
- package/sandbox.js +328 -0
- package/stages.d.ts +114 -0
- package/stages.js +71 -0
package/stages.d.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
export type JsonValue =
|
|
2
|
+
| null
|
|
3
|
+
| boolean
|
|
4
|
+
| number
|
|
5
|
+
| string
|
|
6
|
+
| readonly JsonValue[]
|
|
7
|
+
| { readonly [key: string]: JsonValue };
|
|
8
|
+
export interface HookIdentity {
|
|
9
|
+
name: string;
|
|
10
|
+
version: string;
|
|
11
|
+
cacheInputs?: readonly { name: string; value: string }[];
|
|
12
|
+
}
|
|
13
|
+
/** Runtime arrays are frozen; declarations match generated native descriptors. */
|
|
14
|
+
export interface NativeHook extends Omit<HookIdentity, "cacheInputs"> {
|
|
15
|
+
readonly cacheInputs?: { name: string; value: string }[];
|
|
16
|
+
readonly fingerprint: string;
|
|
17
|
+
run(batchJson: string): string;
|
|
18
|
+
}
|
|
19
|
+
export interface StaticAttributeTransformBatch {
|
|
20
|
+
readonly schema: 1;
|
|
21
|
+
readonly stage: "s2-precanonical-static-attributes";
|
|
22
|
+
readonly plugin: string;
|
|
23
|
+
readonly file: string;
|
|
24
|
+
readonly nodes: readonly {
|
|
25
|
+
readonly id: number;
|
|
26
|
+
readonly kind: "ui.element";
|
|
27
|
+
readonly tag: string;
|
|
28
|
+
readonly namespace: string;
|
|
29
|
+
readonly attrs: readonly { readonly name: string; readonly value: string | null }[];
|
|
30
|
+
}[];
|
|
31
|
+
}
|
|
32
|
+
export type StaticAttributeName =
|
|
33
|
+
| "class"
|
|
34
|
+
| "id"
|
|
35
|
+
| "title"
|
|
36
|
+
| "role"
|
|
37
|
+
| "alt"
|
|
38
|
+
| `data-${string}`
|
|
39
|
+
| `aria-${string}`;
|
|
40
|
+
export interface StaticAttributeEdit {
|
|
41
|
+
kind: "replace-static-attribute";
|
|
42
|
+
node: number;
|
|
43
|
+
name: StaticAttributeName;
|
|
44
|
+
value: string | null;
|
|
45
|
+
}
|
|
46
|
+
export declare function defineTransformPlugin(
|
|
47
|
+
definition: HookIdentity & {
|
|
48
|
+
transform(batch: StaticAttributeTransformBatch): readonly StaticAttributeEdit[];
|
|
49
|
+
},
|
|
50
|
+
): NativeHook;
|
|
51
|
+
export interface CompiledArtifact {
|
|
52
|
+
readonly code: string;
|
|
53
|
+
readonly preamble: string;
|
|
54
|
+
readonly ast: unknown;
|
|
55
|
+
readonly map?: unknown;
|
|
56
|
+
readonly helpers: readonly string[];
|
|
57
|
+
readonly templates?: readonly string[] | null;
|
|
58
|
+
}
|
|
59
|
+
export interface OutputBatch {
|
|
60
|
+
readonly schema: 1;
|
|
61
|
+
readonly plugin: string;
|
|
62
|
+
readonly family: "formatter" | "output";
|
|
63
|
+
readonly offsetEncoding: "utf8";
|
|
64
|
+
readonly compiled: CompiledArtifact;
|
|
65
|
+
}
|
|
66
|
+
export interface FormatEdit {
|
|
67
|
+
start: number;
|
|
68
|
+
end: number;
|
|
69
|
+
text: string;
|
|
70
|
+
}
|
|
71
|
+
export interface OutputAddition {
|
|
72
|
+
placement: "prepend" | "append";
|
|
73
|
+
comment: string;
|
|
74
|
+
}
|
|
75
|
+
export interface FormatterBatch extends OutputBatch {
|
|
76
|
+
readonly family: "formatter";
|
|
77
|
+
}
|
|
78
|
+
export interface OutputHookBatch extends OutputBatch {
|
|
79
|
+
readonly family: "output";
|
|
80
|
+
}
|
|
81
|
+
export declare function defineOutputPlugin(
|
|
82
|
+
definition: HookIdentity & {
|
|
83
|
+
family: "formatter";
|
|
84
|
+
output(batch: FormatterBatch): readonly FormatEdit[];
|
|
85
|
+
},
|
|
86
|
+
): NativeHook & { readonly family: "formatter" };
|
|
87
|
+
export declare function defineOutputPlugin(
|
|
88
|
+
definition: HookIdentity & {
|
|
89
|
+
family: "output";
|
|
90
|
+
output(batch: OutputHookBatch): readonly OutputAddition[];
|
|
91
|
+
},
|
|
92
|
+
): NativeHook & { readonly family: "output" };
|
|
93
|
+
export interface FactProviderBatch {
|
|
94
|
+
readonly schema: 1;
|
|
95
|
+
readonly plugin: string;
|
|
96
|
+
readonly file: string;
|
|
97
|
+
readonly parents: readonly number[];
|
|
98
|
+
readonly nodes: readonly import("./index.js").PluginNode[];
|
|
99
|
+
readonly facts: Readonly<Record<string, readonly (readonly [string | number, JsonValue])[]>>;
|
|
100
|
+
}
|
|
101
|
+
export declare function defineFactProvider(
|
|
102
|
+
definition: HookIdentity & {
|
|
103
|
+
visit?: readonly string[];
|
|
104
|
+
demands?: readonly string[];
|
|
105
|
+
provides: readonly string[];
|
|
106
|
+
provide(
|
|
107
|
+
batch: FactProviderBatch,
|
|
108
|
+
): Readonly<Record<string, readonly (readonly [string | number, JsonValue])[]>>;
|
|
109
|
+
},
|
|
110
|
+
): NativeHook & {
|
|
111
|
+
readonly provides: string[];
|
|
112
|
+
readonly visit?: string[];
|
|
113
|
+
readonly demands: string[];
|
|
114
|
+
};
|
package/stages.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { SDK_FINGERPRINT } from "./identity.js";
|
|
3
|
+
|
|
4
|
+
/** Define a synchronous native pre-canonical static-attribute transform. */
|
|
5
|
+
export function defineTransformPlugin({ name, version, cacheInputs, transform }) {
|
|
6
|
+
return stage({ name, version, cacheInputs }, transform, (batch, edits) => {
|
|
7
|
+
if (batch.schema !== 1 || batch.stage !== "s2-precanonical-static-attributes") {
|
|
8
|
+
throw new Error(`${name}: unsupported L2 transform batch`);
|
|
9
|
+
}
|
|
10
|
+
return { schema: 1, edits };
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Define a formatter or output hook over an actual compiled artifact. */
|
|
15
|
+
export function defineOutputPlugin({ name, version, family, cacheInputs, output }) {
|
|
16
|
+
if (!["formatter", "output"].includes(family)) throw new TypeError("unknown output hook family");
|
|
17
|
+
return stage({ name, version, family, cacheInputs }, output, (batch, operations) => {
|
|
18
|
+
if (batch.schema !== 1 || batch.family !== family || batch.offsetEncoding !== "utf8") {
|
|
19
|
+
throw new Error(`${name}: unsupported compiled output batch`);
|
|
20
|
+
}
|
|
21
|
+
return operations;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Define a namespaced provider consumed by native plugin fact demands. */
|
|
26
|
+
export function defineFactProvider({
|
|
27
|
+
name,
|
|
28
|
+
version,
|
|
29
|
+
visit,
|
|
30
|
+
demands = [],
|
|
31
|
+
provides,
|
|
32
|
+
cacheInputs,
|
|
33
|
+
provide,
|
|
34
|
+
}) {
|
|
35
|
+
return stage(
|
|
36
|
+
{ name, version, visit, demands, provides, cacheInputs },
|
|
37
|
+
provide,
|
|
38
|
+
(batch, facts) => {
|
|
39
|
+
if (batch.schema !== 1) throw new Error(`${name}: unsupported fact visit batch`);
|
|
40
|
+
return facts;
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function stage(manifest, callback, encode) {
|
|
46
|
+
if (!manifest.name || !manifest.version || typeof callback !== "function") {
|
|
47
|
+
throw new TypeError("a plugin hook needs name, version and a synchronous function");
|
|
48
|
+
}
|
|
49
|
+
manifest = structuredClone(manifest);
|
|
50
|
+
const fingerprint = createHash("sha256")
|
|
51
|
+
.update(JSON.stringify([SDK_FINGERPRINT, manifest, String(callback)]))
|
|
52
|
+
.digest("hex");
|
|
53
|
+
return immutable({
|
|
54
|
+
...manifest,
|
|
55
|
+
fingerprint,
|
|
56
|
+
run(batchJson) {
|
|
57
|
+
const batch = immutable(JSON.parse(batchJson));
|
|
58
|
+
const result = callback(batch);
|
|
59
|
+
if (result?.then) throw new TypeError(`${manifest.name}: asynchronous hooks are unsupported`);
|
|
60
|
+
return JSON.stringify(encode(batch, result));
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function immutable(value) {
|
|
66
|
+
if (value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
67
|
+
for (const child of Object.values(value)) immutable(child);
|
|
68
|
+
Object.freeze(value);
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
}
|