-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathhelpers.ts
More file actions
50 lines (42 loc) · 1.51 KB
/
helpers.ts
File metadata and controls
50 lines (42 loc) · 1.51 KB
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
import fs from 'node:fs'
import type { Readable } from 'node:stream'
import { isAPIError } from './types.ts'
export function getEnvCredentials(): { 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) return null
return { authKey, authSecret }
}
export function createReadStream(file: string): Readable {
if (file === '-') return process.stdin
return fs.createReadStream(file)
}
export async function streamToBuffer(stream: Readable): Promise<Buffer> {
const chunks: Buffer[] = []
for await (const chunk of stream) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
}
return Buffer.concat(chunks)
}
export function formatAPIError(err: unknown): string {
if (isAPIError(err)) {
return `${err.error}: ${err.message}`
}
if (err instanceof Error) {
return err.message
}
return String(err)
}
// Re-export APIError type for CLI consumers relying on deep imports.
/** @public */
export type { APIError } from './types.ts'
export function zip<A, B>(listA: A[], listB: B[]): [A, B][]
export function zip<T>(...lists: T[][]): T[][]
export function zip<T>(...lists: T[][]): T[][] {
const length = Math.max(...lists.map((list) => list.length))
const result: T[][] = new Array(length)
for (let i = 0; i < result.length; i++) {
result[i] = lists.map((list) => list[i] as T)
}
return result
}