Socket
Socket
Sign inDemoInstall

koa-connect

Package Overview
Dependencies
0
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.1 to 2.1.0

examples/typescript.ts

2

examples/session.js

@@ -10,3 +10,3 @@ const Koa = require('koa')

app.use((ctx) {
app.use((ctx) => {
const name = ctx.req.session.name = ctx.query.name || ctx.req.session.name

@@ -13,0 +13,0 @@ ctx.body = name || 'Please, enter your name'

@@ -12,3 +12,3 @@ const Koa = require('koa')

app.use((ctx) {
app.use((ctx) => {
ctx.body = 'koa'

@@ -15,0 +15,0 @@ })

@@ -0,38 +1,37 @@

"use strict";
const noop = () => { };
/**
* If the middleware function does declare receiving the `next` callback
* assume that it's synchronous and invoke `next` ourselves
* If the middleware function does not declare receiving the `next` callback
* assume that it's synchronous and invoke `next` ourselves.
*/
function noCallbackHandler(ctx, connectMiddleware, next) {
connectMiddleware(ctx.req, ctx.res)
return next()
connectMiddleware(ctx.req, ctx.res, noop);
return next();
}
/**
* The middleware function does include the `next` callback so only resolve
* the Promise when it's called. If it's never called, the middleware stack
* completion will stall
* completion will stall.
*/
function withCallbackHandler(ctx, connectMiddleware, next) {
return new Promise((resolve, reject) => {
connectMiddleware(ctx.req, ctx.res, err => {
if (err) reject(err)
else resolve(next())
})
})
return new Promise((resolve, reject) => {
connectMiddleware(ctx.req, ctx.res, (err) => {
if (err)
reject(err);
else
resolve(next());
});
});
}
/**
* Returns a Koa middleware function that varies its async logic based on if the
* given middleware function declares at least 3 parameters, i.e. includes
* the `next` callback function
* the `next` callback function.
*/
function koaConnect(connectMiddleware) {
const handler = connectMiddleware.length < 3
? noCallbackHandler
: withCallbackHandler
return function koaConnect(ctx, next) {
return handler(ctx, connectMiddleware, next)
}
const handler = connectMiddleware.length < 3 ? noCallbackHandler : withCallbackHandler;
return function koaConnect(ctx, next) {
return handler(ctx, connectMiddleware, next);
};
}
module.exports = koaConnect
module.exports = koaConnect;
{
"name": "koa-connect",
"version": "2.0.1",
"version": "2.1.0",
"description": "Use Connect/Express middleware in Koa",
"repository": "vkurchatkin/koa-connect",
"main": "index.js",
"types": "index.d.ts",
"author": "Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com>",

@@ -13,9 +14,22 @@ "contributors": [

"scripts": {
"test": "mocha tests.js",
"test:watch": "npm run test -- --watch --reporter nyan"
"start": "tsc --watch",
"build": "tsc -p tsconfig-build.json",
"test": "mocha -r ts-node/register tests.ts",
"test:watch": "npm run test -- --watch --watch-extensions ts --reporter nyan"
},
"devDependencies": {
"@types/body-parser": "^1.19.0",
"@types/koa": "^2.0.46",
"@types/mocha": "^7.0.2",
"@types/node": "^10.9.2",
"@types/supertest": "^2.0.9",
"body-parser": "^1.19.0",
"husky": "^4.2.5",
"koa": "^2.0.0",
"lint-staged": "^10.2.9",
"mocha": "^2.3.4",
"supertest": "^1.1.0"
"prettier": "^2.0.5",
"supertest": "^1.1.0",
"ts-node": "^8.10.2",
"typescript": "^3.0.1"
},

@@ -27,3 +41,12 @@ "keywords": [

"middleware"
]
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"!(*.d).ts": "prettier --single-quote --write",
"*.md": "prettier --write"
}
}

@@ -31,10 +31,10 @@ # koa-connect [![npm package badge][npm badge]][npm]

```javascript
const Koa = require('koa')
const c2k = require('koa-connect')
const Koa = require('koa');
const c2k = require('koa-connect');
// A generic Express-style middleware function
function connectMiddlware(req, res, next) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('From the Connect middleware')
next()
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('From the Connect middleware');
next();
}

@@ -48,5 +48,5 @@

})
.catch(err => {
.catch((err) => {
// Error handling from downstream middleware, like usual
})
});
}

@@ -57,3 +57,3 @@

try {
await next()
await next();
} catch (e) {

@@ -65,18 +65,30 @@ // Normal error handling

const app = new Koa()
app.use(koaMiddlware)
app.use(c2k(connectMiddlware))
const app = new Koa();
app.use(koaMiddlware);
app.use(c2k(connectMiddlware));
app.use((ctx, next) => {
console.log('It will continue on to here')
})
console.log('It will continue on to here');
});
app.listen(3000)
app.listen(3000);
```
## Testing
## Contributing
Tests are in `tests.js` and are made with the [Mocha](https://mochajs.org) framework. You can run them with `npm test` or `npm run test:watch`
### Developing
`npm start` starts the TypeScript compiler in watch mode. Code will be auto-formatted upon commit as part of the Prettier + lint-staged + Husky setup.
### Building
`npm run build` produces the types declaration file and JavaScript file for publishing.
### Testing
`npm test` runs the tests. Tests are in `tests.js` and are made with the [Mocha](https://mochajs.org) framework.
Use `npm run test:watch` in conjunction with `npm start` to automatically re-run the tests when the source changes.
## License
MIT
SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc