/**
* A complete customer website for one explicitly public film. Requires Node 22.
* Register the configured SITE_ORIGIN as a test site, then register ASSET_ID in that same environment.
* Set PARADYM_PLAYER_KEY to a playback:issue credential in the server's environment.
* For private films implement the customer's membership check on every launch.
*/
import http from "node:http";
import { randomUUID } from "node:crypto";
const origin = new URL(process.env.SITE_ORIGIN || "http://127.0.0.1:3976")
.origin;
const api = new URL(process.env.PARADYM_API_ORIGIN || "https://api.paradym.tv")
.origin;
const player = new URL(process.env.PLAYER_ORIGIN || "https://player.paradym.tv")
.origin;
const asset = process.env.ASSET_ID || "demo-film";
const key = process.env.PARADYM_PLAYER_KEY;
if (!key) throw Error("Set PARADYM_PLAYER_KEY on the customer server.");
for (const value of [origin, api, player]) {
const url = new URL(value);
if (
url.protocol !== "https:" &&
!(
url.protocol === "http:" &&
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
)
)
throw Error("Use HTTPS outside loopback.");
}
http
.createServer(async (req, res) => {
function send(status, type, body, headers = {}) {
res.writeHead(status, {
"Content-Type": type,
"X-Content-Type-Options": "nosniff",
...headers,
});
res.end(body);
}
if (req.method === "GET" && req.url === "/") {
send(
200,
"text/html; charset=utf-8",
'
My cinemaMy cinema
',
{
"Cache-Control": "public, max-age=60",
"Content-Security-Policy":
"default-src 'self'; frame-src " +
player +
"; script-src 'self' " +
player +
"; object-src 'none'; base-uri 'none'",
},
);
return;
}
if (req.method === "GET" && req.url === "/mount.js") {
send(
200,
"text/javascript; charset=utf-8",
`const handle=ParadymPlayer.mount(document.querySelector('#film'),{
playerOrigin:${JSON.stringify(player)},locale:'en',
launch:async()=>{
const result=await fetch('/launch',{method:'POST',headers:{'X-Player-Launch':'1'},cache:'no-store'});
if(!result.ok)throw Error('Cannot authorize playback');
return result.json();
}
});
handle.ready.catch(()=>document.querySelector('[role=status]').textContent='Player unavailable. Reload to retry.');`,
);
return;
}
if (req.method === "POST" && req.url === "/launch") {
const headers = { "Cache-Control": "private, no-store" };
if (
req.headers.origin !== origin ||
req.headers["x-player-launch"] !== "1"
)
return send(403, "text/plain", "Origin denied", headers);
try {
// This example authorizes guests for this one fixed, explicitly public asset.
const response = await fetch(api + "/partner/v1/playback-sessions", {
method: "POST",
redirect: "error",
signal: AbortSignal.timeout(10000),
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + key,
},
body: JSON.stringify({
asset_id: asset,
viewer_id: "guest-" + randomUUID(),
origin,
}),
});
if (!response.ok)
return send(
response.status,
"text/plain",
"Playback unavailable",
headers,
);
const { grant, embed_url, expires_at } = await response.json();
return send(
200,
"application/json",
JSON.stringify({ grant, embed_url, expires_at }),
headers,
);
} catch {
return send(502, "text/plain", "Service unavailable", headers);
}
}
send(404, "text/plain", "Not found");
})
.listen(Number(process.env.PORT || 3976), process.env.HOST || "127.0.0.1");