Code Monkey home page Code Monkey logo

Comments (4)

iwanofski avatar iwanofski commented on April 27, 2024

Just to be clear, you expect ctx.body = Buffer.from(...) to result in a response body as:

{
  "type":"Buffer",
  "data":[116,101,115,116]
}

Because that's super opinionated? Koa doesn't do anything special when body is set to a buffer. This is how node:http works.

from koa.

adebayohountondji avatar adebayohountondji commented on April 27, 2024

Hello @AlcaYezzz,

Koa doesn't do anything special, you should try this to get the behavior you're hoping for:

const fooFunction = async (ctx: Context) => {
  // ...
  ctx.body = Buffer.from('test').toJSON()
  return ctx
}

from koa.

lagden avatar lagden commented on April 27, 2024

@AlcaYezzz

I can only show you the door, you're the one who have to walk through it.

import {PassThrough, Readable} from 'node:stream'

function * _generate() {
    yield 'test'
}

async function _stream(ctx) {
    const stream = Readable.from(_generate())
    ctx.type = 'application/octet-stream'
    ctx.response.set('content-disposition', 'attachment; filename="via_stream.txt"')
    ctx.body = stream.pipe(new PassThrough())
}

async function _json(ctx) {
    ctx.response.set('content-disposition', 'attachment; filename="via_json.json"')
    const _data = []
    for (const data of _generate()) {
        _data.push(data)
    }
    ctx.body = {
        type: 'Uint8Array',
        data: [...(new globalThis.TextEncoder().encode(_data.join('')))],
    }
}

Using @koa/router:

GET /json

curl -vJO 'http://localhost:5001/json
cat via_json.json
// => {"type":"Uint8Array","data":[116,101,115,116]}

GET /

curl -vJO 'http://localhost:5001/
cat via_stream.txt
// => test

from koa.

siakc avatar siakc commented on April 27, 2024

Koa does something special in case body is set to a buffer. Changes the content type to bin.

from koa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.