{"version":3,"file":"formdata.mjs","sources":["../../../../lib/web/fetch/formdata.js"],"sourcesContent":["'use strict'\n\nconst { iteratorMixin } = require('./util')\nconst { kEnumerableProperty } = require('../../core/util')\nconst { webidl } = require('../webidl')\nconst nodeUtil = require('node:util')\nconst { runtimeFeatures } = require('../../util/runtime-features.js')\n\nconst random = runtimeFeatures.has('crypto')\n  ? require('node:crypto').randomInt\n  : (max) => Math.floor(Math.random() * max)\n\n// https://xhr.spec.whatwg.org/#formdata\nclass FormData {\n  #state = []\n  #boundary = null\n\n  constructor (form = undefined) {\n    webidl.util.markAsUncloneable(this)\n\n    if (form !== undefined) {\n      throw webidl.errors.conversionFailed({\n        prefix: 'FormData constructor',\n        argument: 'Argument 1',\n        types: ['undefined']\n      })\n    }\n  }\n\n  append (name, value, filename = undefined) {\n    webidl.brandCheck(this, FormData)\n\n    const prefix = 'FormData.append'\n    webidl.argumentLengthCheck(arguments, 2, prefix)\n\n    name = webidl.converters.USVString(name)\n\n    if (arguments.length === 3 || webidl.is.Blob(value)) {\n      value = webidl.converters.Blob(value, prefix, 'value')\n\n      if (filename !== undefined) {\n        filename = webidl.converters.USVString(filename)\n      }\n    } else {\n      value = webidl.converters.USVString(value)\n    }\n\n    // 1. Let value be value if given; otherwise blobValue.\n\n    // 2. Let entry be the result of creating an entry with\n    // name, value, and filename if given.\n    const entry = makeEntry(name, value, filename)\n\n    // 3. Append entry to this’s entry list.\n    this.#state.push(entry)\n  }\n\n  delete (name) {\n    webidl.brandCheck(this, FormData)\n\n    const prefix = 'FormData.delete'\n    webidl.argumentLengthCheck(arguments, 1, prefix)\n\n    name = webidl.converters.USVString(name)\n\n    // The delete(name) method steps are to remove all entries whose name\n    // is name from this’s entry list.\n    this.#state = this.#state.filter(entry => entry.name !== name)\n  }\n\n  get (name) {\n    webidl.brandCheck(this, FormData)\n\n    const prefix = 'FormData.get'\n    webidl.argumentLengthCheck(arguments, 1, prefix)\n\n    name = webidl.converters.USVString(name)\n\n    // 1. If there is no entry whose name is name in this’s entry list,\n    // then return null.\n    const idx = this.#state.findIndex((entry) => entry.name === name)\n    if (idx === -1) {\n      return null\n    }\n\n    // 2. Return the value of the first entry whose name is name from\n    // this’s entry list.\n    return this.#state[idx].value\n  }\n\n  getAll (name) {\n    webidl.brandCheck(this, FormData)\n\n    const prefix = 'FormData.getAll'\n    webidl.argumentLengthCheck(arguments, 1, prefix)\n\n    name = webidl.converters.USVString(name)\n\n    // 1. If there is no entry whose name is name in this’s entry list,\n    // then return the empty list.\n    // 2. Return the values of all entries whose name is name, in order,\n    // from this’s entry list.\n    return this.#state\n      .filter((entry) => entry.name === name)\n      .map((entry) => entry.value)\n  }\n\n  has (name) {\n    webidl.brandCheck(this, FormData)\n\n    const prefix = 'FormData.has'\n    webidl.argumentLengthCheck(arguments, 1, prefix)\n\n    name = webidl.converters.USVString(name)\n\n    // The has(name) method steps are to return true if there is an entry\n    // whose name is name in this’s entry list; otherwise false.\n    return this.#state.findIndex((entry) => entry.name === name) !== -1\n  }\n\n  set (name, value, filename = undefined) {\n    webidl.brandCheck(this, FormData)\n\n    const prefix = 'FormData.set'\n    webidl.argumentLengthCheck(arguments, 2, prefix)\n\n    name = webidl.converters.USVString(name)\n\n    if (arguments.length === 3 || webidl.is.Blob(value)) {\n      value = webidl.converters.Blob(value, prefix, 'value')\n\n      if (filename !== undefined) {\n        filename = webidl.converters.USVString(filename)\n      }\n    } else {\n      value = webidl.converters.USVString(value)\n    }\n\n    // The set(name, value) and set(name, blobValue, filename) method steps\n    // are:\n\n    // 1. Let value be value if given; otherwise blobValue.\n\n    // 2. Let entry be the result of creating an entry with name, value, and\n    // filename if given.\n    const entry = makeEntry(name, value, filename)\n\n    // 3. If there are entries in this’s entry list whose name is name, then\n    // replace the first such entry with entry and remove the others.\n    const idx = this.#state.findIndex((entry) => entry.name === name)\n    if (idx !== -1) {\n      this.#state = [\n        ...this.#state.slice(0, idx),\n        entry,\n        ...this.#state.slice(idx + 1).filter((entry) => entry.name !== name)\n      ]\n    } else {\n      // 4. Otherwise, append entry to this’s entry list.\n      this.#state.push(entry)\n    }\n  }\n\n  [nodeUtil.inspect.custom] (depth, options) {\n    const state = this.#state.reduce((a, b) => {\n      if (a[b.name]) {\n        if (Array.isArray(a[b.name])) {\n          a[b.name].push(b.value)\n        } else {\n          a[b.name] = [a[b.name], b.value]\n        }\n      } else {\n        a[b.name] = b.value\n      }\n\n      return a\n    }, { __proto__: null })\n\n    options.depth ??= depth\n    options.colors ??= true\n\n    const output = nodeUtil.formatWithOptions(options, state)\n\n    // remove [Object null prototype]\n    return `FormData ${output.slice(output.indexOf(']') + 2)}`\n  }\n\n  /**\n   * @param {FormData} formData\n   */\n  static getFormDataState (formData) {\n    return formData.#state\n  }\n\n  /**\n   * @param {FormData} formData\n   * @param {any[]} newState\n   */\n  static setFormDataState (formData, newState) {\n    formData.#state = newState\n  }\n\n  /**\n   * @param {FormData} formData\n   * @returns {string | null}\n   */\n  static getFormDataBoundary (formData) {\n    const boundary = formData.#boundary\n    if (boundary != null) return boundary\n\n    // eslint-disable-next-line no-return-assign\n    return formData.#boundary = `----formdata-undici-0${`${random(1e11)}`.padStart(11, '0')}`\n  }\n}\n\nconst { getFormDataState, setFormDataState, getFormDataBoundary } = FormData\nReflect.deleteProperty(FormData, 'getFormDataState')\nReflect.deleteProperty(FormData, 'setFormDataState')\nReflect.deleteProperty(FormData, 'getFormDataBoundary')\n\niteratorMixin('FormData', FormData, getFormDataState, 'name', 'value')\n\nObject.defineProperties(FormData.prototype, {\n  append: kEnumerableProperty,\n  delete: kEnumerableProperty,\n  get: kEnumerableProperty,\n  getAll: kEnumerableProperty,\n  has: kEnumerableProperty,\n  set: kEnumerableProperty,\n  [Symbol.toStringTag]: {\n    value: 'FormData',\n    configurable: true\n  }\n})\n\n/**\n * @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#create-an-entry\n * @param {string} name\n * @param {string|Blob} value\n * @param {?string} filename\n * @returns\n */\nfunction makeEntry (name, value, filename) {\n  // 1. Set name to the result of converting name into a scalar value string.\n  // Note: This operation was done by the webidl converter USVString.\n\n  // 2. If value is a string, then set value to the result of converting\n  //    value into a scalar value string.\n  if (typeof value === 'string') {\n    // Note: This operation was done by the webidl converter USVString.\n  } else {\n    // 3. Otherwise:\n\n    // 1. If value is not a File object, then set value to a new File object,\n    //    representing the same bytes, whose name attribute value is \"blob\"\n    if (!webidl.is.File(value)) {\n      value = new File([value], 'blob', { type: value.type })\n    }\n\n    // 2. If filename is given, then set value to a new File object,\n    //    representing the same bytes, whose name attribute is filename.\n    if (filename !== undefined) {\n      /** @type {FilePropertyBag} */\n      const options = {\n        type: value.type,\n        lastModified: value.lastModified\n      }\n\n      value = new File([value], filename, options)\n    }\n  }\n\n  // 4. Return an entry whose name is name and whose value is value.\n  return { name, value }\n}\n\nwebidl.is.FormData = webidl.util.MakeTypeAssertion(FormData)\n\nmodule.exports = { FormData, makeEntry, setFormDataState, getFormDataBoundary }\n"],"names":["iteratorMixin","require$$0","kEnumerableProperty","require$$1","webidl","require$$2","nodeUtil","require$$3","runtimeFeatures","require$$4","random","has","require$$5.randomInt","max","Math","floor","FormData","state","boundary","constructor","form","undefined","util","markAsUncloneable","this","errors","conversionFailed","prefix","argument","types","append","name","value","filename","brandCheck","argumentLengthCheck","arguments","converters","USVString","length","is","Blob","entry","makeEntry","push","filter","get","idx","findIndex","getAll","map","set","slice","inspect","custom","depth","options","reduce","a","b","Array","isArray","__proto__","colors","output","formatWithOptions","indexOf","getFormDataState","formData","setFormDataState","newState","getFormDataBoundary","padStart","File","type","lastModified","Reflect","deleteProperty","Object","defineProperties","prototype","delete","Symbol","toStringTag","configurable","MakeTypeAssertion","formdata"],"mappings":"scAEA,MAAMA,eAAoBC,eACpBC,qBAA0BC,QAC1BC,QAAaC,gBACbC,SAAWC,MACXC,iBAAsBC,yBAEtBC,OAASF,gBAAgBG,IAAI,UAC/BC,UACCC,KAAQC,KAAKC,MAAMD,KAAKJ,SAAWG;;AAGxC,MAAMG,SACJ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