@nomac/phone 0.0.0-stage → 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 NoMac
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,28 @@
1
- # Temporary Holding Version
1
+ # @nomac/phone
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A [NoMac Simulator](https://nomac.app) phone in your page, for a platform that gives its users a live iPhone Simulator.
4
+
5
+ Get a link for the phone from NoMac's API: every phone's answer carries `links.view` (watch) and `links.control` (use), or `POST /api/v1/simulator/sessions/{id}/view` with `{"control": true, "minutes": 120}` makes one for your user. The link is the permission: hand it to your own user, no NoMac sign-in.
6
+
7
+ ```js
8
+ import { embedPhone } from "@nomac/phone";
9
+
10
+ const phone = embedPhone(document.getElementById("phone"), link, {
11
+ onEvent: (event) => console.log(event.type), // ready, app_launched, ended, error
12
+ });
13
+ phone.home();
14
+ phone.tap(0.5, 0.5);
15
+ const png = await phone.screenshot();
16
+ ```
17
+
18
+ React:
19
+
20
+ ```jsx
21
+ import { NomacPhone } from "@nomac/phone/react";
22
+
23
+ <NomacPhone url={link} onEvent={(e) => e.type === "ended" && reload()} style={{ height: 720 }} />
24
+ ```
25
+
26
+ The box keeps an iPhone's shape (402 by 874); for an iPad pass `style={{ aspectRatio: "834 / 1194" }}`. `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
+
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 ADDED
@@ -0,0 +1,23 @@
1
+ export type PhoneEvent =
2
+ | { type: "ready" }
3
+ | { type: "app_launched" }
4
+ | { type: "ended" }
5
+ | { type: "error"; error: string }
6
+ | { type: "screenshot"; data: string };
7
+
8
+ export type Phone = {
9
+ frame: HTMLIFrameElement;
10
+ /** Presses the Home button (a control link only). */
11
+ home(): void;
12
+ /** Taps at x and y, each from 0 to 1 across the screen (a control link only). */
13
+ tap(x: number, y: number): void;
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
+ destroy(): void;
17
+ };
18
+
19
+ export const ORIGIN: 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
+ /** Puts the phone in container; onEvent receives its events. */
23
+ export function embedPhone(container: HTMLElement, viewUrl: string, options?: { title?: string; origin?: string; onEvent?: (event: PhoneEvent) => void }): Phone;
package/index.js ADDED
@@ -0,0 +1,64 @@
1
+ /**
2
+ * A NoMac Simulator phone in a page: the phone's view link (from NoMac's API: the phone's links, or POST
3
+ * /api/v1/simulator/sessions/{id}/view) shown in an iframe, which says ready, app_launched, ended and error, and
4
+ * takes home, tap and screenshot. A control link lets the person (and these calls) use the phone; a view link
5
+ * only watches.
6
+ */
7
+
8
+ const ORIGIN = "https://nomac.app";
9
+
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 } = {}) {
15
+ const url = new URL(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);
17
+ url.searchParams.set("embed", "1");
18
+ return url.toString();
19
+ }
20
+
21
+ /**
22
+ * Puts the phone in container. options.onEvent receives { type, ...detail } for ready, app_launched, ended,
23
+ * error and screenshot. Answers controls for it, and destroy() to take it away.
24
+ */
25
+ export function embedPhone(container, viewUrl, options = {}) {
26
+ const src = embedUrl(viewUrl, { origin: options.origin });
27
+ const origin = new URL(src).origin;
28
+ const frame = document.createElement("iframe");
29
+ frame.src = src;
30
+ frame.title = options.title ?? "Phone";
31
+ frame.allow = "clipboard-write";
32
+ frame.style.cssText = "border:0;width:100%;height:100%;display:block;background:#000";
33
+ container.appendChild(frame);
34
+ const screenshots = [];
35
+ const onMessage = (event) => {
36
+ if (event.source !== frame.contentWindow || event.origin !== origin || event.data?.source !== "nomac-phone") return;
37
+ const { source: _source, ...message } = event.data;
38
+ if (message.type === "screenshot") screenshots.shift()?.(message.data);
39
+ options.onEvent?.(message);
40
+ };
41
+ window.addEventListener("message", onMessage);
42
+ const command = (message) => frame.contentWindow?.postMessage({ target: "nomac-phone", ...message }, origin);
43
+ return {
44
+ frame,
45
+ /** Presses the Home button (a control link only). */
46
+ home: () => command({ type: "home" }),
47
+ /** Taps at x and y, each from 0 to 1 across the screen (a control link only). */
48
+ tap: (x, y) => command({ type: "tap", x, y }),
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
+ }),
60
+ destroy: () => { window.removeEventListener("message", onMessage); frame.remove(); },
61
+ };
62
+ }
63
+
64
+ export { ORIGIN };
package/package.json CHANGED
@@ -1,6 +1,16 @@
1
1
  {
2
2
  "name": "@nomac/phone",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
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
+ "license": "MIT",
6
+ "homepage": "https://nomac.app",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": { "types": "./index.d.ts", "default": "./index.js" },
10
+ "./react": { "types": "./react.d.ts", "default": "./react.js" }
11
+ },
12
+ "files": ["index.js", "index.d.ts", "react.js", "react.d.ts", "README.md", "LICENSE"],
13
+ "peerDependencies": { "react": ">=18" },
14
+ "peerDependenciesMeta": { "react": { "optional": true } },
15
+ "scripts": { "test": "node --test test/*.test.js" }
16
+ }
package/react.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { CSSProperties, ForwardRefExoticComponent, RefAttributes } from "react";
2
+ import type { PhoneEvent } from "./index.js";
3
+
4
+ export type NomacPhoneHandle = { home(): void; tap(x: number, y: number): void; screenshot(options?: { timeoutMs?: number }): Promise<string> };
5
+ export type NomacPhoneProps = { url: string; onEvent?: (event: PhoneEvent) => void; title?: string; className?: string; style?: CSSProperties };
6
+ export const NomacPhone: ForwardRefExoticComponent<NomacPhoneProps & RefAttributes<NomacPhoneHandle>>;
package/react.js ADDED
@@ -0,0 +1,24 @@
1
+ import { createElement, forwardRef, useEffect, useImperativeHandle, useRef } from "react";
2
+ import { embedPhone } from "./index.js";
3
+
4
+ /**
5
+ * <NomacPhone url={viewLink} onEvent={...} /> shows the phone and fills its box; a ref gives home, tap and
6
+ * screenshot.
7
+ */
8
+ export const NomacPhone = forwardRef(function NomacPhone({ url, onEvent, title, className, style }, ref) {
9
+ const box = useRef(null);
10
+ const phone = useRef(null);
11
+ const handler = useRef(onEvent);
12
+ handler.current = onEvent;
13
+ useEffect(() => {
14
+ if (!box.current) return undefined;
15
+ phone.current = embedPhone(box.current, url, { title, onEvent: (event) => handler.current?.(event) });
16
+ return () => { phone.current?.destroy(); phone.current = null; };
17
+ }, [url, title]);
18
+ useImperativeHandle(ref, () => ({
19
+ home: () => phone.current?.home(),
20
+ tap: (x, y) => phone.current?.tap(x, y),
21
+ screenshot: (options) => phone.current?.screenshot(options) ?? Promise.reject(new Error("The phone is not shown")),
22
+ }), []);
23
+ return createElement("div", { ref: box, className, style: { aspectRatio: "402 / 874", ...style } });
24
+ });