Skip to content

Commit

Permalink
Support CF workers
Browse files Browse the repository at this point in the history
  • Loading branch information
matvp91 committed Nov 20, 2024
1 parent ae9cd8d commit c273c13
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 60 deletions.
Binary file modified bun.lockb
Binary file not shown.
9 changes: 3 additions & 6 deletions packages/api/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { parseEnv } from "shared/env";

export const env = parseEnv((z) => ({
// process
PORT: z.coerce.number().default(52001),
HOST: z.string().default("0.0.0.0"),

// config.env
REDIS_HOST: z.string(),
REDIS_PORT: z.coerce.number(),
HOST: z.string().optional(),
REDIS_HOST: z.string().default("localhost"),
REDIS_PORT: z.coerce.number().default(6379),
S3_ENDPOINT: z.string(),
S3_REGION: z.string(),
S3_ACCESS_KEY: z.string(),
Expand Down
5 changes: 2 additions & 3 deletions packages/artisan/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { parseEnv } from "shared/env";

export const env = parseEnv((z) => ({
// config.env
REDIS_HOST: z.string().default("localhost"),
REDIS_PORT: z.coerce.number().default(6379),
S3_ENDPOINT: z.string(),
S3_REGION: z.string(),
S3_ACCESS_KEY: z.string(),
S3_SECRET_KEY: z.string(),
S3_BUCKET: z.string(),
REDIS_HOST: z.string(),
REDIS_PORT: z.coerce.number(),
}));
4 changes: 2 additions & 2 deletions packages/bolt/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { parseEnv } from "shared/env";

const env = parseEnv((z) => ({
// config.env
REDIS_HOST: z.string(),
REDIS_PORT: z.coerce.number(),
REDIS_HOST: z.string().default("localhost"),
REDIS_PORT: z.coerce.number().default(6379),
}));

export const connection = {
Expand Down
2 changes: 1 addition & 1 deletion packages/stitcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
"@matvp91/elysia-swagger": "^2.0.0",
"@superstreamer/api": "workspace:*",
"@xmldom/xmldom": "^0.8.10",
"cryptr": "^6.3.0",
"dom-parser": "^1.1.5",
"elysia": "^1.1.24",
"hh-mm-ss": "^1.2.0",
"lru-cache": "^11.0.2",
"luxon": "^3.5.0",
"redis": "^4.7.0",
"secure-encrypt": "^1.0.12",
"shared": "workspace:*",
"superjson": "^2.2.1",
"uuid": "^10.0.0",
Expand Down
7 changes: 1 addition & 6 deletions packages/stitcher/runtime/local.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { parseEnv } from "shared/env";
import { createApp } from "../src";

const env = parseEnv((z) => ({
PORT: z.coerce.number().default(52002),
HOST: z.string().default("0.0.0.0"),
}));
import { env } from "../src/env";

const app = createApp({
aot: true,
Expand Down
5 changes: 3 additions & 2 deletions packages/stitcher/src/adapters/kv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ interface KeyValue {

export let kv: KeyValue;

if (env.SERVERLESS) {
// Map each KV adapter here to their corresponding import.
if (env.KV === "cloudflare-kv") {
kv = await import("./cloudflare-kv");
} else {
} else if (env.KV === "redis") {
kv = await import("./redis");
}
9 changes: 6 additions & 3 deletions packages/stitcher/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { parseEnv } from "shared/env";

export const env = parseEnv((z) => ({
SERVERLESS: z.coerce.boolean().default(false),
PORT: z.coerce.number().default(52002),
HOST: z.string().optional(),

KV: z.enum(["redis", "cloudflare-kv"]).default("redis"),
REDIS_HOST: z.string().default("localhost"),
REDIS_PORT: z.coerce.number().default(6379),

REDIS_HOST: z.string(),
REDIS_PORT: z.coerce.number(),
PUBLIC_S3_ENDPOINT: z.string(),
PUBLIC_STITCHER_ENDPOINT: z.string(),
PUBLIC_API_ENDPOINT: z.string(),
Expand Down
10 changes: 4 additions & 6 deletions packages/stitcher/src/lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import Cryptr from "cryptr";
import * as crypto from "secure-encrypt";
import { env } from "../env";

const cryptr = new Cryptr(env.SUPER_SECRET ?? "__UNSECURE__", {
encoding: "base64",
});
const secret = env.SUPER_SECRET ?? "__UNSECURE__";

export function encrypt(value: string) {
return cryptr.encrypt(value);
return btoa(crypto.encrypt(value, secret));
}

export function decrypt(value: string) {
return cryptr.decrypt(value);
return crypto.decrypt(atob(value), secret);
}
30 changes: 5 additions & 25 deletions packages/stitcher/src/routes/session.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Elysia, t } from "elysia";
import { getFilterFromQuery, getQueryParamsFromFilter } from "../filters";
import { getFilterFromQuery } from "../filters";
import { decrypt } from "../lib/crypto";
import { makeUrl } from "../lib/url";
import {
formatAssetList,
formatMasterPlaylist,
Expand All @@ -22,8 +21,10 @@ export const sessionRoutes = new Elysia()

const filter = body.filter ?? {};

const url = makeUrl(`session/${session.id}/master.m3u8`, {
...getQueryParamsFromFilter(filter),
const url = makeMasterUrl({
url: session.url,
filter,
session,
});

return { url };
Expand Down Expand Up @@ -87,27 +88,6 @@ export const sessionRoutes = new Elysia()
}),
},
)
.get(
"/session/:sessionId/master.m3u8",
async ({ params, query, redirect }) => {
const session = await getSession(params.sessionId);
const url = makeMasterUrl({
url: session.url,
filter: getFilterFromQuery(query),
session,
});
return redirect(url, 302);
},
{
params: t.Object({
sessionId: t.String(),
}),
query: t.Object({
"filter.resolution": t.Optional(t.String()),
"filter.audioLanguage": t.Optional(t.String()),
}),
},
)
.get(
"/out/master.m3u8",
async ({ set, query }) => {
Expand Down
8 changes: 2 additions & 6 deletions packages/stitcher/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
name = "superstreamer-stitcher"
name = "sprs-stitcher"
compatibility_flags = [ "nodejs_compat" ]
compatibility_date = "2024-09-23"
send_metrics = false

[vars]
SERVERLESS = "true"
PORT = "0"
HOST = "0.0.0.0"
REDIS_HOST = "redis"
REDIS_PORT = "0000"
KV = "cloudflare-kv"
PUBLIC_S3_ENDPOINT = "https://cdn.superstreamer.xyz"
PUBLIC_STITCHER_ENDPOINT = "https://stitcher.superstreamer.xyz"
PUBLIC_API_ENDPOINT = "https://api.tunnel.superstreamer.xyz"
Expand Down

0 comments on commit c273c13

Please sign in to comment.