chushihua

This commit is contained in:
li
2026-01-26 23:20:48 +08:00
commit 7b5aab0206
22521 changed files with 3300090 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
'use strict';
const fs = require('fs');
const stream = require('stream');
const zlib = require('zlib');
const duplexer = require('duplexer');
const pify = require('pify');
const getOptions = options => Object.assign({level: 9}, options);
module.exports = (input, options) => {
if (!input) {
return Promise.resolve(0);
}
// TODO: Remove below comment when new XO release is out
// eslint-disable-next-line no-unused-vars, unicorn/catch-error-name
return pify(zlib.gzip)(input, getOptions(options)).then(data => data.length).catch(_ => 0);
};
module.exports.sync = (input, options) => zlib.gzipSync(input, getOptions(options)).length;
module.exports.stream = options => {
const input = new stream.PassThrough();
const output = new stream.PassThrough();
const wrapper = duplexer(input, output);
let gzipSize = 0;
const gzip = zlib.createGzip(getOptions(options))
.on('data', buf => {
gzipSize += buf.length;
})
.on('error', () => {
wrapper.gzipSize = 0;
})
.on('end', () => {
wrapper.gzipSize = gzipSize;
wrapper.emit('gzip-size', gzipSize);
output.end();
});
input.pipe(gzip);
input.pipe(output, {end: false});
return wrapper;
};
module.exports.file = (path, options) => {
return new Promise((resolve, reject) => {
const stream = fs.createReadStream(path);
stream.on('error', reject);
const gzipStream = stream.pipe(module.exports.stream(options));
gzipStream.on('error', reject);
gzipStream.on('gzip-size', resolve);
});
};
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+40
View File
@@ -0,0 +1,40 @@
{
"name": "gzip-size",
"version": "4.1.0",
"description": "Get the gzipped size of a string or buffer",
"license": "MIT",
"repository": "sindresorhus/gzip-size",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"app",
"tool",
"zlib",
"gzip",
"compressed",
"size",
"string",
"buffer"
],
"dependencies": {
"duplexer": "^0.1.1",
"pify": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"p-event": "^1.3.0",
"xo": "*"
}
}
+67
View File
@@ -0,0 +1,67 @@
# gzip-size [![Build Status](https://travis-ci.org/sindresorhus/gzip-size.svg?branch=master)](https://travis-ci.org/sindresorhus/gzip-size)
> Get the gzipped size of a string or buffer
## Install
```
$ npm install gzip-size
```
## Usage
```js
const gzipSize = require('gzip-size');
const text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';
console.log(text.length);
//=> 191
console.log(gzipSize.sync(text));
//=> 78
```
## API
### gzipSize(input, [options])
Returns a `Promise` for the size.
### gzipSize.sync(input, [options])
Returns the size.
#### input
Type: `string` `Buffer`
#### options
Type: `Object`
Any [`zlib` option](https://nodejs.org/api/zlib.html#zlib_class_options).
### gzipSize.stream([options])
Returns a [`stream.PassThrough`](https://nodejs.org/api/stream.html#stream_class_stream_passthrough). The stream emits a `gzip-size` event and has a `gzipSize` property.
### gzipSize.file(path, [options])
Returns a `Promise` for the size of the file.
#### path
Type: `string`
## Related
- [gzip-size-cli](https://github.com/sindresorhus/gzip-size-cli) - CLI for this module
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)