graphql-upload 17.0.0 → 17.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GraphQLUpload.mjs +3 -3
- package/Upload.mjs +1 -1
- package/graphqlUploadExpress.mjs +13 -12
- package/graphqlUploadKoa.mjs +9 -10
- package/package.json +25 -20
- package/processRequest.mjs +3 -3
- package/readme.md +2 -2
- package/requestFinished.mjs +17 -0
package/GraphQLUpload.mjs
CHANGED
|
@@ -8,12 +8,12 @@ import Upload from "./Upload.mjs";
|
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* A GraphQL scalar `Upload` that can be used in a
|
|
11
|
-
* [`GraphQLSchema`](https://graphql.org/
|
|
12
|
-
* value in resolvers is a promise that resolves
|
|
11
|
+
* [`GraphQLSchema`](https://www.graphql-js.org/api-v17/type/#graphqlschema).
|
|
12
|
+
* Its value in resolvers is a promise that resolves
|
|
13
13
|
* {@link FileUpload file upload details} for processing and storage.
|
|
14
14
|
* @example
|
|
15
15
|
* A schema built using the function
|
|
16
|
-
* [`makeExecutableSchema`](https://
|
|
16
|
+
* [`makeExecutableSchema`](https://the-guild.dev/graphql/tools/docs/generate-schema#makeexecutableschema)
|
|
17
17
|
* from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema):
|
|
18
18
|
*
|
|
19
19
|
* ```js
|
package/Upload.mjs
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
11
11
|
* The {@linkcode processRequest} function places references to an instance of
|
|
12
12
|
* this class wherever the file is expected in the GraphQL operation. The scalar
|
|
13
|
-
* {@linkcode GraphQLUpload} derives
|
|
13
|
+
* {@linkcode GraphQLUpload} derives its value from {@linkcode Upload.promise}.
|
|
14
14
|
*/
|
|
15
15
|
export default class Upload {
|
|
16
16
|
constructor() {
|
package/graphqlUploadExpress.mjs
CHANGED
|
@@ -9,13 +9,20 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import defaultProcessRequest from "./processRequest.mjs";
|
|
12
|
+
import requestFinished from "./requestFinished.mjs";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Creates [Express](https://expressjs.com) middleware that processes incoming
|
|
15
16
|
* [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
16
17
|
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
|
|
17
|
-
* the request `body` to be similar to a conventional GraphQL
|
|
18
|
-
* following GraphQL middleware to consume.
|
|
18
|
+
* the Express request property `body` to be similar to a conventional GraphQL
|
|
19
|
+
* POST request for following GraphQL middleware to consume. Also, it
|
|
20
|
+
* temporarily monkey patches the Express response method
|
|
21
|
+
* {@linkcode Response.send send} to first wait for the request to finish
|
|
22
|
+
* (either because it disconnects early, or it finishes uploading and the
|
|
23
|
+
* response can be sent), because sending a response before the request has
|
|
24
|
+
* finished uploading typically causes the HTTP client to error without
|
|
25
|
+
* processing the response.
|
|
19
26
|
* @param {ProcessRequestOptions & {
|
|
20
27
|
* processRequest?: ProcessRequestFunction,
|
|
21
28
|
* }} options Options.
|
|
@@ -44,27 +51,21 @@ export default function graphqlUploadExpress({
|
|
|
44
51
|
...processRequestOptions
|
|
45
52
|
} = {}) {
|
|
46
53
|
/**
|
|
47
|
-
* [Express](https://expressjs.com) middleware that processes incoming
|
|
48
|
-
* [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
49
|
-
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
|
|
50
|
-
* the request `body` to be similar to a conventional GraphQL POST request for
|
|
51
|
-
* following GraphQL middleware to consume.
|
|
52
54
|
* @param {Request} request Express request.
|
|
53
55
|
* @param {Response} response Express response.
|
|
54
56
|
* @param {NextFunction} next Invokes the next middleware.
|
|
55
57
|
*/
|
|
56
58
|
function graphqlUploadExpressMiddleware(request, response, next) {
|
|
57
59
|
if (request.is("multipart/form-data")) {
|
|
58
|
-
|
|
60
|
+
// Todo: Find a less hacky way that avoids monkey patching.
|
|
59
61
|
const { send } = response;
|
|
60
|
-
|
|
61
|
-
// @ts-ignore Todo: Find a less hacky way to prevent sending a response
|
|
62
|
-
// before the request has ended.
|
|
63
62
|
response.send = (...args) => {
|
|
64
|
-
|
|
63
|
+
requestFinished(request).then(() => {
|
|
65
64
|
response.send = send;
|
|
66
65
|
response.send(...args);
|
|
67
66
|
});
|
|
67
|
+
|
|
68
|
+
return response;
|
|
68
69
|
};
|
|
69
70
|
|
|
70
71
|
processRequest(request, response, processRequestOptions)
|
package/graphqlUploadKoa.mjs
CHANGED
|
@@ -9,13 +9,19 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import defaultProcessRequest from "./processRequest.mjs";
|
|
12
|
+
import requestFinished from "./requestFinished.mjs";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Creates [Koa](https://koajs.com) middleware that processes incoming
|
|
15
16
|
* [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
16
17
|
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
|
|
17
|
-
* the request `body` to be similar to a conventional
|
|
18
|
-
* following GraphQL middleware to consume.
|
|
18
|
+
* the Koa context `request` property `body` to be similar to a conventional
|
|
19
|
+
* GraphQL POST request for following GraphQL middleware to consume. Also, after
|
|
20
|
+
* awaiting the next middleware it waits for the request to finish (either
|
|
21
|
+
* because it disconnects early, or it finishes uploading and the response can
|
|
22
|
+
* be sent), because sending a response before the request has finished
|
|
23
|
+
* uploading typically causes the HTTP client to error without processing the
|
|
24
|
+
* response.
|
|
19
25
|
* @param {ProcessRequestOptions & {
|
|
20
26
|
* processRequest?: ProcessRequestFunction,
|
|
21
27
|
* }} options Options.
|
|
@@ -45,18 +51,11 @@ export default function graphqlUploadKoa({
|
|
|
45
51
|
...processRequestOptions
|
|
46
52
|
} = {}) {
|
|
47
53
|
/**
|
|
48
|
-
* [Koa](https://koajs.com) middleware that processes incoming
|
|
49
|
-
* [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
50
|
-
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
|
|
51
|
-
* the request `body` to be similar to a conventional GraphQL POST request for
|
|
52
|
-
* following GraphQL middleware to consume.
|
|
53
54
|
* @param {ParameterizedContext} ctx Koa context.
|
|
54
55
|
* @param {Next} next Invokes the next middleware.
|
|
55
56
|
*/
|
|
56
57
|
async function graphqlUploadKoaMiddleware(ctx, next) {
|
|
57
58
|
if (ctx.request.is("multipart/form-data")) {
|
|
58
|
-
const requestEnd = new Promise((resolve) => ctx.req.on("end", resolve));
|
|
59
|
-
|
|
60
59
|
try {
|
|
61
60
|
// @ts-ignore This is conventional.
|
|
62
61
|
ctx.request.body = await processRequest(
|
|
@@ -66,7 +65,7 @@ export default function graphqlUploadKoa({
|
|
|
66
65
|
);
|
|
67
66
|
await next();
|
|
68
67
|
} finally {
|
|
69
|
-
await
|
|
68
|
+
await requestFinished(ctx.req);
|
|
70
69
|
}
|
|
71
70
|
} else await next();
|
|
72
71
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-upload",
|
|
3
|
-
"version": "17.
|
|
3
|
+
"version": "17.1.0",
|
|
4
4
|
"description": "Middleware and a scalar Upload 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": {
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"graphqlUploadKoa.mjs",
|
|
37
37
|
"ignoreStream.mjs",
|
|
38
38
|
"processRequest.mjs",
|
|
39
|
+
"requestFinished.mjs",
|
|
39
40
|
"Upload.mjs"
|
|
40
41
|
],
|
|
41
42
|
"sideEffects": false,
|
|
@@ -50,10 +51,16 @@
|
|
|
50
51
|
"engines": {
|
|
51
52
|
"node": "^18.18.0 || ^20.9.0 || >=22.0.0"
|
|
52
53
|
},
|
|
54
|
+
"devEngines": {
|
|
55
|
+
"runtime": {
|
|
56
|
+
"name": "node",
|
|
57
|
+
"version": "^22.13.0 || ^24.0.0 || >=26.0.0"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
53
60
|
"peerDependencies": {
|
|
54
|
-
"@types/express": "4.0.29
|
|
55
|
-
"@types/koa": "^2.11.4",
|
|
56
|
-
"graphql": "^16.3.0"
|
|
61
|
+
"@types/express": "^4.0.29 || ^5.0.0",
|
|
62
|
+
"@types/koa": "^2.11.4 || ^3.0.0",
|
|
63
|
+
"graphql": "^16.3.0 || ^17.0.0"
|
|
57
64
|
},
|
|
58
65
|
"peerDependenciesMeta": {
|
|
59
66
|
"@types/express": {
|
|
@@ -73,27 +80,25 @@
|
|
|
73
80
|
"object-path": "^0.11.8"
|
|
74
81
|
},
|
|
75
82
|
"devDependencies": {
|
|
76
|
-
"@eslint/js": "^
|
|
77
|
-
"@types/express": "^5.0.
|
|
78
|
-
"@types/koa": "^
|
|
79
|
-
"async-listen": "^3.0
|
|
80
|
-
"
|
|
81
|
-
"eslint": "^
|
|
82
|
-
"eslint-plugin-
|
|
83
|
-
"
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"prettier": "^3.3.3",
|
|
90
|
-
"typescript": "^5.6.2"
|
|
83
|
+
"@eslint/js": "^10.0.1",
|
|
84
|
+
"@types/express": "^5.0.6",
|
|
85
|
+
"@types/koa": "^3.0.3",
|
|
86
|
+
"async-listen": "^3.1.0",
|
|
87
|
+
"eslint": "^10.6.0",
|
|
88
|
+
"eslint-plugin-jsdoc": "^63.0.10",
|
|
89
|
+
"eslint-plugin-simple-import-sort": "^14.0.0",
|
|
90
|
+
"express": "^5.2.1",
|
|
91
|
+
"globals": "^17.7.0",
|
|
92
|
+
"graphql": "^17.0.2",
|
|
93
|
+
"koa": "^3.2.1",
|
|
94
|
+
"prettier": "^3.9.1",
|
|
95
|
+
"typescript": "^7.0.2"
|
|
91
96
|
},
|
|
92
97
|
"scripts": {
|
|
93
98
|
"prettier": "prettier -c .",
|
|
94
99
|
"eslint": "eslint",
|
|
95
100
|
"types": "tsc -p jsconfig.json",
|
|
96
|
-
"tests": "coverage-
|
|
101
|
+
"tests": "node --test --experimental-test-coverage --test-coverage-lines=100 --test-coverage-branches=100 --test-coverage-functions=100 --test-coverage-exclude='**/*.test.*' --test-reporter=spec",
|
|
97
102
|
"test": "node --run prettier && node --run eslint && node --run types && node --run tests",
|
|
98
103
|
"prepublishOnly": "node --run test"
|
|
99
104
|
}
|
package/processRequest.mjs
CHANGED
|
@@ -23,7 +23,7 @@ import Upload from "./Upload.mjs";
|
|
|
23
23
|
* It parses the `operations` and `map` fields to create an {@linkcode Upload}
|
|
24
24
|
* instance for each expected file upload, placing references wherever the file
|
|
25
25
|
* is expected in the GraphQL operation for the {@linkcode GraphQLUpload} scalar
|
|
26
|
-
* to derive
|
|
26
|
+
* to derive its value. Errors are created with
|
|
27
27
|
* [`http-errors`](https://npm.im/http-errors) to assist in sending responses
|
|
28
28
|
* with appropriate HTTP status codes. Used to create custom middleware.
|
|
29
29
|
* @type {ProcessRequestFunction}
|
|
@@ -380,7 +380,7 @@ export default function processRequest(
|
|
|
380
380
|
* [Node.js readable stream](https://nodejs.org/api/stream.html#readable-streams)
|
|
381
381
|
* of the file’s contents.
|
|
382
382
|
* @see [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#new-streamreadableoptions).
|
|
383
|
-
* @see [Node.js stream backpressure guide](https://nodejs.org/
|
|
383
|
+
* @see [Node.js stream backpressure guide](https://nodejs.org/learn/modules/backpressuring-in-streams).
|
|
384
384
|
*/
|
|
385
385
|
|
|
386
386
|
/**
|
|
@@ -391,7 +391,7 @@ export default function processRequest(
|
|
|
391
391
|
* strings (without splitting multi-byte characters across chunks) instead of
|
|
392
392
|
* Node.js [`Buffer`](https://nodejs.org/api/buffer.html#buffer) instances.
|
|
393
393
|
* Supported values depend on the
|
|
394
|
-
* [`Buffer` implementation](https://github.com/nodejs/node/blob/
|
|
394
|
+
* [`Buffer` implementation](https://github.com/nodejs/node/blob/v26.5.0/lib/buffer.js#L695-L788)
|
|
395
395
|
* and include `utf8`, `ucs2`, `utf16le`, `latin1`, `ascii`, `base64`,
|
|
396
396
|
* `base64url`, or `hex`. Defaults to `utf8`.
|
|
397
397
|
* @property {ReadStreamOptions["highWaterMark"]} [highWaterMark] Maximum number
|
package/readme.md
CHANGED
|
@@ -12,7 +12,7 @@ Middleware and a scalar [`Upload`](./GraphQLUpload.mjs) to add support for [Grap
|
|
|
12
12
|
>
|
|
13
13
|
> First, check if there are [GraphQL multipart request spec server implementations](https://github.com/jaydenseric/graphql-multipart-request-spec#server) (most for Node.js integrate [`graphql-upload`](https://npm.im/graphql-upload)) that are more suitable for your environment than a manual setup.
|
|
14
14
|
|
|
15
|
-
To install [`graphql-upload`](https://npm.im/graphql-upload) and its peer dependency [`graphql`](https://npm.im/graphql) with [npm](https://npmjs.com/
|
|
15
|
+
To install [`graphql-upload`](https://npm.im/graphql-upload) and its peer dependency [`graphql`](https://npm.im/graphql) with [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), run:
|
|
16
16
|
|
|
17
17
|
```sh
|
|
18
18
|
npm install graphql-upload graphql
|
|
@@ -20,7 +20,7 @@ npm install graphql-upload graphql
|
|
|
20
20
|
|
|
21
21
|
Use the middleware [`graphqlUploadKoa`](./graphqlUploadKoa.mjs) or [`graphqlUploadExpress`](./graphqlUploadExpress.mjs) just before GraphQL middleware. Alternatively, use the function [`processRequest`](./processRequest.mjs) to create custom middleware.
|
|
22
22
|
|
|
23
|
-
A schema built with separate SDL and resolvers (e.g. using the function [`makeExecutableSchema`](https://
|
|
23
|
+
A schema built with separate SDL and resolvers (e.g. using the function [`makeExecutableSchema`](https://the-guild.dev/graphql/tools/docs/generate-schema#makeexecutableschema) from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema)) requires the scalar [`Upload`](./GraphQLUpload.mjs) to be setup.
|
|
24
24
|
|
|
25
25
|
Then, the scalar [`Upload`](./GraphQLUpload.mjs) can be used for query or mutation arguments. For how to use the scalar value in resolvers, see the documentation in the module [`GraphQLUpload.mjs`](./GraphQLUpload.mjs).
|
|
26
26
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/** @import { IncomingMessage } from "node:http" */
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Waits for a request to finish (either because it disconnects early, or it
|
|
7
|
+
* finishes uploading).
|
|
8
|
+
* @see [Node.js stream utility function `finished`](https://nodejs.org/api/stream.html#streamfinishedstream-options).
|
|
9
|
+
* @param {IncomingMessage} request Request.
|
|
10
|
+
* @returns {Promise<void>} Resolves once the request has finished.
|
|
11
|
+
*/
|
|
12
|
+
export default async function requestFinished(request) {
|
|
13
|
+
if (!request.closed)
|
|
14
|
+
await new Promise((resolve) => {
|
|
15
|
+
request.once("close", resolve);
|
|
16
|
+
});
|
|
17
|
+
}
|