Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use raw buffer to parse files #97

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions lib/fetch.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
const got = require('got')

/**
* Tell if we need the `identity` header.
*
* Cloudflare seems to return chunk encoded content if we specify this header
* for HEAD requests (#75).
*
* @param {object} baton
* @return {boolean}
*/
const needsIdentityHeader = (baton) =>
!baton.url.includes('unpkg.com')
/** Limit the transfer size in bytes. */
const DOWNLOAD_LIMIT = 50000

/**
* Normalise the URL.
Expand All @@ -32,20 +23,20 @@ async function fetch(baton) {
const url = normalizeUrl(baton.url)

try {
const res = await got[baton.compression ? 'get' : 'head'](url, {
headers: needsIdentityHeader(baton)
? {
'accept-encoding': 'identity'
}
: undefined
const download = got.get(url)
download.on('downloadProgress', ({ transferred, total = 0, percent }) => {
if (Math.max(transferred, total) > DOWNLOAD_LIMIT && percent !== 1) {
download.cancel()
}
})
const res = await download

baton.originalSize = Number(res.headers['content-length'])
baton.originalSize = res.rawBody.byteLength
baton.size = baton.originalSize
baton.data = res.body
baton.data = res.rawBody
}
catch (err) {
baton.err = new Error('Unknown path')
baton.err = new Error('CancelError' === err.name ? 'Exceeded' : 'Unknown path')
throw baton
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function sendBadge(res, baton) {
'x-shields-url': badgeUrl,
'content-type': badgeRes.headers['content-type']
})
res.end(badgeRes.body)
res.end(badgeRes.rawBody)
}

function sendJSON(res, baton) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"dependencies": {
"brotli-size": "^0.0.3",
"cz-emoji": "^1.2.2",
"got": "^9.0.0",
"got": "^11.8.2",
"gzip-size": "^5.0.0",
"micro": "^9.3.2",
"pretty-bytes": "^5.1.0"
Expand All @@ -56,6 +56,7 @@
"contributor-faces": "^1.0.0",
"eslint": "^5.3.0",
"eslint-config-ngryman": "^1.7.0",
"nock": "^13.1.1",
"nyc": "^12.0.2",
"pre-commit": "^1.1.3",
"test-listen": "^1.0.0"
Expand Down
26 changes: 19 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*eslint max-len: 0*/
import test from 'ava'
import micro from 'micro'
import nock from 'nock'
import got from 'got'
import listen from 'test-listen'
import badgeSize from './api'
Expand Down Expand Up @@ -147,14 +148,9 @@ test('accept json format and differenciate original size from compressed size',
assertBody(t, res, { prettySize: '34 B', originalSize: 14, size: 34, color: '44cc11' })
})

test('work with HEAD request on Cloudflare (#75)', async t => {
const res = await request(t, '/https://unpkg.com/constate.json?style=flat-square')
assertBody(t, res, { prettySize: '573 B', originalSize: 573, size: 573, color: '44cc11' })
})

test('fixup broken absolute URLs (#86)', async t => {
const res = await request(t, '/https:/unpkg.com/constate.json?style=flat-square')
assertBody(t, res, { prettySize: '573 B', originalSize: 573, size: 573, color: '44cc11' })
const res = await request(t, '/https:/unpkg.com/constate@3.3.0.json?style=flat-square')
assertBody(t, res, { prettySize: '978 B', originalSize: 978, size: 978, color: '44cc11' })
})

test('reject denied user agents', async t => {
Expand All @@ -168,3 +164,19 @@ test('reject denied URLs', async t => {
const res = await request(t, '/https://unpkg.com/vxe-table/lib/list/src/list.min.js')
assertHeaders(t, res, '/size-unavailable-lightgrey.svg')
})

test('reject file with large content-length header', async t => {
nock('https://large')
.get('/content-length')
.reply(200, '', { 'content-length': '10000000' })
const res = await request(t, '/https://large/content-length.svg')
assertHeaders(t, res, '/size-exceeded-lightgrey.svg')
})

test('reject file with large content', async t => {
nock('https://large')
.get('/content')
.reply(200, ' '.repeat(10000000))
const res = await request(t, '/https://large/content.svg')
assertHeaders(t, res, '/size-exceeded-lightgrey.svg')
})
Loading