Giter Club home page Giter Club logo

convert's Introduction

koa-convert

Build Status NPM version License Code Size

Convert Koa legacy (0.x & 1.x) generator middleware to modern promise middleware (2.x).

It could also convert modern promise middleware back to legacy generator middleware (useful to help modern middleware support Koa v0.x or v1.x).

Note

Router middleware is special case here. Because it reimplements middleware composition internally, we cannot not simply convert it.

You may use following packages for routing, which are koa 2.x ready now:

Installation

# npm ..
$ npm i koa-convert
# yarn ..
$ yarn add koa-convert

Usage

const Koa = require('koa') // koa v2.x
const convert = require('koa-convert')
const app = new Koa()

app.use(modernMiddleware)

app.use(convert(legacyMiddleware))

app.use(convert.compose(legacyMiddleware, modernMiddleware))

function * legacyMiddleware (next) {
  // before
  yield next
  // after
}

function modernMiddleware (ctx, next) {
  // before
  return next().then(() => {
    // after
  })
}

Distinguish legacy and modern middleware

In koa 0.x and 1.x (without experimental flag), app.use has an assertion that all (legacy) middleware must be generator function and it's tested with fn.constructor.name == 'GeneratorFunction' at here.

Therefore, we can distinguish legacy and modern middleware with fn.constructor.name == 'GeneratorFunction'.

Migration

app.use(legacyMiddleware) is everywhere in 0.x and 1.x and it would be painful to manually change all of them to app.use(convert(legacyMiddleware)).

You can use following snippet to make migration easier.

const _use = app.use
app.use = x => _use.call(app, convert(x))

The above snippet will override app.use method and implicitly convert all legacy generator middleware to modern promise middleware.

Therefore, you can have both app.use(modernMiddleware) and app.use(legacyMiddleware) and your 0.x or 1.x should work without modification.

Complete example:

const Koa = require('koa') // v2.x
const convert = require('koa-convert')
const app = new Koa()

// ---------- override app.use method ----------

const _use = app.use
app.use = x => _use.call(app, convert(x))

// ---------- end ----------

app.use(modernMiddleware)

// this will be converted to modern promise middleware implicitly
app.use(legacyMiddleware)

function * legacyMiddleware (next) {
  // before
  yield next
  // after
}

function modernMiddleware (ctx, next) {
  // before
  return next().then(() => {
    // after
  })
}

API

convert()

Convert legacy generator middleware to modern promise middleware.

modernMiddleware = convert(legacyMiddleware)

convert.compose()

Convert and compose multiple middleware (could mix legacy and modern ones) and return modern promise middleware.

composedModernMiddleware = convert.compose(legacyMiddleware, modernMiddleware)
// or
composedModernMiddleware = convert.compose([legacyMiddleware, modernMiddleware])

convert.back()

Convert modern promise middleware back to legacy generator middleware.

This is useful to help modern promise middleware support koa 0.x or 1.x.

legacyMiddleware = convert.back(modernMiddleware)

License

MIT

convert's People

Contributors

3imed-jaberi avatar 75lb avatar gyson avatar niftylettuce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

convert's Issues

Any special steps if using koa-mount?

I'm planning on upgrading to koa 2.x, however the notes seems to indicate that koa-router needs to be update first. I'm wondering if there is something special about koa-mount as well?

throws error if `next` not defined

I'd like to be able to call converted middleware without a next param, similar to what happens in compose (https://github.com/koajs/compose/blob/master/index.js#L20). For example, using koa 2.x params middleware:

async function noop () {}
export async function organizationIdParam (id, ctx, next) {
  await convert(rbac.allow('read:organization', { organization: id }))(ctx, noop);
  ctx.organization = await Organization.findById(id);
  await next();
}

It would be nice to instead be able to call it like this:

export async function organizationIdParam (id, ctx, next) {
  await convert(rbac.allow('read:organization', { organization: id }))(ctx);
  ctx.organization = await Organization.findById(id);
  await next();
}

What do you think?

Passing argument not working?

First off, I do not know if this belongs here in this module, but I'll start here. I'm experimenting with the following code:

var convert = require('koa-convert');
var genbind = require('generator-bind');

function* myGen(i) {
    console.log(this.x, i);
}
myGen = genbind({x:666}, myGen);
convert(myGen)(9); // <--- this outputs 666 undefined, was expecting 666 9

I can't seem to pass an argument to the generator function. If this isn't to do with koa-convert then I'm guessing generator-bind is at fault.

convert.back() not respecting return from function?

I am working on a Koa v1 app and would like to implement new routes with async/await instead of generator functions. I found this library has convert.back() which is mostly working for me. However, the following example code will illustrate my problem:

router.get('/endpoint',
async function(next) {
    const token = this.request.headers.token;
    if (!token) {
        this.response.status = 401;
        return;
    }
    // I assume next is a Promise so this is not an issue?
    await next;
},
async function(next) {
    // This code still executes, even if there is no token defined on the headers.

    // Do something important that shouldn't execute with invalid auth...
});

return convert.back(router.routes());

If there is no token defined, the endpoint does return a 401 and exits the first function, but the second function is still executed. If I set up the first function as a generator function and use yield next, it works as would be expected.

Versions:

node: 6.13.1
npm: 3.10.10

├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
├─┬ [email protected]
│ ├── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]

convert bug

koa.use(convert(hbs.middleware({
    extname: '.html',
    viewPath: ['./www/admin', './www/home'],
    layoutsPath: './www',
    partialsPath: ['./www/admin', './www/home'],
    disableCache: true
})));

koa.use(async(ctx, next) => {
    const render = ctx.render;
    ctx.render = async function _convertedRender() {
        return co.call(ctx, render.apply(ctx, arguments))
    }

    await next();
});

await ctx.render('index_index');

###Question###
If I open the cache, view rendering not to come out, when I closed the cache, the view can render.

###I recorded a GIF###
http://www.wemallshop.com/hbs.gif

###Result###
497d7471-fbc4-48db-9527-1f7cfba7b8af

Actually return data, but this. The body has no output to the browser

move to koajs organization

i've invited you to be part of the @koajs/koa-v2 team in @koajs. i'd like you to transfer your repo over. i'm not sure how transferring works when you're not an owner of the organization, though. let me know if you run into any issues.

Passing arguments v1.x into middleware?

Currently I can't figure out an elegant way for passing arguments into v1.x middleware.

Is this something that could be useful to anyone?

I'm using the koa-ratelimiter library which currently doesn't have v2.x support. However I'd like to also pass in arguments.

The old version looked like this

app.use(ratelimit({
  max: opts.ratelimit,
  duration: opts.duration,
  db: redis.createClient()
}));

What I'd like to do is pass in the options as a second parameter to convert.

app.use(convert(ratelimit, {
  max: opts.ratelimit,
  duration: opts.duration,
  db: redis.createClient()
}));

Does not work with node v6.5

Hi,
I have a webapp that I built using the koa v2 framework. I use koa-convert, koa-generic-session and koa-redis to store user sessions in my redis instance. Since upgrading to node v6.5, my webapp fails with the following stacktrace:

TypeError: Cannot read property 'done' of undefined
      at next (/Users/dev/work/node_modules/co/index.js:98:14)
      at onFulfilled (/Users/dev/work/node_modules/co/index.js:69:7)
      at /Users/Stephen/dev/work/node_modules/co/index.js:54:5
      at Object.co (/Users/dev/work/node_modules/co/index.js:50:10)
      at next (/Users/dev/work/node_modules/co/index.js:99:29)
      at onFulfilled (/Users/dev/work/node_modules/co/index.js:69:7)

It worked fine with node v6.4. The stacktrace doesnt go past co, but through trial and error, I deleted a lot of third party libs and middleware and narrowed it down to this block of code:

app.use(convert(session({
  key: 'session',
  errorHandler(err, type, ctx) {
    console.error(err, type)
  },
  store: redisStore({ url: config.redis.url })
})))

which can be reduced to app.use(convert(session())) and still trigger the same bug.

I do not use the convert lib anywhere else in the app so I cannot quickly test to see if the issue is specifically in the convert or session lib.

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.