@vormio/plugin-sdk 0.0.0-stage → 0.0.5

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.
@@ -0,0 +1,84 @@
1
+ /** The host bridge; throws outside a Vormio plugin page. */
2
+ export function bridge() {
3
+ const host = globalThis.vormio;
4
+ if (!host)
5
+ throw new Error('This page is not running inside Vormio');
6
+ return host;
7
+ }
8
+ const statuses = {
9
+ 400: 'out-of-bounds',
10
+ 403: 'unauthorized',
11
+ 429: 'over-limit',
12
+ 404: 'missing',
13
+ 409: 'changed',
14
+ };
15
+ /**
16
+ * Reads a file next to the input, relative to the input's folder (`../` may move up within the
17
+ * linked folder). The host allows only extensions the contribution declares.
18
+ */
19
+ export async function readCompanion(input, relativePath) {
20
+ const url = new URL(relativePath, input.url).href;
21
+ let response;
22
+ try {
23
+ response = await fetch(url);
24
+ }
25
+ catch {
26
+ return { status: 'unavailable', url };
27
+ }
28
+ if (response.ok)
29
+ return { status: 'ok', url, bytes: await response.arrayBuffer() };
30
+ return { status: statuses[response.status] ?? 'unavailable', url };
31
+ }
32
+ /** Reads the input's own bytes. */
33
+ export async function readInput(input) {
34
+ const response = await fetch(input.url);
35
+ if (!response.ok)
36
+ throw new Error(`The file could not be read (${response.status})`);
37
+ return response.arrayBuffer();
38
+ }
39
+ /**
40
+ * Runs a viewer: `show` is called for the first input and again whenever the host moves to another
41
+ * file, with the previous call's signal aborted. Resolving reports ready; throwing reports the
42
+ * error message.
43
+ */
44
+ export function viewer(show) {
45
+ const host = bridge();
46
+ let current;
47
+ const run = async (input) => {
48
+ current?.abort();
49
+ const controller = new AbortController();
50
+ current = controller;
51
+ try {
52
+ await show(input, controller.signal);
53
+ if (!controller.signal.aborted)
54
+ host.ready(input);
55
+ }
56
+ catch (error) {
57
+ if (!controller.signal.aborted)
58
+ host.fail(input, error instanceof Error ? error.message : String(error));
59
+ }
60
+ };
61
+ host.onInput((input) => void run(input));
62
+ void host.getInput().then(run);
63
+ }
64
+ /** Runs a thumbnail provider; a canvas is converted to PNG. */
65
+ export function thumbnail(render) {
66
+ bridge().onThumbnailRequest(async (input, size) => {
67
+ const result = await render(input, size);
68
+ let blob;
69
+ if (result instanceof Blob)
70
+ blob = result;
71
+ else if ('convertToBlob' in result)
72
+ blob = await result.convertToBlob({ type: 'image/png' });
73
+ else
74
+ blob = await new Promise((resolve, reject) => result.toBlob((png) => (png ? resolve(png) : reject(new Error('The canvas is empty'))), 'image/png'));
75
+ return blob.arrayBuffer();
76
+ });
77
+ }
78
+ /**
79
+ * Runs an analyzer: `analyze` is called once per request and returns the composition report of
80
+ * the input. Throwing reports the analysis as failed with the error message.
81
+ */
82
+ export function analyzer(analyze) {
83
+ bridge().onAnalyzeRequest(analyze);
84
+ }
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
2
  "name": "@vormio/plugin-sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.5",
4
+ "description": "Manifest schema, runtime helpers and packaging for Vormio plugins.",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "vormio",
8
+ "plugin",
9
+ "sdk"
10
+ ],
11
+ "type": "module",
12
+ "sideEffects": false,
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "./node": {
19
+ "types": "./dist/node/index.d.ts",
20
+ "default": "./dist/node/index.js"
21
+ },
22
+ "./bridge": {
23
+ "types": "./dist/bridge.d.ts",
24
+ "default": "./dist/bridge.js"
25
+ }
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "README.md",
30
+ "LICENSE"
31
+ ],
32
+ "engines": {
33
+ "node": ">=24.21.0"
34
+ },
35
+ "dependencies": {
36
+ "zod": "^4.6.6"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "bin": {
42
+ "vormio-plugin": "./dist/node/cli.js"
43
+ }
6
44
  }