owebjs 1.5.3-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");
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
import { Writable } from "stream";
|
|
5
5
|
import { toLowerCase } from "./utils/string.js";
|
|
6
6
|
import HttpResponseSocket from './responseSocket.js';
|
|
7
|
+
import http from "node:http";
|
|
7
8
|
class HttpResponse extends Writable {
|
|
8
9
|
static {
|
|
9
10
|
__name(this, "HttpResponse");
|
|
@@ -22,7 +23,7 @@ class HttpResponse extends Writable {
|
|
|
22
23
|
this.res = uResponse;
|
|
23
24
|
this.server = uServer;
|
|
24
25
|
this.statusCode = 200;
|
|
25
|
-
this.statusMessage =
|
|
26
|
+
this.statusMessage = null;
|
|
26
27
|
this.__headers = {};
|
|
27
28
|
this.headersSent = false;
|
|
28
29
|
this.socket = new HttpResponseSocket(uResponse);
|
|
@@ -30,6 +31,9 @@ class HttpResponse extends Writable {
|
|
|
30
31
|
this.finished = this.res.finished = true;
|
|
31
32
|
});
|
|
32
33
|
}
|
|
34
|
+
get sent() {
|
|
35
|
+
return this.finished;
|
|
36
|
+
}
|
|
33
37
|
setHeader(name, value) {
|
|
34
38
|
this.__headers[toLowerCase(name)] = value;
|
|
35
39
|
}
|
|
@@ -50,7 +54,8 @@ class HttpResponse extends Writable {
|
|
|
50
54
|
}
|
|
51
55
|
_flushHeaders() {
|
|
52
56
|
if (this.headersSent) return;
|
|
53
|
-
this.
|
|
57
|
+
const message = this.statusMessage || http.STATUS_CODES[this.statusCode] || "Unknown";
|
|
58
|
+
this.res.writeStatus(`${this.statusCode} ${message}`);
|
|
54
59
|
const keys = Object.keys(this.__headers);
|
|
55
60
|
for (let i = 0; i < keys.length; i++) {
|
|
56
61
|
const key = keys[i];
|
|
@@ -5,6 +5,7 @@ import { EventEmitter } from "node:events";
|
|
|
5
5
|
const REQUEST_EVENT = "request";
|
|
6
6
|
import HttpRequest from './request.js';
|
|
7
7
|
import HttpResponse from './response.js';
|
|
8
|
+
import http from "node:http";
|
|
8
9
|
async function server_default({ cert_file_name, key_file_name }) {
|
|
9
10
|
let uWS;
|
|
10
11
|
uWS = (await import("uWebSockets.js")).default;
|
|
@@ -184,6 +185,9 @@ async function server_default({ cert_file_name, key_file_name }) {
|
|
|
184
185
|
statusCode: 200,
|
|
185
186
|
_headers: {},
|
|
186
187
|
finished: false,
|
|
188
|
+
get sent() {
|
|
189
|
+
return this.finished;
|
|
190
|
+
},
|
|
187
191
|
header(key, value) {
|
|
188
192
|
this._headers[key.toLowerCase()] = value;
|
|
189
193
|
return this;
|
|
@@ -195,7 +199,8 @@ async function server_default({ cert_file_name, key_file_name }) {
|
|
|
195
199
|
send(payload) {
|
|
196
200
|
if (aborted || this.finished) return;
|
|
197
201
|
this.finished = true;
|
|
198
|
-
|
|
202
|
+
const message = http.STATUS_CODES[this.statusCode] || "Response";
|
|
203
|
+
res.writeStatus(`${this.statusCode} ${message}`);
|
|
199
204
|
for (const [k, v] of Object.entries(this._headers)) {
|
|
200
205
|
res.writeHeader(k, String(v));
|
|
201
206
|
}
|