graphql-upload 13.0.0 → 15.0.1

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/readme.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [![npm version](https://badgen.net/npm/v/graphql-upload)](https://npm.im/graphql-upload) [![CI status](https://github.com/jaydenseric/graphql-upload/workflows/CI/badge.svg)](https://github.com/jaydenseric/graphql-upload/actions)
6
6
 
7
- Middleware and an [`Upload`](#class-graphqlupload) scalar to add support for [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) (file uploads via queries and mutations) to various Node.js GraphQL servers.
7
+ Middleware and an [`Upload`](./GraphQLUpload.js) scalar to add support for [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) (file uploads via queries and mutations) to various Node.js GraphQL servers.
8
8
 
9
9
  ## Installation
10
10
 
@@ -16,407 +16,44 @@ To install [`graphql-upload`](https://npm.im/graphql-upload) and the [`graphql`]
16
16
  npm install graphql-upload graphql
17
17
  ```
18
18
 
19
- Use the [`graphqlUploadKoa`](#function-graphqluploadkoa) or [`graphqlUploadExpress`](#function-graphqluploadexpress) middleware just before GraphQL middleware. Alternatively, use [`processRequest`](#function-processrequest) to create custom middleware.
19
+ Use the [`graphqlUploadKoa`](./graphqlUploadKoa.js) or [`graphqlUploadExpress`](./graphqlUploadExpress.js) middleware just before GraphQL middleware. Alternatively, use [`processRequest`](./processRequest.js) to create custom middleware.
20
20
 
21
- A schema built with separate SDL and resolvers (e.g. using [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema#makeexecutableschema) from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema)) requires the [`Upload`](#class-graphqlupload) scalar to be setup.
21
+ A schema built with separate SDL and resolvers (e.g. using [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema_src#makeexecutableschema) from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema)) requires the [`Upload`](./GraphQLUpload.js) scalar to be setup.
22
22
 
23
23
  ## Usage
24
24
 
25
- [Clients implementing the GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec#client) upload files as [`Upload`](#class-graphqlupload) scalar query or mutation variables. Their resolver values are promises that resolve [file upload details](#type-fileupload) for processing and storage. Files are typically streamed into cloud storage but may also be stored in the filesystem.
25
+ [Clients implementing the GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec#client) upload files as [`Upload`](./GraphQLUpload.js) scalar query or mutation variables. Their resolver values are promises that resolve file upload details for processing and storage. Files are typically streamed into cloud storage but may also be stored in the filesystem.
26
26
 
27
27
  See the [example API and client](https://github.com/jaydenseric/apollo-upload-examples).
28
28
 
29
29
  ### Tips
30
30
 
31
- - The process must have both read and write access to the directory identified by [`os.tmpdir()`](https://nodejs.org/api/os.html#os_os_tmpdir).
31
+ - The process must have both read and write access to the directory identified by [`os.tmpdir()`](https://nodejs.org/api/os.html#ostmpdir).
32
32
  - The device requires sufficient disk space to buffer the expected number of concurrent upload requests.
33
33
  - 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.
34
34
  - Handle file upload promise rejection and stream errors; uploads sometimes fail due to network connectivity issues or impatient users disconnecting.
35
- - 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.
36
- - 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.
37
- - 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.
35
+ - 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.
36
+ - Only use the function `createReadStream` _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.
37
+ - Use [`stream.destroy()`](https://nodejs.org/api/stream.html#readabledestroyerror) when an incomplete stream is no longer needed, or temporary files may not get cleaned up.
38
38
 
39
39
  ## Architecture
40
40
 
41
41
  The [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec) allows a file to be used for multiple query or mutation variables (file deduplication), and for variables to be used in multiple places. GraphQL resolvers need to be able to manage independent file streams. As resolvers are executed asynchronously, it’s possible they will try to process files in a different order than received in the multipart request.
42
42
 
43
- [`busboy`](https://npm.im/busboy) parses multipart request streams. Once the `operations` and `map` fields have been parsed, [`Upload`](#class-graphqlupload) scalar values in the GraphQL operations are populated with promises, and the operations are passed down the middleware chain to GraphQL resolvers.
43
+ [`busboy`](https://npm.im/busboy) parses multipart request streams. Once the `operations` and `map` fields have been parsed, [`Upload`](./GraphQLUpload.js) scalar values in the GraphQL operations are populated with promises, and the operations are passed down the middleware chain to GraphQL resolvers.
44
44
 
45
- [`fs-capacitor`](https://npm.im/fs-capacitor) is used to buffer file uploads to the filesystem and coordinate simultaneous reading and writing. As soon as a file upload’s contents begins streaming, its data begins buffering to the filesystem and its associated promise resolves. GraphQL resolvers can then create new streams from the buffer by calling [`createReadStream()`](#type-fileupload). The buffer is destroyed once all streams have ended or closed and the server has responded to the request. Any remaining buffer files will be cleaned when the process exits.
45
+ [`fs-capacitor`](https://npm.im/fs-capacitor) is used to buffer file uploads to the filesystem and coordinate simultaneous reading and writing. As soon as a file upload’s contents begins streaming, its data begins buffering to the filesystem and its associated promise resolves. GraphQL resolvers can then create new streams from the buffer by calling the function `createReadStream`. The buffer is destroyed once all streams have ended or closed and the server has responded to the request. Any remaining buffer files will be cleaned when the process exits.
46
46
 
47
47
  ## Requirements
48
48
 
49
- - [Node.js](https://nodejs.org): `^12.22.0 || ^14.17.0 || >= 16.0.0`
49
+ - [Node.js](https://nodejs.org): `^14.17.0 || ^16.0.0 || >= 18.0.0`
50
50
 
51
- ## API
51
+ ## Exports
52
52
 
53
- - [class GraphQLUpload](#class-graphqlupload)
54
- - [class Upload](#class-upload)
55
- - [Upload instance method reject](#upload-instance-method-reject)
56
- - [Upload instance method resolve](#upload-instance-method-resolve)
57
- - [Upload instance property file](#upload-instance-property-file)
58
- - [Upload instance property promise](#upload-instance-property-promise)
59
- - [function graphqlUploadExpress](#function-graphqluploadexpress)
60
- - [function graphqlUploadKoa](#function-graphqluploadkoa)
61
- - [function processRequest](#function-processrequest)
62
- - [type FileUpload](#type-fileupload)
63
- - [type FileUploadCreateReadStream](#type-fileuploadcreatereadstream)
64
- - [type GraphQLOperation](#type-graphqloperation)
65
- - [type ProcessRequestFunction](#type-processrequestfunction)
66
- - [type ProcessRequestOptions](#type-processrequestoptions)
53
+ These CommonJS modules are published to [npm](https://npmjs.com) and exported via the [`package.json`](./package.json) `exports` field:
67
54
 
68
- ### class GraphQLUpload
69
-
70
- A GraphQL `Upload` scalar that can be used in a [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema). It’s value in resolvers is a promise that resolves [file upload details](#type-fileupload) for processing and storage.
71
-
72
- #### Examples
73
-
74
- _Ways to `import`._
75
-
76
- > ```js
77
- > import { GraphQLUpload } from "graphql-upload";
78
- > ```
79
- >
80
- > ```js
81
- > import GraphQLUpload from "graphql-upload/public/GraphQLUpload.js";
82
- > ```
83
-
84
- _Ways to `require`._
85
-
86
- > ```js
87
- > const { GraphQLUpload } = require("graphql-upload");
88
- > ```
89
- >
90
- > ```js
91
- > const GraphQLUpload = require("graphql-upload/public/GraphQLUpload.js");
92
- > ```
93
-
94
- _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)._
95
-
96
- > ```js
97
- > const { makeExecutableSchema } = require("@graphql-tools/schema");
98
- > const { GraphQLUpload } = require("graphql-upload");
99
- >
100
- > const schema = makeExecutableSchema({
101
- > typeDefs: /* GraphQL */ `
102
- > scalar Upload
103
- > `,
104
- > resolvers: {
105
- > Upload: GraphQLUpload,
106
- > },
107
- > });
108
- > ```
109
-
110
- _A manually constructed schema with an image upload mutation._
111
-
112
- > ```js
113
- > const {
114
- > GraphQLSchema,
115
- > GraphQLObjectType,
116
- > GraphQLBoolean,
117
- > } = require("graphql");
118
- > const { GraphQLUpload } = require("graphql-upload");
119
- >
120
- > const schema = new GraphQLSchema({
121
- > mutation: new GraphQLObjectType({
122
- > name: "Mutation",
123
- > fields: {
124
- > uploadImage: {
125
- > description: "Uploads an image.",
126
- > type: GraphQLBoolean,
127
- > args: {
128
- > image: {
129
- > description: "Image file.",
130
- > type: GraphQLUpload,
131
- > },
132
- > },
133
- > async resolve(parent, { image }) {
134
- > const { filename, mimetype, createReadStream } = await image;
135
- > const stream = createReadStream();
136
- > // Promisify the stream and store the file, then…
137
- > return true;
138
- > },
139
- > },
140
- > },
141
- > }),
142
- > });
143
- > ```
144
-
145
- ---
146
-
147
- ### class Upload
148
-
149
- 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`](#class-graphqlupload) scalar derives it’s value from the [`promise`](#upload-instance-property-promise) property.
150
-
151
- #### Examples
152
-
153
- _Ways to `import`._
154
-
155
- > ```js
156
- > import { Upload } from "graphql-upload";
157
- > ```
158
- >
159
- > ```js
160
- > import Upload from "graphql-upload/public/Upload.js";
161
- > ```
162
-
163
- _Ways to `require`._
164
-
165
- > ```js
166
- > const { Upload } = require("graphql-upload");
167
- > ```
168
- >
169
- > ```js
170
- > const Upload = require("graphql-upload/public/Upload.js");
171
- > ```
172
-
173
- #### Upload instance method reject
174
-
175
- Rejects the upload promise with an error. This should only be utilized by [`processRequest`](#function-processrequest).
176
-
177
- | Parameter | Type | Description |
178
- | :-------- | :----- | :-------------- |
179
- | `error` | object | Error instance. |
180
-
181
- #### Upload instance method resolve
182
-
183
- Resolves the upload promise with the file upload details. This should only be utilized by [`processRequest`](#function-processrequest).
184
-
185
- | Parameter | Type | Description |
186
- | :-------- | :----------------------------- | :------------------- |
187
- | `file` | [FileUpload](#type-fileupload) | File upload details. |
188
-
189
- #### Upload instance property file
190
-
191
- The file upload details, available when the [upload promise](#upload-instance-property-promise) resolves. This should only be utilized by [`processRequest`](#function-processrequest).
192
-
193
- **Type:** `undefined` | [FileUpload](#type-fileupload)
194
-
195
- #### Upload instance property promise
196
-
197
- Promise that resolves file upload details. This should only be utilized by [`GraphQLUpload`](#class-graphqlupload).
198
-
199
- **Type:** Promise<[FileUpload](#type-fileupload)>
200
-
201
- ---
202
-
203
- ### function graphqlUploadExpress
204
-
205
- Creates [Express](https://expressjs.com) middleware that processes [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) using [`processRequest`](#function-processrequest), ignoring non-multipart requests. It sets the request body to be [similar to a conventional GraphQL POST request](#type-graphqloperation) for following GraphQL middleware to consume.
206
-
207
- | Parameter | Type | Description |
208
- | :-- | :-- | :-- |
209
- | `options` | [ProcessRequestOptions](#type-processrequestoptions) | Middleware options. Any [`ProcessRequestOptions`](#type-processrequestoptions) can be used. |
210
- | `options.processRequest` | [ProcessRequestFunction](#type-processrequestfunction)? = [processRequest](#function-processrequest) | Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec). |
211
-
212
- **Returns:** Function — Express middleware.
213
-
214
- #### Examples
215
-
216
- _Ways to `import`._
217
-
218
- > ```js
219
- > import { graphqlUploadExpress } from "graphql-upload";
220
- > ```
221
- >
222
- > ```js
223
- > import graphqlUploadExpress from "graphql-upload/public/graphqlUploadExpress.js";
224
- > ```
225
-
226
- _Ways to `require`._
227
-
228
- > ```js
229
- > const { graphqlUploadExpress } = require("graphql-upload");
230
- > ```
231
- >
232
- > ```js
233
- > const graphqlUploadExpress = require("graphql-upload/public/graphqlUploadExpress.js");
234
- > ```
235
-
236
- _Basic [`express-graphql`](https://npm.im/express-graphql) setup._
237
-
238
- > ```js
239
- > const express = require("express");
240
- > const graphqlHTTP = require("express-graphql");
241
- > const { graphqlUploadExpress } = require("graphql-upload");
242
- > const schema = require("./schema");
243
- >
244
- > express()
245
- > .use(
246
- > "/graphql",
247
- > graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
248
- > graphqlHTTP({ schema })
249
- > )
250
- > .listen(3000);
251
- > ```
252
-
253
- ---
254
-
255
- ### function graphqlUploadKoa
256
-
257
- Creates [Koa](https://koajs.com) middleware that processes [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec) using [`processRequest`](#function-processrequest), ignoring non-multipart requests. It sets the request body to be [similar to a conventional GraphQL POST request](#type-graphqloperation) for following GraphQL middleware to consume.
258
-
259
- | Parameter | Type | Description |
260
- | :-- | :-- | :-- |
261
- | `options` | [ProcessRequestOptions](#type-processrequestoptions) | Middleware options. Any [`ProcessRequestOptions`](#type-processrequestoptions) can be used. |
262
- | `options.processRequest` | [ProcessRequestFunction](#type-processrequestfunction)? = [processRequest](#function-processrequest) | Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec). |
263
-
264
- **Returns:** Function — Koa middleware.
265
-
266
- #### Examples
267
-
268
- _Ways to `import`._
269
-
270
- > ```js
271
- > import { graphqlUploadKoa } from "graphql-upload";
272
- > ```
273
- >
274
- > ```js
275
- > import graphqlUploadKoa from "graphql-upload/public/graphqlUploadKoa.js";
276
- > ```
277
-
278
- _Ways to `require`._
279
-
280
- > ```js
281
- > const { graphqlUploadKoa } = require("graphql-upload");
282
- > ```
283
- >
284
- > ```js
285
- > const graphqlUploadKoa = require("graphql-upload/public/graphqlUploadKoa.js");
286
- > ```
287
-
288
- _Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup._
289
-
290
- > ```js
291
- > const Koa = require("koa");
292
- > const bodyParser = require("koa-bodyparser");
293
- > const { errorHandler, execute } = require("graphql-api-koa");
294
- > const { graphqlUploadKoa } = require("graphql-upload");
295
- > const schema = require("./schema");
296
- >
297
- > new Koa()
298
- > .use(errorHandler())
299
- > .use(bodyParser())
300
- > .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
301
- > .use(execute({ schema }))
302
- > .listen(3000);
303
- > ```
304
-
305
- ---
306
-
307
- ### function processRequest
308
-
309
- Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec). It parses the `operations` and `map` fields to create an [`Upload`](#class-upload) instance for each expected file upload, placing references wherever the file is expected in the [GraphQL operation](#type-graphqloperation) for the [`Upload`](#class-graphqlupload) scalar to derive it’s value. Errors are created with [`http-errors`](https://npm.im/http-errors) to assist in sending responses with appropriate HTTP status codes. Used in [`graphqlUploadExpress`](#function-graphqluploadexpress) and [`graphqlUploadKoa`](#function-graphqluploadkoa) and can be used to create custom middleware.
310
-
311
- **Type:** [ProcessRequestFunction](#type-processrequestfunction)
312
-
313
- #### Examples
314
-
315
- _Ways to `import`._
316
-
317
- > ```js
318
- > import { processRequest } from "graphql-upload";
319
- > ```
320
- >
321
- > ```js
322
- > import processRequest from "graphql-upload/public/processRequest.js";
323
- > ```
324
-
325
- _Ways to `require`._
326
-
327
- > ```js
328
- > const { processRequest } = require("graphql-upload");
329
- > ```
330
- >
331
- > ```js
332
- > const processRequest = require("graphql-upload/public/processRequest.js");
333
- > ```
334
-
335
- ---
336
-
337
- ### type FileUpload
338
-
339
- File upload details that are only available after the file’s field in the [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec) has begun streaming in.
340
-
341
- **Type:** object
342
-
343
- | Property | Type | Description |
344
- | :-- | :-- | :-- |
345
- | `filename` | string | File name. |
346
- | `mimetype` | string | File MIME type. Provided by the client and can’t be trusted. |
347
- | `encoding` | string | File stream transfer encoding. |
348
- | `createReadStream` | [FileUploadCreateReadStream](#type-fileuploadcreatereadstream) | Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents, for processing and storage. |
349
-
350
- ---
351
-
352
- ### type FileUploadCreateReadStream
353
-
354
- Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of an [uploading file’s](#type-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.
355
-
356
- **Type:** Function
357
-
358
- | Parameter | Type | Description |
359
- | :-- | :-- | :-- |
360
- | `options` | object? | [`fs-capacitor`](https://npm.im/fs-capacitor) [`ReadStreamOptions`](https://github.com/mike-marcacci/fs-capacitor#readstreamoptions). |
361
- | `options.encoding` | string? = `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`. |
362
- | `options.highWaterMark` | number? = `16384` | Maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource. |
363
-
364
- **Returns:** Readable — [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents.
365
-
366
- #### See
367
-
368
- - [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#stream_new_stream_readable_options).
369
- - [Node.js stream backpressure guide](https://nodejs.org/en/docs/guides/backpressuring-in-streams).
370
-
371
- ---
372
-
373
- ### type GraphQLOperation
374
-
375
- A GraphQL operation object in a shape that can be consumed and executed by most GraphQL servers.
376
-
377
- **Type:** object
378
-
379
- | Property | Type | Description |
380
- | :-- | :-- | :-- |
381
- | `query` | string | GraphQL document containing queries and fragments. |
382
- | `operationName` | string \| `null`? | GraphQL document operation name to execute. |
383
- | `variables` | object \| `null`? | GraphQL document operation variables and values map. |
384
-
385
- #### See
386
-
387
- - [GraphQL over HTTP spec](https://github.com/graphql/graphql-over-http).
388
- - [Apollo Server POST requests](https://www.apollographql.com/docs/apollo-server/requests/#post-requests).
389
-
390
- ---
391
-
392
- ### type ProcessRequestFunction
393
-
394
- Processes a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
395
-
396
- **Type:** Function
397
-
398
- | Parameter | Type | Description |
399
- | :-- | :-- | :-- |
400
- | `request` | IncomingMessage | [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage). |
401
- | `response` | ServerResponse | [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse). |
402
- | `options` | [ProcessRequestOptions](#type-processrequestoptions)? | Options for processing the request. |
403
-
404
- **Returns:** Promise<[GraphQLOperation](#type-graphqloperation) | Array<[GraphQLOperation](#type-graphqloperation)>> — GraphQL operation or batch of operations for a GraphQL server to consume (usually as the request body).
405
-
406
- #### See
407
-
408
- - [`processRequest`](#function-processrequest).
409
-
410
- ---
411
-
412
- ### type ProcessRequestOptions
413
-
414
- Options for processing a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec); mostly relating to security, performance and limits.
415
-
416
- **Type:** object
417
-
418
- | Property | Type | Description |
419
- | :-- | :-- | :-- |
420
- | `maxFieldSize` | number? = `1000000` | Maximum allowed non-file multipart form field size in bytes; enough for your queries. |
421
- | `maxFileSize` | number? = Infinity | Maximum allowed file size in bytes. |
422
- | `maxFiles` | number? = Infinity | Maximum allowed number of files. |
55
+ - [`GraphQLUpload.js`](./GraphQLUpload.js)
56
+ - [`graphqlUploadExpress.js`](./graphqlUploadExpress.js)
57
+ - [`graphqlUploadKoa.js`](./graphqlUploadKoa.js)
58
+ - [`processRequest.js`](./processRequest.js)
59
+ - [`Upload.js`](./Upload.js)
package/public/Upload.js DELETED
@@ -1,75 +0,0 @@
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,76 +0,0 @@
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.js");
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
- };
@@ -1,69 +0,0 @@
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.js");
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
- };