Files
TTtie dc3129e2a4 refactor: switch to ESLint flat config, add JSDoc linting (#147)
This commit switches Dysnomia over to ESLint flat config for
futureproofing and better maintainability. For the most part, the
codebase will pass the test suite, apart from a few exceptions
introduced by the stylistic plugin which I believe are reasonable to
include.

Additionally, JSDoc linting was added to maintain proper 
alignment of various features.
2024-08-02 18:56:44 +00:00

65 lines
2.0 KiB
JavaScript

"use strict";
class MultipartData {
boundary = "----------------Dysnomia";
bufs = [];
attach(fieldName, data, filename) {
if(data === undefined) {
return;
}
let str = "\r\n--" + this.boundary + "\r\nContent-Disposition: form-data; name=\"" + fieldName + "\"";
let contentType;
if(filename) {
str += "; filename=\"" + filename + "\"";
const extension = filename.match(/\.(png|apng|gif|jpg|jpeg|webp|svg|json)$/i);
if(extension) {
let ext = extension[1].toLowerCase();
switch(ext) {
case "png":
case "apng":
case "gif":
case "jpg":
case "jpeg":
case "webp":
case "svg": {
if(ext === "svg") {
ext = "svg+xml";
}
contentType = "image/";
break;
}
case "json": {
contentType = "application/";
break;
}
}
contentType += ext;
}
}
if(contentType) {
str += `\r\nContent-Type: ${contentType}`;
} else if(ArrayBuffer.isView(data)) {
str += "\r\nContent-Type: application/octet-stream";
if(!(data instanceof Uint8Array)) {
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
}
} else if(typeof data === "object") {
str += "\r\nContent-Type: application/json";
data = Buffer.from(JSON.stringify(data));
} else {
data = Buffer.from("" + data);
}
this.bufs.push(Buffer.from(str + "\r\n\r\n"));
this.bufs.push(data);
}
finish() {
this.bufs.push(Buffer.from("\r\n--" + this.boundary + "--"));
return this.bufs;
}
}
module.exports = MultipartData;