@nomac/phone 0.1.0 → 0.1.2

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
@@ -23,4 +23,6 @@ import { NomacPhone } from "@nomac/phone/react";
23
23
  <NomacPhone url={link} onEvent={(e) => e.type === "ended" && reload()} style={{ height: 720 }} />
24
24
  ```
25
25
 
26
+ The box keeps an iPhone's shape (402 by 874); for an iPad pass `style={{ aspectRatio: "834 / 1210" }}` (iPad Pro 11-inch (M5)). `ended` comes once, when a phone that was shown ends; a link that does not work from the start is an `error`. While the phone starts, the view waits for it, and after a dropped connection it comes back by itself. Only view links of https://nomac.app are framed.
27
+
26
28
  The screen comes as H.264 over a WebSocket, decoded in the page (WebCodecs), with no TURN servers. With a control link, one viewer at a time holds input; another can take over.
package/index.d.ts CHANGED
@@ -11,13 +11,13 @@ export type Phone = {
11
11
  home(): void;
12
12
  /** Taps at x and y, each from 0 to 1 across the screen (a control link only). */
13
13
  tap(x: number, y: number): void;
14
- /** The phone's screen as a PNG data URL. */
15
- screenshot(): Promise<string>;
14
+ /** The phone's screen as a PNG data URL; rejected after timeoutMs (10 s by default) when none comes. */
15
+ screenshot(options?: { timeoutMs?: number }): Promise<string>;
16
16
  destroy(): void;
17
17
  };
18
18
 
19
19
  export const ORIGIN: string;
20
- /** The view link with embed=1. Throws for anything that is not a NoMac Simulator view link. */
21
- export function embedUrl(viewUrl: string): string;
20
+ /** The view link with embed=1. Throws for anything that is not a NoMac Simulator view link of origin (https://nomac.app). */
21
+ export function embedUrl(viewUrl: string, options?: { origin?: string }): string;
22
22
  /** Puts the phone in container; onEvent receives its events. */
23
- export function embedPhone(container: HTMLElement, viewUrl: string, options?: { title?: string; onEvent?: (event: PhoneEvent) => void }): Phone;
23
+ export function embedPhone(container: HTMLElement, viewUrl: string, options?: { title?: string; origin?: string; onEvent?: (event: PhoneEvent) => void }): Phone;
package/index.js CHANGED
@@ -7,10 +7,13 @@
7
7
 
8
8
  const ORIGIN = "https://nomac.app";
9
9
 
10
- /** The view link with embed=1: the phone alone, filling its frame, talking to the page. */
11
- export function embedUrl(viewUrl) {
10
+ /**
11
+ * The view link with embed=1: the phone alone, filling its frame, talking to the page. Only NoMac's own view links
12
+ * (https://nomac.app/simulator-use/view/...; origin for another NoMac, such as a test one) are framed.
13
+ */
14
+ export function embedUrl(viewUrl, { origin = ORIGIN } = {}) {
12
15
  const url = new URL(viewUrl);
13
- if (!/^\/simulator-use\/view\/[A-Za-z0-9_.-]+$/.test(url.pathname)) throw new Error("Not a NoMac Simulator view link: " + viewUrl);
16
+ if (url.origin !== origin || !/^\/simulator-use\/view\/[A-Za-z0-9_.-]+$/.test(url.pathname)) throw new Error("Not a NoMac Simulator view link: " + viewUrl);
14
17
  url.searchParams.set("embed", "1");
15
18
  return url.toString();
16
19
  }
@@ -20,7 +23,7 @@ export function embedUrl(viewUrl) {
20
23
  * error and screenshot. Answers controls for it, and destroy() to take it away.
21
24
  */
22
25
  export function embedPhone(container, viewUrl, options = {}) {
23
- const src = embedUrl(viewUrl);
26
+ const src = embedUrl(viewUrl, { origin: options.origin });
24
27
  const origin = new URL(src).origin;
25
28
  const frame = document.createElement("iframe");
26
29
  frame.src = src;
@@ -43,8 +46,17 @@ export function embedPhone(container, viewUrl, options = {}) {
43
46
  home: () => command({ type: "home" }),
44
47
  /** Taps at x and y, each from 0 to 1 across the screen (a control link only). */
45
48
  tap: (x, y) => command({ type: "tap", x, y }),
46
- /** The phone's screen as a PNG data URL. */
47
- screenshot: () => new Promise((resolve) => { screenshots.push(resolve); command({ type: "screenshot" }); }),
49
+ /** The phone's screen as a PNG data URL; rejected after timeoutMs (10 s) when none comes (the phone not shown yet). */
50
+ screenshot: ({ timeoutMs = 10_000 } = {}) => new Promise((resolve, reject) => {
51
+ const answer = (data) => { clearTimeout(timer); resolve(data); };
52
+ const timer = setTimeout(() => {
53
+ const at = screenshots.indexOf(answer);
54
+ if (at >= 0) screenshots.splice(at, 1);
55
+ reject(new Error("No screenshot from the phone in time: is it shown yet (its ready event)?"));
56
+ }, timeoutMs);
57
+ screenshots.push(answer);
58
+ command({ type: "screenshot" });
59
+ }),
48
60
  destroy: () => { window.removeEventListener("message", onMessage); frame.remove(); },
49
61
  };
50
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nomac/phone",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A NoMac Simulator phone in your page: its view link in an iframe, its events, and home, tap and screenshot from your code.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://nomac.app",
package/react.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { CSSProperties, ForwardRefExoticComponent, RefAttributes } from "react";
2
2
  import type { PhoneEvent } from "./index.js";
3
3
 
4
- export type NomacPhoneHandle = { home(): void; tap(x: number, y: number): void; screenshot(): Promise<string> };
4
+ export type NomacPhoneHandle = { home(): void; tap(x: number, y: number): void; screenshot(options?: { timeoutMs?: number }): Promise<string> };
5
5
  export type NomacPhoneProps = { url: string; onEvent?: (event: PhoneEvent) => void; title?: string; className?: string; style?: CSSProperties };
6
6
  export const NomacPhone: ForwardRefExoticComponent<NomacPhoneProps & RefAttributes<NomacPhoneHandle>>;
package/react.js CHANGED
@@ -18,7 +18,7 @@ export const NomacPhone = forwardRef(function NomacPhone({ url, onEvent, title,
18
18
  useImperativeHandle(ref, () => ({
19
19
  home: () => phone.current?.home(),
20
20
  tap: (x, y) => phone.current?.tap(x, y),
21
- screenshot: () => phone.current?.screenshot() ?? Promise.reject(new Error("The phone is not shown")),
21
+ screenshot: (options) => phone.current?.screenshot(options) ?? Promise.reject(new Error("The phone is not shown")),
22
22
  }), []);
23
23
  return createElement("div", { ref: box, className, style: { aspectRatio: "402 / 874", ...style } });
24
24
  });