owebjs 1.5.4-dev → 1.5.5-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -251,28 +251,39 @@ function inner(oweb, route) {
|
|
|
251
251
|
}
|
|
252
252
|
return;
|
|
253
253
|
}
|
|
254
|
+
if (res.sent) return;
|
|
254
255
|
const isIterable = result && typeof result[Symbol.iterator] === "function";
|
|
255
256
|
const isAsyncIterable = result && typeof result[Symbol.asyncIterator] === "function";
|
|
256
|
-
if (
|
|
257
|
+
if (isIterable || isAsyncIterable) {
|
|
258
|
+
const iterator = isAsyncIterable ? result[Symbol.asyncIterator]() : result[Symbol.iterator]();
|
|
259
|
+
let firstChunk;
|
|
260
|
+
try {
|
|
261
|
+
firstChunk = await iterator.next();
|
|
262
|
+
} catch (err) {
|
|
263
|
+
if (routeFunc?.handleError) routeFunc.handleError(req, res, err);
|
|
264
|
+
else oweb._options.OWEB_INTERNAL_ERROR_HANDLER(req, res, err);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (res.sent) return;
|
|
268
|
+
if (firstChunk.done) {
|
|
269
|
+
if (firstChunk.value !== void 0) res.send(firstChunk.value);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
257
272
|
const rawObj = res.raw;
|
|
258
273
|
const uwsRes = rawObj.res && typeof rawObj.res.cork === "function" ? rawObj.res : null;
|
|
259
274
|
const corkedOp = /* @__PURE__ */ __name((op) => {
|
|
260
|
-
if (uwsRes)
|
|
261
|
-
|
|
262
|
-
} else {
|
|
263
|
-
op();
|
|
264
|
-
}
|
|
275
|
+
if (uwsRes) uwsRes.cork(op);
|
|
276
|
+
else op();
|
|
265
277
|
}, "corkedOp");
|
|
266
278
|
corkedOp(() => {
|
|
267
|
-
|
|
279
|
+
const headers = {
|
|
268
280
|
...res.getHeaders(),
|
|
269
281
|
"Content-Type": "text/event-stream",
|
|
270
282
|
"Cache-Control": "no-cache",
|
|
271
283
|
Connection: "keep-alive"
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
284
|
+
};
|
|
285
|
+
res.raw.writeHead(200, headers);
|
|
286
|
+
if (res.raw.flushHeaders) res.raw.flushHeaders();
|
|
276
287
|
});
|
|
277
288
|
let aborted = false;
|
|
278
289
|
const onAborted = /* @__PURE__ */ __name(() => {
|
|
@@ -285,20 +296,16 @@ function inner(oweb, route) {
|
|
|
285
296
|
rawObj["onAborted"](onAborted);
|
|
286
297
|
}
|
|
287
298
|
try {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
corkedOp(() => {
|
|
299
|
-
res.raw.write(formatSSE(chunk));
|
|
300
|
-
});
|
|
301
|
-
}
|
|
299
|
+
corkedOp(() => {
|
|
300
|
+
res.raw.write(formatSSE(firstChunk.value));
|
|
301
|
+
});
|
|
302
|
+
while (true) {
|
|
303
|
+
if (aborted || res.raw.destroyed) break;
|
|
304
|
+
const chunk = await iterator.next();
|
|
305
|
+
if (chunk.done) break;
|
|
306
|
+
corkedOp(() => {
|
|
307
|
+
res.raw.write(formatSSE(chunk.value));
|
|
308
|
+
});
|
|
302
309
|
}
|
|
303
310
|
} catch (err) {
|
|
304
311
|
error(`Error while streaming response for ${route.method.toUpperCase()}:${route.url} - ${err.message}`, "SSE");
|
|
@@ -185,6 +185,9 @@ async function server_default({ cert_file_name, key_file_name }) {
|
|
|
185
185
|
statusCode: 200,
|
|
186
186
|
_headers: {},
|
|
187
187
|
finished: false,
|
|
188
|
+
get sent() {
|
|
189
|
+
return this.finished;
|
|
190
|
+
},
|
|
188
191
|
header(key, value) {
|
|
189
192
|
this._headers[key.toLowerCase()] = value;
|
|
190
193
|
return this;
|