@uzuhq/code-sdk 0.8.11 → 0.8.12

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.
Files changed (2) hide show
  1. package/dist/index.js +29 -16
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1293,6 +1293,35 @@ const onInsetsChange = (cb) => {
1293
1293
  };
1294
1294
  };
1295
1295
  //#endregion
1296
+ //#region src/bridge-envelope.ts
1297
+ const isBridgeChannel = (value) => value === "sdk" || value === "game";
1298
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
1299
+ /**
1300
+ * 親 frame から postMessage で届く envelope を BridgeMessage に復元する。
1301
+ * string でも object でも来るので、形を確かめてから通す。
1302
+ *
1303
+ * payload を欠いた envelope は `{}` で埋める。受け手の handleMessage は
1304
+ * `payload.players` のように payload を直に読むので、ここで落とすと
1305
+ * 例外やハンドラへの `undefined` 渡しになる。
1306
+ */
1307
+ const parseBridgeEnvelope = (raw) => {
1308
+ let value = raw;
1309
+ if (typeof value === "string") try {
1310
+ value = JSON.parse(value);
1311
+ } catch {
1312
+ return null;
1313
+ }
1314
+ if (!isRecord(value)) return null;
1315
+ const { channel, type, payload, playerId } = value;
1316
+ if (!isBridgeChannel(channel) || typeof type !== "string") return null;
1317
+ return {
1318
+ channel,
1319
+ type,
1320
+ payload: isRecord(payload) ? payload : {},
1321
+ ...typeof playerId === "string" ? { playerId } : {}
1322
+ };
1323
+ };
1324
+ //#endregion
1296
1325
  //#region src/scenario-callback.ts
1297
1326
  /**
1298
1327
  * @docs
@@ -2196,22 +2225,6 @@ const sendRaw = (channel, type, payload) => {
2196
2225
  if (window.FlutterHost) window.FlutterHost.postMessage(JSON.stringify(msg));
2197
2226
  else if (window.parent !== window) window.parent.postMessage(JSON.stringify(msg), "*");
2198
2227
  };
2199
- /** 親 frame から届く envelope。string でも object でも来るので、形を確かめてから通す。 */
2200
- const parseBridgeEnvelope = (raw) => {
2201
- let value = raw;
2202
- if (typeof value === "string") try {
2203
- value = JSON.parse(value);
2204
- } catch {
2205
- return null;
2206
- }
2207
- if (typeof value !== "object" || value === null) return null;
2208
- const envelope = value;
2209
- if (typeof envelope.channel !== "string" || typeof envelope.type !== "string") return null;
2210
- return {
2211
- channel: envelope.channel,
2212
- type: envelope.type
2213
- };
2214
- };
2215
2228
  const handleMessage = (msg) => {
2216
2229
  const { channel, type, payload } = msg;
2217
2230
  if (channel === "sdk") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uzuhq/code-sdk",
3
- "version": "0.8.11",
3
+ "version": "0.8.12",
4
4
  "description": "UZU PlayScreen SDK - Flutter ↔ JS ゲーム通信ライブラリ",
5
5
  "type": "module",
6
6
  "exports": {