Welcome to the Linux Foundation Forum!

fastify-reply-from with transform stream

edited November 2022 in LFW212 Class Forum

Hello

Could someone explain to me why this does not work?

  1. 'use strict'
  2.  
  3. const {Transform, Readable} = require('stream');
  4. const createTransformStream = () => {
  5. new Transform({
  6. transform(chunk, enc, next) {
  7. const uppercase = chunk.toString().toUpperCase();
  8. next(null, uppercase);
  9. }
  10. })
  11. }
  12. const uppercaseStream = createTransformStream();
  13.  
  14.  
  15. module.exports = async function (fastify, opts) {
  16. fastify.get('/', async function (request, reply) {
  17. const { url } = request.query;
  18. try {
  19. new URL(url)
  20. } catch (error) {
  21. throw fastify.httpErrors.badRequest()
  22. }
  23. await reply.from(url, {
  24. onResponse(req, reply, response) {
  25. reply.send(response.pipe(uppercaseStream));
  26. }
  27. });
  28. })
  29. }

Thanks

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • I forgot to return the Transform

    1. 'use strict'
    2.  
    3. const {Transform, Readable} = require('stream');
    4. const createTransformStream = () => {
    5. return new Transform({
    6. transform(chunk, enc, next) {
    7. const uppercase = chunk.toString().toUpperCase();
    8. next(null, uppercase);
    9. }
    10. })
    11. }
    12. const uppercaseStream = createTransformStream();
    13.  
    14.  
    15. module.exports = async function (fastify, opts) {
    16. fastify.get('/', async function (request, reply) {
    17. const { url } = request.query;
    18. try {
    19. new URL(url)
    20. } catch (error) {
    21. throw fastify.httpErrors.badRequest()
    22. }
    23. await reply.from(url, {
    24. onResponse(req, reply, response) {
    25. reply.send(response.pipe(uppercaseStream));
    26. }
    27. });
    28. })
    29. }
  • Posts: 160

    nice catch @arturo.miguel.next

  • Posts: 7

    I wrote the code differently, but in all ways, the GET request from the client hangs forever.
    However, sending the reply without any transformation return reply.from(url) works as expected.

    the command curl http://localhost:3000/?url=http://localhost:5050 from inside the server does not return any response either.

    any1 have an idea, why it's not working with me?
    Thanks a lot

    here are two examples where the GET request is stuck forever:

    1. 'use strict'
    2.  
    3. const { Transform, pipeline } = require('stream')
    4.  
    5. function upper() {
    6. return new Transform({
    7. transform(chunk, encoding, callback) {
    8. callback(null, chunk.toString().toUpperCase())
    9. }
    10. })
    11. }
    12.  
    13. module.exports = async function (fastify, opts) {
    14. fastify.get('/', async function (request, reply) {
    15. const { url } = request.query
    16. try {
    17. new URL(url)
    18. } catch (err) {
    19. throw fastify.httpErrors.badRequest()
    20. }
    21. console.log('Fetching from URL:', url)
    22. return reply.from(url, {
    23. onResponse (request, reply, res) {
    24. console.log('Received response from upstream URL')
    25. pipeline(res, upper(), reply.raw,
    26. (err) => {
    27. if (err) {
    28. console.error('Pipeline failed:', err)
    29. reply.code(500).send({ error: 'Internal Server Error' })
    30. } else {
    31. console.log('Pipeline succeeded')
    32. }
    33. }
    34. )
    35. }
    36. })
    37. })
    38. }

    The original code as in the course also beating the same with me:

    1. 'use strict'
    2. const { Readable } = require('stream')
    3. async function * upper (res) {
    4. for await (const chunk of res) {
    5. yield chunk.toString().toUpperCase()
    6. }
    7. }
    8. module.exports = async function (fastify, opts) {
    9. fastify.get('/', async function (request, reply) {
    10. const { url } = request.query
    11. try {
    12. new URL(url)
    13. } catch (err) {
    14. throw fastify.httpErrors.badRequest()
    15. }
    16. return reply.from(url, {
    17. onResponse (request, reply, res) {
    18. reply.send(Readable.from(upper(res)))
    19. }
    20. })
    21. })
    22. }

  • Posts: 7

    for the bellow code, I get an error message "res is not async iterable"

    1. 'use strict'
    2. const { Readable } = require('stream')
    3. async function * upper (res) {
    4. for await (const chunk of res) {
    5. yield chunk.toString().toUpperCase()
    6. }
    7. }
    8. module.exports = async function (fastify, opts) {
    9. fastify.get('/', async function (request, reply) {
    10. const { url } = request.query
    11. try {
    12. new URL(url)
    13. } catch (err) {
    14. throw fastify.httpErrors.badRequest()
    15. }
    16. return reply.from(url, {
    17. onResponse (request, reply, res) {
    18. reply.send(Readable.from(upper(res)))
    19. }
    20. })
    21. })
    22. }

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training