Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const fs = require('mz/fs');
const path = require('path');
const http = require('http');
const url = require('url');
const { Readable } = require('stream');
const colors = require('colors/safe');
import fs from 'node:fs/promises';
import path from 'node:path';
import http from 'node:http';
import { Readable } from 'node:stream';
import { styleText } from 'node:util';

// Setup frames in memory
// Load frames into memory once
Expand Down Expand Up @@ -62,7 +61,7 @@ function streamer(stream, opts) {

// color frame
const colorIdx = lastColor = selectColor(lastColor);
const coloredFrame = colors[colorsOptions[colorIdx]](frames[index]);
const coloredFrame = styleText(colorsOptions[colorIdx], frames[index]);

// try to push; respect backpressure
const ok = stream.push(coloredFrame);
Expand All @@ -86,7 +85,7 @@ function streamer(stream, opts) {
};
}

const validateQuery = ({ flip }) => ({ flip: String(flip).toLowerCase() === 'true' });
const validateQuery = (query) => ({ flip: query.get('flip')?.toLowerCase() === 'true' });

const server = http.createServer((req, res) => {
// Healthcheck route
Expand All @@ -109,7 +108,8 @@ const server = http.createServer((req, res) => {
stream.pipe(res);

// Start streaming with cleanup handler
const opts = validateQuery(url.parse(req.url, true).query);
// The 2nd argument of new URL() accepts an arbitrary dummy domain because we use only the search params
const opts = validateQuery(new URL(req.url, 'http://localhost/').searchParams);
const cleanupLoop = streamer(stream, opts);

// Clean up when the client disconnects
Expand Down
Loading