@nomac/phone 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +25 -2
- package/index.d.ts +23 -0
- package/index.js +52 -0
- package/package.json +14 -4
- package/react.d.ts +6 -0
- package/react.js +24 -0
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,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nomac/phone
|
|
2
2
|
|
|
3
|
-
|
|
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 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. */
|
|
15
|
+
screenshot(): 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. */
|
|
21
|
+
export function embedUrl(viewUrl: string): string;
|
|
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;
|
package/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
/** The view link with embed=1: the phone alone, filling its frame, talking to the page. */
|
|
11
|
+
export function embedUrl(viewUrl) {
|
|
12
|
+
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);
|
|
14
|
+
url.searchParams.set("embed", "1");
|
|
15
|
+
return url.toString();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Puts the phone in container. options.onEvent receives { type, ...detail } for ready, app_launched, ended,
|
|
20
|
+
* error and screenshot. Answers controls for it, and destroy() to take it away.
|
|
21
|
+
*/
|
|
22
|
+
export function embedPhone(container, viewUrl, options = {}) {
|
|
23
|
+
const src = embedUrl(viewUrl);
|
|
24
|
+
const origin = new URL(src).origin;
|
|
25
|
+
const frame = document.createElement("iframe");
|
|
26
|
+
frame.src = src;
|
|
27
|
+
frame.title = options.title ?? "Phone";
|
|
28
|
+
frame.allow = "clipboard-write";
|
|
29
|
+
frame.style.cssText = "border:0;width:100%;height:100%;display:block;background:#000";
|
|
30
|
+
container.appendChild(frame);
|
|
31
|
+
const screenshots = [];
|
|
32
|
+
const onMessage = (event) => {
|
|
33
|
+
if (event.source !== frame.contentWindow || event.origin !== origin || event.data?.source !== "nomac-phone") return;
|
|
34
|
+
const { source: _source, ...message } = event.data;
|
|
35
|
+
if (message.type === "screenshot") screenshots.shift()?.(message.data);
|
|
36
|
+
options.onEvent?.(message);
|
|
37
|
+
};
|
|
38
|
+
window.addEventListener("message", onMessage);
|
|
39
|
+
const command = (message) => frame.contentWindow?.postMessage({ target: "nomac-phone", ...message }, origin);
|
|
40
|
+
return {
|
|
41
|
+
frame,
|
|
42
|
+
/** Presses the Home button (a control link only). */
|
|
43
|
+
home: () => command({ type: "home" }),
|
|
44
|
+
/** Taps at x and y, each from 0 to 1 across the screen (a control link only). */
|
|
45
|
+
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" }); }),
|
|
48
|
+
destroy: () => { window.removeEventListener("message", onMessage); frame.remove(); },
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { ORIGIN };
|
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nomac/phone",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
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(): 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: () => phone.current?.screenshot() ?? Promise.reject(new Error("The phone is not shown")),
|
|
22
|
+
}), []);
|
|
23
|
+
return createElement("div", { ref: box, className, style: { aspectRatio: "402 / 874", ...style } });
|
|
24
|
+
});
|