UNPKG

@fanoutio/grip

Version:
66 lines (65 loc) 2.13 kB
import { createGripChannelHeader, parseChannels } from '../utilities/index.js'; import { createKeepAliveHeader, createMetaHeader, createNextLinkHeader } from '../utilities/http.js'; export class GripInstruct { status; hold; channels = []; timeout = 0; keepAlive; keepAliveTimeout = 0; nextLink; nextLinkTimeout = 0; meta; // Intended to be modified/set directly constructor(channels) { if (channels != null) { this.addChannel(channels); } } addChannel(channels) { this.channels.push(...parseChannels(channels)); } setStatus(status) { this.status = status; } setHoldLongPoll(timeout) { this.hold = 'response'; if (timeout != null) { this.timeout = Math.floor(timeout); } } setHoldStream() { this.hold = 'stream'; } setKeepAlive(data, timeout) { this.keepAlive = data; this.keepAliveTimeout = timeout; } setNextLink(uri, timeout = 0) { this.nextLink = uri; this.nextLinkTimeout = timeout; } toHeaders(additionalHeaders) { const headers = {}; headers['Grip-Channel'] = createGripChannelHeader(this.channels); if (this.status != null) { headers['Grip-Status'] = `${this.status}`; // Convert to string } if (this.hold != null) { headers['Grip-Hold'] = this.hold; if (this.timeout > 0) { headers['Grip-Timeout'] = `${this.timeout}`; // Convert to string } if (this.keepAlive != null) { headers['Grip-Keep-Alive'] = createKeepAliveHeader(this.keepAlive, this.keepAliveTimeout); } if (this.meta != null && Object.entries(this.meta).length > 0) { headers['Grip-Set-Meta'] = createMetaHeader(this.meta); } } if (this.nextLink != null) { headers['Grip-Link'] = createNextLinkHeader(this.nextLink, this.nextLinkTimeout); } Object.assign(headers, additionalHeaders); return headers; } }