本文整理汇总了TypeScript中zlib.gzip函数的典型用法代码示例。如果您正苦于以下问题:TypeScript gzip函数的具体用法?TypeScript gzip怎么用?TypeScript gzip使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了gzip函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: source
/**
* Retrieve, parse and display a map source.
*/
async function source(req: Request, res: Response) {
const key: string = req.params[RouteParam.MapSource];
if (!is.text(key)) {
return view.notFound(req, res);
}
const geo = await loadSource(key.replace('.json', ''));
if (!is.value<MapSource>(geo)) {
return view.notFound(req, res);
}
const geoText = JSON.stringify(geo);
try {
compress.gzip(Buffer.from(geoText), (err: Error, buffer: Buffer) => {
if (is.value(err)) {
view.internalError(res, err);
} else {
res.setHeader(Header.Content.Encoding, Encoding.GZip);
res.setHeader(Header.CacheControl, 'max-age=86400, public'); // seconds
res.setHeader(Header.Content.Type, addCharSet(MimeType.JSON));
res.setHeader(
Header.Content.Disposition,
`attachment; filename=${key}`
);
res.write(buffer);
res.end();
}
});
} catch (err) {
view.internalError(res, err);
}
}
示例2: reject
new Promise<ViewItem>((resolve, reject) => {
let text: string;
let inferredType: MimeType;
if (is.text(htmlOrJSON)) {
text = htmlOrJSON;
inferredType = MimeType.HTML;
} else {
text = JSON.stringify(htmlOrJSON);
inferredType = MimeType.JSON;
}
if (type === undefined) {
type = inferredType;
}
compress.gzip(Buffer.from(text), (err: Error, buffer: Buffer) => {
if (is.value(err)) {
reject(err);
log.error(err, { slug: key });
} else {
resolve({
buffer,
eTag: key + '_' + new Date().getTime().toString(),
type
});
}
});
});
示例3: write
/**
* Write contents into a compressed file.
*
* @async
* @params {String} filename
* @params {String} result
* @params {Function} callback
*/
function write(filename: string, result: any, callback) {
let content = JSON.stringify(result);
return zlib.gzip(content as any, function(err, data) {
if (err) { return callback(err); }
return fs.writeFile(filename, data, callback);
});
};
示例4: resGzip
function resGzip(res: any, data: any) {
res.writeHead(200, { 'Content-Type': 'application/json', 'charset': 'utf-8', 'Content-Encoding': 'gzip' });
gzip(JSON.stringify(data), (err: any, result: any) => {
if (err) {
console.log(err);
}
res.end(result);
})
}
示例5: write
export function write(string, type, res) {
zlib.gzip(string, (err, result) => {
res.writeHead(200, {
'Content-Length': result.length,
'Content-Type': type,
'Content-Encoding': 'gzip'
})
res.write(result)
res.end()
});
}
示例6:
s.on('/', (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Encoding', 'gzip');
if (req.method === 'HEAD') {
res.end();
return;
}
zlib.gzip(testContent, (_, data) => res.end(data));
});
示例7: promiseTo
const gzippedJson = await promiseTo(cb => zlib.gzip(json as any as Buffer, cb));
示例8: deflateRaw
result, { finishFlush: constants.Z_SYNC_FLUSH },
(err: Error | null, result: Buffer) => result
)
);
deflateRaw(compressMeString, (err: Error | null, result: Buffer) => inflateRaw(result, (err: Error | null, result: Buffer) => result));
deflateRaw(
compressMeString,
{ finishFlush: constants.Z_SYNC_FLUSH },
(err: Error | null, result: Buffer) => inflateRaw(result, { finishFlush: constants.Z_SYNC_FLUSH }, (err: Error | null, result: Buffer) => result),
);
const inflatedRaw: Buffer = inflateRawSync(deflateRawSync(compressMe));
const inflatedRawString: Buffer = inflateRawSync(deflateRawSync(compressMeString));
// gzip
gzip(compressMe, (err: Error | null, result: Buffer) => gunzip(result, (err: Error | null, result: Buffer) => result));
gzip(
compressMe,
{ finishFlush: constants.Z_SYNC_FLUSH },
(err: Error | null, result: Buffer) => gunzip(
result, { finishFlush: constants.Z_SYNC_FLUSH },
(err: Error | null, result: Buffer) => result
)
);
const gunzipped: Buffer = gunzipSync(gzipSync(compressMe));
unzip(compressMe, (err: Error | null, result: Buffer) => result);
unzip(compressMe, { finishFlush: constants.Z_SYNC_FLUSH }, (err: Error | null, result: Buffer) => result);
const unzipped: Buffer = unzipSync(compressMe);
const bOpts: BrotliOptions = {
示例9: Promise
const zipped = await new Promise(resolve => zlib.gzip(moduleBuffer, (_, result) => resolve(result)));