@nesso-how/plugin 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +63 -2
- package/dist/commands-xXIv9QeH.js +191 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/package.d.ts +36 -0
- package/dist/package.js +201 -0
- package/dist/runtime-CeTVu8np.d.ts +739 -0
- package/dist/runtime-Dfjx2_F9.js +764 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +3 -0
- package/example/README.md +24 -0
- package/example/package.json +32 -0
- package/example/src/index.ts +14 -0
- package/example/tsconfig.json +14 -0
- package/package.json +47 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Omar Desogus and Paolo Manfredotti
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nesso-how/plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Types, validation, and asynchronous clients for isolated Nesso actions plus data-only vocabularies and themes. The canvas is a trusted renderer, not a plugin.
|
|
4
|
+
|
|
5
|
+
## Create a plugin
|
|
6
|
+
|
|
7
|
+
Start with the [standalone action example](example/README.md). Actions export `create(context)` and build one self-contained browser JavaScript entry, bundling any dependencies. Nesso does not install dependencies, run package scripts, compile received source, or load module graphs. Themes and vocabularies ship a JSON definition file instead; the host validates it and never executes it.
|
|
8
|
+
|
|
9
|
+
- **Actions** return `{ id, label, supportsViews, run }` records. Discovery has no document access or effects; each invocation runs in a fresh worker. See [Import](../../plugins/import) and [Export](../../plugins/export).
|
|
10
|
+
- **Vocabularies** ship a `VocabDefinition` JSON file with stable absolute relation-type IRIs and a default among their own types. See [Base Vocab](../../plugins/base-vocab).
|
|
11
|
+
- **Themes** ship a light-only `ThemeDefinition` JSON file with colors, metrics, and local WOFF2 font descriptors. Include exactly the referenced fonts: at most 32 files, 4 MiB each. The host validates and applies them; arbitrary CSS and remote fonts are not supported. See [Kernel](../../plugins/kernel).
|
|
12
|
+
|
|
13
|
+
Nesso injects the context. Import types such as `ActionContext` and `ActionContribution` from `@nesso-how/plugin`; there is no need to create a client yourself.
|
|
14
|
+
|
|
15
|
+
## Manifest and translations
|
|
16
|
+
|
|
17
|
+
Use the [example package.json](example/package.json). Package names must be valid npm names and versions exact SemVer. Technical declarations live under `nesso`; all fields below are required.
|
|
18
|
+
|
|
19
|
+
| `nesso` field | Supported value |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `apiVersion` | `1` |
|
|
22
|
+
| `kind` | `actions`, `vocab`, or `theme` |
|
|
23
|
+
| `entry` | Actions: package-local `.js` path, such as `dist/index.js`. Themes and vocabularies: package-local `.json` definition path, such as `dist/definition.json` |
|
|
24
|
+
| `writes` | Document-content operation kinds, without duplicates |
|
|
25
|
+
| `capabilities` | Subset of `files.open`, `files.save`, `notifications.notify`, without duplicates |
|
|
26
|
+
|
|
27
|
+
Themes and vocabularies require empty `writes` and `capabilities`. Unknown declarations are rejected. Contribution IDs are unique by kind and use lowercase kebab-case.
|
|
28
|
+
|
|
29
|
+
The top-level `i18n.metadata` requires `name`, `description`, and `documentation`. Each is a `LocalizedText` object: `{ en: string, it?: string, 'zh-CN'?: string }`. Other `i18n` keys use the same shape. Nesso resolves the current language with an English fallback; no translation library is needed. Bundle JSON imports at build time.
|
|
30
|
+
|
|
31
|
+
Use `label: i18n.label` for an action, or reuse `i18n.metadata.name` when the wording matches. Notification titles/descriptions and `PluginError`'s optional third argument also accept localized text. Diagnostic `issues` remain `{ path, message }[]` strings.
|
|
32
|
+
|
|
33
|
+
## Action context
|
|
34
|
+
|
|
35
|
+
`create` receives `{ store, files, notifications, signal }`. Keep discovery free of effects and return stable descriptors. Actions have no persistent background instance.
|
|
36
|
+
|
|
37
|
+
- `store.getState()` and `subscribe()` are synchronous. Narrow with `'viewGraph' in state` before accessing invocation data. Actions always receive the invoked view; invoking on All exposes the complete graph.
|
|
38
|
+
- Await write helpers such as `setConceptLabel`, `addConcept`, and `setViewMembership`. Use `applyOperations(operations, { atRevision })` for one atomic, revision-checked batch. Separate calls are separate transactions; never retry mutations automatically.
|
|
39
|
+
- Receiving the invoked view is not a write sandbox. Declared writes can affect the whole document.
|
|
40
|
+
- `run({ viewId })` keeps its invocation target despite navigation; `null` means All. Target deletion or document reset revokes the context. Use `refresh()` after deferred work and honor `signal`.
|
|
41
|
+
- `files.open()`/`save()` mediate UTF-8 text files up to 16 MiB through the host, returning `null`/`{ saved: false }` on cancellation. `notifications.notify()` posts internal `info`/`warning` messages. Each effect requires its declared capability and must be awaited before returning.
|
|
42
|
+
|
|
43
|
+
Failures reject with `PluginError`, carrying `code` and bounded `issues`. Public types, validators, and tests define the full contract and limits: see `src/runtime.ts`, `src/definitions.ts`, and `test/`.
|
|
44
|
+
|
|
45
|
+
## Installation and isolation
|
|
46
|
+
|
|
47
|
+
External installation is desktop-only; web runs bundled plugins only. Publish versioned npm packages; every installation pins an exact version and the host revalidates the received files. Nesso never installs dependencies or runs package scripts. For local development, see the example's `propose` call.
|
|
48
|
+
|
|
49
|
+
Each action invocation runs in a fresh isolated worker; themes and vocabularies are validated data and never executed. Installation binds approval to the exact received content and its declarations; failed updates keep the previous installation.
|
|
50
|
+
|
|
51
|
+
The host validates requests and revokes effects before teardown. Plugins receive no Node, host DOM, credentials, arbitrary filesystem, or network access. Actions use interruptible workers; themes and vocabularies are validated data and never executed. This does not guarantee hard CPU/RAM containment or host responsiveness.
|
|
52
|
+
|
|
53
|
+
## SDK development
|
|
54
|
+
|
|
55
|
+
From the Nesso repository:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
pnpm --dir packages/plugin build
|
|
59
|
+
pnpm --dir packages/plugin pack --pack-destination /absolute/output
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Use **pnpm pack** so workspace exports become compiled ESM and declarations. The SDK exposes `@nesso-how/plugin` for authors, `/runtime` for runtime validation/client code, and `/package` for package tooling. Consumers do not need other Nesso workspace packages.
|
|
63
|
+
|
|
64
|
+
Run `pnpm test` for contract tests and `pnpm test:plugin-runtime` for desktop/web integration checks.
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
//#region ../errors/src/index.ts
|
|
2
|
+
var NessoError = class extends Error {
|
|
3
|
+
name = "NessoError";
|
|
4
|
+
issues;
|
|
5
|
+
constructor(issues) {
|
|
6
|
+
super(issues.map(({ path, message }) => path ? `${path}: ${message}` : message).join("\n"));
|
|
7
|
+
this.issues = issues;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/errors.ts
|
|
12
|
+
const boundText = (text, limit) => {
|
|
13
|
+
let result = "";
|
|
14
|
+
let bytes = 0;
|
|
15
|
+
const encoder = new TextEncoder();
|
|
16
|
+
for (const character of text) {
|
|
17
|
+
bytes += encoder.encode(character).byteLength;
|
|
18
|
+
if (bytes > limit) break;
|
|
19
|
+
result += character;
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
var PluginError = class extends NessoError {
|
|
24
|
+
name = "PluginError";
|
|
25
|
+
code;
|
|
26
|
+
displayMessage;
|
|
27
|
+
constructor(code, issues, displayMessage) {
|
|
28
|
+
const bounded = issues.slice(0, 32).map(({ path, message }) => ({
|
|
29
|
+
path: boundText(path, 256),
|
|
30
|
+
message: boundText(message, 2048)
|
|
31
|
+
}));
|
|
32
|
+
if (!bounded.length) bounded.push({
|
|
33
|
+
path: "plugin",
|
|
34
|
+
message: "Plugin request failed"
|
|
35
|
+
});
|
|
36
|
+
super(bounded);
|
|
37
|
+
this.code = code;
|
|
38
|
+
if (displayMessage) this.displayMessage = {
|
|
39
|
+
en: boundText(displayMessage.en, 2048),
|
|
40
|
+
...displayMessage.it !== void 0 ? { it: boundText(displayMessage.it, 2048) } : {},
|
|
41
|
+
...displayMessage["zh-CN"] !== void 0 ? { "zh-CN": boundText(displayMessage["zh-CN"], 2048) } : {}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
function parseOrThrow(schema, value, prefix = "") {
|
|
46
|
+
const parsed = schema.safeParse(value);
|
|
47
|
+
if (!parsed.success) throw new PluginError("invalid-input", parsed.error.issues.map((issue) => ({
|
|
48
|
+
path: `${prefix}${issue.path.join(".")}`,
|
|
49
|
+
message: issue.message
|
|
50
|
+
})));
|
|
51
|
+
return parsed.data;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region ../schema/src/core.ts
|
|
55
|
+
const newIri = () => `urn:uuid:${crypto.randomUUID()}`;
|
|
56
|
+
const isAbsoluteIri = (value) => typeof value === "string" && /^[a-z][a-z0-9+.-]*:/i.test(value);
|
|
57
|
+
const relationKey = ({ source, predicate, target }) => JSON.stringify([
|
|
58
|
+
source,
|
|
59
|
+
predicate,
|
|
60
|
+
target
|
|
61
|
+
]);
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/commands.ts
|
|
64
|
+
function createWriteCommands(applyOperations, created) {
|
|
65
|
+
return {
|
|
66
|
+
undo: () => applyOperations([{ kind: "history.undo" }]),
|
|
67
|
+
redo: () => applyOperations([{ kind: "history.redo" }]),
|
|
68
|
+
setSelection: (value) => applyOperations([{
|
|
69
|
+
kind: "selection.set",
|
|
70
|
+
value
|
|
71
|
+
}]),
|
|
72
|
+
setView: (id) => applyOperations([{
|
|
73
|
+
kind: "view.activate",
|
|
74
|
+
id
|
|
75
|
+
}]),
|
|
76
|
+
setConceptPosition: (id, value, historyGroup) => applyOperations([{
|
|
77
|
+
kind: "concept.position",
|
|
78
|
+
id,
|
|
79
|
+
value
|
|
80
|
+
}], historyGroup ? { historyGroup } : void 0),
|
|
81
|
+
setConceptPositions: (updates, historyGroup) => applyOperations([{
|
|
82
|
+
kind: "concept.positions",
|
|
83
|
+
updates
|
|
84
|
+
}], historyGroup ? { historyGroup } : void 0),
|
|
85
|
+
setConceptLabel: (id, value, historyGroup) => applyOperations([{
|
|
86
|
+
kind: "concept.label",
|
|
87
|
+
id,
|
|
88
|
+
value
|
|
89
|
+
}], historyGroup ? { historyGroup } : void 0),
|
|
90
|
+
setConceptDescription: (id, value, historyGroup) => applyOperations([{
|
|
91
|
+
kind: "concept.description",
|
|
92
|
+
id,
|
|
93
|
+
value
|
|
94
|
+
}], historyGroup ? { historyGroup } : void 0),
|
|
95
|
+
setConceptColor: (id, value, historyGroup) => applyOperations([{
|
|
96
|
+
kind: "concept.color",
|
|
97
|
+
id,
|
|
98
|
+
value
|
|
99
|
+
}], historyGroup ? { historyGroup } : void 0),
|
|
100
|
+
addConcept: (position, historyGroup) => {
|
|
101
|
+
const id = newIri();
|
|
102
|
+
return created(id, applyOperations([{
|
|
103
|
+
kind: "concept.add",
|
|
104
|
+
id,
|
|
105
|
+
position
|
|
106
|
+
}], historyGroup ? { historyGroup } : void 0));
|
|
107
|
+
},
|
|
108
|
+
connect: (source, target) => applyOperations([{
|
|
109
|
+
kind: "relation.connect",
|
|
110
|
+
source,
|
|
111
|
+
target
|
|
112
|
+
}]),
|
|
113
|
+
reconnectRelation: (id, source, target) => applyOperations([{
|
|
114
|
+
kind: "relation.reconnect",
|
|
115
|
+
id,
|
|
116
|
+
source,
|
|
117
|
+
target
|
|
118
|
+
}]),
|
|
119
|
+
setRelationType: (id, typeId) => applyOperations([{
|
|
120
|
+
kind: "relation.type",
|
|
121
|
+
id,
|
|
122
|
+
typeId
|
|
123
|
+
}]),
|
|
124
|
+
createRelationType: (id, label) => applyOperations([{
|
|
125
|
+
kind: "relation.type.create",
|
|
126
|
+
id,
|
|
127
|
+
typeId: newIri(),
|
|
128
|
+
label
|
|
129
|
+
}]),
|
|
130
|
+
removeConcept: (id) => applyOperations([{
|
|
131
|
+
kind: "concept.remove",
|
|
132
|
+
id
|
|
133
|
+
}]),
|
|
134
|
+
removeRelation: (id) => applyOperations([{
|
|
135
|
+
kind: "relation.remove",
|
|
136
|
+
id
|
|
137
|
+
}]),
|
|
138
|
+
setActiveVocab: (id) => applyOperations([{
|
|
139
|
+
kind: "preferences.vocab",
|
|
140
|
+
id
|
|
141
|
+
}]),
|
|
142
|
+
setActiveTheme: (id) => applyOperations([{
|
|
143
|
+
kind: "preferences.theme",
|
|
144
|
+
id
|
|
145
|
+
}]),
|
|
146
|
+
setLocale: (value) => applyOperations([{
|
|
147
|
+
kind: "preferences.locale",
|
|
148
|
+
value
|
|
149
|
+
}]),
|
|
150
|
+
setPanelSizes: (value) => applyOperations([{
|
|
151
|
+
kind: "preferences.panels",
|
|
152
|
+
value
|
|
153
|
+
}]),
|
|
154
|
+
setSectionOpen: (id, open) => applyOperations([{
|
|
155
|
+
kind: "preferences.section",
|
|
156
|
+
id,
|
|
157
|
+
open
|
|
158
|
+
}]),
|
|
159
|
+
createView: (name, conceptIds) => {
|
|
160
|
+
const id = newIri();
|
|
161
|
+
return created(id, applyOperations([{
|
|
162
|
+
kind: "view.create",
|
|
163
|
+
id,
|
|
164
|
+
name,
|
|
165
|
+
conceptIds
|
|
166
|
+
}]));
|
|
167
|
+
},
|
|
168
|
+
renameView: (id, name) => applyOperations([{
|
|
169
|
+
kind: "view.rename",
|
|
170
|
+
id,
|
|
171
|
+
name
|
|
172
|
+
}]),
|
|
173
|
+
setViewPinned: (id, pinned) => applyOperations([{
|
|
174
|
+
kind: "view.pin",
|
|
175
|
+
id,
|
|
176
|
+
pinned
|
|
177
|
+
}]),
|
|
178
|
+
setViewMembership: (viewId, conceptId, included) => applyOperations([{
|
|
179
|
+
kind: "view.membership",
|
|
180
|
+
viewId,
|
|
181
|
+
conceptId,
|
|
182
|
+
included
|
|
183
|
+
}]),
|
|
184
|
+
deleteView: (id) => applyOperations([{
|
|
185
|
+
kind: "view.remove",
|
|
186
|
+
id
|
|
187
|
+
}])
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
export { parseOrThrow as a, PluginError as i, isAbsoluteIri as n, relationKey as r, createWriteCommands as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { A as NessoOperation, B as Selection, D as LocalizedText, F as Notifications, G as ThemeDefinition, H as VocabDefinition, I as PanelSizes, L as Preferences, M as NessoStore, N as Notification, P as NotificationAction, R as SavedView, T as PluginError, U as WorkspaceState, V as Viewport, a as BaseState, c as PluginManifest, i as ActionState, j as NessoState, k as GraphSnapshot, l as ViewState, n as ActionContribution, o as PluginContext, r as ActionDescriptor, s as PluginI18n, t as ActionContext, w as createWriteCommands, z as SectionId } from "./runtime-CeTVu8np.js";
|
|
2
|
+
export { type ActionContext, type ActionContribution, type ActionDescriptor, type ActionState, type BaseState, type GraphSnapshot, type LocalizedText, type NessoOperation, type NessoState, type NessoStore, type Notification, type NotificationAction, type Notifications, type PanelSizes, type PluginContext, PluginError, type PluginI18n, type PluginManifest, type Preferences, type SavedView, type SectionId, type Selection, type ThemeDefinition, type ViewState, type Viewport, type VocabDefinition, type WorkspaceState, createWriteCommands };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { i as PluginError, t as createWriteCommands } from "./commands-xXIv9QeH.js";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { c as PluginManifest, s as PluginI18n } from "./runtime-CeTVu8np.js";
|
|
2
|
+
//#region src/package.d.ts
|
|
3
|
+
export declare const packageLimits: {
|
|
4
|
+
readonly entry: number;
|
|
5
|
+
readonly font: number;
|
|
6
|
+
readonly fonts: 32;
|
|
7
|
+
readonly record: number;
|
|
8
|
+
readonly installed: 32;
|
|
9
|
+
readonly total: number;
|
|
10
|
+
};
|
|
11
|
+
export type PluginPackageInput = {
|
|
12
|
+
readonly manifest: unknown;
|
|
13
|
+
readonly files: readonly {
|
|
14
|
+
readonly path: string;
|
|
15
|
+
readonly bytes: Uint8Array;
|
|
16
|
+
}[];
|
|
17
|
+
};
|
|
18
|
+
export type PreparedPlugin = {
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly version: string;
|
|
21
|
+
readonly author?: string;
|
|
22
|
+
readonly repository?: string;
|
|
23
|
+
readonly nesso: PluginManifest;
|
|
24
|
+
readonly i18n: PluginI18n;
|
|
25
|
+
readonly code: string;
|
|
26
|
+
readonly fonts: readonly {
|
|
27
|
+
readonly path: string;
|
|
28
|
+
readonly data: string;
|
|
29
|
+
}[];
|
|
30
|
+
readonly contentId: string;
|
|
31
|
+
};
|
|
32
|
+
export declare function parsePackageIdentity(value: unknown): any;
|
|
33
|
+
export declare function preparePluginPackage(input: PluginPackageInput): Promise<PreparedPlugin>;
|
|
34
|
+
export declare function packageFonts(value: PreparedPlugin): ReadonlyMap<string, Uint8Array>;
|
|
35
|
+
export declare function verifyPluginPackage(value: PreparedPlugin): Promise<PreparedPlugin>;
|
|
36
|
+
//#endregion
|
package/dist/package.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { a as parseOrThrow, i as PluginError } from "./commands-xXIv9QeH.js";
|
|
2
|
+
import { a as parsePluginManifest, c as sha256Hex, i as parsePluginI18n, p as parseDefinition, t as checkMessageSize } from "./runtime-Dfjx2_F9.js";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import parseVersion from "semver/functions/parse.js";
|
|
5
|
+
import validateName from "validate-npm-package-name";
|
|
6
|
+
//#region src/package.ts
|
|
7
|
+
const packageLimits = {
|
|
8
|
+
entry: 16777216,
|
|
9
|
+
font: 4194304,
|
|
10
|
+
fonts: 32,
|
|
11
|
+
record: 33554432,
|
|
12
|
+
installed: 32,
|
|
13
|
+
total: 134217728
|
|
14
|
+
};
|
|
15
|
+
const author = z.union([z.string().max(512), z.object({ name: z.string().max(128) })]).transform((value) => (typeof value === "string" ? value : value.name).split(/[<(]/)[0].trim()).pipe(z.string().min(1).max(128).regex(/^\P{Cc}+$/u)).optional().catch(void 0);
|
|
16
|
+
const repository = z.union([z.string().max(2048), z.object({ url: z.string().max(2048) })]).transform((value) => {
|
|
17
|
+
const source = typeof value === "string" ? value : value.url;
|
|
18
|
+
if (/[\p{Cc}\s\\]/u.test(source)) return void 0;
|
|
19
|
+
try {
|
|
20
|
+
const url = new URL(source.replace(/^git\+/, "").replace(/^github:/, "https://github.com/"));
|
|
21
|
+
const pathname = url.pathname.replace(/\/$/, "").replace(/\.git$/, "");
|
|
22
|
+
if (url.origin !== "https://github.com" || url.username || url.password || url.search || url.hash || !/^\/[a-zA-Z0-9][a-zA-Z0-9-]*\/[a-zA-Z0-9._-]+$/.test(pathname)) return void 0;
|
|
23
|
+
return `${url.origin}${pathname}`;
|
|
24
|
+
} catch {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
}).optional().catch(void 0);
|
|
28
|
+
const identity = z.object({
|
|
29
|
+
name: z.string().max(214).refine((value) => validateName(value).validForNewPackages, "Expected a valid npm package name"),
|
|
30
|
+
version: z.string().max(128).refine((value) => {
|
|
31
|
+
const parsed = parseVersion(value);
|
|
32
|
+
return parsed !== null && `${parsed.version}${parsed.build.length ? `+${parsed.build.join(".")}` : ""}` === value;
|
|
33
|
+
}, "Expected an exact semantic version"),
|
|
34
|
+
author,
|
|
35
|
+
repository,
|
|
36
|
+
nesso: z.unknown().optional(),
|
|
37
|
+
i18n: z.unknown().optional()
|
|
38
|
+
});
|
|
39
|
+
const canonical = (value) => {
|
|
40
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
41
|
+
if (value !== null && typeof value === "object") return `{${Object.entries(value).filter(([, item]) => item !== void 0).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0).map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`).join(",")}}`;
|
|
42
|
+
return JSON.stringify(value);
|
|
43
|
+
};
|
|
44
|
+
function parsePackageIdentity(value) {
|
|
45
|
+
return parseOrThrow(identity, value);
|
|
46
|
+
}
|
|
47
|
+
async function preparePluginPackage(input) {
|
|
48
|
+
checkMessageSize(input.manifest, 65536);
|
|
49
|
+
const { name, version, author, repository, nesso: declaration, i18n: translations } = parsePackageIdentity(input.manifest);
|
|
50
|
+
const nesso = parsePluginManifest(declaration);
|
|
51
|
+
const i18n = parsePluginI18n(translations);
|
|
52
|
+
if (!Array.isArray(input.files) || !input.files.length || input.files.length > (nesso.kind === "theme" ? packageLimits.fonts + 1 : 1)) throw new PluginError("invalid-input", [{
|
|
53
|
+
path: "files",
|
|
54
|
+
message: "Expected one entry and supported local theme fonts only"
|
|
55
|
+
}]);
|
|
56
|
+
const paths = /* @__PURE__ */ new Set();
|
|
57
|
+
const fonts = [];
|
|
58
|
+
const files = [];
|
|
59
|
+
let bytes;
|
|
60
|
+
let admitted = 0;
|
|
61
|
+
for (const file of input.files) {
|
|
62
|
+
if (!file || typeof file.path !== "string" || file.path.length > 240 || paths.has(file.path) || !(file.bytes instanceof Uint8Array) || !file.bytes.byteLength || file.bytes.byteLength > (file.path === nesso.entry ? packageLimits.entry : packageLimits.font)) throw new PluginError("invalid-input", [{
|
|
63
|
+
path: "files",
|
|
64
|
+
message: "Invalid, duplicate, or oversized package file"
|
|
65
|
+
}]);
|
|
66
|
+
paths.add(file.path);
|
|
67
|
+
admitted += file.path === nesso.entry ? file.bytes.byteLength : Math.ceil(file.bytes.byteLength / 3) * 4;
|
|
68
|
+
if (admitted > packageLimits.record) throw new PluginError("invalid-input", [{
|
|
69
|
+
path: "files",
|
|
70
|
+
message: "Package record budget exceeded"
|
|
71
|
+
}]);
|
|
72
|
+
const content = new Uint8Array(file.bytes);
|
|
73
|
+
if (file.path === nesso.entry) bytes = content;
|
|
74
|
+
else {
|
|
75
|
+
if (nesso.kind !== "theme" || !/^(?:[\w-]+\/)*[\w-]+\.woff2$/.test(file.path) || new TextDecoder().decode(content.subarray(0, 4)) !== "wOF2") throw new PluginError("invalid-input", [{
|
|
76
|
+
path: file.path,
|
|
77
|
+
message: "Expected a package-local WOFF2 font"
|
|
78
|
+
}]);
|
|
79
|
+
let binary = "";
|
|
80
|
+
for (let offset = 0; offset < content.length; offset += 32768) binary += String.fromCharCode(...content.subarray(offset, offset + 32768));
|
|
81
|
+
fonts.push({
|
|
82
|
+
path: file.path,
|
|
83
|
+
data: btoa(binary)
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
files.push({
|
|
87
|
+
path: file.path,
|
|
88
|
+
size: content.byteLength,
|
|
89
|
+
sha256: await sha256Hex(content)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (!bytes) throw new PluginError("invalid-input", [{
|
|
93
|
+
path: "nesso.entry",
|
|
94
|
+
message: "Missing plugin entry"
|
|
95
|
+
}]);
|
|
96
|
+
let code;
|
|
97
|
+
try {
|
|
98
|
+
code = new TextDecoder("utf-8", {
|
|
99
|
+
fatal: true,
|
|
100
|
+
ignoreBOM: true
|
|
101
|
+
}).decode(bytes);
|
|
102
|
+
} catch {
|
|
103
|
+
throw new PluginError("invalid-input", [{
|
|
104
|
+
path: "files",
|
|
105
|
+
message: "Expected UTF-8 entry text"
|
|
106
|
+
}]);
|
|
107
|
+
}
|
|
108
|
+
if (nesso.kind !== "actions") {
|
|
109
|
+
let raw;
|
|
110
|
+
try {
|
|
111
|
+
raw = JSON.parse(code);
|
|
112
|
+
} catch {
|
|
113
|
+
throw new PluginError("invalid-input", [{
|
|
114
|
+
path: nesso.entry,
|
|
115
|
+
message: "Expected a JSON definition"
|
|
116
|
+
}]);
|
|
117
|
+
}
|
|
118
|
+
parseDefinition(nesso.kind, raw);
|
|
119
|
+
}
|
|
120
|
+
const order = (left, right) => left.path < right.path ? -1 : left.path > right.path ? 1 : 0;
|
|
121
|
+
const index = {
|
|
122
|
+
name,
|
|
123
|
+
version,
|
|
124
|
+
author,
|
|
125
|
+
repository,
|
|
126
|
+
nesso,
|
|
127
|
+
i18n,
|
|
128
|
+
files: files.sort(order)
|
|
129
|
+
};
|
|
130
|
+
const prepared = {
|
|
131
|
+
name,
|
|
132
|
+
version,
|
|
133
|
+
author,
|
|
134
|
+
repository,
|
|
135
|
+
nesso,
|
|
136
|
+
i18n,
|
|
137
|
+
code,
|
|
138
|
+
fonts: fonts.sort(order),
|
|
139
|
+
contentId: await sha256Hex(new TextEncoder().encode(canonical(index)))
|
|
140
|
+
};
|
|
141
|
+
if (new TextEncoder().encode(JSON.stringify(prepared)).byteLength > packageLimits.record) throw new PluginError("invalid-input", [{
|
|
142
|
+
path: "files",
|
|
143
|
+
message: "Package record budget exceeded"
|
|
144
|
+
}]);
|
|
145
|
+
return prepared;
|
|
146
|
+
}
|
|
147
|
+
function packageFonts(value) {
|
|
148
|
+
if (!Array.isArray(value.fonts) || value.fonts.length > packageLimits.fonts) throw new PluginError("invalid-input", [{
|
|
149
|
+
path: "fonts",
|
|
150
|
+
message: "Invalid installed font assets"
|
|
151
|
+
}]);
|
|
152
|
+
return new Map(value.fonts.map((font) => {
|
|
153
|
+
if (!font || typeof font.path !== "string" || typeof font.data !== "string" || font.data.length > Math.ceil(packageLimits.font / 3) * 4) throw new PluginError("invalid-input", [{
|
|
154
|
+
path: "fonts",
|
|
155
|
+
message: "Invalid font encoding or budget"
|
|
156
|
+
}]);
|
|
157
|
+
try {
|
|
158
|
+
return [font.path, Uint8Array.from(atob(font.data), (character) => character.charCodeAt(0))];
|
|
159
|
+
} catch {
|
|
160
|
+
throw new PluginError("invalid-input", [{
|
|
161
|
+
path: "fonts",
|
|
162
|
+
message: "Invalid font encoding"
|
|
163
|
+
}]);
|
|
164
|
+
}
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
async function verifyPluginPackage(value) {
|
|
168
|
+
if (!value || typeof value.code !== "string" || value.code.length > packageLimits.entry) throw new PluginError("invalid-input", [{
|
|
169
|
+
path: "package",
|
|
170
|
+
message: "Invalid installed plugin package"
|
|
171
|
+
}]);
|
|
172
|
+
const fonts = packageFonts(value);
|
|
173
|
+
if (fonts.size !== value.fonts.length) throw new PluginError("invalid-input", [{
|
|
174
|
+
path: "fonts",
|
|
175
|
+
message: "Duplicate font assets"
|
|
176
|
+
}]);
|
|
177
|
+
const prepared = await preparePluginPackage({
|
|
178
|
+
manifest: {
|
|
179
|
+
name: value.name,
|
|
180
|
+
version: value.version,
|
|
181
|
+
author: value.author,
|
|
182
|
+
repository: value.repository,
|
|
183
|
+
nesso: value.nesso,
|
|
184
|
+
i18n: value.i18n
|
|
185
|
+
},
|
|
186
|
+
files: [{
|
|
187
|
+
path: value.nesso?.entry,
|
|
188
|
+
bytes: new TextEncoder().encode(value.code)
|
|
189
|
+
}, ...[...fonts].map(([path, bytes]) => ({
|
|
190
|
+
path,
|
|
191
|
+
bytes
|
|
192
|
+
}))]
|
|
193
|
+
});
|
|
194
|
+
if (prepared.contentId !== value.contentId) throw new PluginError("denied", [{
|
|
195
|
+
path: "contentId",
|
|
196
|
+
message: "Package content does not match its approval"
|
|
197
|
+
}]);
|
|
198
|
+
return prepared;
|
|
199
|
+
}
|
|
200
|
+
//#endregion
|
|
201
|
+
export { packageFonts, packageLimits, parsePackageIdentity, preparePluginPackage, verifyPluginPackage };
|