node-firebird 2.3.4 → 2.4.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 +574 -20
- package/lib/callback.d.ts +20 -0
- package/lib/callback.js +6 -16
- package/lib/firebird.msg.json +1370 -1370
- package/lib/gdscodes.d.ts +1519 -1520
- package/lib/gdscodes.js +5 -5
- package/lib/ieee754-decimal.d.ts +38 -0
- package/lib/ieee754-decimal.js +71 -118
- package/lib/index.d.ts +37 -338
- package/lib/index.js +97 -86
- package/lib/messages.d.ts +6 -0
- package/lib/messages.js +135 -162
- package/lib/pool.d.ts +23 -0
- package/lib/pool.js +34 -47
- package/lib/srp.d.ts +51 -0
- package/lib/srp.js +62 -72
- package/lib/types.d.ts +375 -0
- package/lib/types.js +22 -0
- package/lib/unix-crypt.d.ts +1 -0
- package/lib/unix-crypt.js +126 -152
- package/lib/utils.d.ts +16 -0
- package/lib/utils.js +24 -48
- package/lib/wire/connection.d.ts +100 -0
- package/lib/wire/connection.js +1029 -1009
- package/lib/wire/const.d.ts +567 -0
- package/lib/wire/const.js +620 -599
- package/lib/wire/database.d.ts +57 -0
- package/lib/wire/database.js +111 -108
- package/lib/wire/eventConnection.d.ts +16 -0
- package/lib/wire/eventConnection.js +29 -31
- package/lib/wire/fbEventManager.d.ts +45 -0
- package/lib/wire/fbEventManager.js +38 -57
- package/lib/wire/serialize.d.ts +97 -0
- package/lib/wire/serialize.js +42 -141
- package/lib/wire/service.d.ts +66 -0
- package/lib/wire/service.js +374 -401
- package/lib/wire/socket.d.ts +59 -0
- package/lib/wire/socket.js +79 -40
- package/lib/wire/statement.d.ts +25 -0
- package/lib/wire/statement.js +13 -9
- package/lib/wire/transaction.d.ts +21 -0
- package/lib/wire/transaction.js +33 -61
- package/lib/wire/xsqlvar.d.ts +175 -0
- package/lib/wire/xsqlvar.js +169 -269
- package/package.json +12 -4
- package/src/callback.ts +55 -0
- package/{poc/node_modules/node-firebird/lib/gdscodes.js → src/gdscodes.ts} +3 -3
- package/{poc/node_modules/node-firebird/lib/ieee754-decimal.js → src/ieee754-decimal.ts} +7 -14
- package/src/index.ts +157 -0
- package/{poc/node_modules/node-firebird/lib/messages.js → src/messages.ts} +163 -162
- package/src/pool.ts +209 -0
- package/{poc/node_modules/node-firebird/lib/srp.js → src/srp.ts} +74 -33
- package/src/types.ts +419 -0
- package/{poc/node_modules/node-firebird/lib/unix-crypt.js → src/unix-crypt.ts} +6 -12
- package/{poc/node_modules/node-firebird/lib/utils.js → src/utils.ts} +11 -20
- package/{poc/node_modules/node-firebird/lib/wire/connection.js → src/wire/connection.ts} +611 -129
- package/{poc/node_modules/node-firebird/lib/wire/const.js → src/wire/const.ts} +96 -9
- package/{poc/node_modules/node-firebird/lib/wire/database.js → src/wire/database.ts} +110 -44
- package/{poc/node_modules/node-firebird/lib/wire/eventConnection.js → src/wire/eventConnection.ts} +29 -14
- package/{poc/node_modules/node-firebird/lib/wire/fbEventManager.js → src/wire/fbEventManager.ts} +39 -30
- package/{poc/node_modules/node-firebird/lib/wire/serialize.js → src/wire/serialize.ts} +78 -59
- package/{poc/node_modules/node-firebird/lib/wire/service.js → src/wire/service.ts} +90 -88
- package/src/wire/socket.ts +262 -0
- package/src/wire/statement.ts +71 -0
- package/{poc/node_modules/node-firebird/lib/wire/transaction.js → src/wire/transaction.ts} +25 -19
- package/{poc/node_modules/node-firebird/lib/wire/xsqlvar.js → src/wire/xsqlvar.ts} +244 -143
- package/.eslintrc.json +0 -12
- package/.github/workflows/codeql.yml +0 -76
- package/.github/workflows/node.js.yml +0 -95
- package/BIGINT_MIGRATION.md +0 -374
- package/CI_DEBUGGING_GUIDE.md +0 -148
- package/ENCRYPTION_CALLBACK.md +0 -152
- package/FIREBIRD_LOG_FEATURE.md +0 -145
- package/MINIMAL_CHANGES_SUMMARY.md +0 -136
- package/PR_SUMMARY.md +0 -96
- package/ROADMAP.md +0 -223
- package/SRP_PROTOCOL.md +0 -482
- package/poc/README.md +0 -160
- package/poc/helpers.js +0 -59
- package/poc/node_modules/.package-lock.json +0 -14
- package/poc/node_modules/node-firebird/.eslintrc.json +0 -12
- package/poc/node_modules/node-firebird/.github/workflows/codeql.yml +0 -76
- package/poc/node_modules/node-firebird/.github/workflows/node.js.yml +0 -95
- package/poc/node_modules/node-firebird/BIGINT_MIGRATION.md +0 -374
- package/poc/node_modules/node-firebird/CI_DEBUGGING_GUIDE.md +0 -148
- package/poc/node_modules/node-firebird/ENCRYPTION_CALLBACK.md +0 -152
- package/poc/node_modules/node-firebird/FIREBIRD_LOG_FEATURE.md +0 -145
- package/poc/node_modules/node-firebird/LICENSE +0 -373
- package/poc/node_modules/node-firebird/MINIMAL_CHANGES_SUMMARY.md +0 -136
- package/poc/node_modules/node-firebird/PR_SUMMARY.md +0 -96
- package/poc/node_modules/node-firebird/README.md +0 -794
- package/poc/node_modules/node-firebird/ROADMAP.md +0 -223
- package/poc/node_modules/node-firebird/SRP_PROTOCOL.md +0 -482
- package/poc/node_modules/node-firebird/lib/callback.js +0 -38
- package/poc/node_modules/node-firebird/lib/gdscodes.d.ts +0 -1524
- package/poc/node_modules/node-firebird/lib/index.d.ts +0 -316
- package/poc/node_modules/node-firebird/lib/index.js +0 -128
- package/poc/node_modules/node-firebird/lib/pool.js +0 -108
- package/poc/node_modules/node-firebird/lib/wire/socket.js +0 -175
- package/poc/node_modules/node-firebird/lib/wire/statement.js +0 -48
- package/poc/node_modules/node-firebird/package.json +0 -38
- package/poc/node_modules/node-firebird/vitest.config.js +0 -24
- package/poc/package-lock.json +0 -21
- package/poc/package.json +0 -12
- package/poc/reproduce-fixed.js +0 -150
- package/poc/reproduce.js +0 -133
- package/vitest.config.js +0 -27
- /package/{poc/node_modules/node-firebird/lib → src}/firebird.msg +0 -0
- /package/{poc/node_modules/node-firebird/lib → src}/firebird.msg.json +0 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import net from "net";
|
|
2
|
+
import zlib from "zlib";
|
|
3
|
+
import crypto from "crypto";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Arc4 stream cipher for Firebird wire encryption.
|
|
7
|
+
* Uses the SRP session key to create RC4 encryption/decryption streams.
|
|
8
|
+
*/
|
|
9
|
+
class Arc4 {
|
|
10
|
+
_s: Uint8Array;
|
|
11
|
+
_i: number;
|
|
12
|
+
_j: number;
|
|
13
|
+
|
|
14
|
+
constructor(key: Buffer | Uint8Array) {
|
|
15
|
+
this._s = new Uint8Array(256);
|
|
16
|
+
this._i = 0;
|
|
17
|
+
this._j = 0;
|
|
18
|
+
|
|
19
|
+
// KSA (Key-Scheduling Algorithm)
|
|
20
|
+
for (var i = 0; i < 256; i++) {
|
|
21
|
+
this._s[i] = i;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var j = 0;
|
|
25
|
+
for (var i = 0; i < 256; i++) {
|
|
26
|
+
j = (j + this._s[i] + key[i % key.length]) & 0xFF;
|
|
27
|
+
// Swap
|
|
28
|
+
var tmp = this._s[i];
|
|
29
|
+
this._s[i] = this._s[j];
|
|
30
|
+
this._s[j] = tmp;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Transform (encrypt/decrypt) data in place.
|
|
36
|
+
* RC4 is symmetric - encrypt and decrypt are the same operation.
|
|
37
|
+
*/
|
|
38
|
+
transform(data: Buffer | Uint8Array): Buffer {
|
|
39
|
+
var out = Buffer.alloc(data.length);
|
|
40
|
+
for (var n = 0; n < data.length; n++) {
|
|
41
|
+
this._i = (this._i + 1) & 0xFF;
|
|
42
|
+
this._j = (this._j + this._s[this._i]) & 0xFF;
|
|
43
|
+
|
|
44
|
+
// Swap
|
|
45
|
+
var tmp = this._s[this._i];
|
|
46
|
+
this._s[this._i] = this._s[this._j];
|
|
47
|
+
this._s[this._j] = tmp;
|
|
48
|
+
|
|
49
|
+
var k = this._s[(this._s[this._i] + this._s[this._j]) & 0xFF];
|
|
50
|
+
out[n] = data[n] ^ k;
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* ChaChaCipher wrapper around Node.js crypto.createCipheriv / createDecipheriv.
|
|
58
|
+
*/
|
|
59
|
+
class ChaChaCipher {
|
|
60
|
+
_cipher: crypto.Cipheriv | crypto.Decipheriv;
|
|
61
|
+
|
|
62
|
+
constructor(key: Buffer, iv: Buffer, ivlen: number, isEncrypt: boolean) {
|
|
63
|
+
const opensslIv = Buffer.alloc(16);
|
|
64
|
+
if (ivlen === 8) {
|
|
65
|
+
iv.copy(opensslIv, 8, 0, 8);
|
|
66
|
+
} else if (ivlen === 16) {
|
|
67
|
+
const ctr = (iv[12] << 24) + (iv[13] << 16) + (iv[14] << 8) + iv[15];
|
|
68
|
+
opensslIv.writeUInt32LE(ctr, 0);
|
|
69
|
+
iv.copy(opensslIv, 4, 0, 12);
|
|
70
|
+
} else if (ivlen === 12) {
|
|
71
|
+
iv.copy(opensslIv, 4, 0, 12);
|
|
72
|
+
} else {
|
|
73
|
+
throw new Error('Wrong IV length: ' + ivlen);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (isEncrypt) {
|
|
77
|
+
this._cipher = crypto.createCipheriv('chacha20', key, opensslIv);
|
|
78
|
+
} else {
|
|
79
|
+
this._cipher = crypto.createDecipheriv('chacha20', key, opensslIv);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
transform(data: Buffer): Buffer {
|
|
84
|
+
return this._cipher.update(data);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Socket proxy.
|
|
90
|
+
*/
|
|
91
|
+
class Socket {
|
|
92
|
+
static Arc4 = Arc4;
|
|
93
|
+
|
|
94
|
+
_socket: net.Socket;
|
|
95
|
+
compress: boolean;
|
|
96
|
+
compressor: zlib.Deflate | null;
|
|
97
|
+
compressorBuffer: Buffer[];
|
|
98
|
+
decompressor: zlib.Inflate | null;
|
|
99
|
+
decompressorBuffer: Buffer[];
|
|
100
|
+
buffer: Buffer | null;
|
|
101
|
+
encrypt: boolean;
|
|
102
|
+
encryptCipher: any;
|
|
103
|
+
decryptCipher: any;
|
|
104
|
+
|
|
105
|
+
constructor(port: number, host: string) {
|
|
106
|
+
this._socket = net.createConnection(port, host);
|
|
107
|
+
this._socket.setNoDelay(true);
|
|
108
|
+
this._socket.setKeepAlive(true, 60000); // 1 minute delay to detect dead/stale connections
|
|
109
|
+
this.compressor = null;
|
|
110
|
+
this.compressorBuffer = [];
|
|
111
|
+
this.decompressor = null;
|
|
112
|
+
this.decompressorBuffer = [];
|
|
113
|
+
this.buffer = null;
|
|
114
|
+
this.encrypt = false;
|
|
115
|
+
this.encryptCipher = null;
|
|
116
|
+
this.decryptCipher = null;
|
|
117
|
+
|
|
118
|
+
return new Proxy(this._socket, this as any) as any;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Decompress and/or decrypt data when received.
|
|
123
|
+
* Override on data event.
|
|
124
|
+
*/
|
|
125
|
+
on(event: string, cb: (...args: any[]) => void): void {
|
|
126
|
+
if (event === 'data') {
|
|
127
|
+
const mainCb = cb;
|
|
128
|
+
cb = (data: Buffer) => {
|
|
129
|
+
// Decrypt first if encryption is enabled
|
|
130
|
+
if (this.encrypt) {
|
|
131
|
+
data = this.decryptCipher.transform(data);
|
|
132
|
+
if (process.env.FIREBIRD_DEBUG) {
|
|
133
|
+
console.log('[fb-debug] socket.read decrypted: length=%d RX-cipher-offset=%d',
|
|
134
|
+
data.length, this.decryptCipher._i);
|
|
135
|
+
console.log('[fb-debug] socket.read decrypted hex: %s', data.toString('hex'));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (this.compress) {
|
|
140
|
+
this.decompressor.write(data, () => {
|
|
141
|
+
mainCb(Buffer.concat(this.decompressorBuffer));
|
|
142
|
+
this.decompressorBuffer = []; // Reset buffer
|
|
143
|
+
});
|
|
144
|
+
} else {
|
|
145
|
+
mainCb(data);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
this._socket.on(event, cb);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Compress and/or encrypt data before sending to socket.
|
|
155
|
+
*/
|
|
156
|
+
write(data: Buffer | Uint8Array, defer = false): void {
|
|
157
|
+
if (process.env.FIREBIRD_DEBUG) {
|
|
158
|
+
console.log('[fb-debug] socket.write: length=%d bytes=%s encrypt=%s defer=%s',
|
|
159
|
+
data.length, Buffer.from(data).toString('hex'), this.encrypt, defer);
|
|
160
|
+
}
|
|
161
|
+
if (defer) {
|
|
162
|
+
// Accumulate deferred packets instead of overwriting. Multiple
|
|
163
|
+
// deferred ops (e.g. op_close_blob followed immediately by
|
|
164
|
+
// op_free_statement) must all be kept until the next flush;
|
|
165
|
+
// overwriting the buffer silently drops packets and desynchronises
|
|
166
|
+
// the request/response queue, causing the connection to hang.
|
|
167
|
+
this.buffer = this.buffer
|
|
168
|
+
? Buffer.concat([this.buffer, Buffer.from(data)])
|
|
169
|
+
: Buffer.from(data);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!defer && this.buffer) {
|
|
174
|
+
data = Buffer.concat([this.buffer, data]);
|
|
175
|
+
this.buffer = null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (this.compress) {
|
|
179
|
+
this.compressor.write(data, () => {
|
|
180
|
+
var compressedData = Buffer.concat(this.compressorBuffer);
|
|
181
|
+
this.compressorBuffer = []; // Reset buffer
|
|
182
|
+
|
|
183
|
+
// Encrypt after compression if encryption is enabled
|
|
184
|
+
if (this.encrypt) {
|
|
185
|
+
compressedData = this.encryptCipher.transform(compressedData);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
this._socket.write(compressedData);
|
|
189
|
+
});
|
|
190
|
+
} else if (this.encrypt) {
|
|
191
|
+
const out = this.encryptCipher.transform(data);
|
|
192
|
+
if (process.env.FIREBIRD_DEBUG) {
|
|
193
|
+
console.log('[fb-debug] socket.write encrypted: length=%d TX-cipher-offset=%d',
|
|
194
|
+
out.length, this.encryptCipher._i);
|
|
195
|
+
}
|
|
196
|
+
this._socket.write(out);
|
|
197
|
+
} else {
|
|
198
|
+
this._socket.write(data);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Enable compression/decompression on the fly.
|
|
204
|
+
*/
|
|
205
|
+
enableCompression(): void {
|
|
206
|
+
this.compress = true;
|
|
207
|
+
|
|
208
|
+
// Create decompressor instance
|
|
209
|
+
this.decompressor = zlib.createInflate();
|
|
210
|
+
this.decompressor.on('data', (inflate) => {
|
|
211
|
+
this.decompressorBuffer.push(inflate);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// Create compressor instance
|
|
215
|
+
this.compressor = zlib.createDeflate({
|
|
216
|
+
flush: zlib.constants.Z_FULL_FLUSH,
|
|
217
|
+
finishFlush: zlib.constants.Z_SYNC_FLUSH
|
|
218
|
+
});
|
|
219
|
+
this.compressor.on('data', (deflate) => {
|
|
220
|
+
this.compressorBuffer.push(deflate);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Enable encryption/decryption.
|
|
226
|
+
* @param {Buffer} sessionKey - The session key from SRP authentication.
|
|
227
|
+
* @param {string} [pluginName='Arc4'] - The selected encryption plugin.
|
|
228
|
+
* @param {Buffer} [iv] - The initialization vector (needed for ChaCha/ChaCha64).
|
|
229
|
+
*/
|
|
230
|
+
enableEncryption(sessionKey: Buffer, pluginName: string = 'Arc4', iv?: Buffer): void {
|
|
231
|
+
this.encrypt = true;
|
|
232
|
+
if (pluginName === 'Arc4') {
|
|
233
|
+
this.encryptCipher = new Arc4(sessionKey);
|
|
234
|
+
this.decryptCipher = new Arc4(sessionKey);
|
|
235
|
+
} else if (pluginName === 'ChaCha' || pluginName === 'ChaCha64') {
|
|
236
|
+
const stretchedKey = crypto.createHash('sha256').update(sessionKey).digest();
|
|
237
|
+
const ivlen = pluginName === 'ChaCha64' ? 8 : (iv ? iv.length : 16);
|
|
238
|
+
|
|
239
|
+
this.encryptCipher = new ChaChaCipher(stretchedKey, iv, ivlen, true);
|
|
240
|
+
this.decryptCipher = new ChaChaCipher(stretchedKey, iv, ivlen, false);
|
|
241
|
+
} else {
|
|
242
|
+
throw new Error('Unsupported encryption plugin: ' + pluginName);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Proxy trap.
|
|
248
|
+
*/
|
|
249
|
+
get(target: any, field: string | symbol): any {
|
|
250
|
+
if (field in this) {
|
|
251
|
+
return this[field].bind(this);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (typeof target[field] === 'function') {
|
|
255
|
+
return target[field].bind(target);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return target[field];
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export = Socket;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/***************************************
|
|
2
|
+
*
|
|
3
|
+
* Statement
|
|
4
|
+
*
|
|
5
|
+
***************************************/
|
|
6
|
+
|
|
7
|
+
class Statement {
|
|
8
|
+
connection: any;
|
|
9
|
+
query: string;
|
|
10
|
+
type: number;
|
|
11
|
+
output: any[];
|
|
12
|
+
input: any[];
|
|
13
|
+
options: any;
|
|
14
|
+
handle: number;
|
|
15
|
+
plan: string;
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
|
|
18
|
+
constructor(connection: any) {
|
|
19
|
+
this.connection = connection;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
close(callback?: (err?: any) => void): void {
|
|
23
|
+
this.connection.closeStatement(this, callback);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
drop(callback?: (err?: any) => void): void {
|
|
27
|
+
this.connection.dropStatement(this, callback);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
release(callback?: (err?: any) => void): void {
|
|
31
|
+
var cache_query = this.connection.getCachedQuery(this.query);
|
|
32
|
+
if (cache_query)
|
|
33
|
+
this.connection.closeStatement(this, callback);
|
|
34
|
+
else
|
|
35
|
+
this.connection.dropStatement(this, callback);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
execute(transaction: any, params?: any, callback?: any, options?: any): void {
|
|
39
|
+
if (params instanceof Function) {
|
|
40
|
+
options = callback;
|
|
41
|
+
callback = params;
|
|
42
|
+
params = undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.options = options;
|
|
46
|
+
this.connection.executeStatement(transaction, this, params, callback, options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fetch(transaction: any, count: number | string, callback: (err: any, result?: any) => void): void {
|
|
50
|
+
this.connection.fetch(this, transaction, count, callback);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fetchScroll(transaction: any, direction: string | number, offset?: any, count?: any, callback?: any): void {
|
|
54
|
+
if (typeof count === 'function') {
|
|
55
|
+
callback = count;
|
|
56
|
+
count = undefined;
|
|
57
|
+
}
|
|
58
|
+
if (typeof offset === 'function') {
|
|
59
|
+
callback = offset;
|
|
60
|
+
offset = undefined;
|
|
61
|
+
count = undefined;
|
|
62
|
+
}
|
|
63
|
+
this.connection.fetchScroll(this, transaction, direction, offset, count, callback);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
fetchAll(transaction: any, callback: (err: any, result?: any) => void): void {
|
|
67
|
+
this.connection.fetchAll(this, transaction, callback);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export = Statement;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { doCallback, doError } from '../callback';
|
|
2
|
+
import { noop } from '../utils';
|
|
3
|
+
import Const from './const';
|
|
3
4
|
|
|
4
5
|
/***************************************
|
|
5
6
|
*
|
|
@@ -8,12 +9,17 @@ const Const = require('./const');
|
|
|
8
9
|
***************************************/
|
|
9
10
|
|
|
10
11
|
class Transaction {
|
|
11
|
-
|
|
12
|
+
connection: any;
|
|
13
|
+
db: any;
|
|
14
|
+
handle: number;
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
|
|
17
|
+
constructor(connection: any) {
|
|
12
18
|
this.connection = connection;
|
|
13
19
|
this.db = connection.db;
|
|
14
20
|
}
|
|
15
21
|
|
|
16
|
-
newStatement(query, callback) {
|
|
22
|
+
newStatement(query: string, callback: (err: any, statement?: any) => void): void {
|
|
17
23
|
var cnx = this.connection;
|
|
18
24
|
var self = this;
|
|
19
25
|
var query_cache = cnx.getCachedQuery(query);
|
|
@@ -25,7 +31,7 @@ class Transaction {
|
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
execute(query, params, callback, options) {
|
|
34
|
+
execute(query: string, params?: any, callback?: any, options?: any): void {
|
|
29
35
|
if (params instanceof Function) {
|
|
30
36
|
options = callback;
|
|
31
37
|
callback = params;
|
|
@@ -33,19 +39,19 @@ class Transaction {
|
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
var self = this;
|
|
36
|
-
this.newStatement(query, function(err, statement) {
|
|
42
|
+
this.newStatement(query, function(err: any, statement: any) {
|
|
37
43
|
|
|
38
44
|
if (err) {
|
|
39
45
|
doError(err, callback);
|
|
40
46
|
return;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
|
-
function dropError(err) {
|
|
49
|
+
function dropError(err: any) {
|
|
44
50
|
statement.release();
|
|
45
51
|
doCallback(err, callback);
|
|
46
52
|
}
|
|
47
53
|
|
|
48
|
-
statement.execute(self, params, function(err, ret) {
|
|
54
|
+
statement.execute(self, params, function(err: any, ret: any) {
|
|
49
55
|
if (err) {
|
|
50
56
|
dropError(err);
|
|
51
57
|
return;
|
|
@@ -53,7 +59,7 @@ class Transaction {
|
|
|
53
59
|
|
|
54
60
|
switch (statement.type) {
|
|
55
61
|
case Const.isc_info_sql_stmt_select:
|
|
56
|
-
statement.fetchAll(self, function(err, r) {
|
|
62
|
+
statement.fetchAll(self, function(err: any, r: any) {
|
|
57
63
|
if (err) {
|
|
58
64
|
dropError(err);
|
|
59
65
|
return;
|
|
@@ -77,7 +83,7 @@ class Transaction {
|
|
|
77
83
|
|
|
78
84
|
break;
|
|
79
85
|
} else if (statement.output.length) {
|
|
80
|
-
statement.fetch(self, 1, function(err, ret) {
|
|
86
|
+
statement.fetch(self, 1, function(err: any, ret: any) {
|
|
81
87
|
if (err) {
|
|
82
88
|
dropError(err);
|
|
83
89
|
return;
|
|
@@ -104,7 +110,7 @@ class Transaction {
|
|
|
104
110
|
});
|
|
105
111
|
}
|
|
106
112
|
|
|
107
|
-
sequentially(query, params, on, callback, options = {}) {
|
|
113
|
+
sequentially(query: string, params?: any, on?: any, callback?: any, options: any = {}): this {
|
|
108
114
|
if (params instanceof Function) {
|
|
109
115
|
options = callback;
|
|
110
116
|
callback = on;
|
|
@@ -122,9 +128,9 @@ class Transaction {
|
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
var self = this;
|
|
125
|
-
var _on = function(row, i, meta, next) {
|
|
131
|
+
var _on = function(row: any, i: number, meta: any, next: (err?: any) => void) {
|
|
126
132
|
var done = false;
|
|
127
|
-
var finish = function(err) {
|
|
133
|
+
var finish = function(err?: any) {
|
|
128
134
|
if (done) {
|
|
129
135
|
return;
|
|
130
136
|
}
|
|
@@ -168,7 +174,7 @@ class Transaction {
|
|
|
168
174
|
return self;
|
|
169
175
|
}
|
|
170
176
|
|
|
171
|
-
query(query, params, callback, options = {}) {
|
|
177
|
+
query(query: string, params?: any, callback?: any, options: any = {}): void {
|
|
172
178
|
if (params instanceof Function) {
|
|
173
179
|
callback = params;
|
|
174
180
|
params = undefined;
|
|
@@ -186,21 +192,21 @@ class Transaction {
|
|
|
186
192
|
this.execute(query, params, callback, options);
|
|
187
193
|
}
|
|
188
194
|
|
|
189
|
-
commit(callback) {
|
|
195
|
+
commit(callback?: (err?: any) => void): void {
|
|
190
196
|
this.connection.commit(this, callback);
|
|
191
197
|
}
|
|
192
198
|
|
|
193
|
-
rollback(callback) {
|
|
199
|
+
rollback(callback?: (err?: any) => void): void {
|
|
194
200
|
this.connection.rollback(this, callback);
|
|
195
201
|
}
|
|
196
202
|
|
|
197
|
-
commitRetaining(callback) {
|
|
203
|
+
commitRetaining(callback?: (err?: any) => void): void {
|
|
198
204
|
this.connection.commitRetaining(this, callback);
|
|
199
205
|
}
|
|
200
206
|
|
|
201
|
-
rollbackRetaining(callback) {
|
|
207
|
+
rollbackRetaining(callback?: (err?: any) => void): void {
|
|
202
208
|
this.connection.rollbackRetaining(this, callback);
|
|
203
209
|
}
|
|
204
210
|
}
|
|
205
211
|
|
|
206
|
-
|
|
212
|
+
export = Transaction;
|