@fastify/compress 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,45 +1,17 @@
1
1
  name: CI
2
- 'on':
2
+
3
+ on:
3
4
  push:
4
5
  paths-ignore:
5
- - docs/**
6
+ - 'docs/**'
6
7
  - '*.md'
7
8
  pull_request:
8
9
  paths-ignore:
9
- - docs/**
10
+ - 'docs/**'
10
11
  - '*.md'
12
+
11
13
  jobs:
12
14
  test:
13
- runs-on: ${{ matrix.os }}
14
- strategy:
15
- matrix:
16
- node-version:
17
- - 14
18
- - 16
19
- - 18
20
- os:
21
- - macos-latest
22
- - ubuntu-latest
23
- - windows-latest
24
- steps:
25
- - uses: actions/checkout@v3
26
- - name: Use Node.js
27
- uses: actions/setup-node@v3
28
- with:
29
- node-version: ${{ matrix.node-version }}
30
- - name: Install Dependencies
31
- run: |
32
- npm install --ignore-scripts
33
- - name: Run Tests
34
- run: |
35
- npm test
36
- automerge:
37
- needs: test
38
- runs-on: ubuntu-latest
39
- permissions:
40
- pull-requests: write
41
- contents: write
42
- steps:
43
- - uses: fastify/github-action-merge-dependabot@v3
44
- with:
45
- github-token: ${{ secrets.GITHUB_TOKEN }}
15
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
16
+ with:
17
+ lint: true
package/README.md CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  ![CI](https://github.com/fastify/fastify-compress/workflows/CI/badge.svg)
4
4
  [![NPM version](https://img.shields.io/npm/v/@fastify/compress.svg?style=flat)](https://www.npmjs.com/package/@fastify/compress)
5
- [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-compress/badge.svg)](https://snyk.io/test/github/fastify/fastify-compress)
6
5
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
7
6
 
8
7
  Adds compression utils to [the Fastify `reply` object](https://www.fastify.io/docs/latest/Reference/Reply/#reply) and a hook to decompress requests payloads.
package/index.js CHANGED
@@ -8,12 +8,10 @@ const intoStream = require('into-stream')
8
8
  const peek = require('peek-stream')
9
9
  const Minipass = require('minipass')
10
10
  const pumpify = require('pumpify')
11
- const isGzip = require('is-gzip')
12
- const isDeflate = require('is-deflate')
13
11
  const encodingNegotiator = require('encoding-negotiator')
14
12
  const { inherits, format } = require('util')
15
13
 
16
- const { isStream } = require('./lib/utils')
14
+ const { isStream, isGzip, isDeflate } = require('./lib/utils')
17
15
 
18
16
  const InvalidRequestEncodingError = createError('FST_CP_ERR_INVALID_CONTENT_ENCODING', 'Unsupported Content-Encoding: %s', 415)
19
17
  const InvalidRequestCompressedPayloadError = createError('FST_CP_ERR_INVALID_CONTENT', 'Could not decompress the request payload using the provided encoding', 400)
package/lib/utils.js CHANGED
@@ -1,7 +1,38 @@
1
1
  'use strict'
2
2
 
3
+ // https://datatracker.ietf.org/doc/html/rfc1950#section-2
4
+ function isDeflate (buffer) {
5
+ return (
6
+ typeof buffer === 'object' &&
7
+ buffer !== null &&
8
+ buffer.length > 1 &&
9
+ // CM = 8 denotes the "deflate" compression method
10
+ (buffer[0] & 0x0f) === 0x08 &&
11
+ // CINFO Values of above 7 are not allowed by RFC 1950
12
+ (buffer[0] & 0x80) === 0 &&
13
+ // The FCHECK value must be such that CMF and FLG, when viewed as
14
+ // a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
15
+ // is a multiple of 31.
16
+ (((buffer[0] << 8) + buffer[1]) % 31) === 0
17
+ )
18
+ }
19
+
20
+ // https://datatracker.ietf.org/doc/html/rfc1952#page-6
21
+ function isGzip (buffer) {
22
+ return (
23
+ typeof buffer === 'object' &&
24
+ buffer !== null &&
25
+ buffer.length > 2 &&
26
+ // ID1
27
+ buffer[0] === 0x1f &&
28
+ // ID2
29
+ buffer[1] === 0x8b &&
30
+ buffer[2] === 0x08
31
+ )
32
+ }
33
+
3
34
  function isStream (stream) {
4
35
  return stream !== null && typeof stream === 'object' && typeof stream.pipe === 'function'
5
36
  }
6
37
 
7
- module.exports = { isStream }
38
+ module.exports = { isGzip, isDeflate, isStream }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/compress",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "Fastify compression utils",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -8,15 +8,11 @@
8
8
  "encoding-negotiator": "^2.0.1",
9
9
  "fastify-plugin": "^3.0.0",
10
10
  "into-stream": "^6.0.0",
11
- "is-deflate": "^1.0.0",
12
- "is-gzip": "^2.0.0",
13
- "is-zip": "^1.0.0",
14
11
  "mime-db": "^1.51.0",
15
12
  "minipass": "^3.1.6",
16
13
  "peek-stream": "^1.1.3",
17
14
  "pump": "^3.0.0",
18
- "pumpify": "^2.0.1",
19
- "string-to-stream": "^3.0.1"
15
+ "pumpify": "^2.0.1"
20
16
  },
21
17
  "devDependencies": {
22
18
  "@types/node": "^17.0.8",
@@ -28,7 +24,7 @@
28
24
  "pre-commit": "^1.2.2",
29
25
  "standard": "^17.0.0",
30
26
  "tap": "^16.0.0",
31
- "tsd": "^0.20.0",
27
+ "tsd": "^0.21.0",
32
28
  "typescript": "^4.5.4"
33
29
  },
34
30
  "scripts": {
@@ -61,9 +57,6 @@
61
57
  "type": "git",
62
58
  "url": "git+https://github.com/fastify/fastify-compress.git"
63
59
  },
64
- "engines": {
65
- "node": ">=10.16"
66
- },
67
60
  "tsd": {
68
61
  "directory": "test/types"
69
62
  },
@@ -4,7 +4,7 @@ const { createReadStream } = require('fs')
4
4
  const { Socket } = require('net')
5
5
  const { Duplex, PassThrough, Readable, Stream, Transform, Writable } = require('stream')
6
6
  const { test } = require('tap')
7
- const { isStream } = require('../lib/utils')
7
+ const { isStream, isDeflate, isGzip } = require('../lib/utils')
8
8
 
9
9
  test('isStream() utility should be able to detect Streams', async (t) => {
10
10
  t.plan(12)
@@ -25,3 +25,39 @@ test('isStream() utility should be able to detect Streams', async (t) => {
25
25
  t.equal(isStream(undefined), false)
26
26
  t.equal(isStream(''), false)
27
27
  })
28
+
29
+ test('isDeflate() utility should be able to detect deflate compressed Buffer', async (t) => {
30
+ t.plan(14)
31
+
32
+ t.equal(isDeflate(Buffer.alloc(0)), false)
33
+ t.equal(isDeflate(Buffer.alloc(0)), false)
34
+ t.equal(isDeflate(Buffer.from([0x78])), false)
35
+ t.equal(isDeflate(Buffer.from([0x78, 0x00])), false)
36
+ t.equal(isDeflate(Buffer.from([0x7a, 0x01])), false)
37
+ t.equal(isDeflate(Buffer.from([0x88, 0x01])), false)
38
+ t.equal(isDeflate(Buffer.from([0x78, 0x11])), false)
39
+ t.equal(isDeflate(Buffer.from([0x78, 0x01])), true)
40
+ t.equal(isDeflate(Buffer.from([0x78, 0x9c])), true)
41
+ t.equal(isDeflate(Buffer.from([0x78, 0xda])), true)
42
+
43
+ t.equal(isDeflate({}), false)
44
+ t.equal(isDeflate(null), false)
45
+ t.equal(isDeflate(undefined), false)
46
+ t.equal(isDeflate(''), false)
47
+ })
48
+
49
+ test('isGzip() utility should be able to detect gzip compressed Buffer', async (t) => {
50
+ t.plan(10)
51
+
52
+ t.equal(isGzip(Buffer.alloc(0)), false)
53
+ t.equal(isGzip(Buffer.alloc(1)), false)
54
+ t.equal(isGzip(Buffer.alloc(2)), false)
55
+ t.equal(isGzip(Buffer.from([0x1f, 0x8b])), false)
56
+ t.equal(isGzip(Buffer.from([0x1f, 0x8b, 0x00])), false)
57
+ t.equal(isGzip(Buffer.from([0x1f, 0x8b, 0x08])), true)
58
+
59
+ t.equal(isGzip({}), false)
60
+ t.equal(isGzip(null), false)
61
+ t.equal(isGzip(undefined), false)
62
+ t.equal(isGzip(''), false)
63
+ })