@tryghost/app-sdk 0.1.0-alpha.2 → 0.1.0-alpha.21

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/dist/app-sdk.js CHANGED
@@ -1,5 +1,57 @@
1
+ //#region ../../contracts/app-contracts/build/identity/codes.js
2
+ /**
3
+ * Ghost couldn't confirm the app is the one it says it is. Kept apart from an app that's down
4
+ * or broken because the publisher should hear something different: contact the developer, and
5
+ * don't install it again from a link, the one way a different developer's key gets pinned.
6
+ */
7
+ var UNCONFIRMED_APP_CODES = [
8
+ "APP_MANIFEST_URL_MISMATCH",
9
+ "APP_MANIFEST_SIGNATURE_INVALID",
10
+ "APP_IDENTITY_MISMATCH",
11
+ "APP_BACKEND_KEY_RETIRED",
12
+ "APP_CHALLENGE_FAILED"
13
+ ];
14
+ new Set(UNCONFIRMED_APP_CODES);
15
+ //#endregion
16
+ //#region ../../contracts/app-contracts/build/failure-codes.js
17
+ /** Trying again may work. */
18
+ var UNREACHABLE_APP_CODES = ["APP_MANIFEST_UNREACHABLE", "APP_CHALLENGE_UNREACHABLE"];
19
+ /** Only the app's developer can fix these. */
20
+ var UNUSABLE_MANIFEST_CODES = [
21
+ "APP_MANIFEST_URL_INVALID",
22
+ "APP_MANIFEST_REDIRECTED",
23
+ "APP_MANIFEST_TOO_LARGE",
24
+ "APP_MANIFEST_NOT_JSON",
25
+ "APP_MANIFEST_UNSIGNED",
26
+ "APP_MANIFEST_INVALID",
27
+ "APP_MANIFEST_OTHER_APP"
28
+ ];
29
+ /**
30
+ * The publisher confirmed a review that's out of date. A changed manifest or installation
31
+ * carries the current review in the error's details when Ghost has one.
32
+ */
33
+ var OUTDATED_REVIEW_CODES = [
34
+ "APP_MANIFEST_CHANGED",
35
+ "APP_INSTALLATION_CHANGED",
36
+ "APP_ALREADY_INSTALLED",
37
+ "APP_INSTALLATION_UNINSTALLED"
38
+ ];
39
+ /**
40
+ * Admin can only explain a code listed here; any other shows as an unknown error. A Ghost
41
+ * core test checks the list against the codes the app installations service throws.
42
+ */
43
+ var APP_FAILURE_CODES = [
44
+ ...UNREACHABLE_APP_CODES,
45
+ ...UNCONFIRMED_APP_CODES,
46
+ ...UNUSABLE_MANIFEST_CODES,
47
+ ...OUTDATED_REVIEW_CODES,
48
+ "APP_NOT_INSTALLED",
49
+ "APP_UNAVAILABLE"
50
+ ];
51
+ new Set(APP_FAILURE_CODES);
52
+ new Set(OUTDATED_REVIEW_CODES);
1
53
  //#endregion
2
- //#region ../app-contracts/build/plain-text.js
54
+ //#region ../../contracts/app-contracts/build/plain-text.js
3
55
  var ALLOWED = "\\p{L}\\p{M}\\p{N}\\p{P}\\p{S}\\p{Zs}\\u200C\\u200D";
4
56
  new RegExp(`^[${ALLOWED}]*$`, "u");
5
57
  var NOT_PLAIN_TEXT = new RegExp(`[^${ALLOWED}]`, "gu");
@@ -8,7 +60,7 @@ function toPlainText(value) {
8
60
  return value.replace(NOT_PLAIN_TEXT, "");
9
61
  }
10
62
  //#endregion
11
- //#region ../app-contracts/build/bridge/rules.js
63
+ //#region ../../contracts/app-contracts/build/bridge/rules.js
12
64
  /**
13
65
  * The bridge's rules that have no dependencies. The root entry point exports them so the SDK
14
66
  * can use them without zod. The schemas in this folder build on them, so the SDK and Admin
@@ -126,22 +178,6 @@ var CONTROL_CHARACTER = /\p{Cc}/u;
126
178
  function isContainedPath(value) {
127
179
  return typeof value === "string" && value.startsWith("/") && !value.startsWith("//") && !value.includes("\\") && !CONTROL_CHARACTER.test(value);
128
180
  }
129
- var APP_PATH_BASE = "https://app.invalid";
130
- /**
131
- * Returns an app path in the URL parser's normalised form, the one form Admin and the SDK
132
- * store it in. `.` and `..` are resolved within the app, so a path can never climb out of it.
133
- * Characters that need escaping are escaped once, so a path the app reported compares equal to
134
- * the same path read back from Admin's URL. `/search?q=a b` becomes `/search?q=a%20b`, and
135
- * `/x/../episodes/1` becomes `/episodes/1`.
136
- *
137
- * Expects a path that `isContainedPath` accepts; anything else may not be a path within the
138
- * app at all. `AppPathSchema` already returns this form, so Admin never calls this directly.
139
- * The SDK, which doesn't load zod, does.
140
- */
141
- function normaliseAppPath(path) {
142
- const url = new URL(path, APP_PATH_BASE);
143
- return `${url.pathname}${url.search}${url.hash}`;
144
- }
145
181
  /** An object, or an array: anything whose fields can be read. */
146
182
  function isRecord(value) {
147
183
  return typeof value === "object" && value !== null;
@@ -854,18 +890,43 @@ function toResponse(answer) {
854
890
  //#endregion
855
891
  //#region src/history.ts
856
892
  /**
857
- * The page's route: the path, query and fragment of its URL, in the normalised form Admin
858
- * stores app paths in.
893
+ * A URL's route: its path, query and fragment. The URL parser has already put them in the
894
+ * normalised form Admin stores app paths in. Resolving them again would read a path that starts
895
+ * with `//` as a host, so a page at `//evil.example/x` would report `/x`; kept as it is, that
896
+ * route fails `isContainedPath` and is never reported.
859
897
  */
898
+ function routeOf({ pathname, search, hash }) {
899
+ return `${pathname}${search}${hash}`;
900
+ }
901
+ /** The page's route. */
860
902
  function currentRoute() {
861
- const { pathname, search, hash } = window.location;
862
- return normaliseAppPath(`${pathname}${search}${hash}`);
903
+ return routeOf(window.location);
863
904
  }
864
905
  /** Whether a node is an HTML or SVG link, which the browser follows when it's clicked. */
865
906
  function isLink(node) {
866
907
  return (node instanceof HTMLAnchorElement || node instanceof SVGElement && node.localName === "a") && node.hasAttribute("href");
867
908
  }
868
909
  /**
910
+ * Whether a route is one Admin would take. Admin owns the app's address, so the page never
911
+ * moves to a route Admin refuses: it would show a place that a reload, Back or a copied Admin
912
+ * link doesn't lead to. `isContainedPath` refuses the same route every time, so the move is
913
+ * dropped, with a warning for the app's developer.
914
+ */
915
+ function isContained(route) {
916
+ if (isContainedPath(route)) return true;
917
+ console.warn(`Ghost Admin can't open the app at ${route}, so the page stays where it is.`);
918
+ return false;
919
+ }
920
+ /**
921
+ * Whether a `pushState` or `replaceState` URL leads to a route Admin refuses. One the URL
922
+ * parser can't read is left to the browser, which throws as it would without the SDK.
923
+ */
924
+ function refuses(url) {
925
+ if (url === void 0 || url === null) return false;
926
+ const next = URL.parse(url, window.location.href);
927
+ return next !== null && next.origin === window.location.origin && !isContained(routeOf(next));
928
+ }
929
+ /**
869
930
  * Makes Admin the owner of the page's history. A frame's history entries are part of the tab's,
870
931
  * and browsers keep them in ways Admin can't follow: Chromium and WebKit keep a removed frame's
871
932
  * entries, which left back steps that did nothing once the app's page opened again, and WebKit
@@ -878,6 +939,7 @@ function isLink(node) {
878
939
  * - A click on a link to the app's own origin, which a router hasn't already handled, is reported
879
940
  * as a push, and the page then loads the link's address in place of its own entry. A link that
880
941
  * opens elsewhere (a `target`, `download`, or a modifier key) is left alone.
942
+ * - Neither is made to a route Admin would refuse, one that isn't contained.
881
943
  * - When Admin moves the page, `follow` replaces the frame's entry and dispatches `popstate`.
882
944
  *
883
945
  * Not intercepted: a form that posts adds one entry of the frame's own, and so do a change to the
@@ -888,8 +950,10 @@ function watchHistory(moved, load = (url) => window.location.replace(url)) {
888
950
  const { history } = window;
889
951
  const { pushState, replaceState } = history;
890
952
  let route = currentRoute();
953
+ let moves = 0;
891
954
  const states = /* @__PURE__ */ new Map([[route, history.state]]);
892
955
  const report = (move) => {
956
+ moves += 1;
893
957
  const next = currentRoute();
894
958
  states.set(next, history.state);
895
959
  if (next !== route) {
@@ -898,10 +962,12 @@ function watchHistory(moved, load = (url) => window.location.replace(url)) {
898
962
  }
899
963
  };
900
964
  const push = (data, unused, url) => {
965
+ if (refuses(url)) return;
901
966
  replaceState.call(history, data, unused, url);
902
967
  report("push");
903
968
  };
904
969
  const replace = (data, unused, url) => {
970
+ if (refuses(url)) return;
905
971
  replaceState.call(history, data, unused, url);
906
972
  report("replace");
907
973
  };
@@ -915,8 +981,13 @@ function watchHistory(moved, load = (url) => window.location.replace(url)) {
915
981
  const url = new URL(link.getAttribute("href"), document.baseURI);
916
982
  if (url.origin !== window.location.origin) return;
917
983
  event.preventDefault();
918
- const go = () => load(url.href);
919
- const next = normaliseAppPath(`${url.pathname}${url.search}${url.hash}`);
984
+ const next = routeOf(url);
985
+ if (!isContained(next)) return;
986
+ moves += 1;
987
+ const thisMove = moves;
988
+ const go = () => {
989
+ if (thisMove === moves) load(url.href);
990
+ };
920
991
  if (next === route) {
921
992
  go();
922
993
  return;
@@ -927,12 +998,14 @@ function watchHistory(moved, load = (url) => window.location.replace(url)) {
927
998
  document.addEventListener("click", onClick);
928
999
  return {
929
1000
  follow(path) {
1001
+ moves += 1;
930
1002
  route = path;
931
1003
  const state = states.get(path) ?? null;
932
1004
  replaceState.call(history, state, "", path);
933
1005
  window.dispatchEvent(new PopStateEvent("popstate", { state }));
934
1006
  },
935
1007
  stop() {
1008
+ moves += 1;
936
1009
  if (history.pushState === push) history.pushState = pushState;
937
1010
  if (history.replaceState === replace) history.replaceState = replaceState;
938
1011
  document.removeEventListener("click", onClick);
@@ -1070,8 +1143,9 @@ var Channel = class {
1070
1143
  };
1071
1144
  this.#theme = Object.freeze({ ...context.theme });
1072
1145
  this.#history = watchHistory((route, move) => this.#moved(route, move));
1073
- this.#path = currentRoute();
1074
- if (this.#path !== context.path) this.#moved(this.#path, "replace");
1146
+ const route = currentRoute();
1147
+ this.#path = context.path;
1148
+ if (route !== context.path) this.#moved(route, "replace");
1075
1149
  if (this.#hostExports.has("titleChanged")) this.#stopWatchingTitle = watchTitle((title) => {
1076
1150
  this.#call("titleChanged", title).catch(() => {});
1077
1151
  });
@@ -1086,6 +1160,7 @@ var Channel = class {
1086
1160
  #pathChanged(path) {
1087
1161
  this.#assertStarted();
1088
1162
  if (!isContainedPath(path)) throw refusal("invalid_argument", "Ghost sent a path this SDK doesn't understand");
1163
+ this.#moves += 1;
1089
1164
  this.#path = path;
1090
1165
  this.#history?.follow(path);
1091
1166
  notify(this.#pathListeners, path);
@@ -1095,6 +1170,7 @@ var Channel = class {
1095
1170
  * for the app, or has refused; a refused move leaves the page where it is.
1096
1171
  */
1097
1172
  async #moved(route, move) {
1173
+ if (!isContainedPath(route)) return;
1098
1174
  this.#path = route;
1099
1175
  this.#moves += 1;
1100
1176
  const thisMove = this.#moves;
@@ -1203,7 +1279,7 @@ var Channel = class {
1203
1279
  * `__SDK_VERSION__` with the version in package.json, because the source stays within `src/`
1204
1280
  * and doesn't read package.json.
1205
1281
  */
1206
- var SDK_VERSION = "0.1.0-alpha.2";
1282
+ var SDK_VERSION = "0.1.0-alpha.21";
1207
1283
  //#endregion
1208
1284
  //#region src/connect.ts
1209
1285
  /**
@@ -1313,7 +1389,7 @@ function forget(attempt) {
1313
1389
  * app. Safe to call as soon as the page loads.
1314
1390
  *
1315
1391
  * Every call shares one connection, and the first call's options apply. Only one copy of the
1316
- * SDK in a page can connect; any other copy rejects with `duplicate_sdk`. Admin answers each
1392
+ * SDK in a page can connect. Any other copy rejects with `duplicate_sdk`. Admin answers each
1317
1393
  * document only once, so after it answers, that connection is the page's only one. Once it
1318
1394
  * fails or its session ends, every call rejects with the error it ended with, and only a new
1319
1395
  * document (after a reload, for example) can connect again. A failure before Admin answered,