UNPKG

@fanoutio/grip

Version:
47 lines (46 loc) 1.51 kB
import { encodeBytesToBase64String } from '../../utilities/index.js'; // The HttpResponseFormat class is the format used to publish messages to // HTTP response clients connected to a GRIP proxy. export class HttpResponseFormat { code; reason; headers; body; constructor(code = null, reason = null, headers = null, body = null) { if (code !== null && typeof code !== 'string') { ({ code = null, reason = null, headers = null, body = null } = code); } this.code = code; this.reason = reason; this.headers = headers; this.body = body; } // Export this Response instance into a dictionary containing all // of the non-null data. If the body is set to a buffer then export // it as 'body-bin' (as opposed to 'body') and encode it as base64. export() { const obj = {}; if (this.code != null) { obj.code = this.code; } if (this.reason != null) { obj.reason = this.reason; } if (this.headers != null) { obj.headers = this.headers; } if (this.body != null) { if (this.body instanceof Uint8Array) { obj['body-bin'] = encodeBytesToBase64String(this.body); } else { obj['body'] = this.body.toString(); } } return obj; } // The name used when publishing this format. name() { return 'http-response'; } }