node-stream-tag
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i node-stream-tag

Weekly Downloads

12

Version

1.1.2

License

MIT

Unpacked Size

88.2 MB

Total Files

976

Last publish

Collaborators

  • johngeorgewright