Spaces:
Build error
Build error
File size: 5,445 Bytes
f316cce | 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 | "use strict";
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 |