-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrender.mjs
More file actions
126 lines (102 loc) · 3.17 KB
/
Copy pathrender.mjs
File metadata and controls
126 lines (102 loc) · 3.17 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
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
import { readFile } from 'node:fs/promises';
import { setTimeout as delay } from 'node:timers/promises';
// The sandbox environment. The API names it "stage".
const API_BASE_URL = 'https://api.shotstack.io/edit/stage';
const POLL_INTERVAL_MS = 5_000;
const MAX_WAIT_MS = 10 * 60 * 1_000;
const apiKey = process.env.SHOTSTACK_API_KEY;
if (!apiKey) {
console.error('Set the SHOTSTACK_API_KEY environment variable first.');
process.exit(1);
}
async function shotstackRequest(path, options = {}) {
let response;
try {
response = await fetch(`${API_BASE_URL}${path}`, {
...options,
signal: AbortSignal.timeout(30_000),
headers: {
Accept: 'application/json',
'x-api-key': apiKey,
...options.headers
}
});
} catch (error) {
throw new Error(
'Could not reach the Shotstack API. ' +
'Check your network connection and try again.',
{ cause: error }
);
}
const responseText = await response.text();
let body = null;
try {
body = JSON.parse(responseText);
} catch {
// Handled below: an error response falls back to the raw body text, and
// a success response that is not JSON gets its own message.
}
if (!response.ok) {
const detail =
body?.errors?.[0]?.detail || (body ? JSON.stringify(body) : responseText);
throw new Error(`Shotstack returned ${response.status}: ${detail}`);
}
if (!body || typeof body !== 'object') {
throw new Error(
`Shotstack returned a non-JSON response with status ${response.status}.`
);
}
return body;
}
async function submitRender(edit) {
const result = await shotstackRequest('/render', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(edit)
});
const renderId = result.response?.id;
if (!renderId) {
throw new Error(
`The response did not contain a render ID: ${JSON.stringify(result)}`
);
}
return renderId;
}
async function waitForRender(renderId) {
const startedAt = Date.now();
while (Date.now() - startedAt < MAX_WAIT_MS) {
const result = await shotstackRequest(`/render/${renderId}`);
const render = result.response || {};
if (!render.status) {
throw new Error(`Unexpected status response: ${JSON.stringify(result)}`);
}
console.log(`Render status: ${render.status}`);
if (render.status === 'done') {
if (!render.url) {
throw new Error('The render finished without an output URL.');
}
return render;
}
if (render.status === 'failed') {
throw new Error(
render.error || 'The render failed without an error message.'
);
}
await delay(POLL_INTERVAL_MS);
}
throw new Error(
`Render ${renderId} did not finish within ${MAX_WAIT_MS / 60_000} minutes.`
);
}
try {
const edit = JSON.parse(
await readFile(new URL('./edit.json', import.meta.url), 'utf8')
);
const renderId = await submitRender(edit);
console.log(`Queued render: ${renderId}`);
const render = await waitForRender(renderId);
console.log(`Temporary output URL: ${render.url}`);
} catch (error) {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
}