UNPKG

graphql-sse

Version:

Zero-dependency, HTTP/1 safe, simple, GraphQL over Server-Sent Events Protocol server and client

84 lines (83 loc) 2.46 kB
"use strict"; /** * * common * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.isAsyncGenerator = exports.isAsyncIterable = exports.parseStreamData = exports.print = exports.validateStreamEvent = exports.TOKEN_QUERY_KEY = exports.TOKEN_HEADER_KEY = void 0; const utils_1 = require("./utils"); /** * Header key through which the event stream token is transmitted * when using the client in "single connection mode". * * Read more: https://github.com/enisdenjo/graphql-sse/blob/master/PROTOCOL.md#single-connection-mode * * @category Common */ exports.TOKEN_HEADER_KEY = 'x-graphql-event-stream-token'; /** * URL query parameter key through which the event stream token is transmitted * when using the client in "single connection mode". * * Read more: https://github.com/enisdenjo/graphql-sse/blob/master/PROTOCOL.md#single-connection-mode * * @category Common */ exports.TOKEN_QUERY_KEY = 'token'; /** @category Common */ function validateStreamEvent(e) { e = e; if (e !== 'next' && e !== 'complete') throw new Error(`Invalid stream event "${e}"`); return e; } exports.validateStreamEvent = validateStreamEvent; /** @category Common */ function print(msg) { let str = `event: ${msg.event}\ndata:`; if (msg.data) { str += ' '; str += JSON.stringify(msg.data); } str += '\n\n'; return str; } exports.print = print; /** @category Common */ function parseStreamData(e, data) { if (data) { try { data = JSON.parse(data); } catch { throw new Error('Invalid stream data'); } } if (e === 'next' && !data) throw new Error('Stream data must be an object for "next" events'); return (data || null); } exports.parseStreamData = parseStreamData; /** * Checks whether the provided value is an async iterable. * * @category Common */ function isAsyncIterable(val) { return typeof Object(val)[Symbol.asyncIterator] === 'function'; } exports.isAsyncIterable = isAsyncIterable; /** * Checks whether the provided value is an async generator. * * @category Common */ function isAsyncGenerator(val) { return ((0, utils_1.isObject)(val) && typeof Object(val)[Symbol.asyncIterator] === 'function' && typeof val.return === 'function' && typeof val.throw === 'function' && typeof val.next === 'function'); } exports.isAsyncGenerator = isAsyncGenerator;