Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 1x 1x 1x 3x 3x 1x 1x 1x 8x 8x 8x 2x 2x 5x 2x 2x 2x 8x 4x 4x 1x 1x 3x 3x 2x 3x 8x 8x 8x 6x 6x 2x 2x 14x 14x 14x 14x 2x 2x 2x 2x 2x 12x 12x 6x 6x 6x 5x 5x 6x 6x 1x 6x 4x 4x 4x 4x 1x 1x 1x 4x 1x 1x 1x 2x 4x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 6x 6x 8x 8x 8x 7x 8x 8x 1x 1x 1x 1x 1x 6x 6x 6x 8x 1x 1x 1x 8x 1x 1x 1x 4x 8x 1x 1x 1x 3x 3x 3x 8x 1x 1x 1x 1x 1x 8x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 1x 1x 1x | #!/usr/bin/env node import { realpathSync } from 'node:fs' import path from 'node:path' import process from 'node:process' import { fileURLToPath } from 'node:url' import { type ZodIssue, z } from 'zod' import { assemblyAuthInstructionsSchema, assemblyInstructionsSchema, } from './alphalib/types/template.ts' import type { OptionalAuthParams } from './apiTypes.ts' import { Transloadit } from './Transloadit.ts' type UrlParamPrimitive = string | number | boolean type UrlParamArray = UrlParamPrimitive[] type NormalizedUrlParams = Record<string, UrlParamPrimitive | UrlParamArray> interface RunSigOptions { providedInput?: string algorithm?: string } interface RunSmartSigOptions { providedInput?: string } const smartCdnParamsSchema = z .object({ workspace: z.string().min(1, 'workspace is required'), template: z.string().min(1, 'template is required'), input: z.union([z.string(), z.number(), z.boolean()]), url_params: z.record(z.unknown()).optional(), expire_at_ms: z.union([z.number(), z.string()]).optional(), }) .passthrough() const cliSignatureParamsSchema = assemblyInstructionsSchema .extend({ auth: assemblyAuthInstructionsSchema.partial().optional() }) .partial() .passthrough() export async function readStdin(): Promise<string> { if (process.stdin.isTTY) return '' process.stdin.setEncoding('utf8') let data = '' for await (const chunk of process.stdin) { data += chunk } return data } function fail(message: string): void { console.error(message) process.exitCode = 1 } function formatIssues(issues: ZodIssue[]): string { return issues .map((issue) => { const path = issue.path.join('.') || '(root)' return `${path}: ${issue.message}` }) .join('; ') } function normalizeUrlParam(value: unknown): UrlParamPrimitive | UrlParamArray | undefined { if (value == null) return undefined if (Array.isArray(value)) { const normalized = value.filter( (item): item is UrlParamPrimitive => typeof item === 'string' || typeof item === 'number' || typeof item === 'boolean', ) return normalized.length > 0 ? normalized : undefined } if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { return value } return undefined } function normalizeUrlParams(params?: Record<string, unknown>): NormalizedUrlParams | undefined { if (params == null) return undefined let normalized: NormalizedUrlParams | undefined for (const [key, value] of Object.entries(params)) { const normalizedValue = normalizeUrlParam(value) if (normalizedValue === undefined) continue if (normalized == null) normalized = {} normalized[key] = normalizedValue } return normalized } function ensureCredentials(): { authKey: string; authSecret: string } | null { const authKey = process.env.TRANSLOADIT_KEY || process.env.TRANSLOADIT_AUTH_KEY const authSecret = process.env.TRANSLOADIT_SECRET || process.env.TRANSLOADIT_AUTH_SECRET if (!authKey || !authSecret) { fail( 'Missing credentials. Please set TRANSLOADIT_KEY and TRANSLOADIT_SECRET environment variables.', ) return null } return { authKey, authSecret } } export async function runSig(options: RunSigOptions = {}): Promise<void> { const credentials = ensureCredentials() if (credentials == null) return const { authKey, authSecret } = credentials const { providedInput, algorithm } = options const rawInput = providedInput ?? (await readStdin()) const input = rawInput.trim() let params: Record<string, unknown> if (input === '') { params = { auth: { key: authKey } } } else { let parsed: unknown try { parsed = JSON.parse(input) } catch (error) { fail(`Failed to parse JSON from stdin: ${(error as Error).message}`) return } if (parsed == null || typeof parsed !== 'object' || Array.isArray(parsed)) { fail('Invalid params provided via stdin. Expected a JSON object.') return } const parsedResult = cliSignatureParamsSchema.safeParse(parsed) if (!parsedResult.success) { fail(`Invalid params: ${formatIssues(parsedResult.error.issues)}`) return } const parsedParams = parsedResult.data as Record<string, unknown> const existingAuth = typeof parsedParams.auth === 'object' && parsedParams.auth != null && !Array.isArray(parsedParams.auth) ? (parsedParams.auth as Record<string, unknown>) : {} params = { ...parsedParams, auth: { ...existingAuth, key: authKey, }, } } const client = new Transloadit({ authKey, authSecret }) try { const signature = client.calcSignature(params as OptionalAuthParams, algorithm) process.stdout.write(`${JSON.stringify(signature)}\n`) } catch (error) { fail(`Failed to generate signature: ${(error as Error).message}`) } } export async function runSmartSig(options: RunSmartSigOptions = {}): Promise<void> { const credentials = ensureCredentials() if (credentials == null) return const { authKey, authSecret } = credentials const rawInput = options.providedInput ?? (await readStdin()) const input = rawInput.trim() if (input === '') { fail( 'Missing params provided via stdin. Expected a JSON object with workspace, template, input, and optional Smart CDN parameters.', ) return } let parsed: unknown try { parsed = JSON.parse(input) } catch (error) { fail(`Failed to parse JSON from stdin: ${(error as Error).message}`) return } if (parsed == null || typeof parsed !== 'object' || Array.isArray(parsed)) { fail('Invalid params provided via stdin. Expected a JSON object.') return } const parsedResult = smartCdnParamsSchema.safeParse(parsed) if (!parsedResult.success) { fail(`Invalid params: ${formatIssues(parsedResult.error.issues)}`) return } const { workspace, template, input: inputFieldRaw, url_params, expire_at_ms } = parsedResult.data const urlParams = normalizeUrlParams(url_params as Record<string, unknown> | undefined) let expiresAt: number | undefined if (typeof expire_at_ms === 'string') { const parsedNumber = Number.parseInt(expire_at_ms, 10) if (Number.isNaN(parsedNumber)) { fail('Invalid params: expire_at_ms must be a number.') return } expiresAt = parsedNumber } else { expiresAt = expire_at_ms } const inputField = typeof inputFieldRaw === 'string' ? inputFieldRaw : String(inputFieldRaw) const client = new Transloadit({ authKey, authSecret }) try { const signedUrl = client.getSignedSmartCDNUrl({ workspace, template, input: inputField, urlParams, expiresAt, }) process.stdout.write(`${signedUrl}\n`) } catch (error) { fail(`Failed to generate Smart CDN URL: ${(error as Error).message}`) } } function parseSigArguments(args: string[]): { algorithm?: string } { let algorithm: string | undefined let index = 0 while (index < args.length) { const arg = args[index] if (arg === '--algorithm' || arg === '-a') { const next = args[index + 1] if (next == null || next.startsWith('-')) { throw new Error('Missing value for --algorithm option') } algorithm = next index += 2 continue } if (arg.startsWith('--algorithm=')) { const [, value] = arg.split('=', 2) if (value === undefined || value === '') { throw new Error('Missing value for --algorithm option') } algorithm = value index += 1 continue } throw new Error(`Unknown option: ${arg}`) } return { algorithm } } export async function main(args = process.argv.slice(2)): Promise<void> { const [command, ...commandArgs] = args switch (command) { case 'smart_sig': { await runSmartSig() break } case 'sig': { try { const { algorithm } = parseSigArguments(commandArgs) await runSig({ algorithm }) } catch (error) { fail((error as Error).message) } break } case '-h': case '--help': case undefined: { process.stdout.write( [ 'Usage:', ' npx transloadit smart_sig Read Smart CDN params JSON from stdin and output a signed URL.', ' npx transloadit sig [--algorithm <name>] Read params JSON from stdin and output signed payload JSON.', '', 'Required JSON fields:', ' smart_sig: workspace, template, input', ' sig: none (object is optional)', 'Optional JSON fields:', ' smart_sig: expire_at_ms, url_params', ' sig: auth.expires and any supported assembly params', '', 'Environment variables:', ' TRANSLOADIT_KEY, TRANSLOADIT_SECRET', ].join('\n'), ) if (command === undefined) process.exitCode = 1 break } default: { fail(`Unknown command: ${command}`) } } } const currentFile = realpathSync(fileURLToPath(import.meta.url)) function resolveInvokedPath(invoked?: string): string | null { if (invoked == null) return null try { return realpathSync(invoked) } catch { return path.resolve(invoked) } } export function shouldRunCli(invoked?: string): boolean { const resolved = resolveInvokedPath(invoked) if (resolved == null) return false return resolved === currentFile } export function runCliWhenExecuted(): void { if (!shouldRunCli(process.argv[1])) return void main().catch((error) => { fail((error as Error).message) }) } runCliWhenExecuted() |