Socket
Socket
Sign inDemoInstall

koa-convert

Package Overview
Dependencies
2
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-convert

convert modern Koa legacy generator-based middleware to promise-based middleware


Version published
Maintainers
2
Weekly downloads
1,205,861
decreased by-15.56%
Install size
31.5 kB

Weekly downloads

Package description

What is koa-convert?

The koa-convert npm package is designed to help transition from Koa's old middleware signature (generator functions) to the new middleware signature (async functions). It wraps Koa's old generator-based middleware to be compatible with the new Koa v2.x async middleware signature. This allows developers to use legacy middleware that hasn't yet been updated to the new format, facilitating a smoother upgrade path to Koa v2.x.

What are koa-convert's main functionalities?

Converting generator-based middleware

This feature allows you to wrap old Koa middleware that uses generator functions into a form that's compatible with Koa v2.x's async middleware. The code sample demonstrates how to convert a simple timing middleware to the new format.

const convert = require('koa-convert');
const oldMiddleware = function *(next) {
  const start = new Date;
  yield next;
  const ms = new Date - start;
  console.log(`${this.method} ${this.url} - ${ms}ms`);
};

app.use(convert(oldMiddleware));

Other packages similar to koa-convert

Readme

Source

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

Keywords

FAQs

Last updated on 10 Sep 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc