Home / File/ stream.2.test.js — fastify Source File

stream.2.test.js — fastify Source File

Architecture documentation for stream.2.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const { test } = require('node:test')
const proxyquire = require('proxyquire')
const fs = require('node:fs')
const resolve = require('node:path').resolve
const zlib = require('node:zlib')
const pipeline = require('node:stream').pipeline
const Fastify = require('..')
const { waitForCb } = require('./toolkit')

test('onSend hook stream', t => {
  t.plan(4)
  const fastify = Fastify()

  fastify.get('/', function (req, reply) {
    reply.send({ hello: 'world' })
  })

  const { stepIn, patience } = waitForCb({ steps: 2 })

  fastify.addHook('onSend', (req, reply, payload, done) => {
    const gzStream = zlib.createGzip()

    reply.header('Content-Encoding', 'gzip')
    pipeline(
      fs.createReadStream(resolve(__filename), 'utf8'),
      gzStream,
      (err) => {
        t.assert.ifError(err)
        stepIn()
      }
    )
    done(null, gzStream)
  })

  fastify.inject({
    url: '/',
    method: 'GET'
  }, (err, res) => {
    t.assert.ifError(err)
    t.assert.strictEqual(res.headers['content-encoding'], 'gzip')
    const file = fs.readFileSync(resolve(__filename), 'utf8')
    const payload = zlib.gunzipSync(res.rawPayload)
    t.assert.strictEqual(payload.toString('utf-8'), file)
    fastify.close()
    stepIn()
  })

  return patience
})

test('onSend hook stream should work even if payload is not a proper stream', (t, testDone) => {
  t.plan(1)

  const reply = proxyquire('../lib/reply', {
    'node:stream': {
      finished: (...args) => {
        if (args.length === 2) { args[1](new Error('test-error')) }
      }
// ... (70 more lines)

Frequently Asked Questions

What does stream.2.test.js do?
stream.2.test.js is a source file in the fastify codebase, written in javascript.
Where is stream.2.test.js in the architecture?
stream.2.test.js is located at test/stream.2.test.js (directory: test).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free