node-firebird 2.3.3 → 2.4.0

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.
Files changed (109) hide show
  1. package/README.md +544 -6
  2. package/lib/callback.d.ts +20 -0
  3. package/lib/callback.js +6 -16
  4. package/lib/firebird.msg.json +1370 -1370
  5. package/lib/gdscodes.d.ts +1519 -1520
  6. package/lib/gdscodes.js +5 -5
  7. package/lib/ieee754-decimal.d.ts +38 -0
  8. package/lib/ieee754-decimal.js +71 -118
  9. package/lib/index.d.ts +37 -338
  10. package/lib/index.js +97 -86
  11. package/lib/messages.d.ts +6 -0
  12. package/lib/messages.js +135 -162
  13. package/lib/pool.d.ts +23 -0
  14. package/lib/pool.js +34 -47
  15. package/lib/srp.d.ts +51 -0
  16. package/lib/srp.js +62 -72
  17. package/lib/types.d.ts +375 -0
  18. package/lib/types.js +22 -0
  19. package/lib/unix-crypt.d.ts +1 -0
  20. package/lib/unix-crypt.js +126 -152
  21. package/lib/utils.d.ts +16 -0
  22. package/lib/utils.js +24 -48
  23. package/lib/wire/connection.d.ts +100 -0
  24. package/lib/wire/connection.js +1029 -1009
  25. package/lib/wire/const.d.ts +567 -0
  26. package/lib/wire/const.js +620 -599
  27. package/lib/wire/database.d.ts +57 -0
  28. package/lib/wire/database.js +111 -108
  29. package/lib/wire/eventConnection.d.ts +16 -0
  30. package/lib/wire/eventConnection.js +29 -31
  31. package/lib/wire/fbEventManager.d.ts +45 -0
  32. package/lib/wire/fbEventManager.js +38 -57
  33. package/lib/wire/serialize.d.ts +97 -0
  34. package/lib/wire/serialize.js +42 -141
  35. package/lib/wire/service.d.ts +66 -0
  36. package/lib/wire/service.js +374 -401
  37. package/lib/wire/socket.d.ts +59 -0
  38. package/lib/wire/socket.js +79 -40
  39. package/lib/wire/statement.d.ts +25 -0
  40. package/lib/wire/statement.js +13 -9
  41. package/lib/wire/transaction.d.ts +21 -0
  42. package/lib/wire/transaction.js +33 -61
  43. package/lib/wire/xsqlvar.d.ts +175 -0
  44. package/lib/wire/xsqlvar.js +169 -269
  45. package/package.json +12 -4
  46. package/src/callback.ts +55 -0
  47. package/{poc/node_modules/node-firebird/lib/gdscodes.js → src/gdscodes.ts} +3 -3
  48. package/{poc/node_modules/node-firebird/lib/ieee754-decimal.js → src/ieee754-decimal.ts} +7 -14
  49. package/src/index.ts +157 -0
  50. package/{poc/node_modules/node-firebird/lib/messages.js → src/messages.ts} +163 -162
  51. package/src/pool.ts +209 -0
  52. package/{poc/node_modules/node-firebird/lib/srp.js → src/srp.ts} +74 -33
  53. package/src/types.ts +419 -0
  54. package/{poc/node_modules/node-firebird/lib/unix-crypt.js → src/unix-crypt.ts} +6 -12
  55. package/{poc/node_modules/node-firebird/lib/utils.js → src/utils.ts} +11 -20
  56. package/{poc/node_modules/node-firebird/lib/wire/connection.js → src/wire/connection.ts} +611 -129
  57. package/{poc/node_modules/node-firebird/lib/wire/const.js → src/wire/const.ts} +96 -9
  58. package/{poc/node_modules/node-firebird/lib/wire/database.js → src/wire/database.ts} +110 -44
  59. package/{poc/node_modules/node-firebird/lib/wire/eventConnection.js → src/wire/eventConnection.ts} +29 -14
  60. package/{poc/node_modules/node-firebird/lib/wire/fbEventManager.js → src/wire/fbEventManager.ts} +39 -30
  61. package/{poc/node_modules/node-firebird/lib/wire/serialize.js → src/wire/serialize.ts} +78 -59
  62. package/{poc/node_modules/node-firebird/lib/wire/service.js → src/wire/service.ts} +90 -88
  63. package/src/wire/socket.ts +262 -0
  64. package/src/wire/statement.ts +71 -0
  65. package/{poc/node_modules/node-firebird/lib/wire/transaction.js → src/wire/transaction.ts} +25 -19
  66. package/{poc/node_modules/node-firebird/lib/wire/xsqlvar.js → src/wire/xsqlvar.ts} +244 -143
  67. package/.eslintrc.json +0 -12
  68. package/.github/workflows/codeql.yml +0 -76
  69. package/.github/workflows/node.js.yml +0 -95
  70. package/BIGINT_MIGRATION.md +0 -374
  71. package/CI_DEBUGGING_GUIDE.md +0 -148
  72. package/ENCRYPTION_CALLBACK.md +0 -152
  73. package/FIREBIRD_LOG_FEATURE.md +0 -145
  74. package/MINIMAL_CHANGES_SUMMARY.md +0 -136
  75. package/PR_SUMMARY.md +0 -96
  76. package/ROADMAP.md +0 -223
  77. package/SRP_PROTOCOL.md +0 -482
  78. package/poc/README.md +0 -160
  79. package/poc/helpers.js +0 -59
  80. package/poc/node_modules/.package-lock.json +0 -14
  81. package/poc/node_modules/node-firebird/.eslintrc.json +0 -12
  82. package/poc/node_modules/node-firebird/.github/workflows/codeql.yml +0 -76
  83. package/poc/node_modules/node-firebird/.github/workflows/node.js.yml +0 -95
  84. package/poc/node_modules/node-firebird/BIGINT_MIGRATION.md +0 -374
  85. package/poc/node_modules/node-firebird/CI_DEBUGGING_GUIDE.md +0 -148
  86. package/poc/node_modules/node-firebird/ENCRYPTION_CALLBACK.md +0 -152
  87. package/poc/node_modules/node-firebird/FIREBIRD_LOG_FEATURE.md +0 -145
  88. package/poc/node_modules/node-firebird/LICENSE +0 -373
  89. package/poc/node_modules/node-firebird/MINIMAL_CHANGES_SUMMARY.md +0 -136
  90. package/poc/node_modules/node-firebird/PR_SUMMARY.md +0 -96
  91. package/poc/node_modules/node-firebird/README.md +0 -794
  92. package/poc/node_modules/node-firebird/ROADMAP.md +0 -223
  93. package/poc/node_modules/node-firebird/SRP_PROTOCOL.md +0 -482
  94. package/poc/node_modules/node-firebird/lib/callback.js +0 -38
  95. package/poc/node_modules/node-firebird/lib/gdscodes.d.ts +0 -1524
  96. package/poc/node_modules/node-firebird/lib/index.d.ts +0 -316
  97. package/poc/node_modules/node-firebird/lib/index.js +0 -128
  98. package/poc/node_modules/node-firebird/lib/pool.js +0 -108
  99. package/poc/node_modules/node-firebird/lib/wire/socket.js +0 -175
  100. package/poc/node_modules/node-firebird/lib/wire/statement.js +0 -48
  101. package/poc/node_modules/node-firebird/package.json +0 -38
  102. package/poc/node_modules/node-firebird/vitest.config.js +0 -24
  103. package/poc/package-lock.json +0 -21
  104. package/poc/package.json +0 -12
  105. package/poc/reproduce-fixed.js +0 -150
  106. package/poc/reproduce.js +0 -133
  107. package/vitest.config.js +0 -27
  108. /package/{poc/node_modules/node-firebird/lib → src}/firebird.msg +0 -0
  109. /package/{poc/node_modules/node-firebird/lib → src}/firebird.msg.json +0 -0
@@ -1,316 +0,0 @@
1
- // Type definitions for node-firebird
2
- // Project: node-firebird
3
- // Definitions by: Marco Warm <https://github.com/MarcusCalidus>
4
-
5
- declare module 'node-firebird' {
6
- type DatabaseCallback = (err: any, db: Database) => void;
7
- type TransactionCallback = (err: any, transaction: Transaction) => void;
8
- type QueryCallback = (err: any, result: any[]) => void;
9
- type SimpleCallback = (err: any) => void;
10
- type SequentialCallback = (row: any, index: number, next?: (err?: any) => void) => void | Promise<void>;
11
-
12
- export const AUTH_PLUGIN_LEGACY: string;
13
- export const AUTH_PLUGIN_SRP: string;
14
- export const AUTH_PLUGIN_SRP256: string;
15
-
16
- export const WIRE_CRYPT_ENABLE: number;
17
- export const WIRE_CRYPT_DISABLE: number;
18
-
19
- /** A transaction sees changes done by uncommitted transactions. */
20
- export const ISOLATION_READ_UNCOMMITTED: number[];
21
- /** A transaction sees only data committed before the statement has been executed. */
22
- export const ISOLATION_READ_COMMITTED: number[];
23
- /** A transaction sees during its lifetime only data committed before the transaction has been started. */
24
- export const ISOLATION_REPEATABLE_READ: number[];
25
- /**
26
- * This is the strictest isolation level, which enforces transaction serialization.
27
- * Data accessed in the context of a serializable transaction cannot be accessed by any other transaction.
28
- */
29
- export const ISOLATION_SERIALIZABLE: number[];
30
- export const ISOLATION_READ_COMMITTED_READ_ONLY: number[];
31
-
32
- export type Isolation = number[];
33
-
34
- export type TransactionOptions = {
35
- autoCommit?: boolean;
36
- autoUndo?: boolean;
37
- isolation?: Isolation;
38
- ignoreLimbo?: boolean;
39
- readOnly?: boolean;
40
- wait?: boolean;
41
- waitTimeout?: number;
42
- };
43
-
44
- export type QueryOptions = {
45
- timeout: number;
46
- }
47
-
48
- export interface Database {
49
- detach(callback?: SimpleCallback): Database;
50
- transaction(options: TransactionOptions|Isolation|TransactionCallback, callback?: TransactionCallback): Database;
51
- query(query: string, params: any[], callback: QueryCallback, options?: QueryOptions): Database;
52
- execute(query: string, params: any[], callback: QueryCallback, options?: QueryOptions): Database;
53
- sequentially(query: string, params: any[], rowCallback: SequentialCallback, callback: SimpleCallback, options?: QueryOptions | boolean): Database;
54
- drop(callback: SimpleCallback): void;
55
- escape(value: any): string;
56
- attachEvent(callback: any): this;
57
- }
58
-
59
- export interface Transaction {
60
- query(query: string, params: any[], callback: QueryCallback, options?: QueryOptions): void;
61
- execute(query: string, params: any[], callback: QueryCallback, options?: QueryOptions): void;
62
- sequentially(query: string, params: any[], rowCallback: SequentialCallback, callback: SimpleCallback, options?: QueryOptions | boolean): Database;
63
- commit(callback?: SimpleCallback): void;
64
- commitRetaining(callback?: SimpleCallback): void;
65
- rollback(callback?: SimpleCallback): void;
66
- rollbackRetaining(callback?: SimpleCallback): void;
67
- }
68
-
69
- export type SupportedCharacterSet = |
70
- 'NONE' |
71
- 'CP943C' |
72
- 'DOS737' |
73
- 'DOS775' |
74
- 'DOS858' |
75
- 'DOS862' |
76
- 'DOS864' |
77
- 'DOS866' |
78
- 'DOS869' |
79
- 'GB18030' |
80
- 'GBK' |
81
- 'ISO8859_1' |
82
- 'ISO8859_2' |
83
- 'ISO8859_3' |
84
- 'ISO8859_4' |
85
- 'ISO8859_5' |
86
- 'ISO8859_6' |
87
- 'ISO8859_7' |
88
- 'ISO8859_8' |
89
- 'ISO8859_9' |
90
- 'ISO8859_13' |
91
- 'KOI8R' |
92
- 'KOI8U' |
93
- 'TIS620' |
94
- 'UTF8' |
95
- 'WIN1251' |
96
- 'WIN1252' |
97
- 'WIN1253' |
98
- 'WIN1254' |
99
- 'WIN1255' |
100
- 'WIN1256' |
101
- 'WIN1257' |
102
- 'WIN1258' |
103
- 'WIN_1258';
104
-
105
- export interface Options {
106
- host?: string;
107
- port?: number;
108
- database?: string;
109
- user?: string;
110
- password?: string;
111
- lowercase_keys?: boolean;
112
- role?: string;
113
- pageSize?: number;
114
- retryConnectionInterval?: number;
115
- encoding?: SupportedCharacterSet;
116
- blobAsText?: boolean; // only affects for blob subtype 1
117
- wireCrypt?: number; // WIRE_CRYPT_DISABLE or WIRE_CRYPT_ENABLE
118
- wireCompression?: boolean;
119
- pluginName?: string;
120
- dbCryptConfig?: string; // Database encryption key callback config (base64: prefix for base64, or plain string)
121
- }
122
-
123
- export interface SvcMgrOptions extends Options {
124
- manager: true; // Attach to ServiceManager
125
- }
126
-
127
- export interface ConnectionPool {
128
- get(callback: DatabaseCallback): void;
129
- destroy(callback?: SimpleCallback): void;
130
- }
131
-
132
- export function attach(options: Options, callback: DatabaseCallback): void;
133
- export function attach(options: SvcMgrOptions, callback: ServiceManagerCallback): void;
134
- export function escape(value: any, protocolVersion?: number /*PROTOCOL_VERSION13*/): string;
135
- export function create(options: Options, callback: DatabaseCallback): void;
136
- export function attachOrCreate(options: Options, callback: DatabaseCallback): void;
137
- export function pool(max: number, options: Options): ConnectionPool;
138
- export function drop(options: Options, callback: SimpleCallback): void;
139
-
140
- interface ReadableOptions {
141
- optread?: 'byline' | 'buffer'; // default 'byline'
142
- buffersize?: number; // default 'byline': 2048, 'buffer': 8192
143
- timeout?: number;
144
- }
145
-
146
- export interface BackupOptions extends ReadableOptions {
147
- database?: string;
148
- files: string | { filename: string, sizefile: string }[];
149
- factor?: number; // If backing up to a physical tape device, this switch lets you specify the tape's blocking factor
150
- verbose?: boolean;
151
- ignorechecksums?: boolean;
152
- ignorelimbo?: boolean;
153
- metadataonly?: boolean;
154
- nogarbasecollect?: boolean;
155
- olddescriptions?: boolean;
156
- nontransportable?: boolean;
157
- convert?: boolean;
158
- expand?: boolean;
159
- notriggers?: boolean;
160
- }
161
-
162
- export interface NBackupOptions extends ReadableOptions {
163
- database?: string;
164
- file: string;
165
- level?: number; // nb day for incremental
166
- notriggers?: boolean;
167
- direct?: 'on' | 'off'; // default 'on'
168
- }
169
-
170
- export interface RestoreOptions extends ReadableOptions {
171
- database?: string;
172
- files: string | string[];
173
- verbose?: boolean;
174
- cachebuffers?: number; // default 2048, gbak -buffers
175
- pagesize?: boolean; // default 4096
176
- readonly?: boolean; // default false
177
- deactivateindexes?: boolean; // default false
178
- noshadow?: boolean; // default false
179
- novalidity?: boolean; // default false
180
- individualcommit?: boolean; // default true
181
- replace?: boolean; // default false
182
- create?: boolean; // default true
183
- useallspace?: boolean; // default false
184
- metadataonly?: boolean; // default false
185
- fixfssdata?: string; // default null
186
- fixfssmetadata?: string; // default null
187
- }
188
-
189
- export interface NRestoreOptions extends ReadableOptions {
190
- database?: string;
191
- files: string | string[];
192
- }
193
-
194
- export interface ValidateOptions extends ReadableOptions {
195
- database?: string;
196
- checkdb?: boolean;
197
- ignorechecksums?: boolean;
198
- killshadows?: boolean;
199
- mend?: boolean;
200
- validate?: boolean;
201
- full?: boolean;
202
- sweep?: boolean;
203
- listlimbo?: boolean;
204
- icu?: boolean;
205
- }
206
-
207
- export interface StatsOptions extends ReadableOptions {
208
- database?: string;
209
- record?: boolean;
210
- nocreation?: boolean;
211
- tables?: boolean;
212
- pages?: boolean;
213
- header?: boolean;
214
- indexes?: boolean;
215
- tablesystem?: boolean;
216
- encryption?: boolean;
217
- objects?: string; // space-separated list of object index,table,systemtable
218
- }
219
-
220
- interface UserInfo {
221
- userid: number;
222
- groupid: number;
223
- username: string;
224
- firstname: string;
225
- middlename: string;
226
- lastname: string
227
- admin: number;
228
- rolename?: string;
229
- groupname?: string;
230
- }
231
-
232
- export interface ServerInfo {
233
- result: number;
234
- dbinfo?: { database: any[], nbattachment: number, nbdatabase: number };
235
- fbconfig?: any;
236
- svcversion?: number;
237
- fbversion?: string;
238
- fbimplementation?: string;
239
- fbcapatibilities: string[];
240
- pathsecuritydb?: string;
241
- fbenv?: string;
242
- fbenvlock?: string;
243
- fbenvmsg?: string;
244
- limbotrans?: number[];
245
- fbusers?: UserInfo[]
246
- }
247
-
248
- export interface ServerInfoReq {
249
- dbinfo?: boolean;
250
- fbconfig?: boolean;
251
- svcversion?: boolean;
252
- fbversion?: boolean;
253
- fbimplementation?: boolean;
254
- fbcapatibilities?: boolean;
255
- pathsecuritydb?: boolean;
256
- fbenv?: boolean;
257
- fbenvlock?: boolean;
258
- fbenvmsg?: boolean;
259
- limbotrans?: boolean;
260
- }
261
-
262
- export interface TraceOptions extends ReadableOptions {
263
- configfile?: string; // startTrace uses it
264
- tracename?: string; // startTrace uses it
265
- traceid?: number; // suspendTrace, stopTrace, and resumeTrace use it
266
- }
267
-
268
- type ServiceManagerCallback = (err: any, svc: ServiceManager) => void;
269
- // @ts-ignore
270
- type ReadableCallback = (err: any, reader: NodeJS.ReadableStream) => void;
271
- type InfoCallback = (err: any, info: ServerInfo) => void;
272
- type LineCallback = (err: any, data: { result: number, line: string }) => void;
273
-
274
- export enum ShutdownMode { NORMAL = 0, MULTI = 1, SINGLE = 2, FULL = 3 }
275
- export enum ShutdownKind { FORCED = 0, DENY_TRANSACTION = 1, DENY_ATTACHMENT = 2 }
276
-
277
- export interface ServiceManager {
278
- detach(callback?: SimpleCallback, force?: boolean): void;
279
- backup(options: BackupOptions, callback: ReadableCallback): void;
280
- nbackup(options: BackupOptions, callback: ReadableCallback): void;
281
- restore(options: NRestoreOptions, callback: ReadableCallback): void;
282
- nrestore(options: any, callback: Function): void;
283
- setDialect(db: string, dialect: 1 | 3, callback: ReadableCallback): void;
284
- setSweepinterval(db: string, interval: number, callback: Function): void; // gfix -h INTERVAL
285
- setCachebuffer(db: string, nbpages: any, callback: ReadableCallback): void; // gfix -b NBPAGES
286
- BringOnline(db: string, callback: ReadableCallback): void; // gfix -o
287
- Shutdown(db: string, kind: ShutdownKind, delay: number, mode: ShutdownMode, callback: ReadableCallback): void; // server version >= 2.0
288
- Shutdown(db: string, kind: ShutdownKind, delay: number, callback: ReadableCallback): void; // server version < 2.0
289
- setShadow(db: string, val: boolean, callback: ReadableCallback): void;
290
- setForcewrite(db: string, val: boolean, callback: ReadableCallback): void; // gfix -write
291
- setReservespace(db: string, val: boolean, callback: ReadableCallback): void; // true: gfix -use reserve, false: gfix -use full
292
- setReadonlyMode(db: string, callback: ReadableCallback): void; // gfix -mode read_only
293
- setReadwriteMode(db: string, callback: ReadableCallback): void; // gfix -mode read_write
294
- validate(options: ValidateOptions, callback: ReadableCallback): void; // gfix -validate
295
- commit(db: string, transactid: number, callback: ReadableCallback): void; // gfix -commit
296
- rollback(db: string, transactid: number, callback: ReadableCallback): void;
297
- recover(db: string, transactid: number, callback: ReadableCallback): void;
298
- getStats(options: StatsOptions, callback: ReadableCallback): void;
299
- getLog(options: ReadableOptions, callback: ReadableCallback): void;
300
- getUsers(username: string | null, callback: InfoCallback): void;
301
- addUser(username: string, password: string, info: UserInfo, callback: ReadableCallback): void;
302
- editUser(username: string, info: UserInfo, callback: ReadableCallback): void;
303
- removeUser(username: string, rolename: string | null, callback: ReadableCallback): void;
304
- getFbserverInfos(infos: ServerInfoReq, options: { buffersize?: number, timeout?: number }, callback: InfoCallback): void; // if infos is empty all options are asked to the service
305
- startTrace(options: TraceOptions, callback: ReadableCallback): void;
306
- suspendTrace(options: TraceOptions, callback: ReadableCallback): void;
307
- resumeTrace(options: TraceOptions, callback: ReadableCallback): void;
308
- stopTrace(options: TraceOptions, callback: ReadableCallback): void;
309
- getTraceList(options: ReadableOptions, callback: ReadableCallback): void;
310
- readline(options: ReadableOptions, callback: LineCallback): void;
311
- readeof(options: ReadableOptions, callback: LineCallback): void;
312
- hasRunningAction(options: ReadableOptions, callback: ReadableCallback): void;
313
- readusers(options: ReadableOptions, callback: ReadableCallback): void;
314
- readlimbo(options: ReadableOptions, callback: ReadableCallback): void;
315
- }
316
- }
@@ -1,128 +0,0 @@
1
- const Const = require('./wire/const');
2
- const {doError, doCallback} = require('./callback');
3
- const Connection = require('./wire/connection');
4
- const Pool = require('./pool');
5
- const {escape} = require('./utils');
6
-
7
- if (typeof(setImmediate) === 'undefined') {
8
- global.setImmediate = function(cb) {
9
- process.nextTick(cb);
10
- };
11
- }
12
-
13
- exports.AUTH_PLUGIN_LEGACY = Const.AUTH_PLUGIN_LEGACY;
14
- exports.AUTH_PLUGIN_SRP = Const.AUTH_PLUGIN_SRP;
15
- exports.AUTH_PLUGIN_SRP256 = Const.AUTH_PLUGIN_SRP256;
16
-
17
- exports.WIRE_CRYPT_DISABLE = Const.WIRE_CRYPT_DISABLE;
18
- exports.WIRE_CRYPT_ENABLE = Const.WIRE_CRYPT_ENABLE;
19
-
20
- exports.ISOLATION_READ_UNCOMMITTED = Const.ISOLATION_READ_UNCOMMITTED;
21
- exports.ISOLATION_READ_COMMITTED = Const.ISOLATION_READ_COMMITTED;
22
- exports.ISOLATION_REPEATABLE_READ = Const.ISOLATION_REPEATABLE_READ;
23
- exports.ISOLATION_SERIALIZABLE = Const.ISOLATION_SERIALIZABLE;
24
- exports.ISOLATION_READ_COMMITTED_READ_ONLY = Const.ISOLATION_READ_COMMITTED_READ_ONLY;
25
-
26
- exports.escape = escape;
27
-
28
- exports.attach = function(options, callback) {
29
- var host = options.host || Const.DEFAULT_HOST;
30
- var port = options.port || Const.DEFAULT_PORT;
31
- var manager = options.manager || false;
32
- var cnx = this.connection = new Connection(host, port, function(err) {
33
-
34
- if (err) {
35
- doError(err, callback);
36
- return;
37
- }
38
-
39
- cnx.connect(options, function(err) {
40
- if (err) {
41
- doError(err, callback);
42
- } else {
43
- if (manager)
44
- cnx.svcattach(options, callback);
45
- else
46
- cnx.attach(options, callback);
47
- }
48
- });
49
-
50
- }, options);
51
- };
52
-
53
- exports.drop = function(options, callback) {
54
- exports.attach(options, function(err, db) {
55
- if (err) {
56
- callback({ error: err, message: "Drop error" });
57
- return;
58
- }
59
-
60
- db.drop(callback);
61
- });
62
- };
63
-
64
- exports.create = function(options, callback) {
65
- var host = options.host || Const.DEFAULT_HOST;
66
- var port = options.port || Const.DEFAULT_PORT;
67
- var cnx = this.connection = new Connection(host, port, function(err) {
68
-
69
- var self = cnx;
70
-
71
- if (err) {
72
- callback({ error: err, message: "Connect error" });
73
- return;
74
- }
75
-
76
- cnx.connect(options, function(err) {
77
- if (err) {
78
- self.db.emit('error', err);
79
- doError(err, callback);
80
- return;
81
- }
82
-
83
- cnx.createDatabase(options, callback);
84
- });
85
- }, options);
86
- };
87
-
88
- exports.attachOrCreate = function(options, callback) {
89
-
90
- var host = options.host || Const.DEFAULT_HOST;
91
- var port = options.port || Const.DEFAULT_PORT;
92
-
93
- var cnx = this.connection = new Connection(host, port, function(err) {
94
-
95
- var self = cnx;
96
-
97
- if (err) {
98
- callback({ error: err, message: "Connect error" });
99
- return;
100
- }
101
-
102
- cnx.connect(options, function(err) {
103
-
104
- if (err) {
105
- doError(err, callback);
106
- return;
107
- }
108
-
109
- cnx.attach(options, function(err, ret) {
110
-
111
- if (!err) {
112
- if (self.db)
113
- self.db.emit('connect', ret);
114
- doCallback(ret, callback);
115
- return;
116
- }
117
-
118
- cnx.createDatabase(options, callback);
119
- });
120
- });
121
-
122
- }, options);
123
- };
124
-
125
- // Pooling
126
- exports.pool = function(max, options) {
127
- return new Pool(exports.attach, max, Object.assign({}, options, { isPool: true }));
128
- };
@@ -1,108 +0,0 @@
1
- /***************************************
2
- *
3
- * Simple Pooling
4
- *
5
- ***************************************/
6
-
7
- class Pool {
8
- constructor(attach, max, options) {
9
- this.attach = attach;
10
- this.internaldb = []; // connection created by the pool (for destroy)
11
- this.pooldb = []; // available connection in the pool
12
- this.dbinuse = 0; // connection currently in use into the pool
13
- this._creating = 0; // connections currently being created
14
- this.max = max || 4;
15
- this.pending = [];
16
- this.options = options;
17
- }
18
-
19
- get(callback) {
20
- var self = this;
21
- self.pending.push(callback);
22
- self.check();
23
- return self;
24
- }
25
-
26
- check() {
27
- var self = this;
28
- if ((self.dbinuse + self._creating) >= self.max)
29
- return self;
30
-
31
- var cb = self.pending.shift();
32
- if (!cb)
33
- return self;
34
- if (self.pooldb.length) {
35
- self.dbinuse++;
36
- cb(null, self.pooldb.shift());
37
- } else {
38
- self._creating++;
39
- this.attach(self.options, function (err, db) {
40
- self._creating--;
41
- if (!err) {
42
- self.dbinuse++;
43
- self.internaldb.push(db);
44
- db.on('detach', function () {
45
- // also in pool (could be a twice call to detach)
46
- if (self.pooldb.indexOf(db) !== -1 || self.internaldb.indexOf(db) === -1)
47
- return;
48
- // if not usable don't put in again in the pool and remove reference on it
49
- if (db.connection._isClosed || db.connection._isDetach || db.connection._pooled === false)
50
- self.internaldb.splice(self.internaldb.indexOf(db), 1);
51
- else
52
- self.pooldb.push(db);
53
-
54
- self.dbinuse--;
55
- self.check();
56
- });
57
- }
58
-
59
- cb(err, db);
60
- });
61
- }
62
- setImmediate(function() {
63
- self.check();
64
- });
65
-
66
- return self;
67
- }
68
-
69
- destroy(callback) {
70
- var self = this;
71
-
72
- var connectionCount = this.internaldb.length;
73
-
74
- if (connectionCount === 0 && callback) {
75
- callback();
76
- }
77
-
78
- function detachCallback(err) {
79
- if (err) {
80
- if (callback) {
81
- callback(err);
82
- }
83
- return;
84
- }
85
-
86
- connectionCount--;
87
- if (connectionCount === 0 && callback) {
88
- callback();
89
- }
90
- }
91
-
92
- this.internaldb.forEach(function(db) {
93
- if (db.connection._pooled === false) {
94
- detachCallback();
95
- return;
96
- }
97
- // check if the db is not free into the pool otherwise user should manual detach it
98
- var _db_in_pool = self.pooldb.indexOf(db);
99
- if (_db_in_pool !== -1) {
100
- self.pooldb.splice(_db_in_pool, 1);
101
- db.connection._pooled = false;
102
- db.detach(detachCallback);
103
- }
104
- });
105
- }
106
- }
107
-
108
- module.exports = Pool;