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

integrate isDeflate and isGzip #230

Merged
merged 5 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ const intoStream = require('into-stream')
const peek = require('peek-stream')
const Minipass = require('minipass')
const pumpify = require('pumpify')
const isGzip = require('is-gzip')
const isDeflate = require('is-deflate')
const encodingNegotiator = require('encoding-negotiator')
const { inherits, format } = require('util')

const { isStream } = require('./lib/utils')
const { isStream, isGzip, isDeflate } = require('./lib/utils')

const InvalidRequestEncodingError = createError('FST_CP_ERR_INVALID_CONTENT_ENCODING', 'Unsupported Content-Encoding: %s', 415)
const InvalidRequestCompressedPayloadError = createError('FST_CP_ERR_INVALID_CONTENT', 'Could not decompress the request payload using the provided encoding', 400)
Expand Down
23 changes: 22 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
'use strict'

function isDeflate (buffer) {
return (
typeof buffer === 'object' &&
buffer !== null &&
buffer.length > 1 &&
buffer[0] === 0x78 &&
(buffer[1] === 1 || buffer[1] === 0x9c || buffer[1] === 0xda)
)
}

function isGzip (buffer) {
return (
typeof buffer === 'object' &&
buffer !== null &&
buffer.length > 2 &&
buffer[0] === 0x1f &&
buffer[1] === 0x8b &&
buffer[2] === 0x08
)
}

function isStream (stream) {
return stream !== null && typeof stream === 'object' && typeof stream.pipe === 'function'
}

module.exports = { isStream }
module.exports = { isGzip, isDeflate, isStream }
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"encoding-negotiator": "^2.0.1",
"fastify-plugin": "^3.0.0",
"into-stream": "^6.0.0",
"is-deflate": "^1.0.0",
"is-gzip": "^2.0.0",
"is-zip": "^1.0.0",
"mime-db": "^1.51.0",
"minipass": "^3.1.6",
Expand Down
35 changes: 34 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { createReadStream } = require('fs')
const { Socket } = require('net')
const { Duplex, PassThrough, Readable, Stream, Transform, Writable } = require('stream')
const { test } = require('tap')
const { isStream } = require('../lib/utils')
const { isStream, isDeflate, isGzip } = require('../lib/utils')

test('isStream() utility should be able to detect Streams', async (t) => {
t.plan(12)
Expand All @@ -25,3 +25,36 @@ test('isStream() utility should be able to detect Streams', async (t) => {
t.equal(isStream(undefined), false)
t.equal(isStream(''), false)
})

test('isDeflate() utility should be able to detect deflate compressed Buffer', async (t) => {
t.plan(11)

t.equal(isDeflate(Buffer.alloc(0)), false)
t.equal(isDeflate(Buffer.alloc(0)), false)
t.equal(isDeflate(Buffer.from([0x78])), false)
t.equal(isDeflate(Buffer.from([0x78, 0x00])), false)
t.equal(isDeflate(Buffer.from([0x78, 0x01])), true)
t.equal(isDeflate(Buffer.from([0x78, 0x9c])), true)
t.equal(isDeflate(Buffer.from([0x78, 0xda])), true)

t.equal(isDeflate({}), false)
t.equal(isDeflate(null), false)
t.equal(isDeflate(undefined), false)
t.equal(isDeflate(''), false)
})

test('isGzip() utility should be able to detect gzip compressed Buffer', async (t) => {
t.plan(10)

t.equal(isGzip(Buffer.alloc(0)), false)
t.equal(isGzip(Buffer.alloc(1)), false)
t.equal(isGzip(Buffer.alloc(2)), false)
t.equal(isGzip(Buffer.from([0x1f, 0x8b])), false)
t.equal(isGzip(Buffer.from([0x1f, 0x8b, 0x00])), false)
t.equal(isGzip(Buffer.from([0x1f, 0x8b, 0x08])), true)

t.equal(isGzip({}), false)
t.equal(isGzip(null), false)
t.equal(isGzip(undefined), false)
t.equal(isGzip(''), false)
})