wormkey-overlay 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/dist/auto.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ declare global {
2
+ interface Window {
3
+ __WORMKEY_OVERLAY_URL__?: string;
4
+ }
5
+ }
6
+ export declare function auto(scriptUrl?: string): boolean;
package/dist/auto.js ADDED
@@ -0,0 +1,19 @@
1
+ export function auto(scriptUrl) {
2
+ if (typeof document === "undefined")
3
+ return false;
4
+ const src = scriptUrl ??
5
+ window.__WORMKEY_OVERLAY_URL__ ??
6
+ document.querySelector('meta[name="wormkey-overlay-url"]')?.getAttribute("content") ??
7
+ "";
8
+ if (!src)
9
+ return false;
10
+ if (document.querySelector(`script[data-wormkey-overlay="1"]`))
11
+ return true;
12
+ const script = document.createElement("script");
13
+ script.defer = true;
14
+ script.src = src;
15
+ script.dataset.wormkeyOverlay = "1";
16
+ document.head.appendChild(script);
17
+ return true;
18
+ }
19
+ auto();
@@ -0,0 +1,13 @@
1
+ export interface ExpressOverlayOptions {
2
+ scriptUrl: string;
3
+ }
4
+ interface ReqLike {
5
+ [k: string]: unknown;
6
+ }
7
+ interface ResLike {
8
+ send: (body: unknown) => unknown;
9
+ getHeader: (name: string) => unknown;
10
+ }
11
+ type NextLike = () => void;
12
+ export declare function wormkeyOverlayMiddleware(options: ExpressOverlayOptions): (_req: ReqLike, res: ResLike, next: NextLike) => void;
13
+ export {};
@@ -0,0 +1,16 @@
1
+ export function wormkeyOverlayMiddleware(options) {
2
+ return function overlay(_req, res, next) {
3
+ const send = res.send.bind(res);
4
+ res.send = ((body) => {
5
+ const contentType = String(res.getHeader("content-type") ?? "");
6
+ if (typeof body === "string" &&
7
+ contentType.includes("text/html") &&
8
+ !body.includes("data-wormkey-overlay=\"1\"")) {
9
+ const tag = `<script defer data-wormkey-overlay=\"1\" src=\"${options.scriptUrl}\"></script>`;
10
+ body = body.includes("</head>") ? body.replace("</head>", `${tag}</head>`) : `${tag}${body}`;
11
+ }
12
+ return send(body);
13
+ });
14
+ next();
15
+ };
16
+ }
@@ -0,0 +1,3 @@
1
+ export { auto } from "./auto.js";
2
+ export { wormkeyOverlayMiddleware } from "./express.js";
3
+ export { WormkeyOverlay } from "./react.js";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { auto } from "./auto.js";
2
+ export { wormkeyOverlayMiddleware } from "./express.js";
3
+ export { WormkeyOverlay } from "./react.js";
@@ -0,0 +1,21 @@
1
+ export interface WormkeyOverlayProps {
2
+ /** Full overlay script URL, e.g. https://wormkey.run/.wormkey/overlay.js?slug=pale-snow-91 */
3
+ scriptUrl?: string;
4
+ /** Gateway base URL (e.g. https://wormkey.run). Use with slug. */
5
+ gatewayUrl?: string;
6
+ /** Slug for the wormhole. If omitted and viewing at /s/:slug, derived from URL. */
7
+ slug?: string;
8
+ }
9
+ /**
10
+ * Wormkey overlay bar for owners. Import in your RootLayout.
11
+ * Loads the overlay script from the gateway; API requests go to the gateway (not the page origin).
12
+ *
13
+ * @example
14
+ * // Option 1: Full script URL (from CLI output or session)
15
+ * <WormkeyOverlay scriptUrl="https://wormkey.run/.wormkey/overlay.js?slug=pale-snow-91" />
16
+ *
17
+ * @example
18
+ * // Option 2: Gateway + slug (slug from env or derived from URL when viewing at /s/:slug)
19
+ * <WormkeyOverlay gatewayUrl="https://wormkey.run" slug={slug} />
20
+ */
21
+ export declare function WormkeyOverlay({ scriptUrl, gatewayUrl, slug: slugProp }: WormkeyOverlayProps): null;
package/dist/react.js ADDED
@@ -0,0 +1,34 @@
1
+ "use client";
2
+ import { useEffect } from "react";
3
+ /**
4
+ * Wormkey overlay bar for owners. Import in your RootLayout.
5
+ * Loads the overlay script from the gateway; API requests go to the gateway (not the page origin).
6
+ *
7
+ * @example
8
+ * // Option 1: Full script URL (from CLI output or session)
9
+ * <WormkeyOverlay scriptUrl="https://wormkey.run/.wormkey/overlay.js?slug=pale-snow-91" />
10
+ *
11
+ * @example
12
+ * // Option 2: Gateway + slug (slug from env or derived from URL when viewing at /s/:slug)
13
+ * <WormkeyOverlay gatewayUrl="https://wormkey.run" slug={slug} />
14
+ */
15
+ export function WormkeyOverlay({ scriptUrl, gatewayUrl, slug: slugProp }) {
16
+ useEffect(() => {
17
+ if (typeof document === "undefined")
18
+ return;
19
+ const slug = slugProp ??
20
+ (typeof window !== "undefined" && window.location.pathname.match(/^\/s\/([^/]+)/)?.[1]);
21
+ const base = gatewayUrl?.replace(/\/$/, "") ?? process.env.NEXT_PUBLIC_WORMKEY_GATEWAY_URL;
22
+ const url = scriptUrl ?? (base && slug ? `${base}/.wormkey/overlay.js?slug=${slug}` : null);
23
+ if (!url)
24
+ return;
25
+ if (document.querySelector('[data-wormkey-overlay="1"]'))
26
+ return;
27
+ const script = document.createElement("script");
28
+ script.src = url;
29
+ script.defer = true;
30
+ script.setAttribute("data-wormkey-overlay", "1");
31
+ document.body.appendChild(script);
32
+ }, [scriptUrl, gatewayUrl, slugProp]);
33
+ return null;
34
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "wormkey-overlay",
3
+ "version": "0.4.0",
4
+ "description": "Wormkey in-app overlay helpers",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "exports": {
12
+ ".": "./dist/index.js",
13
+ "./auto": "./dist/auto.js",
14
+ "./express": "./dist/express.js",
15
+ "./react": "./dist/react.js"
16
+ },
17
+ "scripts": {
18
+ "build": "tsc"
19
+ },
20
+ "peerDependencies": {
21
+ "react": ">=18.0.0"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^20.10.0",
25
+ "@types/react": "^18.0.0",
26
+ "react": "^18.0.0",
27
+ "typescript": "^5.3.0"
28
+ }
29
+ }