Upload.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // @ts-check
  2. "use strict";
  3. /** @typedef {import("./GraphQLUpload.js")} GraphQLUpload */
  4. /** @typedef {import("./processRequest.js")} processRequest */
  5. /**
  6. * A file expected to be uploaded as it was declared in the `map` field of a
  7. * [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
  8. * The {@linkcode processRequest} function places references to an instance of
  9. * this class wherever the file is expected in the GraphQL operation. The scalar
  10. * {@linkcode GraphQLUpload} derives it’s value from {@linkcode Upload.promise}.
  11. */
  12. class Upload {
  13. constructor() {
  14. /**
  15. * Promise that resolves file upload details. This should only be utilized
  16. * by {@linkcode GraphQLUpload}.
  17. * @type {Promise<import("./processRequest.js").FileUpload>}
  18. */
  19. this.promise = new Promise((resolve, reject) => {
  20. /**
  21. * Resolves the upload promise with the file upload details. This should
  22. * only be utilized by {@linkcode processRequest}.
  23. * @param {import("./processRequest.js").FileUpload} file File upload
  24. * details.
  25. */
  26. this.resolve = (file) => {
  27. /**
  28. * The file upload details, available when the
  29. * {@linkcode Upload.promise} resolves. This should only be utilized by
  30. * {@linkcode processRequest}.
  31. * @type {import("./processRequest.js").FileUpload | undefined}
  32. */
  33. this.file = file;
  34. resolve(file);
  35. };
  36. /**
  37. * Rejects the upload promise with an error. This should only be
  38. * utilized by {@linkcode processRequest}.
  39. * @param {Error} error Error instance.
  40. */
  41. this.reject = reject;
  42. });
  43. // Prevent errors crashing Node.js, see:
  44. // https://github.com/nodejs/node/issues/20392
  45. this.promise.catch(() => {});
  46. }
  47. }
  48. module.exports = Upload;