@vehicle-sil/gui-client 0.4.0-rc.1 → 0.4.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.
package/README.ja.md CHANGED
@@ -106,7 +106,7 @@ editor.setVisible(true);
106
106
  | 型 | `TokenPlace` |
107
107
  | 定数 | `TOKEN_STORAGE_KEY` |
108
108
 
109
- ブラウザでは `takeToken(window)` と呼びます。URL の `#token=` を読んで URL から消し、sessionStorage に置きます。URL になければ sessionStorage に置いたものを返し、どちらにもなければ `null` を返します。
109
+ ブラウザでは `takeToken(window)` と呼びます。URL の `#token=` を読んで URL から消し、sessionStorage に置きます。URL になければ sessionStorage に置いたものを返し、どちらにもなければ `null` を返します。`--no-auth` で起動したサーバーにつなぐときは、`token` を省略するか `null` にできます(トークン認証が有効なサーバーでは、HTTP は 401 で失敗し WebSocket は `rejected` になります)。
110
110
 
111
111
  ### 接続(読むだけ)
112
112
 
package/README.md CHANGED
@@ -106,7 +106,7 @@ The names that `src/index.ts` exports, by group. Parentheses list the members of
106
106
  | Type | `TokenPlace` |
107
107
  | Constant | `TOKEN_STORAGE_KEY` |
108
108
 
109
- In the browser, call `takeToken(window)`. It reads `#token=` from the URL, removes it from the URL and stores it in sessionStorage. If the URL has no token, it returns the one in sessionStorage; if neither has one, it returns `null`.
109
+ In the browser, call `takeToken(window)`. It reads `#token=` from the URL, removes it from the URL and stores it in sessionStorage. If the URL has no token, it returns the one in sessionStorage; if neither has one, it returns `null`. When connecting to a server started with `--no-auth`, `token` may be omitted or `null` (against a server with token authentication enabled, HTTP requests reject with 401 and WebSocket sets `rejected`).
110
110
 
111
111
  ### Connection (read-only)
112
112
 
package/dist/index.js CHANGED
@@ -1788,7 +1788,8 @@ function errorMessage(error) {
1788
1788
  }
1789
1789
  function fetchTransport(options) {
1790
1790
  const send = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
1791
- const token = typeof options.token === "function" ? options.token : () => options.token;
1791
+ const rawToken = options.token;
1792
+ const token = typeof rawToken === "function" ? rawToken : () => rawToken ?? null;
1792
1793
  return (request, done) => {
1793
1794
  const headers = {};
1794
1795
  const current = token();
@@ -4326,7 +4327,8 @@ function clientErrorSink(client) {
4326
4327
  return clientErrorSinks.get(client);
4327
4328
  }
4328
4329
  function buildClient(options) {
4329
- const token = typeof options.token === "function" ? options.token : () => options.token;
4330
+ const rawToken = options.token;
4331
+ const token = typeof rawToken === "function" ? rawToken : () => rawToken ?? null;
4330
4332
  const onError = options.onError ?? reportToConsole;
4331
4333
  const origin = options.origin ?? defaultOrigin(options.baseUrl);
4332
4334
  const http = options.http ?? fetchTransport({ baseUrl: options.baseUrl, token, origin });
@@ -4444,7 +4446,7 @@ function buildClient(options) {
4444
4446
  app: app.readonly(),
4445
4447
  limits: limits.readonly(),
4446
4448
  connect: () => {
4447
- if (token() && !session.stopped) connection.connect();
4449
+ if (!session.stopped) connection.connect();
4448
4450
  },
4449
4451
  close: () => {
4450
4452
  connection.close();
@@ -4583,8 +4585,9 @@ function runOf(s) {
4583
4585
  const outcome = runOutcome(session);
4584
4586
  if (outcome === "crashed") return starting ? "starting" : "crashed";
4585
4587
  if (outcome === "ended" || outcome === "finishing") return starting ? "starting" : "ended";
4586
- if (outcome === "stopping" || outcome === "forcing" || s.stopConfirmed) return "stopping";
4587
4588
  const state = session.launcherState;
4589
+ if (outcome === "stopping" || outcome === "forcing" || s.stopConfirmed || (state?.stop?.requests ?? 0) > 0)
4590
+ return "stopping";
4588
4591
  const phase = state?.phase || session.phase;
4589
4592
  const paused = state?.paused ?? null;
4590
4593
  switch (phase) {
@@ -4717,7 +4720,11 @@ function composeControls(s, run) {
4717
4720
  const session = s.session;
4718
4721
  const live = isRunActive(session) && run !== "ended" && run !== "crashed";
4719
4722
  const state = session?.launcherState ?? null;
4720
- const stopsAsked = Math.max(session?.stopRequests ?? 0, s.stopConfirmed ? 1 : 0);
4723
+ const stopsAsked = Math.max(
4724
+ session?.stopRequests ?? 0,
4725
+ state?.stop?.requests ?? 0,
4726
+ s.stopConfirmed ? 1 : 0
4727
+ );
4721
4728
  let start;
4722
4729
  if (link === "rejected" || link === "incompatible") start = control(false, down);
4723
4730
  else if (s.busy.has("start")) start = control(false, busyReason("start"));
@@ -4734,7 +4741,8 @@ function composeControls(s, run) {
4734
4741
  reason("already-running", "a run is going: stop it before you start another")
4735
4742
  ) : control(true);
4736
4743
  const serverStop = s.allowed?.stop;
4737
- const stoppable = s.busy.has("start") || live || run === "starting" || serverStop?.ok === true;
4744
+ const serverForce = s.allowed?.force;
4745
+ const stoppable = s.busy.has("start") || live || run === "starting" || serverStop?.ok === true || serverForce?.ok === true;
4738
4746
  let stop;
4739
4747
  if (!s.busy.has("start") && s.allowed?.start.reason === "starting")
4740
4748
  stop = control(false, reason("starting", "wait until the new run can be identified"));
@@ -4750,7 +4758,6 @@ function composeControls(s, run) {
4750
4758
  reason("already-stopping", "a stop has been asked for: wait, or force the stop")
4751
4759
  );
4752
4760
  else stop = control(true);
4753
- const serverForce = s.allowed?.force;
4754
4761
  let force;
4755
4762
  if (s.busy.has("start") || s.allowed?.start.reason === "starting")
4756
4763
  force = control(false, reason("starting", "wait until the new run can be identified"));
@@ -5906,15 +5913,14 @@ function createFront(options) {
5906
5913
  }
5907
5914
  function pageOptions() {
5908
5915
  const page = globalThis.window;
5909
- const token = page ? takeToken(page) : null;
5910
- if (!page || token === null) {
5916
+ if (!page) {
5911
5917
  throw new ActionError(
5912
5918
  "no-token",
5913
- "connect() without options works in a page that the GUI server served and that was opened from the URL it printed (it ends with #token=...). In Node, pass { base_url, token }",
5919
+ "connect() without options works in a page that the GUI server served. In Node, pass { base_url } (and token unless the server runs with --no-auth)",
5914
5920
  "refused"
5915
5921
  );
5916
5922
  }
5917
- return { base_url: new URL("/", page.location.href).href, token };
5923
+ return { base_url: new URL("/", page.location.href).href, token: takeToken(page) };
5918
5924
  }
5919
5925
  function connect(options) {
5920
5926
  if (options) return open(options, () => void 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vehicle-sil/gui-client",
3
- "version": "0.4.0-rc.1",
3
+ "version": "0.4.0",
4
4
  "description": "Client library of the vehicle-sil GUI server: connection, run and time control, ECU variables. No dependencies; browser and Node.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/types/client.d.ts CHANGED
@@ -22,11 +22,11 @@ export interface ClientOptions {
22
22
  */
23
23
  baseUrl: string;
24
24
  /**
25
- * The token (what follows `#token=` in the URL the server printed). A function is called each time the token is
26
- * used. While it is null, the client does not connect. Switching to another token after a refusal does not
27
- * reconnect (create a new client instead).
25
+ * The token (what follows `#token=` in the URL the server printed), if required. A function is called each time the
26
+ * token is used. Omit or return null when connecting to a server started with `--no-auth`. Switching to another
27
+ * token after a refusal does not reconnect (create a new client instead).
28
28
  */
29
- token: string | (() => string | null);
29
+ token?: string | null | (() => string | null);
30
30
  /**
31
31
  * Origin to send with POST and the WebSocket handshake. Not needed in browsers, which add it themselves. In Node,
32
32
  * the origin of baseUrl is sent if omitted (the server accepts only its own origin). Do not pass it in a Web Worker
@@ -74,7 +74,7 @@ export interface GuiClient {
74
74
  readonly app: Readable<AppInfo | null>;
75
75
  /** Speed range and the limits of stepping and checks (the limits of app; DEFAULT_LIMITS until then). */
76
76
  readonly limits: Readable<AppInfo['limits']>;
77
- /** Connects through WebSocket (does nothing without a token, after a refusal, or after a version mismatch). */
77
+ /** Connects through WebSocket (does nothing after a refusal or after a version mismatch). */
78
78
  connect(): void;
79
79
  /** Disconnects and stops pending timers. */
80
80
  close(): void;
package/types/front.d.ts CHANGED
@@ -7,8 +7,8 @@ import { ErrorSink, Readable } from './values.js';
7
7
  export interface ConnectOptions {
8
8
  /** URL of the GUI server (what the server printed, without `#token=...`). */
9
9
  base_url: string;
10
- /** The token (what follows `#token=`). A function is called each time the token is used. */
11
- token: string | (() => string | null);
10
+ /** The token (what follows `#token=`), if required. A function is called each time the token is used. */
11
+ token?: string | null | (() => string | null);
12
12
  /** Origin to send (Node sends the origin of `base_url` if omitted). */
13
13
  origin?: string;
14
14
  /** HTTP transport (default: fetch). For tests. */
package/types/http.d.ts CHANGED
@@ -52,8 +52,8 @@ export declare function errorMessage(error: unknown): string;
52
52
  export interface FetchTransportOptions {
53
53
  /** URL of the server (`http://127.0.0.1:<port>/`). Request paths are relative to it. */
54
54
  baseUrl: string;
55
- /** The token (what follows `#token=` in the URL the server printed). A function is called for each request. */
56
- token: string | (() => string | null);
55
+ /** The token (what follows `#token=` in the URL the server printed), if required. A function is called for each request. */
56
+ token?: string | null | (() => string | null);
57
57
  /**
58
58
  * Origin to send with POST. Browsers add it themselves (a value given here is not used). Outside the browser (Node
59
59
  * and so on), pass the server's own origin (the origin of baseUrl).