castle-web-sdk 0.4.56 → 0.4.57

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 CHANGED
@@ -233,7 +233,8 @@ dev serve.
233
233
  - `session.hub.place`, `.title` and `.result` are the place and the result shape
234
234
  as published (`result` is `null` for a place that reports none, such as a town).
235
235
  - `session.hub.reconnectWindowMs`, when present, is Castle's pinned hub-place
236
- reconnect grace in milliseconds, inclusive at the deadline. It is absent on
236
+ reconnect grace in milliseconds, inclusive at the deadline (at most 180000, or
237
+ 300000 on an owner house that opted into `houseResume`). It is absent on
237
238
  sessions without a pinned grace and older hosts. Kits use it to match visit retention to the
238
239
  transport window; deck code does not choose it.
239
240
  - `session.hub.arrival(playerId)` is how a player came in when Castle placed them
package/dist/runtime.js CHANGED
@@ -741,6 +741,10 @@ function connectBridge() {
741
741
  return;
742
742
  }
743
743
  const port = event.ports[0];
744
+ // A shell that answers reload requests: tell it this SDK will ask before
745
+ // reloading, so it lets restarts through to us (we flush saves first).
746
+ const reloadRequests = data.reloadRequests === true;
747
+ bridgeReloadPort = reloadRequests ? port : null;
744
748
  const socket = {
745
749
  readyState: WebSocket.OPEN,
746
750
  send: (value) => {
@@ -756,9 +760,15 @@ function connectBridge() {
756
760
  const value = messageEvent.data;
757
761
  if (!value || typeof value !== "object")
758
762
  return;
763
+ if (value.type === "castle-reload-response") {
764
+ settleReloadRequest(value);
765
+ return;
766
+ }
759
767
  handleLocalMessage(value);
760
768
  };
761
769
  port.start();
770
+ if (reloadRequests)
771
+ port.postMessage({ type: "castle-sdk-hello", reloadRequests: true });
762
772
  localServeSeen = true;
763
773
  ws = socket;
764
774
  for (const msg of logBuffer)
@@ -1108,6 +1118,48 @@ function stashReloadState() {
1108
1118
  export function requestReload() {
1109
1119
  scheduleReload(REQUEST_RELOAD_DEBOUNCE_MS, "requestReload");
1110
1120
  }
1121
+ // Set when the shell's bridge answers reload requests (castle-bridge-init's
1122
+ // `reloadRequests`). Before reloading, this frame asks the shell: a frame whose
1123
+ // content-origin grant has expired would only fail to reload in place, so the
1124
+ // shell moves it to fresh origins instead and this page does nothing more.
1125
+ let bridgeReloadPort = null;
1126
+ const RELOAD_REQUEST_TIMEOUT_MS = 4000;
1127
+ const pendingReloadRequests = new Map();
1128
+ let nextReloadRequest = 1;
1129
+ function settleReloadRequest(value) {
1130
+ if (typeof value.requestId !== "string")
1131
+ return;
1132
+ const settle = pendingReloadRequests.get(value.requestId);
1133
+ if (!settle)
1134
+ return;
1135
+ pendingReloadRequests.delete(value.requestId);
1136
+ settle(value.relocated === true);
1137
+ }
1138
+ /** Whether the shell moved this frame (so it must not reload itself). */
1139
+ function askShellToRelocate() {
1140
+ const port = bridgeReloadPort;
1141
+ if (!port)
1142
+ return Promise.resolve(false);
1143
+ const requestId = `reload-${nextReloadRequest++}`;
1144
+ return new Promise((resolve) => {
1145
+ const timer = setTimeout(() => {
1146
+ pendingReloadRequests.delete(requestId);
1147
+ resolve(false);
1148
+ }, RELOAD_REQUEST_TIMEOUT_MS);
1149
+ pendingReloadRequests.set(requestId, (relocated) => {
1150
+ clearTimeout(timer);
1151
+ resolve(relocated);
1152
+ });
1153
+ try {
1154
+ port.postMessage({ type: "castle-reload-request", requestId, href: location.href });
1155
+ }
1156
+ catch {
1157
+ clearTimeout(timer);
1158
+ pendingReloadRequests.delete(requestId);
1159
+ resolve(false);
1160
+ }
1161
+ });
1162
+ }
1111
1163
  function scheduleReload(delayMs, reason = "unknown") {
1112
1164
  console.log(`[reload] ${panelTag()} scheduling reload (${reason})`);
1113
1165
  if (restartTimer !== null)
@@ -1121,6 +1173,11 @@ function scheduleReload(delayMs, reason = "unknown") {
1121
1173
  // a failed flush shouldn't block the reload
1122
1174
  }
1123
1175
  stashReloadState();
1176
+ // Saves are flushed above either way. A frame the shell moves (its
1177
+ // grant expired) loads afresh elsewhere; otherwise reload in place,
1178
+ // keeping this origin and the save queue stored on it.
1179
+ if (await askShellToRelocate())
1180
+ return;
1124
1181
  location.reload();
1125
1182
  })();
1126
1183
  }, delayMs);
@@ -46,9 +46,13 @@ function unavailable(op, what) {
46
46
  operation: `session.hub.${op}`,
47
47
  });
48
48
  }
49
+ // An opted-in owner house may be pinned up to 300 s (a three-minute app switch keeps
50
+ // it); every other hub place keeps the host's 180 s cap.
49
51
  function reconnectMetadata(pinned) {
50
52
  const value = pinned.reconnectWindowMs;
51
- return pinned.result === null && typeof value === "number" && Number.isInteger(value) && value > 0 && value <= 180_000
53
+ const cap = pinned.houseResume === true && typeof pinned.owner === "string" && pinned.owner.length > 0
54
+ ? 300_000 : 180_000;
55
+ return pinned.result === null && typeof value === "number" && Number.isInteger(value) && value > 0 && value <= cap
52
56
  ? { reconnectWindowMs: value } : {};
53
57
  }
54
58
  export function createServerHub(platformHandle, arrivals, hiddenPair = () => false) {
@@ -126,6 +126,8 @@ export interface PlatformHubPlace {
126
126
  version: 1;
127
127
  };
128
128
  owner?: string;
129
+ /** Ghost's trusted owner-house opt-in; such a house may pin up to 300 s of grace. */
130
+ houseResume?: true;
129
131
  reconnectWindowMs?: number;
130
132
  place?: string;
131
133
  title?: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-sdk",
3
- "version": "0.4.56",
3
+ "version": "0.4.57",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/castle-xyz/castle-experimental-web.git"