Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into main
  • Loading branch information
climba03003 committed Oct 26, 2021
2 parents 41d265c + 8bd42af commit d4f86ef
Show file tree
Hide file tree
Showing 17 changed files with 421 additions and 182 deletions.
14 changes: 13 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,17 @@
"extends": "standard-with-typescript",
"parserOptions": {
"project": "./tsconfig.json"
}
},
"rules": {
// conflict between standard and standard-typescript
"no-void": ["error", { "allowAsStatement": true }]
},
"overrides": [
{
"files": [ "**/*.test.ts"],
"rules": {
"@typescript-eslint/no-floating-promises": "off"
}
}
]
}
28 changes: 19 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ env:
COVERALLS: 0

jobs:
build:
test:
runs-on: ${{ matrix.os }}
outputs:
COVERALLS: ${{ steps.coveralls-trigger.outputs.COVERALLS_TRIGGER }}

strategy:
matrix:
# Maintenance and active LTS
node-version: [12, 14, 16]
node-version: [10, 12, 14, 16]
os: [ubuntu-latest, windows-latest, macOS-latest]
name: Node ${{ matrix.node-version }}

Expand All @@ -28,7 +31,7 @@ jobs:

- name: Use Node.js
id: setup_node
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
node-version: ${{ matrix.node-version }}

Expand All @@ -50,19 +53,26 @@ jobs:
run: npm run coverage

- name: Coveralls Parallel
id: coveralls-parallel
continue-on-error: true
uses: coverallsapp/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel: true
flag-name: run-${{ matrix.node-version }}-${{ matrix.os }}

- name: Typescript
id: typescript_test
run: npm run typescript --if-present
- name: Should Trigger coverallsapp/[email protected]
id: coveralls-trigger
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#steps-context
# when continue-on-error failed, outcome is failure and conclusion is success
if: steps.coveralls-parallel.conclusion == 'success' && steps.coveralls-parallel.outcome != 'success'
run: |
echo "::set-output name=COVERALLS_TRIGGER::failure"
coverage:
needs: build
needs: test
runs-on: ubuntu-latest
if: needs.test.outputs.COVERALLS != 'failure'
steps:
- name: Coveralls Finished
uses: coverallsapp/[email protected]
Expand All @@ -71,10 +81,10 @@ jobs:
parallel-finished: true

automerge:
needs: build
needs: test
runs-on: ubuntu-latest
steps:
- uses: fastify/github-action-merge-dependabot@v2.4.0
- uses: fastify/github-action-merge-dependabot@v2.5.0
if: ${{ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request' }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
9 changes: 5 additions & 4 deletions .github/workflows/package-manager-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- main

jobs:
pnpm:
Expand All @@ -12,14 +13,14 @@ jobs:
strategy:
matrix:
# Maintenance and active LTS
node-version: [12, 14, 16]
node-version: [10, 12, 14, 16]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2

- name: Use Node.js
uses: actions/[email protected].0
uses: actions/[email protected].1
id: setup_node
with:
node-version: ${{ matrix.node-version }}
Expand All @@ -39,15 +40,15 @@ jobs:
strategy:
matrix:
# Maintenance and active LTS
node-version: [12, 14, 16]
node-version: [10, 12, 14, 16]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2

- name: Use Node.js
id: setup_node
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
node-version: ${{ matrix.node-version }}

Expand Down
5 changes: 5 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ts: true
jsx: false
flow: false
check-coverage: true
coverage: true
6 changes: 0 additions & 6 deletions jest.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const plugin: FastifyPluginAsync<FastifyFormidableOptions> = async function (fas
await fs.promises.mkdir(options.formidable.uploadDir, { recursive: true })
}

const formidable = new IncomingForm(options.formidable)
const formidable = options.formidable instanceof IncomingForm ? options.formidable : new IncomingForm(options.formidable)

fastify.decorateRequest(kIsMultipart, false)
fastify.decorateRequest(kIsMultipartParsed, false)
Expand Down
31 changes: 12 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
"lib": "lib"
},
"scripts": {
"clean": "npx rimraf \"lib/**/*.js\" \"lib/**/*.map\" \"lib/**/*.d.ts\" \"test/**/*.js\" \"test/**/*.map\" \"test/**/*.d.ts\"",
"lint": "eslint --ext .ts lib test",
"lint:fix": "npm run lint -- --fix",
"build": "rollup -c",
"build:tsc": "tsc -b",
"unit": "jest",
"unit": "tap",
"test": "npm run lint && npm run unit",
"coverage": "jest --coverage",
"coverage": "tap --coverage --coverage-report=lcovonly",
"prepublishOnly": "npm run build"
},
"publishConfig": {
Expand All @@ -40,29 +41,21 @@
},
"devDependencies": {
"@rollup/plugin-typescript": "^8.2.1",
"@types/jest": "^27.0.1",
"@typescript-eslint/eslint-plugin": "4",
"eslint": "7",
"eslint-config-standard-with-typescript": "^20.0.0",
"eslint-plugin-import": "2",
"eslint-plugin-node": "11",
"eslint-plugin-promise": "5",
"eslint-plugin-standard": "5",
"@types/tap": "^15.0.5",
"@typescript-eslint/eslint-plugin": "^4.0.1",
"eslint": "^7.12.1",
"eslint-config-standard-with-typescript": "^21.0.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.0.0",
"fastify": "^3.19.0",
"fastify-swagger": "^4.8.2",
"form-data": "^4.0.0",
"husky": "^7.0.1",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"rollup": "^2.53.1",
"ts-jest": "^27.0.3",
"tap": "^15.0.10",
"ts-node": "^10.4.0",
"typescript": "^4.3.5",
"undici": "^4.1.0"
},
"husky": {
"hooks": {
"pre-commit": "npm test",
"pre-push": "npm test"
}
}
}
45 changes: 24 additions & 21 deletions test/addContentTypeParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import * as fs from 'fs'
import { AddressInfo } from 'net'
import * as path from 'path'
import t from 'tap'
import { createFastify } from './createFastify'
import { request } from './request'
import FormData = require('form-data')

const filePath = path.join(__dirname, '../package.json')

describe('addContentTypeParser', function () {
test('single file', async function () {
const fastify = await createFastify({ addContentTypeParser: true })
t.plan(1)
t.test('addContentTypeParser', function (t) {
t.plan(2)

t.test('single file', async function (t) {
t.plan(5)

const fastify = await createFastify(t, { addContentTypeParser: true })

const form = new FormData()
form.append('foo', 'bar')
form.append('file', fs.createReadStream(filePath))

const response = await request(`http://localhost:${(fastify.server.address() as AddressInfo).port}`, form)

expect(response.status).toStrictEqual(200)
t.equal(response.status, 200)

const json = await response.json()

expect(json.body.foo).toStrictEqual('bar')
expect(/upload_/.test(json.body.file)).toStrictEqual(true)
expect(json.files.file).toBeDefined()
expect(json.files.file.name).toStrictEqual('package.json')

await fastify.close()
t.equal(json.body.foo, 'bar')
t.equal(/upload_/.test(json.body.file), true)
t.ok(json.files.file)
t.equal(json.files.file.name, 'package.json')
})

test('multiple files', async function () {
const fastify = await createFastify({ addContentTypeParser: true, formidable: { multiples: true } })
t.test('multiple files', async function (t) {
t.plan(6)

const fastify = await createFastify(t, { addContentTypeParser: true, formidable: { multiples: true } })

const form = new FormData()
form.append('foo', 'bar')
Expand All @@ -39,16 +44,14 @@ describe('addContentTypeParser', function () {

const response = await request(`http://localhost:${(fastify.server.address() as AddressInfo).port}`, form)

expect(response.status).toStrictEqual(200)
t.equal(response.status, 200)

const json = await response.json()

expect(json.body.foo).toStrictEqual('bar')
expect(Array.isArray(json.body.file)).toStrictEqual(true)
expect(json.files.file).toBeDefined()
expect(json.files.file[0].name).toStrictEqual('package.json')
expect(json.files.file[1].name).toStrictEqual('package.json')

await fastify.close()
t.equal(json.body.foo, 'bar')
t.equal(Array.isArray(json.body.file), true)
t.ok(json.files.file)
t.equal(json.files.file[0].name, 'package.json')
t.equal(json.files.file[1].name, 'package.json')
})
})
61 changes: 41 additions & 20 deletions test/addHooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import * as fs from 'fs'
import { AddressInfo } from 'net'
import * as path from 'path'
import t from 'tap'
import { createFastify } from './createFastify'
import { request } from './request'
import { request, requestJSON } from './request'
import FormData = require('form-data')

const filePath = path.join(__dirname, '../package.json')

describe('addHooks', function () {
test('single file', async function () {
const fastify = await createFastify({ addHooks: true })
t.plan(1)
t.test('addHooks', function (t) {
t.plan(3)

t.test('single file', async function (t) {
t.plan(5)

const fastify = await createFastify(t, { addHooks: true })

const form = new FormData()
form.append('foo', 'bar')
form.append('file', fs.createReadStream(filePath))

const response = await request(`http://localhost:${(fastify.server.address() as AddressInfo).port}`, form)

expect(response.status).toStrictEqual(200)
t.equal(response.status, 200)

const json = await response.json()

expect(json.body.foo).toStrictEqual('bar')
expect(/upload_/.test(json.body.file)).toStrictEqual(true)
expect(json.files.file).toBeDefined()
expect(json.files.file.name).toStrictEqual('package.json')

await fastify.close()
t.equal(json.body.foo, 'bar')
t.equal(/upload_/.test(json.body.file), true)
t.ok(json.files.file)
t.equal(json.files.file.name, 'package.json')
})

test('multiple files', async function () {
const fastify = await createFastify({ addHooks: true, formidable: { multiples: true } })
t.test('multiple files', async function (t) {
t.plan(6)

const fastify = await createFastify(t, { addHooks: true, formidable: { multiples: true } })

const form = new FormData()
form.append('foo', 'bar')
Expand All @@ -39,16 +45,31 @@ describe('addHooks', function () {

const response = await request(`http://localhost:${(fastify.server.address() as AddressInfo).port}`, form)

expect(response.status).toStrictEqual(200)
t.equal(response.status, 200)

const json = await response.json()

expect(json.body.foo).toStrictEqual('bar')
expect(Array.isArray(json.body.file)).toStrictEqual(true)
expect(json.files.file).toBeDefined()
expect(json.files.file[0].name).toStrictEqual('package.json')
expect(json.files.file[1].name).toStrictEqual('package.json')
t.equal(json.body.foo, 'bar')
t.equal(Array.isArray(json.body.file), true)
t.ok(json.files.file)
t.equal(json.files.file[0].name, 'package.json')
t.equal(json.files.file[1].name, 'package.json')
})

t.test('non-multipart', async function (t) {
t.plan(3)

const fastify = await createFastify(t, { addHooks: true, removeFilesFromBody: true })

const response = await requestJSON(`http://localhost:${(fastify.server.address() as AddressInfo).port}`, {
foo: 'bar'
} as any)

t.equal(response.status, 200)

const json = await response.json()

await fastify.close()
t.equal(json.body.foo, 'bar')
t.equal(json.files, null)
})
})
Loading

0 comments on commit d4f86ef

Please sign in to comment.