本文整理匯總了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)));