Skip to content

Commit 7b9d69f

Browse files
committed
refactor golden templates layout
1 parent 7e81cc1 commit 7b9d69f

5 files changed

Lines changed: 145 additions & 54 deletions

File tree

packages/node/src/Transloadit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export {
6666
TimeoutError,
6767
UploadError,
6868
} from 'got'
69-
export { goldenTemplates } from './alphalib/goldenTemplates.ts'
69+
export { goldenTemplates } from './alphalib/goldenTemplates/index.ts'
7070
export type { AssemblyStatus } from './alphalib/types/assemblyStatus.ts'
7171
export * from './apiTypes.ts'
7272
export { InconsistentResponseError, ApiError }

packages/node/src/alphalib/goldenTemplates.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { GoldenTemplateDefinition } from '../types.ts'
2+
3+
export const goldenTemplate: GoldenTemplateDefinition = {
4+
slug: '~transloadit/encode-hls-video@0.0.1',
5+
version: '0.0.1',
6+
description:
7+
'Encode an input video into HLS renditions (270p, 360p, 540p) with an adaptive playlist.',
8+
steps: {
9+
':original': {
10+
robot: '/upload/handle',
11+
},
12+
low: {
13+
robot: '/video/encode',
14+
use: ':original',
15+
ffmpeg_stack: 'v7.0.0',
16+
preset: 'hls-270p',
17+
result: true,
18+
turbo: true,
19+
},
20+
mid: {
21+
robot: '/video/encode',
22+
use: ':original',
23+
ffmpeg_stack: 'v7.0.0',
24+
preset: 'hls-360p',
25+
result: true,
26+
turbo: true,
27+
},
28+
high: {
29+
robot: '/video/encode',
30+
use: ':original',
31+
ffmpeg_stack: 'v7.0.0',
32+
preset: 'hls-540p',
33+
result: true,
34+
turbo: true,
35+
},
36+
adaptive: {
37+
robot: '/video/adaptive',
38+
use: {
39+
steps: ['low', 'mid', 'high'],
40+
bundle_steps: true,
41+
},
42+
technique: 'hls',
43+
playlist_name: 'my_playlist.m3u8',
44+
},
45+
},
46+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { goldenTemplate as encodeHlsVideo001 } from './encode-hls-video/0.0.1.ts'
2+
import type { GoldenTemplate, GoldenTemplateDefinition } from './types.ts'
3+
4+
export type { GoldenTemplate, GoldenTemplateDefinition }
5+
6+
export const goldenTemplates = {
7+
[encodeHlsVideo001.slug]: encodeHlsVideo001,
8+
} satisfies Record<string, GoldenTemplateDefinition>
9+
10+
const goldenTemplatesByKey: Record<string, GoldenTemplateDefinition> = goldenTemplates
11+
12+
function parseSemver(version: string): [number, number, number] {
13+
const parts = version.split('.')
14+
const readPart = (index: number): number => {
15+
const part = parts[index] ?? ''
16+
const match = part.match(/^\d+/)
17+
return match ? Number(match[0]) : 0
18+
}
19+
return [readPart(0), readPart(1), readPart(2)]
20+
}
21+
22+
function compareSemver(a: string, b: string): number {
23+
const [aMajor, aMinor, aPatch] = parseSemver(a)
24+
const [bMajor, bMinor, bPatch] = parseSemver(b)
25+
if (aMajor !== bMajor) {
26+
return aMajor - bMajor
27+
}
28+
if (aMinor !== bMinor) {
29+
return aMinor - bMinor
30+
}
31+
if (aPatch !== bPatch) {
32+
return aPatch - bPatch
33+
}
34+
if (a === b) {
35+
return 0
36+
}
37+
return a < b ? -1 : 1
38+
}
39+
40+
function versionFromKey(key: string): string {
41+
const [, version] = key.split('@')
42+
return version ?? ''
43+
}
44+
45+
function compareGoldenKeys(a: string, b: string): number {
46+
return compareSemver(versionFromKey(a), versionFromKey(b))
47+
}
48+
49+
export function resolveGoldenTemplateKey(slug: string): string | null {
50+
if (slug.includes('@')) {
51+
return goldenTemplatesByKey[slug] ? slug : null
52+
}
53+
54+
const matches = Object.keys(goldenTemplatesByKey).filter((key) => key.startsWith(`${slug}@`))
55+
if (matches.length === 0) {
56+
return null
57+
}
58+
59+
let latest = matches[0]
60+
for (const candidate of matches.slice(1)) {
61+
if (compareGoldenKeys(candidate, latest) > 0) {
62+
latest = candidate
63+
}
64+
}
65+
return latest
66+
}
67+
68+
export function resolveGoldenTemplate(slug: string): GoldenTemplate | null {
69+
const key = resolveGoldenTemplateKey(slug)
70+
return key ? goldenTemplatesByKey[key] : null
71+
}
72+
73+
export function listGoldenTemplateKeys(include: 'latest' | 'all' = 'latest'): string[] {
74+
const keys = Object.keys(goldenTemplatesByKey)
75+
if (include === 'all') {
76+
return keys
77+
}
78+
79+
const latestBySlug = new Map<string, string>()
80+
for (const key of keys) {
81+
const base = key.split('@')[0]
82+
const existing = latestBySlug.get(base)
83+
if (!existing || compareGoldenKeys(key, existing) > 0) {
84+
latestBySlug.set(base, key)
85+
}
86+
}
87+
return [...latestBySlug.values()]
88+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { AssemblyInstructionsInput } from '../types/template.ts'
2+
3+
export type GoldenTemplate = {
4+
slug: string
5+
version: string
6+
description: string
7+
steps: AssemblyInstructionsInput['steps']
8+
}
9+
10+
export type GoldenTemplateDefinition = GoldenTemplate

0 commit comments

Comments
 (0)