graphql-upload 8.0.4 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +64 -0
- package/lib/constants.js +6 -0
- package/lib/constants.mjs +2 -0
- package/lib/graphqlUploadExpress.js +5 -2
- package/lib/graphqlUploadExpress.mjs +6 -3
- package/lib/graphqlUploadKoa.js +6 -3
- package/lib/graphqlUploadKoa.mjs +10 -3
- package/lib/ignoreStream.js +11 -0
- package/lib/ignoreStream.mjs +4 -0
- package/lib/index.js +5 -16
- package/lib/index.mjs +4 -4
- package/lib/isEnumerableObject.js +9 -0
- package/lib/isEnumerableObject.mjs +2 -0
- package/lib/processRequest.js +176 -112
- package/lib/processRequest.mjs +163 -102
- package/package.json +33 -56
- package/readme.md +70 -47
package/changelog.md
CHANGED
|
@@ -1,5 +1,69 @@
|
|
|
1
1
|
# graphql-upload changelog
|
|
2
2
|
|
|
3
|
+
## 8.1.0
|
|
4
|
+
|
|
5
|
+
### Minor
|
|
6
|
+
|
|
7
|
+
- `processRequest` now throws an appropriate error when a multipart field value exceeds the configured size limit, fixing [#159](https://github.com/jaydenseric/graphql-upload/issues/159).
|
|
8
|
+
- When the file size limit is exceeded, mention how many bytes the limit is in the stream error message.
|
|
9
|
+
- Added a new `processRequest` option to the `graphqlUploadExpress` and `graphqlUploadKoa` middleware, for improved testing without mocks or spies which are difficult to achieve with ESM.
|
|
10
|
+
|
|
11
|
+
### Patch
|
|
12
|
+
|
|
13
|
+
- Updated dependencies.
|
|
14
|
+
- Due to updated dependencies: Lint fixes, removed redundant `eslint-disable-next-line` comments, and regenerated the readme.
|
|
15
|
+
- Documented [`koa-graphql`](https://npm.im/koa-graphql) as known to be compatible, via [#156](https://github.com/jaydenseric/graphql-upload/pull/156).
|
|
16
|
+
- Fixed a readme typo, via [#161](https://github.com/jaydenseric/graphql-upload/pull/161).
|
|
17
|
+
- Use GitHub Actions instead of Travis for CI.
|
|
18
|
+
- Removed `package-lock.json` from `.gitignore` and `.prettierignore`, as it’s disabled in `.npmrc` anyway.
|
|
19
|
+
- New file structure.
|
|
20
|
+
- Explicitly defined main exports (instead of using `export * from`) to prevent accidental public exposure of internal APIs.
|
|
21
|
+
- Moved JSDoc typedefs into the index main entry file, alphabetically sorted.
|
|
22
|
+
- Nicer Browserslist query syntax.
|
|
23
|
+
- Replaced the `isObject` helper with a smarter and tested `isEnumerableObject`.
|
|
24
|
+
- Removed the `isString` helper.
|
|
25
|
+
- Enforced 100% code coverage for tests, and improved `processRequest` internals and tests (including a new test using vanilla Node.js HTTP), fixing [#130](https://github.com/jaydenseric/graphql-upload/issues/130) via [#162](https://github.com/jaydenseric/graphql-upload/pull/162).
|
|
26
|
+
- Removed a workaround from the `startServer` test helper.
|
|
27
|
+
- Added a new `ProcessRequestFunction` JSDoc type, and applied it to `processRequest`.
|
|
28
|
+
- Renamed the `UploadOptions` JSDoc type to `ProcessRequestOptions`.
|
|
29
|
+
- Misc. documentation improvements.
|
|
30
|
+
|
|
31
|
+
## 8.0.7
|
|
32
|
+
|
|
33
|
+
### Patch
|
|
34
|
+
|
|
35
|
+
- Updated dependencies.
|
|
36
|
+
- Handle invalid object paths in `map` multipart field entries, fixing [#154](https://github.com/jaydenseric/graphql-upload/issues/154).
|
|
37
|
+
- Import `WriteStream` from [`fs-capacitor`](https://npm.im/fs-capacitor) as a named rather than default import.
|
|
38
|
+
|
|
39
|
+
## 8.0.6
|
|
40
|
+
|
|
41
|
+
### Patch
|
|
42
|
+
|
|
43
|
+
- Updated dependencies.
|
|
44
|
+
- Allow batched operations again, fixing [#142](https://github.com/jaydenseric/graphql-upload/issues/142).
|
|
45
|
+
- Simplify tests by writing JSON as strings instead of using `JSON.stringify`.
|
|
46
|
+
- Use async middleware with [`express-async-handler`](https://npm.im/express-async-handler) for Express tests.
|
|
47
|
+
- Removed unintended `maxFiles` config in certain tests.
|
|
48
|
+
- Added the Open Graph image design to the logo Sketch file.
|
|
49
|
+
|
|
50
|
+
## 8.0.5
|
|
51
|
+
|
|
52
|
+
### Patch
|
|
53
|
+
|
|
54
|
+
- Updated dependencies.
|
|
55
|
+
- Handle invalid types in multipart fields and respond with meaningful HTTP 400 errors, via [#139](https://github.com/jaydenseric/graphql-upload/pull/139):
|
|
56
|
+
- Invalid `operations` type.
|
|
57
|
+
- Invalid `map` type.
|
|
58
|
+
- Invalid `map` entry type.
|
|
59
|
+
- Invalid `map` entry array item type.
|
|
60
|
+
- Additionally test current Node.js v8 and v10 versions with Travis.
|
|
61
|
+
- Reduced the size of the published `package.json` by moving dev tool config to files. This also prevents editor extensions such as Prettier and ESLint from detecting config and attempting to operate when opening package files installed in `node_modules`.
|
|
62
|
+
- Removed the [`watch`](https://npm.im/watch) dev dependency and `watch` script.
|
|
63
|
+
- Simplified the `prepublishOnly` script.
|
|
64
|
+
- Change to the `classic` TAP reporter for tests.
|
|
65
|
+
- Add [`apollo-server-koa`](https://npm.im/apollo-server-koa) and [`apollo-server-express`](https://npm.im/apollo-server-express) back to the compatible environments list in the readme, now that they use the current version of this package.
|
|
66
|
+
|
|
3
67
|
## 8.0.4
|
|
4
68
|
|
|
5
69
|
### Patch
|
package/lib/constants.js
ADDED
|
@@ -5,7 +5,10 @@ exports.graphqlUploadExpress = void 0
|
|
|
5
5
|
|
|
6
6
|
var _processRequest = require('./processRequest')
|
|
7
7
|
|
|
8
|
-
const graphqlUploadExpress =
|
|
8
|
+
const graphqlUploadExpress = ({
|
|
9
|
+
processRequest = _processRequest.processRequest,
|
|
10
|
+
...processRequestOptions
|
|
11
|
+
} = {}) => (request, response, next) => {
|
|
9
12
|
if (!request.is('multipart/form-data')) return next()
|
|
10
13
|
const finished = new Promise(resolve => request.on('end', resolve))
|
|
11
14
|
const { send } = response
|
|
@@ -17,7 +20,7 @@ const graphqlUploadExpress = options => (request, response, next) => {
|
|
|
17
20
|
})
|
|
18
21
|
}
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
processRequest(request, response, processRequestOptions)
|
|
21
24
|
.then(body => {
|
|
22
25
|
request.body = body
|
|
23
26
|
next()
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { processRequest } from './processRequest'
|
|
2
|
-
export const graphqlUploadExpress =
|
|
1
|
+
import { processRequest as defaultProcessRequest } from './processRequest'
|
|
2
|
+
export const graphqlUploadExpress = ({
|
|
3
|
+
processRequest = defaultProcessRequest,
|
|
4
|
+
...processRequestOptions
|
|
5
|
+
} = {}) => (request, response, next) => {
|
|
3
6
|
if (!request.is('multipart/form-data')) return next()
|
|
4
7
|
const finished = new Promise(resolve => request.on('end', resolve))
|
|
5
8
|
const { send } = response
|
|
@@ -11,7 +14,7 @@ export const graphqlUploadExpress = options => (request, response, next) => {
|
|
|
11
14
|
})
|
|
12
15
|
}
|
|
13
16
|
|
|
14
|
-
processRequest(request, response,
|
|
17
|
+
processRequest(request, response, processRequestOptions)
|
|
15
18
|
.then(body => {
|
|
16
19
|
request.body = body
|
|
17
20
|
next()
|
package/lib/graphqlUploadKoa.js
CHANGED
|
@@ -5,15 +5,18 @@ exports.graphqlUploadKoa = void 0
|
|
|
5
5
|
|
|
6
6
|
var _processRequest = require('./processRequest')
|
|
7
7
|
|
|
8
|
-
const graphqlUploadKoa =
|
|
8
|
+
const graphqlUploadKoa = ({
|
|
9
|
+
processRequest = _processRequest.processRequest,
|
|
10
|
+
...processRequestOptions
|
|
11
|
+
} = {}) => async (ctx, next) => {
|
|
9
12
|
if (!ctx.request.is('multipart/form-data')) return next()
|
|
10
13
|
const finished = new Promise(resolve => ctx.req.on('end', resolve))
|
|
11
14
|
|
|
12
15
|
try {
|
|
13
|
-
ctx.request.body = await
|
|
16
|
+
ctx.request.body = await processRequest(
|
|
14
17
|
ctx.req,
|
|
15
18
|
ctx.res,
|
|
16
|
-
|
|
19
|
+
processRequestOptions
|
|
17
20
|
)
|
|
18
21
|
await next()
|
|
19
22
|
} finally {
|
package/lib/graphqlUploadKoa.mjs
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import { processRequest } from './processRequest'
|
|
2
|
-
export const graphqlUploadKoa =
|
|
1
|
+
import { processRequest as defaultProcessRequest } from './processRequest'
|
|
2
|
+
export const graphqlUploadKoa = ({
|
|
3
|
+
processRequest = defaultProcessRequest,
|
|
4
|
+
...processRequestOptions
|
|
5
|
+
} = {}) => async (ctx, next) => {
|
|
3
6
|
if (!ctx.request.is('multipart/form-data')) return next()
|
|
4
7
|
const finished = new Promise(resolve => ctx.req.on('end', resolve))
|
|
5
8
|
|
|
6
9
|
try {
|
|
7
|
-
ctx.request.body = await processRequest(
|
|
10
|
+
ctx.request.body = await processRequest(
|
|
11
|
+
ctx.req,
|
|
12
|
+
ctx.res,
|
|
13
|
+
processRequestOptions
|
|
14
|
+
)
|
|
8
15
|
await next()
|
|
9
16
|
} finally {
|
|
10
17
|
await finished
|
package/lib/index.js
CHANGED
|
@@ -1,31 +1,20 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true
|
|
4
|
+
exports.graphqlUploadExpress = exports.graphqlUploadKoa = exports.processRequest = exports.GraphQLUpload = void 0
|
|
4
5
|
|
|
5
6
|
var _GraphQLUpload = require('./GraphQLUpload')
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
if (key === 'default' || key === '__esModule') return
|
|
9
|
-
exports[key] = _GraphQLUpload[key]
|
|
10
|
-
})
|
|
8
|
+
exports.GraphQLUpload = _GraphQLUpload.GraphQLUpload
|
|
11
9
|
|
|
12
10
|
var _processRequest = require('./processRequest')
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
if (key === 'default' || key === '__esModule') return
|
|
16
|
-
exports[key] = _processRequest[key]
|
|
17
|
-
})
|
|
12
|
+
exports.processRequest = _processRequest.processRequest
|
|
18
13
|
|
|
19
14
|
var _graphqlUploadKoa = require('./graphqlUploadKoa')
|
|
20
15
|
|
|
21
|
-
|
|
22
|
-
if (key === 'default' || key === '__esModule') return
|
|
23
|
-
exports[key] = _graphqlUploadKoa[key]
|
|
24
|
-
})
|
|
16
|
+
exports.graphqlUploadKoa = _graphqlUploadKoa.graphqlUploadKoa
|
|
25
17
|
|
|
26
18
|
var _graphqlUploadExpress = require('./graphqlUploadExpress')
|
|
27
19
|
|
|
28
|
-
|
|
29
|
-
if (key === 'default' || key === '__esModule') return
|
|
30
|
-
exports[key] = _graphqlUploadExpress[key]
|
|
31
|
-
})
|
|
20
|
+
exports.graphqlUploadExpress = _graphqlUploadExpress.graphqlUploadExpress
|
package/lib/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
1
|
+
export { GraphQLUpload } from './GraphQLUpload'
|
|
2
|
+
export { processRequest } from './processRequest'
|
|
3
|
+
export { graphqlUploadKoa } from './graphqlUploadKoa'
|
|
4
|
+
export { graphqlUploadExpress } from './graphqlUploadExpress'
|
package/lib/processRequest.js
CHANGED
|
@@ -7,18 +7,23 @@ var _util = _interopRequireDefault(require('util'))
|
|
|
7
7
|
|
|
8
8
|
var _busboy = _interopRequireDefault(require('busboy'))
|
|
9
9
|
|
|
10
|
-
var _fsCapacitor =
|
|
10
|
+
var _fsCapacitor = require('fs-capacitor')
|
|
11
11
|
|
|
12
12
|
var _httpErrors = _interopRequireDefault(require('http-errors'))
|
|
13
13
|
|
|
14
14
|
var _objectPath = _interopRequireDefault(require('object-path'))
|
|
15
15
|
|
|
16
|
+
var _constants = require('./constants')
|
|
17
|
+
|
|
18
|
+
var _ignoreStream = require('./ignoreStream')
|
|
19
|
+
|
|
20
|
+
var _isEnumerableObject = require('./isEnumerableObject')
|
|
21
|
+
|
|
22
|
+
// istanbul ignore next
|
|
16
23
|
function _interopRequireDefault(obj) {
|
|
17
24
|
return obj && obj.__esModule ? obj : { default: obj }
|
|
18
25
|
}
|
|
19
26
|
|
|
20
|
-
const SPEC_URL = 'https://github.com/jaydenseric/graphql-multipart-request-spec'
|
|
21
|
-
|
|
22
27
|
class Upload {
|
|
23
28
|
constructor() {
|
|
24
29
|
this.promise = new Promise((resolve, reject) => {
|
|
@@ -39,8 +44,7 @@ const processRequest = (
|
|
|
39
44
|
{ maxFieldSize = 1000000, maxFileSize = Infinity, maxFiles = Infinity } = {}
|
|
40
45
|
) =>
|
|
41
46
|
new Promise((resolve, reject) => {
|
|
42
|
-
let
|
|
43
|
-
let released = false
|
|
47
|
+
let released
|
|
44
48
|
let exitError
|
|
45
49
|
let currentStream
|
|
46
50
|
let operations
|
|
@@ -72,6 +76,7 @@ const processRequest = (
|
|
|
72
76
|
}
|
|
73
77
|
|
|
74
78
|
const release = () => {
|
|
79
|
+
// istanbul ignore next
|
|
75
80
|
if (released) return
|
|
76
81
|
released = true
|
|
77
82
|
if (map)
|
|
@@ -79,130 +84,197 @@ const processRequest = (
|
|
|
79
84
|
if (upload.file) upload.file.capacitor.destroy()
|
|
80
85
|
}
|
|
81
86
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
(0, _httpErrors.default)(
|
|
91
|
-
400,
|
|
92
|
-
`Invalid JSON in the ‘operations’ multipart field (${SPEC_URL}).`
|
|
93
|
-
)
|
|
94
|
-
)
|
|
95
|
-
}
|
|
87
|
+
const abort = () => {
|
|
88
|
+
exit(
|
|
89
|
+
(0, _httpErrors.default)(
|
|
90
|
+
499,
|
|
91
|
+
'Request disconnected during file upload stream parsing.'
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
}
|
|
96
95
|
|
|
97
|
-
|
|
96
|
+
parser.on(
|
|
97
|
+
'field',
|
|
98
|
+
(fieldName, value, fieldNameTruncated, valueTruncated) => {
|
|
99
|
+
if (exitError) return
|
|
100
|
+
if (valueTruncated)
|
|
101
|
+
return exit(
|
|
102
|
+
(0, _httpErrors.default)(
|
|
103
|
+
413,
|
|
104
|
+
`The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
|
|
105
|
+
)
|
|
106
|
+
)
|
|
98
107
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
108
|
+
switch (fieldName) {
|
|
109
|
+
case 'operations':
|
|
110
|
+
try {
|
|
111
|
+
operations = JSON.parse(value)
|
|
112
|
+
} catch (error) {
|
|
113
|
+
return exit(
|
|
114
|
+
(0, _httpErrors.default)(
|
|
115
|
+
400,
|
|
116
|
+
`Invalid JSON in the ‘operations’ multipart field (${_constants.SPEC_URL}).`
|
|
117
|
+
)
|
|
105
118
|
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (
|
|
122
|
+
!(0, _isEnumerableObject.isEnumerableObject)(operations) &&
|
|
123
|
+
!Array.isArray(operations)
|
|
106
124
|
)
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return exit(
|
|
113
|
-
(0, _httpErrors.default)(
|
|
114
|
-
400,
|
|
115
|
-
`Invalid JSON in the ‘map’ multipart field (${SPEC_URL}).`
|
|
125
|
+
return exit(
|
|
126
|
+
(0, _httpErrors.default)(
|
|
127
|
+
400,
|
|
128
|
+
`Invalid type for the ‘operations’ multipart field (${_constants.SPEC_URL}).`
|
|
129
|
+
)
|
|
116
130
|
)
|
|
117
|
-
)
|
|
118
|
-
|
|
131
|
+
operationsPath = (0, _objectPath.default)(operations)
|
|
132
|
+
break
|
|
119
133
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
134
|
+
case 'map': {
|
|
135
|
+
if (!operations)
|
|
136
|
+
return exit(
|
|
137
|
+
(0, _httpErrors.default)(
|
|
138
|
+
400,
|
|
139
|
+
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${_constants.SPEC_URL}).`
|
|
140
|
+
)
|
|
125
141
|
)
|
|
126
|
-
|
|
127
|
-
map = new Map()
|
|
142
|
+
let parsedMap
|
|
128
143
|
|
|
129
|
-
|
|
130
|
-
|
|
144
|
+
try {
|
|
145
|
+
parsedMap = JSON.parse(value)
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return exit(
|
|
148
|
+
(0, _httpErrors.default)(
|
|
149
|
+
400,
|
|
150
|
+
`Invalid JSON in the ‘map’ multipart field (${_constants.SPEC_URL}).`
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
}
|
|
131
154
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
155
|
+
if (!(0, _isEnumerableObject.isEnumerableObject)(parsedMap))
|
|
156
|
+
return exit(
|
|
157
|
+
(0, _httpErrors.default)(
|
|
158
|
+
400,
|
|
159
|
+
`Invalid type for the ‘map’ multipart field (${_constants.SPEC_URL}).`
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
const mapEntries = Object.entries(parsedMap)
|
|
163
|
+
if (mapEntries.length > maxFiles)
|
|
164
|
+
return exit(
|
|
165
|
+
(0, _httpErrors.default)(
|
|
166
|
+
413,
|
|
167
|
+
`${maxFiles} max file uploads exceeded.`
|
|
168
|
+
)
|
|
169
|
+
)
|
|
170
|
+
map = new Map()
|
|
171
|
+
|
|
172
|
+
for (const [fieldName, paths] of mapEntries) {
|
|
173
|
+
if (!Array.isArray(paths))
|
|
174
|
+
return exit(
|
|
175
|
+
(0, _httpErrors.default)(
|
|
176
|
+
400,
|
|
177
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${_constants.SPEC_URL}).`
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
map.set(fieldName, new Upload())
|
|
181
|
+
|
|
182
|
+
for (const [index, path] of paths.entries()) {
|
|
183
|
+
if (typeof path !== 'string')
|
|
184
|
+
return exit(
|
|
185
|
+
(0, _httpErrors.default)(
|
|
186
|
+
400,
|
|
187
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${_constants.SPEC_URL}).`
|
|
188
|
+
)
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
operationsPath.set(path, map.get(fieldName).promise)
|
|
193
|
+
} catch (error) {
|
|
194
|
+
return exit(
|
|
195
|
+
(0, _httpErrors.default)(
|
|
196
|
+
400,
|
|
197
|
+
`Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${_constants.SPEC_URL}).`
|
|
198
|
+
)
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
135
203
|
|
|
136
|
-
|
|
204
|
+
resolve(operations)
|
|
205
|
+
}
|
|
137
206
|
}
|
|
138
207
|
}
|
|
139
|
-
|
|
208
|
+
)
|
|
140
209
|
parser.on('file', (fieldName, stream, filename, encoding, mimetype) => {
|
|
210
|
+
if (exitError) {
|
|
211
|
+
;(0, _ignoreStream.ignoreStream)(stream)
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
|
|
141
215
|
if (!map) {
|
|
142
|
-
|
|
143
|
-
stream.resume()
|
|
216
|
+
;(0, _ignoreStream.ignoreStream)(stream)
|
|
144
217
|
return exit(
|
|
145
218
|
(0, _httpErrors.default)(
|
|
146
219
|
400,
|
|
147
|
-
`Misordered multipart fields; files should follow ‘map’ (${SPEC_URL}).`
|
|
220
|
+
`Misordered multipart fields; files should follow ‘map’ (${_constants.SPEC_URL}).`
|
|
148
221
|
)
|
|
149
222
|
)
|
|
150
223
|
}
|
|
151
224
|
|
|
152
225
|
currentStream = stream
|
|
153
226
|
stream.on('end', () => {
|
|
154
|
-
|
|
227
|
+
currentStream = null
|
|
155
228
|
})
|
|
156
229
|
const upload = map.get(fieldName)
|
|
157
230
|
|
|
158
|
-
if (upload) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
231
|
+
if (!upload) {
|
|
232
|
+
;(0, _ignoreStream.ignoreStream)(stream)
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const capacitor = new _fsCapacitor.WriteStream()
|
|
237
|
+
capacitor.on('error', () => {
|
|
238
|
+
stream.unpipe()
|
|
239
|
+
stream.resume()
|
|
240
|
+
})
|
|
241
|
+
stream.on('limit', () => {
|
|
242
|
+
stream.unpipe()
|
|
243
|
+
capacitor.destroy(
|
|
244
|
+
(0, _httpErrors.default)(
|
|
245
|
+
413,
|
|
246
|
+
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
|
|
172
247
|
)
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
248
|
+
)
|
|
249
|
+
})
|
|
250
|
+
stream.on('error', error => {
|
|
251
|
+
stream.unpipe() // istanbul ignore next
|
|
252
|
+
|
|
253
|
+
capacitor.destroy(exitError || error)
|
|
254
|
+
})
|
|
255
|
+
stream.pipe(capacitor)
|
|
256
|
+
const file = {
|
|
257
|
+
filename,
|
|
258
|
+
mimetype,
|
|
259
|
+
encoding,
|
|
260
|
+
|
|
261
|
+
createReadStream() {
|
|
262
|
+
const error = capacitor.error || (released ? exitError : null)
|
|
263
|
+
if (error) throw error
|
|
264
|
+
return capacitor.createReadStream()
|
|
190
265
|
}
|
|
191
|
-
let capacitorStream
|
|
192
|
-
Object.defineProperty(file, 'stream', {
|
|
193
|
-
get: _util.default.deprecate(function() {
|
|
194
|
-
if (!capacitorStream) capacitorStream = this.createReadStream()
|
|
195
|
-
return capacitorStream
|
|
196
|
-
}, 'File upload property ‘stream’ is deprecated. Use ‘createReadStream()’ instead.')
|
|
197
|
-
})
|
|
198
|
-
Object.defineProperty(file, 'capacitor', {
|
|
199
|
-
value: capacitor
|
|
200
|
-
})
|
|
201
|
-
upload.resolve(file)
|
|
202
|
-
} else {
|
|
203
|
-
stream.on('error', () => {})
|
|
204
|
-
stream.resume()
|
|
205
266
|
}
|
|
267
|
+
let capacitorStream
|
|
268
|
+
Object.defineProperty(file, 'stream', {
|
|
269
|
+
get: _util.default.deprecate(function() {
|
|
270
|
+
if (!capacitorStream) capacitorStream = this.createReadStream()
|
|
271
|
+
return capacitorStream
|
|
272
|
+
}, 'File upload property ‘stream’ is deprecated. Use ‘createReadStream()’ instead.')
|
|
273
|
+
})
|
|
274
|
+
Object.defineProperty(file, 'capacitor', {
|
|
275
|
+
value: capacitor
|
|
276
|
+
})
|
|
277
|
+
upload.resolve(file)
|
|
206
278
|
})
|
|
207
279
|
parser.once('filesLimit', () =>
|
|
208
280
|
exit(
|
|
@@ -216,14 +288,14 @@ const processRequest = (
|
|
|
216
288
|
return exit(
|
|
217
289
|
(0, _httpErrors.default)(
|
|
218
290
|
400,
|
|
219
|
-
`Missing multipart field ‘operations’ (${SPEC_URL}).`
|
|
291
|
+
`Missing multipart field ‘operations’ (${_constants.SPEC_URL}).`
|
|
220
292
|
)
|
|
221
293
|
)
|
|
222
294
|
if (!map)
|
|
223
295
|
return exit(
|
|
224
296
|
(0, _httpErrors.default)(
|
|
225
297
|
400,
|
|
226
|
-
`Missing multipart field ‘map’ (${SPEC_URL}).`
|
|
298
|
+
`Missing multipart field ‘map’ (${_constants.SPEC_URL}).`
|
|
227
299
|
)
|
|
228
300
|
)
|
|
229
301
|
|
|
@@ -236,17 +308,9 @@ const processRequest = (
|
|
|
236
308
|
parser.once('error', exit)
|
|
237
309
|
response.once('finish', release)
|
|
238
310
|
response.once('close', release)
|
|
311
|
+
request.once('close', abort)
|
|
239
312
|
request.once('end', () => {
|
|
240
|
-
|
|
241
|
-
})
|
|
242
|
-
request.once('close', () => {
|
|
243
|
-
if (!requestEnded)
|
|
244
|
-
exit(
|
|
245
|
-
(0, _httpErrors.default)(
|
|
246
|
-
499,
|
|
247
|
-
'Request disconnected during file upload stream parsing.'
|
|
248
|
-
)
|
|
249
|
-
)
|
|
313
|
+
request.removeListener('close', abort)
|
|
250
314
|
})
|
|
251
315
|
request.pipe(parser)
|
|
252
316
|
})
|
package/lib/processRequest.mjs
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import util from 'util'
|
|
2
2
|
import Busboy from 'busboy'
|
|
3
|
-
import WriteStream from 'fs-capacitor'
|
|
3
|
+
import { WriteStream } from 'fs-capacitor'
|
|
4
4
|
import createError from 'http-errors'
|
|
5
5
|
import objectPath from 'object-path'
|
|
6
|
-
|
|
6
|
+
import { SPEC_URL } from './constants'
|
|
7
|
+
import { ignoreStream } from './ignoreStream'
|
|
8
|
+
import { isEnumerableObject } from './isEnumerableObject'
|
|
7
9
|
|
|
8
10
|
class Upload {
|
|
9
11
|
constructor() {
|
|
@@ -25,8 +27,7 @@ export const processRequest = (
|
|
|
25
27
|
{ maxFieldSize = 1000000, maxFileSize = Infinity, maxFiles = Infinity } = {}
|
|
26
28
|
) =>
|
|
27
29
|
new Promise((resolve, reject) => {
|
|
28
|
-
let
|
|
29
|
-
let released = false
|
|
30
|
+
let released
|
|
30
31
|
let exitError
|
|
31
32
|
let currentStream
|
|
32
33
|
let operations
|
|
@@ -58,6 +59,7 @@ export const processRequest = (
|
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
const release = () => {
|
|
62
|
+
// istanbul ignore next
|
|
61
63
|
if (released) return
|
|
62
64
|
released = true
|
|
63
65
|
if (map)
|
|
@@ -65,65 +67,130 @@ export const processRequest = (
|
|
|
65
67
|
if (upload.file) upload.file.capacitor.destroy()
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
const abort = () => {
|
|
71
|
+
exit(
|
|
72
|
+
createError(
|
|
73
|
+
499,
|
|
74
|
+
'Request disconnected during file upload stream parsing.'
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
parser.on(
|
|
80
|
+
'field',
|
|
81
|
+
(fieldName, value, fieldNameTruncated, valueTruncated) => {
|
|
82
|
+
if (exitError) return
|
|
83
|
+
if (valueTruncated)
|
|
84
|
+
return exit(
|
|
85
|
+
createError(
|
|
86
|
+
413,
|
|
87
|
+
`The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
|
|
88
|
+
)
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
switch (fieldName) {
|
|
92
|
+
case 'operations':
|
|
93
|
+
try {
|
|
94
|
+
operations = JSON.parse(value)
|
|
95
|
+
} catch (error) {
|
|
96
|
+
return exit(
|
|
97
|
+
createError(
|
|
98
|
+
400,
|
|
99
|
+
`Invalid JSON in the ‘operations’ multipart field (${SPEC_URL}).`
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!isEnumerableObject(operations) && !Array.isArray(operations))
|
|
105
|
+
return exit(
|
|
106
|
+
createError(
|
|
107
|
+
400,
|
|
108
|
+
`Invalid type for the ‘operations’ multipart field (${SPEC_URL}).`
|
|
109
|
+
)
|
|
110
|
+
)
|
|
73
111
|
operationsPath = objectPath(operations)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
112
|
+
break
|
|
113
|
+
|
|
114
|
+
case 'map': {
|
|
115
|
+
if (!operations)
|
|
116
|
+
return exit(
|
|
117
|
+
createError(
|
|
118
|
+
400,
|
|
119
|
+
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${SPEC_URL}).`
|
|
120
|
+
)
|
|
79
121
|
)
|
|
80
|
-
|
|
81
|
-
}
|
|
122
|
+
let parsedMap
|
|
82
123
|
|
|
83
|
-
|
|
124
|
+
try {
|
|
125
|
+
parsedMap = JSON.parse(value)
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return exit(
|
|
128
|
+
createError(
|
|
129
|
+
400,
|
|
130
|
+
`Invalid JSON in the ‘map’ multipart field (${SPEC_URL}).`
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
}
|
|
84
134
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
135
|
+
if (!isEnumerableObject(parsedMap))
|
|
136
|
+
return exit(
|
|
137
|
+
createError(
|
|
138
|
+
400,
|
|
139
|
+
`Invalid type for the ‘map’ multipart field (${SPEC_URL}).`
|
|
140
|
+
)
|
|
91
141
|
)
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
mapEntries = Object.entries(JSON.parse(value))
|
|
97
|
-
} catch (error) {
|
|
98
|
-
return exit(
|
|
99
|
-
createError(
|
|
100
|
-
400,
|
|
101
|
-
`Invalid JSON in the ‘map’ multipart field (${SPEC_URL}).`
|
|
142
|
+
const mapEntries = Object.entries(parsedMap)
|
|
143
|
+
if (mapEntries.length > maxFiles)
|
|
144
|
+
return exit(
|
|
145
|
+
createError(413, `${maxFiles} max file uploads exceeded.`)
|
|
102
146
|
)
|
|
103
|
-
)
|
|
104
|
-
}
|
|
147
|
+
map = new Map()
|
|
105
148
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
149
|
+
for (const [fieldName, paths] of mapEntries) {
|
|
150
|
+
if (!Array.isArray(paths))
|
|
151
|
+
return exit(
|
|
152
|
+
createError(
|
|
153
|
+
400,
|
|
154
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${SPEC_URL}).`
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
map.set(fieldName, new Upload())
|
|
111
158
|
|
|
112
|
-
|
|
113
|
-
|
|
159
|
+
for (const [index, path] of paths.entries()) {
|
|
160
|
+
if (typeof path !== 'string')
|
|
161
|
+
return exit(
|
|
162
|
+
createError(
|
|
163
|
+
400,
|
|
164
|
+
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${SPEC_URL}).`
|
|
165
|
+
)
|
|
166
|
+
)
|
|
114
167
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
168
|
+
try {
|
|
169
|
+
operationsPath.set(path, map.get(fieldName).promise)
|
|
170
|
+
} catch (error) {
|
|
171
|
+
return exit(
|
|
172
|
+
createError(
|
|
173
|
+
400,
|
|
174
|
+
`Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${SPEC_URL}).`
|
|
175
|
+
)
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
118
180
|
|
|
119
|
-
|
|
181
|
+
resolve(operations)
|
|
182
|
+
}
|
|
120
183
|
}
|
|
121
184
|
}
|
|
122
|
-
|
|
185
|
+
)
|
|
123
186
|
parser.on('file', (fieldName, stream, filename, encoding, mimetype) => {
|
|
187
|
+
if (exitError) {
|
|
188
|
+
ignoreStream(stream)
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
|
|
124
192
|
if (!map) {
|
|
125
|
-
stream
|
|
126
|
-
stream.resume()
|
|
193
|
+
ignoreStream(stream)
|
|
127
194
|
return exit(
|
|
128
195
|
createError(
|
|
129
196
|
400,
|
|
@@ -134,55 +201,57 @@ export const processRequest = (
|
|
|
134
201
|
|
|
135
202
|
currentStream = stream
|
|
136
203
|
stream.on('end', () => {
|
|
137
|
-
|
|
204
|
+
currentStream = null
|
|
138
205
|
})
|
|
139
206
|
const upload = map.get(fieldName)
|
|
140
207
|
|
|
141
|
-
if (upload) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
208
|
+
if (!upload) {
|
|
209
|
+
ignoreStream(stream)
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const capacitor = new WriteStream()
|
|
214
|
+
capacitor.on('error', () => {
|
|
215
|
+
stream.unpipe()
|
|
216
|
+
stream.resume()
|
|
217
|
+
})
|
|
218
|
+
stream.on('limit', () => {
|
|
219
|
+
stream.unpipe()
|
|
220
|
+
capacitor.destroy(
|
|
221
|
+
createError(
|
|
222
|
+
413,
|
|
223
|
+
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
|
|
152
224
|
)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
225
|
+
)
|
|
226
|
+
})
|
|
227
|
+
stream.on('error', error => {
|
|
228
|
+
stream.unpipe() // istanbul ignore next
|
|
229
|
+
|
|
230
|
+
capacitor.destroy(exitError || error)
|
|
231
|
+
})
|
|
232
|
+
stream.pipe(capacitor)
|
|
233
|
+
const file = {
|
|
234
|
+
filename,
|
|
235
|
+
mimetype,
|
|
236
|
+
encoding,
|
|
237
|
+
|
|
238
|
+
createReadStream() {
|
|
239
|
+
const error = capacitor.error || (released ? exitError : null)
|
|
240
|
+
if (error) throw error
|
|
241
|
+
return capacitor.createReadStream()
|
|
170
242
|
}
|
|
171
|
-
let capacitorStream
|
|
172
|
-
Object.defineProperty(file, 'stream', {
|
|
173
|
-
get: util.deprecate(function() {
|
|
174
|
-
if (!capacitorStream) capacitorStream = this.createReadStream()
|
|
175
|
-
return capacitorStream
|
|
176
|
-
}, 'File upload property ‘stream’ is deprecated. Use ‘createReadStream()’ instead.')
|
|
177
|
-
})
|
|
178
|
-
Object.defineProperty(file, 'capacitor', {
|
|
179
|
-
value: capacitor
|
|
180
|
-
})
|
|
181
|
-
upload.resolve(file)
|
|
182
|
-
} else {
|
|
183
|
-
stream.on('error', () => {})
|
|
184
|
-
stream.resume()
|
|
185
243
|
}
|
|
244
|
+
let capacitorStream
|
|
245
|
+
Object.defineProperty(file, 'stream', {
|
|
246
|
+
get: util.deprecate(function() {
|
|
247
|
+
if (!capacitorStream) capacitorStream = this.createReadStream()
|
|
248
|
+
return capacitorStream
|
|
249
|
+
}, 'File upload property ‘stream’ is deprecated. Use ‘createReadStream()’ instead.')
|
|
250
|
+
})
|
|
251
|
+
Object.defineProperty(file, 'capacitor', {
|
|
252
|
+
value: capacitor
|
|
253
|
+
})
|
|
254
|
+
upload.resolve(file)
|
|
186
255
|
})
|
|
187
256
|
parser.once('filesLimit', () =>
|
|
188
257
|
exit(createError(413, `${maxFiles} max file uploads exceeded.`))
|
|
@@ -209,17 +278,9 @@ export const processRequest = (
|
|
|
209
278
|
parser.once('error', exit)
|
|
210
279
|
response.once('finish', release)
|
|
211
280
|
response.once('close', release)
|
|
281
|
+
request.once('close', abort)
|
|
212
282
|
request.once('end', () => {
|
|
213
|
-
|
|
214
|
-
})
|
|
215
|
-
request.once('close', () => {
|
|
216
|
-
if (!requestEnded)
|
|
217
|
-
exit(
|
|
218
|
-
createError(
|
|
219
|
-
499,
|
|
220
|
-
'Request disconnected during file upload stream parsing.'
|
|
221
|
-
)
|
|
222
|
-
)
|
|
283
|
+
request.removeListener('close', abort)
|
|
223
284
|
})
|
|
224
285
|
request.pipe(parser)
|
|
225
286
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-upload",
|
|
3
|
-
"version": "8.0
|
|
3
|
+
"version": "8.1.0",
|
|
4
4
|
"description": "Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -25,45 +25,47 @@
|
|
|
25
25
|
],
|
|
26
26
|
"files": [
|
|
27
27
|
"lib",
|
|
28
|
-
"
|
|
28
|
+
"!*.test.*",
|
|
29
|
+
"!test-helpers"
|
|
29
30
|
],
|
|
30
31
|
"main": "lib",
|
|
31
32
|
"engines": {
|
|
32
33
|
"node": ">=8.5"
|
|
33
34
|
},
|
|
34
|
-
"browserslist": "node 8.5",
|
|
35
|
+
"browserslist": "node >= 8.5",
|
|
35
36
|
"peerDependencies": {
|
|
36
37
|
"graphql": "0.13.1 - 14"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
|
-
"busboy": "^0.3.
|
|
40
|
-
"fs-capacitor": "^2.0.
|
|
41
|
-
"http-errors": "^1.7.
|
|
40
|
+
"busboy": "^0.3.1",
|
|
41
|
+
"fs-capacitor": "^2.0.4",
|
|
42
|
+
"http-errors": "^1.7.3",
|
|
42
43
|
"object-path": "^0.11.4"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
|
-
"@babel/cli": "^7.
|
|
46
|
-
"@babel/core": "^7.
|
|
47
|
-
"@babel/preset-env": "^7.
|
|
48
|
-
"babel-eslint": "^10.0.
|
|
49
|
-
"eslint": "^5.
|
|
50
|
-
"eslint-config-env": "^
|
|
51
|
-
"eslint-config-prettier": "^
|
|
52
|
-
"eslint-plugin-import": "^2.
|
|
53
|
-
"eslint-plugin-import-order-alphabetical": "0.0
|
|
54
|
-
"eslint-plugin-
|
|
55
|
-
"eslint-plugin-
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
46
|
+
"@babel/cli": "^7.6.3",
|
|
47
|
+
"@babel/core": "^7.6.3",
|
|
48
|
+
"@babel/preset-env": "^7.6.3",
|
|
49
|
+
"babel-eslint": "^10.0.3",
|
|
50
|
+
"eslint": "^6.5.1",
|
|
51
|
+
"eslint-config-env": "^9.1.0",
|
|
52
|
+
"eslint-config-prettier": "^6.4.0",
|
|
53
|
+
"eslint-plugin-import": "^2.18.2",
|
|
54
|
+
"eslint-plugin-import-order-alphabetical": "^1.0.0",
|
|
55
|
+
"eslint-plugin-jsdoc": "^15.9.10",
|
|
56
|
+
"eslint-plugin-node": "^10.0.0",
|
|
57
|
+
"eslint-plugin-prettier": "^3.1.1",
|
|
58
|
+
"express": "^4.17.1",
|
|
59
|
+
"express-async-handler": "^1.1.4",
|
|
60
|
+
"form-data": "^2.5.1",
|
|
61
|
+
"graphql": "^14.5.8",
|
|
62
|
+
"husky": "^3.0.8",
|
|
63
|
+
"jsdoc-md": "^4.0.1",
|
|
64
|
+
"koa": "^2.8.2",
|
|
65
|
+
"lint-staged": "^9.4.2",
|
|
66
|
+
"node-fetch": "^2.6.0",
|
|
67
|
+
"prettier": "^1.18.2",
|
|
68
|
+
"tap": "^14.6.9"
|
|
67
69
|
},
|
|
68
70
|
"scripts": {
|
|
69
71
|
"prepare": "npm run prepare:clean && npm run prepare:mjs && npm run prepare:js && npm run prepare:jsdoc && npm run prepare:prettier",
|
|
@@ -72,35 +74,10 @@
|
|
|
72
74
|
"prepare:js": "babel src -d lib",
|
|
73
75
|
"prepare:jsdoc": "jsdoc-md",
|
|
74
76
|
"prepare:prettier": "prettier 'lib/**/*.{mjs,js}' readme.md --write",
|
|
75
|
-
"test": "npm run test:eslint && npm run test:prettier && npm run test:
|
|
77
|
+
"test": "npm run test:eslint && npm run test:prettier && npm run test:tap",
|
|
76
78
|
"test:eslint": "eslint . --ext mjs,js",
|
|
77
79
|
"test:prettier": "prettier '**/*.{json,yml,md}' -l",
|
|
78
|
-
"test:
|
|
79
|
-
"
|
|
80
|
-
"prepublishOnly": "npm run prepare && npm test",
|
|
81
|
-
"watch": "watch 'npm run prepublishOnly --silent' src --interval 1"
|
|
82
|
-
},
|
|
83
|
-
"husky": {
|
|
84
|
-
"hooks": {
|
|
85
|
-
"pre-commit": "lint-staged"
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
"lint-staged": {
|
|
89
|
-
"*.{mjs,js}": "eslint",
|
|
90
|
-
"*.{json,yml,md}": "prettier -l"
|
|
91
|
-
},
|
|
92
|
-
"eslintIgnore": [
|
|
93
|
-
"lib",
|
|
94
|
-
"tap-snapshots"
|
|
95
|
-
],
|
|
96
|
-
"eslintConfig": {
|
|
97
|
-
"extends": [
|
|
98
|
-
"env"
|
|
99
|
-
]
|
|
100
|
-
},
|
|
101
|
-
"prettier": {
|
|
102
|
-
"proseWrap": "never",
|
|
103
|
-
"singleQuote": true,
|
|
104
|
-
"semi": false
|
|
80
|
+
"test:tap": "tap --test-ignore=src --100",
|
|
81
|
+
"prepublishOnly": "npm test"
|
|
105
82
|
}
|
|
106
83
|
}
|
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# graphql-upload
|
|
4
4
|
|
|
5
|
-
[](https://npm.im/graphql-upload) [](https://npm.im/graphql-upload) [](https://github.com/jaydenseric/graphql-upload/actions)
|
|
6
6
|
|
|
7
7
|
Middleware and an [`Upload` scalar](#class-graphqlupload) to add support for [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) (file uploads via queries and mutations) to various Node.js GraphQL servers.
|
|
8
8
|
|
|
@@ -17,14 +17,17 @@ The following environments are known to be compatible:
|
|
|
17
17
|
- Native ESM with [`--experimental-modules`](https://nodejs.org/api/esm.html#esm_enabling)
|
|
18
18
|
- [Koa](https://koajs.com)
|
|
19
19
|
- [`graphql-api-koa`](https://npm.im/graphql-api-koa)
|
|
20
|
+
- [`koa-graphql`](https://npm.im/koa-graphql)
|
|
21
|
+
- [`apollo-server-koa`](https://npm.im/apollo-server-koa) (inbuilt)
|
|
20
22
|
- [Express](https://expressjs.com)
|
|
21
23
|
- [`express-graphql`](https://npm.im/express-graphql)
|
|
24
|
+
- [`apollo-server-express`](https://npm.im/apollo-server-express) (inbuilt)
|
|
22
25
|
|
|
23
26
|
See also [GraphQL multipart request spec server implementations](https://github.com/jaydenseric/graphql-multipart-request-spec#server).
|
|
24
27
|
|
|
25
28
|
## Setup
|
|
26
29
|
|
|
27
|
-
Setup is necessary if your environment doesn’t feature this package
|
|
30
|
+
Setup is necessary if your environment doesn’t feature this package inbuilt (see **_[Support](#support)_**).
|
|
28
31
|
|
|
29
32
|
To install [`graphql-upload`](https://npm.im/graphql-upload) and the [`graphql`](https://npm.im/graphql) peer dependency from [npm](https://npmjs.com) run:
|
|
30
33
|
|
|
@@ -65,17 +68,13 @@ The [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-mult
|
|
|
65
68
|
### Table of contents
|
|
66
69
|
|
|
67
70
|
- [class GraphQLUpload](#class-graphqlupload)
|
|
68
|
-
- [Examples](#examples)
|
|
69
71
|
- [function graphqlUploadExpress](#function-graphqluploadexpress)
|
|
70
|
-
- [Examples](#examples-1)
|
|
71
72
|
- [function graphqlUploadKoa](#function-graphqluploadkoa)
|
|
72
|
-
- [Examples](#examples-2)
|
|
73
73
|
- [function processRequest](#function-processrequest)
|
|
74
|
-
- [Examples](#examples-3)
|
|
75
74
|
- [type FileUpload](#type-fileupload)
|
|
76
75
|
- [type GraphQLOperation](#type-graphqloperation)
|
|
77
|
-
|
|
78
|
-
- [type
|
|
76
|
+
- [type ProcessRequestFunction](#type-processrequestfunction)
|
|
77
|
+
- [type ProcessRequestOptions](#type-processrequestoptions)
|
|
79
78
|
|
|
80
79
|
### class GraphQLUpload
|
|
81
80
|
|
|
@@ -131,15 +130,18 @@ _A manually constructed schema with an image upload mutation._
|
|
|
131
130
|
> })
|
|
132
131
|
> ```
|
|
133
132
|
|
|
133
|
+
---
|
|
134
|
+
|
|
134
135
|
### function graphqlUploadExpress
|
|
135
136
|
|
|
136
|
-
Creates [Express](https://expressjs.com) middleware that processes GraphQL multipart requests using [`processRequest`](#function-processrequest), ignoring non-multipart requests. It sets the request body to be [similar to a conventional GraphQL POST request](#type-graphqloperation) for following GraphQL middleware to consume.
|
|
137
|
+
Creates [Express](https://expressjs.com) middleware that processes [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) using [`processRequest`](#function-processrequest), ignoring non-multipart requests. It sets the request body to be [similar to a conventional GraphQL POST request](#type-graphqloperation) for following GraphQL middleware to consume.
|
|
137
138
|
|
|
138
|
-
| Parameter | Type
|
|
139
|
-
|
|
|
140
|
-
| `options` | [
|
|
139
|
+
| Parameter | Type | Description |
|
|
140
|
+
| :-- | :-- | :-- |
|
|
141
|
+
| `options` | [ProcessRequestOptions](#type-processrequestoptions) | Middleware options. Any [`ProcessRequestOptions`](#type-processrequestoptions) can be used. |
|
|
142
|
+
| `options.processRequest` | [ProcessRequestFunction](#type-processrequestfunction)? = [processRequest](#function-processrequest) | Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec). |
|
|
141
143
|
|
|
142
|
-
**Returns:**
|
|
144
|
+
**Returns:** Function — Express middleware.
|
|
143
145
|
|
|
144
146
|
#### Examples
|
|
145
147
|
|
|
@@ -160,15 +162,18 @@ _Basic [`express-graphql`](https://npm.im/express-graphql) setup._
|
|
|
160
162
|
> .listen(3000)
|
|
161
163
|
> ```
|
|
162
164
|
|
|
165
|
+
---
|
|
166
|
+
|
|
163
167
|
### function graphqlUploadKoa
|
|
164
168
|
|
|
165
|
-
Creates [Koa](https://koajs.com) middleware that processes GraphQL multipart requests using [`processRequest`](#function-processrequest), ignoring non-multipart requests. It sets the request body to be [similar to a conventional GraphQL POST request](#type-graphqloperation) for following GraphQL middleware to consume.
|
|
169
|
+
Creates [Koa](https://koajs.com) middleware that processes [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) using [`processRequest`](#function-processrequest), ignoring non-multipart requests. It sets the request body to be [similar to a conventional GraphQL POST request](#type-graphqloperation) for following GraphQL middleware to consume.
|
|
166
170
|
|
|
167
|
-
| Parameter | Type
|
|
168
|
-
|
|
|
169
|
-
| `options` | [
|
|
171
|
+
| Parameter | Type | Description |
|
|
172
|
+
| :-- | :-- | :-- |
|
|
173
|
+
| `options` | [ProcessRequestOptions](#type-processrequestoptions) | Middleware options. Any [`ProcessRequestOptions`](#type-processrequestoptions) can be used. |
|
|
174
|
+
| `options.processRequest` | [ProcessRequestFunction](#type-processrequestfunction)? = [processRequest](#function-processrequest) | Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec). |
|
|
170
175
|
|
|
171
|
-
**Returns:**
|
|
176
|
+
**Returns:** Function — Koa middleware.
|
|
172
177
|
|
|
173
178
|
#### Examples
|
|
174
179
|
|
|
@@ -189,17 +194,13 @@ _Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup._
|
|
|
189
194
|
> .listen(3000)
|
|
190
195
|
> ```
|
|
191
196
|
|
|
192
|
-
|
|
197
|
+
---
|
|
193
198
|
|
|
194
|
-
|
|
199
|
+
### function processRequest
|
|
195
200
|
|
|
196
|
-
|
|
197
|
-
| :--------- | :------------------------------------ | :-------------------------------------------------------------------------------------------------------- |
|
|
198
|
-
| `request` | IncomingMessage | [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage). |
|
|
199
|
-
| `response` | ServerResponse | [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse). |
|
|
200
|
-
| `options` | [UploadOptions](#type-uploadoptions)? | GraphQL upload options. |
|
|
201
|
+
Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec). Errors are created with [`http-errors`](https://npm.im/http-errors) to assist in sending responses with appropriate HTTP status codes. Used in [`graphqlUploadExpress`](#function-graphqluploadexpress) and [`graphqlUploadKoa`](#function-graphqluploadkoa) and can be used to create custom middleware.
|
|
201
202
|
|
|
202
|
-
**
|
|
203
|
+
**Type:** [ProcessRequestFunction](#type-processrequestfunction)
|
|
203
204
|
|
|
204
205
|
#### Examples
|
|
205
206
|
|
|
@@ -209,44 +210,66 @@ _How to import._
|
|
|
209
210
|
> import { processRequest } from 'graphql-upload'
|
|
210
211
|
> ```
|
|
211
212
|
|
|
213
|
+
---
|
|
214
|
+
|
|
212
215
|
### type FileUpload
|
|
213
216
|
|
|
214
217
|
File upload details, resolved from an [`Upload` scalar](#class-graphqlupload) promise.
|
|
215
218
|
|
|
216
|
-
**Type:**
|
|
219
|
+
**Type:** object
|
|
220
|
+
|
|
221
|
+
| Property | Type | Description |
|
|
222
|
+
| :-- | :-- | :-- |
|
|
223
|
+
| `filename` | string | File name. |
|
|
224
|
+
| `mimetype` | string | File MIME type. Provided by the client and can’t be trusted. |
|
|
225
|
+
| `encoding` | string | File stream transfer encoding. |
|
|
226
|
+
| `createReadStream` | Function | Returns a Node.js readable stream of the file contents, for processing and storing the file. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request. |
|
|
217
227
|
|
|
218
|
-
|
|
219
|
-
| :----------------- | :---------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
220
|
-
| `filename` | [string](https://mdn.io/string) | File name. |
|
|
221
|
-
| `mimetype` | [string](https://mdn.io/string) | File MIME type. Provided by the client and can’t be trusted. |
|
|
222
|
-
| `encoding` | [string](https://mdn.io/string) | File stream transfer encoding. |
|
|
223
|
-
| `createReadStream` | [function](https://mdn.io/function) | Returns a Node.js readable stream of the file contents, for processing and storing the file. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request. |
|
|
228
|
+
---
|
|
224
229
|
|
|
225
230
|
### type GraphQLOperation
|
|
226
231
|
|
|
227
232
|
A GraphQL operation object in a shape that can be consumed and executed by most GraphQL servers.
|
|
228
233
|
|
|
229
|
-
**Type:**
|
|
234
|
+
**Type:** object
|
|
230
235
|
|
|
231
|
-
| Property
|
|
232
|
-
|
|
|
233
|
-
| `query`
|
|
234
|
-
| `operationName` |
|
|
235
|
-
| `variables`
|
|
236
|
+
| Property | Type | Description |
|
|
237
|
+
| :-- | :-- | :-- |
|
|
238
|
+
| `query` | string | GraphQL document containing queries and fragments. |
|
|
239
|
+
| `operationName` | string \| `null`? | GraphQL document operation name to execute. |
|
|
240
|
+
| `variables` | object \| `null`? | GraphQL document operation variables and values map. |
|
|
236
241
|
|
|
237
242
|
#### See
|
|
238
243
|
|
|
239
244
|
- [GraphQL over HTTP spec](https://github.com/APIs-guru/graphql-over-http#request-parameters).
|
|
240
245
|
- [Apollo Server POST requests](https://www.apollographql.com/docs/apollo-server/requests#postRequests).
|
|
241
246
|
|
|
242
|
-
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
### type ProcessRequestFunction
|
|
250
|
+
|
|
251
|
+
Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
252
|
+
|
|
253
|
+
**Type:** Function
|
|
254
|
+
|
|
255
|
+
| Parameter | Type | Description |
|
|
256
|
+
| :-- | :-- | :-- |
|
|
257
|
+
| `request` | IncomingMessage | [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage). |
|
|
258
|
+
| `response` | ServerResponse | [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse). |
|
|
259
|
+
| `options` | [ProcessRequestOptions](#type-processrequestoptions)? | Options for processing the request. |
|
|
260
|
+
|
|
261
|
+
**Returns:** Promise<[GraphQLOperation](#type-graphqloperation) | Array<[GraphQLOperation](#type-graphqloperation)>> — GraphQL operation or batch of operations for a GraphQL server to consume (usually as the request body).
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
### type ProcessRequestOptions
|
|
243
266
|
|
|
244
|
-
GraphQL
|
|
267
|
+
Options for processing a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec); mostly relating to security, performance and limits.
|
|
245
268
|
|
|
246
|
-
**Type:**
|
|
269
|
+
**Type:** object
|
|
247
270
|
|
|
248
|
-
| Property
|
|
249
|
-
|
|
|
250
|
-
| `maxFieldSize` |
|
|
251
|
-
| `maxFileSize`
|
|
252
|
-
| `maxFiles`
|
|
271
|
+
| Property | Type | Description |
|
|
272
|
+
| :-- | :-- | :-- |
|
|
273
|
+
| `maxFieldSize` | number? = `1000000` | Maximum allowed non-file multipart form field size in bytes; enough for your queries. |
|
|
274
|
+
| `maxFileSize` | number? = Infinity | Maximum allowed file size in bytes. |
|
|
275
|
+
| `maxFiles` | number? = Infinity | Maximum allowed number of files. |
|