-
Notifications
You must be signed in to change notification settings - Fork 64
/
lambda.js
50 lines (43 loc) · 1.42 KB
/
lambda.js
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
const Mux = require('@mux/mux-node');
const { Video } = new Mux(process.env.MUX_ACCESS_TOKEN, process.env.MUX_SECRET);
const { JWT } = Mux;
exports.handler = async (event) => {
// only allow POST requests
if (event.httpMethod !== 'POST') {
return { statusCode: 405 };
}
const playbackId = event.body.playbackId;
const playbackType = event.body.type || 'video';
// query params to be used for playback
const params = event.body.params || {};
// A playback ID is required to generate a signed url
if (!playbackId) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: '`playbackId` is required to generate signed url' }),
};
}
const data = await Video.SigningKeys.create();
const token = JWT.sign(playbackId, {
keyId: data.id,
keySecret: data.private_key,
type: playbackType,
// expiration expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms).
expiration: '7d',
params,
});
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
playbackId,
token,
signedUrl: `https://stream.mux.com/${playbackId}.m3u8?token=${token}`,
}),
};
};