typescript - 使用 HTTP Content-Type 标头确定二进制数据与文本
问题描述
我正在编写代码以在 Node.js 环境中通过 HTTP/HTTPS 读取任意 Web 资源,当它是文本数据时,我想将内容作为字符串返回,当它是二进制数据时,我想作为缓冲区返回。
很明显,以 开头的任何内容text
,例如text/html
,都是最好以字符串形式返回的文本数据——当然,使用适当的字符编码,可以明确定义(例如text/html; charset=utf-8
),也可以不明确定义。此外,任何明确的定义都charset
将表明内容是文本,而不是二进制,无论 MIME 类型如何。
据我所知,几乎所有其他内容都是二进制数据。我知道的所有音频和视频格式都是二进制的,几乎所有的图像类型,除了image/svg+xml
. 似乎大多数application/...
类型都是二进制的,但也有一些值得注意的常见例外,例如application/json
.
以下功能似乎足以确定问题吗?如果没有,我可能会遗漏哪些值得注意的例外情况?
function isBinary(contentType: string): boolean {
let $: string[];
if (/;\s*charset\s*=/i.test(contentType))
return false;
// Remove anything other than MIME type.
contentType = contentType.replace(/;.*$/, '').trim();
if (/^text\//i.test(contentType) || /\+xml$/i.test(contentType))
return false;
else if (($ = /^application\/(.+)/i.exec(contentType)))
return !/^(javascript|ecmascript|json|ld\+json|rtf)$/i.test($[1]);
else
return true;
}
解决方案
好吧,您可以在从 URL 获取的缓冲区上使用 istextorbinary。
例如,在 lambda 中,您将:
const fetch = require('node-fetch');
const { isText, isBinary, getEncoding } = require('istextorbinary');
module.exports.handler = async (event, context, callback) => {
.
.
const customUrl = 'www.abc.net.au';
const url = `https://${customUrl}${event.path}`;
// Request with GET/HEAD method cannot have body | undefined
// Set fetch parameters in params
var params = {};
if (event.httpMethod === 'GET' || event.httpMethod === 'HEAD') {
params = {
method: event.httpMethod,
headers: customRequestHeader
};
} else {
params = {
method: event.httpMethod,
headers: customRequestHeader,
body: JSON.stringify(parsedbody)
};
}
console.debug('request params: ' + JSON.stringify(params));
// Fetch URL with params
const response = await fetch(url, params);
var textResponse = await response.buffer();
var isBase64EncodedValue = false;
if ( isBinary(null, textResponse) ) {
console.log('textResponse is a binary blob - setting isBase64Encoded to true on returnResponse');
isBase64EncodedValue = true;
console.log('isBase64EncodedValue in returnResponse is: ' + isBase64EncodedValue);
// We need to return binary base64 encoded data for binary files - otherwise APIGW will throw 500 error back to the user
textResponse = textResponse.toString('base64');
// If you need to convert it back you could do something like - textResponse.toString('binary')
console.log('When isBase64EncodedValue is true, textResponse is: ' + textResponse);
} else {
console.log('textResponse is not a binary blob - setting isBase64Encoded to false on returnResponse');
isBase64EncodedValue = false;
console.log('isBase64EncodedValue in returnResponse is: ' + isBase64EncodedValue);
textResponse = textResponse.toString('utf8');
console.log('When isBase64EncodedValue is false, textResponse is: ' + textResponse);
}
.
.
};
你最终使用了你的功能吗?想分享你的最终代码吗?
推荐阅读
- c++ - IMFTransform::ProcessOutput 效率
- docker - Docker Desktop 版本 2.5.0.0 中断了已安装文件夹内卷的安装
- scala - 如何将表情符号替换为scala数据框中的空字符串?
- maven - 在 CI 中使用本地 Maven 存储库
- c# - 带有 MySql 的 EntityFrameworkCore 未创建所有表
- android - 如何从 Huawei AppGallery Connect 下载我的应用分析?
- javascript - 如何将一列添加到 1d json 数组并将其保存到文件中?
- amazon-sqs - 同一个 DLQ 可以同时用于 SNS 故障和 SQS 故障吗?
- security - 方法定义中的默认定义者权限
- installation - 如何在 InstallBuilder 中查找和检查多个文件?