Skip to content

Commit

Permalink
refactor: rewrote infinite servers to use TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
BramComyn committed Sep 3, 2024
1 parent 5fb4fdb commit 465d18b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/coverage
/dist
/node_modules
/.vscode
32 changes: 0 additions & 32 deletions src/infinite-server/infinite-server-http.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Main infinite server constants
const INTERVAL_TIME = 200;
const CONTENT_LENGTH = 100;
const HTTP_PORT = 8080;
const HTTPS_PORT = 8443;
const HTTP1MSSG = 'Infinite server endpoint (HTTP/1.1)\n';
const HTTP2MSSG = 'Infinite server endpoint (HTTP/2)\n';

module.exports = {
export {
INTERVAL_TIME,
CONTENT_LENGTH,
HTTP_PORT,
Expand Down
41 changes: 41 additions & 0 deletions src/infinite-server/infiniteServerHttp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { Server } from 'node:http';
import { createServer } from 'node:http';

import {
CONTENT_LENGTH,
HTTP1MSSG,
HTTP_PORT,
INTERVAL_TIME,
} from './infiniteServerConstants';

const server: Server = createServer((req, res): void => {
res.writeHead(200, {
/* eslint-disable-next-line ts/naming-convention */
'content-type': 'text/plain',
// ! Stream will abort once DATA size is larger than the Content-Length header (if specified)
// ! Ommiting the content-length header will allow the stream to be infinite
// ? Not found anything in the specs about having to close the stream, yet.
// * TD: figure out if this is Node.js specific or HTTP/1.1 specific
/* eslint-disable-next-line ts/naming-convention */
'content-length': CONTENT_LENGTH,
});

if (req.url !== undefined && req.url === '/infinite') {
// Infinite response cycle
const interval = setInterval((): void => {
res.write(`${req.url!.slice(1)}\n`);
}, INTERVAL_TIME);

req.on('close', (): void => {
clearInterval(interval);
res.end();
});

return;
}

res.write(HTTP1MSSG);
res.end();
});

server.listen(HTTP_PORT);
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
const http2 = require('node:http2');
const fs = require('node:fs');
const { INTERVAL_TIME, HTTP2MSSG, HTTPS_PORT, CONTENT_LENGTH } = require('./infinite-server-constants');
import type { Http2SecureServer, ServerHttp2Stream } from 'node:http2';
import { createSecureServer } from 'node:http2';
import { readFileSync } from 'node:fs';

import {
CONTENT_LENGTH,
HTTP2MSSG,
HTTPS_PORT,
INTERVAL_TIME,
} from './infiniteServerConstants';

/* eslint no-sync: ["error", { allowAtRootLevel: true }] */
const server = http2.createSecureServer({
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem'),
});
const options = {
key: readFileSync('localhost-privkey.pem'),
cert: readFileSync('localhost-cert.pem'),
};

server.on('stream', (stream, headers) => {
const server: Http2SecureServer = createSecureServer(options);
server.on('stream', (stream: ServerHttp2Stream, headers): void => {
const path = headers[':path'];

if (path === '/infinite') {
stream.respond({
// eslint-disable-next-line ts/naming-convention
'content-type': 'text/plain',
// eslint-disable-next-line ts/naming-convention
':status': 200,
// ! Stream will abort once DATA size is larger than the Content-Length header (if specified)
// ! Ommiting the content-length header will allow the stream to be infinite
// * See also https://datatracker.ietf.org/doc/html/rfc9113#name-malformed-messages
// * TD: figure out if this is Node.js specific or HTTP/2 specific
// Setting this header allows for checking whether all browser clients stop reading data
// eslint-disable-next-line ts/naming-convention
'content-length': CONTENT_LENGTH,
});

// Infinite response cycle
const interval = setInterval(() => {
const interval = setInterval((): void => {
stream.write(`${path.slice(1)}\n`);
}, INTERVAL_TIME);

stream.on('close', () => {
stream.on('close', (): void => {
clearInterval(interval);
stream.end();
});
Expand All @@ -37,7 +48,7 @@ server.on('stream', (stream, headers) => {
}

stream.respond({
'content-type': 'text/html',
// eslint-disable-next-line ts/naming-convention
':status': 200,
});

Expand Down

0 comments on commit 465d18b

Please sign in to comment.