diff --git a/.autod.conf.js b/.autod.conf.js deleted file mode 100644 index b080039..0000000 --- a/.autod.conf.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict' - -module.exports = { - write: true, - prefix: '^', - devprefix: '^', - exclude: [ - 'test/fixtures', - ], - devdep: [ - 'autod', - 'egg-bin', - 'egg-ci', - 'eslint', - 'eslint-config-egg', - ], - keep: [ - ], - semver: [ - ], -}; diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 7e3649a..0000000 --- a/.editorconfig +++ /dev/null @@ -1,16 +0,0 @@ -# http://editorconfig.org -root = true - -[*] -indent_style = space -indent_size = 2 -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -[*.md] -trim_trailing_whitespace = false - -[Makefile] -indent_style = tab diff --git a/.eslintrc b/.eslintrc index 9f92098..c799fe5 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,3 @@ { - "extends": "egg" + "extends": "eslint-config-egg" } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..22a8662 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,18 @@ +name: CI + +on: + push: + branches: [ master ] + + pull_request: + branches: [ master ] + + workflow_dispatch: {} + +jobs: + Job: + name: Node.js + uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 + with: + os: 'ubuntu-latest' + version: '14, 16, 18' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..1612587 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,17 @@ +name: Release + +on: + push: + branches: [ master ] + + workflow_dispatch: {} + +jobs: + release: + name: Node.js + uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 + secrets: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} + with: + checkTest: false diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 5ec1467..0000000 --- a/.travis.yml +++ /dev/null @@ -1,12 +0,0 @@ -sudo: false -language: node_js -node_js: - - '4' - - '6' - - '7' -install: - - npm i npminstall && npminstall -script: - - npm run ci -after_script: - - npminstall codecov && codecov diff --git a/README.md b/README.md index 6a762d9..f915b52 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,14 @@ # ready-callback [![NPM version][npm-image]][npm-url] -[![build status][travis-image]][travis-url] +[![CI](https://github.com/node-modules/ready-callback/actions/workflows/ci.yml/badge.svg)](https://github.com/node-modules/ready-callback/actions/workflows/ci.yml) [![Test coverage][codecov-image]][codecov-url] -[![David deps][david-image]][david-url] [![npm download][download-image]][download-url] [npm-image]: https://img.shields.io/npm/v/ready-callback.svg?style=flat-square [npm-url]: https://npmjs.org/package/ready-callback -[travis-image]: https://img.shields.io/travis/node-modules/ready-callback.svg?style=flat-square -[travis-url]: https://travis-ci.org/node-modules/ready-callback [codecov-image]: https://codecov.io/github/node-modules/ready-callback/coverage.svg?branch=master [codecov-url]: https://codecov.io/github/node-modules/ready-callback?branch=master -[david-image]: https://img.shields.io/david/node-modules/ready-callback.svg?style=flat-square -[david-url]: https://david-dm.org/node-modules/ready-callback [download-image]: https://img.shields.io/npm/dm/ready-callback.svg?style=flat-square [download-url]: https://npmjs.org/package/ready-callback @@ -23,7 +18,7 @@ Launch server after all async task ready ## Install -``` +```bash $ npm install ready-callback ``` @@ -31,7 +26,7 @@ $ npm install ready-callback **Note: ready-callback is using `class`, so you should use node>=2** -``` +```js var koa = require('koa'); var ready = require('ready-callback')(); var app = koa(); @@ -51,7 +46,7 @@ app.ready(function() { If task is called with error, `error` event will be emit, `ready` will never be called. -``` +```js // register a service that will emit error var done = app.readyCallback('service'); serviceLaunch(function(err) { @@ -68,7 +63,7 @@ app.on('error', function(err) { If you set a task weak dependency, task will be done without emit `error`. -``` +```js var done = app.readyCallback('service', {isWeakDep: true}); serviceLaunch(function(err) { done(err); @@ -86,7 +81,7 @@ app.on('error', function(err) { You can also set for all ready-callback -``` +```js var ready = require('ready-callback')({isWeakDep: true}); ``` @@ -94,7 +89,7 @@ var ready = require('ready-callback')({isWeakDep: true}); You can get status every callback end. -``` +```js app.on('ready_stat', function(data) { console.log(data.id); // id of the ended task console.log(data.remain); // tasks waiting to be ended @@ -105,7 +100,7 @@ app.on('ready_stat', function(data) { You can set timeout when a task run a long time. -``` +```js var ready = require('ready-callback')({timeout: 1000}); ready.mixin(app); app.on('ready_timeout', function(id) { @@ -121,7 +116,7 @@ serviceLaunch(function() { You can also set timeout for every task -``` +```js ready.mixin(app); app.on('ready_timeout', function(id) { // this will be called after 1s that `service` task don't complete diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 2efd0fa..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,16 +0,0 @@ -environment: - matrix: - - nodejs_version: '4' - - nodejs_version: '6' - - nodejs_version: '7' - -install: - - ps: Install-Product node $env:nodejs_version - - npm i npminstall && node_modules\.bin\npminstall - -test_script: - - node --version - - npm --version - - npm run ci - -build: off diff --git a/lib/ready.js b/lib/ready.js index b8204f5..7e2394c 100644 --- a/lib/ready.js +++ b/lib/ready.js @@ -1,10 +1,8 @@ -'use strict'; - +const debug = require('util').debuglog('ready-callback'); const EventEmitter = require('events'); const once = require('once'); const ready = require('get-ready'); -const uuid = require('uuid'); -const debug = require('debug')('ready-callback'); +const { randomUUID } = require('crypto'); const defaults = { timeout: 10000, @@ -17,7 +15,7 @@ const defaults = { class Ready extends EventEmitter { /** - * @constructor + * @class * @param {Object} opt * - {Number} [timeout=10000] - emit `ready_timeout` when it doesn't finish but reach the timeout * - {Boolean} [isWeakDep=false] - whether it's a weak dependency @@ -48,7 +46,7 @@ class Ready extends EventEmitter { /** * Mix `ready` and `readyCallback` to `obj` - * @method Ready#mixin + * @function Ready#mixin * @param {Object} obj - The mixed object * @return {Ready} this */ @@ -76,14 +74,14 @@ class Ready extends EventEmitter { /** * Create a callback, ready won't be fired until all the callbacks are triggered. - * @method Ready#readyCallback + * @function Ready#readyCallback * @param {String} name - * @param {Object} opt - the options that will override global * @return {Function} - a callback */ readyCallback(name, opt) { opt = Object.assign({}, defaults, this.opt, opt); - const cacheKey = uuid.v1(); + const cacheKey = randomUUID(); opt.name = name || cacheKey; const timer = setTimeout(() => this.emit('ready_timeout', opt.name), opt.timeout); const cb = once(err => { @@ -104,7 +102,7 @@ class Ready extends EventEmitter { /** * resolve ths callback when readyCallback be called - * @method Ready#readyDone + * @function Ready#readyDone * @private * @param {String} id - unique id generated by readyCallback * @param {Object} opt - the options that will override global diff --git a/package.json b/package.json index 73189a1..b8f56e8 100644 --- a/package.json +++ b/package.json @@ -13,19 +13,15 @@ "lib" ], "dependencies": { - "debug": "^2.6.0", - "get-ready": "^2.0.0", - "once": "^1.4.0", - "uuid": "^3.0.1" + "debug": "^4.3.4", + "get-ready": "^2.0.1", + "once": "^1.4.0" }, "devDependencies": { - "autod": "^2.7.1", - "egg-bin": "^1.11.1", - "egg-ci": "^1.1.0", - "eslint": "^3.15.0", - "eslint-config-egg": "^3.2.0", + "egg-bin": "^5.9.0", + "eslint": "^8.31.0", + "eslint-config-egg": "^12.1.0", "koa": "^1.2.4", - "mz-modules": "^2.1.0", "spy": "^1.0.0" }, "repository": { @@ -36,16 +32,12 @@ "author": "popomore ", "license": "MIT", "scripts": { - "autod": "autod", "lint": "eslint .", "test": "npm run lint -- --fix && egg-bin test", "cov": "egg-bin cov", "ci": "npm run lint && egg-bin cov" }, "engines": { - "node": ">=4.0.0" - }, - "ci": { - "version": "4, 6, 7" + "node": ">=14.0.0" } } diff --git a/test/index.test.js b/test/index.test.js index 756dbf3..1d9bc6d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,5 +1,3 @@ -'use strict'; - const assert = require('assert'); const koa = require('koa'); const spy = require('spy'); diff --git a/test/ready.test.js b/test/ready.test.js index 31ab28c..fdbd004 100644 --- a/test/ready.test.js +++ b/test/ready.test.js @@ -1,10 +1,13 @@ -'use strict'; - const assert = require('assert'); const EventEmitter = require('events'); const spy = require('spy'); const Ready = require('..').Ready; -const sleep = require('mz-modules/sleep'); + +function sleep(ms) { + return new Promise(resolve => { + setTimeout(resolve, ms); + }); +} describe('Ready', function() {