formData.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.MultipartFormData = void 0;
  6. var _utilsBundle = require("../utilsBundle");
  7. /**
  8. * Copyright (c) Microsoft Corporation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. class MultipartFormData {
  23. constructor() {
  24. this._boundary = void 0;
  25. this._chunks = [];
  26. this._boundary = generateUniqueBoundaryString();
  27. }
  28. contentTypeHeader() {
  29. return `multipart/form-data; boundary=${this._boundary}`;
  30. }
  31. addField(name, value) {
  32. this._beginMultiPartHeader(name);
  33. this._finishMultiPartHeader();
  34. this._chunks.push(Buffer.from(value));
  35. this._finishMultiPartField();
  36. }
  37. addFileField(name, value) {
  38. this._beginMultiPartHeader(name);
  39. this._chunks.push(Buffer.from(`; filename="${value.name}"`));
  40. this._chunks.push(Buffer.from(`\r\ncontent-type: ${value.mimeType || _utilsBundle.mime.getType(value.name) || 'application/octet-stream'}`));
  41. this._finishMultiPartHeader();
  42. this._chunks.push(value.buffer);
  43. this._finishMultiPartField();
  44. }
  45. finish() {
  46. this._addBoundary(true);
  47. return Buffer.concat(this._chunks);
  48. }
  49. _beginMultiPartHeader(name) {
  50. this._addBoundary();
  51. this._chunks.push(Buffer.from(`content-disposition: form-data; name="${name}"`));
  52. }
  53. _finishMultiPartHeader() {
  54. this._chunks.push(Buffer.from(`\r\n\r\n`));
  55. }
  56. _finishMultiPartField() {
  57. this._chunks.push(Buffer.from(`\r\n`));
  58. }
  59. _addBoundary(isLastBoundary) {
  60. this._chunks.push(Buffer.from('--' + this._boundary));
  61. if (isLastBoundary) this._chunks.push(Buffer.from('--'));
  62. this._chunks.push(Buffer.from('\r\n'));
  63. }
  64. }
  65. exports.MultipartFormData = MultipartFormData;
  66. const alphaNumericEncodingMap = [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42];
  67. // See generateUniqueBoundaryString() in WebKit
  68. function generateUniqueBoundaryString() {
  69. const charCodes = [];
  70. for (let i = 0; i < 16; i++) charCodes.push(alphaNumericEncodingMap[Math.floor(Math.random() * alphaNumericEncodingMap.length)]);
  71. return '----WebKitFormBoundary' + String.fromCharCode(...charCodes);
  72. }