@capybara-hq/plugin-sdk 0.1.0-rc.8 → 0.1.0-rc.80
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 +1 -1
- package/agent-docs/agent-templates.md +14 -2
- package/agent-docs/concepts.md +14 -5
- package/agent-docs/connector-templates.md +11 -3
- package/agent-docs/extension-points/channel.md +1 -1
- package/agent-docs/extension-points/context.md +15 -0
- package/agent-docs/extension-points/device-provider.md +15 -0
- package/agent-docs/extension-points/file-preview.md +3 -1
- package/agent-docs/extension-points/identity-source.md +2 -0
- package/agent-docs/extension-points/model-provider.md +3 -1
- package/agent-docs/extension-points/object-store.md +2 -0
- package/agent-docs/extension-points/push-provider.md +17 -0
- package/agent-docs/extensions.md +77 -4
- package/agent-docs/plugin-format.md +45 -10
- package/agent-docs/setup.md +54 -0
- package/dist/agent-template.d.ts +15 -6
- package/dist/agent-template.js +32 -13
- package/dist/avatar-presets.d.ts +8 -0
- package/dist/avatar-presets.js +17 -0
- package/dist/bootstrap.d.ts +109 -1
- package/dist/bootstrap.js +185 -2
- package/dist/cards.d.ts +60 -13
- package/dist/cards.js +88 -18
- package/dist/channel.d.ts +128 -47
- package/dist/channel.js +10 -5
- package/dist/client-css.d.ts +36 -0
- package/dist/client-css.js +326 -0
- package/dist/client-section.d.ts +35 -0
- package/dist/client-section.js +133 -0
- package/dist/config-ui.d.ts +28 -6
- package/dist/config-ui.js +88 -25
- package/dist/config-url-fetch.d.ts +19 -0
- package/dist/config-url-fetch.js +43 -0
- package/dist/connector-auth.d.ts +13 -2
- package/dist/connector-auth.js +82 -8
- package/dist/connector.d.ts +74 -32
- package/dist/context.d.ts +40 -0
- package/dist/context.js +2 -0
- package/dist/ctx.d.ts +224 -30
- package/dist/ctx.js +9 -1
- package/dist/define-extension.d.ts +23 -13
- package/dist/define-extension.js +19 -5
- package/dist/device-denied-paths.d.ts +20 -0
- package/dist/device-denied-paths.js +118 -0
- package/dist/device-operation.d.ts +339 -0
- package/dist/device-operation.js +312 -0
- package/dist/device-protocol.d.ts +285 -50
- package/dist/device-protocol.js +755 -90
- package/dist/device-provider.d.ts +780 -0
- package/dist/device-provider.js +409 -0
- package/dist/device-service.d.ts +126 -0
- package/dist/device-service.js +211 -0
- package/dist/dial-in.d.ts +286 -0
- package/dist/dial-in.js +213 -0
- package/dist/directory.d.ts +15 -12
- package/dist/extension-point.d.ts +2 -0
- package/dist/extension-point.js +7 -3
- package/dist/field-meta.d.ts +19 -0
- package/dist/field-meta.js +54 -0
- package/dist/file-preview.d.ts +43 -10
- package/dist/git-hosts.d.ts +53 -0
- package/dist/git-hosts.js +98 -0
- package/dist/hooks.d.ts +125 -20
- package/dist/hooks.js +5 -0
- package/dist/http-access.d.ts +71 -0
- package/dist/http-access.js +12 -0
- package/dist/identity.d.ts +70 -9
- package/dist/identity.js +46 -1
- package/dist/index.d.ts +28 -4
- package/dist/index.js +28 -4
- package/dist/model-params.d.ts +41 -22
- package/dist/model-params.js +28 -15
- package/dist/model.d.ts +210 -75
- package/dist/model.js +152 -19
- package/dist/native-failure.d.ts +13 -0
- package/dist/native-failure.js +31 -0
- package/dist/net.d.ts +52 -0
- package/dist/net.js +34 -0
- package/dist/network-classes.d.ts +98 -0
- package/dist/network-classes.js +291 -0
- package/dist/permissions.d.ts +65 -22
- package/dist/permissions.js +139 -43
- package/dist/platform-only.d.ts +27 -0
- package/dist/platform-only.js +35 -0
- package/dist/plugin-manifest.d.ts +30 -6
- package/dist/plugin-manifest.js +147 -55
- package/dist/plugin-package.d.ts +82 -39
- package/dist/plugin-package.js +68 -41
- package/dist/push.d.ts +75 -0
- package/dist/push.js +87 -0
- package/dist/sandbox-linux-runtime-source.d.ts +1 -0
- package/dist/sandbox-linux-runtime-source.js +2343 -0
- package/dist/sandbox-native-runtime-source.d.ts +10 -0
- package/dist/sandbox-native-runtime-source.js +480 -0
- package/dist/sandbox-runtime-source.d.ts +47 -0
- package/dist/sandbox-runtime-source.js +73 -0
- package/dist/sandbox-write-denies.d.ts +139 -0
- package/dist/sandbox-write-denies.js +869 -0
- package/dist/secret.d.ts +8 -2
- package/dist/secret.js +11 -4
- package/dist/setup-check.d.ts +128 -0
- package/dist/setup-check.js +142 -0
- package/dist/setup-guide-parse.d.ts +121 -0
- package/dist/setup-guide-parse.js +356 -0
- package/dist/setup-guide-render.d.ts +45 -0
- package/dist/setup-guide-render.js +351 -0
- package/dist/setup-guide.d.ts +86 -72
- package/dist/setup-guide.js +340 -108
- package/dist/shell-capabilities.d.ts +211 -0
- package/dist/shell-capabilities.js +44 -0
- package/dist/shell-environment.d.ts +57 -0
- package/dist/shell-environment.js +21 -0
- package/dist/skill-invocation.d.ts +5 -0
- package/dist/skill-invocation.js +4 -3
- package/dist/skill.d.ts +27 -17
- package/dist/skill.js +40 -26
- package/dist/storage.d.ts +52 -25
- package/dist/svg-viewbox.d.ts +10 -0
- package/dist/svg-viewbox.js +24 -0
- package/dist/theme-assets.d.ts +25 -9
- package/dist/theme-assets.js +87 -47
- package/dist/theme-tokens.generated.d.ts +26 -2
- package/dist/theme-tokens.generated.js +17 -0
- package/dist/theme.d.ts +48 -10
- package/dist/theme.js +100 -21
- package/dist/tool.d.ts +156 -30
- package/dist/tool.js +91 -9
- package/dist/ui-parts.d.ts +23 -0
- package/dist/ui-parts.js +385 -0
- package/dist/ui-resolve.d.ts +35 -0
- package/dist/ui-resolve.js +42 -0
- package/dist/ui-slots.d.ts +65 -0
- package/dist/ui-slots.js +261 -0
- package/dist/ui.d.ts +245 -77
- package/dist/ui.js +104 -27
- package/dist/url.d.ts +4 -1
- package/dist/url.js +4 -1
- package/dist/web.d.ts +26 -20
- package/package.json +12 -1
- package/schemas/bootstrap-extra.v1.schema.json +186 -0
- package/schemas/bootstrap.v1.schema.json +20 -0
- package/schemas/connector-template.v1.schema.json +62 -4
- package/schemas/plugin.v1.schema.json +74 -143
- package/schemas/theme.v1.schema.json +77 -0
- package/agent-docs/extension-points/sandbox.md +0 -15
- package/dist/org-access.d.ts +0 -27
- package/dist/org-access.js +0 -35
- package/dist/sandbox.d.ts +0 -254
- package/dist/sandbox.js +0 -153
package/dist/plugin-package.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import semver from "semver";
|
|
2
2
|
import { ConnectorAuthSchema } from "./connector-auth.js";
|
|
3
3
|
import { EXTENSION_NAME_RE } from "./define-extension.js";
|
|
4
|
+
import { chainMeta, wrapperChain } from "./field-meta.js";
|
|
5
|
+
import { validateGitSource } from "./git-hosts.js";
|
|
4
6
|
/**
|
|
5
7
|
* Plugin package (npm-pack tgz) rules shared by the SDK, the packer and the kernel (spec §4.1). Pure
|
|
6
8
|
* functions only: the kernel reads the tgz and scans imports, `packages/plugin-tools` packs it. The
|
|
@@ -10,16 +12,24 @@ import { EXTENSION_NAME_RE } from "./define-extension.js";
|
|
|
10
12
|
* entry is single-file ESM with third-party dependencies bundled; only {@link HOST_PROVIDED_PACKAGES}
|
|
11
13
|
* come from the host.
|
|
12
14
|
*/
|
|
13
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Packages provided by the host (peerDependencies, linked to the host's copy on install); every
|
|
17
|
+
* other third-party dependency must be bundled into the entry.
|
|
18
|
+
*/
|
|
14
19
|
export const HOST_PROVIDED_PACKAGES = ["@capybara-hq/plugin-sdk", "zod"];
|
|
15
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Scripts npm runs automatically on install: a plugin package must never have any (installs run no
|
|
22
|
+
* scripts, so having them would mislead).
|
|
23
|
+
*/
|
|
16
24
|
export const FORBIDDEN_INSTALL_SCRIPTS = [
|
|
17
25
|
"preinstall",
|
|
18
26
|
"install",
|
|
19
27
|
"postinstall",
|
|
20
28
|
"prepare",
|
|
21
29
|
];
|
|
22
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Dependency fields that must not appear: third-party dependencies must be bundled into the entry.
|
|
32
|
+
*/
|
|
23
33
|
const FORBIDDEN_DEP_FIELDS = [
|
|
24
34
|
"dependencies",
|
|
25
35
|
"optionalDependencies",
|
|
@@ -27,16 +37,20 @@ const FORBIDDEN_DEP_FIELDS = [
|
|
|
27
37
|
"bundledDependencies",
|
|
28
38
|
];
|
|
29
39
|
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
30
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* npm package name rule (simplified): may carry a scope, lowercase, ≤ 214 characters (the length is
|
|
42
|
+
* checked separately).
|
|
43
|
+
*/
|
|
31
44
|
export const NPM_PACKAGE_NAME_RE = /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/;
|
|
32
45
|
const NPM_NAME_RE = NPM_PACKAGE_NAME_RE;
|
|
33
46
|
/**
|
|
34
|
-
*
|
|
47
|
+
* Whether a path relative to the package is safe: non-empty, not starting with `/`, no `\`, control
|
|
48
|
+
* characters or drive letters, no empty segments, `.` or `..` segments, ≤ 1024 characters.
|
|
35
49
|
*/
|
|
36
50
|
export function isSafePackagePath(p) {
|
|
37
51
|
if (typeof p !== "string" || p === "" || p.length > 1024)
|
|
38
52
|
return false;
|
|
39
|
-
// biome-ignore lint/suspicious/noControlCharactersInRegex:
|
|
53
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are exactly what must be rejected
|
|
40
54
|
if (p.startsWith("/") || p.includes("\\") || /[\u0000-\u001f\u007f]/.test(p))
|
|
41
55
|
return false;
|
|
42
56
|
if (/^[A-Za-z]:/.test(p))
|
|
@@ -45,9 +59,11 @@ export function isSafePackagePath(p) {
|
|
|
45
59
|
}
|
|
46
60
|
const isHostPackage = (name) => HOST_PROVIDED_PACKAGES.some((h) => name === h || name.startsWith(`${h}/`));
|
|
47
61
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
62
|
+
* Checks the entry module's imports (C11): static and literal dynamic imports may only be `node:*`,
|
|
63
|
+
* {@link HOST_PROVIDED_PACKAGES} (subpaths included)
|
|
64
|
+
* and in-package relative paths (`./`, `../`; whether they escape the package is judged by the
|
|
65
|
+
* caller from the file location); non-literal dynamic imports are always rejected.
|
|
66
|
+
* Returns the list of problems (empty means valid).
|
|
51
67
|
*/
|
|
52
68
|
export function checkEntryImports(specifiers) {
|
|
53
69
|
const out = [];
|
|
@@ -68,31 +84,31 @@ export function checkEntryImports(specifiers) {
|
|
|
68
84
|
}
|
|
69
85
|
return out;
|
|
70
86
|
}
|
|
71
|
-
const WRAPPERS = new Set(["default", "prefault", "optional", "nullable", "readonly", "catch"]);
|
|
72
87
|
const defOf = (s) => s._zod.def;
|
|
73
|
-
|
|
74
|
-
|
|
88
|
+
/**
|
|
89
|
+
* The same traversal as the kernel's `configFields`: objects are expanded recursively, wrappers
|
|
90
|
+
* such as default / optional are stripped (their meta merged, `secret` from any of them), and a
|
|
91
|
+
* secret on an object is inherited downward.
|
|
92
|
+
*/
|
|
75
93
|
function configFieldsOf(schema, prefix = "", inheritedSecret = false) {
|
|
76
94
|
if (!schema)
|
|
77
95
|
return [];
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
meta ??= metaOf(node);
|
|
83
|
-
}
|
|
84
|
-
const secret = inheritedSecret || meta?.secret === true;
|
|
85
|
-
const def = defOf(node);
|
|
96
|
+
const chain = wrapperChain(schema);
|
|
97
|
+
const meta = chainMeta(schema);
|
|
98
|
+
const secret = inheritedSecret || meta.secret === true;
|
|
99
|
+
const def = defOf(chain[chain.length - 1]);
|
|
86
100
|
if (def.type === "object" && def.shape) {
|
|
87
101
|
return Object.entries(def.shape).flatMap(([k, v]) => configFieldsOf(v, prefix ? `${prefix}.${k}` : k, secret));
|
|
88
102
|
}
|
|
89
|
-
return [{ path: prefix, secret, hot: meta
|
|
103
|
+
return [{ path: prefix, secret, hot: meta.hot === true }];
|
|
90
104
|
}
|
|
91
105
|
const byPath = (a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0;
|
|
92
106
|
const jsonClone = (v) => JSON.parse(JSON.stringify(v));
|
|
93
107
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
108
|
+
* The static part of the real manifest: written into `capybara.manifest` when packing, and compared
|
|
109
|
+
* with the in-package one at load time (`manifest_mismatch`).
|
|
110
|
+
* The result survives a JSON round trip (undefined fields removed); config fields are sorted by
|
|
111
|
+
* path.
|
|
96
112
|
*/
|
|
97
113
|
export function staticManifestOf(m) {
|
|
98
114
|
const out = {
|
|
@@ -105,7 +121,6 @@ export function staticManifestOf(m) {
|
|
|
105
121
|
permissions: jsonClone(m.permissions ?? {}),
|
|
106
122
|
config: {
|
|
107
123
|
deployment: configFieldsOf(m.config?.deployment).sort(byPath),
|
|
108
|
-
org: configFieldsOf(m.config?.org).sort(byPath),
|
|
109
124
|
},
|
|
110
125
|
};
|
|
111
126
|
if (m.ui !== undefined)
|
|
@@ -253,24 +268,27 @@ export function hostSdkProblem(sdk, hostSdkVersion) {
|
|
|
253
268
|
return undefined;
|
|
254
269
|
return `extension.manifest.sdk "${sdk}" is not satisfied by the host SDK version ${hostSdkVersion}`;
|
|
255
270
|
}
|
|
256
|
-
/**
|
|
271
|
+
/** Limits of connector templates and catalog segments. */
|
|
257
272
|
export const CONNECTOR_TEMPLATE_LIMITS = {
|
|
258
|
-
/**
|
|
273
|
+
/** Upper bound on icon bytes. */
|
|
259
274
|
iconBytes: 64 * 1024,
|
|
260
|
-
/**
|
|
275
|
+
/** Upper bound on the characters of the endpoint preset after JSON serialization. */
|
|
261
276
|
endpointChars: 16 * 1024,
|
|
262
|
-
/** `editableEndpoint`
|
|
277
|
+
/** Upper bound on the number of `editableEndpoint` entries. */
|
|
263
278
|
editablePaths: 50,
|
|
264
|
-
/** `defaultToolPolicies`
|
|
279
|
+
/** Upper bound on the number of `defaultToolPolicies` entries. */
|
|
265
280
|
toolPolicies: 1000,
|
|
266
|
-
/** publisher
|
|
281
|
+
/** Upper bound on the characters of publisher. */
|
|
267
282
|
publisherChars: 100,
|
|
268
|
-
/** description
|
|
283
|
+
/** Upper bound on the characters of description (per language when it is `{ zh, en }`). */
|
|
269
284
|
descriptionChars: 1024,
|
|
270
|
-
/** displayName
|
|
285
|
+
/** Upper bound on the characters of displayName per language. */
|
|
271
286
|
displayNameChars: 100,
|
|
272
287
|
};
|
|
273
|
-
/**
|
|
288
|
+
/**
|
|
289
|
+
* Catalog entry name (the same rule as manifest entry names): starts with a lowercase letter or
|
|
290
|
+
* digit, then lowercase letters, digits and hyphens, ≤ 64.
|
|
291
|
+
*/
|
|
274
292
|
export const CATALOG_ITEM_NAME_RE = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
275
293
|
/** An exact semver version (a regex JSON Schema can use). */
|
|
276
294
|
export const SEMVER_VERSION_RE = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?$/;
|
|
@@ -279,8 +297,9 @@ export const SHA512_INTEGRITY_RE = /^sha512-[A-Za-z0-9+/]{86}==$/;
|
|
|
279
297
|
const PATH_SEG_RE = /^[A-Za-z0-9_-]{1,100}$/;
|
|
280
298
|
const PROTO_KEYS = new Set(["__proto__", "prototype", "constructor"]);
|
|
281
299
|
/**
|
|
282
|
-
*
|
|
283
|
-
*
|
|
300
|
+
* Credential-style field names (compared lowercased with `-` and `_` removed): templates are seen
|
|
301
|
+
* by every org, so endpoint presets containing these keys are always rejected.
|
|
302
|
+
* Header names such as `authorization` / `cookie` are listed too (credentials in endpoint headers).
|
|
284
303
|
*/
|
|
285
304
|
const CREDENTIAL_KEYS = new Set([
|
|
286
305
|
"token",
|
|
@@ -305,7 +324,7 @@ const CREDENTIAL_KEYS = new Set([
|
|
|
305
324
|
"sessiontoken",
|
|
306
325
|
]);
|
|
307
326
|
const normKey = (k) => k.toLowerCase().replace(/[-_]/g, "");
|
|
308
|
-
/**
|
|
327
|
+
/** Recursively finds credential-style keys (reports only paths, never echoes values). */
|
|
309
328
|
function credentialKeyPaths(v, at, out, depth = 0) {
|
|
310
329
|
if (depth > 20)
|
|
311
330
|
return;
|
|
@@ -333,14 +352,20 @@ const TEMPLATE_KEYS = new Set([
|
|
|
333
352
|
"auth",
|
|
334
353
|
"ownership",
|
|
335
354
|
"defaultToolPolicies",
|
|
355
|
+
"git",
|
|
336
356
|
"icon",
|
|
337
357
|
]);
|
|
338
358
|
/**
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
359
|
+
* Validates the static part of a connector template ({@link ConnectorTemplateSpec}) and returns the
|
|
360
|
+
* list of problems (empty means valid; paths are relative to the template root, values are not
|
|
361
|
+
* echoed):
|
|
362
|
+
* fields complete, no unknown fields; `auth` conforms to {@link ConnectorAuthSchema} (strict: extra
|
|
363
|
+
* fields such as `token` are rejected);
|
|
364
|
+
* no credential-style key at any level of the endpoint preset; `editableEndpoint` is a list of
|
|
365
|
+
* distinct dotted paths; `ownership` agrees with `auth.ownership`;
|
|
366
|
+
* default policies may only be allow / ask / deny; icon is an in-package .svg / .png path.
|
|
367
|
+
* Whether the endpoint conforms to the connector's `endpointSchema` and whether `editableEndpoint`
|
|
368
|
+
* exists in it is checked by the kernel on import against the loaded connector.
|
|
344
369
|
*/
|
|
345
370
|
export function validateConnectorTemplate(t) {
|
|
346
371
|
if (!isObj(t))
|
|
@@ -439,6 +464,8 @@ export function validateConnectorTemplate(t) {
|
|
|
439
464
|
}
|
|
440
465
|
}
|
|
441
466
|
}
|
|
467
|
+
if (t.git !== undefined)
|
|
468
|
+
errs.push(...validateGitSource(t.git, "template.git"));
|
|
442
469
|
if (t.icon !== undefined) {
|
|
443
470
|
if (typeof t.icon !== "string" || !isSafePackagePath(t.icon) || !/\.(svg|png)$/i.test(t.icon)) {
|
|
444
471
|
errs.push("template.icon must be a relative .svg or .png path inside the package");
|
package/dist/push.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/** Push provider extension point ref (`push-provider@1`): delivers push notifications to mobile apps. */
|
|
2
|
+
export declare const PUSH_PROVIDER_EXTENSION_POINT = "push-provider@1";
|
|
3
|
+
/** Limits the kernel enforces on push registrations and messages. */
|
|
4
|
+
export declare const PUSH_LIMITS: {
|
|
5
|
+
readonly titleChars: 100;
|
|
6
|
+
readonly bodyChars: 240;
|
|
7
|
+
readonly pathChars: 512;
|
|
8
|
+
readonly collapseIdChars: 64;
|
|
9
|
+
readonly tokenChars: 4096;
|
|
10
|
+
readonly appIdChars: 255;
|
|
11
|
+
readonly deviceNameChars: 64;
|
|
12
|
+
readonly sendTimeoutMs: 15000;
|
|
13
|
+
readonly registrationsPerAccount: 20;
|
|
14
|
+
};
|
|
15
|
+
export type PushPlatform = "ios" | "android";
|
|
16
|
+
export interface PushMessage {
|
|
17
|
+
title: string;
|
|
18
|
+
body: string;
|
|
19
|
+
/** In-app path opened on tap; starts with `/`. */
|
|
20
|
+
path: string;
|
|
21
|
+
threadId: string;
|
|
22
|
+
/**
|
|
23
|
+
* The same id for every send of one event to one device (1..64 of `A-Z a-z 0-9 . _ -`): map it
|
|
24
|
+
* to the channel's collapse key (APNs `apns-collapse-id`, FCM `collapse_key`), so a resend after
|
|
25
|
+
* a crash or a timeout replaces the notification instead of showing it twice.
|
|
26
|
+
*/
|
|
27
|
+
collapseId: string;
|
|
28
|
+
badge?: number;
|
|
29
|
+
/** The deployment origin (scheme://host[:port]); the shell matches it to a profile. */
|
|
30
|
+
origin: string;
|
|
31
|
+
}
|
|
32
|
+
/** One device registration a message is sent to. */
|
|
33
|
+
export interface PushTarget {
|
|
34
|
+
id: string;
|
|
35
|
+
platform: PushPlatform;
|
|
36
|
+
appId: string;
|
|
37
|
+
token: string;
|
|
38
|
+
}
|
|
39
|
+
export type PushErrorKind = "auth" | "rate_limited" | "transient";
|
|
40
|
+
/** Thrown by a provider; applies to every registration of the call. */
|
|
41
|
+
export declare class PushError extends Error {
|
|
42
|
+
readonly kind: PushErrorKind;
|
|
43
|
+
readonly retryAfterMs?: number;
|
|
44
|
+
constructor(message: string, opts: {
|
|
45
|
+
kind: PushErrorKind;
|
|
46
|
+
retryAfterMs?: number;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
export type PushResult = {
|
|
50
|
+
registrationId: string;
|
|
51
|
+
status: "delivered";
|
|
52
|
+
} | {
|
|
53
|
+
registrationId: string;
|
|
54
|
+
status: "unregistered";
|
|
55
|
+
} | {
|
|
56
|
+
registrationId: string;
|
|
57
|
+
status: "failed";
|
|
58
|
+
kind: PushErrorKind;
|
|
59
|
+
message: string;
|
|
60
|
+
retryAfterMs?: number;
|
|
61
|
+
};
|
|
62
|
+
/** No retries inside; a thrown PushError applies to every registration of the call, any other throw is transient. */
|
|
63
|
+
export interface PushProvider {
|
|
64
|
+
/** App ids (bundle / application ids) the configured credentials cover; synchronous, from config. */
|
|
65
|
+
apps(): readonly string[];
|
|
66
|
+
send(args: {
|
|
67
|
+
registrations: readonly PushTarget[];
|
|
68
|
+
message: PushMessage;
|
|
69
|
+
signal: AbortSignal;
|
|
70
|
+
}): Promise<PushResult[]>;
|
|
71
|
+
}
|
|
72
|
+
/** null when `results` answers every registration exactly once, else the problem (no token in it). */
|
|
73
|
+
export declare function checkPushResults(registrations: readonly PushTarget[], results: unknown): string | null;
|
|
74
|
+
/** null when the message is within PUSH_LIMITS and well formed, else the problem. */
|
|
75
|
+
export declare function validatePushMessage(m: PushMessage): string | null;
|
package/dist/push.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Push provider extension point ref (`push-provider@1`): delivers push notifications to mobile apps. */
|
|
2
|
+
export const PUSH_PROVIDER_EXTENSION_POINT = "push-provider@1";
|
|
3
|
+
/** Limits the kernel enforces on push registrations and messages. */
|
|
4
|
+
export const PUSH_LIMITS = {
|
|
5
|
+
titleChars: 100,
|
|
6
|
+
bodyChars: 240,
|
|
7
|
+
pathChars: 512,
|
|
8
|
+
collapseIdChars: 64,
|
|
9
|
+
tokenChars: 4096,
|
|
10
|
+
appIdChars: 255,
|
|
11
|
+
deviceNameChars: 64,
|
|
12
|
+
sendTimeoutMs: 15_000,
|
|
13
|
+
registrationsPerAccount: 20,
|
|
14
|
+
};
|
|
15
|
+
/** Thrown by a provider; applies to every registration of the call. */
|
|
16
|
+
export class PushError extends Error {
|
|
17
|
+
kind;
|
|
18
|
+
retryAfterMs;
|
|
19
|
+
constructor(message, opts) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "PushError";
|
|
22
|
+
this.kind = opts.kind;
|
|
23
|
+
if (opts.retryAfterMs !== undefined)
|
|
24
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const KINDS = ["auth", "rate_limited", "transient"];
|
|
28
|
+
/** null when `results` answers every registration exactly once, else the problem (no token in it). */
|
|
29
|
+
export function checkPushResults(registrations, results) {
|
|
30
|
+
if (!Array.isArray(results))
|
|
31
|
+
return "push provider returned a non-array result";
|
|
32
|
+
const known = new Set(registrations.map((r) => r.id));
|
|
33
|
+
const seen = new Set();
|
|
34
|
+
for (const r of results) {
|
|
35
|
+
if (typeof r !== "object" || r === null)
|
|
36
|
+
return "push provider returned a non-object result";
|
|
37
|
+
const o = r;
|
|
38
|
+
const id = o.registrationId;
|
|
39
|
+
if (typeof id !== "string" || !known.has(id)) {
|
|
40
|
+
return "push provider answered an unknown registration id";
|
|
41
|
+
}
|
|
42
|
+
if (seen.has(id))
|
|
43
|
+
return `push provider answered registration ${id} more than once`;
|
|
44
|
+
seen.add(id);
|
|
45
|
+
if (o.status === "failed") {
|
|
46
|
+
if (typeof o.kind !== "string" || !KINDS.includes(o.kind)) {
|
|
47
|
+
return `push provider returned a failed result without a known kind for ${id}`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else if (o.status !== "delivered" && o.status !== "unregistered") {
|
|
51
|
+
return `push provider returned an unknown status for ${id}`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (seen.size !== known.size)
|
|
55
|
+
return "push provider left a registration unanswered";
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
/** null when the message is within PUSH_LIMITS and well formed, else the problem. */
|
|
59
|
+
export function validatePushMessage(m) {
|
|
60
|
+
if (m.title.length > PUSH_LIMITS.titleChars) {
|
|
61
|
+
return `title exceeds ${PUSH_LIMITS.titleChars} characters`;
|
|
62
|
+
}
|
|
63
|
+
if (m.body.length > PUSH_LIMITS.bodyChars) {
|
|
64
|
+
return `body exceeds ${PUSH_LIMITS.bodyChars} characters`;
|
|
65
|
+
}
|
|
66
|
+
if (!m.path.startsWith("/") || m.path.length > PUSH_LIMITS.pathChars) {
|
|
67
|
+
return `path must start with "/" and be at most ${PUSH_LIMITS.pathChars} characters`;
|
|
68
|
+
}
|
|
69
|
+
if (m.collapseId.length === 0 ||
|
|
70
|
+
m.collapseId.length > PUSH_LIMITS.collapseIdChars ||
|
|
71
|
+
!/^[A-Za-z0-9._-]+$/.test(m.collapseId)) {
|
|
72
|
+
return `collapseId must be 1 to ${PUSH_LIMITS.collapseIdChars} characters of A-Z a-z 0-9 . _ -`;
|
|
73
|
+
}
|
|
74
|
+
let url;
|
|
75
|
+
try {
|
|
76
|
+
url = new URL(m.origin);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return "origin must be scheme://host[:port]";
|
|
80
|
+
}
|
|
81
|
+
if ((url.protocol !== "https:" && url.protocol !== "http:") ||
|
|
82
|
+
url.origin !== m.origin ||
|
|
83
|
+
m.origin.endsWith("/")) {
|
|
84
|
+
return "origin must be scheme://host[:port] with no path";
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const LINUX_SANDBOX_SOURCE: string;
|