Skip to content

Commit

Permalink
Upgrade to Fastify v3 (#125)
Browse files Browse the repository at this point in the history
* upgrade fastify dependency to v3

* add dependabot config

* add dependabot config

* Update .dependabot/config.yml

Co-Authored-By: Matteo Collina <matteo.collina@gmail.com>

* add build status badge

* remove tslint, change request.req to request.raw and reply.res to reply.raw

* update version

* Update index.js

* Update package.json

Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
frikille and mcollina committed May 4, 2020
1 parent b3add45 commit 7aaf1af
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 96 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: CI workflow
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install Dependencies
run: npm install --ignore-scripts
- name: Test
run: npm test
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# fastify-static
[![Build Status](https://travis-ci.org/fastify/fastify-static.svg?branch=master)](https://travis-ci.org/fastify/fastify-static) [![Greenkeeper badge](https://badges.greenkeeper.io/fastify/fastify-static.svg)](https://greenkeeper.io/) [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-static/badge.svg)](https://snyk.io/test/github/fastify/fastify-static)
![CI workflow](https://github.com/fastify/fastify-static/workflows/CI%20workflow/badge.svg) [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-static/badge.svg)](https://snyk.io/test/github/fastify/fastify-static)

Plugin for serving static files as fast as possible. Supports Fastify versions `>=2.0.0`.

Expand Down
66 changes: 26 additions & 40 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,38 @@
// Definitions by: Jannik <https://github.com/jannikkeye>
// Leo <https://github.com/leomelzer>
/// <reference types="node" />
import * as fastify from 'fastify';
import { Plugin } from "fastify";
import { Server, IncomingMessage, ServerResponse } from "http";
import { Http2SecureServer, Http2Server, Http2ServerRequest, Http2ServerResponse } from "http2";
import * as https from "https";

type HttpServer = Server | Http2Server | Http2SecureServer | https.Server;
type HttpRequest = IncomingMessage | Http2ServerRequest;
type HttpResponse = ServerResponse | Http2ServerResponse;
import { FastifyPlugin, FastifyReply, RawServerBase } from 'fastify'

declare module "fastify" {
interface FastifyReply<HttpResponse> {
sendFile(filename: string, rootPath?: string): FastifyReply<HttpResponse>;
interface FastifyReplyInterface {
sendFile(filename: string, rootPath?: string): FastifyReply;
}
}

declare function fastifyStatic(): fastify.Plugin<
Server,
IncomingMessage,
ServerResponse,
{
root: string;
prefix?: string;
prefixAvoidTrailingSlash?: boolean;
serve?: boolean;
decorateReply?: boolean;
schemaHide?: boolean;
setHeaders?: (...args: any[]) => void;
redirect?: boolean;
wildcard?: boolean | string;
export interface FastifyStaticOptions {
root: string;
prefix?: string;
prefixAvoidTrailingSlash?: boolean;
serve?: boolean;
decorateReply?: boolean;
schemaHide?: boolean;
setHeaders?: (...args: any[]) => void;
redirect?: boolean;
wildcard?: boolean | string;

// Passed on to `send`
acceptRanges?: boolean;
cacheControl?: boolean;
dotfiles?: boolean;
etag?: boolean;
extensions?: string[];
immutable?: boolean;
index?: string[];
lastModified?: boolean;
maxAge?: string | number;
}
>;

declare namespace fastifyStatic {
interface FastifyStaticOptions {}
// Passed on to `send`
acceptRanges?: boolean;
cacheControl?: boolean;
dotfiles?: boolean;
etag?: boolean;
extensions?: string[];
immutable?: boolean;
index?: string[];
lastModified?: boolean;
maxAge?: string | number;
}

export = fastifyStatic;
declare const fastifyStatic: FastifyPlugin<FastifyStaticOptions>

export default fastifyStatic;
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function fastifyStatic (fastify, opts, next) {
const wrap = new PassThrough({
flush (cb) {
this.finished = true
if (reply.res.statusCode === 304) {
if (reply.raw.statusCode === 304) {
reply.send('')
}
cb()
Expand All @@ -68,7 +68,7 @@ function fastifyStatic (fastify, opts, next) {
})
Object.defineProperty(wrap, 'statusCode', {
get () {
return reply.res.statusCode
return reply.raw.statusCode
},
set (code) {
reply.code(code)
Expand Down Expand Up @@ -207,6 +207,6 @@ function checkRootPathForErrors (fastify, rootPath) {
}

module.exports = fp(fastifyStatic, {
fastify: '>=2.0.0',
fastify: '3.x',
name: 'fastify-static'
})
27 changes: 18 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"lint": "npm run lint:standard && npm run lint:typescript",
"lint:standard": "standard | snazzy",
"lint:typescript": "standard --parser typescript-eslint-parser --plugin typescript test/types/*.ts",
"lint": "standard | snazzy",
"unit": "tap test/*.test.js",
"typescript": "tsc --project ./test/types/tsconfig.json",
"typescript": "tsd",
"test": "npm run lint && npm run unit && npm run typescript",
"example": "node example/server.js",
"coverage": "tap --cov --coverage-report=html test",
Expand All @@ -30,25 +28,36 @@
},
"homepage": "https://github.com/fastify/fastify-static",
"dependencies": {
"fastify-plugin": "^1.6.0",
"fastify-plugin": "^2.0.0",
"glob": "^7.1.4",
"readable-stream": "^3.4.0",
"send": "^0.16.0"
},
"devDependencies": {
"@types/node": "^10.14.9",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"concat-stream": "^2.0.0",
"coveralls": "^3.0.4",
"eslint-plugin-typescript": "^0.14.0",
"fastify": "^2.5.0",
"fastify-compress": "^0.8.1",
"fastify": "^3.0.0-rc.1",
"fastify-compress": "^3.0.0",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.0",
"simple-get": "^3.0.3",
"snazzy": "^8.0.0",
"standard": "^12.0.0",
"tap": "^12.7.0",
"typescript": "^3.5.2",
"typescript-eslint-parser": "^22.0.0"
"tsd": "^0.11.0",
"typescript": "^3.5.2"
},
"tsd": {
"directory": "test/types"
},
"eslintConfig": {
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error"
}
}
}
2 changes: 1 addition & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ t.test('not found responses can be customized with fastify.setNotFoundHandler()'
const fastify = Fastify()

fastify.setNotFoundHandler(function notFoundHandler (request, reply) {
reply.code(404).type('text/plain').send(request.req.url + ' Not Found')
reply.code(404).type('text/plain').send(request.raw.url + ' Not Found')
})

fastify.register(fastifyStatic, pluginOptions)
Expand Down
15 changes: 4 additions & 11 deletions test/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import * as fastify from 'fastify'
import * as http2 from 'http2'
import * as fastifyStatic from '../..'

console.log(http2.constants.NGHTTP2_NO_ERROR) // fix for http2 is declared but not used
import fastify from 'fastify'
import fastifyStatic, { FastifyStaticOptions } from '../..'

const appWithImplicitHttp = fastify()
const options = {
const options: FastifyStaticOptions = {
acceptRanges: true,
cacheControl: true,
decorateReply: true,
Expand Down Expand Up @@ -35,11 +32,7 @@ appWithImplicitHttp
})
})

const appWithHttp2: fastify.FastifyInstance<
http2.Http2Server,
http2.Http2ServerRequest,
http2.Http2ServerResponse
> = fastify({ http2: true })
const appWithHttp2 = fastify({ http2: true })

appWithHttp2
.register(fastifyStatic, options)
Expand Down
11 changes: 0 additions & 11 deletions test/types/tsconfig.json

This file was deleted.

0 comments on commit 7aaf1af

Please sign in to comment.