UNPKG

1.5 kBJavaScriptView Raw
1const { get } = require('https')
2const zipObject = require('lodash.zipobject')
3
4module.exports = async (data) => {
5 const p = []
6 const videoKeys = Object.keys(data.videos)
7 videoKeys.forEach((videoKey) => {
8 const fetcher = new Promise((resolve, reject) => {
9 if (/_/.test(videoKey)) return reject(new Error(`Video key ${videoKey} must not contain underscores`))
10 get(data.videos[videoKey], (res) => {
11 if (res.statusCode === 200) {
12 res.setEncoding('utf8')
13 let body = ''
14 res.on('data', chunk => { body += chunk })
15 res.on('end', () => {
16 const { headers } = res
17 if (headers['content-type'] === 'application/json') {
18 resolve({ body, headers })
19 } else {
20 reject(new Error(`Invalid JSON URI, you defined videos.${videoKey} to be '${data.videos[videoKey]}'. Check if this is a valid outputs.json`))
21 }
22 })
23 } else {
24 reject(new Error(`Invalid JSON URI, you defined videos.${videoKey} to be '${data.videos[videoKey]}'. Check if this is a valid outputs.json`))
25 }
26 }).on('error', (err) => reject(err))
27 })
28 p.push(fetcher)
29 })
30 const results = await Promise.all(p)
31 const values = results.map((res, i) => {
32 const parsed = JSON.parse(res.body)
33 parsed.originalKey = res.headers['x-amz-meta-original-key']
34 parsed.outputs = videoKeys[i]
35 return parsed
36 })
37 data.videos = zipObject(videoKeys, values)
38 return data
39}