From 956758013be2778d19116f09c04689cde97a6aeb Mon Sep 17 00:00:00 2001 From: taoqf Date: Tue, 16 Jul 2019 12:35:10 +0800 Subject: [PATCH 1/3] typescript --- .gitignore | 2 + .npmignore | 1 + .yarnrc | 1 + package.json | 144 ++++++++++++++++++++++--------------------- src/index.js | 68 -------------------- src/index.ts | 69 +++++++++++++++++++++ test/benchmark.js | 2 +- test/index.test.js | 2 +- test/maxuses.test.js | 2 +- tsconfig.json | 69 +++++++++++++++++++++ tslint.json | 56 +++++++++++++++++ 11 files changed, 276 insertions(+), 140 deletions(-) create mode 100644 .yarnrc delete mode 100644 src/index.js create mode 100644 src/index.ts create mode 100644 tsconfig.json create mode 100644 tslint.json diff --git a/.gitignore b/.gitignore index e9b4add..e774741 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ data/ # Build directory lib/ .npmrc + +dist/ diff --git a/.npmignore b/.npmignore index 6b8f416..2141daf 100644 --- a/.npmignore +++ b/.npmignore @@ -30,6 +30,7 @@ build/Release # Temporary / data folders .tmp data/ +dist/ # Source directory src/ diff --git a/.yarnrc b/.yarnrc new file mode 100644 index 0000000..9de2b7e --- /dev/null +++ b/.yarnrc @@ -0,0 +1 @@ +--no-lockfile true diff --git a/package.json b/package.json index c9da6d1..b07ecfb 100644 --- a/package.json +++ b/package.json @@ -1,70 +1,76 @@ { - "name": "phantom-pool", - "version": "1.2.2", - "description": "Resource pool for Node.js PhantomJS", - "main": "./lib/index.js", - "directories": { - "test": "test" - }, - "scripts": { - "test": "npm run test:fast", - "build": "npm run clean && babel ./src --out-dir ./lib --copy-files", - "clean": "rimraf ./lib", - "lint": "eslint src/ test/", - "pretest": "npm run lint", - "test:fast": "babel-tape-runner test/*.test.js", - "test:watch": "nodemon --exec npm -- run --silent test:fast || true", - "semantic-release": "git push && npm test && semantic-release pre && npm run build && npm publish && semantic-release post" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/binded/phantom-pool.git" - }, - "keywords": [ - "phantom", - "phantomjs", - "generic-pool", - "pool", - "pooling" - ], - "author": "Oli Lalonde (https://syskall.com/)", - "license": "MIT", - "bugs": { - "url": "https://github.com/binded/phantom-pool/issues" - }, - "homepage": "https://github.com/binded/phantom-pool#readme", - "engines": { - "node": ">=6" - }, - "devDependencies": { - "babel-cli": "^6.18.0", - "babel-plugin-add-module-exports": "^0.2.1", - "babel-plugin-transform-async-to-generator": "^6.24.1", - "babel-plugin-transform-object-rest-spread": "^6.23.0", - "babel-preset-es2015": "^6.24.1", - "babel-tape-runner": "^2.0.1", - "blue-tape": "^1.0.0", - "cz-conventional-changelog": "^1.2.0", - "eslint": "^4.1.1", - "eslint-config-blockai": "^1.0.3", - "nodemon": "^1.11.0", - "rimraf": "^2.5.4", - "semantic-release": "^6.3.2" - }, - "release": { - "debug": false, - "verifyConditions": { - "path": "./node_modules/semantic-release/src/lib/plugin-noop.js" - } - }, - "config": { - "commitizen": { - "path": "cz-conventional-changelog" - } - }, - "dependencies": { - "debug": "^2.3.3", - "generic-pool": "^3.1.4", - "phantom": "^3.2.0" - } -} + "name": "phantom-pool", + "version": "1.2.2", + "description": "Resource pool for Node.js PhantomJS", + "main": "./lib/index.js", + "types": "./lib/", + "directories": { + "test": "test" + }, + "scripts": { + "test": "npm run test:fast", + "build": "npm run clean && tsc && babel ./dist --out-dir ./lib --copy-files", + "clean": "rimraf ./lib ./dist", + "lint": "tslint -p tsconfig.json && eslint test/", + "pretest": "npm run lint && tsc", + "test:fast": "babel-tape-runner test/*.test.js", + "test:watch": "nodemon --exec npm -- run --silent test:fast || true", + "semantic-release": "git push && npm test && semantic-release pre && npm run build && npm publish && semantic-release post" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/binded/phantom-pool.git" + }, + "keywords": [ + "phantom", + "phantomjs", + "generic-pool", + "pool", + "pooling" + ], + "author": "Oli Lalonde (https://syskall.com/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/binded/phantom-pool/issues" + }, + "homepage": "https://github.com/binded/phantom-pool#readme", + "engines": { + "node": ">=6" + }, + "devDependencies": { + "@types/generic-pool": "^3.1.9", + "@types/phantom": "^3.2.5", + "babel-cli": "^6.26.0", + "babel-plugin-add-module-exports": "^0.2.1", + "babel-plugin-transform-async-to-generator": "^6.24.1", + "babel-plugin-transform-object-rest-spread": "^6.26.0", + "babel-preset-es2015": "^6.24.1", + "babel-tape-runner": "^2.0.0", + "blue-tape": "^1.0.0", + "cz-conventional-changelog": "^2.1.0", + "eslint": "^6.0.1", + "eslint-config-blockai": "^1.0.3", + "eslint-plugin-import": "^2.18.0", + "nodemon": "^1.19.1", + "rimraf": "^2.6.3", + "semantic-release": "^15.13.18", + "tslint": "^5.18.0", + "typescript": "^3.5.3" + }, + "release": { + "debug": false, + "verifyConditions": { + "path": "./node_modules/semantic-release/src/lib/plugin-noop.js" + } + }, + "config": { + "commitizen": { + "path": "cz-conventional-changelog" + } + }, + "dependencies": { + "debug": "^4.1.1", + "generic-pool": "^3.7.1", + "phantom": "^6.2.0" + } +} \ No newline at end of file diff --git a/src/index.js b/src/index.js deleted file mode 100644 index ee6d5e4..0000000 --- a/src/index.js +++ /dev/null @@ -1,68 +0,0 @@ -import phantom from 'phantom' -import genericPool from 'generic-pool' - -// import initDebug from 'debug' -// const debug = initDebug('phantom-pool') - -const initPhantomPool = ({ - max = 10, - // optional. if you set this, make sure to drain() (see step 3) - min = 2, - // specifies how long a resource can stay idle in pool before being removed - idleTimeoutMillis = 30000, - // specifies the maximum number of times a resource can be reused before being destroyed - maxUses = 50, - testOnBorrow = true, - phantomArgs = [], - validator = () => Promise.resolve(true), - ...otherConfig -} = {}) => { - // TODO: randomly destroy old instances to avoid resource leak? - const factory = { - create: () => phantom.create(...phantomArgs) - .then(instance => { - instance.useCount = 0 - return instance - }), - destroy: (instance) => instance.exit(), - validate: (instance) => validator(instance) - .then(valid => Promise.resolve(valid && (maxUses <= 0 || instance.useCount < maxUses))), - } - const config = { - max, - min, - idleTimeoutMillis, - testOnBorrow, - ...otherConfig, - } - const pool = genericPool.createPool(factory, config) - const genericAcquire = pool.acquire.bind(pool) - pool.acquire = () => genericAcquire().then(r => { - r.useCount += 1 - return r - }) - pool.use = (fn) => { - let resource - return pool.acquire() - .then(r => { - resource = r - return resource - }) - .then(fn) - .then((result) => { - pool.release(resource) - return result - }, (err) => { - pool.release(resource) - throw err - }) - } - - return pool -} - -// To avoid breaking backwards compatibility -// https://github.com/binded/phantom-pool/issues/12 -initPhantomPool.default = initPhantomPool - -export default initPhantomPool diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..6f7ede2 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,69 @@ +import genericPool, { Factory } from 'generic-pool'; +import phantom, { PhantomJS } from 'phantom'; + +// import initDebug from 'debug' +// const debug = initDebug('phantom-pool') + +interface PhantomPoolInstance extends PhantomJS { + useCount: number; +} + +function initPhantomPool({ + max = 10, + // optional. if you set this, make sure to drain() (see step 3) + min = 2, + // specifies how long a resource can stay idle in pool before being removed + idleTimeoutMillis = 30000, + // specifies the maximum number of times a resource can be reused before being destroyed + maxUses = 50, + testOnBorrow = true, + phantomArgs = [], + validator = (_instance: PhantomPoolInstance) => Promise.resolve(true), + ...otherConfig +} = {}) { + // TODO: randomly destroy old instances to avoid resource leak? + const factory = { + async create() { + const instance = await phantom.create(...phantomArgs) as PhantomPoolInstance; + instance.useCount = 0; + return instance; + }, + async destroy(instance) { + return instance.exit(); + }, + async validate(instance) { + const valid = await validator(instance); + return valid && (maxUses <= 0 || instance.useCount < maxUses); + } + } as Factory; + const config = { + idleTimeoutMillis, + max, + min, + testOnBorrow, + ...otherConfig + }; + const pool = genericPool.createPool(factory, config); + const genericAcquire = pool.acquire.bind(pool); + pool.acquire = async () => { + const r = await genericAcquire(); + r.useCount += 1; + return r; + }; + pool.use = async (fn) => { + const resource = await pool.acquire(); + try { + return await fn(resource); + } finally { + pool.release(resource); + } + }; + + return pool; +} + +// To avoid breaking backwards compatibility +// https://github.com/binded/phantom-pool/issues/12 +initPhantomPool.default = initPhantomPool; + +export = initPhantomPool; diff --git a/test/benchmark.js b/test/benchmark.js index 3ff6e83..6a2a199 100644 --- a/test/benchmark.js +++ b/test/benchmark.js @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import phantom from 'phantom' import http from 'http' -import createPhantomPool from '../src' +import createPhantomPool from '../dist' const startServer = () => new Promise((resolve, reject) => { const server = http.createServer((req, res) => { diff --git a/test/index.test.js b/test/index.test.js index 1bd5f5a..2fc6f18 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,5 +1,5 @@ import test from 'blue-tape' -import createPool from '../src' +import createPool from '../dist' const getState = ({ size, available, pending, max, min }) => { const state = { size, available, pending, max, min } diff --git a/test/maxuses.test.js b/test/maxuses.test.js index 146744b..c59caf7 100644 --- a/test/maxuses.test.js +++ b/test/maxuses.test.js @@ -1,5 +1,5 @@ import test from 'blue-tape' -import createPool from '../src' +import createPool from '../dist' let phantomPool test('create pool with maxUses', async () => { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..8a9675f --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,69 @@ +{ + "include": [ + "./src/" + ], + "compilerOptions": { + "module": "commonjs", + "target": "esnext", + "noImplicitAny": true, + "sourceMap": false, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "strictNullChecks": true, + "noImplicitThis": true, + "rootDir": "./src/", + "rootDirs": [], + "allowJs": false, + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "alwaysStrict": true, + "baseUrl": "", + "charset": "utf8", + "declaration": true, + "declarationDir": "./lib", + "inlineSourceMap": false, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "diagnostics": false, + "emitBOM": false, + "forceConsistentCasingInFileNames": false, + "importHelpers": false, + "inlineSources": false, + "isolatedModules": false, + "lib": [ + "esnext" + ], + "listFiles": false, + "listEmittedFiles": false, + "locale": "zh_CN", + "newLine": "lf", + "noEmit": false, + "moduleResolution": "node", + "noEmitHelpers": false, + "noEmitOnError": false, + "noImplicitReturns": false, + "noImplicitUseStrict": false, + "maxNodeModuleJsDepth": 0, + "noLib": false, + "outDir": "./dist", + "noFallthroughCasesInSwitch": false, + "noResolve": false, + "noUnusedLocals": true, + "noUnusedParameters": true, + "paths": {}, + "preserveConstEnums": false, + "pretty": true, + "removeComments": false, + "skipDefaultLibCheck": true, + "skipLibCheck": true, + "stripInternal": false, + "suppressExcessPropertyErrors": false, + "suppressImplicitAnyIndexErrors": true, + "traceResolution": false, + "typeRoots": [], + "types": [ + "node" + ], + "watch": false + } +} \ No newline at end of file diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..55aa4e6 --- /dev/null +++ b/tslint.json @@ -0,0 +1,56 @@ +{ + "extends": "tslint:recommended", + "rulesDirectory": [ + "./src/" + ], + "rules": { + "no-any": true, + "no-unused-expression": true, + "unified-signatures": false, + "whitespace": [ + true, + "check-branch", + "check-operator" + ], + "indent": [ + true, + "tabs", + 2 + ], + "quotemark": [ + true, + "single", + "avoid-escape", + "avoid-template" + ], + "max-line-length": false, + "trailing-comma": [ + true, + { + "multiline": "never", + "singleline": "never" + } + ], + "variable-name": [ + true, + "ban-keywords", + "check-format", + "allow-leading-underscore", + "allow-snake-case" + ], + "interface-name": false, + "no-bitwise": false, + "no-empty": false, + "align": [ + true, + "elements", + "members", + "statements" + ], + "new-parens": true, + "no-arg": true, + "no-conditional-assignment": true, + "no-consecutive-blank-lines": false, + "no-console": false + } +} \ No newline at end of file From 40d587bbb27afc963143d02b69a062bf648d3b78 Mon Sep 17 00:00:00 2001 From: taoqf Date: Tue, 16 Jul 2019 17:31:27 +0800 Subject: [PATCH 2/3] node 8 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3be9f94..e251ebb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,4 @@ language: node_js notifications: email: false node_js: - - 6 \ No newline at end of file + - 8 \ No newline at end of file From 6524dfa557c0658ab497244b5d3e9792d18e4569 Mon Sep 17 00:00:00 2001 From: taoqf Date: Wed, 17 Jul 2019 09:56:36 +0800 Subject: [PATCH 3/3] build ts files before lint tests --- package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b07ecfb..4c68cbc 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,9 @@ "test": "npm run test:fast", "build": "npm run clean && tsc && babel ./dist --out-dir ./lib --copy-files", "clean": "rimraf ./lib ./dist", - "lint": "tslint -p tsconfig.json && eslint test/", - "pretest": "npm run lint && tsc", + "lint:ts": "tslint -p tsconfig.json", + "lint:test": "eslint test/", + "pretest": "npm run lint:ts && tsc && npm run lint:test", "test:fast": "babel-tape-runner test/*.test.js", "test:watch": "nodemon --exec npm -- run --silent test:fast || true", "semantic-release": "git push && npm test && semantic-release pre && npm run build && npm publish && semantic-release post"