@arcadible/sdk 0.3.0 → 1.0.0

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.
@@ -2,11 +2,11 @@
2
2
  // alone when no page answers, running the page's end itself, in its own
3
3
  // page, so the rules are the same.
4
4
  // [import]
5
- import { ArcadibleError, BUS_VERSION, Codes, HANDSHAKE_TIMEOUT, HOST_ORIGINS, isMessage, isWelcome, REQUEST_TIMEOUT, } from "./protocol.js";
5
+ import { ArcadibleError, BUS_VERSION, Codes, HANDSHAKE_TIMEOUT, isHostOrigin, isMessage, isWelcome, REQUEST_TIMEOUT, } from "./protocol.js";
6
6
  // [function]
7
7
  export function openBus(options) {
8
8
  const win = options.window ?? (typeof window === "undefined" ? undefined : window);
9
- const origins = options.origins ?? HOST_ORIGINS;
9
+ const origins = options.origins;
10
10
  const requestTimeout = options.requestTimeout ?? REQUEST_TIMEOUT;
11
11
  let state = "waiting";
12
12
  let port = null;
@@ -79,7 +79,7 @@ export function openBus(options) {
79
79
  if (state !== "waiting" ||
80
80
  !win ||
81
81
  event.source !== win.parent ||
82
- !origins.includes(event.origin) ||
82
+ !isHostOrigin(event.origin, origins) ||
83
83
  !isWelcome(event.data) ||
84
84
  !event.ports[0]) {
85
85
  return;
@@ -1,7 +1,7 @@
1
1
  export declare const BUS_VERSION = 1;
2
2
  export declare const HANDSHAKE_TIMEOUT = 1000;
3
3
  export declare const REQUEST_TIMEOUT = 10000;
4
- export declare const HOST_ORIGINS: readonly string[];
4
+ export declare function isHostOrigin(origin: string, origins: readonly string[] | undefined): boolean;
5
5
  export declare const MAX_IN_FLIGHT = 64;
6
6
  export declare const MAX_MESSAGE: number;
7
7
  export declare const Codes: {
@@ -40,7 +40,7 @@ export type Request = {
40
40
  name: string;
41
41
  params?: unknown;
42
42
  };
43
- export type Response = {
43
+ type Response = {
44
44
  kind: "response";
45
45
  id: number;
46
46
  result: unknown;
@@ -49,7 +49,7 @@ export type Response = {
49
49
  id: number;
50
50
  error: ErrorBody;
51
51
  };
52
- export type BusEvent = {
52
+ type BusEvent = {
53
53
  kind: "event";
54
54
  name: string;
55
55
  data?: unknown;
@@ -60,9 +60,9 @@ export declare class ArcadibleError extends Error {
60
60
  constructor(code: Code, message: string);
61
61
  toJSON(): ErrorBody;
62
62
  }
63
- export declare function isObject(value: unknown): value is Record<string, unknown>;
64
63
  export declare function isHello(value: unknown): value is Hello;
65
64
  export declare function isWelcome(value: unknown): value is Welcome;
66
65
  export declare function isMessage(value: unknown): value is Message;
67
66
  export declare function utf8Size(text: string): number;
68
67
  export declare function messageSize(value: unknown, limit?: number): number;
68
+ export {};
@@ -9,11 +9,23 @@ export const BUS_VERSION = 1;
9
9
  export const HANDSHAKE_TIMEOUT = 1000;
10
10
  // How long a request waits for its response.
11
11
  export const REQUEST_TIMEOUT = 10_000;
12
- // The play pages a game accepts a welcome from: production's only. A
13
- // development page (play.arcadible.test, `arc preview`) is passed with
14
- // `createClient({ origins })`, never trusted by what games ship, since a
15
- // hostile network could pose as an http one.
16
- export const HOST_ORIGINS = ["https://play.arcadible.com"];
12
+ // The play pages a game accepts a welcome from: production's, and `arc
13
+ // serve`'s on this machine. Another development page (play.arcadible.test)
14
+ // is passed with `createClient({ origins })`, never trusted by what games
15
+ // ship, since a hostile network could pose as an http one.
16
+ const HOST_ORIGINS = ["https://play.arcadible.com"];
17
+ // `arc serve`'s play page, on any port. The network can't pose as loopback,
18
+ // and on Arcadible a build's `frame-ancestors` lets only the play page frame
19
+ // it, so no local page can reach a player's saves there.
20
+ const LOCAL_ORIGIN = /^http:\/\/localhost:\d+$/u;
21
+ // [function]
22
+ // Whether a game accepts a welcome from `origin`: one of `origins` when it
23
+ // was given, or else production's or `arc serve`'s.
24
+ export function isHostOrigin(origin, origins) {
25
+ return origins
26
+ ? origins.includes(origin)
27
+ : HOST_ORIGINS.includes(origin) || LOCAL_ORIGIN.test(origin);
28
+ }
17
29
  // The most requests one game can have waiting on the page; past it, a
18
30
  // request fails with `busy`.
19
31
  export const MAX_IN_FLIGHT = 64;
@@ -53,7 +65,7 @@ export class ArcadibleError extends Error {
53
65
  }
54
66
  }
55
67
  // [function]
56
- export function isObject(value) {
68
+ function isObject(value) {
57
69
  return typeof value === "object" && value !== null;
58
70
  }
59
71
  // [function]
package/dist/host.d.ts CHANGED
@@ -7,7 +7,7 @@ export type ServeOptions = {
7
7
  backend: Backend;
8
8
  now?: () => Date;
9
9
  };
10
- export type HostOptions = ServeOptions & {
10
+ type HostOptions = ServeOptions & {
11
11
  frame: HTMLIFrameElement;
12
12
  origin: string;
13
13
  window?: Window;
@@ -18,6 +18,4 @@ export declare function createHost(options: HostOptions): {
18
18
  };
19
19
  export type { Server } from "./bus/server.js";
20
20
  export { indexedDBBackend } from "./saves/backend/indexeddb.js";
21
- export type { IndexedDBOptions } from "./saves/backend/indexeddb.js";
22
21
  export { memoryBackend } from "./saves/backend/memory.js";
23
- export type { Backend, Change, NewRecord, SaveRecord, Scope, } from "./saves/backend/types.js";
package/dist/host.js CHANGED
@@ -1,4 +1,4 @@
1
- // `@arcadible/sdk/host`: the page's side. The play page uses `createHost`;
1
+ // The page's side of the bus. The play page uses `createHost`;
2
2
  // a game standing alone runs `serve` in its own page.
3
3
  // [import]
4
4
  import { answerHellos, createServer } from "./bus/server.js";
@@ -28,7 +28,6 @@ export type SavesChange = {
28
28
  save: Save | null;
29
29
  revision: number;
30
30
  };
31
- export declare function isWellFormed(text: string): boolean;
32
31
  export declare function checkName(name: unknown): ArcadibleError | null;
33
32
  export declare function checkWrite(saves: ReadonlyMap<string, {
34
33
  info: SaveMeta;
@@ -11,7 +11,7 @@ const NAME = /^[A-Za-z0-9_.-]{1,64}$/u;
11
11
  // A lone surrogate: half a character, which storage and the network corrupt.
12
12
  const LONE_SURROGATE = /\p{Surrogate}/u;
13
13
  // [function]
14
- export function isWellFormed(text) {
14
+ function isWellFormed(text) {
15
15
  return !LONE_SURROGATE.test(text);
16
16
  }
17
17
  // [function]
@@ -1,11 +1,10 @@
1
1
  import type { Module, ModuleContext } from "../bus/server.js";
2
- import type { Backend, SaveRecord, Scope } from "./backend/types.js";
3
- import { type Save } from "./rules.js";
4
- export type SavesServerOptions = {
2
+ import type { Backend, Scope } from "./backend/types.js";
3
+ type SavesServerOptions = {
5
4
  scope: Scope;
6
5
  version: string | null;
7
6
  backend: Backend;
8
7
  now: () => Date;
9
8
  };
10
- export declare function toSave(record: SaveRecord): Save;
11
9
  export declare function savesServer(options: SavesServerOptions): (context: ModuleContext) => Module;
10
+ export {};
@@ -5,7 +5,7 @@
5
5
  import { ArcadibleError, Codes, utf8Size } from "../bus/protocol.js";
6
6
  import { checkName, checkWrite, } from "./rules.js";
7
7
  // [function]
8
- export function toSave(record) {
8
+ function toSave(record) {
9
9
  const { name, data, version, savedAt, size, revision } = record;
10
10
  return { name, data, info: { version, savedAt, size, revision } };
11
11
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.3.0";
1
+ export declare const VERSION = "1.0.0";
package/dist/version.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // The client's version, sent in its hello and shown as `arcadible.version`,
2
2
  // for bug reports. It matches package.json (test/unit/protocol.test.ts).
3
- export const VERSION = "0.3.0";
3
+ export const VERSION = "1.0.0";
package/package.json CHANGED
@@ -43,5 +43,5 @@
43
43
  ],
44
44
  "type": "module",
45
45
  "types": "./dist/index.d.ts",
46
- "version": "0.3.0"
46
+ "version": "1.0.0"
47
47
  }