graphql-upload 10.0.0 → 11.0.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 +21 -1
- package/package.json +33 -21
- package/{lib → private}/constants.js +2 -2
- package/{lib → private}/ignoreStream.js +4 -4
- package/{lib → public}/GraphQLUpload.js +43 -27
- package/{lib → public}/Upload.js +26 -10
- package/{lib → public}/graphqlUploadExpress.js +39 -23
- package/{lib → public}/graphqlUploadKoa.js +31 -15
- package/{lib → public}/index.js +6 -6
- package/public/index.mjs +5 -0
- package/{lib → public}/processRequest.js +121 -109
- package/readme.md +130 -34
package/changelog.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# graphql-upload changelog
|
|
2
2
|
|
|
3
|
+
## 11.0.0
|
|
4
|
+
|
|
5
|
+
### Major
|
|
6
|
+
|
|
7
|
+
- Updated Node.js support to `^10.13.0 || ^12.0.0 || >= 13.7.0`.
|
|
8
|
+
- Added a [package `exports` field](https://nodejs.org/api/esm.html#esm_package_entry_points) with [conditional exports](https://nodejs.org/api/esm.html#esm_conditional_exports) to support native ESM in Node.js and keep internal code private, [whilst avoiding the dual package hazard](https://nodejs.org/api/esm.html#esm_approach_1_use_an_es_module_wrapper). Published files have been reorganized, so previously undocumented deep imports will need to be rewritten according to the newly documented paths.
|
|
9
|
+
- Updated dev dependencies, some of which require newer Node.js versions than previously supported.
|
|
10
|
+
|
|
11
|
+
### Patch
|
|
12
|
+
|
|
13
|
+
- Updated the [`graphql`](https://npm.im/graphql) peer dependency to `0.13.1 - 15`, fixing [#200](https://github.com/jaydenseric/graphql-upload/issues/200) via [#201](https://github.com/jaydenseric/graphql-upload/pull/201).
|
|
14
|
+
- Updated Prettier related package scripts.
|
|
15
|
+
- Configured Prettier option `semi` to the default, `true`.
|
|
16
|
+
- Ensure GitHub Actions run on pull request.
|
|
17
|
+
- Also run GitHub Actions with Node.js v14.
|
|
18
|
+
- Fixed the `ignoreStream` function tests for Node.js v14 with a new `CountReadableStream` test helper, fixing [#209](https://github.com/jaydenseric/graphql-upload/issues/209).
|
|
19
|
+
- Minor JSDoc wording tweak for consistency.
|
|
20
|
+
- Mention [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) in the readme “Tips” section.
|
|
21
|
+
- Updated MDN Web Docs links.
|
|
22
|
+
|
|
3
23
|
## 10.0.0
|
|
4
24
|
|
|
5
25
|
### Major
|
|
@@ -306,7 +326,7 @@ Big thanks to new collaborator [@mike-marcacci](https://github.com/mike-marcacci
|
|
|
306
326
|
- Updated dependencies.
|
|
307
327
|
- Simplified npm scripts.
|
|
308
328
|
- Readme updates:
|
|
309
|
-
- Documented [`Blob`](https://developer.mozilla.org/en/docs/Web/API/Blob) types, via [#39](https://github.com/jaydenseric/apollo-upload-server/pull/39).
|
|
329
|
+
- Documented [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) types, via [#39](https://github.com/jaydenseric/apollo-upload-server/pull/39).
|
|
310
330
|
- Explained how to use `processRequest` for custom middleware.
|
|
311
331
|
- Improved usage instructions.
|
|
312
332
|
- Display oldest supported Node.js version.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-upload",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.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": {
|
|
@@ -20,17 +20,29 @@
|
|
|
20
20
|
"server",
|
|
21
21
|
"koa",
|
|
22
22
|
"express",
|
|
23
|
-
"apollo"
|
|
23
|
+
"apollo",
|
|
24
|
+
"esm",
|
|
25
|
+
"mjs"
|
|
24
26
|
],
|
|
25
27
|
"files": [
|
|
26
|
-
"
|
|
28
|
+
"private",
|
|
29
|
+
"public"
|
|
27
30
|
],
|
|
28
|
-
"main": "
|
|
31
|
+
"main": "public",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": "./public/index.mjs",
|
|
35
|
+
"require": "./public/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./public/": "./public/",
|
|
38
|
+
"./package": "./package.json",
|
|
39
|
+
"./package.json": "./package.json"
|
|
40
|
+
},
|
|
29
41
|
"engines": {
|
|
30
|
-
"node": ">=
|
|
42
|
+
"node": "^10.13.0 || ^12.0.0 || >= 13.7.0"
|
|
31
43
|
},
|
|
32
44
|
"peerDependencies": {
|
|
33
|
-
"graphql": "0.13.1 -
|
|
45
|
+
"graphql": "0.13.1 - 15"
|
|
34
46
|
},
|
|
35
47
|
"dependencies": {
|
|
36
48
|
"busboy": "^0.3.1",
|
|
@@ -40,31 +52,31 @@
|
|
|
40
52
|
"object-path": "^0.11.4"
|
|
41
53
|
},
|
|
42
54
|
"devDependencies": {
|
|
43
|
-
"coverage-node": "^2.0.
|
|
44
|
-
"eslint": "^
|
|
45
|
-
"eslint-config-env": "^
|
|
46
|
-
"eslint-config-prettier": "^6.
|
|
47
|
-
"eslint-plugin-import": "^2.20.
|
|
48
|
-
"eslint-plugin-jsdoc": "^
|
|
49
|
-
"eslint-plugin-node": "^11.
|
|
50
|
-
"eslint-plugin-prettier": "^3.1.
|
|
55
|
+
"coverage-node": "^2.0.3",
|
|
56
|
+
"eslint": "^7.1.0",
|
|
57
|
+
"eslint-config-env": "^14.0.0",
|
|
58
|
+
"eslint-config-prettier": "^6.11.0",
|
|
59
|
+
"eslint-plugin-import": "^2.20.2",
|
|
60
|
+
"eslint-plugin-jsdoc": "^25.4.2",
|
|
61
|
+
"eslint-plugin-node": "^11.1.0",
|
|
62
|
+
"eslint-plugin-prettier": "^3.1.3",
|
|
51
63
|
"express": "^4.17.1",
|
|
52
64
|
"form-data": "^3.0.0",
|
|
53
|
-
"graphql": "^
|
|
65
|
+
"graphql": "^15.0.0",
|
|
54
66
|
"hard-rejection": "^2.1.0",
|
|
55
|
-
"jsdoc-md": "^
|
|
56
|
-
"koa": "^2.
|
|
67
|
+
"jsdoc-md": "^6.0.0",
|
|
68
|
+
"koa": "^2.12.0",
|
|
57
69
|
"node-fetch": "^2.6.0",
|
|
58
|
-
"prettier": "^
|
|
59
|
-
"test-director": "^
|
|
70
|
+
"prettier": "^2.0.5",
|
|
71
|
+
"test-director": "^4.0.1"
|
|
60
72
|
},
|
|
61
73
|
"scripts": {
|
|
62
74
|
"prepare": "npm run prepare:jsdoc && npm run prepare:prettier",
|
|
63
75
|
"prepare:jsdoc": "jsdoc-md",
|
|
64
|
-
"prepare:prettier": "prettier readme.md
|
|
76
|
+
"prepare:prettier": "prettier --write readme.md",
|
|
65
77
|
"test": "npm run test:eslint && npm run test:prettier && npm run test:api",
|
|
66
78
|
"test:eslint": "eslint .",
|
|
67
|
-
"test:prettier": "prettier
|
|
79
|
+
"test:prettier": "prettier -c .",
|
|
68
80
|
"test:api": "coverage-node -r hard-rejection/register test",
|
|
69
81
|
"prepublishOnly": "npm test"
|
|
70
82
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Official [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
@@ -9,4 +9,4 @@
|
|
|
9
9
|
* @ignore
|
|
10
10
|
*/
|
|
11
11
|
exports.SPEC_URL =
|
|
12
|
-
'https://github.com/jaydenseric/graphql-multipart-request-spec'
|
|
12
|
+
'https://github.com/jaydenseric/graphql-multipart-request-spec';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Safely ignores a Node.js readable stream.
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
*/
|
|
10
10
|
module.exports = function ignoreStream(stream) {
|
|
11
11
|
// Prevent an unhandled error from crashing the process.
|
|
12
|
-
stream.on('error', () => {})
|
|
12
|
+
stream.on('error', () => {});
|
|
13
13
|
|
|
14
14
|
// Waste the stream.
|
|
15
|
-
stream.resume()
|
|
16
|
-
}
|
|
15
|
+
stream.resume();
|
|
16
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
const { GraphQLScalarType, GraphQLError } = require('graphql')
|
|
4
|
-
const Upload = require('./Upload')
|
|
3
|
+
const { GraphQLScalarType, GraphQLError } = require('graphql');
|
|
4
|
+
const Upload = require('./Upload');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* A GraphQL `Upload` scalar that can be used in a
|
|
@@ -10,28 +10,44 @@ const Upload = require('./Upload')
|
|
|
10
10
|
* [file upload details]{@link FileUpload} for processing and storage.
|
|
11
11
|
* @kind class
|
|
12
12
|
* @name GraphQLUpload
|
|
13
|
+
* @example <caption>Ways to `import`.</caption>
|
|
14
|
+
* ```js
|
|
15
|
+
* import { GraphQLUpload } from 'graphql-upload';
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';
|
|
20
|
+
* ```
|
|
21
|
+
* @example <caption>Ways to `require`.</caption>
|
|
22
|
+
* ```js
|
|
23
|
+
* const { GraphQLUpload } = require('graphql-upload');
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* ```js
|
|
27
|
+
* const GraphQLUpload = require('graphql-upload/public/GraphQLUpload');
|
|
28
|
+
* ```
|
|
13
29
|
* @example <caption>Setup for a schema built with [`makeExecutableSchema`](https://apollographql.com/docs/graphql-tools/generate-schema#makeExecutableSchema).</caption>
|
|
14
30
|
* ```js
|
|
15
|
-
* const { makeExecutableSchema } = require('graphql-tools')
|
|
16
|
-
* const { GraphQLUpload } = require('graphql-upload')
|
|
31
|
+
* const { makeExecutableSchema } = require('graphql-tools');
|
|
32
|
+
* const { GraphQLUpload } = require('graphql-upload');
|
|
17
33
|
*
|
|
18
34
|
* const schema = makeExecutableSchema({
|
|
19
35
|
* typeDefs: /* GraphQL *\/ `
|
|
20
36
|
* scalar Upload
|
|
21
37
|
* `,
|
|
22
38
|
* resolvers: {
|
|
23
|
-
* Upload: GraphQLUpload
|
|
24
|
-
* }
|
|
25
|
-
* })
|
|
39
|
+
* Upload: GraphQLUpload,
|
|
40
|
+
* },
|
|
41
|
+
* });
|
|
26
42
|
* ```
|
|
27
43
|
* @example <caption>A manually constructed schema with an image upload mutation.</caption>
|
|
28
44
|
* ```js
|
|
29
45
|
* const {
|
|
30
46
|
* GraphQLSchema,
|
|
31
47
|
* GraphQLObjectType,
|
|
32
|
-
* GraphQLBoolean
|
|
33
|
-
* } = require('graphql')
|
|
34
|
-
* const { GraphQLUpload } = require('graphql-upload')
|
|
48
|
+
* GraphQLBoolean,
|
|
49
|
+
* } = require('graphql');
|
|
50
|
+
* const { GraphQLUpload } = require('graphql-upload');
|
|
35
51
|
*
|
|
36
52
|
* const schema = new GraphQLSchema({
|
|
37
53
|
* mutation: new GraphQLObjectType({
|
|
@@ -43,32 +59,32 @@ const Upload = require('./Upload')
|
|
|
43
59
|
* args: {
|
|
44
60
|
* image: {
|
|
45
61
|
* description: 'Image file.',
|
|
46
|
-
* type: GraphQLUpload
|
|
47
|
-
* }
|
|
62
|
+
* type: GraphQLUpload,
|
|
63
|
+
* },
|
|
48
64
|
* },
|
|
49
65
|
* async resolve(parent, { image }) {
|
|
50
|
-
* const { filename, mimetype, createReadStream } = await image
|
|
51
|
-
* const stream = createReadStream()
|
|
66
|
+
* const { filename, mimetype, createReadStream } = await image;
|
|
67
|
+
* const stream = createReadStream();
|
|
52
68
|
* // Promisify the stream and store the file, then…
|
|
53
|
-
* return true
|
|
54
|
-
* }
|
|
55
|
-
* }
|
|
56
|
-
* }
|
|
57
|
-
* })
|
|
58
|
-
* })
|
|
69
|
+
* return true;
|
|
70
|
+
* },
|
|
71
|
+
* },
|
|
72
|
+
* },
|
|
73
|
+
* }),
|
|
74
|
+
* });
|
|
59
75
|
* ```
|
|
60
76
|
*/
|
|
61
77
|
module.exports = new GraphQLScalarType({
|
|
62
78
|
name: 'Upload',
|
|
63
79
|
description: 'The `Upload` scalar type represents a file upload.',
|
|
64
80
|
parseValue(value) {
|
|
65
|
-
if (value instanceof Upload) return value.promise
|
|
66
|
-
throw new GraphQLError('Upload value invalid.')
|
|
81
|
+
if (value instanceof Upload) return value.promise;
|
|
82
|
+
throw new GraphQLError('Upload value invalid.');
|
|
67
83
|
},
|
|
68
84
|
parseLiteral(ast) {
|
|
69
|
-
throw new GraphQLError('Upload literal unsupported.', ast)
|
|
85
|
+
throw new GraphQLError('Upload literal unsupported.', ast);
|
|
70
86
|
},
|
|
71
87
|
serialize() {
|
|
72
|
-
throw new GraphQLError('Upload serialization unsupported.')
|
|
73
|
-
}
|
|
74
|
-
})
|
|
88
|
+
throw new GraphQLError('Upload serialization unsupported.');
|
|
89
|
+
},
|
|
90
|
+
});
|
package/{lib → public}/Upload.js
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A file expected to be uploaded as it has been declared in the `map` field of
|
|
@@ -10,6 +10,22 @@
|
|
|
10
10
|
* [`promise`]{@link Upload#promise} property.
|
|
11
11
|
* @kind class
|
|
12
12
|
* @name Upload
|
|
13
|
+
* @example <caption>Ways to `import`.</caption>
|
|
14
|
+
* ```js
|
|
15
|
+
* import { Upload } from 'graphql-upload';
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* import Upload from 'graphql-upload/public/Upload.js';
|
|
20
|
+
* ```
|
|
21
|
+
* @example <caption>Ways to `require`.</caption>
|
|
22
|
+
* ```js
|
|
23
|
+
* const { Upload } = require('graphql-upload');
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* ```js
|
|
27
|
+
* const Upload = require('graphql-upload/public/Upload');
|
|
28
|
+
* ```
|
|
13
29
|
*/
|
|
14
30
|
module.exports = class Upload {
|
|
15
31
|
constructor() {
|
|
@@ -28,7 +44,7 @@ module.exports = class Upload {
|
|
|
28
44
|
* @name Upload#resolve
|
|
29
45
|
* @param {FileUpload} file File upload details.
|
|
30
46
|
*/
|
|
31
|
-
this.resolve = file => {
|
|
47
|
+
this.resolve = (file) => {
|
|
32
48
|
/**
|
|
33
49
|
* The file upload details, available when the
|
|
34
50
|
* [upload promise]{@link Upload#promise} resolves. This should only be
|
|
@@ -37,23 +53,23 @@ module.exports = class Upload {
|
|
|
37
53
|
* @name Upload#file
|
|
38
54
|
* @type {undefined|FileUpload}
|
|
39
55
|
*/
|
|
40
|
-
this.file = file
|
|
56
|
+
this.file = file;
|
|
41
57
|
|
|
42
|
-
resolve(file)
|
|
43
|
-
}
|
|
58
|
+
resolve(file);
|
|
59
|
+
};
|
|
44
60
|
|
|
45
61
|
/**
|
|
46
|
-
* Rejects the upload promise with an error. This
|
|
62
|
+
* Rejects the upload promise with an error. This should only be
|
|
47
63
|
* utilized by [`processRequest`]{@link processRequest}.
|
|
48
64
|
* @kind function
|
|
49
65
|
* @name Upload#reject
|
|
50
66
|
* @param {object} error Error instance.
|
|
51
67
|
*/
|
|
52
|
-
this.reject = reject
|
|
53
|
-
})
|
|
68
|
+
this.reject = reject;
|
|
69
|
+
});
|
|
54
70
|
|
|
55
71
|
// Prevent errors crashing Node.js, see:
|
|
56
72
|
// https://github.com/nodejs/node/issues/20392
|
|
57
|
-
this.promise.catch(() => {})
|
|
73
|
+
this.promise.catch(() => {});
|
|
58
74
|
}
|
|
59
|
-
}
|
|
75
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
const defaultProcessRequest = require('./processRequest')
|
|
3
|
+
const defaultProcessRequest = require('./processRequest');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Creates [Express](https://expressjs.com) middleware that processes
|
|
@@ -14,12 +14,28 @@ const defaultProcessRequest = require('./processRequest')
|
|
|
14
14
|
* @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
|
|
15
15
|
* @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
16
16
|
* @returns {Function} Express middleware.
|
|
17
|
+
* @example <caption>Ways to `import`.</caption>
|
|
18
|
+
* ```js
|
|
19
|
+
* import { graphqlUploadExpress } from 'graphql-upload';
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ```js
|
|
23
|
+
* import graphqlUploadExpress from 'graphql-upload/public/graphqlUploadExpress.js';
|
|
24
|
+
* ```
|
|
25
|
+
* @example <caption>Ways to `require`.</caption>
|
|
26
|
+
* ```js
|
|
27
|
+
* const { graphqlUploadExpress } = require('graphql-upload');
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* ```js
|
|
31
|
+
* const graphqlUploadExpress = require('graphql-upload/public/graphqlUploadExpress');
|
|
32
|
+
* ```
|
|
17
33
|
* @example <caption>Basic [`express-graphql`](https://npm.im/express-graphql) setup.</caption>
|
|
18
34
|
* ```js
|
|
19
|
-
* const express = require('express')
|
|
20
|
-
* const graphqlHTTP = require('express-graphql')
|
|
21
|
-
* const { graphqlUploadExpress } = require('graphql-upload')
|
|
22
|
-
* const schema = require('./schema')
|
|
35
|
+
* const express = require('express');
|
|
36
|
+
* const graphqlHTTP = require('express-graphql');
|
|
37
|
+
* const { graphqlUploadExpress } = require('graphql-upload');
|
|
38
|
+
* const schema = require('./schema');
|
|
23
39
|
*
|
|
24
40
|
* express()
|
|
25
41
|
* .use(
|
|
@@ -27,7 +43,7 @@ const defaultProcessRequest = require('./processRequest')
|
|
|
27
43
|
* graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
|
|
28
44
|
* graphqlHTTP({ schema })
|
|
29
45
|
* )
|
|
30
|
-
* .listen(3000)
|
|
46
|
+
* .listen(3000);
|
|
31
47
|
* ```
|
|
32
48
|
*/
|
|
33
49
|
module.exports = function graphqlUploadExpress({
|
|
@@ -35,26 +51,26 @@ module.exports = function graphqlUploadExpress({
|
|
|
35
51
|
...processRequestOptions
|
|
36
52
|
} = {}) {
|
|
37
53
|
return function graphqlUploadExpressMiddleware(request, response, next) {
|
|
38
|
-
if (!request.is('multipart/form-data')) return next()
|
|
54
|
+
if (!request.is('multipart/form-data')) return next();
|
|
39
55
|
|
|
40
|
-
const finished = new Promise(resolve => request.on('end', resolve))
|
|
41
|
-
const { send } = response
|
|
56
|
+
const finished = new Promise((resolve) => request.on('end', resolve));
|
|
57
|
+
const { send } = response;
|
|
42
58
|
|
|
43
59
|
response.send = (...args) => {
|
|
44
60
|
finished.then(() => {
|
|
45
|
-
response.send = send
|
|
46
|
-
response.send(...args)
|
|
47
|
-
})
|
|
48
|
-
}
|
|
61
|
+
response.send = send;
|
|
62
|
+
response.send(...args);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
49
65
|
|
|
50
66
|
processRequest(request, response, processRequestOptions)
|
|
51
|
-
.then(body => {
|
|
52
|
-
request.body = body
|
|
53
|
-
next()
|
|
54
|
-
})
|
|
55
|
-
.catch(error => {
|
|
56
|
-
if (error.status && error.expose) response.status(error.status)
|
|
57
|
-
next(error)
|
|
67
|
+
.then((body) => {
|
|
68
|
+
request.body = body;
|
|
69
|
+
next();
|
|
58
70
|
})
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
.catch((error) => {
|
|
72
|
+
if (error.status && error.expose) response.status(error.status);
|
|
73
|
+
next(error);
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
const defaultProcessRequest = require('./processRequest')
|
|
3
|
+
const defaultProcessRequest = require('./processRequest');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Creates [Koa](https://koajs.com) middleware that processes
|
|
@@ -14,20 +14,36 @@ const defaultProcessRequest = require('./processRequest')
|
|
|
14
14
|
* @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
|
|
15
15
|
* @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
16
16
|
* @returns {Function} Koa middleware.
|
|
17
|
+
* @example <caption>Ways to `import`.</caption>
|
|
18
|
+
* ```js
|
|
19
|
+
* import { graphqlUploadKoa } from 'graphql-upload';
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ```js
|
|
23
|
+
* import graphqlUploadKoa from 'graphql-upload/public/graphqlUploadKoa.js';
|
|
24
|
+
* ```
|
|
25
|
+
* @example <caption>Ways to `require`.</caption>
|
|
26
|
+
* ```js
|
|
27
|
+
* const { graphqlUploadKoa } = require('graphql-upload');
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* ```js
|
|
31
|
+
* const graphqlUploadKoa = require('graphql-upload/public/graphqlUploadKoa');
|
|
32
|
+
* ```
|
|
17
33
|
* @example <caption>Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup.</caption>
|
|
18
34
|
* ```js
|
|
19
|
-
* const Koa = require('koa')
|
|
20
|
-
* const bodyParser = require('koa-bodyparser')
|
|
21
|
-
* const { errorHandler, execute } = require('graphql-api-koa')
|
|
22
|
-
* const { graphqlUploadKoa } = require('graphql-upload')
|
|
23
|
-
* const schema = require('./schema')
|
|
35
|
+
* const Koa = require('koa');
|
|
36
|
+
* const bodyParser = require('koa-bodyparser');
|
|
37
|
+
* const { errorHandler, execute } = require('graphql-api-koa');
|
|
38
|
+
* const { graphqlUploadKoa } = require('graphql-upload');
|
|
39
|
+
* const schema = require('./schema');
|
|
24
40
|
*
|
|
25
41
|
* new Koa()
|
|
26
42
|
* .use(errorHandler())
|
|
27
43
|
* .use(bodyParser())
|
|
28
44
|
* .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
|
|
29
45
|
* .use(execute({ schema }))
|
|
30
|
-
* .listen(3000)
|
|
46
|
+
* .listen(3000);
|
|
31
47
|
* ```
|
|
32
48
|
*/
|
|
33
49
|
module.exports = function graphqlUploadKoa({
|
|
@@ -35,19 +51,19 @@ module.exports = function graphqlUploadKoa({
|
|
|
35
51
|
...processRequestOptions
|
|
36
52
|
} = {}) {
|
|
37
53
|
return async function graphqlUploadKoaMiddleware(ctx, next) {
|
|
38
|
-
if (!ctx.request.is('multipart/form-data')) return next()
|
|
54
|
+
if (!ctx.request.is('multipart/form-data')) return next();
|
|
39
55
|
|
|
40
|
-
const finished = new Promise(resolve => ctx.req.on('end', resolve))
|
|
56
|
+
const finished = new Promise((resolve) => ctx.req.on('end', resolve));
|
|
41
57
|
|
|
42
58
|
try {
|
|
43
59
|
ctx.request.body = await processRequest(
|
|
44
60
|
ctx.req,
|
|
45
61
|
ctx.res,
|
|
46
62
|
processRequestOptions
|
|
47
|
-
)
|
|
48
|
-
await next()
|
|
63
|
+
);
|
|
64
|
+
await next();
|
|
49
65
|
} finally {
|
|
50
|
-
await finished
|
|
66
|
+
await finished;
|
|
51
67
|
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
68
|
+
};
|
|
69
|
+
};
|
package/{lib → public}/index.js
RENAMED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
exports.GraphQLUpload = require('./GraphQLUpload')
|
|
4
|
-
exports.processRequest = require('./processRequest')
|
|
5
|
-
exports.graphqlUploadKoa = require('./graphqlUploadKoa')
|
|
6
|
-
exports.graphqlUploadExpress = require('./graphqlUploadExpress')
|
|
7
|
-
exports.Upload = require('./Upload')
|
|
3
|
+
exports.GraphQLUpload = require('./GraphQLUpload');
|
|
4
|
+
exports.processRequest = require('./processRequest');
|
|
5
|
+
exports.graphqlUploadKoa = require('./graphqlUploadKoa');
|
|
6
|
+
exports.graphqlUploadExpress = require('./graphqlUploadExpress');
|
|
7
|
+
exports.Upload = require('./Upload');
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* File upload details that are only available after the file’s field in the
|
package/public/index.mjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as GraphQLUpload } from './GraphQLUpload.js';
|
|
2
|
+
export { default as processRequest } from './processRequest.js';
|
|
3
|
+
export { default as graphqlUploadKoa } from './graphqlUploadKoa.js';
|
|
4
|
+
export { default as graphqlUploadExpress } from './graphqlUploadExpress.js';
|
|
5
|
+
export { default as Upload } from './Upload.js';
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
const Busboy = require('busboy')
|
|
4
|
-
const { WriteStream } = require('fs-capacitor')
|
|
5
|
-
const createError = require('http-errors')
|
|
6
|
-
const isObject = require('isobject')
|
|
7
|
-
const objectPath = require('object-path')
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
3
|
+
const Busboy = require('busboy');
|
|
4
|
+
const { WriteStream } = require('fs-capacitor');
|
|
5
|
+
const createError = require('http-errors');
|
|
6
|
+
const isObject = require('isobject');
|
|
7
|
+
const objectPath = require('object-path');
|
|
8
|
+
const { SPEC_URL } = require('../private/constants');
|
|
9
|
+
const ignoreStream = require('../private/ignoreStream');
|
|
10
|
+
const Upload = require('./Upload');
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
@@ -24,9 +24,21 @@ const ignoreStream = require('./ignoreStream')
|
|
|
24
24
|
* @kind function
|
|
25
25
|
* @name processRequest
|
|
26
26
|
* @type {ProcessRequestFunction}
|
|
27
|
-
* @example <caption>
|
|
27
|
+
* @example <caption>Ways to `import`.</caption>
|
|
28
28
|
* ```js
|
|
29
|
-
*
|
|
29
|
+
* import { processRequest } from 'graphql-upload';
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ```js
|
|
33
|
+
* import processRequest from 'graphql-upload/public/processRequest.js';
|
|
34
|
+
* ```
|
|
35
|
+
* @example <caption>Ways to `require`.</caption>
|
|
36
|
+
* ```js
|
|
37
|
+
* const { processRequest } = require('graphql-upload');
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* ```js
|
|
41
|
+
* const processRequest = require('graphql-upload/public/processRequest');
|
|
30
42
|
* ```
|
|
31
43
|
*/
|
|
32
44
|
module.exports = function processRequest(
|
|
@@ -35,16 +47,16 @@ module.exports = function processRequest(
|
|
|
35
47
|
{
|
|
36
48
|
maxFieldSize = 1000000, // 1 MB
|
|
37
49
|
maxFileSize = Infinity,
|
|
38
|
-
maxFiles = Infinity
|
|
50
|
+
maxFiles = Infinity,
|
|
39
51
|
} = {}
|
|
40
52
|
) {
|
|
41
53
|
return new Promise((resolve, reject) => {
|
|
42
|
-
let released
|
|
43
|
-
let exitError
|
|
44
|
-
let currentStream
|
|
45
|
-
let operations
|
|
46
|
-
let operationsPath
|
|
47
|
-
let map
|
|
54
|
+
let released;
|
|
55
|
+
let exitError;
|
|
56
|
+
let currentStream;
|
|
57
|
+
let operations;
|
|
58
|
+
let operationsPath;
|
|
59
|
+
let map;
|
|
48
60
|
|
|
49
61
|
const parser = new Busboy({
|
|
50
62
|
headers: request.headers,
|
|
@@ -52,9 +64,9 @@ module.exports = function processRequest(
|
|
|
52
64
|
fieldSize: maxFieldSize,
|
|
53
65
|
fields: 2, // Only operations and map.
|
|
54
66
|
fileSize: maxFileSize,
|
|
55
|
-
files: maxFiles
|
|
56
|
-
}
|
|
57
|
-
})
|
|
67
|
+
files: maxFiles,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
58
70
|
|
|
59
71
|
/**
|
|
60
72
|
* Exits request processing with an error. Successive calls have no effect.
|
|
@@ -63,30 +75,30 @@ module.exports = function processRequest(
|
|
|
63
75
|
* @param {object} error Error instance.
|
|
64
76
|
* @ignore
|
|
65
77
|
*/
|
|
66
|
-
const exit = error => {
|
|
67
|
-
if (exitError) return
|
|
68
|
-
exitError = error
|
|
78
|
+
const exit = (error) => {
|
|
79
|
+
if (exitError) return;
|
|
80
|
+
exitError = error;
|
|
69
81
|
|
|
70
|
-
reject(exitError)
|
|
82
|
+
reject(exitError);
|
|
71
83
|
|
|
72
|
-
parser.destroy()
|
|
84
|
+
parser.destroy();
|
|
73
85
|
|
|
74
|
-
if (currentStream) currentStream.destroy(exitError)
|
|
86
|
+
if (currentStream) currentStream.destroy(exitError);
|
|
75
87
|
|
|
76
88
|
if (map)
|
|
77
89
|
for (const upload of map.values())
|
|
78
|
-
if (!upload.file) upload.reject(exitError)
|
|
90
|
+
if (!upload.file) upload.reject(exitError);
|
|
79
91
|
|
|
80
|
-
request.unpipe(parser)
|
|
92
|
+
request.unpipe(parser);
|
|
81
93
|
|
|
82
94
|
// With a sufficiently large request body, subsequent events in the same
|
|
83
95
|
// event frame cause the stream to pause after the parser is destroyed. To
|
|
84
96
|
// ensure that the request resumes, the call to .resume() is scheduled for
|
|
85
97
|
// later in the event loop.
|
|
86
98
|
setImmediate(() => {
|
|
87
|
-
request.resume()
|
|
88
|
-
})
|
|
89
|
-
}
|
|
99
|
+
request.resume();
|
|
100
|
+
});
|
|
101
|
+
};
|
|
90
102
|
|
|
91
103
|
/**
|
|
92
104
|
* Releases resources and cleans up Capacitor temporary files. Successive
|
|
@@ -96,13 +108,13 @@ module.exports = function processRequest(
|
|
|
96
108
|
* @ignore
|
|
97
109
|
*/
|
|
98
110
|
const release = () => {
|
|
99
|
-
if (released) return
|
|
100
|
-
released = true
|
|
111
|
+
if (released) return;
|
|
112
|
+
released = true;
|
|
101
113
|
|
|
102
114
|
if (map)
|
|
103
115
|
for (const upload of map.values())
|
|
104
|
-
if (upload.file) upload.file.capacitor.release()
|
|
105
|
-
}
|
|
116
|
+
if (upload.file) upload.file.capacitor.release();
|
|
117
|
+
};
|
|
106
118
|
|
|
107
119
|
/**
|
|
108
120
|
* Handles when the request is closed before it properly ended.
|
|
@@ -116,13 +128,13 @@ module.exports = function processRequest(
|
|
|
116
128
|
499,
|
|
117
129
|
'Request disconnected during file upload stream parsing.'
|
|
118
130
|
)
|
|
119
|
-
)
|
|
120
|
-
}
|
|
131
|
+
);
|
|
132
|
+
};
|
|
121
133
|
|
|
122
134
|
parser.on(
|
|
123
135
|
'field',
|
|
124
136
|
(fieldName, value, fieldNameTruncated, valueTruncated) => {
|
|
125
|
-
if (exitError) return
|
|
137
|
+
if (exitError) return;
|
|
126
138
|
|
|
127
139
|
if (valueTruncated)
|
|
128
140
|
return exit(
|
|
@@ -130,19 +142,19 @@ module.exports = function processRequest(
|
|
|
130
142
|
413,
|
|
131
143
|
`The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
|
|
132
144
|
)
|
|
133
|
-
)
|
|
145
|
+
);
|
|
134
146
|
|
|
135
147
|
switch (fieldName) {
|
|
136
148
|
case 'operations':
|
|
137
149
|
try {
|
|
138
|
-
operations = JSON.parse(value)
|
|
150
|
+
operations = JSON.parse(value);
|
|
139
151
|
} catch (error) {
|
|
140
152
|
return exit(
|
|
141
153
|
createError(
|
|
142
154
|
400,
|
|
143
155
|
`Invalid JSON in the ‘operations’ multipart field (${SPEC_URL}).`
|
|
144
156
|
)
|
|
145
|
-
)
|
|
157
|
+
);
|
|
146
158
|
}
|
|
147
159
|
|
|
148
160
|
if (!isObject(operations) && !Array.isArray(operations))
|
|
@@ -151,11 +163,11 @@ module.exports = function processRequest(
|
|
|
151
163
|
400,
|
|
152
164
|
`Invalid type for the ‘operations’ multipart field (${SPEC_URL}).`
|
|
153
165
|
)
|
|
154
|
-
)
|
|
166
|
+
);
|
|
155
167
|
|
|
156
|
-
operationsPath = objectPath(operations)
|
|
168
|
+
operationsPath = objectPath(operations);
|
|
157
169
|
|
|
158
|
-
break
|
|
170
|
+
break;
|
|
159
171
|
case 'map': {
|
|
160
172
|
if (!operations)
|
|
161
173
|
return exit(
|
|
@@ -163,18 +175,18 @@ module.exports = function processRequest(
|
|
|
163
175
|
400,
|
|
164
176
|
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${SPEC_URL}).`
|
|
165
177
|
)
|
|
166
|
-
)
|
|
178
|
+
);
|
|
167
179
|
|
|
168
|
-
let parsedMap
|
|
180
|
+
let parsedMap;
|
|
169
181
|
try {
|
|
170
|
-
parsedMap = JSON.parse(value)
|
|
182
|
+
parsedMap = JSON.parse(value);
|
|
171
183
|
} catch (error) {
|
|
172
184
|
return exit(
|
|
173
185
|
createError(
|
|
174
186
|
400,
|
|
175
187
|
`Invalid JSON in the ‘map’ multipart field (${SPEC_URL}).`
|
|
176
188
|
)
|
|
177
|
-
)
|
|
189
|
+
);
|
|
178
190
|
}
|
|
179
191
|
|
|
180
192
|
if (!isObject(parsedMap))
|
|
@@ -183,18 +195,18 @@ module.exports = function processRequest(
|
|
|
183
195
|
400,
|
|
184
196
|
`Invalid type for the ‘map’ multipart field (${SPEC_URL}).`
|
|
185
197
|
)
|
|
186
|
-
)
|
|
198
|
+
);
|
|
187
199
|
|
|
188
|
-
const mapEntries = Object.entries(parsedMap)
|
|
200
|
+
const mapEntries = Object.entries(parsedMap);
|
|
189
201
|
|
|
190
202
|
// Check max files is not exceeded, even though the number of files to
|
|
191
203
|
// parse might not match th(e map provided by the client.
|
|
192
204
|
if (mapEntries.length > maxFiles)
|
|
193
205
|
return exit(
|
|
194
206
|
createError(413, `${maxFiles} max file uploads exceeded.`)
|
|
195
|
-
)
|
|
207
|
+
);
|
|
196
208
|
|
|
197
|
-
map = new Map()
|
|
209
|
+
map = new Map();
|
|
198
210
|
for (const [fieldName, paths] of mapEntries) {
|
|
199
211
|
if (!Array.isArray(paths))
|
|
200
212
|
return exit(
|
|
@@ -202,9 +214,9 @@ module.exports = function processRequest(
|
|
|
202
214
|
400,
|
|
203
215
|
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${SPEC_URL}).`
|
|
204
216
|
)
|
|
205
|
-
)
|
|
217
|
+
);
|
|
206
218
|
|
|
207
|
-
map.set(fieldName, new Upload())
|
|
219
|
+
map.set(fieldName, new Upload());
|
|
208
220
|
|
|
209
221
|
for (const [index, path] of paths.entries()) {
|
|
210
222
|
if (typeof path !== 'string')
|
|
@@ -213,104 +225,104 @@ module.exports = function processRequest(
|
|
|
213
225
|
400,
|
|
214
226
|
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${SPEC_URL}).`
|
|
215
227
|
)
|
|
216
|
-
)
|
|
228
|
+
);
|
|
217
229
|
|
|
218
230
|
try {
|
|
219
|
-
operationsPath.set(path, map.get(fieldName))
|
|
231
|
+
operationsPath.set(path, map.get(fieldName));
|
|
220
232
|
} catch (error) {
|
|
221
233
|
return exit(
|
|
222
234
|
createError(
|
|
223
235
|
400,
|
|
224
236
|
`Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${SPEC_URL}).`
|
|
225
237
|
)
|
|
226
|
-
)
|
|
238
|
+
);
|
|
227
239
|
}
|
|
228
240
|
}
|
|
229
241
|
}
|
|
230
242
|
|
|
231
|
-
resolve(operations)
|
|
243
|
+
resolve(operations);
|
|
232
244
|
}
|
|
233
245
|
}
|
|
234
246
|
}
|
|
235
|
-
)
|
|
247
|
+
);
|
|
236
248
|
|
|
237
249
|
parser.on('file', (fieldName, stream, filename, encoding, mimetype) => {
|
|
238
250
|
if (exitError) {
|
|
239
|
-
ignoreStream(stream)
|
|
240
|
-
return
|
|
251
|
+
ignoreStream(stream);
|
|
252
|
+
return;
|
|
241
253
|
}
|
|
242
254
|
|
|
243
255
|
if (!map) {
|
|
244
|
-
ignoreStream(stream)
|
|
256
|
+
ignoreStream(stream);
|
|
245
257
|
return exit(
|
|
246
258
|
createError(
|
|
247
259
|
400,
|
|
248
260
|
`Misordered multipart fields; files should follow ‘map’ (${SPEC_URL}).`
|
|
249
261
|
)
|
|
250
|
-
)
|
|
262
|
+
);
|
|
251
263
|
}
|
|
252
264
|
|
|
253
|
-
currentStream = stream
|
|
265
|
+
currentStream = stream;
|
|
254
266
|
stream.on('end', () => {
|
|
255
|
-
currentStream = null
|
|
256
|
-
})
|
|
267
|
+
currentStream = null;
|
|
268
|
+
});
|
|
257
269
|
|
|
258
|
-
const upload = map.get(fieldName)
|
|
270
|
+
const upload = map.get(fieldName);
|
|
259
271
|
|
|
260
272
|
if (!upload) {
|
|
261
273
|
// The file is extraneous. As the rest can still be processed, just
|
|
262
274
|
// ignore it and don’t exit with an error.
|
|
263
|
-
ignoreStream(stream)
|
|
264
|
-
return
|
|
275
|
+
ignoreStream(stream);
|
|
276
|
+
return;
|
|
265
277
|
}
|
|
266
278
|
|
|
267
|
-
let fileError
|
|
268
|
-
const capacitor = new WriteStream()
|
|
279
|
+
let fileError;
|
|
280
|
+
const capacitor = new WriteStream();
|
|
269
281
|
|
|
270
282
|
capacitor.on('error', () => {
|
|
271
|
-
stream.unpipe()
|
|
272
|
-
stream.resume()
|
|
273
|
-
})
|
|
283
|
+
stream.unpipe();
|
|
284
|
+
stream.resume();
|
|
285
|
+
});
|
|
274
286
|
|
|
275
287
|
stream.on('limit', () => {
|
|
276
288
|
fileError = createError(
|
|
277
289
|
413,
|
|
278
290
|
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
|
|
279
|
-
)
|
|
280
|
-
stream.unpipe()
|
|
281
|
-
capacitor.destroy(fileError)
|
|
282
|
-
})
|
|
291
|
+
);
|
|
292
|
+
stream.unpipe();
|
|
293
|
+
capacitor.destroy(fileError);
|
|
294
|
+
});
|
|
283
295
|
|
|
284
|
-
stream.on('error', error => {
|
|
285
|
-
fileError = error
|
|
286
|
-
stream.unpipe()
|
|
287
|
-
capacitor.destroy(exitError)
|
|
288
|
-
})
|
|
296
|
+
stream.on('error', (error) => {
|
|
297
|
+
fileError = error;
|
|
298
|
+
stream.unpipe();
|
|
299
|
+
capacitor.destroy(exitError);
|
|
300
|
+
});
|
|
289
301
|
|
|
290
302
|
const file = {
|
|
291
303
|
filename,
|
|
292
304
|
mimetype,
|
|
293
305
|
encoding,
|
|
294
306
|
createReadStream(options) {
|
|
295
|
-
const error = fileError || (released ? exitError : null)
|
|
296
|
-
if (error) throw error
|
|
297
|
-
return capacitor.createReadStream(options)
|
|
298
|
-
}
|
|
299
|
-
}
|
|
307
|
+
const error = fileError || (released ? exitError : null);
|
|
308
|
+
if (error) throw error;
|
|
309
|
+
return capacitor.createReadStream(options);
|
|
310
|
+
},
|
|
311
|
+
};
|
|
300
312
|
|
|
301
|
-
Object.defineProperty(file, 'capacitor', { value: capacitor })
|
|
313
|
+
Object.defineProperty(file, 'capacitor', { value: capacitor });
|
|
302
314
|
|
|
303
|
-
stream.pipe(capacitor)
|
|
304
|
-
upload.resolve(file)
|
|
305
|
-
})
|
|
315
|
+
stream.pipe(capacitor);
|
|
316
|
+
upload.resolve(file);
|
|
317
|
+
});
|
|
306
318
|
|
|
307
319
|
parser.once('filesLimit', () =>
|
|
308
320
|
exit(createError(413, `${maxFiles} max file uploads exceeded.`))
|
|
309
|
-
)
|
|
321
|
+
);
|
|
310
322
|
|
|
311
323
|
parser.once('finish', () => {
|
|
312
|
-
request.unpipe(parser)
|
|
313
|
-
request.resume()
|
|
324
|
+
request.unpipe(parser);
|
|
325
|
+
request.resume();
|
|
314
326
|
|
|
315
327
|
if (!operations)
|
|
316
328
|
return exit(
|
|
@@ -318,28 +330,28 @@ module.exports = function processRequest(
|
|
|
318
330
|
400,
|
|
319
331
|
`Missing multipart field ‘operations’ (${SPEC_URL}).`
|
|
320
332
|
)
|
|
321
|
-
)
|
|
333
|
+
);
|
|
322
334
|
|
|
323
335
|
if (!map)
|
|
324
336
|
return exit(
|
|
325
337
|
createError(400, `Missing multipart field ‘map’ (${SPEC_URL}).`)
|
|
326
|
-
)
|
|
338
|
+
);
|
|
327
339
|
|
|
328
340
|
for (const upload of map.values())
|
|
329
341
|
if (!upload.file)
|
|
330
|
-
upload.reject(createError(400, 'File missing in the request.'))
|
|
331
|
-
})
|
|
342
|
+
upload.reject(createError(400, 'File missing in the request.'));
|
|
343
|
+
});
|
|
332
344
|
|
|
333
|
-
parser.once('error', exit)
|
|
345
|
+
parser.once('error', exit);
|
|
334
346
|
|
|
335
|
-
response.once('finish', release)
|
|
336
|
-
response.once('close', release)
|
|
347
|
+
response.once('finish', release);
|
|
348
|
+
response.once('close', release);
|
|
337
349
|
|
|
338
|
-
request.once('close', abort)
|
|
350
|
+
request.once('close', abort);
|
|
339
351
|
request.once('end', () => {
|
|
340
|
-
request.removeListener('close', abort)
|
|
341
|
-
})
|
|
352
|
+
request.removeListener('close', abort);
|
|
353
|
+
});
|
|
342
354
|
|
|
343
|
-
request.pipe(parser)
|
|
344
|
-
})
|
|
345
|
-
}
|
|
355
|
+
request.pipe(parser);
|
|
356
|
+
});
|
|
357
|
+
};
|
package/readme.md
CHANGED
|
@@ -12,7 +12,7 @@ Middleware and an [`Upload` scalar](#class-graphqlupload) to add support for [Gr
|
|
|
12
12
|
|
|
13
13
|
The following environments are known to be compatible:
|
|
14
14
|
|
|
15
|
-
- [Node.js](https://nodejs.org) >=
|
|
15
|
+
- [Node.js](https://nodejs.org) `^10.13.0 || ^12.0.0 || >= 13.7.0`
|
|
16
16
|
- [Koa](https://koajs.com)
|
|
17
17
|
- [`graphql-api-koa`](https://npm.im/graphql-api-koa)
|
|
18
18
|
- [`koa-graphql`](https://npm.im/koa-graphql)
|
|
@@ -49,7 +49,7 @@ See the [example API and client](https://github.com/jaydenseric/apollo-upload-ex
|
|
|
49
49
|
- The device requires sufficient disk space to buffer the expected number of concurrent upload requests.
|
|
50
50
|
- Promisify and await file upload streams in resolvers or the server will send a response to the client before uploads are complete, causing a disconnect.
|
|
51
51
|
- Handle file upload promise rejection and stream errors; uploads sometimes fail due to network connectivity issues or impatient users disconnecting.
|
|
52
|
-
- Process multiple uploads asynchronously with [`Promise.all`](https://developer.mozilla.org/docs/web/javascript/reference/global_objects/promise/all) or a more flexible solution where an error in one does not reject them all.
|
|
52
|
+
- Process multiple uploads asynchronously with [`Promise.all`](https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/promise/all) or a more flexible solution such as [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) where an error in one does not reject them all.
|
|
53
53
|
- Only use [`createReadStream()`](#type-fileupload) _before_ the resolver returns; late calls (e.g. in an unawaited async function or callback) throw an error. Existing streams can still be used after a response is sent, although there are few valid reasons for not awaiting their completion.
|
|
54
54
|
- Use [`stream.destroy()`](https://nodejs.org/api/stream.html#stream_readable_destroy_error) when an incomplete stream is no longer needed, or temporary files may not get cleaned up.
|
|
55
55
|
|
|
@@ -86,20 +86,40 @@ A GraphQL `Upload` scalar that can be used in a [`GraphQLSchema`](https://graphq
|
|
|
86
86
|
|
|
87
87
|
#### Examples
|
|
88
88
|
|
|
89
|
+
_Ways to `import`._
|
|
90
|
+
|
|
91
|
+
> ```js
|
|
92
|
+
> import { GraphQLUpload } from 'graphql-upload';
|
|
93
|
+
> ```
|
|
94
|
+
>
|
|
95
|
+
> ```js
|
|
96
|
+
> import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';
|
|
97
|
+
> ```
|
|
98
|
+
|
|
99
|
+
_Ways to `require`._
|
|
100
|
+
|
|
101
|
+
> ```js
|
|
102
|
+
> const { GraphQLUpload } = require('graphql-upload');
|
|
103
|
+
> ```
|
|
104
|
+
>
|
|
105
|
+
> ```js
|
|
106
|
+
> const GraphQLUpload = require('graphql-upload/public/GraphQLUpload');
|
|
107
|
+
> ```
|
|
108
|
+
|
|
89
109
|
_Setup for a schema built with [`makeExecutableSchema`](https://apollographql.com/docs/graphql-tools/generate-schema#makeExecutableSchema)._
|
|
90
110
|
|
|
91
111
|
> ```js
|
|
92
|
-
> const { makeExecutableSchema } = require('graphql-tools')
|
|
93
|
-
> const { GraphQLUpload } = require('graphql-upload')
|
|
112
|
+
> const { makeExecutableSchema } = require('graphql-tools');
|
|
113
|
+
> const { GraphQLUpload } = require('graphql-upload');
|
|
94
114
|
>
|
|
95
115
|
> const schema = makeExecutableSchema({
|
|
96
116
|
> typeDefs: /* GraphQL */ `
|
|
97
117
|
> scalar Upload
|
|
98
118
|
> `,
|
|
99
119
|
> resolvers: {
|
|
100
|
-
> Upload: GraphQLUpload
|
|
101
|
-
> }
|
|
102
|
-
> })
|
|
120
|
+
> Upload: GraphQLUpload,
|
|
121
|
+
> },
|
|
122
|
+
> });
|
|
103
123
|
> ```
|
|
104
124
|
|
|
105
125
|
_A manually constructed schema with an image upload mutation._
|
|
@@ -108,9 +128,9 @@ _A manually constructed schema with an image upload mutation._
|
|
|
108
128
|
> const {
|
|
109
129
|
> GraphQLSchema,
|
|
110
130
|
> GraphQLObjectType,
|
|
111
|
-
> GraphQLBoolean
|
|
112
|
-
> } = require('graphql')
|
|
113
|
-
> const { GraphQLUpload } = require('graphql-upload')
|
|
131
|
+
> GraphQLBoolean,
|
|
132
|
+
> } = require('graphql');
|
|
133
|
+
> const { GraphQLUpload } = require('graphql-upload');
|
|
114
134
|
>
|
|
115
135
|
> const schema = new GraphQLSchema({
|
|
116
136
|
> mutation: new GraphQLObjectType({
|
|
@@ -122,19 +142,19 @@ _A manually constructed schema with an image upload mutation._
|
|
|
122
142
|
> args: {
|
|
123
143
|
> image: {
|
|
124
144
|
> description: 'Image file.',
|
|
125
|
-
> type: GraphQLUpload
|
|
126
|
-
> }
|
|
145
|
+
> type: GraphQLUpload,
|
|
146
|
+
> },
|
|
127
147
|
> },
|
|
128
148
|
> async resolve(parent, { image }) {
|
|
129
|
-
> const { filename, mimetype, createReadStream } = await image
|
|
130
|
-
> const stream = createReadStream()
|
|
149
|
+
> const { filename, mimetype, createReadStream } = await image;
|
|
150
|
+
> const stream = createReadStream();
|
|
131
151
|
> // Promisify the stream and store the file, then…
|
|
132
|
-
> return true
|
|
133
|
-
> }
|
|
134
|
-
> }
|
|
135
|
-
> }
|
|
136
|
-
> })
|
|
137
|
-
> })
|
|
152
|
+
> return true;
|
|
153
|
+
> },
|
|
154
|
+
> },
|
|
155
|
+
> },
|
|
156
|
+
> }),
|
|
157
|
+
> });
|
|
138
158
|
> ```
|
|
139
159
|
|
|
140
160
|
---
|
|
@@ -143,9 +163,31 @@ _A manually constructed schema with an image upload mutation._
|
|
|
143
163
|
|
|
144
164
|
A file expected to be uploaded as it has been declared in the `map` field of a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec). The [`processRequest`](#function-processrequest) function places references to an instance of this class wherever the file is expected in the [GraphQL operation](#type-graphqloperation). The [`Upload` scalar](#class-graphqlupload) derives it’s value from the [`promise`](#upload-instance-property-promise) property.
|
|
145
165
|
|
|
166
|
+
#### Examples
|
|
167
|
+
|
|
168
|
+
_Ways to `import`._
|
|
169
|
+
|
|
170
|
+
> ```js
|
|
171
|
+
> import { Upload } from 'graphql-upload';
|
|
172
|
+
> ```
|
|
173
|
+
>
|
|
174
|
+
> ```js
|
|
175
|
+
> import Upload from 'graphql-upload/public/Upload.js';
|
|
176
|
+
> ```
|
|
177
|
+
|
|
178
|
+
_Ways to `require`._
|
|
179
|
+
|
|
180
|
+
> ```js
|
|
181
|
+
> const { Upload } = require('graphql-upload');
|
|
182
|
+
> ```
|
|
183
|
+
>
|
|
184
|
+
> ```js
|
|
185
|
+
> const Upload = require('graphql-upload/public/Upload');
|
|
186
|
+
> ```
|
|
187
|
+
|
|
146
188
|
#### Upload instance method reject
|
|
147
189
|
|
|
148
|
-
Rejects the upload promise with an error. This
|
|
190
|
+
Rejects the upload promise with an error. This should only be utilized by [`processRequest`](#function-processrequest).
|
|
149
191
|
|
|
150
192
|
| Parameter | Type | Description |
|
|
151
193
|
| :-------- | :----- | :-------------- |
|
|
@@ -186,13 +228,33 @@ Creates [Express](https://expressjs.com) middleware that processes [GraphQL mult
|
|
|
186
228
|
|
|
187
229
|
#### Examples
|
|
188
230
|
|
|
231
|
+
_Ways to `import`._
|
|
232
|
+
|
|
233
|
+
> ```js
|
|
234
|
+
> import { graphqlUploadExpress } from 'graphql-upload';
|
|
235
|
+
> ```
|
|
236
|
+
>
|
|
237
|
+
> ```js
|
|
238
|
+
> import graphqlUploadExpress from 'graphql-upload/public/graphqlUploadExpress.js';
|
|
239
|
+
> ```
|
|
240
|
+
|
|
241
|
+
_Ways to `require`._
|
|
242
|
+
|
|
243
|
+
> ```js
|
|
244
|
+
> const { graphqlUploadExpress } = require('graphql-upload');
|
|
245
|
+
> ```
|
|
246
|
+
>
|
|
247
|
+
> ```js
|
|
248
|
+
> const graphqlUploadExpress = require('graphql-upload/public/graphqlUploadExpress');
|
|
249
|
+
> ```
|
|
250
|
+
|
|
189
251
|
_Basic [`express-graphql`](https://npm.im/express-graphql) setup._
|
|
190
252
|
|
|
191
253
|
> ```js
|
|
192
|
-
> const express = require('express')
|
|
193
|
-
> const graphqlHTTP = require('express-graphql')
|
|
194
|
-
> const { graphqlUploadExpress } = require('graphql-upload')
|
|
195
|
-
> const schema = require('./schema')
|
|
254
|
+
> const express = require('express');
|
|
255
|
+
> const graphqlHTTP = require('express-graphql');
|
|
256
|
+
> const { graphqlUploadExpress } = require('graphql-upload');
|
|
257
|
+
> const schema = require('./schema');
|
|
196
258
|
>
|
|
197
259
|
> express()
|
|
198
260
|
> .use(
|
|
@@ -200,7 +262,7 @@ _Basic [`express-graphql`](https://npm.im/express-graphql) setup._
|
|
|
200
262
|
> graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
|
|
201
263
|
> graphqlHTTP({ schema })
|
|
202
264
|
> )
|
|
203
|
-
> .listen(3000)
|
|
265
|
+
> .listen(3000);
|
|
204
266
|
> ```
|
|
205
267
|
|
|
206
268
|
---
|
|
@@ -218,21 +280,41 @@ Creates [Koa](https://koajs.com) middleware that processes [GraphQL multipart re
|
|
|
218
280
|
|
|
219
281
|
#### Examples
|
|
220
282
|
|
|
283
|
+
_Ways to `import`._
|
|
284
|
+
|
|
285
|
+
> ```js
|
|
286
|
+
> import { graphqlUploadKoa } from 'graphql-upload';
|
|
287
|
+
> ```
|
|
288
|
+
>
|
|
289
|
+
> ```js
|
|
290
|
+
> import graphqlUploadKoa from 'graphql-upload/public/graphqlUploadKoa.js';
|
|
291
|
+
> ```
|
|
292
|
+
|
|
293
|
+
_Ways to `require`._
|
|
294
|
+
|
|
295
|
+
> ```js
|
|
296
|
+
> const { graphqlUploadKoa } = require('graphql-upload');
|
|
297
|
+
> ```
|
|
298
|
+
>
|
|
299
|
+
> ```js
|
|
300
|
+
> const graphqlUploadKoa = require('graphql-upload/public/graphqlUploadKoa');
|
|
301
|
+
> ```
|
|
302
|
+
|
|
221
303
|
_Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup._
|
|
222
304
|
|
|
223
305
|
> ```js
|
|
224
|
-
> const Koa = require('koa')
|
|
225
|
-
> const bodyParser = require('koa-bodyparser')
|
|
226
|
-
> const { errorHandler, execute } = require('graphql-api-koa')
|
|
227
|
-
> const { graphqlUploadKoa } = require('graphql-upload')
|
|
228
|
-
> const schema = require('./schema')
|
|
306
|
+
> const Koa = require('koa');
|
|
307
|
+
> const bodyParser = require('koa-bodyparser');
|
|
308
|
+
> const { errorHandler, execute } = require('graphql-api-koa');
|
|
309
|
+
> const { graphqlUploadKoa } = require('graphql-upload');
|
|
310
|
+
> const schema = require('./schema');
|
|
229
311
|
>
|
|
230
312
|
> new Koa()
|
|
231
313
|
> .use(errorHandler())
|
|
232
314
|
> .use(bodyParser())
|
|
233
315
|
> .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
|
|
234
316
|
> .use(execute({ schema }))
|
|
235
|
-
> .listen(3000)
|
|
317
|
+
> .listen(3000);
|
|
236
318
|
> ```
|
|
237
319
|
|
|
238
320
|
---
|
|
@@ -245,10 +327,24 @@ Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-m
|
|
|
245
327
|
|
|
246
328
|
#### Examples
|
|
247
329
|
|
|
248
|
-
|
|
330
|
+
_Ways to `import`._
|
|
249
331
|
|
|
250
332
|
> ```js
|
|
251
|
-
>
|
|
333
|
+
> import { processRequest } from 'graphql-upload';
|
|
334
|
+
> ```
|
|
335
|
+
>
|
|
336
|
+
> ```js
|
|
337
|
+
> import processRequest from 'graphql-upload/public/processRequest.js';
|
|
338
|
+
> ```
|
|
339
|
+
|
|
340
|
+
_Ways to `require`._
|
|
341
|
+
|
|
342
|
+
> ```js
|
|
343
|
+
> const { processRequest } = require('graphql-upload');
|
|
344
|
+
> ```
|
|
345
|
+
>
|
|
346
|
+
> ```js
|
|
347
|
+
> const processRequest = require('graphql-upload/public/processRequest');
|
|
252
348
|
> ```
|
|
253
349
|
|
|
254
350
|
---
|