Welcome to the Linux Foundation Forum!

Lab 12

jhortalejhortale Posts: 7
edited February 20 in LFW211 Class Forum

Hey guys,

I'm trying to solve lab 2 in chapter 12, but I always get an error when I use the transform in the pipeline.

Does anyone know what may be causing this error:

internal/streams/pipeline.js:27
  stream.on('close', () => {
         ^

TypeError: stream.on is not a function
    at destroyer (internal/streams/pipeline.js:27:10)
    at internal/streams/pipeline.js:79:12
    at Array.map (<anonymous>)
    at pipeline (internal/streams/pipeline.js:76:28)
    at Object.<anonymous> (/Users/joao.hortale/Dev/OpenJsCertification/LFW211/labs/ch-12/labs-2/index.js:33:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)

this is my code:

'use strict'
const { Readable, Writable, Transform, PassThrough, pipeline } = require('stream')
const assert = require('assert')
const createWritable = () => {
  const sink = []
  const writable = new Writable({
    write(chunk, enc, cb) {
      sink.push(chunk.toString())
      cb()
    }
  })
  writable.sink = sink
  return writable
}
const readable = Readable.from(['a', 'b', 'c'])
const writable = createWritable()

const transform = () => {  
    return new Transform({    
        objectMode: true,  
        transform (chunk, enc, next) {   
      chunk.toString().toUpperCase()
      next()
        }  
    })
}

pipeline(readable, transform, writable, (err) => {
  assert.ifError(err)
  assert.deepStrictEqual(writable.sink, ['A', 'B', 'C'])
  console.log('passed!')
})

Thanks in advanced for your support.

Comments

  • I updated my code but I still have the same problem:

    'use strict'
    const { Readable, Writable, Transform, PassThrough, pipeline } = require('stream')
    const assert = require('assert')
    const createWritable = () => {
      const sink = []
      const writable = new Writable({
        write(chunk, enc, cb) {
          sink.push(chunk.toString())
          cb()
        }
      })
      writable.sink = sink
      return writable
    }
    const readable = Readable.from(['a', 'b', 'c'])
    const writable = createWritable()
    
    const transform = () => {  
        return new Transform({    
            transform (chunk, enc, next) {      
                const uppercased = chunk.toString().toUpperCase()      
                next(null, uppercased)    
            }  
        })
    }
    
    pipeline(readable, transform, writable, (err) => {
      assert.ifError(err)
      assert.deepStrictEqual(writable.sink, ['A', 'B', 'C'])
      console.log('passed!')
    })
    
  • @jhortale you're passing transform to pipeline but it's a function (and so has no on method).

    fix: pipeline(readable, transform(), writable, ...)

  • @davidmarkclements said:
    @jhortale you're passing transform to pipeline but it's a function (and so has no on method).

    fix: pipeline(readable, transform(), writable, ...)

    stupid error. Thank you so much @davidmarkclements

  • DevBDevB Posts: 2

    That helped @davidmarkclements . Thanks. I was perplexed.

Sign In or Register to comment.