graphql-upload 13.0.0 → 14.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.
@@ -1,12 +1,12 @@
1
+ // @ts-check
2
+
1
3
  "use strict";
2
4
 
3
5
  /**
4
6
  * [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
5
7
  * URL. Useful for error messages, etc.
6
- * @kind constant
7
- * @name GRAPHQL_MULTIPART_REQUEST_SPEC_URL
8
- * @type {string}
9
- * @ignore
10
8
  */
11
- module.exports =
9
+ const GRAPHQL_MULTIPART_REQUEST_SPEC_URL =
12
10
  "https://github.com/jaydenseric/graphql-multipart-request-spec";
11
+
12
+ module.exports = GRAPHQL_MULTIPART_REQUEST_SPEC_URL;
@@ -1,42 +1,28 @@
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 */
1
+ // @ts-check
4
2
 
5
3
  "use strict";
6
4
 
7
5
  const { GraphQLScalarType, GraphQLError } = require("graphql");
8
- const Upload = require("./Upload");
6
+ const Upload = require("./Upload.js");
7
+
8
+ /** @typedef {import("./processRequest").FileUpload} FileUpload */
9
9
 
10
10
  /**
11
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
- * ```
12
+ * [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema). It’s
13
+ * value in resolvers is a promise that resolves
14
+ * {@link FileUpload file upload details} for processing and storage.
15
+ * @example
16
+ * A schema built using
17
+ * [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema_src#makeexecutableschema)
18
+ * from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema):
29
19
  *
30
20
  * ```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
21
  * const { makeExecutableSchema } = require("@graphql-tools/schema");
36
- * const { GraphQLUpload } = require("graphql-upload");
22
+ * const GraphQLUpload = require("graphql-upload/GraphQLUpload.js");
37
23
  *
38
24
  * const schema = makeExecutableSchema({
39
- * typeDefs: /* GraphQL *\/ `
25
+ * typeDefs: `
40
26
  * scalar Upload
41
27
  * `,
42
28
  * resolvers: {
@@ -44,10 +30,12 @@ const Upload = require("./Upload");
44
30
  * },
45
31
  * });
46
32
  * ```
47
- * @example <caption>A manually constructed schema with an image upload mutation.</caption>
33
+ * @example
34
+ * A manually constructed schema with an image upload mutation:
35
+ *
48
36
  * ```js
49
37
  * const { GraphQLSchema, GraphQLObjectType, GraphQLBoolean } = require("graphql");
50
- * const { GraphQLUpload } = require("graphql-upload");
38
+ * const GraphQLUpload = require("graphql-upload/GraphQLUpload.js");
51
39
  *
52
40
  * const schema = new GraphQLSchema({
53
41
  * mutation: new GraphQLObjectType({
@@ -74,17 +62,19 @@ const Upload = require("./Upload");
74
62
  * });
75
63
  * ```
76
64
  */
77
- module.exports = new GraphQLScalarType({
65
+ const GraphQLUpload = new GraphQLScalarType({
78
66
  name: "Upload",
79
67
  description: "The `Upload` scalar type represents a file upload.",
80
68
  parseValue(value) {
81
69
  if (value instanceof Upload) return value.promise;
82
70
  throw new GraphQLError("Upload value invalid.");
83
71
  },
84
- parseLiteral(ast) {
85
- throw new GraphQLError("Upload literal unsupported.", ast);
72
+ parseLiteral(node) {
73
+ throw new GraphQLError("Upload literal unsupported.", { nodes: node });
86
74
  },
87
75
  serialize() {
88
76
  throw new GraphQLError("Upload serialization unsupported.");
89
77
  },
90
78
  });
79
+
80
+ module.exports = GraphQLUpload;
package/Upload.js ADDED
@@ -0,0 +1,55 @@
1
+ // @ts-check
2
+
3
+ "use strict";
4
+
5
+ /** @typedef {import("./GraphQLUpload.js")} GraphQLUpload */
6
+ /** @typedef {import("./processRequest.js")} processRequest */
7
+
8
+ /**
9
+ * A file expected to be uploaded as it was declared in the `map` field of a
10
+ * [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
11
+ * The {@linkcode processRequest} function places references to an instance of
12
+ * this class wherever the file is expected in the GraphQL operation. The scalar
13
+ * {@linkcode GraphQLUpload} derives it’s value from {@linkcode Upload.promise}.
14
+ */
15
+ class Upload {
16
+ constructor() {
17
+ /**
18
+ * Promise that resolves file upload details. This should only be utilized
19
+ * by {@linkcode GraphQLUpload}.
20
+ * @type {Promise<import("./processRequest.js").FileUpload>}
21
+ */
22
+ this.promise = new Promise((resolve, reject) => {
23
+ /**
24
+ * Resolves the upload promise with the file upload details. This should
25
+ * only be utilized by {@linkcode processRequest}.
26
+ * @param {import("./processRequest.js").FileUpload} file File upload
27
+ * details.
28
+ */
29
+ this.resolve = (file) => {
30
+ /**
31
+ * The file upload details, available when the
32
+ * {@linkcode Upload.promise} resolves. This should only be utilized by
33
+ * {@linkcode processRequest}.
34
+ * @type {import("./processRequest.js").FileUpload | undefined}
35
+ */
36
+ this.file = file;
37
+
38
+ resolve(file);
39
+ };
40
+
41
+ /**
42
+ * Rejects the upload promise with an error. This should only be
43
+ * utilized by {@linkcode processRequest}.
44
+ * @param {Error} error Error instance.
45
+ */
46
+ this.reject = reject;
47
+ });
48
+
49
+ // Prevent errors crashing Node.js, see:
50
+ // https://github.com/nodejs/node/issues/20392
51
+ this.promise.catch(() => {});
52
+ }
53
+ }
54
+
55
+ module.exports = Upload;
@@ -0,0 +1,80 @@
1
+ // @ts-check
2
+
3
+ "use strict";
4
+
5
+ const defaultProcessRequest = require("./processRequest.js");
6
+
7
+ /**
8
+ * Creates [Express](https://expressjs.com) middleware that processes incoming
9
+ * [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
10
+ * using {@linkcode processRequest}, ignoring non multipart requests. It sets
11
+ * the request `body` to be similar to a conventional GraphQL POST request for
12
+ * following GraphQL middleware to consume.
13
+ * @param {import("./processRequest.js").ProcessRequestOptions & {
14
+ * processRequest?: import("./processRequest.js").ProcessRequestFunction
15
+ * }} options Options.
16
+ * @returns Express middleware.
17
+ * @example
18
+ * Basic [`express-graphql`](https://npm.im/express-graphql) setup:
19
+ *
20
+ * ```js
21
+ * const express = require("express");
22
+ * const graphqlHTTP = require("express-graphql");
23
+ * const graphqlUploadExpress = require("graphql-upload/graphqlUploadExpress.js");
24
+ * const schema = require("./schema.js");
25
+ *
26
+ * express()
27
+ * .use(
28
+ * "/graphql",
29
+ * graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
30
+ * graphqlHTTP({ schema })
31
+ * )
32
+ * .listen(3000);
33
+ * ```
34
+ */
35
+ function graphqlUploadExpress({
36
+ processRequest = defaultProcessRequest,
37
+ ...processRequestOptions
38
+ } = {}) {
39
+ /**
40
+ * [Express](https://expressjs.com) middleware that processes incoming
41
+ * [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
42
+ * using {@linkcode processRequest}, ignoring non multipart requests. It sets
43
+ * the request `body` to be similar to a conventional GraphQL POST request for
44
+ * following GraphQL middleware to consume.
45
+ * @param {import("express").Request} request
46
+ * @param {import("express").Response} response
47
+ * @param {import("express").NextFunction} next
48
+ */
49
+ function graphqlUploadExpressMiddleware(request, response, next) {
50
+ if (!request.is("multipart/form-data")) return next();
51
+
52
+ const requestEnd = new Promise((resolve) => request.on("end", resolve));
53
+ const { send } = response;
54
+
55
+ // @ts-ignore Todo: Find a less hacky way to prevent sending a response
56
+ // before the request has ended.
57
+ response.send =
58
+ /** @param {Array<unknown>} args */
59
+ (...args) => {
60
+ requestEnd.then(() => {
61
+ response.send = send;
62
+ response.send(...args);
63
+ });
64
+ };
65
+
66
+ processRequest(request, response, processRequestOptions)
67
+ .then((body) => {
68
+ request.body = body;
69
+ next();
70
+ })
71
+ .catch((error) => {
72
+ if (error.status && error.expose) response.status(error.status);
73
+ next(error);
74
+ });
75
+ }
76
+
77
+ return graphqlUploadExpressMiddleware;
78
+ }
79
+
80
+ module.exports = graphqlUploadExpress;
@@ -0,0 +1,69 @@
1
+ // @ts-check
2
+
3
+ "use strict";
4
+
5
+ const defaultProcessRequest = require("./processRequest.js");
6
+
7
+ /**
8
+ * Creates [Koa](https://koajs.com) middleware that processes incoming
9
+ * [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
10
+ * using {@linkcode processRequest}, ignoring non multipart requests. It sets
11
+ * the request `body` to be similar to a conventional GraphQL POST request for
12
+ * following GraphQL middleware to consume.
13
+ * @param {import("./processRequest.js").ProcessRequestOptions & {
14
+ * processRequest?: import("./processRequest.js").ProcessRequestFunction
15
+ * }} options Options.
16
+ * @returns Koa middleware.
17
+ * @example
18
+ * Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup:
19
+ *
20
+ * ```js
21
+ * const Koa = require("koa");
22
+ * const bodyParser = require("koa-bodyparser");
23
+ * const { errorHandler, execute } = require("graphql-api-koa");
24
+ * const graphqlUploadKoa = require("graphql-upload/graphqlUploadKoa.js");
25
+ * const schema = require("./schema.js");
26
+ *
27
+ * new Koa()
28
+ * .use(errorHandler())
29
+ * .use(bodyParser())
30
+ * .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
31
+ * .use(execute({ schema }))
32
+ * .listen(3000);
33
+ * ```
34
+ */
35
+ function graphqlUploadKoa({
36
+ processRequest = defaultProcessRequest,
37
+ ...processRequestOptions
38
+ } = {}) {
39
+ /**
40
+ * [Koa](https://koajs.com) middleware that processes incoming
41
+ * [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
42
+ * using {@linkcode processRequest}, ignoring non multipart requests. It sets
43
+ * the request `body` to be similar to a conventional GraphQL POST request for
44
+ * following GraphQL middleware to consume.
45
+ * @param {import("koa").Context} ctx
46
+ * @param {() => Promise<unknown>} next
47
+ */
48
+ async function graphqlUploadKoaMiddleware(ctx, next) {
49
+ if (!ctx.request.is("multipart/form-data")) return next();
50
+
51
+ const requestEnd = new Promise((resolve) => ctx.req.on("end", resolve));
52
+
53
+ try {
54
+ // @ts-ignore This is conventional.
55
+ ctx.request.body = await processRequest(
56
+ ctx.req,
57
+ ctx.res,
58
+ processRequestOptions
59
+ );
60
+ await next();
61
+ } finally {
62
+ await requestEnd;
63
+ }
64
+ }
65
+
66
+ return graphqlUploadKoaMiddleware;
67
+ }
68
+
69
+ module.exports = graphqlUploadKoa;
@@ -1,16 +1,17 @@
1
+ // @ts-check
2
+
1
3
  "use strict";
2
4
 
3
5
  /**
4
6
  * Safely ignores a Node.js readable stream.
5
- * @kind function
6
- * @name ignoreStream
7
- * @param {ReadableStream} stream Node.js readable stream.
8
- * @ignore
7
+ * @param {import("stream").Readable} stream Node.js readable stream.
9
8
  */
10
- module.exports = function ignoreStream(stream) {
9
+ function ignoreStream(stream) {
11
10
  // Prevent an unhandled error from crashing the process.
12
11
  stream.on("error", () => {});
13
12
 
14
13
  // Waste the stream.
15
14
  stream.resume();
16
- };
15
+ }
16
+
17
+ module.exports = ignoreStream;
package/license.md ADDED
@@ -0,0 +1,9 @@
1
+ # MIT License
2
+
3
+ Copyright Jayden Seric
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-upload",
3
- "version": "13.0.0",
3
+ "version": "14.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": {
@@ -27,59 +27,70 @@
27
27
  "mjs"
28
28
  ],
29
29
  "files": [
30
- "private",
31
- "public"
30
+ "GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js",
31
+ "GraphQLUpload.js",
32
+ "graphqlUploadExpress.js",
33
+ "graphqlUploadKoa.js",
34
+ "ignoreStream.js",
35
+ "processRequest.js",
36
+ "Upload.js"
32
37
  ],
33
38
  "sideEffects": false,
34
- "main": "./public/index.js",
35
39
  "exports": {
36
- ".": {
37
- "import": "./public/index.mjs",
38
- "require": "./public/index.js"
39
- },
40
- "./public/*": "./public/*",
41
- "./package.json": "./package.json"
40
+ "./GraphQLUpload.js": "./GraphQLUpload.js",
41
+ "./graphqlUploadExpress.js": "./graphqlUploadExpress.js",
42
+ "./graphqlUploadKoa.js": "./graphqlUploadKoa.js",
43
+ "./package.json": "./package.json",
44
+ "./processRequest.js": "./processRequest.js",
45
+ "./Upload.js": "./Upload.js"
42
46
  },
43
47
  "engines": {
44
- "node": "^12.22.0 || ^14.17.0 || >= 16.0.0"
48
+ "node": "^14.17.0 || ^16.0.0 || >= 18.0.0"
45
49
  },
46
50
  "peerDependencies": {
47
- "graphql": "0.13.1 - 16"
51
+ "@types/express": "^4.0.29",
52
+ "@types/koa": "^2.11.4",
53
+ "graphql": "^16.3.0"
54
+ },
55
+ "peerDependenciesMeta": {
56
+ "@types/express": {
57
+ "optional": true
58
+ },
59
+ "@types/koa": {
60
+ "optional": true
61
+ }
48
62
  },
49
63
  "dependencies": {
64
+ "@types/busboy": "^0.3.2",
65
+ "@types/node": "*",
66
+ "@types/object-path": "^0.11.1",
50
67
  "busboy": "^0.3.1",
51
68
  "fs-capacitor": "^6.2.0",
52
- "http-errors": "^1.8.1",
69
+ "http-errors": "^2.0.0",
53
70
  "object-path": "^0.11.8"
54
71
  },
55
72
  "devDependencies": {
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",
64
- "express": "^4.17.1",
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",
73
+ "@types/express": "^4.17.13",
74
+ "@types/koa": "^2.13.4",
75
+ "coverage-node": "^6.1.0",
76
+ "eslint": "^8.16.0",
77
+ "eslint-plugin-simple-import-sort": "^7.0.0",
78
+ "express": "^4.18.0",
79
+ "form-data-encoder": "^2.0.1",
80
+ "graphql": "^16.5.0",
70
81
  "koa": "^2.13.4",
71
82
  "node-abort-controller": "^3.0.1",
72
- "node-fetch": "^3.1.0",
73
- "prettier": "^2.5.0",
74
- "test-director": "^7.0.0"
83
+ "node-fetch": "^3.2.3",
84
+ "prettier": "^2.6.2",
85
+ "test-director": "^8.0.2",
86
+ "typescript": "^4.7.1-rc"
75
87
  },
76
88
  "scripts": {
77
- "docs-update": "jsdoc-md",
78
- "docs-check": "jsdoc-md -c",
79
89
  "eslint": "eslint .",
80
90
  "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",
91
+ "types": "tsc -p jsconfig.json",
92
+ "tests": "coverage-node --unhandled-rejections=throw test.mjs",
93
+ "test": "npm run eslint && npm run prettier && npm run types && npm run tests",
83
94
  "prepublishOnly": "npm test"
84
95
  }
85
96
  }