graphql-upload 9.0.0 → 13.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/package.json +47 -41
- package/private/GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js +12 -0
- package/{ignoreStream.js → private/ignoreStream.js} +7 -5
- package/public/GraphQLUpload.js +90 -0
- package/public/Upload.js +75 -0
- package/{graphqlUploadExpress.js → public/graphqlUploadExpress.js} +41 -23
- package/{graphqlUploadKoa.js → public/graphqlUploadKoa.js} +32 -14
- package/public/index.js +75 -0
- package/public/index.mjs +5 -0
- package/public/processRequest.js +348 -0
- package/readme.md +216 -70
- package/GraphQLUpload.js +0 -67
- package/changelog.md +0 -441
- package/constants.js +0 -10
- package/index.js +0 -51
- package/isEnumerableObject.js +0 -11
- package/processRequest.js +0 -390
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-upload",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.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": {
|
|
@@ -17,63 +17,69 @@
|
|
|
17
17
|
"upload",
|
|
18
18
|
"file",
|
|
19
19
|
"multipart",
|
|
20
|
+
"request",
|
|
20
21
|
"server",
|
|
22
|
+
"middleware",
|
|
21
23
|
"koa",
|
|
22
24
|
"express",
|
|
23
|
-
"apollo"
|
|
25
|
+
"apollo",
|
|
26
|
+
"esm",
|
|
27
|
+
"mjs"
|
|
24
28
|
],
|
|
25
29
|
"files": [
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"graphqlUploadExpress.js",
|
|
29
|
-
"graphqlUploadKoa.js",
|
|
30
|
-
"ignoreStream.js",
|
|
31
|
-
"index.js",
|
|
32
|
-
"isEnumerableObject.js",
|
|
33
|
-
"processRequest.js"
|
|
30
|
+
"private",
|
|
31
|
+
"public"
|
|
34
32
|
],
|
|
35
|
-
"
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"main": "./public/index.js",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": "./public/index.mjs",
|
|
38
|
+
"require": "./public/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./public/*": "./public/*",
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
36
43
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
44
|
+
"node": "^12.22.0 || ^14.17.0 || >= 16.0.0"
|
|
38
45
|
},
|
|
39
46
|
"peerDependencies": {
|
|
40
|
-
"graphql": "0.13.1 -
|
|
47
|
+
"graphql": "0.13.1 - 16"
|
|
41
48
|
},
|
|
42
49
|
"dependencies": {
|
|
43
50
|
"busboy": "^0.3.1",
|
|
44
|
-
"fs-capacitor": "^
|
|
45
|
-
"http-errors": "^1.
|
|
46
|
-
"object-path": "^0.11.
|
|
51
|
+
"fs-capacitor": "^6.2.0",
|
|
52
|
+
"http-errors": "^1.8.1",
|
|
53
|
+
"object-path": "^0.11.8"
|
|
47
54
|
},
|
|
48
55
|
"devDependencies": {
|
|
49
|
-
"
|
|
50
|
-
"eslint
|
|
51
|
-
"eslint-config-
|
|
52
|
-
"eslint-
|
|
53
|
-
"eslint-plugin-import
|
|
54
|
-
"eslint-plugin-jsdoc": "^
|
|
55
|
-
"eslint-plugin-node": "^
|
|
56
|
-
"eslint-plugin-prettier": "^
|
|
56
|
+
"coverage-node": "^5.0.1",
|
|
57
|
+
"eslint": "^8.3.0",
|
|
58
|
+
"eslint-config-env": "^23.0.0",
|
|
59
|
+
"eslint-config-prettier": "^8.3.0",
|
|
60
|
+
"eslint-plugin-import": "^2.25.3",
|
|
61
|
+
"eslint-plugin-jsdoc": "^37.0.3",
|
|
62
|
+
"eslint-plugin-node": "^11.1.0",
|
|
63
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
57
64
|
"express": "^4.17.1",
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"graphql": "^
|
|
61
|
-
"
|
|
62
|
-
"jsdoc-md": "^
|
|
63
|
-
"koa": "^2.
|
|
64
|
-
"
|
|
65
|
-
"node-fetch": "^
|
|
66
|
-
"prettier": "^
|
|
67
|
-
"
|
|
65
|
+
"form-data-encoder": "^1.7.1",
|
|
66
|
+
"formdata-node": "^4.3.1",
|
|
67
|
+
"graphql": "^16.0.1",
|
|
68
|
+
"hard-rejection": "^2.1.0",
|
|
69
|
+
"jsdoc-md": "^11.0.2",
|
|
70
|
+
"koa": "^2.13.4",
|
|
71
|
+
"node-abort-controller": "^3.0.1",
|
|
72
|
+
"node-fetch": "^3.1.0",
|
|
73
|
+
"prettier": "^2.5.0",
|
|
74
|
+
"test-director": "^7.0.0"
|
|
68
75
|
},
|
|
69
76
|
"scripts": {
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"test
|
|
76
|
-
"test:tap": "tap --test-ignore=src --100 --nyc-arg=--exclude='**/test-helpers/**'",
|
|
77
|
+
"docs-update": "jsdoc-md",
|
|
78
|
+
"docs-check": "jsdoc-md -c",
|
|
79
|
+
"eslint": "eslint .",
|
|
80
|
+
"prettier": "prettier -c .",
|
|
81
|
+
"tests": "coverage-node -r hard-rejection/register test/index.mjs",
|
|
82
|
+
"test": "npm run eslint && npm run prettier && npm run docs-check && npm run tests",
|
|
77
83
|
"prepublishOnly": "npm test"
|
|
78
84
|
}
|
|
79
85
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
5
|
+
* URL. Useful for error messages, etc.
|
|
6
|
+
* @kind constant
|
|
7
|
+
* @name GRAPHQL_MULTIPART_REQUEST_SPEC_URL
|
|
8
|
+
* @type {string}
|
|
9
|
+
* @ignore
|
|
10
|
+
*/
|
|
11
|
+
module.exports =
|
|
12
|
+
"https://github.com/jaydenseric/graphql-multipart-request-spec";
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
* Safely ignores a readable stream.
|
|
4
|
+
* Safely ignores a Node.js readable stream.
|
|
3
5
|
* @kind function
|
|
4
6
|
* @name ignoreStream
|
|
5
|
-
* @param {ReadableStream} stream
|
|
7
|
+
* @param {ReadableStream} stream Node.js readable stream.
|
|
6
8
|
* @ignore
|
|
7
9
|
*/
|
|
8
10
|
module.exports = function ignoreStream(stream) {
|
|
9
11
|
// Prevent an unhandled error from crashing the process.
|
|
10
|
-
stream.on(
|
|
12
|
+
stream.on("error", () => {});
|
|
11
13
|
|
|
12
14
|
// Waste the stream.
|
|
13
|
-
stream.resume()
|
|
14
|
-
}
|
|
15
|
+
stream.resume();
|
|
16
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// One of the JSDoc code examples contains `*/` escaped using `*\/`, see:
|
|
2
|
+
// https://github.com/gajus/eslint-plugin-jsdoc/issues/710
|
|
3
|
+
/* eslint-disable jsdoc/check-examples */
|
|
4
|
+
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const { GraphQLScalarType, GraphQLError } = require("graphql");
|
|
8
|
+
const Upload = require("./Upload");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A GraphQL `Upload` scalar that can be used in a
|
|
12
|
+
* [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema).
|
|
13
|
+
* It’s value in resolvers is a promise that resolves
|
|
14
|
+
* [file upload details]{@link FileUpload} for processing and storage.
|
|
15
|
+
* @kind class
|
|
16
|
+
* @name GraphQLUpload
|
|
17
|
+
* @example <caption>Ways to `import`.</caption>
|
|
18
|
+
* ```js
|
|
19
|
+
* import { GraphQLUpload } from "graphql-upload";
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ```js
|
|
23
|
+
* import GraphQLUpload from "graphql-upload/public/GraphQLUpload.js";
|
|
24
|
+
* ```
|
|
25
|
+
* @example <caption>Ways to `require`.</caption>
|
|
26
|
+
* ```js
|
|
27
|
+
* const { GraphQLUpload } = require("graphql-upload");
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* ```js
|
|
31
|
+
* const GraphQLUpload = require("graphql-upload/public/GraphQLUpload.js");
|
|
32
|
+
* ```
|
|
33
|
+
* @example <caption>A schema built using [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema#makeexecutableschema) from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema).</caption>
|
|
34
|
+
* ```js
|
|
35
|
+
* const { makeExecutableSchema } = require("@graphql-tools/schema");
|
|
36
|
+
* const { GraphQLUpload } = require("graphql-upload");
|
|
37
|
+
*
|
|
38
|
+
* const schema = makeExecutableSchema({
|
|
39
|
+
* typeDefs: /* GraphQL *\/ `
|
|
40
|
+
* scalar Upload
|
|
41
|
+
* `,
|
|
42
|
+
* resolvers: {
|
|
43
|
+
* Upload: GraphQLUpload,
|
|
44
|
+
* },
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
* @example <caption>A manually constructed schema with an image upload mutation.</caption>
|
|
48
|
+
* ```js
|
|
49
|
+
* const { GraphQLSchema, GraphQLObjectType, GraphQLBoolean } = require("graphql");
|
|
50
|
+
* const { GraphQLUpload } = require("graphql-upload");
|
|
51
|
+
*
|
|
52
|
+
* const schema = new GraphQLSchema({
|
|
53
|
+
* mutation: new GraphQLObjectType({
|
|
54
|
+
* name: "Mutation",
|
|
55
|
+
* fields: {
|
|
56
|
+
* uploadImage: {
|
|
57
|
+
* description: "Uploads an image.",
|
|
58
|
+
* type: GraphQLBoolean,
|
|
59
|
+
* args: {
|
|
60
|
+
* image: {
|
|
61
|
+
* description: "Image file.",
|
|
62
|
+
* type: GraphQLUpload,
|
|
63
|
+
* },
|
|
64
|
+
* },
|
|
65
|
+
* async resolve(parent, { image }) {
|
|
66
|
+
* const { filename, mimetype, createReadStream } = await image;
|
|
67
|
+
* const stream = createReadStream();
|
|
68
|
+
* // Promisify the stream and store the file, then…
|
|
69
|
+
* return true;
|
|
70
|
+
* },
|
|
71
|
+
* },
|
|
72
|
+
* },
|
|
73
|
+
* }),
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
module.exports = new GraphQLScalarType({
|
|
78
|
+
name: "Upload",
|
|
79
|
+
description: "The `Upload` scalar type represents a file upload.",
|
|
80
|
+
parseValue(value) {
|
|
81
|
+
if (value instanceof Upload) return value.promise;
|
|
82
|
+
throw new GraphQLError("Upload value invalid.");
|
|
83
|
+
},
|
|
84
|
+
parseLiteral(ast) {
|
|
85
|
+
throw new GraphQLError("Upload literal unsupported.", ast);
|
|
86
|
+
},
|
|
87
|
+
serialize() {
|
|
88
|
+
throw new GraphQLError("Upload serialization unsupported.");
|
|
89
|
+
},
|
|
90
|
+
});
|
package/public/Upload.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A file expected to be uploaded as it has been declared in the `map` field of
|
|
5
|
+
* a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
6
|
+
* The [`processRequest`]{@link processRequest} function places references to an
|
|
7
|
+
* instance of this class wherever the file is expected in the
|
|
8
|
+
* [GraphQL operation]{@link GraphQLOperation}. The
|
|
9
|
+
* [`Upload`]{@link GraphQLUpload} scalar derives it’s value from the
|
|
10
|
+
* [`promise`]{@link Upload#promise} property.
|
|
11
|
+
* @kind class
|
|
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.js");
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
module.exports = class Upload {
|
|
31
|
+
constructor() {
|
|
32
|
+
/**
|
|
33
|
+
* Promise that resolves file upload details. This should only be utilized
|
|
34
|
+
* by [`GraphQLUpload`]{@link GraphQLUpload}.
|
|
35
|
+
* @kind member
|
|
36
|
+
* @name Upload#promise
|
|
37
|
+
* @type {Promise<FileUpload>}
|
|
38
|
+
*/
|
|
39
|
+
this.promise = new Promise((resolve, reject) => {
|
|
40
|
+
/**
|
|
41
|
+
* Resolves the upload promise with the file upload details. This should
|
|
42
|
+
* only be utilized by [`processRequest`]{@link processRequest}.
|
|
43
|
+
* @kind function
|
|
44
|
+
* @name Upload#resolve
|
|
45
|
+
* @param {FileUpload} file File upload details.
|
|
46
|
+
*/
|
|
47
|
+
this.resolve = (file) => {
|
|
48
|
+
/**
|
|
49
|
+
* The file upload details, available when the
|
|
50
|
+
* [upload promise]{@link Upload#promise} resolves. This should only be
|
|
51
|
+
* utilized by [`processRequest`]{@link processRequest}.
|
|
52
|
+
* @kind member
|
|
53
|
+
* @name Upload#file
|
|
54
|
+
* @type {undefined|FileUpload}
|
|
55
|
+
*/
|
|
56
|
+
this.file = file;
|
|
57
|
+
|
|
58
|
+
resolve(file);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Rejects the upload promise with an error. This should only be
|
|
63
|
+
* utilized by [`processRequest`]{@link processRequest}.
|
|
64
|
+
* @kind function
|
|
65
|
+
* @name Upload#reject
|
|
66
|
+
* @param {object} error Error instance.
|
|
67
|
+
*/
|
|
68
|
+
this.reject = reject;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Prevent errors crashing Node.js, see:
|
|
72
|
+
// https://github.com/nodejs/node/issues/20392
|
|
73
|
+
this.promise.catch(() => {});
|
|
74
|
+
}
|
|
75
|
+
};
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const defaultProcessRequest = require("./processRequest");
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Creates [Express](https://expressjs.com) middleware that processes
|
|
@@ -12,20 +14,36 @@ const defaultProcessRequest = require('./processRequest')
|
|
|
12
14
|
* @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
|
|
13
15
|
* @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
14
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.js");
|
|
32
|
+
* ```
|
|
15
33
|
* @example <caption>Basic [`express-graphql`](https://npm.im/express-graphql) setup.</caption>
|
|
16
34
|
* ```js
|
|
17
|
-
* const express = require(
|
|
18
|
-
* const graphqlHTTP = require(
|
|
19
|
-
* const { graphqlUploadExpress } = require(
|
|
20
|
-
* const schema = require(
|
|
35
|
+
* const express = require("express");
|
|
36
|
+
* const graphqlHTTP = require("express-graphql");
|
|
37
|
+
* const { graphqlUploadExpress } = require("graphql-upload");
|
|
38
|
+
* const schema = require("./schema");
|
|
21
39
|
*
|
|
22
40
|
* express()
|
|
23
41
|
* .use(
|
|
24
|
-
*
|
|
42
|
+
* "/graphql",
|
|
25
43
|
* graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
|
|
26
44
|
* graphqlHTTP({ schema })
|
|
27
45
|
* )
|
|
28
|
-
* .listen(3000)
|
|
46
|
+
* .listen(3000);
|
|
29
47
|
* ```
|
|
30
48
|
*/
|
|
31
49
|
module.exports = function graphqlUploadExpress({
|
|
@@ -33,26 +51,26 @@ module.exports = function graphqlUploadExpress({
|
|
|
33
51
|
...processRequestOptions
|
|
34
52
|
} = {}) {
|
|
35
53
|
return function graphqlUploadExpressMiddleware(request, response, next) {
|
|
36
|
-
if (!request.is(
|
|
54
|
+
if (!request.is("multipart/form-data")) return next();
|
|
37
55
|
|
|
38
|
-
const finished = new Promise(resolve => request.on(
|
|
56
|
+
const finished = new Promise((resolve) => request.on("end", resolve));
|
|
57
|
+
const { send } = response;
|
|
39
58
|
|
|
40
|
-
const { send } = response
|
|
41
59
|
response.send = (...args) => {
|
|
42
60
|
finished.then(() => {
|
|
43
|
-
response.send = send
|
|
44
|
-
response.send(...args)
|
|
45
|
-
})
|
|
46
|
-
}
|
|
61
|
+
response.send = send;
|
|
62
|
+
response.send(...args);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
47
65
|
|
|
48
66
|
processRequest(request, response, processRequestOptions)
|
|
49
|
-
.then(body => {
|
|
50
|
-
request.body = body
|
|
51
|
-
next()
|
|
52
|
-
})
|
|
53
|
-
.catch(error => {
|
|
54
|
-
if (error.status && error.expose) response.status(error.status)
|
|
55
|
-
next(error)
|
|
67
|
+
.then((body) => {
|
|
68
|
+
request.body = body;
|
|
69
|
+
next();
|
|
56
70
|
})
|
|
57
|
-
|
|
58
|
-
|
|
71
|
+
.catch((error) => {
|
|
72
|
+
if (error.status && error.expose) response.status(error.status);
|
|
73
|
+
next(error);
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
};
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const defaultProcessRequest = require("./processRequest");
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Creates [Koa](https://koajs.com) middleware that processes
|
|
@@ -12,20 +14,36 @@ const defaultProcessRequest = require('./processRequest')
|
|
|
12
14
|
* @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
|
|
13
15
|
* @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
14
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.js");
|
|
32
|
+
* ```
|
|
15
33
|
* @example <caption>Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup.</caption>
|
|
16
34
|
* ```js
|
|
17
|
-
* const Koa = require(
|
|
18
|
-
* const bodyParser = require(
|
|
19
|
-
* const { errorHandler, execute } = require(
|
|
20
|
-
* const { graphqlUploadKoa } = require(
|
|
21
|
-
* const schema = require(
|
|
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");
|
|
22
40
|
*
|
|
23
41
|
* new Koa()
|
|
24
42
|
* .use(errorHandler())
|
|
25
43
|
* .use(bodyParser())
|
|
26
44
|
* .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
|
|
27
45
|
* .use(execute({ schema }))
|
|
28
|
-
* .listen(3000)
|
|
46
|
+
* .listen(3000);
|
|
29
47
|
* ```
|
|
30
48
|
*/
|
|
31
49
|
module.exports = function graphqlUploadKoa({
|
|
@@ -33,19 +51,19 @@ module.exports = function graphqlUploadKoa({
|
|
|
33
51
|
...processRequestOptions
|
|
34
52
|
} = {}) {
|
|
35
53
|
return async function graphqlUploadKoaMiddleware(ctx, next) {
|
|
36
|
-
if (!ctx.request.is(
|
|
54
|
+
if (!ctx.request.is("multipart/form-data")) return next();
|
|
37
55
|
|
|
38
|
-
const finished = new Promise(resolve => ctx.req.on(
|
|
56
|
+
const finished = new Promise((resolve) => ctx.req.on("end", resolve));
|
|
39
57
|
|
|
40
58
|
try {
|
|
41
59
|
ctx.request.body = await processRequest(
|
|
42
60
|
ctx.req,
|
|
43
61
|
ctx.res,
|
|
44
62
|
processRequestOptions
|
|
45
|
-
)
|
|
46
|
-
await next()
|
|
63
|
+
);
|
|
64
|
+
await next();
|
|
47
65
|
} finally {
|
|
48
|
-
await finished
|
|
66
|
+
await finished;
|
|
49
67
|
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
68
|
+
};
|
|
69
|
+
};
|
package/public/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
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");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* File upload details that are only available after the file’s field in the
|
|
11
|
+
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
12
|
+
* has begun streaming in.
|
|
13
|
+
* @kind typedef
|
|
14
|
+
* @name FileUpload
|
|
15
|
+
* @type {object}
|
|
16
|
+
* @prop {string} filename File name.
|
|
17
|
+
* @prop {string} mimetype File MIME type. Provided by the client and can’t be trusted.
|
|
18
|
+
* @prop {string} encoding File stream transfer encoding.
|
|
19
|
+
* @prop {FileUploadCreateReadStream} createReadStream Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents, for processing and storage.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates a
|
|
24
|
+
* [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams)
|
|
25
|
+
* of an [uploading file’s]{@link FileUpload} contents, for processing and
|
|
26
|
+
* storage. Multiple calls create independent streams. Throws if called after
|
|
27
|
+
* all resolvers have resolved, or after an error has interrupted the request.
|
|
28
|
+
* @kind typedef
|
|
29
|
+
* @name FileUploadCreateReadStream
|
|
30
|
+
* @type {Function}
|
|
31
|
+
* @param {object} [options] [`fs-capacitor`](https://npm.im/fs-capacitor) [`ReadStreamOptions`](https://github.com/mike-marcacci/fs-capacitor#readstreamoptions).
|
|
32
|
+
* @param {string} [options.encoding=null] Specify an encoding for the [`data`](https://nodejs.org/api/stream.html#stream_event_data) chunks to be strings (without splitting multi-byte characters across chunks) instead of Node.js [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) instances. Supported values depend on the [`Buffer` implementation](https://github.com/nodejs/node/blob/v13.7.0/lib/buffer.js#L587-L663) and include `utf8`, `ucs2`, `utf16le`, `latin1`, `ascii`, `base64`, or `hex`.
|
|
33
|
+
* @param {number} [options.highWaterMark=16384] Maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.
|
|
34
|
+
* @returns {Readable} [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents.
|
|
35
|
+
* @see [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#stream_new_stream_readable_options).
|
|
36
|
+
* @see [Node.js stream backpressure guide](https://nodejs.org/en/docs/guides/backpressuring-in-streams).
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A GraphQL operation object in a shape that can be consumed and executed by
|
|
41
|
+
* most GraphQL servers.
|
|
42
|
+
* @kind typedef
|
|
43
|
+
* @name GraphQLOperation
|
|
44
|
+
* @type {object}
|
|
45
|
+
* @prop {string} query GraphQL document containing queries and fragments.
|
|
46
|
+
* @prop {string|null} [operationName] GraphQL document operation name to execute.
|
|
47
|
+
* @prop {object|null} [variables] GraphQL document operation variables and values map.
|
|
48
|
+
* @see [GraphQL over HTTP spec](https://github.com/graphql/graphql-over-http).
|
|
49
|
+
* @see [Apollo Server POST requests](https://www.apollographql.com/docs/apollo-server/requests/#post-requests).
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Processes a
|
|
54
|
+
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
55
|
+
* @kind typedef
|
|
56
|
+
* @name ProcessRequestFunction
|
|
57
|
+
* @type {Function}
|
|
58
|
+
* @param {IncomingMessage} request [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage).
|
|
59
|
+
* @param {ServerResponse} response [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse).
|
|
60
|
+
* @param {ProcessRequestOptions} [options] Options for processing the request.
|
|
61
|
+
* @returns {Promise<GraphQLOperation | Array<GraphQLOperation>>} GraphQL operation or batch of operations for a GraphQL server to consume (usually as the request body).
|
|
62
|
+
* @see [`processRequest`]{@link processRequest}.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Options for processing a
|
|
67
|
+
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec);
|
|
68
|
+
* mostly relating to security, performance and limits.
|
|
69
|
+
* @kind typedef
|
|
70
|
+
* @name ProcessRequestOptions
|
|
71
|
+
* @type {object}
|
|
72
|
+
* @prop {number} [maxFieldSize=1000000] Maximum allowed non-file multipart form field size in bytes; enough for your queries.
|
|
73
|
+
* @prop {number} [maxFileSize=Infinity] Maximum allowed file size in bytes.
|
|
74
|
+
* @prop {number} [maxFiles=Infinity] Maximum allowed number of files.
|
|
75
|
+
*/
|
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";
|