Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to fs-capacitor v4 #166

Merged
merged 19 commits into from Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
node: ['8.5', '8', '10', '12']
node: ['8', '10', '12', '13']
steps:
- uses: actions/checkout@v1
- name: Setup Node.js v${{ matrix.node }}
Expand Down
3 changes: 0 additions & 3 deletions .nycrc.json

This file was deleted.

12 changes: 6 additions & 6 deletions package.json
Expand Up @@ -30,15 +30,15 @@
],
"main": "lib",
"engines": {
"node": ">=8.5"
"node": ">=8.10"
},
"browserslist": "node >= 8.5",
"browserslist": "node >= 8.10",
"peerDependencies": {
"graphql": "0.13.1 - 14"
},
"dependencies": {
"busboy": "^0.3.1",
"fs-capacitor": "^2.0.4",
"fs-capacitor": "^4.0.0",
"http-errors": "^1.7.3",
"object-path": "^0.11.4"
},
Expand All @@ -48,7 +48,7 @@
"@babel/preset-env": "^7.6.3",
"babel-eslint": "^10.0.3",
"eslint": "^6.5.1",
"eslint-config-env": "^9.1.0",
"eslint-config-env": "^11.0.0",
"eslint-config-prettier": "^6.4.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-import-order-alphabetical": "^1.0.0",
Expand All @@ -65,7 +65,7 @@
"lint-staged": "^9.4.2",
"node-fetch": "^2.6.0",
"prettier": "^1.18.2",
"tap": "^14.6.9"
"tap": "^14.9.2"
},
"scripts": {
"prepare": "npm run prepare:clean && npm run prepare:mjs && npm run prepare:js && npm run prepare:jsdoc && npm run prepare:prettier",
Expand All @@ -77,7 +77,7 @@
"test": "npm run test:eslint && npm run test:prettier && npm run test:tap",
"test:eslint": "eslint . --ext mjs,js",
"test:prettier": "prettier '**/*.{json,yml,md}' -l",
"test:tap": "tap --test-ignore=src --100",
"test:tap": "tap --test-ignore=src --100 --nyc-arg=--exclude='**/test-helpers/**'",
"prepublishOnly": "npm test"
}
}
28 changes: 17 additions & 11 deletions src/processRequest.mjs
@@ -1,12 +1,14 @@
import util from 'util'
import Busboy from 'busboy'
import { WriteStream } from 'fs-capacitor'
import fsCapacitor from 'fs-capacitor'
import createError from 'http-errors'
import objectPath from 'object-path'
import { SPEC_URL } from './constants'
import { ignoreStream } from './ignoreStream'
import { isEnumerableObject } from './isEnumerableObject'

const { WriteStream } = fsCapacitor

/**
* An expected file upload.
* @kind class
Expand Down Expand Up @@ -139,7 +141,7 @@ export const processRequest = (

if (map)
for (const upload of map.values())
if (upload.file) upload.file.capacitor.destroy()
if (upload.file) upload.file.capacitor.release()
}

/**
Expand Down Expand Up @@ -302,6 +304,7 @@ export const processRequest = (
return
}

let fileError
const capacitor = new WriteStream()

capacitor.on('error', () => {
Expand All @@ -310,29 +313,27 @@ export const processRequest = (
})

stream.on('limit', () => {
stream.unpipe()
capacitor.destroy(
createError(
413,
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
)
fileError = createError(
413,
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
)
stream.unpipe()
capacitor.destroy(fileError)
})

stream.on('error', error => {
fileError = error
stream.unpipe()
// istanbul ignore next
capacitor.destroy(exitError || error)
})

stream.pipe(capacitor)

const file = {
filename,
mimetype,
encoding,
createReadStream() {
const error = capacitor.error || (released ? exitError : null)
const error = fileError || (released ? exitError : null)
if (error) throw error
return capacitor.createReadStream()
}
Expand All @@ -348,6 +349,11 @@ export const processRequest = (

Object.defineProperty(file, 'capacitor', { value: capacitor })

capacitor.once('close', () =>
Object.defineProperty(file, 'closed', { value: true })
)

stream.pipe(capacitor)
upload.resolve(file)
})

Expand Down