Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ data/
# Build directory
lib/
.npmrc

dist/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ build/Release
# Temporary / data folders
.tmp
data/
dist/

# Source directory
src/
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ language: node_js
notifications:
email: false
node_js:
- 6
- 8
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--no-lockfile true
145 changes: 76 additions & 69 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,70 +1,77 @@
{
"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 <olalonde@gmail.com> (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: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"
},
"repository": {
"type": "git",
"url": "git+https://github.com/binded/phantom-pool.git"
},
"keywords": [
"phantom",
"phantomjs",
"generic-pool",
"pool",
"pooling"
],
"author": "Oli Lalonde <olalonde@gmail.com> (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"
}
}
68 changes: 0 additions & 68 deletions src/index.js

This file was deleted.

69 changes: 69 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<PhantomPoolInstance>;
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;
2 changes: 1 addition & 1 deletion test/benchmark.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion test/maxuses.test.js
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
Loading