diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..00afbd7 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,46 @@ +# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions + +name: Node.js CI + +on: + push: + branches: + - main + - master + pull_request: + branches: + - main + - master + schedule: + - cron: '0 2 * * *' + +jobs: + build: + runs-on: ${{ matrix.os }} + + strategy: + fail-fast: false + matrix: + node-version: [12, 14, 16, 18] + os: [ubuntu-latest, macos-latest] + + steps: + - name: Checkout Git Source + uses: actions/checkout@v2 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: Install Dependencies + run: npm i -g npminstall && npminstall + + - name: Continuous Integration + run: npm run ci + + - name: Code Coverage + uses: codecov/codecov-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 72c016d..0000000 --- a/.travis.yml +++ /dev/null @@ -1,12 +0,0 @@ -sudo: false -language: node_js -node_js: - - '18' - - '16' - - '14' - - '12' - - '10' - - '8' - - '6' -script: 'npm run test-travis' -after_script: 'npm i codecov && codecov' diff --git a/LICENSE.txt b/LICENSE.txt index b4c7a59..0fee3ce 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,7 +1,7 @@ This software is licensed under the MIT License. Copyright (C) 2013 - 2014 fengmk2 -Copyright (C) 2015 - 2016 node-modules +Copyright (C) 2015 - present node-modules and other contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index d9001b5..557e6fe 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,14 @@ address ======= [![NPM version][npm-image]][npm-url] -[![build status][travis-image]][travis-url] +[![Node.js CI](https://github.com/node-modules/address/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/address/actions/workflows/nodejs.yml) [![Test coverage][coveralls-image]][coveralls-url] -[![Gittip][gittip-image]][gittip-url] -[![David deps][david-image]][david-url] [![npm download][download-image]][download-url] [npm-image]: https://img.shields.io/npm/v/address.svg?style=flat-square [npm-url]: https://npmjs.org/package/address -[travis-image]: https://img.shields.io/travis/node-modules/address.svg?style=flat-square -[travis-url]: https://travis-ci.org/node-modules/address [coveralls-image]: https://img.shields.io/coveralls/node-modules/address.svg?style=flat-square [coveralls-url]: https://coveralls.io/r/node-modules/address?branch=master -[gittip-image]: https://img.shields.io/gittip/fengmk2.svg?style=flat-square -[gittip-url]: https://www.gittip.com/fengmk2/ -[david-image]: https://img.shields.io/david/node-modules/address.svg?style=flat-square -[david-url]: https://david-dm.org/node-modules/address [download-image]: https://img.shields.io/npm/dm/address.svg?style=flat-square [download-url]: https://npmjs.org/package/address @@ -36,7 +28,7 @@ $ npm install address Get IP is sync and get MAC is async for now. ```js -var address = require('address'); +const address = require('address'); // default interface 'eth' on linux, 'en' on osx. address.ip(); // '192.168.0.2' @@ -57,12 +49,12 @@ address.mac('vboxnet', function (err, addr) { ### Get all addresses: IPv4, IPv6 and MAC ```js -address(function (err, addrs) { +address((err, addrs) => { console.log(addrs.ip, addrs.ipv6, addrs.mac); // '192.168.0.2', 'fe80::7aca:39ff:feb0:e67d', '78:ca:39:b0:e6:7d' }); -address('vboxnet', function (err, addrs) { +address('vboxnet', (err, addrs) => { console.log(addrs.ip, addrs.ipv6, addrs.mac); // '192.168.56.1', null, '0a:00:27:00:00:00' }); @@ -78,26 +70,16 @@ address.interface('IPv4', 'eth1'); ### Get DNS servers ```js -address.dns(function (err, addrs) { +address.dns((err, addrs) => { console.log(addrs); // ['10.13.2.1', '10.13.2.6'] }); ``` -## benchmark - -run `$ npm run benchmark` - -``` -18,929 op/s » #ip -17,622 op/s » #ipv6 -16,347 op/s » #mac -11,906 op/s » #dns -``` - ## License [MIT](LICENSE.txt) + ## Contributors @@ -108,4 +90,4 @@ run `$ npm run benchmark` This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Fri Apr 29 2022 20:59:23 GMT+0800`. - \ No newline at end of file + diff --git a/benchmark/address.benchmark.js b/benchmark/address.benchmark.js deleted file mode 100644 index c37dbb6..0000000 --- a/benchmark/address.benchmark.js +++ /dev/null @@ -1,29 +0,0 @@ -var Benchmark = require('benchmark'); -var benchmarks = require('beautify-benchmark'); - -var suite = new Benchmark.Suite(); - -var address = require('..'); -var os = require('os'); - -suite.add('os.platform()', function() { - os.platform(); -}); - -suite.on('cycle', function(event) { - benchmarks.add(event.target); -}); - -suite.on('start', function(event) { - console.log('\n Starting...', - process.version, Date()); -}); - -suite.on('complete', function done() { - benchmarks.log(); -}); - -suite.run({ 'async': false }); - - -// os.platform() x 67,436,816 ops/sec ±1.56% (84 runs sampled) \ No newline at end of file diff --git a/package.json b/package.json index c83b720..1de3d1b 100644 --- a/package.json +++ b/package.json @@ -8,26 +8,18 @@ "lib" ], "scripts": { - "test": "mocha --check-leaks -R spec -t 5000 test/*.test.js", - "test-cov": "istanbul cover node_modules/.bin/_mocha -- --check-leaks -t 5000 test/*.test.js", - "test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- --check-leaks -t 5000 test/*.test.js", - "benchmark": "matcha", - "autod": "autod -w --prefix '^'", + "test": "egg-bin test", + "ci": "egg-bin cov", "contributors": "git-contributor" }, "dependencies": {}, "devDependencies": { "@types/node": "^12.7.2", - "beautify-benchmark": "*", - "benchmark": "*", + "egg-bin": "^4.19.0", + "egg-ci": "^1.19.0", "git-contributor": "^1.1.0", - "istanbul": "*", - "matcha": "*", "mm": "*", - "mocha": "*", - "pedding": "*", "runscript": "^1.4.0", - "should": "*", "typescript": "^3.5.3" }, "repository": { @@ -43,6 +35,13 @@ "engines": { "node": ">= 10.0.0" }, + "ci": { + "version": "12, 14, 16, 18", + "type": "github", + "os": { + "github": "linux, macos" + } + }, "author": "fengmk2 ", "license": "MIT" } diff --git a/test/address.test.js b/test/address.test.js index 15a5290..2891665 100644 --- a/test/address.test.js +++ b/test/address.test.js @@ -1,188 +1,183 @@ 'use strict'; -var os = require('os'); -var child = require('child_process'); -var path = require('path'); -var should = require('should'); -var mm = require('mm'); -var fs = require('fs'); -var pedding = require('pedding'); -var address = require('../'); - -var fixtures = path.join(__dirname, 'fixtures'); - -describe('address.test.js', function () { +const assert = require('assert'); +const os = require('os'); +const child = require('child_process'); +const path = require('path'); +const mm = require('mm'); +const fs = require('fs'); +const address = require('..'); + +const fixtures = path.join(__dirname, 'fixtures'); + +describe('test/address.test.js', () => { beforeEach(mm.restore); + afterEach(mm.restore); - describe('regex check', function () { - it('should MAC_IP_RE pass', function () { - should.ok(address.MAC_IP_RE.test(' inet 10.7.84.211 netmask 0xfffffc00 broadcast 10.7.87.255')); - should.ok(address.MAC_IP_RE.test(' inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0')); + describe('regex check', () => { + it('should MAC_IP_RE pass', () => { + assert(address.MAC_IP_RE.test(' inet 10.7.84.211 netmask 0xfffffc00 broadcast 10.7.87.255')); + assert(address.MAC_IP_RE.test(' inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0')); }); - it('should MAC_RE pass', function () { - should.ok(address.MAC_RE.test(' ether c4:2c:03:32:d5:3d ')); - should.ok(address.MAC_RE.test('eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 ')); + it('should MAC_RE pass', () => { + assert(address.MAC_RE.test(' ether c4:2c:03:32:d5:3d ')); + assert(address.MAC_RE.test('eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 ')); }); }); - describe('address()', function () { - it('should return first ethernet addrs', function (done) { - address(function (err, addr) { - should.not.exists(err); - addr.should.have.keys('ip', 'ipv6', 'mac'); - addr.mac && addr.mac.should.match(/^(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}$/i); - addr.ip && addr.ip.should.match(/^\d+\.\d+\.\d+\.\d+$/); + describe('address()', () => { + it('should return first ethernet addrs', done => { + address((err, addr) => { + assert(!err); + assert.deepStrictEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); + assert.match(addr.mac, /^(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}$/i); + assert.match(addr.ip, /^\d+\.\d+\.\d+\.\d+$/); done(); }); }); - it('should return first ethernet addrs from osx', function (done) { - mm(address, 'interface', function () { - return {address: '192.168.2.104'}; + it('should return first ethernet addrs from osx', done => { + mm(address, 'interface', () => { + return { address: '192.168.2.104' }; }); mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'darwin.txt'), 'utf8')); - address('en', function (err, addr) { - should.not.exists(err); - addr.should.have.keys('ip', 'ipv6', 'mac'); - addr.ip.should.equal('192.168.2.104'); - // addr.ipv6.should.match(/^[a-z0-9]{4}\:\:[a-z0-9]{4}\:[a-z0-9]{4}\:[a-z0-9]{4}\:[a-z0-9]{4}$/); - addr.mac.should.equal('78:ca:39:b0:e6:7d'); + address('en', (err, addr) => { + assert(!err); + assert.deepStrictEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); + assert.strictEqual(addr.ip, '192.168.2.104'); + assert.strictEqual(addr.mac, '78:ca:39:b0:e6:7d'); done(); }); }); - it('should return first ethernet addrs from linux', function (done) { - mm(address, 'interface', function () { - return {address: '10.125.5.202'}; + it('should return first ethernet addrs from linux', done => { + mm(address, 'interface', () => { + return { address: '10.125.5.202' }; }); mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'linux.txt'), 'utf8')); - address('eth', function (err, addr) { - should.not.exists(err); - addr.should.have.keys('ip', 'ipv6', 'mac'); - addr.ip.should.equal('10.125.5.202'); - // addr.ipv6.should.match(/^[a-z0-9]{4}\:\:[a-z0-9]{4}\:[a-z0-9]{4}\:[a-z0-9]{4}\:[a-z0-9]{4}$/); - addr.mac.should.equal('00:16:3E:00:0A:29'); + address('eth', (err, addr) => { + assert(!err); + assert.deepStrictEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); + assert.strictEqual(addr.ip, '10.125.5.202'); + assert.strictEqual(addr.mac, '00:16:3E:00:0A:29'); done(); }); }); - it('should return first vnic interface addrs from osx', function (done) { - mm(address, 'ip', function () { + it('should return first vnic interface addrs from osx', done => { + mm(address, 'ip', () => { return '10.211.55.2'; }); mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'darwin.txt'), 'utf8')); - address('vnic', function (err, addr) { - should.not.exists(err); - addr.ip.should.equal('10.211.55.2') - // console.log(addr) - // addr.mac.should.equal('00:1c:42:00:00:08'); - should.not.exists(addr.ipv6); + address('vnic', (err, addr) => { + assert(!err); + assert.strictEqual(addr.ip, '10.211.55.2') + assert(!addr.ipv6); done(); }); }); - it('should return first local loopback addrs', function (done) { - address('lo', function (err, addr) { - should.not.exists(err); - addr.should.have.keys('ip', 'ipv6', 'mac'); - addr.should.property('ip').with.equal('127.0.0.1'); + it('should return first local loopback addrs', done => { + address('lo', (err, addr) => { + assert(!err); + assert.deepStrictEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); + assert.strictEqual(addr.ip, '127.0.0.1'); done(); }); }); - it('should return first local loopback addrs from linux', function (done) { + it('should return first local loopback addrs from linux', done => { mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'linux.txt'), 'utf8')); - address('lo', function (err, addr) { - should.not.exists(err); - addr.should.have.keys('ip', 'ipv6', 'mac'); - addr.should.property('ip').with.equal('127.0.0.1'); + address('lo', (err, addr) => { + assert(!err); + assert.deepStrictEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); + assert.strictEqual(addr.ip, '127.0.0.1'); done(); }); }); }); - describe('interface()', function () { - it('should return interface with family', function () { - var item = address.interface(); - should.exists(item); - item.should.have.property('address'); - item.should.have.property('family'); + describe('interface()', () => { + it('should return interface with family', () => { + const item = address.interface(); + assert(item); + assert(item.address); + assert(item.family); }); }); - describe('address.mac()', function () { - it.skip('should return mac', function (done) { - address.mac(function (err, mac) { - should.not.exists(err); - should.exists(mac); - mac.should.match(/(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}/i); + describe('address.mac()', () => { + it.skip('should return mac', done => { + address.mac((err, mac) => { + assert(!err); + assert(mac); + assert.match(mac, /(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}/i); done(); }); }); - it('should return mock mac address', function (done) { - mm(address, 'interface', function () { - return {address: os.platform() === 'linux' ? '10.125.5.202' : '192.168.2.104'}; + it('should return mock mac address', done => { + mm(address, 'interface', () => { + return { address: os.platform() === 'linux' ? '10.125.5.202' : '192.168.2.104' }; }); mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, os.platform() + '.txt'), 'utf8')); - address.mac(os.platform() === 'linux' ? 'eth' : 'en', function (err, mac) { - should.not.exists(err); - should.exists(mac); - mac.should.match(/(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}/i); + address.mac(os.platform() === 'linux' ? 'eth' : 'en', (err, mac) => { + assert(!err); + assert(mac); + assert.match(mac, /(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}/i); done(); }); }); - it('should return null when ip not exists', function (done) { - mm(address, 'interface', function () { + it('should return null when ip not exists', done => { + mm(address, 'interface', () => { return null; }); - address.mac(function (err, mac) { - should.not.exists(err); - should.not.exists(mac); + address.mac((err, mac) => { + assert(!err); + assert(!mac); done(); }); }); - it('should return err when ifconfig cmd exec error', function (done) { - mm(address, 'interface', function () { + it('should return err when ifconfig cmd exec error', done => { + mm(address, 'interface', () => { return null; }); mm.error(child, 'exec'); - address.mac(function (err, mac) { - // should.exists(err); - should.not.exists(mac); + address.mac((err, mac) => { + assert(!err); + assert(!mac); done(); }); }); - it('should return mac mock win32', function (done) { - - mm(os, 'platform', function () { + it('should return mac mock win32', done => { + mm(os, 'platform', () => { return 'win32'; }); - mm(os, 'networkInterfaces', function () { + mm(os, 'networkInterfaces', () => { return require(path.join(__dirname, './fixtures/win32_interfaces.json')); }); - address.mac(function (err, mac) { - should.not.exists(err); - should.exists(mac); - mac.should.equal('e8:2a:ea:8b:c2:20'); + address.mac((err, mac) => { + assert(!err); + assert(mac); + assert.strictEqual(mac, 'e8:2a:ea:8b:c2:20'); done(); }); }); }); - describe('address.ip()', function () { - it('should return 127.0.0.1', function () { - address.ip('lo').should.equal('127.0.0.1'); + describe('address.ip()', () => { + it('should return 127.0.0.1', () => { + assert.strictEqual(address.ip('lo'), '127.0.0.1'); }); - it('should return the first not 127.0.0.1 interface', function () { - mm(os, 'networkInterfaces', function () { + it('should return the first not 127.0.0.1 interface', () => { + mm(os, 'networkInterfaces', () => { return { lo: [ { address: '127.0.0.1', @@ -193,11 +188,11 @@ describe('address.test.js', function () { family: 'IPv4', internal: false } ] }; }); - address.ip().should.equal('10.206.52.79'); + assert.strictEqual(address.ip(), '10.206.52.79'); }); - it('should return utun1', function () { - mm(os, 'networkInterfaces', function () { + it('should return utun1', () => { + mm(os, 'networkInterfaces', () => { return { lo: [ { address: '127.0.0.1', @@ -212,39 +207,39 @@ describe('address.test.js', function () { family: 'IPv4', internal: false } ] }; }); - address.ip('utun').should.equal('10.206.52.79'); - address.ipv6('utun').should.equal('fe80::696:ad3d:eeec:1722'); + assert.strictEqual(address.ip('utun'), '10.206.52.79'); + assert.strictEqual(address.ipv6('utun'), 'fe80::696:ad3d:eeec:1722'); }); }); - describe('address.dns()', function () { - it('should return dns servers from osx', function (done) { + describe('address.dns()', () => { + it('should return dns servers from osx', done => { mm.data(fs, 'readFile', fs.readFileSync(path.join(fixtures, 'dns_darwin.txt'), 'utf8')); - address.dns(function (err, servers) { - should.not.exists(err); - should.exists(servers); - servers.should.be.instanceof(Array); - servers.length.should.above(0); + address.dns((err, servers) => { + assert(!err); + assert(servers); + assert(Array.isArray(servers)); + assert(servers.length > 0); done(); }); }); - it('should return dns servers from linux', function (done) { + it('should return dns servers from linux', done => { mm.data(fs, 'readFile', fs.readFileSync(path.join(fixtures, 'dns_linux.txt'), 'utf8')); - address.dns(function (err, servers) { - should.not.exists(err); - should.exists(servers); - servers.should.be.instanceof(Array); - servers.length.should.above(0); + address.dns((err, servers) => { + assert(!err); + assert(servers); + assert(Array.isArray(servers)); + assert(servers.length > 0); done(); }); }); - it('should return err when fs error', function (done) { + it('should return err when fs error', done => { mm.error(fs, 'readFile'); - address.dns(function (err, servers) { - should.exists(err); - should.not.exists(servers); + address.dns((err, servers) => { + assert(err); + assert(!servers); done(); }); }); diff --git a/test/ts.test.js b/test/ts.test.js index bc04ca8..5a5597c 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -1,9 +1,8 @@ const runscript = require('runscript'); const path = require('path'); -const assert = require('assert'); -describe('test/ts.test.js', function() { - it('should works with ts without error', function (done) { +describe('test/ts.test.js', () => { + it('should works with ts without error', done => { const cwd = path.resolve(__dirname, './fixtures/ts'); const tsconfigPath = path.resolve(cwd, 'tsconfig.json'); const testFile = path.resolve(cwd, 'test');