-
Notifications
You must be signed in to change notification settings - Fork 31
/
parse-cli.ts
executable file
·565 lines (551 loc) · 17.2 KB
/
parse-cli.ts
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import Yargs, { Options, Argv } from "yargs";
import yargsToMist from "./yargs-to-mist";
import {
CamelKeys,
Ingest,
NodeAddress,
OrchestratorNodeAddress,
Price,
} from "./types/common";
import { defaultTaskExchange } from "./store/queue";
import { FfmpegProfile } from "./schema/types";
import profileValidator from "./schema/validators/ffmpeg-profile";
import { ValidateFunction, ErrorObject } from "ajv";
const DEFAULT_ARWEAVE_GATEWAY_PREFIXES = [
"https://arweave.net/",
"https://gateway.arweave.net/",
];
const yargs = Yargs() as unknown as Argv;
function coerceArr(arg: any) {
if (!Array.isArray(arg)) {
const arr = [];
for (const [key, value] of Object.entries(arg)) {
arr[key] = value;
}
return arr;
}
return arg;
}
function coerceJsonStrArr(arg: string): string[] {
if (!arg) {
return undefined;
}
const arr = JSON.parse(arg);
const isStrArr =
Array.isArray(arr) && arr.every((str) => typeof str === "string");
if (!isStrArr) {
throw new Error("not a JSON array of strings");
}
return arr;
}
function coerceRegexList(flagName: string) {
return (arg: string): (string | RegExp)[] => {
try {
const arr = coerceJsonStrArr(arg);
if (!arr) {
return undefined;
}
return arr.map((str) => {
if (str.startsWith("/") && str.endsWith("/")) {
return new RegExp(str.slice(1, -1));
}
return str;
});
} catch (err) {
throw new Error(`Error in CLI flag --${flagName}: ${err.message}`);
}
};
}
function coerceJsonValue<T>(flagName: string) {
return (arg: string): T => {
if (!arg) {
return undefined;
}
try {
return JSON.parse(arg);
} catch (err) {
throw new Error(`Error in CLI flag --${flagName}: ${err.message}`);
}
};
}
function coerceJsonProfileArr(flagName: string) {
return (str: string): FfmpegProfile[] => {
let profiles;
const validator = profileValidator as ValidateFunction;
try {
profiles = JSON.parse(str);
} catch (e) {
throw new Error(`--${flagName} JSON parsing error: ${e.message}`);
}
if (!Array.isArray(profiles)) {
throw new Error(`--${flagName} must be a JSON array`);
}
const errors: ErrorObject[] = [];
for (const profile of profiles) {
const valid = validator(profile);
if (!valid) {
errors.push(...validator.errors);
}
}
if (errors.length > 0) {
throw new Error(
`--${flagName} validation error: ${JSON.stringify(errors)}`
);
}
return profiles;
};
}
export type CliArgs = ReturnType<typeof parseCli>;
// Hack alert! We need to capture the args passed to yarns.options to generate the
// mist compatible config on -j. But assigning the `.options()` object to a variable
// before passing it to yargs completely breaks type inference, which is a huge shame.
// So... this monkeypatches yargs to capture that variable. If you know of a more
// elegant way, I'd love to hear it!
let allOptions: { [key: string]: Options };
const originalOpts = yargs.options;
yargs.options = function (...args) {
if (!allOptions) {
allOptions = args[0];
}
return originalOpts.call(this, ...args);
};
export default function parseCli(argv?: string | readonly string[]) {
if (!argv) {
argv = process.argv.slice(2);
}
const parsedProm = yargs
.options({
port: {
describe: "port to listen on",
default: 3004,
demandOption: true,
type: "number",
},
"postgres-url": {
describe: "url of a postgres database",
type: "string",
demandOption: true,
default: "postgresql://postgres@127.0.0.1/livepeer",
},
"postgres-replica-url": {
describe: "url of a postgres read replica database",
type: "string",
},
"default-cache-ttl": {
describe: "default TTL for entries cached in memory, in seconds",
type: "number",
default: 120,
},
"amqp-url": {
describe: "the RabbitMQ Url",
type: "string",
},
"amqp-tasks-exchange": {
describe:
"the name of the exchange for scheduling tasks and receiving results",
type: "string",
default: defaultTaskExchange,
},
"client-id": {
describe: "google auth ID",
type: "string",
},
"frontend-domain": {
describe: "the domain used in templating urls, example: livepeer.org",
type: "string",
default: "livepeer.studio",
},
"kube-namespace": {
describe:
"namespace of the Kubernetes cluster we're in. required for Kubernetes service discovery.",
type: "string",
},
"kube-broadcaster-service": {
describe: "name of the service we should look at for broadcasters.",
type: "string",
},
"kube-broadcaster-template": {
describe:
"template string of the form https://{{nodeName}}.example.com to give broadcasters external identity.",
type: "string",
default: "https://{{nodeName}}.livepeer.live",
},
"kube-orchestrator-service": {
describe: "name of the service we should look at for orchestrators.",
type: "string",
},
"kube-orchestrator-template": {
describe:
"template string of the form {{ip}} for the broadcaster webhook.",
type: "string",
default: "https://{{ip}}:8935",
},
"ipfs-gateway-url": {
describe:
"base URL to use for the IPFS content gateway returned on assets.",
type: "string",
default: "https://ipfs.livepeer.studio/ipfs/",
},
"trusted-ipfs-gateways": {
describe:
"comma-separated list of regexes for trusted IPFS gateways to automatically convert to IPFS URLs",
type: "string",
default: `["https://ipfs.livepeer.studio/ipfs/"]`,
coerce: coerceRegexList("trusted-ipfs-gateways"),
},
"trusted-arweave-gateways": {
describe:
"comma-separated list of regexes for trusted Arweave gateways to automatically convert to Arweave URLs",
type: "string",
default: JSON.stringify(DEFAULT_ARWEAVE_GATEWAY_PREFIXES),
coerce: coerceRegexList("trusted-arweave-gateways"),
},
"return-region-in-orchestrator": {
describe: "return /api/region result also in /api/orchestrator",
type: "boolean",
},
"subgraph-url": {
describe: "URL of subgraph to look for orchestrators",
type: "string",
},
"http-prefix": {
describe: "accept requests at this prefix",
default: "/api",
demandOption: true,
type: "string",
},
"fallback-proxy": {
describe:
"if a request would otherwise be a 404, send it here instead. useful for dev.",
type: "string",
},
"jwt-secret": {
describe:
"phrase used to sign JSON web token, a way to securely transmit information between parties",
type: "string",
},
"jwt-audience": {
describe: "identifies the recipients that the JWT is intended for",
type: "string",
},
"cors-jwt-allowlist": {
describe:
"comma-separated list of domains to allow CORS for requests authenticated with a JWT. " +
"add a / prefix and suffix to an element to have it parsed as a regex",
type: "string",
default: `["https://livepeer.studio"]`,
coerce: coerceRegexList("cors-jwt-allowlist"),
},
broadcasters: {
describe:
"hardcoded list of broadcasters to return from /api/broadcaster.",
type: "string",
default: "[]",
coerce: coerceJsonValue<NodeAddress[]>("broadcasters"),
},
orchestrators: {
describe:
"hardcoded list of orchestrators to return from /api/orchestrator.",
type: "string",
default: "[]",
coerce: coerceJsonValue<OrchestratorNodeAddress[]>("orchestrators"),
},
ingest: {
describe: "hardcoded list of ingest points to return from /api/ingest.",
type: "string",
default: "[]",
coerce: coerceJsonValue<Ingest[]>("ingest"),
},
prices: {
describe:
"hardcoded list of prices for broadcasters to return from /api/orchestrator/hook/auth",
type: "string",
default: "[]",
coerce: coerceJsonValue<Price[]>("prices"),
},
"support-addr": {
describe:
"email address where outgoing emails originate. should be of the form name/email@example.com",
type: "string",
default: undefined,
coerce: (supportAddr: string) => {
if (!supportAddr) {
return undefined;
}
const split = supportAddr.split("/");
if (split.length !== 2) {
throw new Error(
`supportAddr should be of the form name / email, got ${supportAddr} `
);
}
return split as [string, string];
},
},
"sendgrid-api-key": {
describe: "sendgrid api key for sending emails",
type: "string",
},
"sendgrid-validation-api-key": {
describe: "sendgrid api key for validating email addresses",
type: "string",
},
"sendgrid-template-id": {
describe: "sendgrid template id to use",
type: "string",
},
"insecure-test-token": {
describe:
"[DO NOT USE EXCEPT FOR TESTING] token that test harness can use to bypass validation and access the database",
type: "string",
},
region: {
describe:
"list of ingest endpoints to use as options for /api/geolocate",
type: "array",
default: [],
coerce: coerceArr,
},
vodObjectStoreId: {
describe: "object store ID to use for VOD",
type: "string",
},
vodCatalystObjectStoreId: {
describe: "object store ID to use for Catalyst VOD",
type: "string",
},
secondaryVodObjectStoreId: {
describe: "secondary object store ID for Catalyst VOD",
type: "string",
},
vodCatalystPrivateAssetsObjectStoreId: {
describe: "object store ID to use for private assets in Catalyst VOD",
type: "string",
},
secondaryVodPrivateAssetsObjectStoreId: {
describe:
"secondary object store ID to use for private assets in Catalyst VOD",
type: "string",
},
recordCatalystObjectStoreId: {
describe: "object store ID used by Catalyst to store recordings",
type: "string",
},
secondaryRecordObjectStoreId: {
describe:
"secondary object store ID used by Catalyst to store recordings",
type: "string",
},
catalystBaseUrl: {
describe: "base URL of Catalyst",
type: "string",
default: "http://127.0.0.1:7979",
},
googleCloudUrlSigningKeyName: {
describe:
"name of the signing key to use for signing access cookies for private assets on Google Cloud CDN",
type: "string",
},
googleCloudUrlSigningKey: {
describe:
"value of the signing key to use for signing access cookies for private assets on Google Cloud CDN",
type: "string",
},
vodMaxConcurrentTasksPerUser: {
describe:
"maximum number of tasks that can be running for a given user",
default: 5,
type: "number",
},
vodMaxScheduledTasksPerUser: {
describe:
"maximum number of tasks that can be in the VOD execution queue for a given user",
default: 100,
type: "number",
},
"ingest-region": {
describe:
"list of ingest endpoints to use as servers to consult for /api/ingest",
type: "array",
default: [],
coerce: coerceArr,
},
"api-region": {
describe:
"list of api endpoints to forward on incoming API requests. defining this delegates all non-geolocation tasks to the upstream servers",
type: "array",
default: [],
coerce: coerceArr,
},
"record-object-store-id": {
describe:
"id of the object store that should be used for `record: true` requests that don't otherwise have an os",
type: "string",
},
"supress-record-in-hook": {
describe:
"do not return record object store in /stream/hook response, even if it is specified in the stream object",
type: "boolean",
},
"base-stream-name": {
describe:
"base stream name to be used in wildcard-based routing scheme.",
type: "string",
},
"own-region": {
describe: "identify region in which this server runs (fra, mdw, etc)",
type: "string",
},
consul: {
describe: "url of the Consul agent",
type: "string",
},
"stripe-secret-key": {
describe: "Stripe secret key",
type: "string",
},
"stripe-webhook-secret": {
describe: "Stripe webhook secret",
type: "string",
},
"access-control-admin-pubkey": {
describe: "Access Control Admin signing public key",
type: "string",
},
"access-control-admin-privkey": {
describe: "Access Control Admin signing private key",
type: "string",
},
"verification-frequency": {
describe: "verificationFreq field to return from stream/hook",
default: 0,
type: "number",
},
"recaptcha-secret-key": {
describe: "google recaptcha secret key",
type: "string",
},
"require-email-verification": {
describe: "require Email Verification",
default: false,
type: "boolean",
},
"half-region-orchestrators-untrusted": {
describe:
"mark half of the orchestrators returned by /api/region as untrusted. For use in staging!",
default: false,
type: "boolean",
},
json: {
describe: "print MistController-compatible json description",
default: false,
type: "boolean",
alias: "j",
},
did: {
describe: "Livepeer DID key",
type: "string",
},
livekitHost: {
describe: "Endpoint for LiveKit server",
type: "string",
default: "",
},
livekitApiKey: {
describe: "API key for LiveKit access",
type: "string",
},
livekitSecret: {
describe: "Secret for LiveKit access",
type: "string",
},
livekitMeetUrl: {
describe: "Livekit Meet Webapp URL",
type: "string",
default: "https://meet.livekit.io/custom",
},
saltForRequesterId: {
describe: "Salt for generating requesterId",
type: "string",
default: "",
},
frontend: {
describe: "serve the embedded @livepeer/www Next.js frontend",
type: "boolean",
default: true,
},
"stream-info-service": {
describe: "start the Stream Info service instead of Studio API",
type: "boolean",
},
broadcaster: {
describe:
"stream-info-service: broadcaster host:port to fetch info from",
type: "string",
default: "127.0.0.1:7935",
},
"default-stream-profiles": {
describe: "default stream transcoding profiles if none are provided",
type: "string",
default: JSON.stringify([
{
name: "240p0",
fps: 0,
bitrate: 250000,
width: 426,
height: 240,
profile: "H264ConstrainedHigh",
},
{
name: "360p0",
fps: 0,
bitrate: 800000,
width: 640,
height: 360,
profile: "H264ConstrainedHigh",
},
{
name: "480p0",
fps: 0,
bitrate: 1600000,
width: 854,
height: 480,
profile: "H264ConstrainedHigh",
},
{
name: "720p0",
fps: 0,
bitrate: 3000000,
width: 1280,
height: 720,
profile: "H264ConstrainedHigh",
},
] as FfmpegProfile[]),
coerce: coerceJsonProfileArr("default-stream-profiles"),
},
})
.usage(
`
Livepeer Studio API Node
Options my also be provided as LP_API_ prefixed environment variables, e.g. LP_API_PORT=5000 is the same as --port=5000.
--broadcaster and --orchestrator options should be of the form
[{"address":"https://127.0.0.1:3086","cliAddress":"http://127.0.0.1:3076"}]
`
)
.strict(
process.env.NODE_ENV !== "test" && process.env.NODE_ENV !== "development"
)
.env("LP_API_")
.strict(
process.env.NODE_ENV !== "test" && process.env.NODE_ENV !== "development"
)
.help()
.parse(argv);
// yargs returns a Promise even tho we don't have any async middlewares
const parsed = parsedProm as Awaited<typeof parsedProm>;
const mistOutput = yargsToMist(allOptions);
if (parsed.json === true) {
console.log(JSON.stringify(mistOutput));
process.exit(0);
}
return parsed as any as CamelKeys<typeof parsed>;
}