Skip to content

Commit

Permalink
refactor: required Node.js >= 12.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 6, 2022
1 parent cffcb1e commit fe76c4a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x]
node-version: [12.17.0, 12.x, 14.x, 16.x, 18.x]

steps:
- uses: actions/checkout@v2
Expand Down
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![gitter][gitter-image]][gitter-url]
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![build status][github-action-image]][github-action-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![OpenCollective Backers][backers-image]](#backers)
[![OpenCollective Sponsors][sponsors-image]](#sponsors)
Expand All @@ -17,7 +17,7 @@

## Installation

Koa requires __node v12__ or higher for ES2015 and async function support.
Koa requires __node v12.17.0__ or higher for ES2015 and async function support.

```
$ npm install koa
Expand Down Expand Up @@ -275,8 +275,8 @@ Become a sponsor and get your logo on our README on Github with a link to your s

[npm-image]: https://img.shields.io/npm/v/koa.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/koa
[travis-image]: https://img.shields.io/travis/koajs/koa/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/koa
[github-action-image]: https://github.com/koajs/koa/actions/workflows/node.js.yml/badge.svg
[github-action-url]: https://github.com/koajs/koa/actions/workflows/node.js.yml
[coveralls-image]: https://img.shields.io/codecov/c/github/koajs/koa.svg?style=flat-square
[coveralls-url]: https://codecov.io/github/koajs/koa?branch=master
[backers-image]: https://opencollective.com/koajs/backers/badge.svg?style=flat-square
Expand Down
64 changes: 64 additions & 0 deletions __tests__/application/currentContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

'use strict'

const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')

describe('app.currentContext', () => {
it('should throw error if AsyncLocalStorage not support', () => {
if (require('async_hooks').AsyncLocalStorage) return
assert.throws(() => new Koa({ asyncLocalStorage: true }),
/Requires node 12\.17\.0 or higher to enable asyncLocalStorage/)
})

it('should get currentContext return context when asyncLocalStorage enable', async () => {
if (!require('async_hooks').AsyncLocalStorage) return

const app = new Koa({ asyncLocalStorage: true })

app.use(async ctx => {
assert(ctx === app.currentContext)
await new Promise(resolve => {
setTimeout(() => {
assert(ctx === app.currentContext)
resolve()
}, 1)
})
await new Promise(resolve => {
assert(ctx === app.currentContext)
setImmediate(() => {
assert(ctx === app.currentContext)
resolve()
})
})
assert(ctx === app.currentContext)
app.currentContext.body = 'ok'
})

const requestServer = async () => {
assert(app.currentContext === undefined)
await request(app.callback()).get('/').expect('ok')
assert(app.currentContext === undefined)
}

await Promise.all([
requestServer(),
requestServer(),
requestServer(),
requestServer(),
requestServer()
])
})

it('should get currentContext return undefined when asyncLocalStorage disable', async () => {
const app = new Koa()

app.use(async ctx => {
assert(app.currentContext === undefined)
ctx.body = 'ok'
})

await request(app.callback()).get('/').expect('ok')
})
})
9 changes: 5 additions & 4 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

const debug = require('debug')('koa:application')
const assert = require('assert')
const onFinished = require('on-finished')
const response = require('./response')
const compose = require('koa-compose')
Expand Down Expand Up @@ -63,9 +64,9 @@ module.exports = class Application extends Emitter {
this[util.inspect.custom] = this.inspect
}
if (options.asyncLocalStorage) {
const { AsyncLocalStorage } = require('async_hooks');
assert(AsyncLocalStorage, 'Requires node 13.0.0 or higher to enable asyncLocalStorage');
this.ctxStorage = new AsyncLocalStorage();
const { AsyncLocalStorage } = require('async_hooks')
assert(AsyncLocalStorage, 'Requires node 12.17.0 or higher to enable asyncLocalStorage')
this.ctxStorage = new AsyncLocalStorage()
}
}

Expand Down Expand Up @@ -154,7 +155,7 @@ module.exports = class Application extends Emitter {
/**
* return currnect contenxt from async local storage
*/
get currentContext() {
get currentContext () {
if (this.ctxStorage) return this.ctxStorage.getStore()
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"supertest": "^6.2.4"
},
"engines": {
"node": ">= 12"
"node": ">= 12.17.0"
},
"files": [
"dist",
Expand Down
64 changes: 0 additions & 64 deletions test/application/currentContext.js

This file was deleted.

0 comments on commit fe76c4a

Please sign in to comment.