UNPKG

gigachat-node

Version:

The unoffical JavaScript/TypesSript library for the GigaChat API

1 lines 1.19 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FormDataBuilder=void 0;const crypto_1=require("crypto");class FormDataBuilder{constructor(){this.parts=[],this.lineEnding="\r\n",this.boundary=`----${(0,crypto_1.randomBytes)(16).toString("hex")}`}appendField(t,n){this.parts.push(Buffer.from(`--${this.boundary}${this.lineEnding}Content-Disposition: form-data; name="${t}"${this.lineEnding}${this.lineEnding}${n}${this.lineEnding}`))}appendFile(t,n){const i=`--${this.boundary}${this.lineEnding}Content-Disposition: form-data; name="${t}"; filename="${n.name}"${this.lineEnding}Content-Type: ${n.mimeType}${this.lineEnding}${this.lineEnding}`;this.parts.push(Buffer.from(i),n.readFile(),Buffer.from(this.lineEnding))}getHeaders(){return{"Content-Type":`multipart/form-data; boundary=${this.boundary}`,"Content-Length":this.getTotalLength().toString()}}getBody(){const t=Buffer.from(`${this.lineEnding}--${this.boundary}--${this.lineEnding}`);return Buffer.concat([...this.parts,t])}getTotalLength(){return this.parts.reduce(((t,n)=>t+n.length),0)+Buffer.from(`${this.lineEnding}--${this.boundary}--${this.lineEnding}`).length}}exports.FormDataBuilder=FormDataBuilder;