Skip to content

Commit

Permalink
fix: Remove shared enums (#134)
Browse files Browse the repository at this point in the history
* Removed shared enums

* Removed getLangCode from shared

* Updated kv
  • Loading branch information
matvp91 authored Dec 5, 2024
1 parent 3c014ca commit 9cc8ff2
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 122 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 2 additions & 3 deletions packages/api/src/routes/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
pipelineQueue,
transcodeQueue,
} from "bolt";
import { AudioCodec, VideoCodec } from "bolt/types";
import { Elysia, t } from "elysia";
import { auth } from "../auth";
import { DeliberateError } from "../errors";
Expand Down Expand Up @@ -43,14 +42,14 @@ const InputSchema = t.Union([
const StreamSchema = t.Union([
t.Object({
type: t.Literal("video"),
codec: t.Enum(VideoCodec),
codec: t.Union([t.Literal("h264"), t.Literal("vp9"), t.Literal("hevc")]),
height: t.Number(),
bitrate: t.Optional(t.Number({ description: "Bitrate in bps" })),
framerate: t.Optional(t.Number({ description: "Frames per second" })),
}),
t.Object({
type: t.Literal("audio"),
codec: t.Enum(AudioCodec),
codec: t.Union([t.Literal("aac"), t.Literal("ac3"), t.Literal("eac3")]),
bitrate: t.Optional(t.Number({ description: "Bitrate in bps" })),
language: t.Optional(t.String()),
channels: t.Optional(t.Number()),
Expand Down
62 changes: 31 additions & 31 deletions packages/artisan/src/lib/default-values.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { AudioCodec, VideoCodec } from "bolt/types";
import type { AudioCodec, VideoCodec } from "bolt";

const DEFAULT_AUDIO_BITRATE: Record<number, Record<AudioCodec, number>> = {
2: {
[AudioCodec.aac]: 128000,
[AudioCodec.ac3]: 192000,
[AudioCodec.eac3]: 96000,
aac: 128000,
ac3: 192000,
eac3: 96000,
},
6: {
[AudioCodec.aac]: 256000,
[AudioCodec.ac3]: 384000,
[AudioCodec.eac3]: 192000,
aac: 256000,
ac3: 384000,
eac3: 192000,
},
};

Expand All @@ -19,44 +19,44 @@ export function getDefaultAudioBitrate(channels: number, codec: AudioCodec) {

const DEFAULT_VIDEO_BITRATE: Record<number, Record<VideoCodec, number>> = {
144: {
[VideoCodec.h264]: 108000,
[VideoCodec.hevc]: 96000,
[VideoCodec.vp9]: 96000,
h264: 108000,
hevc: 96000,
vp9: 96000,
},
240: {
[VideoCodec.h264]: 242000,
[VideoCodec.hevc]: 151000,
[VideoCodec.vp9]: 151000,
h264: 242000,
hevc: 151000,
vp9: 151000,
},
360: {
[VideoCodec.h264]: 400000,
[VideoCodec.hevc]: 277000,
[VideoCodec.vp9]: 277000,
h264: 400000,
hevc: 277000,
vp9: 277000,
},
480: {
[VideoCodec.h264]: 1000000,
[VideoCodec.hevc]: 512000,
[VideoCodec.vp9]: 512000,
h264: 1000000,
hevc: 512000,
vp9: 512000,
},
720: {
[VideoCodec.h264]: 2000000,
[VideoCodec.hevc]: 1000000,
[VideoCodec.vp9]: 1000000,
h264: 2000000,
hevc: 1000000,
vp9: 1000000,
},
1080: {
[VideoCodec.h264]: 4000000,
[VideoCodec.hevc]: 2000000,
[VideoCodec.vp9]: 2000000,
h264: 4000000,
hevc: 2000000,
vp9: 2000000,
},
1440: {
[VideoCodec.h264]: 9000000,
[VideoCodec.hevc]: 6000000,
[VideoCodec.vp9]: 6000000,
h264: 9000000,
hevc: 6000000,
vp9: 6000000,
},
2160: {
[VideoCodec.h264]: 17000000,
[VideoCodec.hevc]: 12000000,
[VideoCodec.vp9]: 12000000,
h264: 17000000,
hevc: 12000000,
vp9: 12000000,
},
};

Expand Down
9 changes: 8 additions & 1 deletion packages/artisan/src/workers/transcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
outcomeQueue,
waitForChildren,
} from "bolt";
import { by639_2T } from "iso-language-codes";
import { assert } from "shared/assert";
import { getLangCode } from "shared/lang";
import {
getDefaultAudioBitrate,
getDefaultVideoBitrate,
Expand Down Expand Up @@ -353,3 +353,10 @@ function defaultReason<T extends Stream["type"]>(
"You will have to provide it in the stream instead."
);
}

function getLangCode(value?: string) {
if (value && value in by639_2T) {
return value;
}
return null;
}
21 changes: 10 additions & 11 deletions packages/artisan/test/workers/transcode.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "bun";
import { AudioCodec, VideoCodec } from "bolt/types";
import { describe, expect, test } from "bun:test";
import {
getMatches,
Expand Down Expand Up @@ -74,7 +73,7 @@ describe("merge stream", () => {
const stream = mergeStream(
{
type: "video",
codec: VideoCodec.h264,
codec: "h264",
height: 720,
},
{
Expand All @@ -90,7 +89,7 @@ describe("merge stream", () => {
const stream = mergeStream(
{
type: "video",
codec: VideoCodec.h264,
codec: "h264",
height: 1080,
},
{
Expand All @@ -107,7 +106,7 @@ describe("merge stream", () => {
const stream = mergeStream(
{
type: "video",
codec: VideoCodec.h264,
codec: "h264",
height: 480,
},
{
Expand All @@ -124,7 +123,7 @@ describe("merge stream", () => {
const stream = mergeStream(
{
type: "audio",
codec: AudioCodec.aac,
codec: "aac",
},
{
type: "audio",
Expand All @@ -143,34 +142,34 @@ describe("get list of matches", () => {
[
{
type: "video",
codec: VideoCodec.hevc,
codec: "hevc",
height: 1080,
},
{
type: "video",
codec: VideoCodec.h264,
codec: "h264",
height: 720,
},
{
type: "audio",
codec: AudioCodec.eac3,
codec: "eac3",
channels: 100,
bitrate: 1_000_000,
},
{
type: "audio",
codec: AudioCodec.ac3,
codec: "ac3",
channels: 6,
},
{
type: "audio",
codec: AudioCodec.aac,
codec: "aac",
channels: 2,
language: "eng",
},
{
type: "audio",
codec: AudioCodec.aac,
codec: "aac",
language: "nld",
},
],
Expand Down
5 changes: 1 addition & 4 deletions packages/bolt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"private": true,
"version": "0.0.0",
"type": "module",
"exports": {
".": "./src/index.ts",
"./types": "./src/types.ts"
},
"main": "./src/index.ts",
"scripts": {
"lint": "tsc && eslint"
},
Expand Down
12 changes: 2 additions & 10 deletions packages/bolt/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
export enum AudioCodec {
aac = "aac",
ac3 = "ac3",
eac3 = "eac3",
}
export type VideoCodec = "h264" | "vp9" | "hevc";

export enum VideoCodec {
h264 = "h264",
vp9 = "vp9",
hevc = "hevc",
}
export type AudioCodec = "aac" | "ac3" | "eac3";

export type PartialStream =
| {
Expand Down
2 changes: 0 additions & 2 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
"lint": "tsc && eslint"
},
"dependencies": {
"@sinclair/typebox": "^0.32.34",
"dotenv": "^16.4.5",
"find-config": "^1.0.0",
"iso-language-codes": "^2.0.0",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
8 changes: 0 additions & 8 deletions packages/shared/src/lang.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/stitcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"config": "workspace:*",
"eslint": "^9.14.0",
"typescript": "^5.6.3",
"wrangler": "^3.84.1"
"wrangler": "^3.92.0"
},
"dependencies": {
"@elysiajs/cors": "^1.1.1",
Expand Down
36 changes: 20 additions & 16 deletions packages/stitcher/src/adapters/kv/cloudflare-kv.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import type { Kv } from ".";
import type { KVNamespace } from "@cloudflare/workers-types";

// Make sure wrangler.toml has a binding named "kv".
const kv = process.env["kv"] as unknown as KVNamespace;
export class CloudflareKv implements Kv {
async set(key: string, value: string, ttl: number) {
const kv = this.getKv_();
await kv.put(key, value, {
expirationTtl: ttl,
});
}

if (!kv) {
throw new ReferenceError(
'No kv found for Cloudflare, make sure you have "kv"' +
" set as binding in wrangler.toml.",
);
}

export async function set(key: string, value: string, ttl: number) {
await kv.put(key, value, {
expirationTtl: ttl,
});
}
async get(key: string) {
const kv = this.getKv_();
return await kv.get(key);
}

export async function get(key: string) {
return await kv.get(key);
private getKv_() {
// Make sure wrangler.toml has a binding named "kv".
if ("kv" in process.env) {
// @ts-expect-error Proper cast
return process.env.kv as KVNamespace;
}
throw new Error("Cloudflare KV is not found in process.env");
}
}
12 changes: 7 additions & 5 deletions packages/stitcher/src/adapters/kv/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { CloudflareKv } from "./cloudflare-kv";
import { RedisKv } from "./redis-kv";
import { env } from "../../env";

interface KeyValue {
export interface Kv {
set(key: string, value: string, ttl: number): Promise<void>;
get(key: string): Promise<string | null>;
}

export let kv: KeyValue;
export let kv: Kv;

// Map each KV adapter here to their corresponding import.
// Map each KV adapter here to their corresponding constructor.
if (env.KV === "cloudflare-kv") {
kv = await import("./cloudflare-kv");
kv = new CloudflareKv();
} else if (env.KV === "redis") {
kv = await import("./redis");
kv = new RedisKv(env.REDIS_HOST, env.REDIS_PORT);
}
36 changes: 36 additions & 0 deletions packages/stitcher/src/adapters/kv/redis-kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createClient } from "redis";
import type { Kv } from ".";

const REDIS_PREFIX = "stitcher";

export class RedisKv implements Kv {
private isConnected_ = false;

private client_: ReturnType<typeof createClient>;

constructor(host: string, port: number) {
this.client_ = createClient({
socket: { host, port },
});
}

async set(key: string, value: string, ttl: number) {
await this.init_();
await this.client_.set(`${REDIS_PREFIX}:${key}`, value, {
EX: ttl,
});
}

async get(key: string) {
await this.init_();
return await this.client_.get(`${REDIS_PREFIX}:${key}`);
}

private async init_() {
if (this.isConnected_) {
return;
}
await this.client_.connect();
this.isConnected_ = true;
}
}
Loading

0 comments on commit 9cc8ff2

Please sign in to comment.