Comparing version 0.4.1 to 1.0.0
183
index.js
@@ -1,124 +0,19 @@ | ||
const url = require('url') | ||
const got = require('got') | ||
const gzipSize = require('gzip-size') | ||
const prettyBytes = require('pretty-bytes') | ||
const color = require('./lib/color') | ||
const compression = require('./lib/compression') | ||
const constraints = require('./lib/constraints') | ||
const fetch = require('./lib/fetch') | ||
const init = require('./lib/init') | ||
const pretty = require('./lib/pretty') | ||
const send = require('./lib/send') | ||
/** URLs of services in use. */ | ||
const GITHUB_URL = 'https://raw.githubusercontent.com' | ||
const SHIELDS_URL = 'https://img.shields.io/badge' | ||
const bind = (fn, ...args) => fn.bind(fn, ...args) | ||
/** | ||
* Parse infos from request parameters and build a baton. | ||
* | ||
* @param {ServerRequest} req | ||
* @return {Promise} | ||
*/ | ||
function parse(req) { | ||
return new Promise((resolve, reject) => { | ||
const { pathname, query } = url.parse(req.url, true) | ||
let baton = { | ||
label: query.label || ((query.compression ? 'gzip ' : '') + 'size'), | ||
color: query.color || 'brightgreen', | ||
style: query.style || null, | ||
value: 'unknown', | ||
extension: 'svg', | ||
size: 0, | ||
compression: query.compression, | ||
compressedSize: 0, | ||
err: null | ||
} | ||
// empty path | ||
if ('/' === pathname) { | ||
baton.err = new Error('Empty path') | ||
return reject(baton) | ||
} | ||
// url or github path | ||
if (pathname.startsWith('/http')) { | ||
baton.url = pathname.substr(1) | ||
} | ||
else { | ||
baton.url = `${GITHUB_URL}${pathname}` | ||
} | ||
// image extension | ||
let index = pathname.lastIndexOf('.') | ||
if (-1 !== index) { | ||
baton.extension = pathname.substr(index + 1) | ||
if (-1 === 'svg|png|jpg'.indexOf(baton.extension)) { | ||
baton.extension = 'svg' | ||
} | ||
else { | ||
baton.url = `${GITHUB_URL}${pathname.substr(0, index)}` | ||
} | ||
} | ||
resolve(baton) | ||
}) | ||
const cond = (fn, param) => async (baton) => { | ||
if (baton[param] != null) { | ||
await fn(baton) | ||
} | ||
} | ||
/** | ||
* Fetch file to stat from Github. | ||
* | ||
* @param {object} baton | ||
* @return {Promise} | ||
*/ | ||
function fetch(baton) { | ||
return new Promise((resolve, reject) => { | ||
got[baton.compression ? 'get' : 'head'](baton.url, { | ||
headers: { | ||
'accept-encoding': 'identity' | ||
} | ||
}).then(res => { | ||
baton.size = Number(res.headers['content-length']) | ||
baton.data = res.body | ||
resolve(baton) | ||
}).catch(err => { | ||
baton.err = 'Unknown path' | ||
return reject(baton) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Stat compressed size of the file if requested. | ||
* | ||
* @param {object} baton | ||
* @return {object|Promise} | ||
*/ | ||
function compressed(baton) { | ||
if (!baton.compression) return baton | ||
return new Promise((resolve, reject) => { | ||
baton.compressedSize = baton.size | ||
if ('gzip' === baton.compression) { | ||
gzipSize(baton.data, (err, size) => { | ||
/* istanbul ignore if */ | ||
if (err) { | ||
baton.err = err | ||
return reject(baton) | ||
} | ||
baton.compressedSize = size | ||
resolve(baton) | ||
}) | ||
} | ||
else { | ||
baton.err = 'Unknown compression' | ||
reject(baton) | ||
} | ||
}) | ||
} | ||
/** | ||
* Make file size pretty to read. | ||
* | ||
* @param {object} baton | ||
* @return {object} | ||
*/ | ||
function pretty(baton) { | ||
baton.value = prettyBytes(baton.compressedSize || baton.size) | ||
const tap = (fn) => async (baton) => { | ||
await fn(baton) | ||
return baton | ||
@@ -128,38 +23,2 @@ } | ||
/** | ||
* Redirect to shields.io to serve the badge image. | ||
* | ||
* @param {ServerResponse} res | ||
* @return {function} | ||
*/ | ||
function redirect(res) { | ||
return function(baton) { | ||
if (baton.err) { | ||
baton.value = ('string' === typeof baton.err ? | ||
baton.err : | ||
baton.err.message | ||
).toLowerCase() | ||
baton.color = 'lightgrey' | ||
} | ||
let pathname = encodeURI( | ||
`/${baton.label}-${baton.value}-${baton.color}.${baton.extension}` | ||
) | ||
let badgeUrl = `${SHIELDS_URL}${pathname}` | ||
if (baton.style) badgeUrl += `?style=${baton.style}` | ||
res.writeHead(303, { | ||
'location': badgeUrl, | ||
// align on github raw cdn which caches content for 5 minutes | ||
'cache-control': 'max-age=300', | ||
// set expires to avoid github caching | ||
// https://github.com/github/markup/issues/224#issuecomment-48532178 | ||
'expires': new Date(Date.now() + 300 * 1000).toUTCString() | ||
}) | ||
res.end() | ||
} | ||
} | ||
/* -------------------------------------------------------------------------- */ | ||
/** | ||
* Handle a badge request. | ||
@@ -173,8 +32,10 @@ * It redirects to a shields.io badge of type: `size-{size}-brightgreen`. | ||
module.exports = function badgeSize(req, res) { | ||
return parse(req) | ||
.then(fetch) | ||
.then(compressed) | ||
.then(pretty) | ||
.then(redirect(res)) | ||
.catch(redirect(res)) | ||
return init(req) | ||
.then(tap(fetch)) | ||
.then(tap(cond(compression, 'compression'))) | ||
.then(tap(pretty)) | ||
.then(tap(cond(constraints, 'max'))) | ||
.then(tap(color)) | ||
.then(bind(send, res)) | ||
.catch(bind(send, res)) | ||
} |
{ | ||
"name": "badge-size", | ||
"version": "0.4.1", | ||
"version": "1.0.0", | ||
"description": "Displays the size of a given file in your repository", | ||
@@ -10,6 +10,7 @@ "author": "Nicolas Gryman <ngryman@gmail.com> (http://ngryman.sh)", | ||
"engines": { | ||
"node": ">=6" | ||
"node": "^8.10.0 || >=9.10.0" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"lib" | ||
], | ||
@@ -19,3 +20,3 @@ "scripts": { | ||
"unit": "nyc ava", | ||
"start": "micro index.js -p ${PORT:=3000}", | ||
"start": "micro index.js --listen tcp://0.0.0.0:${PORT-3000}", | ||
"test": "npm run lint -s && npm run unit -s", | ||
@@ -25,2 +26,3 @@ "dev": "npm run unit -- --watch", | ||
"check-coverage": "nyc check-coverage --lines 95 --functions 95 --branches 95", | ||
"see-coverage": "nyc report --reporter=html && open coverage/index.html", | ||
"contributors": "contributor-faces --exclude '*-bot'" | ||
@@ -47,14 +49,15 @@ }, | ||
"dependencies": { | ||
"got": "^6.5.0", | ||
"gzip-size": "^3.0.0", | ||
"micro": "^6.0.2", | ||
"pretty-bytes": "^4.0.2" | ||
"brotli-size": "^0.0.3", | ||
"got": "^9.0.0", | ||
"gzip-size": "^5.0.0", | ||
"micro": "^9.3.2", | ||
"pretty-bytes": "^5.1.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.16.0", | ||
"ava": "^0.25.0", | ||
"codecov.io": "^0.1.6", | ||
"contributor-faces": "^0.3.0", | ||
"eslint": "^3.7.0", | ||
"contributor-faces": "^1.0.0", | ||
"eslint": "^5.3.0", | ||
"eslint-config-ngryman": "^1.7.0", | ||
"nyc": "^8.3.0", | ||
"nyc": "^12.0.2", | ||
"pre-commit": "^1.1.3", | ||
@@ -61,0 +64,0 @@ "test-listen": "^1.0.0" |
@@ -24,2 +24,3 @@ # badge-size [![npm][npm-image]][npm-url] [![travis][travis-image]][travis-url] | ||
Gzipped size | ![](http://img.badgesize.io/ngryman/badge-size/master/index.js.svg?compression=gzip) | ||
Brotli size | ![](http://img.badgesize.io/ngryman/badge-size/master/index.js.svg?compression=brotli) | ||
Custom label | ![](http://img.badgesize.io/ngryman/badge-size/master/index.js.svg?label=As_tiny_as) | ||
@@ -36,3 +37,3 @@ PNG format | ![](http://img.badgesize.io/ngryman/badge-size/master/index.js.png) | ||
``` | ||
http://img.badgesize.io/:filepath[.svg|png|jpg][?compression=gzip][&label=string] | ||
http://img.badgesize.io/:filepath[.svg|png|jpg][?compression=gzip|brotli][&label=string][&max=string][&softmax=string] | ||
``` | ||
@@ -42,3 +43,5 @@ | ||
It's the url of your file on `github` when you browse it in the source explorer, minus `blob/` part. | ||
Relative URL of file on GitHub of any absolute URL if hosted elsewhere. | ||
The format of the GitHub URL is the same as when you browse it in the source explorer, minus `blob/` part. | ||
Here is its typical form: | ||
@@ -60,10 +63,10 @@ | ||
#### `[?compression=gzip]` | ||
#### `[?compression=gzip|brotli]` | ||
Optional compression format to measure. It's useful if you want to advertise the *true* size your | ||
file would take on the wire, assuming the server has `gzip` compression enabled. | ||
file would take on the wire, assuming the server has `gzip` or `brotli` compression enabled. | ||
#### `[&label=string]` | ||
Optional text to display in the badge instead of *size* / *gzip size*. | ||
Optional text to display in the badge instead of *size* / *gzip size* / *brotli size*. | ||
@@ -95,10 +98,31 @@ #### `[&color=string]` | ||
#### `[&max=string] [&softmax=string]` | ||
Optional size limits in bytes.<br> | ||
Max is a hard limit. Exceeding this will generate a red badge. <br> | ||
If softlimit is provided (in addition to max) and the file size falls within the range of max and softmax, a yellow badge will be generated.<br> | ||
This setting will override the color option in the above two scenarios. | ||
``` | ||
http://img.badgesize.io/:filepath?max=100000&softmax=200000 | ||
``` | ||
![](https://img.shields.io/badge/size-50%20kB-brightgreen.svg) | ||
![](https://img.shields.io/badge/size-150%20kB-yellow.svg) | ||
![](https://img.shields.io/badge/size-250%20kB-red.svg) | ||
## Contributors | ||
[//]: contributor-faces | ||
<a href="https://github.com/ngryman"><img src="https://avatars.githubusercontent.com/u/892048?v=3" title="ngryman" width="80" height="80"></a> | ||
<a href="https://github.com/bfred-it"><img src="https://avatars.githubusercontent.com/u/1402241?v=3" title="bfred-it" width="80" height="80"></a> | ||
<a href="https://github.com/nathancahill"><img src="https://avatars.githubusercontent.com/u/1383872?v=3" title="nathancahill" width="80" height="80"></a> | ||
<a href="https://github.com/coopy"><img src="https://avatars.githubusercontent.com/u/794843?v=3" title="coopy" width="80" height="80"></a> | ||
<a href="https://github.com/ngryman"><img src="https://avatars2.githubusercontent.com/u/892048?v=4" title="ngryman" width="80" height="80"></a> | ||
<a href="https://github.com/bfred-it"><img src="https://avatars3.githubusercontent.com/u/1402241?v=4" title="bfred-it" width="80" height="80"></a> | ||
<a href="https://github.com/nathancahill"><img src="https://avatars0.githubusercontent.com/u/1383872?v=4" title="nathancahill" width="80" height="80"></a> | ||
<a href="https://github.com/apps/greenkeeper"><img src="https://avatars3.githubusercontent.com/in/505?v=4" title="greenkeeper[bot]" width="80" height="80"></a> | ||
<a href="https://github.com/FezVrasta"><img src="https://avatars2.githubusercontent.com/u/5382443?v=4" title="FezVrasta" width="80" height="80"></a> | ||
<a href="https://github.com/OliverJAsh"><img src="https://avatars2.githubusercontent.com/u/921609?v=4" title="OliverJAsh" width="80" height="80"></a> | ||
<a href="https://github.com/coopy"><img src="https://avatars2.githubusercontent.com/u/794843?v=4" title="coopy" width="80" height="80"></a> | ||
<a href="https://github.com/hairmot"><img src="https://avatars2.githubusercontent.com/u/8102124?v=4" title="hairmot" width="80" height="80"></a> | ||
[//]: contributor-faces | ||
@@ -105,0 +129,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12803
10
218
1
131
5
3
+ Addedbrotli-size@^0.0.3
+ Added@sindresorhus/is@0.14.0(transitive)
+ Added@szmarczak/http-timer@1.1.2(transitive)
+ Addedansi-regex@2.1.1(transitive)
+ Addedaproba@1.2.0(transitive)
+ Addedare-we-there-yet@1.1.7(transitive)
+ Addedarg@4.1.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedbrotli-size@0.0.3(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedbytes@3.1.0(transitive)
+ Addedcacheable-request@6.1.0(transitive)
+ Addedchownr@1.1.4(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addedconsole-control-strings@1.1.0(transitive)
+ Addedcontent-type@1.0.4(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddecompress-response@3.3.04.2.1(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addeddefer-to-connect@1.1.3(transitive)
+ Addeddelegates@1.0.0(transitive)
+ Addeddepd@1.1.2(transitive)
+ Addeddetect-libc@1.0.3(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedexpand-template@2.0.3(transitive)
+ Addedfs-constants@1.0.0(transitive)
+ Addedgauge@2.7.4(transitive)
+ Addedget-stream@4.1.05.2.0(transitive)
+ Addedgithub-from-package@0.0.0(transitive)
+ Addedgot@9.6.0(transitive)
+ Addedgzip-size@5.1.1(transitive)
+ Addedhas-unicode@2.0.1(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedhttp-errors@1.7.3(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addediltorb@2.4.5(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedini@1.3.8(transitive)
+ Addedis-fullwidth-code-point@1.0.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedjson-buffer@3.0.0(transitive)
+ Addedkeyv@3.1.0(transitive)
+ Addedlowercase-keys@2.0.0(transitive)
+ Addedmicro@9.4.1(transitive)
+ Addedmimic-response@1.0.12.1.0(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp-classic@0.5.3(transitive)
+ Addednan@2.22.0(transitive)
+ Addednapi-build-utils@1.0.2(transitive)
+ Addednode-abi@2.30.1(transitive)
+ Addednoop-logger@0.1.1(transitive)
+ Addednormalize-url@4.5.1(transitive)
+ Addednpmlog@4.1.2(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-cancelable@1.1.0(transitive)
+ Addedpify@4.0.1(transitive)
+ Addedprebuild-install@5.3.6(transitive)
+ Addedprepend-http@2.0.0(transitive)
+ Addedpretty-bytes@5.6.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedraw-body@2.4.1(transitive)
+ Addedrc@1.2.8(transitive)
+ Addedreadable-stream@2.3.83.6.2(transitive)
+ Addedresponselike@1.0.2(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedsetprototypeof@1.1.1(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedsimple-concat@1.0.1(transitive)
+ Addedsimple-get@3.1.1(transitive)
+ Addedstatuses@1.5.0(transitive)
+ Addedstring-width@1.0.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedstrip-json-comments@2.0.1(transitive)
+ Addedtar-fs@2.1.2(transitive)
+ Addedtar-stream@2.2.0(transitive)
+ Addedto-readable-stream@1.0.0(transitive)
+ Addedtoidentifier@1.0.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedurl-parse-lax@3.0.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwhich-pm-runs@1.1.0(transitive)
+ Addedwide-align@1.1.5(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedasync-to-gen@1.3.0(transitive)
- Removedbabylon@6.18.0(transitive)
- Removedbytes@2.4.0(transitive)
- Removedcapture-stack-trace@1.0.2(transitive)
- Removedcreate-error-class@3.0.2(transitive)
- Removedget-stream@3.0.0(transitive)
- Removedgot@6.7.1(transitive)
- Removedgzip-size@3.0.0(transitive)
- Removediconv-lite@0.4.15(transitive)
- Removedis-async-supported@1.2.0(transitive)
- Removedis-redirect@1.0.0(transitive)
- Removedis-retry-allowed@1.2.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedmagic-string@0.19.1(transitive)
- Removedmedia-typer@0.3.0(transitive)
- Removedmicro@6.2.1(transitive)
- Removedminimist@1.2.0(transitive)
- Removedprepend-http@1.0.4(transitive)
- Removedpretty-bytes@4.0.2(transitive)
- Removedraw-body@2.2.0(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedtimed-out@4.0.1(transitive)
- Removedunzip-response@2.0.1(transitive)
- Removedurl-parse-lax@1.0.0(transitive)
- Removedvlq@0.2.3(transitive)
Updatedgot@^9.0.0
Updatedgzip-size@^5.0.0
Updatedmicro@^9.3.2
Updatedpretty-bytes@^5.1.0