graphql-upload 13.0.0 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
- };
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";