-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.js
62 lines (53 loc) · 2.06 KB
/
middlewares.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
51
52
53
54
55
56
57
58
59
60
61
62
const EntitlementService = require('./services/entitlement.service.js');
const CatalogService = require('./services/catalog.service.js');
const { createProxyMiddleware, responseInterceptor } = require('http-proxy-middleware');
const config = require('./utils/config');
const ProxyHelpers = {
onProxyRes: (proxyRes, req, res) => {
if (proxyRes.statusCode != 200) {
console.log(proxyRes.headers);
res.sendStatus(500);
} else {
console.log(proxyRes.headers['x-axdrm-message']);
//The License Request Info Message is only for consumption by the proxy. This should not be returned to the end-user.
delete proxyRes.headers['x-axdrm-message'];
}
}, onError: (err, req, res, target) => {
console.log(err);
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end();
}
};
const Middlewares = {
fairplayProxy: createProxyMiddleware({
target: config.FAIRPLAY_LS_URL,
secure: false, ignorePath: true, changeOrigin: true, ...ProxyHelpers
}),
playreadyProxy: createProxyMiddleware({
target: config.PLAYREADY_LS_URL,
secure: false, ignorePath: true, changeOrigin: true, ...ProxyHelpers
}),
widevineProxy: createProxyMiddleware({
target: config.WIDEVINE_LS_URL,
secure: false, ignorePath: true, changeOrigin: true, ...ProxyHelpers
}),
setTokenHeader: async (req, res, next) => {
const videoId = req.params.id;
console.log(videoId);
const targetVideo = await CatalogService.getVideoById(videoId);
if (targetVideo) {
// The video with the specified id was found
console.log('Found video:', targetVideo);
} else {
// No video with the specified id was found
console.log('Video not found');
res.sendStatus(500);
}
const token = await EntitlementService.getToken(targetVideo);
req.headers['X-AxDRM-Message'] = token;
next();
}
};
module.exports = Middlewares;