Spaces:
Build error
Build error
| ; | |
| var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | |
| var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | |
| if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | |
| else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | |
| return c > 3 && r && Object.defineProperty(target, key, r), r; | |
| }; | |
| var __metadata = (this && this.__metadata) || function (k, v) { | |
| if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | |
| }; | |
| var _a; | |
| Object.defineProperty(exports, "__esModule", { value: true }); | |
| exports.OutputServerEventStream = exports.InputServerEventStream = void 0; | |
| const civkit_1 = require("civkit"); | |
| const stream_1 = require("stream"); | |
| class InputServerEventStream extends stream_1.Transform { | |
| constructor(options) { | |
| super({ | |
| ...options, | |
| readableObjectMode: true | |
| }); | |
| this.cache = []; | |
| } | |
| decodeRoutine() { | |
| if (!this.cache.length) { | |
| return; | |
| } | |
| const vecs = this.cache.join('').split(/\r?\n\r?\n/); | |
| this.cache.length = 0; | |
| const lastVec = vecs.pop(); | |
| if (lastVec) { | |
| this.cache.push(lastVec); | |
| } | |
| for (const x of vecs) { | |
| const lines = x.split(/\r?\n/); | |
| const event = {}; | |
| for (const l of lines) { | |
| const columnPos = l.indexOf(':'); | |
| if (columnPos <= 0) { | |
| continue; | |
| } | |
| const key = l.substring(0, columnPos); | |
| const rawValue = l.substring(columnPos + 1); | |
| const value = rawValue.startsWith(' ') ? rawValue.slice(1) : rawValue; | |
| if (key === 'data') { | |
| if (event.data) { | |
| event.data += value || '\n'; | |
| } | |
| else if (event.data === '') { | |
| event.data += '\n'; | |
| event.data += value || '\n'; | |
| } | |
| else { | |
| event.data = value; | |
| } | |
| } | |
| else if (key === 'retry') { | |
| event.retry = parseInt(value, 10); | |
| } | |
| else { | |
| Reflect.set(event, key, value); | |
| } | |
| } | |
| if (event.data) { | |
| const parsed = (0, civkit_1.parseJSONText)(event.data); | |
| if (parsed && typeof parsed === 'object') { | |
| event.data = parsed; | |
| } | |
| } | |
| if (Object.keys(event).length) { | |
| this.push(event); | |
| } | |
| } | |
| } | |
| _transform(chunk, encoding, callback) { | |
| if (chunk === null) { | |
| this.push(null); | |
| } | |
| this.cache.push(chunk.toString()); | |
| this.decodeRoutine(); | |
| callback(); | |
| } | |
| _final(callback) { | |
| this.decodeRoutine(); | |
| callback(); | |
| } | |
| } | |
| exports.InputServerEventStream = InputServerEventStream; | |
| let OutputServerEventStream = class OutputServerEventStream extends stream_1.Transform { | |
| constructor(options) { | |
| super({ | |
| ...options, writableObjectMode: true, encoding: 'utf-8' | |
| }); | |
| this.n = 0; | |
| } | |
| encodeRoutine(chunk) { | |
| if (typeof chunk === 'object') { | |
| const lines = []; | |
| if (chunk.event) { | |
| lines.push(`event: ${chunk.event}`); | |
| } | |
| if (chunk.data) { | |
| if (typeof chunk.data === 'string') { | |
| for (const x of chunk.data.split(/\r?\n/)) { | |
| lines.push(`data: ${x}`); | |
| } | |
| } | |
| else { | |
| lines.push(`data: ${JSON.stringify(chunk.data)}`); | |
| } | |
| } | |
| if (chunk.id) { | |
| lines.push(`id: ${chunk.id}`); | |
| } | |
| if (chunk.retry) { | |
| lines.push(`retry: ${chunk.retry}`); | |
| } | |
| if (!lines.length) { | |
| lines.push(`data: ${JSON.stringify(chunk)}`); | |
| } | |
| this.push(lines.join('\n')); | |
| this.push('\n\n'); | |
| this.n++; | |
| return; | |
| } | |
| else if (typeof chunk === 'string') { | |
| const lines = []; | |
| for (const x of chunk.split(/\r?\n/)) { | |
| lines.push(`data: ${x}`); | |
| } | |
| this.push(lines.join('\n')); | |
| this.push('\n\n'); | |
| this.n++; | |
| } | |
| } | |
| _transform(chunk, encoding, callback) { | |
| if (chunk === null) { | |
| this.push(null); | |
| } | |
| this.encodeRoutine(chunk); | |
| callback(); | |
| } | |
| }; | |
| exports.OutputServerEventStream = OutputServerEventStream; | |
| exports.OutputServerEventStream = OutputServerEventStream = __decorate([ | |
| (0, civkit_1.TPM)({ | |
| contentType: 'text/event-stream', | |
| }), | |
| __metadata("design:paramtypes", [typeof (_a = typeof stream_1.TransformOptions !== "undefined" && stream_1.TransformOptions) === "function" ? _a : Object]) | |
| ], OutputServerEventStream); | |
| //# sourceMappingURL=transform-server-event-stream.js.map |