lightning-pool
Advanced tools
Comparing version 4.2.2 to 4.3.0
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createPool = void 0; | ||
exports.createPool = createPool; | ||
const tslib_1 = require("tslib"); | ||
const pool_js_1 = require("./pool.js"); | ||
__exportStar(require("./definitions.js"), exports); | ||
__exportStar(require("./pool.js"), exports); | ||
__exportStar(require("./abort-error.js"), exports); | ||
tslib_1.__exportStar(require("./abort-error.js"), exports); | ||
tslib_1.__exportStar(require("./definitions.js"), exports); | ||
tslib_1.__exportStar(require("./pool.js"), exports); | ||
function createPool(factory, config) { | ||
return new pool_js_1.Pool(factory, config); | ||
} | ||
exports.createPool = createPool; |
@@ -16,3 +16,3 @@ "use strict"; | ||
maxQueue: 1000, | ||
validation: true | ||
validation: true, | ||
}; | ||
@@ -54,3 +54,4 @@ class PoolOptions extends events_1.EventEmitter { | ||
set acquireTimeoutMillis(val) { | ||
this._acquireTimeoutMillis = val >= 0 ? val : defaultValues.acquireTimeoutMillis; | ||
this._acquireTimeoutMillis = | ||
val >= 0 ? val : defaultValues.acquireTimeoutMillis; | ||
this.emit('change', 'acquireTimeoutMillis', this._acquireTimeoutMillis); | ||
@@ -57,0 +58,0 @@ } |
@@ -5,9 +5,9 @@ "use strict"; | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
function noop() { | ||
} | ||
function noop() { } | ||
class PoolRequest { | ||
constructor(pool, callback) { | ||
constructor(pool, callback, options) { | ||
this.timedOut = false; | ||
this.created = Date.now(); | ||
this.callback = callback || noop; | ||
this.options = options; | ||
if (pool.options.acquireTimeoutMillis) { | ||
@@ -14,0 +14,0 @@ this.timeoutHandle = setTimeout(() => { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Pool = void 0; | ||
const doublylinked_1 = __importDefault(require("doublylinked")); | ||
const tslib_1 = require("tslib"); | ||
const doublylinked_1 = tslib_1.__importDefault(require("doublylinked")); | ||
const events_1 = require("events"); | ||
const putil_promisify_1 = __importDefault(require("putil-promisify")); | ||
const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify")); | ||
const abort_error_js_1 = require("./abort-error.js"); | ||
@@ -25,13 +23,18 @@ const definitions_js_1 = require("./definitions.js"); | ||
this._state = definitions_js_1.PoolState.IDLE; | ||
if (typeof factory !== 'object') | ||
if (typeof factory !== 'object') { | ||
throw new TypeError('You must provide `factory` object'); | ||
if (typeof factory.create !== 'function') | ||
} | ||
if (typeof factory.create !== 'function') { | ||
throw new TypeError('factory.create must be a function'); | ||
if (typeof factory.destroy !== 'function') | ||
} | ||
if (typeof factory.destroy !== 'function') { | ||
throw new TypeError('factory.destroy must be a function'); | ||
if (factory.validate && typeof factory.validate !== 'function') | ||
} | ||
if (factory.validate && typeof factory.validate !== 'function') { | ||
throw new TypeError('factory.validate can be a function'); | ||
if (factory.reset && typeof factory.reset !== 'function') | ||
} | ||
if (factory.reset && typeof factory.reset !== 'function') { | ||
throw new TypeError('factory.reset can be a function'); | ||
const opts = this._options = new pool_options_js_1.PoolOptions(this); | ||
} | ||
const opts = (this._options = new pool_options_js_1.PoolOptions(this)); | ||
opts.on('change', (prop, val) => { | ||
@@ -97,4 +100,5 @@ if (prop === 'houseKeepInterval') | ||
return; | ||
if (this._state >= definitions_js_1.PoolState.CLOSING) | ||
if (this._state >= definitions_js_1.PoolState.CLOSING) { | ||
throw new Error('Closed pool can not be started again'); | ||
} | ||
this._state = definitions_js_1.PoolState.STARTED; | ||
@@ -111,10 +115,11 @@ this._setHouseKeep(this.options.houseKeepInterval); | ||
else { | ||
terminateWait = typeof arg0 === 'number' ? arg0 : | ||
(!!arg0 ? 0 : Infinity); | ||
terminateWait = typeof arg0 === 'number' ? arg0 : arg0 ? 0 : Infinity; | ||
callback = arg1; | ||
} | ||
if (!callback) | ||
if (!callback) { | ||
return putil_promisify_1.default.fromCallback(cb => this.close(terminateWait, cb)); | ||
if (this._state === definitions_js_1.PoolState.CLOSED || this._state === definitions_js_1.PoolState.IDLE) | ||
} | ||
if (this._state === definitions_js_1.PoolState.CLOSED || this._state === definitions_js_1.PoolState.IDLE) { | ||
return callback(); | ||
} | ||
if (this._state === definitions_js_1.PoolState.CLOSING) { | ||
@@ -156,6 +161,15 @@ this.once('close', callback); | ||
} | ||
acquire(arg0) { | ||
if (!arg0) | ||
acquire(arg0, arg1) { | ||
let callback; | ||
let factoryCreateOptions; | ||
if (typeof arg0 === 'function') { | ||
callback = arg0; | ||
} | ||
else if (typeof arg0 === 'object') { | ||
factoryCreateOptions = arg0; | ||
if (typeof arg1 === 'function') | ||
callback = arg1; | ||
} | ||
if (!callback) | ||
return putil_promisify_1.default.fromCallback(cb => this.acquire(cb)); | ||
const callback = arg0; | ||
try { | ||
@@ -167,5 +181,6 @@ this.start(); | ||
} | ||
if (this.options.maxQueue && this.pending >= this.options.maxQueue) | ||
if (this.options.maxQueue && this.pending >= this.options.maxQueue) { | ||
return callback(new Error('Pool queue is full')); | ||
this._requestQueue.push(new pool_request_js_1.PoolRequest(this, callback)); | ||
} | ||
this._requestQueue.push(new pool_request_js_1.PoolRequest(this, callback, factoryCreateOptions)); | ||
this._processNextRequest(); | ||
@@ -178,4 +193,5 @@ } | ||
const item = this._allResources.get(resource); | ||
if (item && item.state !== definitions_js_1.ResourceState.IDLE) | ||
if (item && item.state !== definitions_js_1.ResourceState.IDLE) { | ||
this._itemSetIdle(item, callback); | ||
} | ||
this._processNextRequest(); | ||
@@ -225,4 +241,5 @@ } | ||
if (this._state !== definitions_js_1.PoolState.STARTED || | ||
this._requestsProcessing >= this.options.max - this.acquired) | ||
this._requestsProcessing >= this.options.max - this.acquired) { | ||
return; | ||
} | ||
const request = this._requestQueue.shift(); | ||
@@ -302,3 +319,3 @@ if (!request) | ||
tries, | ||
maxRetries: this.options.acquireMaxRetries | ||
maxRetries: this.options.acquireMaxRetries, | ||
}); | ||
@@ -312,5 +329,6 @@ if (err instanceof abort_error_js_1.AbortError || tries >= maxRetries) { | ||
this._creating--; | ||
if (this._allResources.has(obj)) | ||
return callback && | ||
callback(new Error('Factory error. Resource already in pool')); | ||
if (this._allResources.has(obj)) { | ||
return (callback && | ||
callback(new Error('Factory error. Resource already in pool'))); | ||
} | ||
const item = new resource_item_js_1.ResourceItem(obj); | ||
@@ -325,6 +343,7 @@ this._itemSetIdle(item); | ||
try { | ||
const o = this._factory.create({ tries, maxRetries }); | ||
const o = this._factory.create({ tries, maxRetries }, request?.options); | ||
/* istanbul ignore next */ | ||
if (!o) | ||
if (!o) { | ||
return handleCallback(new abort_error_js_1.AbortError('Factory returned no resource')); | ||
} | ||
putil_promisify_1.default.await(o, handleCallback); | ||
@@ -342,4 +361,6 @@ } | ||
this._houseKeepTimer = undefined; | ||
if (ms > 0 && this.state === definitions_js_1.PoolState.STARTED || this.state === definitions_js_1.PoolState.CLOSING) | ||
if ((ms > 0 && this.state === definitions_js_1.PoolState.STARTED) || | ||
this.state === definitions_js_1.PoolState.CLOSING) { | ||
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms); | ||
} | ||
} | ||
@@ -353,6 +374,5 @@ _houseKeep() { | ||
this._idleResources.every((item) => { | ||
if (isClosing || | ||
item.idleTime + this.options.idleTimeoutMillis < now) { | ||
if (isClosing || item.idleTime + this.options.idleTimeoutMillis < now) { | ||
this._itemDestroy(item); | ||
return isClosing || !!((--n) && (--m)); | ||
return isClosing || !!(--n && --m); | ||
} | ||
@@ -403,2 +423,4 @@ return false; | ||
break; | ||
default: | ||
break; | ||
} | ||
@@ -405,0 +427,0 @@ } |
import { Pool } from './pool.js'; | ||
export * from './abort-error.js'; | ||
export * from './definitions.js'; | ||
export * from './pool.js'; | ||
export * from './abort-error.js'; | ||
export function createPool(factory, config) { | ||
return new Pool(factory, config); | ||
} |
@@ -13,3 +13,3 @@ import { EventEmitter } from 'events'; | ||
maxQueue: 1000, | ||
validation: true | ||
validation: true, | ||
}; | ||
@@ -51,3 +51,4 @@ export class PoolOptions extends EventEmitter { | ||
set acquireTimeoutMillis(val) { | ||
this._acquireTimeoutMillis = val >= 0 ? val : defaultValues.acquireTimeoutMillis; | ||
this._acquireTimeoutMillis = | ||
val >= 0 ? val : defaultValues.acquireTimeoutMillis; | ||
this.emit('change', 'acquireTimeoutMillis', this._acquireTimeoutMillis); | ||
@@ -54,0 +55,0 @@ } |
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
function noop() { | ||
} | ||
function noop() { } | ||
export class PoolRequest { | ||
constructor(pool, callback) { | ||
constructor(pool, callback, options) { | ||
this.timedOut = false; | ||
this.created = Date.now(); | ||
this.callback = callback || noop; | ||
this.options = options; | ||
if (pool.options.acquireTimeoutMillis) { | ||
@@ -10,0 +10,0 @@ this.timeoutHandle = setTimeout(() => { |
@@ -5,3 +5,3 @@ import DoublyLinked from 'doublylinked'; | ||
import { AbortError } from './abort-error.js'; | ||
import { PoolState, ResourceState } from './definitions.js'; | ||
import { PoolState, ResourceState, } from './definitions.js'; | ||
import { PoolOptions } from './pool-options.js'; | ||
@@ -20,13 +20,18 @@ import { PoolRequest } from './pool-request.js'; | ||
this._state = PoolState.IDLE; | ||
if (typeof factory !== 'object') | ||
if (typeof factory !== 'object') { | ||
throw new TypeError('You must provide `factory` object'); | ||
if (typeof factory.create !== 'function') | ||
} | ||
if (typeof factory.create !== 'function') { | ||
throw new TypeError('factory.create must be a function'); | ||
if (typeof factory.destroy !== 'function') | ||
} | ||
if (typeof factory.destroy !== 'function') { | ||
throw new TypeError('factory.destroy must be a function'); | ||
if (factory.validate && typeof factory.validate !== 'function') | ||
} | ||
if (factory.validate && typeof factory.validate !== 'function') { | ||
throw new TypeError('factory.validate can be a function'); | ||
if (factory.reset && typeof factory.reset !== 'function') | ||
} | ||
if (factory.reset && typeof factory.reset !== 'function') { | ||
throw new TypeError('factory.reset can be a function'); | ||
const opts = this._options = new PoolOptions(this); | ||
} | ||
const opts = (this._options = new PoolOptions(this)); | ||
opts.on('change', (prop, val) => { | ||
@@ -92,4 +97,5 @@ if (prop === 'houseKeepInterval') | ||
return; | ||
if (this._state >= PoolState.CLOSING) | ||
if (this._state >= PoolState.CLOSING) { | ||
throw new Error('Closed pool can not be started again'); | ||
} | ||
this._state = PoolState.STARTED; | ||
@@ -106,10 +112,11 @@ this._setHouseKeep(this.options.houseKeepInterval); | ||
else { | ||
terminateWait = typeof arg0 === 'number' ? arg0 : | ||
(!!arg0 ? 0 : Infinity); | ||
terminateWait = typeof arg0 === 'number' ? arg0 : arg0 ? 0 : Infinity; | ||
callback = arg1; | ||
} | ||
if (!callback) | ||
if (!callback) { | ||
return promisify.fromCallback(cb => this.close(terminateWait, cb)); | ||
if (this._state === PoolState.CLOSED || this._state === PoolState.IDLE) | ||
} | ||
if (this._state === PoolState.CLOSED || this._state === PoolState.IDLE) { | ||
return callback(); | ||
} | ||
if (this._state === PoolState.CLOSING) { | ||
@@ -151,6 +158,15 @@ this.once('close', callback); | ||
} | ||
acquire(arg0) { | ||
if (!arg0) | ||
acquire(arg0, arg1) { | ||
let callback; | ||
let factoryCreateOptions; | ||
if (typeof arg0 === 'function') { | ||
callback = arg0; | ||
} | ||
else if (typeof arg0 === 'object') { | ||
factoryCreateOptions = arg0; | ||
if (typeof arg1 === 'function') | ||
callback = arg1; | ||
} | ||
if (!callback) | ||
return promisify.fromCallback(cb => this.acquire(cb)); | ||
const callback = arg0; | ||
try { | ||
@@ -162,5 +178,6 @@ this.start(); | ||
} | ||
if (this.options.maxQueue && this.pending >= this.options.maxQueue) | ||
if (this.options.maxQueue && this.pending >= this.options.maxQueue) { | ||
return callback(new Error('Pool queue is full')); | ||
this._requestQueue.push(new PoolRequest(this, callback)); | ||
} | ||
this._requestQueue.push(new PoolRequest(this, callback, factoryCreateOptions)); | ||
this._processNextRequest(); | ||
@@ -173,4 +190,5 @@ } | ||
const item = this._allResources.get(resource); | ||
if (item && item.state !== ResourceState.IDLE) | ||
if (item && item.state !== ResourceState.IDLE) { | ||
this._itemSetIdle(item, callback); | ||
} | ||
this._processNextRequest(); | ||
@@ -220,4 +238,5 @@ } | ||
if (this._state !== PoolState.STARTED || | ||
this._requestsProcessing >= this.options.max - this.acquired) | ||
this._requestsProcessing >= this.options.max - this.acquired) { | ||
return; | ||
} | ||
const request = this._requestQueue.shift(); | ||
@@ -297,3 +316,3 @@ if (!request) | ||
tries, | ||
maxRetries: this.options.acquireMaxRetries | ||
maxRetries: this.options.acquireMaxRetries, | ||
}); | ||
@@ -307,5 +326,6 @@ if (err instanceof AbortError || tries >= maxRetries) { | ||
this._creating--; | ||
if (this._allResources.has(obj)) | ||
return callback && | ||
callback(new Error('Factory error. Resource already in pool')); | ||
if (this._allResources.has(obj)) { | ||
return (callback && | ||
callback(new Error('Factory error. Resource already in pool'))); | ||
} | ||
const item = new ResourceItem(obj); | ||
@@ -320,6 +340,7 @@ this._itemSetIdle(item); | ||
try { | ||
const o = this._factory.create({ tries, maxRetries }); | ||
const o = this._factory.create({ tries, maxRetries }, request?.options); | ||
/* istanbul ignore next */ | ||
if (!o) | ||
if (!o) { | ||
return handleCallback(new AbortError('Factory returned no resource')); | ||
} | ||
promisify.await(o, handleCallback); | ||
@@ -337,4 +358,6 @@ } | ||
this._houseKeepTimer = undefined; | ||
if (ms > 0 && this.state === PoolState.STARTED || this.state === PoolState.CLOSING) | ||
if ((ms > 0 && this.state === PoolState.STARTED) || | ||
this.state === PoolState.CLOSING) { | ||
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms); | ||
} | ||
} | ||
@@ -348,6 +371,5 @@ _houseKeep() { | ||
this._idleResources.every((item) => { | ||
if (isClosing || | ||
item.idleTime + this.options.idleTimeoutMillis < now) { | ||
if (isClosing || item.idleTime + this.options.idleTimeoutMillis < now) { | ||
this._itemDestroy(item); | ||
return isClosing || !!((--n) && (--m)); | ||
return isClosing || !!(--n && --m); | ||
} | ||
@@ -398,2 +420,4 @@ return false; | ||
break; | ||
default: | ||
break; | ||
} | ||
@@ -400,0 +424,0 @@ } |
{ | ||
"name": "lightning-pool", | ||
"description": "Fastest generic Pool written with TypeScript", | ||
"version": "4.2.2", | ||
"version": "4.3.0", | ||
"author": "Panates Ltd.", | ||
@@ -17,39 +17,10 @@ "contributors": [ | ||
"dependencies": { | ||
"doublylinked": "^2.5.3", | ||
"doublylinked": "^2.5.4", | ||
"putil-promisify": "^1.10.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/eslint-parser": "^7.22.15", | ||
"@types/jest": "^29.5.4", | ||
"@types/node": "^20.6.0", | ||
"@typescript-eslint/eslint-plugin": "^6.6.0", | ||
"@typescript-eslint/parser": "^6.6.0", | ||
"dotenv": "^16.3.1", | ||
"eslint": "^8.49.0", | ||
"eslint-config-google": "^0.14.0", | ||
"eslint-plugin-import": "^2.28.1", | ||
"eslint-plugin-security": "^1.7.1", | ||
"eslint-plugin-simple-import-sort": "^10.0.0", | ||
"eslint-plugin-unused-imports": "^3.0.0", | ||
"jest": "^29.6.4", | ||
"npm-run-path": "^5.1.0", | ||
"ts-cleanup": "^0.2.6", | ||
"ts-jest": "^29.1.1", | ||
"ts-loader": "^9.4.4", | ||
"ts-node": "^10.9.1", | ||
"tsconfig-paths": "^4.2.0", | ||
"typescript": "^5.2.2" | ||
}, | ||
"type": "module", | ||
"types": "esm/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./cjs/index.js", | ||
"default": "./esm/index.js" | ||
}, | ||
"./cjs": "./cjs/index.js", | ||
"./esm": "./esm/index.js" | ||
}, | ||
"main": "./cjs/index.js", | ||
"module": "./esm/index.js", | ||
"types": "./esm/index.d.ts", | ||
"files": [ | ||
"bin/", | ||
"cjs/", | ||
@@ -59,19 +30,3 @@ "esm/", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"lint": "eslint .", | ||
"clean": "npm run clean:src && npm run clean:dist", | ||
"clean:dist": "rimraf cjs esm coverage", | ||
"clean:src": "ts-cleanup -s src --all | ts-cleanup -s test", | ||
"prebuild": "npm run clean:dist && npm run lint", | ||
"build": "npm run build:cjs && npm run build:esm", | ||
"build:cjs": "tsc -b tsconfig-build-cjs.json", | ||
"build:esm": "tsc -b tsconfig-build-esm.json", | ||
"postbuild": "cp package.cjs.json ./cjs/package.json", | ||
"test": "jest --runInBand --detectOpenHandles", | ||
"precover": "rimraf coverage", | ||
"cover": "jest --maxWorkers=1 --coverage", | ||
"precitest": "rimraf coverage", | ||
"citest": "jest --coverage --coverageReporters=lcov" | ||
} | ||
} | ||
] | ||
} |
@@ -124,4 +124,10 @@ # lightning-pool | ||
`pool.acquire()` | ||
`pool.acquire(): Promise<any>` | ||
`pool.acquire(factoryCreateOptions: any): Promise<any>` | ||
`pool.acquire(callback:Callback): Promise<any>` | ||
`pool.acquire(factoryCreateOptions?: any, callback:Callback): Promise<any>` | ||
- *Returns*: A Promise | ||
@@ -128,0 +134,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
392
60458
18
1344
Updateddoublylinked@^2.5.4