UNPKG

rcs-data

Version:

RCS消息数据结构

132 lines 4.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Entity = void 0; const nanoid_1 = require("nanoid"); const grammar_1 = require("./grammar"); const parse_1 = require("./parse"); const debug = require('debug')('mime:entity'); const RE_LINE_BREAK = /\n/gm; const RE_LINE_BREAK2 = /\r\r/gm; class Entity { constructor() { this._headers = {}; this._body = null; } _setBody(body) { if (typeof body === 'string') { body = body.replace(RE_LINE_BREAK, '\r\n'); this._body = body.replace(RE_LINE_BREAK2, '\r'); } else this._body = body; let contentType = this._headers['Content-Type']; if (Array.isArray(body)) { if (!contentType || contentType.type !== 'multipart') { this.contentType = 'multipart/mixed;boundary=' + (0, nanoid_1.nanoid)(); } else if (!contentType.params.boundary) { this.contentType = contentType.fulltype + ';boundary=' + (0, nanoid_1.nanoid)(); } } else { if (!contentType || contentType.type === 'multipart') { this.contentType = 'text/plain;charset=utf-8'; } } } get contentType() { return this._headers['Content-Type']; } set contentType(value) { if (value) { if (typeof value === 'string') this._headers['Content-Type'] = (0, parse_1.parseHeaderValue)(grammar_1.grammar.headerRules['Content-Type'], value); else this._headers['Content-Type'] = value; } else { delete this._headers['Content-Type']; } } get contentTransferEncoding() { let contentTransferEncoding = this._headers['Content-Transfer-Encoding']; return contentTransferEncoding ? contentTransferEncoding.value : undefined; } set contentTransferEncoding(value) { if (value) { this._headers['Content-Transfer-Encoding'] = (0, parse_1.parseHeaderValue)(grammar_1.grammar.headerRules['Content-Transfer-Encoding'], value); } else { delete this._headers['Content-Transfer-Encoding']; } } header(name, value) { name = grammar_1.grammar.headerize(name); if (!value && value !== null) { if (this._headers[name]) { return this._headers[name].value; } debug(`头[${name}]不存在`); } else if (value) { this._headers[name] = { value: value, }; } else { delete this._headers[name]; } } get body() { return this._body; } set body(body) { body ? this._setBody(body) : delete this._body; } bodyLF() { if (typeof this._body === 'string') return this._body.replace(/\r\n/gm, '\n'); return this._body; } get isMultiPart() { let contentType = this._headers['Content-Type']; return contentType && contentType.type === 'multipart'; } toString(options = { noHeaders: false }) { let rawBody = ''; if (Array.isArray(this._body) && this._body.length > 0) { let contentType = this._headers['Content-Type']; let boundary = contentType.params.boundary; rawBody = this._body.reduce((str, part, index) => { if (index > 0) str += '\r\n'; str += '--' + boundary + '\r\n' + part.toString(); return str; }, rawBody); rawBody += '\r\n--' + boundary + '--'; } else if (typeof this._body === 'string') { rawBody += this._body; } else if (typeof this._body === 'function') { rawBody += this._body(); } else if (typeof this._body === 'object') { rawBody += JSON.stringify(this._body); } let rawHeader = ''; if (!options.noHeaders) { let name, header; for (name in this._headers) { if (this._headers.hasOwnProperty(name)) { header = this._headers[name]; rawHeader += name + ': ' + header.value + '\r\n'; } } rawHeader += '\r\n'; } return rawHeader + rawBody; } } exports.Entity = Entity; //# sourceMappingURL=entity.js.map