Giter Site home page Giter Site logo

Comments (3)

webrunner42 avatar webrunner42 commented on June 1, 2024 1

For people in the future3 who have this problem:

 let chunk;
    while (null !== (chunk = ctx.req.read())) {
      // get rid of the existing things
    }
    const toUnshift = JSON.stringify(bodyJson);
    ctx.req.unshift(toUnshift);
    ctx.req.headers["content-length"] = toUnshift.length.toString();

this will clobber the existing body and replace it with your own

from boardgame.io.

kevinddchen avatar kevinddchen commented on June 1, 2024

Your specific use-case may be handled by the setup() callback: https://boardgame.io/documentation/#/api/Game

However, I also want to add middleware to modify the server behavior, e.g. invalidate player names upon joining a match if another player has that same name. #1107 is another issue on the same topic.

from boardgame.io.

kevinddchen avatar kevinddchen commented on June 1, 2024

@webrunner42 Thank you very much for your snippet! 🎉 I was able to arrive at a good workaround. My use-case was pretty simple, since I just needed to intercept the body and then pass it without any changes. For reference, this is what I used:

async (ctx, next) => { 

    const chunks = [];
    let chunk;
    while (null !== (chunk = ctx.req.read())) {
      chunks.push(chunk);
    }

    const rawBody = Buffer.concat(chunks).toString('utf8');
    const body = JSON.parse(rawBody);

    // do stuff

    ctx.req.unshift(rawBody);
    ctx.req.headers["content-length"] = rawBody.length.toString();
    await next();
  });

It seems that a more permanent fix may be added to the koa library: koajs/bodyparser#127.

from boardgame.io.

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.