node-stream-tag
v1.1.2
Published
A tagged template literal for streams.
Downloads
104
Readme
node-stream-tag
Template tag for streaming content. Template parts must be "stringable" or a promise of something "stringable". The strings will be pushed on to the stream as soon as they are available.
Example
import { Server } from 'http'
import { Readable } from 'stream'
import { setTimeout } from 'timers/promises'
import stream from 'node-stream-tag'
const server = new Server((_, res) => {
const secs = 2
stream`<!DOCTYPE html>
<html>
<head>
<title>Streaming tagged templates</title>
</head>
<body>
<p>
I will now wait for ${secs} secs then say hello... ${setTimeout(
secs * 1_000,
'<b>Hello</b>'
)}
</p>
</body>
</html>`.pipe(res)
}).listen(3_000, () => console.info('server listening on', server.address()))
The above example will immediately push a string on to the response, and then will wait for 2secs before finishing.
When using promises, you must ensure they handle errors correctly, otherwise your stream may break.