|
| 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 | + |
| 27 | +const carriage = '\r\n' |
| 28 | +const dashes = '-'.repeat(2) |
| 29 | +const carriageLength = Buffer.byteLength(carriage) |
| 30 | + |
| 31 | +const NAME = Symbol.toStringTag |
| 32 | + |
| 33 | +const isBlob = object => { |
| 34 | + return ( |
| 35 | + typeof object === 'object' && |
| 36 | + typeof object.arrayBuffer === 'function' && |
| 37 | + typeof object.type === 'string' && |
| 38 | + typeof object.stream === 'function' && |
| 39 | + typeof object.constructor === 'function' && |
| 40 | + /^(Blob|File)$/.test(object[NAME]) |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @param {string} boundary |
| 46 | + */ |
| 47 | +const getFooter = boundary => `${dashes}${boundary}${dashes}${carriage.repeat(2)}` |
| 48 | + |
| 49 | +/** |
| 50 | + * @param {string} boundary |
| 51 | + * @param {string} name |
| 52 | + * @param {*} field |
| 53 | + * |
| 54 | + * @return {string} |
| 55 | + */ |
| 56 | +function getHeader (boundary, name, field) { |
| 57 | + let header = '' |
| 58 | + |
| 59 | + header += `${dashes}${boundary}${carriage}` |
| 60 | + header += `Content-Disposition: form-data; name="${name}"` |
| 61 | + |
| 62 | + if (isBlob(field)) { |
| 63 | + header += `; filename="${field.name}"${carriage}` |
| 64 | + header += `Content-Type: ${field.type || 'application/octet-stream'}` |
| 65 | + } |
| 66 | + |
| 67 | + return `${header}${carriage.repeat(2)}` |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * @return {string} |
| 72 | + */ |
| 73 | +module.exports.getBoundary = () => { |
| 74 | + // This generates a 50 character boundary similar to those used by Firefox. |
| 75 | + // They are optimized for boyer-moore parsing. |
| 76 | + var boundary = '--------------------------' |
| 77 | + for (var i = 0; i < 24; i++) { |
| 78 | + boundary += Math.floor(Math.random() * 10).toString(16) |
| 79 | + } |
| 80 | + |
| 81 | + return boundary |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @param {FormData} form |
| 86 | + * @param {string} boundary |
| 87 | + */ |
| 88 | +module.exports.formDataIterator = function * (form, boundary) { |
| 89 | + for (const [name, value] of form) { |
| 90 | + yield getHeader(boundary, name, value) |
| 91 | + |
| 92 | + if (isBlob(value)) { |
| 93 | + yield * value.stream() |
| 94 | + } else { |
| 95 | + yield value |
| 96 | + } |
| 97 | + |
| 98 | + yield carriage |
| 99 | + } |
| 100 | + |
| 101 | + yield getFooter(boundary) |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * @param {FormData} form |
| 106 | + * @param {string} boundary |
| 107 | + */ |
| 108 | +module.exports.getFormDataLength = function (form, boundary) { |
| 109 | + let length = 0 |
| 110 | + |
| 111 | + for (const [name, value] of form) { |
| 112 | + length += Buffer.byteLength(getHeader(boundary, name, value)) |
| 113 | + |
| 114 | + if (isBlob(value)) { |
| 115 | + length += value.size |
| 116 | + } else { |
| 117 | + length += Buffer.byteLength(String(value)) |
| 118 | + } |
| 119 | + |
| 120 | + length += carriageLength |
| 121 | + } |
| 122 | + |
| 123 | + length += Buffer.byteLength(getFooter(boundary)) |
| 124 | + |
| 125 | + return length |
| 126 | +} |
| 127 | + |
| 128 | +module.exports.isBlob = isBlob |
0 commit comments