uploads.mjs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { ReadableStreamFrom } from "./shims.mjs";
  2. export const checkFileSupport = () => {
  3. if (typeof File === 'undefined') {
  4. const { process } = globalThis;
  5. const isOldNode = typeof process?.versions?.node === 'string' && parseInt(process.versions.node.split('.')) < 20;
  6. throw new Error('`File` is not defined as a global, which is required for file uploads.' +
  7. (isOldNode ?
  8. " Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`."
  9. : ''));
  10. }
  11. };
  12. /**
  13. * Construct a `File` instance. This is used to ensure a helpful error is thrown
  14. * for environments that don't define a global `File` yet.
  15. */
  16. export function makeFile(fileBits, fileName, options) {
  17. checkFileSupport();
  18. return new File(fileBits, fileName ?? 'unknown_file', options);
  19. }
  20. export function getName(value, stripPath) {
  21. const val = (typeof value === 'object' &&
  22. value !== null &&
  23. (('name' in value && value.name && String(value.name)) ||
  24. ('url' in value && value.url && String(value.url)) ||
  25. ('filename' in value && value.filename && String(value.filename)) ||
  26. ('path' in value && value.path && String(value.path)))) ||
  27. '';
  28. return stripPath ? val.split(/[\\/]/).pop() || undefined : val;
  29. }
  30. export const isAsyncIterable = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
  31. /**
  32. * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
  33. * Otherwise returns the request as is.
  34. */
  35. export const maybeMultipartFormRequestOptions = async (opts, fetch) => {
  36. if (!hasUploadableValue(opts.body))
  37. return opts;
  38. return { ...opts, body: await createForm(opts.body, fetch) };
  39. };
  40. export const multipartFormRequestOptions = async (opts, fetch, stripFilenames = true) => {
  41. return { ...opts, body: await createForm(opts.body, fetch, stripFilenames) };
  42. };
  43. const supportsFormDataMap = /* @__PURE__ */ new WeakMap();
  44. /**
  45. * node-fetch doesn't support the global FormData object in recent node versions. Instead of sending
  46. * properly-encoded form data, it just stringifies the object, resulting in a request body of "[object FormData]".
  47. * This function detects if the fetch function provided supports the global FormData object to avoid
  48. * confusing error messages later on.
  49. */
  50. function supportsFormData(fetchObject) {
  51. const fetch = typeof fetchObject === 'function' ? fetchObject : fetchObject.fetch;
  52. const cached = supportsFormDataMap.get(fetch);
  53. if (cached)
  54. return cached;
  55. const promise = (async () => {
  56. try {
  57. const FetchResponse = ('Response' in fetch ?
  58. fetch.Response
  59. : (await fetch('data:,')).constructor);
  60. const data = new FormData();
  61. if (data.toString() === (await new FetchResponse(data).text())) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. catch {
  67. // avoid false negatives
  68. return true;
  69. }
  70. })();
  71. supportsFormDataMap.set(fetch, promise);
  72. return promise;
  73. }
  74. export const createForm = async (body, fetch, stripFilenames = true) => {
  75. if (!(await supportsFormData(fetch))) {
  76. throw new TypeError('The provided fetch function does not support file uploads with the current global FormData class.');
  77. }
  78. const form = new FormData();
  79. await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value, stripFilenames)));
  80. return form;
  81. };
  82. // We check for Blob not File because Bun.File doesn't inherit from File,
  83. // but they both inherit from Blob and have a `name` property at runtime.
  84. const isNamedBlob = (value) => value instanceof Blob && 'name' in value;
  85. const isUploadable = (value) => typeof value === 'object' &&
  86. value !== null &&
  87. (value instanceof Response || isAsyncIterable(value) || isNamedBlob(value));
  88. const hasUploadableValue = (value) => {
  89. if (isUploadable(value))
  90. return true;
  91. if (Array.isArray(value))
  92. return value.some(hasUploadableValue);
  93. if (value && typeof value === 'object') {
  94. for (const k in value) {
  95. if (hasUploadableValue(value[k]))
  96. return true;
  97. }
  98. }
  99. return false;
  100. };
  101. const addFormValue = async (form, key, value, stripFilenames) => {
  102. if (value === undefined)
  103. return;
  104. if (value == null) {
  105. throw new TypeError(`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`);
  106. }
  107. // TODO: make nested formats configurable
  108. if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
  109. form.append(key, String(value));
  110. }
  111. else if (value instanceof Response) {
  112. let options = {};
  113. const contentType = value.headers.get('Content-Type');
  114. if (contentType) {
  115. options = { type: contentType };
  116. }
  117. form.append(key, makeFile([await value.blob()], getName(value, stripFilenames), options));
  118. }
  119. else if (isAsyncIterable(value)) {
  120. form.append(key, makeFile([await new Response(ReadableStreamFrom(value)).blob()], getName(value, stripFilenames)));
  121. }
  122. else if (isNamedBlob(value)) {
  123. form.append(key, makeFile([value], getName(value, stripFilenames), { type: value.type }));
  124. }
  125. else if (Array.isArray(value)) {
  126. await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry, stripFilenames)));
  127. }
  128. else if (typeof value === 'object') {
  129. await Promise.all(Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop, stripFilenames)));
  130. }
  131. else {
  132. throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`);
  133. }
  134. };
  135. //# sourceMappingURL=uploads.mjs.map