all files / blackbird/modules/ Message.js

89.41% Statements 76/85
69.57% Branches 32/46
90.48% Functions 19/21
89.41% Lines 76/85
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                             367× 367×       534×       382×   382×                   382× 53× 74× 74×                     94×       95×   95× 95×         93×         16× 16×   16×                       11×       16×       128×       16×       21× 21×   16× 16×             97× 97× 65×   32× 32×             153×       439× 303×     439× 53× 53×   386×     439×             150× 150×     150×       93× 90×         15×       15× 12×     15× 15×   15×                
const bodec = require("bodec");
const Stream = require("bufferedstream");
const bufferStream = require("./utils/bufferStream");
const normalizeHeaderName = require("./utils/normalizeHeaderName");
const parseCookie = require("./utils/parseCookie");
const {parse: parseQuery} = require("qs");
const R = require("ramda");
 
/**
 * The default content to use for new messages.
 */
const DEFAULT_CONTENT = bodec.fromString("");
 
/**
 * The default maximum length (in bytes) to use in Message#parseContent.
 */
const DEFAULT_MAX_CONTENT_LENGTH = Math.pow(2, 20); // 1M
 
const HEADERS_LINE_SEPARATOR = /\r?\n/;
const HEADER_SEPARATOR = ": ";
 
function defaultParser(message, maxLength) {
    return message.stringifyContent(maxLength);
}
 
class Message {
    constructor(content, headers) {
        this.headers = headers;
        this.content = content;
    }
 
    get headers() {
        return this._headers;
    }
 
    set headers(value) {
        this._headers = {};
 
        Iif (typeof value === "string") {
            value.split(HEADERS_LINE_SEPARATOR).forEach(function (line) {
                const index = line.indexOf(HEADER_SEPARATOR);
 
                if (index === -1) {
                    this.addHeader(line, true);
                } else {
                    this.addHeader(line.substring(0, index), line.substring(index + HEADER_SEPARATOR.length));
                }
            }, this);
        } else if (!R.isNil(value)) {
            for (const headerName in value) {
                Eif (value.hasOwnProperty(headerName)) {
                    this.addHeader(headerName, value[headerName]);
                }
            }
        }
    }
 
    getHeader(headerName) {
        return this.headers[normalizeHeaderName(headerName)];
    }
 
    setHeader(headerName, value) {
        this.headers[normalizeHeaderName(headerName)] = value;
    }
 
    addHeader(headerName, value) {
        headerName = normalizeHeaderName(headerName);
 
        const headers = this.headers;
        if (headerName in headers) {
            Iif (Array.isArray(headers[headerName])) {
                headers[headerName].push(value);
            } else {
                headers[headerName] = [headers[headerName], value];
            }
        } else {
            headers[headerName] = value;
        }
    }
 
    get cookies() {
        Eif (!this._cookies) {
            const header = this.headers.Cookie;
 
            if (header) {
                const cookies = parseCookie(header);
 
                // From RFC 2109:
                // If multiple cookies satisfy the criteria above, they are ordered in
                // the Cookie header such that those with more specific Path attributes
                // precede those with less specific. Ordering with respect to other
                // attributes (e.g., Domain) is unspecified.
                for (const cookieName in cookies) {
                    Iif (Array.isArray(cookies[cookieName])) {
                        cookies[cookieName] = cookies[cookieName][0] || "";
                    }
                }
 
                this._cookies = cookies;
            } else {
                this._cookies = {};
            }
        }
 
        return this._cookies;
    }
 
    get contentType() {
        return this.headers["Content-Type"];
    }
 
    set contentType(value) {
        this.headers["Content-Type"] = value;
    }
 
    get mediaType() {
        const contentType = this.contentType;
        if (!contentType) {
            return null;
        }
        const match = contentType.match(/^([^;,]+)/);
        return match ? match[1].toLowerCase() : null;
    }
 
    set mediaType(value) {
        this.contentType = value + (this.charset ? `;charset=${this.charset}` : "");
    }
 
    get charset() {
        const contentType = this.contentType;
        if (!contentType) {
            return null;
        }
        const match = contentType.match(/\bcharset=([\w-]+)/);
        return match ? match[1] : null;
    }
 
    set charset(value) {
        this.contentType = this.mediaType + (value ? `;charset=${value}` : "");
    }
 
    get content() {
        return this._content;
    }
 
    set content(value) {
        if (R.isNil(value)) {
            value = DEFAULT_CONTENT;
        }
 
        if (R.is(Stream, value)) {
            this._content = value;
            value.pause();
        } else {
            this._content = new Stream(value);
        }
 
        Reflect.deleteProperty(this, "_bufferedContent");
    }
 
    get isBuffered() {
        return !R.isNil(this._bufferedContent);
    }
 
    bufferContent(maxLength) {
        Eif (R.isNil(this._bufferedContent)) {
            this._bufferedContent = bufferStream(this.content, maxLength);
        }
 
        return this._bufferedContent;
    }
 
    stringifyContent(maxLength, encoding) {
        return this.bufferContent(maxLength).then((chunk) =>
            bodec.toString(chunk, encoding || this.charset)
        );
    }
 
    parseContent(maxLength) {
        Iif (this._parsedContent) {
            return this._parsedContent;
        }
 
        if (!R.is(Number, maxLength)) {
            maxLength = DEFAULT_MAX_CONTENT_LENGTH;
        }
 
        const parser = Message.PARSERS[this.mediaType] || defaultParser;
        this._parsedContent = parser(this, maxLength);
 
        return this._parsedContent;
    }
}
 
Message.PARSERS = {
    "application/json": (message, maxLength) =>
        message.stringifyContent(maxLength).then(JSON.parse),
    "application/x-www-form-urlencoded": (message, maxLength) =>
        message.stringifyContent(maxLength).then(parseQuery)
};
 
module.exports = Message;