本文整理汇总了TypeScript中buffer.Buffer.byteLength方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Buffer.byteLength方法的具体用法?TypeScript Buffer.byteLength怎么用?TypeScript Buffer.byteLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类buffer.Buffer
的用法示例。
在下文中一共展示了Buffer.byteLength方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: serveFile
export async function serveFile(devServerConfig: d.DevServerConfig, fs: d.FileSystem, req: d.HttpRequest, res: http.ServerResponse) {
try {
if (isSimpleText(req.filePath)) {
// easy text file, use the internal cache
let content = await fs.readFile(req.filePath);
if (isHtmlFile(req.filePath) && !isDevServerClient(req.pathname)) {
// auto inject our dev server script
content += injectDevServerClient();
} else if (isCssFile(req.filePath)) {
content = updateStyleUrls(req.url, content);
}
const contentLength = Buffer.byteLength(content, 'utf8');
if (shouldCompress(devServerConfig, req, contentLength)) {
// let's gzip this well known web dev text file
res.writeHead(200, responseHeaders({
'Content-Type': getContentType(devServerConfig, req.filePath)
}));
zlib.createGzip().pipe(res);
} else {
// let's not gzip this file
res.writeHead(200, responseHeaders({
'Content-Type': getContentType(devServerConfig, req.filePath),
'Content-Length': contentLength
}));
res.write(content);
res.end();
}
} else {
// non-well-known text file or other file, probably best we use a stream
// but don't bother trying to gzip this file for the dev server
res.writeHead(200, responseHeaders({
'Content-Type': getContentType(devServerConfig, req.filePath),
'Content-Length': req.stats.size
}));
fs.createReadStream(req.filePath).pipe(res);
}
} catch (e) {
serve500(res, e);
}
}
示例2: next
server.use((ctx, next) => {
ctx.res.statusCode = 200
expectedLength = Buffer.byteLength(JSON.stringify({ test: 'obj' }))
ctx.body = { test: 'obj' }
return next()
})