@nullplatform/ui-plugin-sdk 0.2.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.
@@ -0,0 +1,70 @@
1
+ /** Splits a path into segments; rejects traversal, empty segments and query/hash fragments. */
2
+ export function normalizePath(path) {
3
+ if (typeof path !== 'string' || !path.startsWith('/'))
4
+ return null;
5
+ if (path.includes('?') || path.includes('#') || path.includes('\\'))
6
+ return null;
7
+ let decoded;
8
+ try {
9
+ decoded = decodeURIComponent(path);
10
+ }
11
+ catch {
12
+ return null;
13
+ }
14
+ const segments = decoded.split('/').slice(1);
15
+ if (segments.some((segment) => segment === '' || segment === '.' || segment === '..'))
16
+ return null;
17
+ return segments;
18
+ }
19
+ /** `{param}` placeholders match exactly one non-empty segment. */
20
+ function pathMatches(pattern, requestSegments) {
21
+ const patternSegments = normalizePath(pattern);
22
+ if (!patternSegments || patternSegments.length !== requestSegments.length)
23
+ return false;
24
+ return patternSegments.every((segment, index) => {
25
+ if (segment.startsWith('{') && segment.endsWith('}'))
26
+ return requestSegments[index].length > 0;
27
+ return segment === requestSegments[index];
28
+ });
29
+ }
30
+ /**
31
+ * Decides whether a plugin API request (a nullplatform public API path) is covered
32
+ * by its declared `permissions.api`. Query strings are never part of the decision.
33
+ */
34
+ export function isApiRequestAllowed(permissions, method, path) {
35
+ if (!permissions?.length)
36
+ return false;
37
+ const requestMethod = method.toUpperCase();
38
+ const requestSegments = normalizePath(path);
39
+ if (!requestSegments)
40
+ return false;
41
+ return permissions.some((permission) => (permission.method === '*' || permission.method === requestMethod) && pathMatches(permission.path, requestSegments));
42
+ }
43
+ /** @deprecated use `isApiRequestAllowed`. */
44
+ export const isRequestAllowed = isApiRequestAllowed;
45
+ /**
46
+ * Decides whether an external HTTP request is covered by `permissions.http`:
47
+ * https only, no credentials in the URL, exact host (including port when declared),
48
+ * declared method and path pattern.
49
+ */
50
+ export function isHttpRequestAllowed(permissions, method, url) {
51
+ if (!permissions?.length)
52
+ return false;
53
+ let parsed;
54
+ try {
55
+ parsed = new URL(url);
56
+ }
57
+ catch {
58
+ return false;
59
+ }
60
+ if (parsed.protocol !== 'https:' || parsed.username || parsed.password)
61
+ return false;
62
+ const requestMethod = method.toUpperCase();
63
+ const requestSegments = normalizePath(parsed.pathname);
64
+ if (!requestSegments)
65
+ return false;
66
+ const host = parsed.host.toLowerCase();
67
+ return permissions.some((permission) => permission.host.toLowerCase() === host &&
68
+ (permission.method === '*' || permission.method === requestMethod) &&
69
+ pathMatches(permission.path, requestSegments));
70
+ }
@@ -0,0 +1,4 @@
1
+ export { PROTOCOL_VERSION, validateManifest, isSectionSlot, ManifestError, type PluginManifest, type Contribution, type ApiPermission, type HttpPermission, type HttpMethod, } from './manifest.js';
2
+ export { isApiRequestAllowed, isHttpRequestAllowed, isRequestAllowed, normalizePath } from './allowlist.js';
3
+ export { CONTEXT_ROOTS, extractRoots, validateWhen, evaluateWhen, rolloutIncludes, WhenError, type WhenQuery, type ContextRoot, type Rollout, } from './when.js';
4
+ export { PLUGIN_STORAGE_ORIGIN, validateStorageUrl, resolveAssetUrl } from './storage.js';
@@ -0,0 +1,4 @@
1
+ export { PROTOCOL_VERSION, validateManifest, isSectionSlot, ManifestError, } from './manifest.js';
2
+ export { isApiRequestAllowed, isHttpRequestAllowed, isRequestAllowed, normalizePath } from './allowlist.js';
3
+ export { CONTEXT_ROOTS, extractRoots, validateWhen, evaluateWhen, rolloutIncludes, WhenError, } from './when.js';
4
+ export { PLUGIN_STORAGE_ORIGIN, validateStorageUrl, resolveAssetUrl } from './storage.js';
@@ -0,0 +1,46 @@
1
+ import { type WhenQuery } from './when.js';
2
+ export declare const PROTOCOL_VERSION = 1;
3
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
4
+ export interface ApiPermission {
5
+ method: HttpMethod | '*';
6
+ /** Path pattern relative to the public API root, `{param}` placeholders allowed, e.g. `/application/{id}`. */
7
+ path: string;
8
+ }
9
+ export interface HttpPermission {
10
+ /** Exact host (lowercase, optional port), e.g. `api.example.com`. Only https is ever allowed. */
11
+ host: string;
12
+ method: HttpMethod | '*';
13
+ /** Path pattern on that host, `{param}` placeholders allowed. */
14
+ path: string;
15
+ }
16
+ export interface Contribution {
17
+ slot: string;
18
+ /** Widget id exported by the plugin through `defineUI`. */
19
+ widget: string;
20
+ /** Section id (required for `*.section` slots; used in the URL). */
21
+ id?: string;
22
+ title?: string;
23
+ icon?: string;
24
+ /** Visual size hint used to reserve space while the widget loads. */
25
+ height?: number;
26
+ /** Author condition on the context (Mongo query over the context roots). */
27
+ when?: WhenQuery;
28
+ }
29
+ export interface PluginManifest {
30
+ protocol: number;
31
+ id: string;
32
+ version: string;
33
+ name?: string;
34
+ description?: string;
35
+ permissions?: {
36
+ api?: ApiPermission[];
37
+ http?: HttpPermission[];
38
+ };
39
+ contributes: Contribution[];
40
+ }
41
+ export declare class ManifestError extends Error {
42
+ constructor(message: string);
43
+ }
44
+ export declare function isSectionSlot(slot: string): boolean;
45
+ /** Validates an untrusted manifest object; throws `ManifestError` describing the first problem. */
46
+ export declare function validateManifest(input: unknown): PluginManifest;
@@ -0,0 +1,108 @@
1
+ import { validateWhen, WhenError } from './when.js';
2
+ export const PROTOCOL_VERSION = 1;
3
+ export class ManifestError extends Error {
4
+ constructor(message) {
5
+ super(message);
6
+ this.name = 'ManifestError';
7
+ }
8
+ }
9
+ const ID_PATTERN = /^[a-z0-9][a-z0-9-]{1,63}$/;
10
+ const SLOT_PATTERN = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)+$/;
11
+ const HOST_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+(:[0-9]{1,5})?$/;
12
+ const METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', '*']);
13
+ export function isSectionSlot(slot) {
14
+ return slot.endsWith('.section');
15
+ }
16
+ function readMethod(raw, where) {
17
+ const method = typeof raw === 'string' ? raw.toUpperCase() : '';
18
+ if (!METHODS.has(method))
19
+ throw new ManifestError(`${where}.method is invalid`);
20
+ return method;
21
+ }
22
+ function readPath(raw, where) {
23
+ if (typeof raw !== 'string' || !raw.startsWith('/'))
24
+ throw new ManifestError(`${where}.path must start with "/"`);
25
+ return raw;
26
+ }
27
+ /** Validates an untrusted manifest object; throws `ManifestError` describing the first problem. */
28
+ export function validateManifest(input) {
29
+ if (!input || typeof input !== 'object')
30
+ throw new ManifestError('Manifest must be an object');
31
+ const m = input;
32
+ if (m.protocol !== PROTOCOL_VERSION) {
33
+ throw new ManifestError(`Unsupported protocol ${String(m.protocol)} (expected ${PROTOCOL_VERSION})`);
34
+ }
35
+ if (typeof m.id !== 'string' || !ID_PATTERN.test(m.id)) {
36
+ throw new ManifestError('Manifest id must be lowercase letters, digits or dashes (2-64 chars)');
37
+ }
38
+ if (typeof m.version !== 'string' || !m.version)
39
+ throw new ManifestError('Manifest version is required');
40
+ if (!Array.isArray(m.contributes) || m.contributes.length === 0) {
41
+ throw new ManifestError('Manifest must contribute at least one widget');
42
+ }
43
+ const contributes = m.contributes.map((raw, index) => {
44
+ if (!raw || typeof raw !== 'object')
45
+ throw new ManifestError(`contributes[${index}] must be an object`);
46
+ const c = raw;
47
+ if (typeof c.slot !== 'string' || !SLOT_PATTERN.test(c.slot)) {
48
+ throw new ManifestError(`contributes[${index}].slot is invalid`);
49
+ }
50
+ if (typeof c.widget !== 'string' || !c.widget)
51
+ throw new ManifestError(`contributes[${index}].widget is required`);
52
+ if (isSectionSlot(c.slot) && (typeof c.id !== 'string' || !ID_PATTERN.test(c.id))) {
53
+ throw new ManifestError(`contributes[${index}].id is required for section slots`);
54
+ }
55
+ const contribution = { slot: c.slot, widget: c.widget };
56
+ if (typeof c.id === 'string')
57
+ contribution.id = c.id;
58
+ if (typeof c.title === 'string')
59
+ contribution.title = c.title;
60
+ if (typeof c.icon === 'string')
61
+ contribution.icon = c.icon;
62
+ if (typeof c.height === 'number')
63
+ contribution.height = c.height;
64
+ if (c.when !== undefined && c.when !== null) {
65
+ try {
66
+ contribution.when = validateWhen(c.when);
67
+ }
68
+ catch (error) {
69
+ if (error instanceof WhenError)
70
+ throw new ManifestError(`contributes[${index}].when: ${error.message}`);
71
+ throw error;
72
+ }
73
+ }
74
+ return contribution;
75
+ });
76
+ const permissions = {};
77
+ const rawPermissions = (m.permissions ?? {});
78
+ if (rawPermissions.api !== undefined) {
79
+ if (!Array.isArray(rawPermissions.api))
80
+ throw new ManifestError('permissions.api must be an array');
81
+ permissions.api = rawPermissions.api.map((raw, index) => {
82
+ const p = (raw ?? {});
83
+ const where = `permissions.api[${index}]`;
84
+ return { method: readMethod(p.method, where), path: readPath(p.path, where) };
85
+ });
86
+ }
87
+ if (rawPermissions.http !== undefined) {
88
+ if (!Array.isArray(rawPermissions.http))
89
+ throw new ManifestError('permissions.http must be an array');
90
+ permissions.http = rawPermissions.http.map((raw, index) => {
91
+ const p = (raw ?? {});
92
+ const where = `permissions.http[${index}]`;
93
+ const host = typeof p.host === 'string' ? p.host.toLowerCase() : '';
94
+ if (!HOST_PATTERN.test(host))
95
+ throw new ManifestError(`${where}.host must be an exact hostname (optional port), no scheme, no wildcards`);
96
+ return { host, method: readMethod(p.method, where), path: readPath(p.path, where) };
97
+ });
98
+ }
99
+ return {
100
+ protocol: PROTOCOL_VERSION,
101
+ id: m.id,
102
+ version: m.version,
103
+ name: typeof m.name === 'string' ? m.name : m.id,
104
+ description: typeof m.description === 'string' ? m.description : undefined,
105
+ permissions,
106
+ contributes,
107
+ };
108
+ }
@@ -0,0 +1,4 @@
1
+ /** Production plugin files are served exclusively by the platform's bundle CDN. */
2
+ export declare const PLUGIN_STORAGE_ORIGIN = "https://ui-plugins.nullplatform.io";
3
+ export declare function validateStorageUrl(raw: string): URL;
4
+ export declare function resolveAssetUrl(base: string, path: string): string;
@@ -0,0 +1,19 @@
1
+ /** Production plugin files are served exclusively by the platform's bundle CDN. */
2
+ export const PLUGIN_STORAGE_ORIGIN = 'https://ui-plugins.nullplatform.io';
3
+ export function validateStorageUrl(raw) {
4
+ const url = new URL(raw);
5
+ if (url.origin !== PLUGIN_STORAGE_ORIGIN || url.username || url.password || url.search || url.hash
6
+ || !raw.startsWith(PLUGIN_STORAGE_ORIGIN + '/bundles/')
7
+ || !url.pathname.startsWith('/bundles/') || /[\\%]/.test(raw)
8
+ || raw.split('/').some((part) => part === '.' || part === '..')) {
9
+ throw new Error('Plugin files must point to nullplatform bundle storage');
10
+ }
11
+ return url;
12
+ }
13
+ export function resolveAssetUrl(base, path) {
14
+ validateStorageUrl(base);
15
+ if (!base.endsWith('/') || !path || !path.split('/').every((part) => /^[a-zA-Z0-9_.-]+$/.test(part) && part !== '.' && part !== '..')) {
16
+ throw new Error('Invalid plugin asset path');
17
+ }
18
+ return validateStorageUrl(base + path).href;
19
+ }
@@ -0,0 +1,19 @@
1
+ export type WhenQuery = Record<string, unknown>;
2
+ /** Roots a condition may reference. The API knows how to load each one; anything else is rejected at write time. */
3
+ export declare const CONTEXT_ROOTS: readonly ["nrn", "organization", "account", "namespace", "application", "scope", "deployment", "user"];
4
+ export type ContextRoot = (typeof CONTEXT_ROOTS)[number];
5
+ export declare class WhenError extends Error {
6
+ constructor(message: string);
7
+ }
8
+ /** Top-level roots referenced by a query (walks `$and` / `$or` / `$nor`; field operands are never roots). */
9
+ export declare function extractRoots(when: WhenQuery | null | undefined): string[];
10
+ /** Validates an untrusted condition: plain object, JSON only, no code operators, known roots. Returns it unchanged. */
11
+ export declare function validateWhen(input: unknown): WhenQuery;
12
+ /** True when the query matches the context. An empty or missing query always matches; an invalid one never does. */
13
+ export declare function evaluateWhen(when: WhenQuery | null | undefined, context: Record<string, unknown>): boolean;
14
+ export interface Rollout {
15
+ /** 0–100. Missing means everyone. */
16
+ percentage?: number;
17
+ }
18
+ /** Deterministic per (user, package): the same user always lands in the same bucket for a package. */
19
+ export declare function rolloutIncludes(rollout: Rollout | null | undefined, userId: string | number | undefined, packageSlug: string): boolean;
@@ -0,0 +1,104 @@
1
+ /**
2
+ * `when` conditions: a Mongo-style query (evaluated with `sift`) over a context
3
+ * document whose top-level keys are the roots below. The same evaluator runs in
4
+ * the API (installations and manifests) and in the host (developer mode).
5
+ */
6
+ import sift from 'sift';
7
+ /** Roots a condition may reference. The API knows how to load each one; anything else is rejected at write time. */
8
+ export const CONTEXT_ROOTS = ['nrn', 'organization', 'account', 'namespace', 'application', 'scope', 'deployment', 'user'];
9
+ /** Operators that would execute code or are meaningless outside MongoDB. */
10
+ const FORBIDDEN_OPERATORS = new Set(['$where', '$function', '$expr', '$jsonSchema', '$text', '$accumulator']);
11
+ /** Logical operators whose operands are themselves queries against the same document. */
12
+ const LOGICAL_LIST_OPERATORS = new Set(['$and', '$or', '$nor']);
13
+ export class WhenError extends Error {
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = 'WhenError';
17
+ }
18
+ }
19
+ const isPlainObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;
20
+ const rootOf = (field) => field.split('.')[0];
21
+ /** Top-level roots referenced by a query (walks `$and` / `$or` / `$nor`; field operands are never roots). */
22
+ export function extractRoots(when) {
23
+ const roots = new Set();
24
+ const walk = (query) => {
25
+ if (!isPlainObject(query))
26
+ return;
27
+ for (const [key, value] of Object.entries(query)) {
28
+ if (key.startsWith('$')) {
29
+ if (LOGICAL_LIST_OPERATORS.has(key) && Array.isArray(value))
30
+ value.forEach(walk);
31
+ else if (key === '$not')
32
+ walk(value);
33
+ continue;
34
+ }
35
+ roots.add(rootOf(key));
36
+ }
37
+ };
38
+ walk(when);
39
+ return [...roots];
40
+ }
41
+ /** Deep check for forbidden operators and non-JSON values (functions, symbols). */
42
+ function assertSafe(value, path) {
43
+ if (typeof value === 'function' || typeof value === 'symbol')
44
+ throw new WhenError(`${path || 'when'} must be plain JSON`);
45
+ if (Array.isArray(value)) {
46
+ value.forEach((item, index) => assertSafe(item, `${path}[${index}]`));
47
+ return;
48
+ }
49
+ if (!isPlainObject(value))
50
+ return;
51
+ for (const [key, item] of Object.entries(value)) {
52
+ if (FORBIDDEN_OPERATORS.has(key))
53
+ throw new WhenError(`Operator ${key} is not allowed`);
54
+ assertSafe(item, path ? `${path}.${key}` : key);
55
+ }
56
+ }
57
+ /** Validates an untrusted condition: plain object, JSON only, no code operators, known roots. Returns it unchanged. */
58
+ export function validateWhen(input) {
59
+ if (!isPlainObject(input))
60
+ throw new WhenError('when must be an object');
61
+ assertSafe(input, '');
62
+ for (const root of extractRoots(input)) {
63
+ if (!CONTEXT_ROOTS.includes(root)) {
64
+ throw new WhenError(`Unknown context root "${root}" (allowed: ${CONTEXT_ROOTS.join(', ')})`);
65
+ }
66
+ }
67
+ return input;
68
+ }
69
+ /** True when the query matches the context. An empty or missing query always matches; an invalid one never does. */
70
+ export function evaluateWhen(when, context) {
71
+ if (!when || Object.keys(when).length === 0)
72
+ return true;
73
+ try {
74
+ return sift(when)(context ?? {});
75
+ }
76
+ catch {
77
+ return false;
78
+ }
79
+ }
80
+ /** FNV-1a 32-bit: stable across runtimes, good enough to bucket users. */
81
+ function fnv1a(input) {
82
+ let hash = 0x811c9dc5;
83
+ for (let i = 0; i < input.length; i++) {
84
+ hash ^= input.charCodeAt(i);
85
+ hash = Math.imul(hash, 0x01000193) >>> 0;
86
+ }
87
+ return hash >>> 0;
88
+ }
89
+ /** Deterministic per (user, package): the same user always lands in the same bucket for a package. */
90
+ export function rolloutIncludes(rollout, userId, packageSlug) {
91
+ const raw = rollout?.percentage;
92
+ if (raw === undefined || raw === null)
93
+ return true;
94
+ const percentage = Math.max(0, Math.min(100, Number(raw)));
95
+ if (Number.isNaN(percentage))
96
+ return true;
97
+ if (percentage >= 100)
98
+ return true;
99
+ if (percentage <= 0)
100
+ return false;
101
+ if (userId === undefined || userId === null || userId === '')
102
+ return false;
103
+ return fnv1a(`${userId}:${packageSlug}`) % 100 < percentage;
104
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isRequestAllowed = void 0;
4
+ exports.normalizePath = normalizePath;
5
+ exports.isApiRequestAllowed = isApiRequestAllowed;
6
+ exports.isHttpRequestAllowed = isHttpRequestAllowed;
7
+ /** Splits a path into segments; rejects traversal, empty segments and query/hash fragments. */
8
+ function normalizePath(path) {
9
+ if (typeof path !== 'string' || !path.startsWith('/'))
10
+ return null;
11
+ if (path.includes('?') || path.includes('#') || path.includes('\\'))
12
+ return null;
13
+ let decoded;
14
+ try {
15
+ decoded = decodeURIComponent(path);
16
+ }
17
+ catch {
18
+ return null;
19
+ }
20
+ const segments = decoded.split('/').slice(1);
21
+ if (segments.some((segment) => segment === '' || segment === '.' || segment === '..'))
22
+ return null;
23
+ return segments;
24
+ }
25
+ /** `{param}` placeholders match exactly one non-empty segment. */
26
+ function pathMatches(pattern, requestSegments) {
27
+ const patternSegments = normalizePath(pattern);
28
+ if (!patternSegments || patternSegments.length !== requestSegments.length)
29
+ return false;
30
+ return patternSegments.every((segment, index) => {
31
+ if (segment.startsWith('{') && segment.endsWith('}'))
32
+ return requestSegments[index].length > 0;
33
+ return segment === requestSegments[index];
34
+ });
35
+ }
36
+ /**
37
+ * Decides whether a plugin API request (a nullplatform public API path) is covered
38
+ * by its declared `permissions.api`. Query strings are never part of the decision.
39
+ */
40
+ function isApiRequestAllowed(permissions, method, path) {
41
+ if (!permissions?.length)
42
+ return false;
43
+ const requestMethod = method.toUpperCase();
44
+ const requestSegments = normalizePath(path);
45
+ if (!requestSegments)
46
+ return false;
47
+ return permissions.some((permission) => (permission.method === '*' || permission.method === requestMethod) && pathMatches(permission.path, requestSegments));
48
+ }
49
+ /** @deprecated use `isApiRequestAllowed`. */
50
+ exports.isRequestAllowed = isApiRequestAllowed;
51
+ /**
52
+ * Decides whether an external HTTP request is covered by `permissions.http`:
53
+ * https only, no credentials in the URL, exact host (including port when declared),
54
+ * declared method and path pattern.
55
+ */
56
+ function isHttpRequestAllowed(permissions, method, url) {
57
+ if (!permissions?.length)
58
+ return false;
59
+ let parsed;
60
+ try {
61
+ parsed = new URL(url);
62
+ }
63
+ catch {
64
+ return false;
65
+ }
66
+ if (parsed.protocol !== 'https:' || parsed.username || parsed.password)
67
+ return false;
68
+ const requestMethod = method.toUpperCase();
69
+ const requestSegments = normalizePath(parsed.pathname);
70
+ if (!requestSegments)
71
+ return false;
72
+ const host = parsed.host.toLowerCase();
73
+ return permissions.some((permission) => permission.host.toLowerCase() === host &&
74
+ (permission.method === '*' || permission.method === requestMethod) &&
75
+ pathMatches(permission.path, requestSegments));
76
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveAssetUrl = exports.validateStorageUrl = exports.PLUGIN_STORAGE_ORIGIN = exports.WhenError = exports.rolloutIncludes = exports.evaluateWhen = exports.validateWhen = exports.extractRoots = exports.CONTEXT_ROOTS = exports.normalizePath = exports.isRequestAllowed = exports.isHttpRequestAllowed = exports.isApiRequestAllowed = exports.ManifestError = exports.isSectionSlot = exports.validateManifest = exports.PROTOCOL_VERSION = void 0;
4
+ var manifest_1 = require("./manifest");
5
+ Object.defineProperty(exports, "PROTOCOL_VERSION", { enumerable: true, get: function () { return manifest_1.PROTOCOL_VERSION; } });
6
+ Object.defineProperty(exports, "validateManifest", { enumerable: true, get: function () { return manifest_1.validateManifest; } });
7
+ Object.defineProperty(exports, "isSectionSlot", { enumerable: true, get: function () { return manifest_1.isSectionSlot; } });
8
+ Object.defineProperty(exports, "ManifestError", { enumerable: true, get: function () { return manifest_1.ManifestError; } });
9
+ var allowlist_1 = require("./allowlist");
10
+ Object.defineProperty(exports, "isApiRequestAllowed", { enumerable: true, get: function () { return allowlist_1.isApiRequestAllowed; } });
11
+ Object.defineProperty(exports, "isHttpRequestAllowed", { enumerable: true, get: function () { return allowlist_1.isHttpRequestAllowed; } });
12
+ Object.defineProperty(exports, "isRequestAllowed", { enumerable: true, get: function () { return allowlist_1.isRequestAllowed; } });
13
+ Object.defineProperty(exports, "normalizePath", { enumerable: true, get: function () { return allowlist_1.normalizePath; } });
14
+ var when_1 = require("./when");
15
+ Object.defineProperty(exports, "CONTEXT_ROOTS", { enumerable: true, get: function () { return when_1.CONTEXT_ROOTS; } });
16
+ Object.defineProperty(exports, "extractRoots", { enumerable: true, get: function () { return when_1.extractRoots; } });
17
+ Object.defineProperty(exports, "validateWhen", { enumerable: true, get: function () { return when_1.validateWhen; } });
18
+ Object.defineProperty(exports, "evaluateWhen", { enumerable: true, get: function () { return when_1.evaluateWhen; } });
19
+ Object.defineProperty(exports, "rolloutIncludes", { enumerable: true, get: function () { return when_1.rolloutIncludes; } });
20
+ Object.defineProperty(exports, "WhenError", { enumerable: true, get: function () { return when_1.WhenError; } });
21
+ var storage_1 = require("./storage");
22
+ Object.defineProperty(exports, "PLUGIN_STORAGE_ORIGIN", { enumerable: true, get: function () { return storage_1.PLUGIN_STORAGE_ORIGIN; } });
23
+ Object.defineProperty(exports, "validateStorageUrl", { enumerable: true, get: function () { return storage_1.validateStorageUrl; } });
24
+ Object.defineProperty(exports, "resolveAssetUrl", { enumerable: true, get: function () { return storage_1.resolveAssetUrl; } });
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ManifestError = exports.PROTOCOL_VERSION = void 0;
4
+ exports.isSectionSlot = isSectionSlot;
5
+ exports.validateManifest = validateManifest;
6
+ const when_1 = require("./when");
7
+ exports.PROTOCOL_VERSION = 1;
8
+ class ManifestError extends Error {
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = 'ManifestError';
12
+ }
13
+ }
14
+ exports.ManifestError = ManifestError;
15
+ const ID_PATTERN = /^[a-z0-9][a-z0-9-]{1,63}$/;
16
+ const SLOT_PATTERN = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)+$/;
17
+ const HOST_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+(:[0-9]{1,5})?$/;
18
+ const METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', '*']);
19
+ function isSectionSlot(slot) {
20
+ return slot.endsWith('.section');
21
+ }
22
+ function readMethod(raw, where) {
23
+ const method = typeof raw === 'string' ? raw.toUpperCase() : '';
24
+ if (!METHODS.has(method))
25
+ throw new ManifestError(`${where}.method is invalid`);
26
+ return method;
27
+ }
28
+ function readPath(raw, where) {
29
+ if (typeof raw !== 'string' || !raw.startsWith('/'))
30
+ throw new ManifestError(`${where}.path must start with "/"`);
31
+ return raw;
32
+ }
33
+ /** Validates an untrusted manifest object; throws `ManifestError` describing the first problem. */
34
+ function validateManifest(input) {
35
+ if (!input || typeof input !== 'object')
36
+ throw new ManifestError('Manifest must be an object');
37
+ const m = input;
38
+ if (m.protocol !== exports.PROTOCOL_VERSION) {
39
+ throw new ManifestError(`Unsupported protocol ${String(m.protocol)} (expected ${exports.PROTOCOL_VERSION})`);
40
+ }
41
+ if (typeof m.id !== 'string' || !ID_PATTERN.test(m.id)) {
42
+ throw new ManifestError('Manifest id must be lowercase letters, digits or dashes (2-64 chars)');
43
+ }
44
+ if (typeof m.version !== 'string' || !m.version)
45
+ throw new ManifestError('Manifest version is required');
46
+ if (!Array.isArray(m.contributes) || m.contributes.length === 0) {
47
+ throw new ManifestError('Manifest must contribute at least one widget');
48
+ }
49
+ const contributes = m.contributes.map((raw, index) => {
50
+ if (!raw || typeof raw !== 'object')
51
+ throw new ManifestError(`contributes[${index}] must be an object`);
52
+ const c = raw;
53
+ if (typeof c.slot !== 'string' || !SLOT_PATTERN.test(c.slot)) {
54
+ throw new ManifestError(`contributes[${index}].slot is invalid`);
55
+ }
56
+ if (typeof c.widget !== 'string' || !c.widget)
57
+ throw new ManifestError(`contributes[${index}].widget is required`);
58
+ if (isSectionSlot(c.slot) && (typeof c.id !== 'string' || !ID_PATTERN.test(c.id))) {
59
+ throw new ManifestError(`contributes[${index}].id is required for section slots`);
60
+ }
61
+ const contribution = { slot: c.slot, widget: c.widget };
62
+ if (typeof c.id === 'string')
63
+ contribution.id = c.id;
64
+ if (typeof c.title === 'string')
65
+ contribution.title = c.title;
66
+ if (typeof c.icon === 'string')
67
+ contribution.icon = c.icon;
68
+ if (typeof c.height === 'number')
69
+ contribution.height = c.height;
70
+ if (c.when !== undefined && c.when !== null) {
71
+ try {
72
+ contribution.when = (0, when_1.validateWhen)(c.when);
73
+ }
74
+ catch (error) {
75
+ if (error instanceof when_1.WhenError)
76
+ throw new ManifestError(`contributes[${index}].when: ${error.message}`);
77
+ throw error;
78
+ }
79
+ }
80
+ return contribution;
81
+ });
82
+ const permissions = {};
83
+ const rawPermissions = (m.permissions ?? {});
84
+ if (rawPermissions.api !== undefined) {
85
+ if (!Array.isArray(rawPermissions.api))
86
+ throw new ManifestError('permissions.api must be an array');
87
+ permissions.api = rawPermissions.api.map((raw, index) => {
88
+ const p = (raw ?? {});
89
+ const where = `permissions.api[${index}]`;
90
+ return { method: readMethod(p.method, where), path: readPath(p.path, where) };
91
+ });
92
+ }
93
+ if (rawPermissions.http !== undefined) {
94
+ if (!Array.isArray(rawPermissions.http))
95
+ throw new ManifestError('permissions.http must be an array');
96
+ permissions.http = rawPermissions.http.map((raw, index) => {
97
+ const p = (raw ?? {});
98
+ const where = `permissions.http[${index}]`;
99
+ const host = typeof p.host === 'string' ? p.host.toLowerCase() : '';
100
+ if (!HOST_PATTERN.test(host))
101
+ throw new ManifestError(`${where}.host must be an exact hostname (optional port), no scheme, no wildcards`);
102
+ return { host, method: readMethod(p.method, where), path: readPath(p.path, where) };
103
+ });
104
+ }
105
+ return {
106
+ protocol: exports.PROTOCOL_VERSION,
107
+ id: m.id,
108
+ version: m.version,
109
+ name: typeof m.name === 'string' ? m.name : m.id,
110
+ description: typeof m.description === 'string' ? m.description : undefined,
111
+ permissions,
112
+ contributes,
113
+ };
114
+ }
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PLUGIN_STORAGE_ORIGIN = void 0;
4
+ exports.validateStorageUrl = validateStorageUrl;
5
+ exports.resolveAssetUrl = resolveAssetUrl;
6
+ /** Production plugin files are served exclusively by the platform's bundle CDN. */
7
+ exports.PLUGIN_STORAGE_ORIGIN = 'https://ui-plugins.nullplatform.io';
8
+ function validateStorageUrl(raw) {
9
+ const url = new URL(raw);
10
+ if (url.origin !== exports.PLUGIN_STORAGE_ORIGIN || url.username || url.password || url.search || url.hash
11
+ || !raw.startsWith(exports.PLUGIN_STORAGE_ORIGIN + '/bundles/')
12
+ || !url.pathname.startsWith('/bundles/') || /[\\%]/.test(raw)
13
+ || raw.split('/').some((part) => part === '.' || part === '..')) {
14
+ throw new Error('Plugin files must point to nullplatform bundle storage');
15
+ }
16
+ return url;
17
+ }
18
+ function resolveAssetUrl(base, path) {
19
+ validateStorageUrl(base);
20
+ if (!base.endsWith('/') || !path || !path.split('/').every((part) => /^[a-zA-Z0-9_.-]+$/.test(part) && part !== '.' && part !== '..')) {
21
+ throw new Error('Invalid plugin asset path');
22
+ }
23
+ return validateStorageUrl(base + path).href;
24
+ }