|
| 1 | +/** |
| 2 | + * @license https://raw.githubusercontent.com/node-fetch/node-fetch/master/LICENSE.md |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 - 2020 Node Fetch Team |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + * |
| 26 | + * Extracted from https://github.com/node-fetch/node-fetch/blob/64c5c296a0250b852010746c76144cb9e14698d9/src/utils/form-data.js |
| 27 | + */ |
| 28 | + |
| 29 | +const carriage = '\r\n' |
| 30 | +const dashes = '-'.repeat(2) |
| 31 | + |
| 32 | +const NAME = Symbol.toStringTag |
| 33 | + |
| 34 | +const isBlob = object => { |
| 35 | + return ( |
| 36 | + typeof object === 'object' && |
| 37 | + typeof object.arrayBuffer === 'function' && |
| 38 | + typeof object.type === 'string' && |
| 39 | + typeof object.stream === 'function' && |
| 40 | + typeof object.constructor === 'function' && |
| 41 | + /^(Blob|File)$/.test(object[NAME]) |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * @param {string} boundary |
| 47 | + */ |
| 48 | +const getFooter = boundary => `${dashes}${boundary}${dashes}${carriage.repeat(2)}` |
| 49 | + |
| 50 | +/** |
| 51 | + * @param {string} boundary |
| 52 | + * @param {string} name |
| 53 | + * @param {*} field |
| 54 | + * |
| 55 | + * @return {string} |
| 56 | + */ |
| 57 | +function getHeader (boundary, name, field) { |
| 58 | + let header = '' |
| 59 | + |
| 60 | + header += `${dashes}${boundary}${carriage}` |
| 61 | + header += `Content-Disposition: form-data; name="${name}"` |
| 62 | + |
| 63 | + if (isBlob(field)) { |
| 64 | + header += `; filename="${field.name}"${carriage}` |
| 65 | + header += `Content-Type: ${field.type || 'application/octet-stream'}` |
| 66 | + } |
| 67 | + |
| 68 | + return `${header}${carriage.repeat(2)}` |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * @return {string} |
| 73 | + */ |
| 74 | +module.exports.getBoundary = () => { |
| 75 | + // This generates a 50 character boundary similar to those used by Firefox. |
| 76 | + // They are optimized for boyer-moore parsing. |
| 77 | + var boundary = '--------------------------' |
| 78 | + for (var i = 0; i < 24; i++) { |
| 79 | + boundary += Math.floor(Math.random() * 10).toString(16) |
| 80 | + } |
| 81 | + |
| 82 | + return boundary |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * @param {FormData} form |
| 87 | + * @param {string} boundary |
| 88 | + */ |
| 89 | +module.exports.formDataIterator = function * (form, boundary) { |
| 90 | + for (const [name, value] of form) { |
| 91 | + yield getHeader(boundary, name, value) |
| 92 | + |
| 93 | + if (isBlob(value)) { |
| 94 | + yield * value.stream() |
| 95 | + } else { |
| 96 | + yield value |
| 97 | + } |
| 98 | + |
| 99 | + yield carriage |
| 100 | + } |
| 101 | + |
| 102 | + yield getFooter(boundary) |
| 103 | +} |
| 104 | + |
| 105 | +module.exports.isBlob = isBlob |
0 commit comments