graphql-upload 8.0.7 → 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 CHANGED
@@ -1,5 +1,114 @@
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
+
23
+ ## 10.0.0
24
+
25
+ ### Major
26
+
27
+ - Updated Node.js support from v8.10+ to v10+, as earlier versions have reached end-of-life.
28
+ - Updated the [`fs-capacitor`](https://npm.im/fs-capacitor) dependency to v6, which now requires Node.js v10+, via [#179](https://github.com/jaydenseric/graphql-upload/pull/179).
29
+ - Updated dev dependencies, some of which now require Node.js v10+.
30
+ - Replaced the [`tap`](https://npm.im/tap) dev dependency with [`test-director`](https://npm.im/test-director), [`coverage-node`](https://npm.im/coverage-node), and [`hard-rejection`](https://npm.im/hard-rejection) to improve the dev experience and reduce the dev install size by ~75.7 MB. These new dev dependencies require Node.js v10+.
31
+ - Reorganized files. This is only a breaking change for projects using undocumented deep imports.
32
+ - Removed now redundant Node.js version compatibility logic in the `processRequest` function.
33
+ - The `processRequest` function now places references to instances of the now exported and documented `Upload` class in the GraphQL operation for the `GraphQLUpload` scalar to derive its value, and the `GraphQLUpload` scalar now throws a `GraphQLError` when it parses an invalid value, fixing [#175](https://github.com/jaydenseric/graphql-upload/issues/175) via [#181](https://github.com/jaydenseric/graphql-upload/pull/181).
34
+ - The `GraphQLUpload` scalar `parseLiteral` and `serialize` methods now throw `GraphQLError` (instead of `Error`) instances, with tweaked messages.
35
+
36
+ ### Minor
37
+
38
+ - The `createReadStream` function in resolved file uploads now accepts options to configure the encoding and high water mark, fixing [#177](https://github.com/jaydenseric/graphql-upload/issues/177) via [#179](https://github.com/jaydenseric/graphql-upload/pull/179).
39
+
40
+ ### Patch
41
+
42
+ - Removed the now redundant [`eslint-plugin-import-order-alphabetical`](https://npm.im/eslint-plugin-import-order-alphabetical) and [`express-async-handler`](https://npm.im/express-async-handler) dev dependencies.
43
+ - Stop using [`husky`](https://npm.im/husky) and [`lint-staged`](https://npm.im/lint-staged).
44
+ - Use [`isobject`](https://npm.im/isobject) for checking if values are enumerable, non-array objects.
45
+ - Tests have been massively reorganized, refactored, and improved.
46
+ - Test the `GraphQLUpload` scalar.
47
+ - Test the `ignoreStream` function.
48
+ - Moved the `Upload` class to its own file.
49
+ - Added JSDoc for the `Upload` class instance property `file`.
50
+ - Test the `Upload` class.
51
+ - Improved JSDoc `FileUpload` typedef description.
52
+ - Removed now redundant `eslint-disable-next-line` comments.
53
+ - Use strict mode for scripts.
54
+
55
+ ## 9.0.0
56
+
57
+ ### Major
58
+
59
+ - Updated Node.js support from v8.5+ to v8.10+, to match what the [`eslint`](https://npm.im/eslint) dev dependency now supports. This is unlikely to be a breaking change for the published package.
60
+ - Removed the `Upload` scalar promise resolved `stream` property that has been deprecated since v7, along with associated tests.
61
+ - ESM is no longer published, due to CJS/ESM compatibility issues across recent Node.js versions, via [#169](https://github.com/jaydenseric/graphql-upload/pull/169).
62
+ - The file structure and non-index file exports have changed. This should only affect projects using undocumented deep imports.
63
+
64
+ ### Minor
65
+
66
+ - Updated the [`fs-capacitor`](https://npm.im/fs-capacitor) dependency to v4 to support Node.js v13, making required changes to the source and tests, via [#166](https://github.com/jaydenseric/graphql-upload/pull/166).
67
+ - JSDoc comments are now included in the published code.
68
+ - Several anonymous functions have been named, for better error stack traces.
69
+ - Setup [GitHub Sponsors funding](https://github.com/sponsors/jaydenseric):
70
+ - Added `.github/funding.yml` to display a sponsor button in GitHub.
71
+ - Added a `package.json` `funding` field to enable npm CLI funding features.
72
+
73
+ ### Patch
74
+
75
+ - Updated dev dependencies.
76
+ - Removed the `.nycrc.json` file:
77
+ - [`tap`](https://npm.im/tap) now ignores test files by default.
78
+ - The `lib/test-helpers` directory is now ignored using [`tap`](https://npm.im/tap) CLI arguments due to [tapjs/node-tap#612](https://github.com/tapjs/node-tap/issues/612).
79
+ - Removed the `esm` and `mjs` package tags; they will be added back once native ESM is properly supported.
80
+ - Updated JSDoc code examples to use CJS instead of ESM, as native ESM is not yet properly supported.
81
+ - No longer test [`fs-capacitor`](https://npm.im/fs-capacitor) implementation details such as temp file creation and cleanup.
82
+ - Commented the reasons for several `istanbul ignore next` comments.
83
+
84
+ ## 8.1.0
85
+
86
+ ### Minor
87
+
88
+ - `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).
89
+ - When the file size limit is exceeded, mention how many bytes the limit is in the stream error message.
90
+ - 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.
91
+
92
+ ### Patch
93
+
94
+ - Updated dependencies.
95
+ - Due to updated dependencies: Lint fixes, removed redundant `eslint-disable-next-line` comments, and regenerated the readme.
96
+ - Documented [`koa-graphql`](https://npm.im/koa-graphql) as known to be compatible, via [#156](https://github.com/jaydenseric/graphql-upload/pull/156).
97
+ - Fixed a readme typo, via [#161](https://github.com/jaydenseric/graphql-upload/pull/161).
98
+ - Use GitHub Actions instead of Travis for CI.
99
+ - Removed `package-lock.json` from `.gitignore` and `.prettierignore`, as it’s disabled in `.npmrc` anyway.
100
+ - New file structure.
101
+ - Explicitly defined main exports (instead of using `export * from`) to prevent accidental public exposure of internal APIs.
102
+ - Moved JSDoc typedefs into the index main entry file, alphabetically sorted.
103
+ - Nicer Browserslist query syntax.
104
+ - Replaced the `isObject` helper with a smarter and tested `isEnumerableObject`.
105
+ - Removed the `isString` helper.
106
+ - 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).
107
+ - Removed a workaround from the `startServer` test helper.
108
+ - Added a new `ProcessRequestFunction` JSDoc type, and applied it to `processRequest`.
109
+ - Renamed the `UploadOptions` JSDoc type to `ProcessRequestOptions`.
110
+ - Misc. documentation improvements.
111
+
3
112
  ## 8.0.7
4
113
 
5
114
  ### Patch
@@ -217,7 +326,7 @@ Big thanks to new collaborator [@mike-marcacci](https://github.com/mike-marcacci
217
326
  - Updated dependencies.
218
327
  - Simplified npm scripts.
219
328
  - Readme updates:
220
- - 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).
221
330
  - Explained how to use `processRequest` for custom middleware.
222
331
  - Improved usage instructions.
223
332
  - Display oldest supported Node.js version.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-upload",
3
- "version": "8.0.7",
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": {
@@ -11,6 +11,7 @@
11
11
  "repository": "github:jaydenseric/graphql-upload",
12
12
  "homepage": "https://github.com/jaydenseric/graphql-upload#readme",
13
13
  "bugs": "https://github.com/jaydenseric/graphql-upload/issues",
14
+ "funding": "https://github.com/sponsors/jaydenseric",
14
15
  "keywords": [
15
16
  "graphql",
16
17
  "upload",
@@ -24,59 +25,59 @@
24
25
  "mjs"
25
26
  ],
26
27
  "files": [
27
- "lib",
28
- "!lib/test.*"
28
+ "private",
29
+ "public"
29
30
  ],
30
- "main": "lib",
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
+ },
31
41
  "engines": {
32
- "node": ">=8.5"
42
+ "node": "^10.13.0 || ^12.0.0 || >= 13.7.0"
33
43
  },
34
- "browserslist": "node 8.5",
35
44
  "peerDependencies": {
36
- "graphql": "0.13.1 - 14"
45
+ "graphql": "0.13.1 - 15"
37
46
  },
38
47
  "dependencies": {
39
48
  "busboy": "^0.3.1",
40
- "fs-capacitor": "^2.0.4",
41
- "http-errors": "^1.7.2",
49
+ "fs-capacitor": "^6.1.0",
50
+ "http-errors": "^1.7.3",
51
+ "isobject": "^4.0.0",
42
52
  "object-path": "^0.11.4"
43
53
  },
44
54
  "devDependencies": {
45
- "@babel/cli": "^7.4.4",
46
- "@babel/core": "^7.4.5",
47
- "@babel/preset-env": "^7.4.5",
48
- "babel-eslint": "^10.0.1",
49
- "eslint": "^5.16.0",
50
- "eslint-config-env": "^6.0.0",
51
- "eslint-config-prettier": "^4.3.0",
52
- "eslint-plugin-import": "^2.17.3",
53
- "eslint-plugin-import-order-alphabetical": "^0.0.2",
54
- "eslint-plugin-node": "^9.1.0",
55
- "eslint-plugin-prettier": "^3.1.0",
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",
56
63
  "express": "^4.17.1",
57
- "express-async-handler": "^1.1.4",
58
- "form-data": "^2.3.3",
59
- "graphql": "^14.3.1",
60
- "husky": "^2.4.1",
61
- "jsdoc-md": "^3.0.0",
62
- "koa": "^2.7.0",
63
- "lint-staged": "^8.2.0",
64
+ "form-data": "^3.0.0",
65
+ "graphql": "^15.0.0",
66
+ "hard-rejection": "^2.1.0",
67
+ "jsdoc-md": "^6.0.0",
68
+ "koa": "^2.12.0",
64
69
  "node-fetch": "^2.6.0",
65
- "prettier": "^1.18.2",
66
- "tap": "^14.2.2"
70
+ "prettier": "^2.0.5",
71
+ "test-director": "^4.0.1"
67
72
  },
68
73
  "scripts": {
69
- "prepare": "npm run prepare:clean && npm run prepare:mjs && npm run prepare:js && npm run prepare:jsdoc && npm run prepare:prettier",
70
- "prepare:clean": "rm -rf lib",
71
- "prepare:mjs": "BABEL_ESM=1 babel src -d lib --keep-file-extension",
72
- "prepare:js": "babel src -d lib",
74
+ "prepare": "npm run prepare:jsdoc && npm run prepare:prettier",
73
75
  "prepare:jsdoc": "jsdoc-md",
74
- "prepare:prettier": "prettier 'lib/**/*.{mjs,js}' readme.md --write",
75
- "test": "npm run test:eslint && npm run test:prettier && npm run test:mjs && npm run test:js",
76
- "test:eslint": "eslint . --ext mjs,js",
77
- "test:prettier": "prettier '**/*.{json,yml,md}' -l",
78
- "test:mjs": "node --experimental-modules --no-warnings lib/test | tap-mocha-reporter classic",
79
- "test:js": "node lib/test | tap-mocha-reporter classic",
76
+ "prepare:prettier": "prettier --write readme.md",
77
+ "test": "npm run test:eslint && npm run test:prettier && npm run test:api",
78
+ "test:eslint": "eslint .",
79
+ "test:prettier": "prettier -c .",
80
+ "test:api": "coverage-node -r hard-rejection/register test",
80
81
  "prepublishOnly": "npm test"
81
82
  }
82
83
  }
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Official [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
5
+ * URL. Useful for error messages, etc.
6
+ * @kind constant
7
+ * @name SPEC_URL
8
+ * @type {string}
9
+ * @ignore
10
+ */
11
+ exports.SPEC_URL =
12
+ 'https://github.com/jaydenseric/graphql-multipart-request-spec';
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Safely ignores a Node.js readable stream.
5
+ * @kind function
6
+ * @name ignoreStream
7
+ * @param {ReadableStream} stream Node.js readable stream.
8
+ * @ignore
9
+ */
10
+ module.exports = function ignoreStream(stream) {
11
+ // Prevent an unhandled error from crashing the process.
12
+ stream.on('error', () => {});
13
+
14
+ // Waste the stream.
15
+ stream.resume();
16
+ };
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ const { GraphQLScalarType, GraphQLError } = require('graphql');
4
+ const Upload = require('./Upload');
5
+
6
+ /**
7
+ * A GraphQL `Upload` scalar that can be used in a
8
+ * [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema).
9
+ * It’s value in resolvers is a promise that resolves
10
+ * [file upload details]{@link FileUpload} for processing and storage.
11
+ * @kind class
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
+ * ```
29
+ * @example <caption>Setup for a schema built with [`makeExecutableSchema`](https://apollographql.com/docs/graphql-tools/generate-schema#makeExecutableSchema).</caption>
30
+ * ```js
31
+ * const { makeExecutableSchema } = require('graphql-tools');
32
+ * const { GraphQLUpload } = require('graphql-upload');
33
+ *
34
+ * const schema = makeExecutableSchema({
35
+ * typeDefs: /* GraphQL *\/ `
36
+ * scalar Upload
37
+ * `,
38
+ * resolvers: {
39
+ * Upload: GraphQLUpload,
40
+ * },
41
+ * });
42
+ * ```
43
+ * @example <caption>A manually constructed schema with an image upload mutation.</caption>
44
+ * ```js
45
+ * const {
46
+ * GraphQLSchema,
47
+ * GraphQLObjectType,
48
+ * GraphQLBoolean,
49
+ * } = 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
+ });
@@ -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` scalar]{@link GraphQLUpload} 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');
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
+ };
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ const defaultProcessRequest = require('./processRequest');
4
+
5
+ /**
6
+ * Creates [Express](https://expressjs.com) middleware that processes
7
+ * [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
8
+ * using [`processRequest`]{@link processRequest}, ignoring non-multipart
9
+ * requests. It sets the request body to be
10
+ * [similar to a conventional GraphQL POST request]{@link GraphQLOperation} for
11
+ * following GraphQL middleware to consume.
12
+ * @kind function
13
+ * @name graphqlUploadExpress
14
+ * @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
15
+ * @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
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
+ * ```
33
+ * @example <caption>Basic [`express-graphql`](https://npm.im/express-graphql) setup.</caption>
34
+ * ```js
35
+ * const express = require('express');
36
+ * const graphqlHTTP = require('express-graphql');
37
+ * const { graphqlUploadExpress } = require('graphql-upload');
38
+ * const schema = require('./schema');
39
+ *
40
+ * express()
41
+ * .use(
42
+ * '/graphql',
43
+ * graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
44
+ * graphqlHTTP({ schema })
45
+ * )
46
+ * .listen(3000);
47
+ * ```
48
+ */
49
+ module.exports = function graphqlUploadExpress({
50
+ processRequest = defaultProcessRequest,
51
+ ...processRequestOptions
52
+ } = {}) {
53
+ return function graphqlUploadExpressMiddleware(request, response, next) {
54
+ if (!request.is('multipart/form-data')) return next();
55
+
56
+ const finished = new Promise((resolve) => request.on('end', resolve));
57
+ const { send } = response;
58
+
59
+ response.send = (...args) => {
60
+ finished.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
+ };
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+
3
+ const defaultProcessRequest = require('./processRequest');
4
+
5
+ /**
6
+ * Creates [Koa](https://koajs.com) middleware that processes
7
+ * [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
8
+ * using [`processRequest`]{@link processRequest}, ignoring non-multipart
9
+ * requests. It sets the request body to be
10
+ * [similar to a conventional GraphQL POST request]{@link GraphQLOperation} for
11
+ * following GraphQL middleware to consume.
12
+ * @kind function
13
+ * @name graphqlUploadKoa
14
+ * @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
15
+ * @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
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
+ * ```
33
+ * @example <caption>Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup.</caption>
34
+ * ```js
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');
40
+ *
41
+ * new Koa()
42
+ * .use(errorHandler())
43
+ * .use(bodyParser())
44
+ * .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
45
+ * .use(execute({ schema }))
46
+ * .listen(3000);
47
+ * ```
48
+ */
49
+ module.exports = function graphqlUploadKoa({
50
+ processRequest = defaultProcessRequest,
51
+ ...processRequestOptions
52
+ } = {}) {
53
+ return async function graphqlUploadKoaMiddleware(ctx, next) {
54
+ if (!ctx.request.is('multipart/form-data')) return next();
55
+
56
+ const finished = new Promise((resolve) => ctx.req.on('end', resolve));
57
+
58
+ try {
59
+ ctx.request.body = await processRequest(
60
+ ctx.req,
61
+ ctx.res,
62
+ processRequestOptions
63
+ );
64
+ await next();
65
+ } finally {
66
+ await finished;
67
+ }
68
+ };
69
+ };
@@ -0,0 +1,69 @@
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 [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of an [uploading file’s]{@link FileUpload} contents, for processing and storage. Multiple calls create independent streams. Throws if called after all resolvers have resolved, or after an error has interrupted the request.
24
+ * @kind typedef
25
+ * @name FileUploadCreateReadStream
26
+ * @type {Function}
27
+ * @param {object} [options] [`fs-capacitor`](https://npm.im/fs-capacitor) [`ReadStreamOptions`](https://github.com/mike-marcacci/fs-capacitor#readstreamoptions).
28
+ * @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`.
29
+ * @param {number} [options.highWaterMark=16384] Maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.
30
+ * @returns {Readable} [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents.
31
+ * @see [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#stream_new_stream_readable_options).
32
+ * @see [Node.js stream backpressure guide](https://nodejs.org/es/docs/guides/backpressuring-in-streams).
33
+ */
34
+
35
+ /**
36
+ * A GraphQL operation object in a shape that can be consumed and executed by
37
+ * most GraphQL servers.
38
+ * @kind typedef
39
+ * @name GraphQLOperation
40
+ * @type {object}
41
+ * @prop {string} query GraphQL document containing queries and fragments.
42
+ * @prop {string|null} [operationName] GraphQL document operation name to execute.
43
+ * @prop {object|null} [variables] GraphQL document operation variables and values map.
44
+ * @see [GraphQL over HTTP spec](https://github.com/APIs-guru/graphql-over-http#request-parameters).
45
+ * @see [Apollo Server POST requests](https://www.apollographql.com/docs/apollo-server/requests#postRequests).
46
+ */
47
+
48
+ /**
49
+ * Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
50
+ * @kind typedef
51
+ * @name ProcessRequestFunction
52
+ * @type {Function}
53
+ * @param {IncomingMessage} request [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage).
54
+ * @param {ServerResponse} response [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse).
55
+ * @param {ProcessRequestOptions} [options] Options for processing the request.
56
+ * @returns {Promise<GraphQLOperation | Array<GraphQLOperation>>} GraphQL operation or batch of operations for a GraphQL server to consume (usually as the request body).
57
+ * @see [`processRequest`]{@link processRequest}.
58
+ */
59
+
60
+ /**
61
+ * Options for processing a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec);
62
+ * mostly relating to security, performance and limits.
63
+ * @kind typedef
64
+ * @name ProcessRequestOptions
65
+ * @type {object}
66
+ * @prop {number} [maxFieldSize=1000000] Maximum allowed non-file multipart form field size in bytes; enough for your queries.
67
+ * @prop {number} [maxFileSize=Infinity] Maximum allowed file size in bytes.
68
+ * @prop {number} [maxFiles=Infinity] Maximum allowed number of files.
69
+ */
@@ -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';