@tryghost/app-sdk 0.1.0-alpha.6 → 0.1.0-alpha.7
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 +99 -10
- package/build/backend.d.ts +1 -10
- package/build/backend.js +1 -108
- package/build/backend.js.map +1 -1
- package/build/chunks/manifest-CInTdKjI.d.ts +19 -0
- package/build/chunks/manifest-DX1GTozy.js +219 -0
- package/build/chunks/manifest-DX1GTozy.js.map +1 -0
- package/build/chunks/rules-_dou3jRF.js +159 -0
- package/build/chunks/rules-_dou3jRF.js.map +1 -0
- package/build/index.js +2 -153
- package/build/index.js.map +1 -1
- package/build/tooling.d.ts +177 -0
- package/build/tooling.js +1130 -0
- package/build/tooling.js.map +1 -0
- package/dist/app-sdk.js +1 -1
- package/package.json +6 -2
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
//#region ../app-contracts/build/plain-text.js
|
|
2
|
+
var ALLOWED = "\\p{L}\\p{M}\\p{N}\\p{P}\\p{S}\\p{Zs}\\u200C\\u200D";
|
|
3
|
+
var PLAIN_TEXT = new RegExp(`^[${ALLOWED}]*$`, "u");
|
|
4
|
+
var NOT_PLAIN_TEXT = new RegExp(`[^${ALLOWED}]`, "gu");
|
|
5
|
+
/** Whether text is one line of plain text, as a name, a description or a title must be. */
|
|
6
|
+
function isPlainText(value) {
|
|
7
|
+
return PLAIN_TEXT.test(value);
|
|
8
|
+
}
|
|
9
|
+
/** Returns the text with the characters `isPlainText` refuses removed. */
|
|
10
|
+
function toPlainText(value) {
|
|
11
|
+
return value.replace(NOT_PLAIN_TEXT, "");
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region ../app-contracts/build/bridge/rules.js
|
|
15
|
+
/**
|
|
16
|
+
* The bridge's rules that have no dependencies. The root entry point exports them so the SDK
|
|
17
|
+
* can use them without zod. The schemas in this folder build on them, so the SDK and Admin
|
|
18
|
+
* read a path or a refusal the same way.
|
|
19
|
+
*
|
|
20
|
+
* What Admin sends the page has no schema, because Admin builds it and never parses it. Its
|
|
21
|
+
* types are declared here, next to the guards the SDK checks it with. The SDK trusts Admin, so
|
|
22
|
+
* the guards only check shape and path containment. They turn a mismatch into a clear error
|
|
23
|
+
* rather than `undefined` somewhere in the app. Only the schemas check limits.
|
|
24
|
+
*/
|
|
25
|
+
/** Posted by the app's page to `window.parent` once the SDK has loaded. */
|
|
26
|
+
var HELLO_MESSAGE_TYPE = "ghost-app:hello";
|
|
27
|
+
/**
|
|
28
|
+
* How long an app's page has to start, in milliseconds. Admin gives up on a frame this long
|
|
29
|
+
* after mounting it, and the SDK waits this long for Admin to start the app unless the app
|
|
30
|
+
* passes its own `timeout`. Long enough for a slow page on a slow connection to load and send
|
|
31
|
+
* its hello.
|
|
32
|
+
*/
|
|
33
|
+
var START_TIMEOUT_MS = 1e4;
|
|
34
|
+
/**
|
|
35
|
+
* The methods an app may use, in uppercase: the ones the allowlist has routes for. The SDK
|
|
36
|
+
* uppercases what apps pass.
|
|
37
|
+
*/
|
|
38
|
+
var FETCH_METHODS = [
|
|
39
|
+
"GET",
|
|
40
|
+
"POST",
|
|
41
|
+
"PUT",
|
|
42
|
+
"DELETE"
|
|
43
|
+
];
|
|
44
|
+
/**
|
|
45
|
+
* Whether a request with this method may carry a body. Expects an uppercase method from
|
|
46
|
+
* `FETCH_METHODS`. The Admin API reads no body on a `GET`, and fetch refuses to send one.
|
|
47
|
+
*/
|
|
48
|
+
function methodAllowsBody(method) {
|
|
49
|
+
return method !== "GET";
|
|
50
|
+
}
|
|
51
|
+
var RESERVED_REQUEST_HEADER = /^(?:cookie|authorization|x-ghost-.*|app-pragma|host|origin|referer|content-length|content-type|content-encoding|x-forwarded-.*|forwarded|x-real-ip|via)$/i;
|
|
52
|
+
/**
|
|
53
|
+
* Whether a request header is one that apps may not set, in any case. Admin refuses a request
|
|
54
|
+
* that sets one rather than dropping the header, so the app gets an error instead of wondering
|
|
55
|
+
* why the header never arrived. The browser refuses some of these anyway; they are listed here
|
|
56
|
+
* so that none of them relies on the browser.
|
|
57
|
+
*/
|
|
58
|
+
function isReservedRequestHeader(name) {
|
|
59
|
+
return RESERVED_REQUEST_HEADER.test(name);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The messages Admin and the SDK use when they refuse an argument for the same reason, so an
|
|
63
|
+
* app gets the same message whichever side catches the problem. The SDK checks only for obvious
|
|
64
|
+
* mistakes, before anything crosses the channel; Admin's schemas check everything again.
|
|
65
|
+
*/
|
|
66
|
+
var ARGUMENT_MESSAGES = {
|
|
67
|
+
fetchPath: "Expected an Admin API path such as /posts/, not a URL",
|
|
68
|
+
fetchMethod: `Expected a method of: ${FETCH_METHODS.join(", ")}`,
|
|
69
|
+
fetchBody: "Expected no body on a GET request",
|
|
70
|
+
reservedHeader: (name) => `Expected no ${name.toLowerCase()} header, which only Admin sets`,
|
|
71
|
+
jsonBody: "Expected the body as JSON text",
|
|
72
|
+
appPath: "Expected a path within the app, starting with a single /",
|
|
73
|
+
pathMove: "Expected a move of push or replace",
|
|
74
|
+
place: "Expected a place such as { post: { id } }, { page: { id } }, { member: { id } } or { apps: true }"
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* The codes a refused call carries across the channel. The structured clone that carries an
|
|
78
|
+
* error keeps only its message, so the refusing side puts the code at the start of the
|
|
79
|
+
* message, as in `blocked: Apps can't use GET /db/`, and the other side parses it back out. Codes
|
|
80
|
+
* are only ever added: an SDK that doesn't know a code treats the whole message as a generic
|
|
81
|
+
* refusal.
|
|
82
|
+
*
|
|
83
|
+
* The host codes, which Admin refuses the page's calls with:
|
|
84
|
+
*
|
|
85
|
+
* - `session_ended`: the session is over; the page is about to be replaced.
|
|
86
|
+
* - `invalid_argument`: an argument Admin does not take.
|
|
87
|
+
* - `invalid_path`: a path that is not an Admin API path or a route within the app, or one
|
|
88
|
+
* the Admin API only responds to with a redirect, such as a path without its trailing `/`.
|
|
89
|
+
* - `blocked`: a request the allowlist doesn't allow, or a write it refuses.
|
|
90
|
+
* - `network_error`: Admin could not reach its own API.
|
|
91
|
+
* - `rejected`: anything else; the detail says why.
|
|
92
|
+
*/
|
|
93
|
+
var HOST_ERROR_CODES = [
|
|
94
|
+
"session_ended",
|
|
95
|
+
"invalid_argument",
|
|
96
|
+
"invalid_path",
|
|
97
|
+
"blocked",
|
|
98
|
+
"network_error",
|
|
99
|
+
"rejected"
|
|
100
|
+
];
|
|
101
|
+
[...HOST_ERROR_CODES];
|
|
102
|
+
var CODED_MESSAGE = /^([a-z_]+): ([\s\S]*)$/;
|
|
103
|
+
/**
|
|
104
|
+
* Builds the message a refusal crosses the channel as: `code: detail`. Each side should pass
|
|
105
|
+
* only its own codes; typing its refusals as `HostErrorCode` or `AppErrorCode` enforces that.
|
|
106
|
+
*/
|
|
107
|
+
function encodeBridgeError(code, detail) {
|
|
108
|
+
return `${code}: ${detail}`;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Reads the code and detail back from a refusal's `code: detail` message. Takes the codes the
|
|
112
|
+
* other side may send: `HOST_ERROR_CODES` for a refusal from Admin, and `APP_ERROR_CODES` for
|
|
113
|
+
* one from the page. Returns `undefined` for a message that doesn't start with one of them.
|
|
114
|
+
*/
|
|
115
|
+
function decodeBridgeError(message, codes) {
|
|
116
|
+
const [, code = "", detail = ""] = CODED_MESSAGE.exec(message) ?? [];
|
|
117
|
+
return codes.includes(code) ? {
|
|
118
|
+
code,
|
|
119
|
+
detail
|
|
120
|
+
} : void 0;
|
|
121
|
+
}
|
|
122
|
+
var THEME_MODES = ["light", "dark"];
|
|
123
|
+
var CONTROL_CHARACTER = /\p{Cc}/u;
|
|
124
|
+
/**
|
|
125
|
+
* Whether a value is a path that stays within the base it is resolved against, rather than
|
|
126
|
+
* escaping it. It must start with a single `/`, so it can't carry a scheme or be read as
|
|
127
|
+
* `//host`. It must not contain `\`, which URL parsers read as `/`, or any control character.
|
|
128
|
+
*/
|
|
129
|
+
function isContainedPath(value) {
|
|
130
|
+
return typeof value === "string" && value.startsWith("/") && !value.startsWith("//") && !value.includes("\\") && !CONTROL_CHARACTER.test(value);
|
|
131
|
+
}
|
|
132
|
+
/** An object, or an array: anything whose fields can be read. */
|
|
133
|
+
function isRecord(value) {
|
|
134
|
+
return typeof value === "object" && value !== null;
|
|
135
|
+
}
|
|
136
|
+
function hasStrings(value, keys) {
|
|
137
|
+
return isRecord(value) && keys.every((key) => typeof value[key] === "string");
|
|
138
|
+
}
|
|
139
|
+
function isTheme(value) {
|
|
140
|
+
return hasStrings(value, ["accent_color"]) && THEME_MODES.includes(value.mode);
|
|
141
|
+
}
|
|
142
|
+
function isStartContext(value) {
|
|
143
|
+
return isRecord(value) && hasStrings(value.site, [
|
|
144
|
+
"url",
|
|
145
|
+
"title",
|
|
146
|
+
"timezone",
|
|
147
|
+
"locale"
|
|
148
|
+
]) && hasStrings(value.user, ["id", "role"]) && isTheme(value.theme) && isRecord(value.session) && Number.isSafeInteger(value.session.revision) && value.session.revision >= 0 && isContainedPath(value.path);
|
|
149
|
+
}
|
|
150
|
+
function isConnectMessage(value) {
|
|
151
|
+
return isRecord(value) && value.type === "ghost-app:connect" && Array.isArray(value.exports) && value.exports.every((name) => typeof name === "string");
|
|
152
|
+
}
|
|
153
|
+
function isFetchResponse(value) {
|
|
154
|
+
return isRecord(value) && Number.isSafeInteger(value.status) && value.status >= 200 && value.status <= 599 && typeof value.statusText === "string" && isRecord(value.headers) && !Array.isArray(value.headers) && Object.values(value.headers).every((header) => typeof header === "string") && typeof value.body === "string";
|
|
155
|
+
}
|
|
156
|
+
//#endregion
|
|
157
|
+
export { toPlainText as _, START_TIMEOUT_MS as a, isConnectMessage as c, isRecord as d, isReservedRequestHeader as f, isPlainText as g, methodAllowsBody as h, HOST_ERROR_CODES as i, isContainedPath as l, isTheme as m, FETCH_METHODS as n, decodeBridgeError as o, isStartContext as p, HELLO_MESSAGE_TYPE as r, encodeBridgeError as s, ARGUMENT_MESSAGES as t, isFetchResponse as u };
|
|
158
|
+
|
|
159
|
+
//# sourceMappingURL=rules-_dou3jRF.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules-_dou3jRF.js","names":[],"sources":["../../../app-contracts/build/plain-text.js","../../../app-contracts/build/bridge/rules.js"],"sourcesContent":["// What plain text may contain: letters, combining marks, digits, punctuation, symbols and\n// spaces, plus the two joiners that emoji sequences and scripts such as Persian need.\n// Everything else is refused by not being listed: control and formatting characters, line\n// breaks, the characters that reorder text, private-use and unassigned code points. Those can\n// make text display as something other than what it is, such as a title that reverses\n// whatever Admin shows after it.\nconst ALLOWED = '\\\\p{L}\\\\p{M}\\\\p{N}\\\\p{P}\\\\p{S}\\\\p{Zs}\\\\u200C\\\\u200D';\nconst PLAIN_TEXT = new RegExp(`^[${ALLOWED}]*$`, 'u');\nconst NOT_PLAIN_TEXT = new RegExp(`[^${ALLOWED}]`, 'gu');\n/** Whether text is one line of plain text, as a name, a description or a title must be. */\nexport function isPlainText(value) {\n return PLAIN_TEXT.test(value);\n}\n/** Returns the text with the characters `isPlainText` refuses removed. */\nexport function toPlainText(value) {\n return value.replace(NOT_PLAIN_TEXT, '');\n}\n//# sourceMappingURL=plain-text.js.map","/**\n * The bridge's rules that have no dependencies. The root entry point exports them so the SDK\n * can use them without zod. The schemas in this folder build on them, so the SDK and Admin\n * read a path or a refusal the same way.\n *\n * What Admin sends the page has no schema, because Admin builds it and never parses it. Its\n * types are declared here, next to the guards the SDK checks it with. The SDK trusts Admin, so\n * the guards only check shape and path containment. They turn a mismatch into a clear error\n * rather than `undefined` somewhere in the app. Only the schemas check limits.\n */\n/** Posted by the app's page to `window.parent` once the SDK has loaded. */\nexport const HELLO_MESSAGE_TYPE = 'ghost-app:hello';\n/** Posted by Admin to the frame that sent the hello, with the channel's port attached. */\nexport const CONNECT_MESSAGE_TYPE = 'ghost-app:connect';\n/**\n * How long an app's page has to start, in milliseconds. Admin gives up on a frame this long\n * after mounting it, and the SDK waits this long for Admin to start the app unless the app\n * passes its own `timeout`. Long enough for a slow page on a slow connection to load and send\n * its hello.\n */\nexport const START_TIMEOUT_MS = 10_000;\n/** Long enough for any real page title, short enough that Admin's own name still shows. */\nexport const TITLE_MAX_LENGTH = 200;\n/**\n * The methods an app may use, in uppercase: the ones the allowlist has routes for. The SDK\n * uppercases what apps pass.\n */\nexport const FETCH_METHODS = ['GET', 'POST', 'PUT', 'DELETE'];\n/**\n * Whether a request with this method may carry a body. Expects an uppercase method from\n * `FETCH_METHODS`. The Admin API reads no body on a `GET`, and fetch refuses to send one.\n */\nexport function methodAllowsBody(method) {\n return method !== 'GET';\n}\n// The headers Admin sets itself on a request it makes for an app: the credentials, which only\n// Admin may send; Admin's own markers, which Ghost core treats as coming from Admin; the headers\n// that describe the connection, or the proxies along the way, rather than the request; and the\n// ones that say how to read the body. A body is always JSON, which Admin labels itself: the Admin\n// API also reads forms and uploads, and decodes JSON in any charset it's told, so a content type\n// of the app's choosing could carry a write past the checks on what it contains.\nconst RESERVED_REQUEST_HEADER = /^(?:cookie|authorization|x-ghost-.*|app-pragma|host|origin|referer|content-length|content-type|content-encoding|x-forwarded-.*|forwarded|x-real-ip|via)$/i;\n/**\n * Whether a request header is one that apps may not set, in any case. Admin refuses a request\n * that sets one rather than dropping the header, so the app gets an error instead of wondering\n * why the header never arrived. The browser refuses some of these anyway; they are listed here\n * so that none of them relies on the browser.\n */\nexport function isReservedRequestHeader(name) {\n return RESERVED_REQUEST_HEADER.test(name);\n}\n/**\n * The messages Admin and the SDK use when they refuse an argument for the same reason, so an\n * app gets the same message whichever side catches the problem. The SDK checks only for obvious\n * mistakes, before anything crosses the channel; Admin's schemas check everything again.\n */\nexport const ARGUMENT_MESSAGES = {\n fetchPath: 'Expected an Admin API path such as /posts/, not a URL',\n fetchMethod: `Expected a method of: ${FETCH_METHODS.join(', ')}`,\n fetchBody: 'Expected no body on a GET request',\n // Header names are case-insensitive, and `Headers` lowercases them on the SDK's side, so the\n // name is lowercased here too and both sides produce the same message.\n reservedHeader: (name) => `Expected no ${name.toLowerCase()} header, which only Admin sets`,\n jsonBody: 'Expected the body as JSON text',\n appPath: 'Expected a path within the app, starting with a single /',\n pathMove: 'Expected a move of push or replace',\n place: 'Expected a place such as { post: { id } }, { page: { id } }, { member: { id } } or { apps: true }',\n};\n/**\n * The statuses a `fetch` response may have. The SDK turns the response back into a\n * `Response`, which only accepts statuses from 200 to 599. Fetch never returns an\n * informational (1xx) status to scripts anyway.\n */\nexport const FETCH_RESPONSE_STATUS_MIN = 200;\nexport const FETCH_RESPONSE_STATUS_MAX = 599;\n/**\n * The codes a refused call carries across the channel. The structured clone that carries an\n * error keeps only its message, so the refusing side puts the code at the start of the\n * message, as in `blocked: Apps can't use GET /db/`, and the other side parses it back out. Codes\n * are only ever added: an SDK that doesn't know a code treats the whole message as a generic\n * refusal.\n *\n * The host codes, which Admin refuses the page's calls with:\n *\n * - `session_ended`: the session is over; the page is about to be replaced.\n * - `invalid_argument`: an argument Admin does not take.\n * - `invalid_path`: a path that is not an Admin API path or a route within the app, or one\n * the Admin API only responds to with a redirect, such as a path without its trailing `/`.\n * - `blocked`: a request the allowlist doesn't allow, or a write it refuses.\n * - `network_error`: Admin could not reach its own API.\n * - `rejected`: anything else; the detail says why.\n */\nexport const HOST_ERROR_CODES = [\n 'session_ended',\n 'invalid_argument',\n 'invalid_path',\n 'blocked',\n 'network_error',\n 'rejected',\n];\n/**\n * The app codes, which the page refuses Admin's calls with:\n *\n * - `invalid_context`: a start context the SDK cannot read, or a call before the app has\n * started.\n * - `invalid_argument`: an argument the SDK does not take, such as a theme it cannot read.\n */\nexport const APP_ERROR_CODES = ['invalid_context', 'invalid_argument'];\n/** Every code a refusal can carry, in either direction. */\nexport const BRIDGE_ERROR_CODES = [...HOST_ERROR_CODES, 'invalid_context'];\nconst CODED_MESSAGE = /^([a-z_]+): ([\\s\\S]*)$/;\n/**\n * Builds the message a refusal crosses the channel as: `code: detail`. Each side should pass\n * only its own codes; typing its refusals as `HostErrorCode` or `AppErrorCode` enforces that.\n */\nexport function encodeBridgeError(code, detail) {\n return `${code}: ${detail}`;\n}\n/**\n * Reads the code and detail back from a refusal's `code: detail` message. Takes the codes the\n * other side may send: `HOST_ERROR_CODES` for a refusal from Admin, and `APP_ERROR_CODES` for\n * one from the page. Returns `undefined` for a message that doesn't start with one of them.\n */\nexport function decodeBridgeError(message, codes) {\n const [, code = '', detail = ''] = CODED_MESSAGE.exec(message) ?? [];\n return codes.includes(code) ? { code: code, detail } : undefined;\n}\nconst THEME_MODES = ['light', 'dark'];\n// URL parsers and routers don't agree on whether to strip or refuse control characters, so a\n// path containing one can mean something different to each of them.\nconst CONTROL_CHARACTER = /\\p{Cc}/u;\n/**\n * Whether a value is a path that stays within the base it is resolved against, rather than\n * escaping it. It must start with a single `/`, so it can't carry a scheme or be read as\n * `//host`. It must not contain `\\`, which URL parsers read as `/`, or any control character.\n */\nexport function isContainedPath(value) {\n return (typeof value === 'string' &&\n value.startsWith('/') &&\n !value.startsWith('//') &&\n !value.includes('\\\\') &&\n !CONTROL_CHARACTER.test(value));\n}\n// Used only as a base for parsing, never requested. Parsing against it gives a path the URL\n// parser's normalised form.\nconst APP_PATH_BASE = 'https://app.invalid';\n/**\n * Returns an app path in the URL parser's normalised form, the one form Admin and the SDK\n * store it in. `.` and `..` are resolved within the app, so `..` can never climb above its root.\n * Characters that need escaping are escaped once, so a path the app reported compares equal to\n * the same path read back from Admin's URL. `/search?q=a b` becomes `/search?q=a%20b`, and\n * `/x/../episodes/1` becomes `/episodes/1`.\n *\n * Expects a path that `isContainedPath` accepts; anything else may not be a path within the\n * app at all. Even then, the result may not be: resolving `.` and `..` can leave `//` at the\n * start, so `/.//host` becomes `//host`, which resolves to another site. Check the result with\n * `isContainedPath` before resolving it against anything. `AppPathSchema` does both and returns\n * this form.\n */\nexport function normaliseAppPath(path) {\n const url = new URL(path, APP_PATH_BASE);\n return `${url.pathname}${url.search}${url.hash}`;\n}\n/** An object, or an array: anything whose fields can be read. */\nexport function isRecord(value) {\n return typeof value === 'object' && value !== null;\n}\nfunction hasStrings(value, keys) {\n return isRecord(value) && keys.every((key) => typeof value[key] === 'string');\n}\nexport function isTheme(value) {\n return (hasStrings(value, ['accent_color']) && THEME_MODES.includes(value.mode));\n}\nexport function isStartContext(value) {\n return (isRecord(value) &&\n hasStrings(value.site, ['url', 'title', 'timezone', 'locale']) &&\n hasStrings(value.user, ['id', 'role']) &&\n isTheme(value.theme) &&\n isRecord(value.session) &&\n Number.isSafeInteger(value.session.revision) &&\n value.session.revision >= 0 &&\n isContainedPath(value.path));\n}\nexport function isConnectMessage(value) {\n return (isRecord(value) &&\n value.type === CONNECT_MESSAGE_TYPE &&\n Array.isArray(value.exports) &&\n value.exports.every((name) => typeof name === 'string'));\n}\nexport function isFetchResponse(value) {\n return (isRecord(value) &&\n Number.isSafeInteger(value.status) &&\n value.status >= FETCH_RESPONSE_STATUS_MIN &&\n value.status <= FETCH_RESPONSE_STATUS_MAX &&\n typeof value.statusText === 'string' &&\n isRecord(value.headers) &&\n !Array.isArray(value.headers) &&\n Object.values(value.headers).every((header) => typeof header === 'string') &&\n typeof value.body === 'string');\n}\n//# sourceMappingURL=rules.js.map"],"mappings":"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"}
|
package/build/index.js
CHANGED
|
@@ -1,156 +1,5 @@
|
|
|
1
|
+
import { _ as toPlainText, a as START_TIMEOUT_MS, c as isConnectMessage, d as isRecord, f as isReservedRequestHeader, h as methodAllowsBody, i as HOST_ERROR_CODES, l as isContainedPath, m as isTheme, n as FETCH_METHODS, o as decodeBridgeError, p as isStartContext, r as HELLO_MESSAGE_TYPE, s as encodeBridgeError, t as ARGUMENT_MESSAGES, u as isFetchResponse } from "./chunks/rules-_dou3jRF.js";
|
|
1
2
|
import { ThreadMessagePort } from "@quilted/threads";
|
|
2
|
-
//#region ../app-contracts/build/plain-text.js
|
|
3
|
-
var ALLOWED = "\\p{L}\\p{M}\\p{N}\\p{P}\\p{S}\\p{Zs}\\u200C\\u200D";
|
|
4
|
-
new RegExp(`^[${ALLOWED}]*$`, "u");
|
|
5
|
-
var NOT_PLAIN_TEXT = new RegExp(`[^${ALLOWED}]`, "gu");
|
|
6
|
-
/** Returns the text with the characters `isPlainText` refuses removed. */
|
|
7
|
-
function toPlainText(value) {
|
|
8
|
-
return value.replace(NOT_PLAIN_TEXT, "");
|
|
9
|
-
}
|
|
10
|
-
//#endregion
|
|
11
|
-
//#region ../app-contracts/build/bridge/rules.js
|
|
12
|
-
/**
|
|
13
|
-
* The bridge's rules that have no dependencies. The root entry point exports them so the SDK
|
|
14
|
-
* can use them without zod. The schemas in this folder build on them, so the SDK and Admin
|
|
15
|
-
* read a path or a refusal the same way.
|
|
16
|
-
*
|
|
17
|
-
* What Admin sends the page has no schema, because Admin builds it and never parses it. Its
|
|
18
|
-
* types are declared here, next to the guards the SDK checks it with. The SDK trusts Admin, so
|
|
19
|
-
* the guards only check shape and path containment. They turn a mismatch into a clear error
|
|
20
|
-
* rather than `undefined` somewhere in the app. Only the schemas check limits.
|
|
21
|
-
*/
|
|
22
|
-
/** Posted by the app's page to `window.parent` once the SDK has loaded. */
|
|
23
|
-
var HELLO_MESSAGE_TYPE = "ghost-app:hello";
|
|
24
|
-
/**
|
|
25
|
-
* How long an app's page has to start, in milliseconds. Admin gives up on a frame this long
|
|
26
|
-
* after mounting it, and the SDK waits this long for Admin to start the app unless the app
|
|
27
|
-
* passes its own `timeout`. Long enough for a slow page on a slow connection to load and send
|
|
28
|
-
* its hello.
|
|
29
|
-
*/
|
|
30
|
-
var START_TIMEOUT_MS = 1e4;
|
|
31
|
-
/**
|
|
32
|
-
* The methods an app may use, in uppercase: the ones the allowlist has routes for. The SDK
|
|
33
|
-
* uppercases what apps pass.
|
|
34
|
-
*/
|
|
35
|
-
var FETCH_METHODS = [
|
|
36
|
-
"GET",
|
|
37
|
-
"POST",
|
|
38
|
-
"PUT",
|
|
39
|
-
"DELETE"
|
|
40
|
-
];
|
|
41
|
-
/**
|
|
42
|
-
* Whether a request with this method may carry a body. Expects an uppercase method from
|
|
43
|
-
* `FETCH_METHODS`. The Admin API reads no body on a `GET`, and fetch refuses to send one.
|
|
44
|
-
*/
|
|
45
|
-
function methodAllowsBody(method) {
|
|
46
|
-
return method !== "GET";
|
|
47
|
-
}
|
|
48
|
-
var RESERVED_REQUEST_HEADER = /^(?:cookie|authorization|x-ghost-.*|app-pragma|host|origin|referer|content-length|content-type|content-encoding|x-forwarded-.*|forwarded|x-real-ip|via)$/i;
|
|
49
|
-
/**
|
|
50
|
-
* Whether a request header is one that apps may not set, in any case. Admin refuses a request
|
|
51
|
-
* that sets one rather than dropping the header, so the app gets an error instead of wondering
|
|
52
|
-
* why the header never arrived. The browser refuses some of these anyway; they are listed here
|
|
53
|
-
* so that none of them relies on the browser.
|
|
54
|
-
*/
|
|
55
|
-
function isReservedRequestHeader(name) {
|
|
56
|
-
return RESERVED_REQUEST_HEADER.test(name);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* The messages Admin and the SDK use when they refuse an argument for the same reason, so an
|
|
60
|
-
* app gets the same message whichever side catches the problem. The SDK checks only for obvious
|
|
61
|
-
* mistakes, before anything crosses the channel; Admin's schemas check everything again.
|
|
62
|
-
*/
|
|
63
|
-
var ARGUMENT_MESSAGES = {
|
|
64
|
-
fetchPath: "Expected an Admin API path such as /posts/, not a URL",
|
|
65
|
-
fetchMethod: `Expected a method of: ${FETCH_METHODS.join(", ")}`,
|
|
66
|
-
fetchBody: "Expected no body on a GET request",
|
|
67
|
-
reservedHeader: (name) => `Expected no ${name.toLowerCase()} header, which only Admin sets`,
|
|
68
|
-
jsonBody: "Expected the body as JSON text",
|
|
69
|
-
appPath: "Expected a path within the app, starting with a single /",
|
|
70
|
-
pathMove: "Expected a move of push or replace",
|
|
71
|
-
place: "Expected a place such as { post: { id } }, { page: { id } }, { member: { id } } or { apps: true }"
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* The codes a refused call carries across the channel. The structured clone that carries an
|
|
75
|
-
* error keeps only its message, so the refusing side puts the code at the start of the
|
|
76
|
-
* message, as in `blocked: Apps can't use GET /db/`, and the other side parses it back out. Codes
|
|
77
|
-
* are only ever added: an SDK that doesn't know a code treats the whole message as a generic
|
|
78
|
-
* refusal.
|
|
79
|
-
*
|
|
80
|
-
* The host codes, which Admin refuses the page's calls with:
|
|
81
|
-
*
|
|
82
|
-
* - `session_ended`: the session is over; the page is about to be replaced.
|
|
83
|
-
* - `invalid_argument`: an argument Admin does not take.
|
|
84
|
-
* - `invalid_path`: a path that is not an Admin API path or a route within the app, or one
|
|
85
|
-
* the Admin API only responds to with a redirect, such as a path without its trailing `/`.
|
|
86
|
-
* - `blocked`: a request the allowlist doesn't allow, or a write it refuses.
|
|
87
|
-
* - `network_error`: Admin could not reach its own API.
|
|
88
|
-
* - `rejected`: anything else; the detail says why.
|
|
89
|
-
*/
|
|
90
|
-
var HOST_ERROR_CODES = [
|
|
91
|
-
"session_ended",
|
|
92
|
-
"invalid_argument",
|
|
93
|
-
"invalid_path",
|
|
94
|
-
"blocked",
|
|
95
|
-
"network_error",
|
|
96
|
-
"rejected"
|
|
97
|
-
];
|
|
98
|
-
[...HOST_ERROR_CODES];
|
|
99
|
-
var CODED_MESSAGE = /^([a-z_]+): ([\s\S]*)$/;
|
|
100
|
-
/**
|
|
101
|
-
* Builds the message a refusal crosses the channel as: `code: detail`. Each side should pass
|
|
102
|
-
* only its own codes; typing its refusals as `HostErrorCode` or `AppErrorCode` enforces that.
|
|
103
|
-
*/
|
|
104
|
-
function encodeBridgeError(code, detail) {
|
|
105
|
-
return `${code}: ${detail}`;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Reads the code and detail back from a refusal's `code: detail` message. Takes the codes the
|
|
109
|
-
* other side may send: `HOST_ERROR_CODES` for a refusal from Admin, and `APP_ERROR_CODES` for
|
|
110
|
-
* one from the page. Returns `undefined` for a message that doesn't start with one of them.
|
|
111
|
-
*/
|
|
112
|
-
function decodeBridgeError(message, codes) {
|
|
113
|
-
const [, code = "", detail = ""] = CODED_MESSAGE.exec(message) ?? [];
|
|
114
|
-
return codes.includes(code) ? {
|
|
115
|
-
code,
|
|
116
|
-
detail
|
|
117
|
-
} : void 0;
|
|
118
|
-
}
|
|
119
|
-
var THEME_MODES = ["light", "dark"];
|
|
120
|
-
var CONTROL_CHARACTER = /\p{Cc}/u;
|
|
121
|
-
/**
|
|
122
|
-
* Whether a value is a path that stays within the base it is resolved against, rather than
|
|
123
|
-
* escaping it. It must start with a single `/`, so it can't carry a scheme or be read as
|
|
124
|
-
* `//host`. It must not contain `\`, which URL parsers read as `/`, or any control character.
|
|
125
|
-
*/
|
|
126
|
-
function isContainedPath(value) {
|
|
127
|
-
return typeof value === "string" && value.startsWith("/") && !value.startsWith("//") && !value.includes("\\") && !CONTROL_CHARACTER.test(value);
|
|
128
|
-
}
|
|
129
|
-
/** An object, or an array: anything whose fields can be read. */
|
|
130
|
-
function isRecord(value) {
|
|
131
|
-
return typeof value === "object" && value !== null;
|
|
132
|
-
}
|
|
133
|
-
function hasStrings(value, keys) {
|
|
134
|
-
return isRecord(value) && keys.every((key) => typeof value[key] === "string");
|
|
135
|
-
}
|
|
136
|
-
function isTheme(value) {
|
|
137
|
-
return hasStrings(value, ["accent_color"]) && THEME_MODES.includes(value.mode);
|
|
138
|
-
}
|
|
139
|
-
function isStartContext(value) {
|
|
140
|
-
return isRecord(value) && hasStrings(value.site, [
|
|
141
|
-
"url",
|
|
142
|
-
"title",
|
|
143
|
-
"timezone",
|
|
144
|
-
"locale"
|
|
145
|
-
]) && hasStrings(value.user, ["id", "role"]) && isTheme(value.theme) && isRecord(value.session) && Number.isSafeInteger(value.session.revision) && value.session.revision >= 0 && isContainedPath(value.path);
|
|
146
|
-
}
|
|
147
|
-
function isConnectMessage(value) {
|
|
148
|
-
return isRecord(value) && value.type === "ghost-app:connect" && Array.isArray(value.exports) && value.exports.every((name) => typeof name === "string");
|
|
149
|
-
}
|
|
150
|
-
function isFetchResponse(value) {
|
|
151
|
-
return isRecord(value) && Number.isSafeInteger(value.status) && value.status >= 200 && value.status <= 599 && typeof value.statusText === "string" && isRecord(value.headers) && !Array.isArray(value.headers) && Object.values(value.headers).every((header) => typeof header === "string") && typeof value.body === "string";
|
|
152
|
-
}
|
|
153
|
-
//#endregion
|
|
154
3
|
//#region src/errors.ts
|
|
155
4
|
/** The only error type the SDK rejects with, so an app can branch on `code`. */
|
|
156
5
|
var GhostAppError = class extends Error {
|
|
@@ -641,7 +490,7 @@ var Channel = class {
|
|
|
641
490
|
* `__SDK_VERSION__` with the version in package.json, because the source stays within `src/`
|
|
642
491
|
* and doesn't read package.json.
|
|
643
492
|
*/
|
|
644
|
-
var SDK_VERSION = "0.1.0-alpha.
|
|
493
|
+
var SDK_VERSION = "0.1.0-alpha.7";
|
|
645
494
|
//#endregion
|
|
646
495
|
//#region src/connect.ts
|
|
647
496
|
/**
|