-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrote infinite servers to use TypeScript
- Loading branch information
Showing
5 changed files
with
66 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
/coverage | ||
/dist | ||
/node_modules | ||
/.vscode |
This file was deleted.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
...inite-server/infinite-server-constants.js → ...nfinite-server/infiniteServerConstants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters