lightning-pool 4.10.1 → 4.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/cjs/index.js +2 -1
- package/cjs/pool.js +26 -27
- package/cjs/resource-item.js +2 -2
- package/cjs/tsconfig-build-cjs.tsbuildinfo +1 -1
- package/cjs/types.js +2 -0
- package/esm/index.js +2 -1
- package/esm/pool.js +4 -5
- package/esm/resource-item.js +1 -1
- package/esm/tsconfig-build-esm.tsbuildinfo +1 -1
- package/esm/types.js +1 -0
- package/package.json +2 -2
- package/types/constants.d.ts +11 -0
- package/types/index.d.cts +3 -2
- package/types/index.d.ts +3 -2
- package/types/pool-options.d.ts +2 -2
- package/types/pool-request.d.ts +2 -2
- package/types/pool.d.ts +2 -1
- package/types/resource-item.d.ts +2 -2
- package/types/{definitions.d.ts → types.d.ts} +1 -12
- /package/cjs/{definitions.js → constants.js} +0 -0
- /package/esm/{definitions.js → constants.js} +0 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[![NPM Version][npm-image]][npm-url]
|
|
4
4
|
[![NPM Downloads][downloads-image]][downloads-url]
|
|
5
|
-
[![
|
|
5
|
+
[![CI Tests][ci-test-image]][ci-test-url]
|
|
6
6
|
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
7
7
|
|
|
8
8
|
## About
|
|
@@ -377,8 +377,8 @@ Pool.PoolState (Number):
|
|
|
377
377
|
|
|
378
378
|
[npm-image]: https://img.shields.io/npm/v/lightning-pool.svg
|
|
379
379
|
[npm-url]: https://npmjs.org/package/lightning-pool
|
|
380
|
-
[
|
|
381
|
-
[
|
|
380
|
+
[ci-test-image]: https://github.com/panates/lightning-pool/actions/workflows/test.yml/badge.svg
|
|
381
|
+
[ci-test-url]: https://github.com/panates/lightning-pool/actions/workflows/test.yml
|
|
382
382
|
[coveralls-image]: https://img.shields.io/coveralls/panates/lightning-pool/master.svg
|
|
383
383
|
[coveralls-url]: https://coveralls.io/r/panates/lightning-pool
|
|
384
384
|
[downloads-image]: https://img.shields.io/npm/dm/lightning-pool.svg
|
package/cjs/index.js
CHANGED
|
@@ -4,8 +4,9 @@ exports.createPool = createPool;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const pool_js_1 = require("./pool.js");
|
|
6
6
|
tslib_1.__exportStar(require("./abort-error.js"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./
|
|
7
|
+
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./pool.js"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./types.js"), exports);
|
|
9
10
|
function createPool(factory, config) {
|
|
10
11
|
return new pool_js_1.Pool(factory, config);
|
|
11
12
|
}
|
package/cjs/pool.js
CHANGED
|
@@ -6,7 +6,7 @@ const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
|
|
|
6
6
|
const events_1 = require("events");
|
|
7
7
|
const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify"));
|
|
8
8
|
const abort_error_js_1 = require("./abort-error.js");
|
|
9
|
-
const
|
|
9
|
+
const constants_js_1 = require("./constants.js");
|
|
10
10
|
const pool_options_js_1 = require("./pool-options.js");
|
|
11
11
|
const pool_request_js_1 = require("./pool-request.js");
|
|
12
12
|
const resource_item_js_1 = require("./resource-item.js");
|
|
@@ -19,7 +19,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
19
19
|
this._idleResources = new doublylinked_1.default();
|
|
20
20
|
this._creating = 0;
|
|
21
21
|
this._requestsProcessing = 0;
|
|
22
|
-
this._state =
|
|
22
|
+
this._state = constants_js_1.PoolState.IDLE;
|
|
23
23
|
if (typeof factory !== 'object') {
|
|
24
24
|
throw new TypeError('You must provide `factory` object');
|
|
25
25
|
}
|
|
@@ -94,12 +94,12 @@ class Pool extends events_1.EventEmitter {
|
|
|
94
94
|
* Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called
|
|
95
95
|
*/
|
|
96
96
|
start() {
|
|
97
|
-
if (this._state ===
|
|
97
|
+
if (this._state === constants_js_1.PoolState.STARTED)
|
|
98
98
|
return;
|
|
99
|
-
if (this._state >=
|
|
99
|
+
if (this._state >= constants_js_1.PoolState.CLOSING) {
|
|
100
100
|
throw new Error('Closed pool can not be started again');
|
|
101
101
|
}
|
|
102
|
-
this._state =
|
|
102
|
+
this._state = constants_js_1.PoolState.STARTED;
|
|
103
103
|
this._setHouseKeep(this.options.houseKeepInterval);
|
|
104
104
|
this._ensureMin();
|
|
105
105
|
this.emit('start');
|
|
@@ -116,17 +116,17 @@ class Pool extends events_1.EventEmitter {
|
|
|
116
116
|
if (!callback) {
|
|
117
117
|
return putil_promisify_1.default.fromCallback(cb => this.close(terminateWait, cb));
|
|
118
118
|
}
|
|
119
|
-
if (this._state ===
|
|
119
|
+
if (this._state === constants_js_1.PoolState.CLOSED || this._state === constants_js_1.PoolState.IDLE) {
|
|
120
120
|
return callback();
|
|
121
121
|
}
|
|
122
|
-
if (this._state ===
|
|
122
|
+
if (this._state === constants_js_1.PoolState.CLOSING) {
|
|
123
123
|
this.once('close', callback);
|
|
124
124
|
return;
|
|
125
125
|
}
|
|
126
126
|
this.emit('closing');
|
|
127
127
|
if (this._houseKeepTimer)
|
|
128
128
|
clearTimeout(this._houseKeepTimer);
|
|
129
|
-
this._state =
|
|
129
|
+
this._state = constants_js_1.PoolState.CLOSING;
|
|
130
130
|
this._requestQueue.forEach(t => t.stopTimout());
|
|
131
131
|
this._requestQueue = new doublylinked_1.default();
|
|
132
132
|
this._requestsProcessing = 0;
|
|
@@ -175,7 +175,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
175
175
|
*/
|
|
176
176
|
release(resource, callback) {
|
|
177
177
|
const item = this._allResources.get(resource);
|
|
178
|
-
if (item && item.state !==
|
|
178
|
+
if (item && item.state !== constants_js_1.ResourceState.IDLE) {
|
|
179
179
|
this._itemSetIdle(item, callback);
|
|
180
180
|
}
|
|
181
181
|
this._processNextRequest();
|
|
@@ -221,7 +221,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
221
221
|
return this._allResources.has(resource);
|
|
222
222
|
}
|
|
223
223
|
_processNextRequest() {
|
|
224
|
-
if (this._state !==
|
|
224
|
+
if (this._state !== constants_js_1.PoolState.STARTED ||
|
|
225
225
|
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
226
226
|
return;
|
|
227
227
|
}
|
|
@@ -235,7 +235,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
235
235
|
try {
|
|
236
236
|
if (item) {
|
|
237
237
|
/* istanbul ignore next : Hard to simulate */
|
|
238
|
-
if (this._state !==
|
|
238
|
+
if (this._state !== constants_js_1.PoolState.STARTED) {
|
|
239
239
|
this._itemDestroy(item);
|
|
240
240
|
return;
|
|
241
241
|
}
|
|
@@ -256,9 +256,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
256
256
|
if (item) {
|
|
257
257
|
/* Validate resource */
|
|
258
258
|
if (this.options.validation && this._factory.validate) {
|
|
259
|
-
this._itemValidate(item, (err) => {
|
|
259
|
+
this._itemValidate(item, (err, result) => {
|
|
260
260
|
/* Destroy resource on validation error */
|
|
261
|
-
if (err) {
|
|
261
|
+
if (err || result === false) {
|
|
262
262
|
this._itemDestroy(item);
|
|
263
263
|
this.emit('validate-error', err, item.resource);
|
|
264
264
|
this._requestsProcessing--;
|
|
@@ -338,13 +338,13 @@ class Pool extends events_1.EventEmitter {
|
|
|
338
338
|
if (this._houseKeepTimer)
|
|
339
339
|
clearInterval(this._houseKeepTimer);
|
|
340
340
|
this._houseKeepTimer = undefined;
|
|
341
|
-
if ((ms > 0 && this.state ===
|
|
342
|
-
this.state ===
|
|
341
|
+
if ((ms > 0 && this.state === constants_js_1.PoolState.STARTED) ||
|
|
342
|
+
this.state === constants_js_1.PoolState.CLOSING) {
|
|
343
343
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
344
344
|
}
|
|
345
345
|
}
|
|
346
346
|
_houseKeep() {
|
|
347
|
-
const isClosing = this._state ===
|
|
347
|
+
const isClosing = this._state === constants_js_1.PoolState.CLOSING;
|
|
348
348
|
const now = Date.now();
|
|
349
349
|
let m = this._allResources.size - this.options.min;
|
|
350
350
|
let n = this._idleResources.length - this.options.minIdle;
|
|
@@ -362,7 +362,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
362
362
|
if (this._allResources.size)
|
|
363
363
|
return;
|
|
364
364
|
clearInterval(this._houseKeepTimer);
|
|
365
|
-
this._state =
|
|
365
|
+
this._state = constants_js_1.PoolState.CLOSED;
|
|
366
366
|
this._requestsProcessing = 0;
|
|
367
367
|
this.emit('close');
|
|
368
368
|
}
|
|
@@ -375,24 +375,24 @@ class Pool extends events_1.EventEmitter {
|
|
|
375
375
|
});
|
|
376
376
|
}
|
|
377
377
|
_itemSetAcquired(item) {
|
|
378
|
-
if (item.state !==
|
|
378
|
+
if (item.state !== constants_js_1.ResourceState.ACQUIRED) {
|
|
379
379
|
this._itemDetach(item);
|
|
380
|
-
item.state =
|
|
380
|
+
item.state = constants_js_1.ResourceState.ACQUIRED;
|
|
381
381
|
this._acquiredResources.push(item);
|
|
382
382
|
item.acquiredNode = this._acquiredResources.tail;
|
|
383
383
|
}
|
|
384
384
|
}
|
|
385
385
|
_itemDetach(item) {
|
|
386
386
|
switch (item.state) {
|
|
387
|
-
case
|
|
387
|
+
case constants_js_1.ResourceState.IDLE:
|
|
388
388
|
item.idleTime = 0;
|
|
389
389
|
/* istanbul ignore next*/
|
|
390
390
|
if (item.idleNode)
|
|
391
391
|
item.idleNode.remove();
|
|
392
392
|
item.idleNode = undefined;
|
|
393
393
|
break;
|
|
394
|
-
case
|
|
395
|
-
case
|
|
394
|
+
case constants_js_1.ResourceState.ACQUIRED:
|
|
395
|
+
case constants_js_1.ResourceState.VALIDATION:
|
|
396
396
|
/* istanbul ignore next*/
|
|
397
397
|
if (item.acquiredNode)
|
|
398
398
|
item.acquiredNode.remove();
|
|
@@ -403,13 +403,13 @@ class Pool extends events_1.EventEmitter {
|
|
|
403
403
|
}
|
|
404
404
|
}
|
|
405
405
|
_itemSetIdle(item, callback) {
|
|
406
|
-
const isAcquired = item.state ===
|
|
406
|
+
const isAcquired = item.state === constants_js_1.ResourceState.ACQUIRED;
|
|
407
407
|
const handleCallback = (err) => {
|
|
408
408
|
if (err)
|
|
409
409
|
return this._itemDestroy(item, callback);
|
|
410
410
|
this._itemDetach(item);
|
|
411
411
|
item.idleTime = Date.now();
|
|
412
|
-
item.state =
|
|
412
|
+
item.state = constants_js_1.ResourceState.IDLE;
|
|
413
413
|
if (this.options.fifo) {
|
|
414
414
|
this._idleResources.push(item);
|
|
415
415
|
item.idleNode = this._idleResources.tail;
|
|
@@ -461,15 +461,14 @@ class Pool extends events_1.EventEmitter {
|
|
|
461
461
|
}
|
|
462
462
|
}
|
|
463
463
|
_itemValidate(item, callback) {
|
|
464
|
-
item.state =
|
|
464
|
+
item.state = constants_js_1.ResourceState.VALIDATION;
|
|
465
465
|
try {
|
|
466
466
|
const o = this._factory.validate?.(item.resource);
|
|
467
467
|
// @ts-ignore
|
|
468
468
|
putil_promisify_1.default.await(o, callback);
|
|
469
469
|
}
|
|
470
470
|
catch (e) {
|
|
471
|
-
|
|
472
|
-
callback(e);
|
|
471
|
+
callback?.(e);
|
|
473
472
|
}
|
|
474
473
|
}
|
|
475
474
|
}
|
package/cjs/resource-item.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ResourceItem = void 0;
|
|
4
|
-
const
|
|
4
|
+
const constants_js_1 = require("./constants.js");
|
|
5
5
|
class ResourceItem {
|
|
6
6
|
constructor(resource) {
|
|
7
|
-
this.state =
|
|
7
|
+
this.state = constants_js_1.ResourceState.IDLE;
|
|
8
8
|
this.idleTime = 0;
|
|
9
9
|
this.destroyed = false;
|
|
10
10
|
this.resource = resource;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../../src/abort-error.ts","../../src/
|
|
1
|
+
{"root":["../../src/abort-error.ts","../../src/constants.ts","../../src/index.ts","../../src/pool-options.ts","../../src/pool-request.ts","../../src/pool.ts","../../src/resource-item.ts","../../src/types.ts"],"version":"5.8.3"}
|
package/cjs/types.js
ADDED
package/esm/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Pool } from './pool.js';
|
|
2
2
|
export * from './abort-error.js';
|
|
3
|
-
export * from './
|
|
3
|
+
export * from './constants.js';
|
|
4
4
|
export * from './pool.js';
|
|
5
|
+
export * from './types.js';
|
|
5
6
|
export function createPool(factory, config) {
|
|
6
7
|
return new Pool(factory, config);
|
|
7
8
|
}
|
package/esm/pool.js
CHANGED
|
@@ -2,7 +2,7 @@ import DoublyLinked from 'doublylinked';
|
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
3
|
import promisify from 'putil-promisify';
|
|
4
4
|
import { AbortError } from './abort-error.js';
|
|
5
|
-
import { PoolState, ResourceState
|
|
5
|
+
import { PoolState, ResourceState } from './constants.js';
|
|
6
6
|
import { PoolOptions } from './pool-options.js';
|
|
7
7
|
import { PoolRequest } from './pool-request.js';
|
|
8
8
|
import { ResourceItem } from './resource-item.js';
|
|
@@ -252,9 +252,9 @@ export class Pool extends EventEmitter {
|
|
|
252
252
|
if (item) {
|
|
253
253
|
/* Validate resource */
|
|
254
254
|
if (this.options.validation && this._factory.validate) {
|
|
255
|
-
this._itemValidate(item, (err) => {
|
|
255
|
+
this._itemValidate(item, (err, result) => {
|
|
256
256
|
/* Destroy resource on validation error */
|
|
257
|
-
if (err) {
|
|
257
|
+
if (err || result === false) {
|
|
258
258
|
this._itemDestroy(item);
|
|
259
259
|
this.emit('validate-error', err, item.resource);
|
|
260
260
|
this._requestsProcessing--;
|
|
@@ -464,8 +464,7 @@ export class Pool extends EventEmitter {
|
|
|
464
464
|
promisify.await(o, callback);
|
|
465
465
|
}
|
|
466
466
|
catch (e) {
|
|
467
|
-
|
|
468
|
-
callback(e);
|
|
467
|
+
callback?.(e);
|
|
469
468
|
}
|
|
470
469
|
}
|
|
471
470
|
}
|
package/esm/resource-item.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../../src/abort-error.ts","../../src/
|
|
1
|
+
{"root":["../../src/abort-error.ts","../../src/constants.ts","../../src/index.ts","../../src/pool-options.ts","../../src/pool-request.ts","../../src/pool.ts","../../src/resource-item.ts","../../src/types.ts"],"version":"5.8.3"}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lightning-pool",
|
|
3
3
|
"description": "Fastest generic Pool written with TypeScript",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.11.1",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"types": "./types/index.d.ts",
|
|
30
30
|
"repository": {
|
|
31
31
|
"type": "git",
|
|
32
|
-
"url": "https://github.com/panates/lightning-pool.git"
|
|
32
|
+
"url": "git+https://github.com/panates/lightning-pool.git"
|
|
33
33
|
},
|
|
34
34
|
"contributors": [
|
|
35
35
|
"Eray Hanoglu <e.hanoglu@panates.com>"
|
package/types/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { PoolConfiguration, PoolFactory } from './definitions.js';
|
|
2
1
|
import { Pool } from './pool.js';
|
|
2
|
+
import { PoolConfiguration, PoolFactory } from './types.js';
|
|
3
3
|
export * from './abort-error.js';
|
|
4
|
-
export * from './
|
|
4
|
+
export * from './constants.js';
|
|
5
5
|
export * from './pool.js';
|
|
6
|
+
export * from './types.js';
|
|
6
7
|
export declare function createPool<T = any>(factory: PoolFactory<T>, config?: PoolConfiguration): Pool<T>;
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { PoolConfiguration, PoolFactory } from './definitions.js';
|
|
2
1
|
import { Pool } from './pool.js';
|
|
2
|
+
import { PoolConfiguration, PoolFactory } from './types.js';
|
|
3
3
|
export * from './abort-error.js';
|
|
4
|
-
export * from './
|
|
4
|
+
export * from './constants.js';
|
|
5
5
|
export * from './pool.js';
|
|
6
|
+
export * from './types.js';
|
|
6
7
|
export declare function createPool<T = any>(factory: PoolFactory<T>, config?: PoolConfiguration): Pool<T>;
|
package/types/pool-options.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import type { Pool } from './pool.js';
|
|
3
|
+
import type { PoolConfiguration } from './types.js';
|
|
4
4
|
export declare class PoolOptions extends EventEmitter {
|
|
5
5
|
readonly pool: Pool;
|
|
6
6
|
private _acquireMaxRetries;
|
package/types/pool-request.d.ts
CHANGED
package/types/pool.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
|
-
import {
|
|
2
|
+
import { PoolState } from './constants.js';
|
|
3
3
|
import { PoolOptions } from './pool-options.js';
|
|
4
|
+
import type { Callback, PoolConfiguration, PoolFactory } from './types.js';
|
|
4
5
|
export declare class Pool<T = any> extends EventEmitter {
|
|
5
6
|
private readonly _options;
|
|
6
7
|
private readonly _factory;
|
package/types/resource-item.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DoublyLinked } from 'doublylinked';
|
|
2
|
-
import { ResourceState } from './
|
|
1
|
+
import type { DoublyLinked } from 'doublylinked';
|
|
2
|
+
import { ResourceState } from './constants.js';
|
|
3
3
|
export declare class ResourceItem<T> {
|
|
4
4
|
state: ResourceState;
|
|
5
5
|
acquiredNode?: DoublyLinked.Node<ResourceItem<T>>;
|
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
export type Callback = (e?: unknown, ...args: any[]) => void;
|
|
2
|
-
export declare enum PoolState {
|
|
3
|
-
IDLE = 0,
|
|
4
|
-
STARTED = 1,
|
|
5
|
-
CLOSING = 2,
|
|
6
|
-
CLOSED = 3
|
|
7
|
-
}
|
|
8
|
-
export declare enum ResourceState {
|
|
9
|
-
IDLE = 0,
|
|
10
|
-
ACQUIRED = 1,
|
|
11
|
-
VALIDATION = 2
|
|
12
|
-
}
|
|
13
2
|
export interface PoolFactory<T = any> {
|
|
14
3
|
create(info?: {
|
|
15
4
|
tries: number;
|
|
@@ -17,7 +6,7 @@ export interface PoolFactory<T = any> {
|
|
|
17
6
|
}): Promise<T> | T;
|
|
18
7
|
destroy(resource: T): Promise<void> | void;
|
|
19
8
|
reset?(resource: T): Promise<void> | void;
|
|
20
|
-
validate?(resource: T): Promise<void> | void;
|
|
9
|
+
validate?(resource: T): Promise<void | boolean> | void;
|
|
21
10
|
}
|
|
22
11
|
export interface PoolConfiguration {
|
|
23
12
|
acquireMaxRetries?: number;
|
|
File without changes
|
|
File without changes
|