Skip to content

Commit 6bc7f9d

Browse files
Merge pull request #15 from kcoderhtml/update-streamdata-function-with-contenttype
update-streamdata-function-with-contenttype
2 parents c00a88e + a921f26 commit 6bc7f9d

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

bun.lockb

0 Bytes
Binary file not shown.

index.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ const port = 3000;
1111

1212
// Middleware to set Content-Type and enable streaming
1313
app.use((req, res, next) => {
14-
res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
14+
if ((req.headers["user-agent"] as string).includes("Firefox")) {
15+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
16+
} else {
17+
res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
18+
}
1519
res.setHeader("Cache-Control", "no-cache");
1620
res.setHeader("Connection", "keep-alive");
1721
next();
@@ -26,7 +30,7 @@ app.get("/favicon.ico", (req, res, next) => {
2630
// get the home page message and stream it
2731
app.get("/", async (req, res) => {
2832
const message = await getMessage();
29-
streamData(req, res, message);
33+
streamData(req, res, message, res.get("Content-Type"));
3034
});
3135

3236
// get the blog posts's summaries and descriptions and stream them
@@ -79,24 +83,13 @@ app.get("/portfolio/:companyID", async (req, res) => {
7983

8084
let logger = (req: any, res: any, next: any) => {
8185
let current_datetime = new Date();
82-
let formatted_date =
83-
current_datetime.getFullYear() +
84-
"-" +
85-
(current_datetime.getMonth() + 1) +
86-
"-" +
87-
current_datetime.getDate() +
88-
" " +
89-
current_datetime.getHours() +
90-
":" +
91-
current_datetime.getMinutes() +
92-
":" +
93-
current_datetime.getSeconds();
86+
let formatted_date = current_datetime.toISOString();
9487
let method = req.method;
9588
let url = req.url;
9689
let status = res.statusCode;
9790
let user_agent = req.headers["user-agent"];
98-
let log = `[${formatted_date}] ${method}:${url} ${status} ${user_agent}`;
99-
console.log(log);
91+
let log = `\x1b[36m[${formatted_date}]\x1b[0m ${method}:${url} ${status} ${user_agent}`;
92+
console.log(log); // Highlight log in cyan color
10093
};
10194

10295
// Create server
@@ -108,4 +101,5 @@ server.on("request", logger);
108101
// Start server
109102
server.listen(port, () => {
110103
console.log(`Server is listening on port ${port}`);
104+
console.log(`Visit http://localhost:${port}`);
111105
});

utils/streaming.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let speed = 10;
77

88
const transmissionStart = `🐢---------------------
99
START OF TRANSMISSION
10-
type: text/event-stream
10+
type: {contentType}
1111
---------------------🐇
1212
1313
`;
@@ -19,10 +19,10 @@ wrote: {bytes} bytes
1919
-------------------
2020
`;
2121

22-
export function streamData(req: Request, res: Response, message: string) {
22+
export function streamData(req: Request, res: Response, message: string, contentType: string = "text/plain") {
2323
// get total bytes of message and replace {bytes} in transmissionEnd with the number of bytes of the message
2424
const body =
25-
transmissionStart +
25+
transmissionStart.replace("{contentType}", contentType) +
2626
message +
2727
transmissionEnd.replace("{bytes}", Buffer.byteLength(message).toString());
2828

0 commit comments

Comments
 (0)