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/{private/GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js → GRAPHQL_MULTIPART_REQUEST_SPEC_URL.js} +5 -5
- package/{public/GraphQLUpload.js → GraphQLUpload.js} +22 -32
- package/Upload.js +55 -0
- package/graphqlUploadExpress.js +80 -0
- package/graphqlUploadKoa.js +69 -0
- package/{private/ignoreStream.js → ignoreStream.js} +7 -6
- package/license.md +9 -0
- package/package.json +46 -35
- package/processRequest.js +424 -0
- package/readme.md +18 -381
- package/public/Upload.js +0 -75
- package/public/graphqlUploadExpress.js +0 -76
- package/public/graphqlUploadKoa.js +0 -69
- package/public/index.js +0 -75
- package/public/index.mjs +0 -5
- package/public/processRequest.js +0 -348
package/public/index.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.GraphQLUpload = require("./GraphQLUpload");
|
|
4
|
-
exports.processRequest = require("./processRequest");
|
|
5
|
-
exports.graphqlUploadKoa = require("./graphqlUploadKoa");
|
|
6
|
-
exports.graphqlUploadExpress = require("./graphqlUploadExpress");
|
|
7
|
-
exports.Upload = require("./Upload");
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* File upload details that are only available after the file’s field in the
|
|
11
|
-
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec)
|
|
12
|
-
* has begun streaming in.
|
|
13
|
-
* @kind typedef
|
|
14
|
-
* @name FileUpload
|
|
15
|
-
* @type {object}
|
|
16
|
-
* @prop {string} filename File name.
|
|
17
|
-
* @prop {string} mimetype File MIME type. Provided by the client and can’t be trusted.
|
|
18
|
-
* @prop {string} encoding File stream transfer encoding.
|
|
19
|
-
* @prop {FileUploadCreateReadStream} createReadStream Creates a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents, for processing and storage.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Creates a
|
|
24
|
-
* [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams)
|
|
25
|
-
* of an [uploading file’s]{@link FileUpload} contents, for processing and
|
|
26
|
-
* storage. Multiple calls create independent streams. Throws if called after
|
|
27
|
-
* all resolvers have resolved, or after an error has interrupted the request.
|
|
28
|
-
* @kind typedef
|
|
29
|
-
* @name FileUploadCreateReadStream
|
|
30
|
-
* @type {Function}
|
|
31
|
-
* @param {object} [options] [`fs-capacitor`](https://npm.im/fs-capacitor) [`ReadStreamOptions`](https://github.com/mike-marcacci/fs-capacitor#readstreamoptions).
|
|
32
|
-
* @param {string} [options.encoding=null] Specify an encoding for the [`data`](https://nodejs.org/api/stream.html#stream_event_data) chunks to be strings (without splitting multi-byte characters across chunks) instead of Node.js [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) instances. Supported values depend on the [`Buffer` implementation](https://github.com/nodejs/node/blob/v13.7.0/lib/buffer.js#L587-L663) and include `utf8`, `ucs2`, `utf16le`, `latin1`, `ascii`, `base64`, or `hex`.
|
|
33
|
-
* @param {number} [options.highWaterMark=16384] Maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource.
|
|
34
|
-
* @returns {Readable} [Node.js readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) of the file’s contents.
|
|
35
|
-
* @see [Node.js `Readable` stream constructor docs](https://nodejs.org/api/stream.html#stream_new_stream_readable_options).
|
|
36
|
-
* @see [Node.js stream backpressure guide](https://nodejs.org/en/docs/guides/backpressuring-in-streams).
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* A GraphQL operation object in a shape that can be consumed and executed by
|
|
41
|
-
* most GraphQL servers.
|
|
42
|
-
* @kind typedef
|
|
43
|
-
* @name GraphQLOperation
|
|
44
|
-
* @type {object}
|
|
45
|
-
* @prop {string} query GraphQL document containing queries and fragments.
|
|
46
|
-
* @prop {string|null} [operationName] GraphQL document operation name to execute.
|
|
47
|
-
* @prop {object|null} [variables] GraphQL document operation variables and values map.
|
|
48
|
-
* @see [GraphQL over HTTP spec](https://github.com/graphql/graphql-over-http).
|
|
49
|
-
* @see [Apollo Server POST requests](https://www.apollographql.com/docs/apollo-server/requests/#post-requests).
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Processes a
|
|
54
|
-
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
55
|
-
* @kind typedef
|
|
56
|
-
* @name ProcessRequestFunction
|
|
57
|
-
* @type {Function}
|
|
58
|
-
* @param {IncomingMessage} request [Node.js HTTP server request instance](https://nodejs.org/api/http.html#http_class_http_incomingmessage).
|
|
59
|
-
* @param {ServerResponse} response [Node.js HTTP server response instance](https://nodejs.org/api/http.html#http_class_http_serverresponse).
|
|
60
|
-
* @param {ProcessRequestOptions} [options] Options for processing the request.
|
|
61
|
-
* @returns {Promise<GraphQLOperation | Array<GraphQLOperation>>} GraphQL operation or batch of operations for a GraphQL server to consume (usually as the request body).
|
|
62
|
-
* @see [`processRequest`]{@link processRequest}.
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Options for processing a
|
|
67
|
-
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec);
|
|
68
|
-
* mostly relating to security, performance and limits.
|
|
69
|
-
* @kind typedef
|
|
70
|
-
* @name ProcessRequestOptions
|
|
71
|
-
* @type {object}
|
|
72
|
-
* @prop {number} [maxFieldSize=1000000] Maximum allowed non-file multipart form field size in bytes; enough for your queries.
|
|
73
|
-
* @prop {number} [maxFileSize=Infinity] Maximum allowed file size in bytes.
|
|
74
|
-
* @prop {number} [maxFiles=Infinity] Maximum allowed number of files.
|
|
75
|
-
*/
|
package/public/index.mjs
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
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";
|
package/public/processRequest.js
DELETED
|
@@ -1,348 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const Busboy = require("busboy");
|
|
4
|
-
const { WriteStream } = require("fs-capacitor");
|
|
5
|
-
const createError = require("http-errors");
|
|
6
|
-
const objectPath = require("object-path");
|
|
7
|
-
const GRAPHQL_MULTIPART_REQUEST_SPEC_URL = require("../private/GRAPHQL_MULTIPART_REQUEST_SPEC_URL");
|
|
8
|
-
const ignoreStream = require("../private/ignoreStream");
|
|
9
|
-
const Upload = require("./Upload");
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Processes a
|
|
13
|
-
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
14
|
-
* It parses the `operations` and `map` fields to create an
|
|
15
|
-
* [`Upload`]{@link Upload} instance for each expected file upload, placing
|
|
16
|
-
* references wherever the file is expected in the
|
|
17
|
-
* [GraphQL operation]{@link GraphQLOperation} for the
|
|
18
|
-
* [`Upload`]{@link GraphQLUpload} scalar to derive it’s value. Errors are
|
|
19
|
-
* created with [`http-errors`](https://npm.im/http-errors) to assist in sending
|
|
20
|
-
* responses with appropriate HTTP status codes. Used in
|
|
21
|
-
* [`graphqlUploadExpress`]{@link graphqlUploadExpress} and
|
|
22
|
-
* [`graphqlUploadKoa`]{@link graphqlUploadKoa} and can be used to create custom
|
|
23
|
-
* middleware.
|
|
24
|
-
* @kind function
|
|
25
|
-
* @name processRequest
|
|
26
|
-
* @type {ProcessRequestFunction}
|
|
27
|
-
* @example <caption>Ways to `import`.</caption>
|
|
28
|
-
* ```js
|
|
29
|
-
* import { processRequest } from "graphql-upload";
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* ```js
|
|
33
|
-
* import processRequest from "graphql-upload/public/processRequest.js";
|
|
34
|
-
* ```
|
|
35
|
-
* @example <caption>Ways to `require`.</caption>
|
|
36
|
-
* ```js
|
|
37
|
-
* const { processRequest } = require("graphql-upload");
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* ```js
|
|
41
|
-
* const processRequest = require("graphql-upload/public/processRequest.js");
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
module.exports = function processRequest(
|
|
45
|
-
request,
|
|
46
|
-
response,
|
|
47
|
-
{
|
|
48
|
-
maxFieldSize = 1000000, // 1 MB
|
|
49
|
-
maxFileSize = Infinity,
|
|
50
|
-
maxFiles = Infinity,
|
|
51
|
-
} = {}
|
|
52
|
-
) {
|
|
53
|
-
return new Promise((resolve, reject) => {
|
|
54
|
-
let released;
|
|
55
|
-
let exitError;
|
|
56
|
-
let lastFileStream;
|
|
57
|
-
let operations;
|
|
58
|
-
let operationsPath;
|
|
59
|
-
let map;
|
|
60
|
-
|
|
61
|
-
const parser = new Busboy({
|
|
62
|
-
headers: request.headers,
|
|
63
|
-
limits: {
|
|
64
|
-
fieldSize: maxFieldSize,
|
|
65
|
-
fields: 2, // Only operations and map.
|
|
66
|
-
fileSize: maxFileSize,
|
|
67
|
-
files: maxFiles,
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Exits request processing with an error. Successive calls have no effect.
|
|
73
|
-
* @kind function
|
|
74
|
-
* @name processRequest~exit
|
|
75
|
-
* @param {object} error Error instance.
|
|
76
|
-
* @ignore
|
|
77
|
-
*/
|
|
78
|
-
const exit = (error) => {
|
|
79
|
-
// None of the tested scenarios cause multiple calls of this function, but
|
|
80
|
-
// it’t still good to guard against it happening in case it’s possible now
|
|
81
|
-
// or in the future.
|
|
82
|
-
// coverage ignore next line
|
|
83
|
-
if (exitError) return;
|
|
84
|
-
|
|
85
|
-
exitError = error;
|
|
86
|
-
|
|
87
|
-
reject(exitError);
|
|
88
|
-
|
|
89
|
-
parser.destroy();
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
lastFileStream &&
|
|
93
|
-
!lastFileStream.readableEnded &&
|
|
94
|
-
!lastFileStream.destroyed
|
|
95
|
-
)
|
|
96
|
-
lastFileStream.destroy(exitError);
|
|
97
|
-
|
|
98
|
-
if (map)
|
|
99
|
-
for (const upload of map.values())
|
|
100
|
-
if (!upload.file) upload.reject(exitError);
|
|
101
|
-
|
|
102
|
-
request.unpipe(parser);
|
|
103
|
-
|
|
104
|
-
// With a sufficiently large request body, subsequent events in the same
|
|
105
|
-
// event frame cause the stream to pause after the parser is destroyed. To
|
|
106
|
-
// ensure that the request resumes, the call to .resume() is scheduled for
|
|
107
|
-
// later in the event loop.
|
|
108
|
-
setImmediate(() => {
|
|
109
|
-
request.resume();
|
|
110
|
-
});
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
parser.on(
|
|
114
|
-
"field",
|
|
115
|
-
(fieldName, value, fieldNameTruncated, valueTruncated) => {
|
|
116
|
-
if (valueTruncated)
|
|
117
|
-
return exit(
|
|
118
|
-
createError(
|
|
119
|
-
413,
|
|
120
|
-
`The ‘${fieldName}’ multipart field value exceeds the ${maxFieldSize} byte size limit.`
|
|
121
|
-
)
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
switch (fieldName) {
|
|
125
|
-
case "operations":
|
|
126
|
-
try {
|
|
127
|
-
operations = JSON.parse(value);
|
|
128
|
-
} catch (error) {
|
|
129
|
-
return exit(
|
|
130
|
-
createError(
|
|
131
|
-
400,
|
|
132
|
-
`Invalid JSON in the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
133
|
-
)
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// `operations` should be an object or an array. Note that arrays
|
|
138
|
-
// and `null` have an `object` type.
|
|
139
|
-
if (typeof operations !== "object" || !operations)
|
|
140
|
-
return exit(
|
|
141
|
-
createError(
|
|
142
|
-
400,
|
|
143
|
-
`Invalid type for the ‘operations’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
144
|
-
)
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
operationsPath = objectPath(operations);
|
|
148
|
-
|
|
149
|
-
break;
|
|
150
|
-
case "map": {
|
|
151
|
-
if (!operations)
|
|
152
|
-
return exit(
|
|
153
|
-
createError(
|
|
154
|
-
400,
|
|
155
|
-
`Misordered multipart fields; ‘map’ should follow ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
156
|
-
)
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
let parsedMap;
|
|
160
|
-
try {
|
|
161
|
-
parsedMap = JSON.parse(value);
|
|
162
|
-
} catch (error) {
|
|
163
|
-
return exit(
|
|
164
|
-
createError(
|
|
165
|
-
400,
|
|
166
|
-
`Invalid JSON in the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
167
|
-
)
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// `map` should be an object.
|
|
172
|
-
if (
|
|
173
|
-
typeof parsedMap !== "object" ||
|
|
174
|
-
!parsedMap ||
|
|
175
|
-
Array.isArray(parsedMap)
|
|
176
|
-
)
|
|
177
|
-
return exit(
|
|
178
|
-
createError(
|
|
179
|
-
400,
|
|
180
|
-
`Invalid type for the ‘map’ multipart field (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
181
|
-
)
|
|
182
|
-
);
|
|
183
|
-
|
|
184
|
-
const mapEntries = Object.entries(parsedMap);
|
|
185
|
-
|
|
186
|
-
// Check max files is not exceeded, even though the number of files
|
|
187
|
-
// to parse might not match the map provided by the client.
|
|
188
|
-
if (mapEntries.length > maxFiles)
|
|
189
|
-
return exit(
|
|
190
|
-
createError(413, `${maxFiles} max file uploads exceeded.`)
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
map = new Map();
|
|
194
|
-
for (const [fieldName, paths] of mapEntries) {
|
|
195
|
-
if (!Array.isArray(paths))
|
|
196
|
-
return exit(
|
|
197
|
-
createError(
|
|
198
|
-
400,
|
|
199
|
-
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
200
|
-
)
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
map.set(fieldName, new Upload());
|
|
204
|
-
|
|
205
|
-
for (const [index, path] of paths.entries()) {
|
|
206
|
-
if (typeof path !== "string")
|
|
207
|
-
return exit(
|
|
208
|
-
createError(
|
|
209
|
-
400,
|
|
210
|
-
`Invalid type for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
211
|
-
)
|
|
212
|
-
);
|
|
213
|
-
|
|
214
|
-
try {
|
|
215
|
-
operationsPath.set(path, map.get(fieldName));
|
|
216
|
-
} catch (error) {
|
|
217
|
-
return exit(
|
|
218
|
-
createError(
|
|
219
|
-
400,
|
|
220
|
-
`Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
221
|
-
)
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
resolve(operations);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
);
|
|
232
|
-
|
|
233
|
-
parser.on("file", (fieldName, stream, filename, encoding, mimetype) => {
|
|
234
|
-
lastFileStream = stream;
|
|
235
|
-
|
|
236
|
-
if (!map) {
|
|
237
|
-
ignoreStream(stream);
|
|
238
|
-
return exit(
|
|
239
|
-
createError(
|
|
240
|
-
400,
|
|
241
|
-
`Misordered multipart fields; files should follow ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
242
|
-
)
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const upload = map.get(fieldName);
|
|
247
|
-
|
|
248
|
-
if (!upload) {
|
|
249
|
-
// The file is extraneous. As the rest can still be processed, just
|
|
250
|
-
// ignore it and don’t exit with an error.
|
|
251
|
-
ignoreStream(stream);
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
let fileError;
|
|
256
|
-
const capacitor = new WriteStream();
|
|
257
|
-
|
|
258
|
-
capacitor.on("error", () => {
|
|
259
|
-
stream.unpipe();
|
|
260
|
-
stream.resume();
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
stream.on("limit", () => {
|
|
264
|
-
fileError = createError(
|
|
265
|
-
413,
|
|
266
|
-
`File truncated as it exceeds the ${maxFileSize} byte size limit.`
|
|
267
|
-
);
|
|
268
|
-
stream.unpipe();
|
|
269
|
-
capacitor.destroy(fileError);
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
stream.on("error", (error) => {
|
|
273
|
-
fileError = error;
|
|
274
|
-
stream.unpipe();
|
|
275
|
-
capacitor.destroy(fileError);
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
const file = {
|
|
279
|
-
filename,
|
|
280
|
-
mimetype,
|
|
281
|
-
encoding,
|
|
282
|
-
createReadStream(options) {
|
|
283
|
-
const error = fileError || (released ? exitError : null);
|
|
284
|
-
if (error) throw error;
|
|
285
|
-
return capacitor.createReadStream(options);
|
|
286
|
-
},
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
Object.defineProperty(file, "capacitor", { value: capacitor });
|
|
290
|
-
|
|
291
|
-
stream.pipe(capacitor);
|
|
292
|
-
upload.resolve(file);
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
parser.once("filesLimit", () =>
|
|
296
|
-
exit(createError(413, `${maxFiles} max file uploads exceeded.`))
|
|
297
|
-
);
|
|
298
|
-
|
|
299
|
-
parser.once("finish", () => {
|
|
300
|
-
request.unpipe(parser);
|
|
301
|
-
request.resume();
|
|
302
|
-
|
|
303
|
-
if (!operations)
|
|
304
|
-
return exit(
|
|
305
|
-
createError(
|
|
306
|
-
400,
|
|
307
|
-
`Missing multipart field ‘operations’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
308
|
-
)
|
|
309
|
-
);
|
|
310
|
-
|
|
311
|
-
if (!map)
|
|
312
|
-
return exit(
|
|
313
|
-
createError(
|
|
314
|
-
400,
|
|
315
|
-
`Missing multipart field ‘map’ (${GRAPHQL_MULTIPART_REQUEST_SPEC_URL}).`
|
|
316
|
-
)
|
|
317
|
-
);
|
|
318
|
-
|
|
319
|
-
for (const upload of map.values())
|
|
320
|
-
if (!upload.file)
|
|
321
|
-
upload.reject(createError(400, "File missing in the request."));
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
parser.once("error", exit);
|
|
325
|
-
|
|
326
|
-
response.once("close", () => {
|
|
327
|
-
released = true;
|
|
328
|
-
|
|
329
|
-
if (map)
|
|
330
|
-
for (const upload of map.values())
|
|
331
|
-
if (upload.file)
|
|
332
|
-
// Release resources and clean up temporary files.
|
|
333
|
-
upload.file.capacitor.release();
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
request.once("close", () => {
|
|
337
|
-
if (!request.readableEnded)
|
|
338
|
-
exit(
|
|
339
|
-
createError(
|
|
340
|
-
499,
|
|
341
|
-
"Request disconnected during file upload stream parsing."
|
|
342
|
-
)
|
|
343
|
-
);
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
request.pipe(parser);
|
|
347
|
-
});
|
|
348
|
-
};
|