diff --git a/.dependabot/config.yml b/.dependabot/config.yml new file mode 100644 index 0000000..cae63e3 --- /dev/null +++ b/.dependabot/config.yml @@ -0,0 +1,5 @@ +version: 1 +update_configs: + - package_manager: "javascript" + directory: "/" + update_schedule: "daily" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5d0d372 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 24728bc..0000000 --- a/.travis.yml +++ /dev/null @@ -1,20 +0,0 @@ -language: node_js - -node_js: - - "6" - - "8" - - "10" - - "11" - - "12" - -os: - - linux - - windows - -script: - - npm run lint && npm run coveralls && npm run typescript - -notifications: - email: - on_success: never - on_failure: always diff --git a/README.md b/README.md index e791dca..9af80fd 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/index.d.ts b/index.d.ts index cad0d7f..6a916d0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,52 +1,38 @@ // Definitions by: Jannik // Leo /// -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 { - sendFile(filename: string, rootPath?: string): FastifyReply; + 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 + +export default fastifyStatic; diff --git a/index.js b/index.js index 86df09f..be3bd90 100644 --- a/index.js +++ b/index.js @@ -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() @@ -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) @@ -207,6 +207,6 @@ function checkRootPathForErrors (fastify, rootPath) { } module.exports = fp(fastifyStatic, { - fastify: '>=2.0.0', + fastify: '3.x', name: 'fastify-static' }) diff --git a/package.json b/package.json index 714f931..1ca180b 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" + } } } diff --git a/test/static.test.js b/test/static.test.js index 721afa3..2a59aaf 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -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) diff --git a/test/types/index.ts b/test/types/index.ts index f4150d1..38d493a 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -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, @@ -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) diff --git a/test/types/tsconfig.json b/test/types/tsconfig.json deleted file mode 100644 index ceb2803..0000000 --- a/test/types/tsconfig.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "compilerOptions": { - "target": "es6", - "module": "commonjs", - "noEmit": true, - "strict": true, - }, - "files": [ - "./index.ts" - ] - } \ No newline at end of file