Mountebank 管道视频文件流响应不起作用
Mountebank piping video file stream to response not working
我正在尝试让 Mountebank return 从我的 .ts
MPEG-2 文件到 Mountebank 注入的读入文件流管道,以便客户端可以接收流数据并播放视频.有一个关于如何读入文件的 useful Node implementation,我把它读入了 .m3u8
文件,但现在它似乎卡在了第一个 .ts
文件中。
Node 似乎正在将 .ts
文件作为流读取并将其通过管道传输到响应 stream.pipe(res)
。我不确定这如何转化为 Mountebank。这是我的 Mountbank 代码:
装载银行:
function (request, state, logger, callback) {
const path = require('path');
const fs = require('fs');
function _handleError(errorMessage, statusCode) {
logger.error(`[video-qoe-injection:responses]`, errorMessage);
return {
statusCode,
body: {}
}
}
function _sendFile(filepath) {
const data = fs.readFileSync(filepath, 'utf-8');
return {
statusCode: 200,
headers: {'Content-Type': 'application/vnd.apple.mpegurl'},
body: data
}
}
if (request && request.method === 'GET') {
if (path.extname(uri) === '.m3u8') {
filepath = path.join(M3U8_FILE_PATH);
return _sendFile(filepath);
}
} else if (path.extname(uri) === '.ts') {
filepath = path.join(TS_FILE_PATH)
let body = '';
const stream = fs.createReadStream(filepath, { bufferSize: 64 * 1024 });
stream.on('data', (data) => {
body += data.toString();
});
stream.on('end', () => {
const stubResponse = {
statusCode: 200,
headers: {'Content-Type': 'video/MP2T'},
body
}
callback(stubResponse);
});
}
}
我用纯 Node 实现尝试了这个并且它有效,但是,当我将它移植到 Mountebank 时,我收到控制台日志错误并显示以下错误:
hls.0.12.4.min.js:1 Uncaught (in promise) DOMException: The play() request was interrupted by a new load request.
player.js:57 Player error: mediaError - fragParsingError
不确定我在这里遗漏了什么,Mountebank 不支持流响应吗?我什至尝试用 const data = fs.readFileSync(filepath)
阅读 .ts
文件,但 hls.js 似乎不喜欢那样,并得到了承诺拒绝。
我认为您可能需要使用更粗糙的东西来模拟这个 - 来自 the documentation:
HTTP bodies will always be recorded as text, but mountebank does have
the ability to respond in binary. If you want to set up a canned
binary response, set the _mode to binary and base64 encode the body.
mountebank will also try to preserve binary responses in proxies by
looking at the Content-Encoding and Content-Type headers.
我过去用过这个,但只在非 JS 注入存根中使用了一个简单的 PDF 文件:
"responses": [
{
"is": {
"_mode": "binary",
"statusCode": 200,
"headers": {
"Content-Type": "application/pdf",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE"
},
"body": "U29tZVJhbmRvbUJhc2U2NA...<etc>"
}
}
]
我建议尝试几件事:
- 尝试将“_mode”属性 添加到 JS 文件中的 stubResponse 对象中,并在输出时将流式响应转换为 Base 64。
- 或者,尝试使用 Base 64 对您的视频文件进行编码,并提供编码后的文本作为标准存根中的正文。您可能还会发现将 Base 64 文件添加为 EJS 参考并不那么麻烦:
"responses": [
{
"is": {
"_mode": "binary",
"statusCode": 200,
"headers": {
"Content-Type": "video/MP2T",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET"
},
"body": "<% include ./YourBase64EncodedFilePathHere.txt %>"
}
}
]
我正在尝试让 Mountebank return 从我的 .ts
MPEG-2 文件到 Mountebank 注入的读入文件流管道,以便客户端可以接收流数据并播放视频.有一个关于如何读入文件的 useful Node implementation,我把它读入了 .m3u8
文件,但现在它似乎卡在了第一个 .ts
文件中。
Node 似乎正在将 .ts
文件作为流读取并将其通过管道传输到响应 stream.pipe(res)
。我不确定这如何转化为 Mountebank。这是我的 Mountbank 代码:
装载银行:
function (request, state, logger, callback) {
const path = require('path');
const fs = require('fs');
function _handleError(errorMessage, statusCode) {
logger.error(`[video-qoe-injection:responses]`, errorMessage);
return {
statusCode,
body: {}
}
}
function _sendFile(filepath) {
const data = fs.readFileSync(filepath, 'utf-8');
return {
statusCode: 200,
headers: {'Content-Type': 'application/vnd.apple.mpegurl'},
body: data
}
}
if (request && request.method === 'GET') {
if (path.extname(uri) === '.m3u8') {
filepath = path.join(M3U8_FILE_PATH);
return _sendFile(filepath);
}
} else if (path.extname(uri) === '.ts') {
filepath = path.join(TS_FILE_PATH)
let body = '';
const stream = fs.createReadStream(filepath, { bufferSize: 64 * 1024 });
stream.on('data', (data) => {
body += data.toString();
});
stream.on('end', () => {
const stubResponse = {
statusCode: 200,
headers: {'Content-Type': 'video/MP2T'},
body
}
callback(stubResponse);
});
}
}
我用纯 Node 实现尝试了这个并且它有效,但是,当我将它移植到 Mountebank 时,我收到控制台日志错误并显示以下错误:
hls.0.12.4.min.js:1 Uncaught (in promise) DOMException: The play() request was interrupted by a new load request.
player.js:57 Player error: mediaError - fragParsingError
不确定我在这里遗漏了什么,Mountebank 不支持流响应吗?我什至尝试用 const data = fs.readFileSync(filepath)
阅读 .ts
文件,但 hls.js 似乎不喜欢那样,并得到了承诺拒绝。
我认为您可能需要使用更粗糙的东西来模拟这个 - 来自 the documentation:
HTTP bodies will always be recorded as text, but mountebank does have the ability to respond in binary. If you want to set up a canned binary response, set the _mode to binary and base64 encode the body. mountebank will also try to preserve binary responses in proxies by looking at the Content-Encoding and Content-Type headers.
我过去用过这个,但只在非 JS 注入存根中使用了一个简单的 PDF 文件:
"responses": [
{
"is": {
"_mode": "binary",
"statusCode": 200,
"headers": {
"Content-Type": "application/pdf",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE"
},
"body": "U29tZVJhbmRvbUJhc2U2NA...<etc>"
}
}
]
我建议尝试几件事:
- 尝试将“_mode”属性 添加到 JS 文件中的 stubResponse 对象中,并在输出时将流式响应转换为 Base 64。
- 或者,尝试使用 Base 64 对您的视频文件进行编码,并提供编码后的文本作为标准存根中的正文。您可能还会发现将 Base 64 文件添加为 EJS 参考并不那么麻烦:
"responses": [
{
"is": {
"_mode": "binary",
"statusCode": 200,
"headers": {
"Content-Type": "video/MP2T",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET"
},
"body": "<% include ./YourBase64EncodedFilePathHere.txt %>"
}
}
]