Skip to content

Commit

Permalink
V3 upgrade (#107)
Browse files Browse the repository at this point in the history
* Upgrade to Fastify v3

* Added missing input types

* Update node versions

* Updated fastify requirements

* Remove legacy brotli support

* Add github actions and depdendabot

* Update index.js

Co-authored-by: Thomas Heymann <thom@thoms-air.home>
Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
3 people authored Apr 28, 2020
1 parent d108e63 commit f6fed1c
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 117 deletions.
5 changes: 5 additions & 0 deletions .dependabot/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 1
update_configs:
- package_manager: "javascript"
directory: "/"
update_schedule: "daily"
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Continuous Integration
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v1
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,6 @@ fastify.register(
{ customTypes: /x-protobuf$/ }
)
```
### Brotli
Brotli compression is enabled by default if your Node.js supports it natively (≥ v11.7.0).

For Node.js versions that don’t natively support Brotli, it's not enabled by default. If you need it, we recommend installing [`iltorb`](https://www.npmjs.com/package/iltorb) and passing it to the `brotli` option:

```javascript
fastify.register(
require('fastify-compress'),
{ brotli: require('iltorb') }
)
```

### onUnsupportedEncoding
When the encoding is not supported, a custom error response can be sent in place of the uncompressed payload by setting the `onUnsupportedEncoding(encoding, request, reply)` option to be a function that can modify the reply and return a `string | Buffer | Stream | Error` payload.
Expand Down
39 changes: 18 additions & 21 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import { Plugin, FastifyReply, FastifyRequest } from 'fastify'
import { Server, IncomingMessage, ServerResponse } from 'http'
import { FastifyPlugin, FastifyReply, FastifyRequest, RawServerBase } from 'fastify'
import { Stream } from 'stream';
import { Input, InputObject } from 'into-stream';

type EncodingToken = 'br' | 'deflate' | 'gzip' | 'identity'

declare namespace fastifyCompress {
interface FastifyCompressOptions {
global?: boolean
threshold?: number
customTypes?: RegExp
brotli?: NodeModule
zlib?: NodeModule
inflateIfDeflated?: boolean
onUnsupportedEncoding?: (encoding: string, request: FastifyRequest<ServerResponse>, reply: FastifyReply<ServerResponse>) => string | Buffer | Stream
encodings?: Array<EncodingToken>
declare module "fastify" {
interface FastifyReplyInterface {
compress(input: Stream | Input | InputObject): void;
}
}

declare const fastifyCompress: Plugin<
Server,
IncomingMessage,
ServerResponse,
fastifyCompress.FastifyCompressOptions
>
type EncodingToken = 'br' | 'deflate' | 'gzip' | 'identity'

export interface FastifyCompressOptions {
global?: boolean
threshold?: number
customTypes?: RegExp
zlib?: NodeModule
inflateIfDeflated?: boolean
onUnsupportedEncoding?: (encoding: string, request: FastifyRequest<RawServerBase>, reply: FastifyReply<RawServerBase>) => string | Buffer | Stream
encodings?: Array<EncodingToken>
}

export = fastifyCompress
declare const fastifyCompress: FastifyPlugin<FastifyCompressOptions>
export default fastifyCompress;
13 changes: 5 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ function processParams (opts) {
}

const supportedEncodings = ['gzip', 'deflate', 'identity']
if (opts.brotli) {
params.compressStream.br = opts.brotli.compressStream
supportedEncodings.unshift('br')
} else if (zlib.createBrotliCompress) {
if (zlib.createBrotliCompress) {
params.compressStream.br = zlib.createBrotliCompress
supportedEncodings.unshift('br')
}
Expand Down Expand Up @@ -127,7 +124,7 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {

function onSend (req, reply, payload, next) {
if (payload == null) {
reply.res.log.debug('compress: missing payload')
reply.log.debug('compress: missing payload')
return next()
}

Expand Down Expand Up @@ -180,7 +177,7 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
function compress (params) {
return function (payload) {
if (payload == null) {
this.res.log.debug('compress: missing payload')
this.log.debug('compress: missing payload')
this.send(new Error('Internal server error'))
return
}
Expand Down Expand Up @@ -240,7 +237,7 @@ function compress (params) {
}

function onEnd (err) {
if (err) this.res.log.error(err)
if (err) this.raw.log.error(err)
}

function getEncodingHeader (encodings, request) {
Expand Down Expand Up @@ -314,6 +311,6 @@ function unzipStream (inflate, maxRecursion) {
}

module.exports = fp(compressPlugin, {
fastify: '>=2.11.0',
fastify: '3.x',
name: 'fastify-compress'
})
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"main": "index.js",
"dependencies": {
"encoding-negotiator": "^2.0.0",
"fastify-plugin": "^1.0.0",
"into-stream": "4.0.0",
"fastify-plugin": "^2.0.0",
"into-stream": "^5.1.1",
"is-deflate": "^1.0.0",
"is-gzip": "^1.0.0",
"is-gzip": "^2.0.0",
"is-stream": "^2.0.0",
"is-zip": "^1.0.0",
"mime-db": "^1.43.0",
"minipass": "^2.9.0",
"minipass": "^3.1.1",
"peek-stream": "^1.1.0",
"pump": "^3.0.0",
"pumpify": "^2.0.1",
Expand All @@ -23,19 +23,19 @@
"@types/node": "^13.1.0",
"@typescript-eslint/parser": "^2.19.2",
"eslint-plugin-typescript": "^0.14.0",
"fastify": "^2.11.0",
"iltorb": "2.4.3",
"fastify": "^3.0.0-alpha.1",
"jsonstream": "^1.0.3",
"pre-commit": "^1.2.2",
"standard": "^14.3.1",
"tap": "^12.6.6",
"tap": "^14.10.7",
"tsd": "^0.11.0",
"typescript": "^3.7.5"
},
"scripts": {
"unit": "tap test/*.js",
"test": "standard && npm run unit && npm run typescript",
"lint:typescript": "standard --fix --parser @typescript-eslint/parser --plugin typescript test/types/*.ts",
"typescript": "tsc --project ./test/types/tsconfig.json",
"typescript": "tsd",
"coverage": "npm run unit -- --cov",
"coverage-report": "npm run coverage && tap --coverage-report=lcov"
},
Expand All @@ -56,11 +56,10 @@
"type": "git",
"url": "git+https://github.com/fastify/fastify-compress.git"
},
"greenkeeper": {
"ignore": [
"into-stream",
"iltorb",
"tap"
]
"engines": {
"node": ">=10.16"
},
"tsd": {
"directory": "test"
}
}
26 changes: 26 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fastify from 'fastify'
import { createReadStream } from 'fs'
import fastifyCompress from '..'

const zlib = require('zlib')

const app = fastify()

app.register(fastifyCompress, {
global: true,
threshold: 10,
zlib: zlib,
inflateIfDeflated: true,
customTypes: /x-protobuf$/,
encodings: ['gzip', 'br', 'identity', 'deflate']
})

const appWithoutGlobal = fastify();

appWithoutGlobal.register(fastifyCompress, { global: false })

appWithoutGlobal.get('/', (req, reply) => {
reply
.type('text/plain')
.compress(createReadStream('./package.json'))
})
Loading

0 comments on commit f6fed1c

Please sign in to comment.