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
@@ -12,7 +12,7 @@ const defaultOptions = {
12
12
  DEFAULT_LOWERCASE_KEYS : false,
13
13
  DEFAULT_PAGE_SIZE : 4096,
14
14
  DEFAULT_SVC_NAME : 'service_mgr',
15
- DEFAULT_ENCODING : 'UTF8',
15
+ DEFAULT_ENCODING : 'utf8' as BufferEncoding,
16
16
  DEFAULT_FETCHSIZE : 200,
17
17
  };
18
18
 
@@ -121,6 +121,9 @@ const op = {
121
121
  op_crypt_key_callback : 97,
122
122
  op_cond_accept : 98, // Server accepts connection, returns some data to client
123
123
  // and asks client to continue authentication before attach call
124
+ op_fetch_scroll : 112,
125
+ op_info_cursor : 113,
126
+ op_inline_blob : 114,
124
127
  };
125
128
 
126
129
  const dsql = {
@@ -129,6 +132,15 @@ const dsql = {
129
132
  DSQL_unprepare : 4, // >: 2.5
130
133
  };
131
134
 
135
+ const fetchOp = {
136
+ fetch_next : 0,
137
+ fetch_prior : 1,
138
+ fetch_first : 2,
139
+ fetch_last : 3,
140
+ fetch_absolute : 4,
141
+ fetch_relative : 5,
142
+ };
143
+
132
144
  /***********************/
133
145
  /* ISC Error Codes */
134
146
  /***********************/
@@ -194,6 +206,18 @@ const protocol = {
194
206
 
195
207
  // Protocol 17 continues Firebird 4.0 enhancements.
196
208
  PROTOCOL_VERSION17 : (FB_PROTOCOL_FLAG | 17),
209
+
210
+ // Protocol 18 includes support for fetches from scrollable cursors.
211
+ // Introduced in Firebird 5.0.
212
+ PROTOCOL_VERSION18 : (FB_PROTOCOL_FLAG | 18),
213
+
214
+ // Protocol 19 includes support for Inline BLOBs.
215
+ // Introduced in Firebird 5.0.3.
216
+ PROTOCOL_VERSION19 : (FB_PROTOCOL_FLAG | 19),
217
+
218
+ // Protocol 20 includes support for SQL Schemas and Named Arguments.
219
+ // Introduced in Firebird 6.0.
220
+ PROTOCOL_VERSION20 : (FB_PROTOCOL_FLAG | 20),
197
221
  };
198
222
 
199
223
  // Protocols types (accept_type)
@@ -215,17 +239,32 @@ const SUPPORTED_PROTOCOL = [
215
239
  [protocol.PROTOCOL_VERSION15, connect.ARCHITECTURE_GENERIC, acceptType.ptype_lazy_send, acceptType.ptype_lazy_send, 6],
216
240
  [protocol.PROTOCOL_VERSION16, connect.ARCHITECTURE_GENERIC, acceptType.ptype_lazy_send, acceptType.ptype_lazy_send, 7],
217
241
  [protocol.PROTOCOL_VERSION17, connect.ARCHITECTURE_GENERIC, acceptType.ptype_lazy_send, acceptType.ptype_lazy_send, 8],
242
+ [protocol.PROTOCOL_VERSION18, connect.ARCHITECTURE_GENERIC, acceptType.ptype_lazy_send, acceptType.ptype_lazy_send, 9],
243
+ [protocol.PROTOCOL_VERSION19, connect.ARCHITECTURE_GENERIC, acceptType.ptype_lazy_send, acceptType.ptype_lazy_send, 10]
218
244
  ];
219
245
 
220
246
  const authPlugin = {
221
247
  AUTH_PLUGIN_LEGACY : 'Legacy_Auth',
222
248
  AUTH_PLUGIN_SRP : 'Srp',
223
249
  AUTH_PLUGIN_SRP256 : 'Srp256',
250
+ AUTH_PLUGIN_SRP384 : 'Srp384',
251
+ AUTH_PLUGIN_SRP512 : 'Srp512',
224
252
  };
225
253
 
226
254
  const authOptions = {
227
- AUTH_PLUGIN_LIST : [authPlugin.AUTH_PLUGIN_SRP256, authPlugin.AUTH_PLUGIN_SRP, authPlugin.AUTH_PLUGIN_LEGACY],
228
- AUTH_PLUGIN_SRP_LIST : [authPlugin.AUTH_PLUGIN_SRP256, authPlugin.AUTH_PLUGIN_SRP],
255
+ AUTH_PLUGIN_LIST : [
256
+ authPlugin.AUTH_PLUGIN_SRP512,
257
+ authPlugin.AUTH_PLUGIN_SRP384,
258
+ authPlugin.AUTH_PLUGIN_SRP256,
259
+ authPlugin.AUTH_PLUGIN_SRP,
260
+ authPlugin.AUTH_PLUGIN_LEGACY
261
+ ],
262
+ AUTH_PLUGIN_SRP_LIST : [
263
+ authPlugin.AUTH_PLUGIN_SRP512,
264
+ authPlugin.AUTH_PLUGIN_SRP384,
265
+ authPlugin.AUTH_PLUGIN_SRP256,
266
+ authPlugin.AUTH_PLUGIN_SRP
267
+ ],
229
268
  LEGACY_AUTH_SALT : '9z',
230
269
  WIRE_CRYPT_DISABLE : 0,
231
270
  WIRE_CRYPT_ENABLE : 1,
@@ -258,10 +297,6 @@ const sqlType = {
258
297
  SQL_DEC34 : 32762, // >= 4.0 - DECFLOAT(34) - 16 bytes, IEEE 754 Decimal128
259
298
  SQL_BOOLEAN : 32764, // >: 3.0
260
299
  SQL_NULL : 32766, // >= 2.5
261
- SQL_TIMESTAMP_TZ_EX : 32748,
262
- SQL_TIME_TZ_EX : 32750,
263
- SQL_TIMESTAMP_TZ : 32754,
264
- SQL_TIME_TZ : 32756,
265
300
  };
266
301
 
267
302
  const blobType = {
@@ -293,7 +328,6 @@ const blr = {
293
328
  blr_int128 : 26, // >: 4.0
294
329
  blr_dec64 : 24, // >= 4.0 - DECFLOAT(16), IEEE 754 Decimal64
295
330
  blr_dec128 : 25, // >= 4.0 - DECFLOAT(34), IEEE 754 Decimal128
296
- blr_timestamp_tz : 28, // >= 4.0 - timestamp with time zone
297
331
  blr_time_tz : 29, // >= 4.0 - time with time zone
298
332
  blr_blob2 : 17, // >: 2.0
299
333
  blr_domain_name : 18, // >: 2.1
@@ -409,6 +443,16 @@ const dpb = {
409
443
  isc_dpb_reset_icu : 89,
410
444
  isc_dpb_map_attach : 90,
411
445
  isc_dpb_session_time_zone : 91,
446
+ isc_dpb_parallel_workers : 92,
447
+ isc_dpb_max_inline_blob_size : 93,
448
+ // Firebird 6.0 SQL Schema parameters
449
+ // isc_dpb_search_path sets the comma-separated ordered list of schema
450
+ // names to search for unqualified object names (similar to PostgreSQL
451
+ // search_path). Available on Protocol 20+ (Firebird 6.0).
452
+ isc_dpb_search_path : 94,
453
+ // isc_dpb_default_schema overrides the session's current schema
454
+ // (CURRENT_SCHEMA) at connection time. Available on Protocol 20+.
455
+ isc_dpb_default_schema : 95,
412
456
  };
413
457
 
414
458
  const cnct = {
@@ -438,6 +482,7 @@ const common = {
438
482
  isc_info_data_not_ready : 4,
439
483
  isc_info_length : 126,
440
484
  isc_info_flag_end : 127,
485
+ fb_info_parallel_workers : 149,
441
486
  };
442
487
 
443
488
  /*************************************/
@@ -504,6 +549,10 @@ const sqlInfo = {
504
549
  isc_info_sql_batch_fetch : 24,
505
550
  isc_info_sql_relation_alias : 25, // >: 2.0
506
551
  isc_info_sql_explain_plan : 26, // >= 3.0
552
+ // Firebird 6.0 (Protocol 20): schema name of the source relation for a
553
+ // result-set column. The server only sends this item when the column is
554
+ // drawn from a named schema (i.e. not the legacy PUBLIC or unqualified).
555
+ isc_info_sql_relation_schema : 33,
507
556
  };
508
557
 
509
558
  const statementInfo = {
@@ -523,6 +572,8 @@ const statementInfo = {
523
572
  isc_info_sql_stmt_savepoint : 14,
524
573
  };
525
574
 
575
+ // Standard DESCRIBE request (Protocols 10–19). Sent in op_prepare_statement
576
+ // to ask the server for result-set and input column metadata.
526
577
  const DESCRIBE = [
527
578
  sqlInfo.isc_info_sql_stmt_type,
528
579
  sqlInfo.isc_info_sql_select,
@@ -547,6 +598,34 @@ const DESCRIBE = [
547
598
  sqlInfo.isc_info_sql_describe_end
548
599
  ];
549
600
 
601
+ // Extended DESCRIBE request for Protocol 20+ (Firebird 6.0).
602
+ // Adds isc_info_sql_relation_schema to capture the schema name alongside the
603
+ // relation name for each output column.
604
+ const DESCRIBE_WITH_SCHEMA = [
605
+ sqlInfo.isc_info_sql_stmt_type,
606
+ sqlInfo.isc_info_sql_select,
607
+ sqlInfo.isc_info_sql_describe_vars,
608
+ sqlInfo.isc_info_sql_sqlda_seq,
609
+ sqlInfo.isc_info_sql_type,
610
+ sqlInfo.isc_info_sql_sub_type,
611
+ sqlInfo.isc_info_sql_scale,
612
+ sqlInfo.isc_info_sql_length,
613
+ sqlInfo.isc_info_sql_field,
614
+ sqlInfo.isc_info_sql_relation,
615
+ sqlInfo.isc_info_sql_relation_schema, // FB 6.0: schema of source relation
616
+ //isc_info_sql_owner,
617
+ sqlInfo.isc_info_sql_alias,
618
+ sqlInfo.isc_info_sql_describe_end,
619
+ sqlInfo.isc_info_sql_bind,
620
+ sqlInfo.isc_info_sql_describe_vars,
621
+ sqlInfo.isc_info_sql_sqlda_seq,
622
+ sqlInfo.isc_info_sql_type,
623
+ sqlInfo.isc_info_sql_sub_type,
624
+ sqlInfo.isc_info_sql_scale,
625
+ sqlInfo.isc_info_sql_length,
626
+ sqlInfo.isc_info_sql_describe_end
627
+ ];
628
+
550
629
  /***********************/
551
630
  /* ISC Services */
552
631
  /***********************/
@@ -673,6 +752,7 @@ const serviceBackup = {
673
752
  isc_spb_bkp_convert : 0x40,
674
753
  isc_spb_bkp_expand : 0x80,
675
754
  isc_spb_bkp_no_triggers : 0x8000,
755
+ isc_spb_bkp_parallel_workers : 21,
676
756
  // nbackup
677
757
  isc_spb_nbk_level : 5,
678
758
  isc_spb_nbk_file : 6,
@@ -686,6 +766,7 @@ const serviceRestore = {
686
766
  isc_spb_res_page_size : 10,
687
767
  isc_spb_res_length : 11,
688
768
  isc_spb_res_access_mode : 12,
769
+ isc_spb_res_parallel_workers : 21,
689
770
  isc_spb_res_fix_fss_data : 13,
690
771
  isc_spb_res_fix_fss_metadata : 14,
691
772
  isc_spb_res_am_readonly : service.isc_spb_prp_am_readonly,
@@ -728,6 +809,7 @@ const serviceRepair = {
728
809
  isc_spb_rpr_kill_shadows : 0x40,
729
810
  isc_spb_rpr_full : 0x80,
730
811
  isc_spb_rpr_icu : 0x0800,
812
+ isc_spb_rpr_par_workers : 52,
731
813
  };
732
814
 
733
815
  /* · Security Service ·*/
@@ -760,6 +842,7 @@ const serviceStatistics = {
760
842
  isc_spb_sts_record_versions : 0x20,
761
843
  isc_spb_sts_table : 0x40,
762
844
  isc_spb_sts_nocreation : 0x80,
845
+ isc_spb_sts_encryption : 0x100,
763
846
  };
764
847
 
765
848
  /* Trace Service */
@@ -769,7 +852,7 @@ const serviceTrace = {
769
852
  isc_spb_trc_cfg : 3,
770
853
  };
771
854
 
772
- module.exports = Object.freeze({
855
+ const Const = Object.freeze({
773
856
  ...acceptType,
774
857
  ...authPlugin,
775
858
  ...authOptions,
@@ -781,8 +864,10 @@ module.exports = Object.freeze({
781
864
  ...connect,
782
865
  ...defaultOptions,
783
866
  DESCRIBE,
867
+ DESCRIBE_WITH_SCHEMA,
784
868
  ...dpb,
785
869
  ...dsql,
870
+ ...fetchOp,
786
871
  ...int,
787
872
  ...iscAction,
788
873
  ...iscError,
@@ -805,3 +890,5 @@ module.exports = Object.freeze({
805
890
  ...tpb,
806
891
  ...transactionIsolation,
807
892
  });
893
+
894
+ export = Const;
@@ -1,9 +1,9 @@
1
- const Events = require('events');
2
- const { doError } = require('../callback');
3
- const { escape } = require('../utils');
4
- const Const = require('./const');
5
- const EventConnection = require('./eventConnection');
6
- const FbEventManager = require('./fbEventManager');
1
+ import Events from 'events';
2
+ import { doError } from '../callback';
3
+ import { escape } from '../utils';
4
+ import Const from './const';
5
+ import EventConnection from './eventConnection';
6
+ import FbEventManager from './fbEventManager';
7
7
 
8
8
  /***************************************
9
9
  *
@@ -40,7 +40,7 @@ const FbEventManager = require('./fbEventManager');
40
40
  *
41
41
  ***************************************/
42
42
 
43
- function readblob(blob, callback) {
43
+ function readblob(blob: any, callback: (err: any, data?: any) => void): void {
44
44
  if (blob === undefined || blob === null) {
45
45
  callback(null, blob);
46
46
  return;
@@ -51,7 +51,7 @@ function readblob(blob, callback) {
51
51
  return;
52
52
  }
53
53
 
54
- blob(function(err, name, e) {
54
+ blob(function(err: any, name: any, e: any) {
55
55
  if (err) {
56
56
  callback(err);
57
57
  return;
@@ -62,10 +62,10 @@ function readblob(blob, callback) {
62
62
  return;
63
63
  }
64
64
 
65
- const chunks = [];
65
+ const chunks: Buffer[] = [];
66
66
  let chunksLength = 0;
67
67
 
68
- e.on('data', function(chunk) {
68
+ e.on('data', function(chunk: Buffer) {
69
69
  chunksLength += chunk.length;
70
70
  chunks.push(chunk);
71
71
  });
@@ -74,20 +74,20 @@ function readblob(blob, callback) {
74
74
  callback(null, Buffer.concat(chunks, chunksLength));
75
75
  });
76
76
 
77
- e.on('error', function(streamErr) {
77
+ e.on('error', function(streamErr: any) {
78
78
  callback(streamErr);
79
79
  });
80
80
  });
81
81
  }
82
82
 
83
- function fetchBlobSyncRow(row, meta, callback) {
83
+ function fetchBlobSyncRow(row: any, meta: any[], callback: (err: any, row?: any) => void): void {
84
84
  if (!row || !meta || !meta.length) {
85
85
  callback(null, row);
86
86
  return;
87
87
  }
88
88
 
89
89
  const rowKeys = Object.keys(row);
90
- const blobColumns = [];
90
+ const blobColumns: string[] = [];
91
91
 
92
92
  for (let i = 0; i < meta.length; i++) {
93
93
  if (meta[i] && meta[i].type === Const.SQL_BLOB && rowKeys[i] !== undefined) {
@@ -101,10 +101,10 @@ function fetchBlobSyncRow(row, meta, callback) {
101
101
  }
102
102
 
103
103
  let pending = blobColumns.length;
104
- let blobErr;
104
+ let blobErr: any;
105
105
 
106
106
  blobColumns.forEach(function(columnName) {
107
- readblob(row[columnName], function(err, data) {
107
+ readblob(row[columnName], function(err: any, data: any) {
108
108
  if (err && !blobErr) {
109
109
  blobErr = err;
110
110
  }
@@ -118,18 +118,21 @@ function fetchBlobSyncRow(row, meta, callback) {
118
118
  }
119
119
 
120
120
  class Database extends Events.EventEmitter {
121
- constructor(connection) {
121
+ connection: any;
122
+ eventid: number;
123
+
124
+ constructor(connection: any) {
122
125
  super();
123
126
  this.connection = connection;
124
127
  connection.db = this;
125
128
  this.eventid = 1;
126
129
  }
127
130
 
128
- escape(value) {
131
+ escape(value: any): string {
129
132
  return escape(value, this.connection.accept.protocolVersion);
130
133
  }
131
134
 
132
- detach(callback, force) {
135
+ detach(callback?: (err?: any, obj?: any) => void, force?: boolean): this {
133
136
  var self = this;
134
137
 
135
138
  if (!force && self.connection._pending.length > 0) {
@@ -139,7 +142,7 @@ class Database extends Events.EventEmitter {
139
142
  }
140
143
 
141
144
  if (self.connection._pooled === false) {
142
- self.connection.detach(function (err, obj) {
145
+ self.connection.detach(function (err: any, obj: any) {
143
146
 
144
147
  self.connection.disconnect();
145
148
  self.emit('detach', false);
@@ -157,31 +160,31 @@ class Database extends Events.EventEmitter {
157
160
  return self;
158
161
  }
159
162
 
160
- transaction(options, callback) {
163
+ transaction(options: any, callback?: (err: any, transaction?: any) => void): this {
161
164
  return this.startTransaction(options, callback);
162
165
  }
163
166
 
164
- startTransaction(options, callback) {
167
+ startTransaction(options: any, callback?: (err: any, transaction?: any) => void): this {
165
168
  this.connection.startTransaction(options, callback);
166
169
  return this;
167
170
  }
168
171
 
169
- newStatement(query, callback) {
170
- this.startTransaction(function(err, transaction) {
172
+ newStatement(query: string, callback: (err: any, statement?: any) => void): this {
173
+ this.startTransaction(function(err: any, transaction: any) {
171
174
 
172
175
  if (err) {
173
176
  callback(err);
174
177
  return;
175
178
  }
176
179
 
177
- transaction.newStatement(query, function(err, statement) {
180
+ transaction.newStatement(query, function(err: any, statement: any) {
178
181
 
179
182
  if (err) {
180
183
  callback(err);
181
184
  return;
182
185
  }
183
186
 
184
- transaction.commit(function(err) {
187
+ transaction.commit(function(err: any) {
185
188
  callback(err, statement);
186
189
  });
187
190
  });
@@ -190,7 +193,7 @@ class Database extends Events.EventEmitter {
190
193
  return this;
191
194
  }
192
195
 
193
- execute(query, params, callback, options) {
196
+ execute(query: string, params?: any, callback?: any, options?: any): this {
194
197
  if (params instanceof Function) {
195
198
  options = callback;
196
199
  callback = params;
@@ -199,14 +202,14 @@ class Database extends Events.EventEmitter {
199
202
 
200
203
  var self = this;
201
204
 
202
- self.connection.startTransaction(function(err, transaction) {
205
+ self.connection.startTransaction(function(err: any, transaction: any) {
203
206
 
204
207
  if (err) {
205
208
  doError(err, callback);
206
209
  return;
207
210
  }
208
211
 
209
- transaction.execute(query, params, function(err, result, meta, isSelect) {
212
+ transaction.execute(query, params, function(err: any, result: any, meta: any, isSelect: boolean) {
210
213
 
211
214
  if (err) {
212
215
  transaction.rollback(function() {
@@ -215,7 +218,7 @@ class Database extends Events.EventEmitter {
215
218
  return;
216
219
  }
217
220
 
218
- transaction.commit(function(err) {
221
+ transaction.commit(function(err: any) {
219
222
  if (callback)
220
223
  callback(err, result, meta, isSelect);
221
224
  });
@@ -226,7 +229,7 @@ class Database extends Events.EventEmitter {
226
229
  return self;
227
230
  }
228
231
 
229
- sequentially(query, params, on, callback, options = {}) {
232
+ sequentially(query: string, params?: any, on?: any, callback?: any, options: any = {}): this {
230
233
  if (params instanceof Function) {
231
234
  options = callback;
232
235
  callback = on;
@@ -244,9 +247,9 @@ class Database extends Events.EventEmitter {
244
247
  }
245
248
 
246
249
  var self = this;
247
- var _on = function(row, i, meta, next) {
250
+ var _on = function(row: any, i: number, meta: any, next: (err?: any) => void) {
248
251
  var done = false;
249
- var finish = function(err) {
252
+ var finish = function(err?: any) {
250
253
  if (done) {
251
254
  return;
252
255
  }
@@ -254,7 +257,7 @@ class Database extends Events.EventEmitter {
254
257
  next(err);
255
258
  };
256
259
 
257
- fetchBlobSyncRow(row, meta, function(blobErr) {
260
+ fetchBlobSyncRow(row, meta, function(blobErr: any) {
258
261
  if (blobErr) {
259
262
  finish(blobErr);
260
263
  return;
@@ -297,7 +300,7 @@ class Database extends Events.EventEmitter {
297
300
  return self;
298
301
  }
299
302
 
300
- query(query, params, callback, options = {}) {
303
+ query(query: string, params?: any, callback?: any, options: any = {}): this {
301
304
  if (params instanceof Function) {
302
305
  options = callback || {};
303
306
  callback = params;
@@ -315,16 +318,17 @@ class Database extends Events.EventEmitter {
315
318
  return self;
316
319
  }
317
320
 
318
- drop(callback) {
321
+ drop(callback?: (err?: any) => void): void {
319
322
  return this.connection.dropDatabase(callback);
320
323
  }
321
324
 
322
- attachEvent(callback) {
325
+ attachEvent(callback: (err: any, evt?: any) => void): this {
323
326
  var self = this;
327
+ const eventid = self.eventid++;
324
328
  if (process.env.FIREBIRD_DEBUG) {
325
- console.log('[fb-debug] Database.attachEvent: calling auxConnection, eventid=%d queue=%d', self.eventid, self.connection._queue.length);
329
+ console.log('[fb-debug] Database.attachEvent: calling auxConnection, eventid=%d queue=%d', eventid, self.connection._queue.length);
326
330
  }
327
- this.connection.auxConnection(function (err, socket_info) {
331
+ this.connection.auxConnection(eventid, function (err: any, socket_info: any) {
328
332
 
329
333
  if (err) {
330
334
  if (process.env.FIREBIRD_DEBUG) {
@@ -343,7 +347,7 @@ class Database extends Events.EventEmitter {
343
347
  : socket_info.host;
344
348
 
345
349
  const eventConnection = new EventConnection(
346
- host, socket_info.port, function(err) {
350
+ host, socket_info.port, function(err?: any) {
347
351
  if (err) {
348
352
  if (process.env.FIREBIRD_DEBUG) {
349
353
  console.log('[fb-debug] Database.attachEvent: EventConnection error:', err.message);
@@ -353,10 +357,10 @@ class Database extends Events.EventEmitter {
353
357
  }
354
358
 
355
359
  if (process.env.FIREBIRD_DEBUG) {
356
- console.log('[fb-debug] Database.attachEvent: EventConnection connected, creating FbEventManager eventid=%d', self.eventid);
360
+ console.log('[fb-debug] Database.attachEvent: EventConnection connected, creating FbEventManager eventid=%d', eventid);
357
361
  }
358
362
 
359
- const evt = new FbEventManager(self, eventConnection, self.eventid++, function (err) {
363
+ const evt = new FbEventManager(self, eventConnection, eventid, function (err: any) {
360
364
  if (err) {
361
365
  doError(err, callback);
362
366
  return;
@@ -365,14 +369,76 @@ class Database extends Events.EventEmitter {
365
369
  if (process.env.FIREBIRD_DEBUG) {
366
370
  console.log('[fb-debug] Database.attachEvent: FbEventManager ready, eventid=%d', evt.eventid);
367
371
  }
368
-
369
- callback(err, evt);
372
+ callback(err, evt);
370
373
  });
371
374
  }, self);
372
375
  });
373
376
 
374
377
  return this;
375
378
  }
379
+
380
+ /**
381
+ * Create a physical tablespace.
382
+ * Supported in Firebird 6.0+ (Protocol 20+).
383
+ *
384
+ * @param {string} name - The name of the tablespace.
385
+ * @param {string} filePath - The physical file path for the tablespace.
386
+ * @param {function} [callback] - Asynchronous completion callback.
387
+ * @returns {Database}
388
+ */
389
+ createTablespace(name: string, filePath: string, callback?: any): this {
390
+ const sql = `CREATE TABLESPACE ${name} FILE '${filePath}'`;
391
+ return this.execute(sql, [], callback);
392
+ }
393
+
394
+ /**
395
+ * Alter an existing tablespace physical location.
396
+ * Supported in Firebird 6.0+ (Protocol 20+).
397
+ *
398
+ * @param {string} name - The name of the tablespace.
399
+ * @param {string} filePath - The new physical file path.
400
+ * @param {function} [callback] - Asynchronous completion callback.
401
+ * @returns {Database}
402
+ */
403
+ alterTablespace(name: string, filePath: string, callback?: any): this {
404
+ const sql = `ALTER TABLESPACE ${name} SET FILE TO '${filePath}'`;
405
+ return this.execute(sql, [], callback);
406
+ }
407
+
408
+ /**
409
+ * Drop a tablespace.
410
+ * Supported in Firebird 6.0+ (Protocol 20+).
411
+ *
412
+ * @param {string} name - The name of the tablespace.
413
+ * @param {function} [callback] - Asynchronous completion callback.
414
+ * @returns {Database}
415
+ */
416
+ dropTablespace(name: string, callback?: any): this {
417
+ const sql = `DROP TABLESPACE ${name}`;
418
+ return this.execute(sql, [], callback);
419
+ }
420
+
421
+ /**
422
+ * Create a schema/namespace. Can optionally partition/map the namespace
423
+ * to a physical tablespace.
424
+ * Supported in Firebird 6.0+ (Protocol 20+).
425
+ *
426
+ * @param {string} schemaName - The name of the schema.
427
+ * @param {string} [tablespaceName] - Optional tablespace name to bind this schema namespace.
428
+ * @param {function} [callback] - Asynchronous completion callback.
429
+ * @returns {Database}
430
+ */
431
+ createSchema(schemaName: string, tablespaceName?: string | ((err?: any) => void), callback?: any): this {
432
+ if (typeof tablespaceName === 'function') {
433
+ callback = tablespaceName;
434
+ tablespaceName = undefined;
435
+ }
436
+ let sql = `CREATE SCHEMA ${schemaName}`;
437
+ if (tablespaceName) {
438
+ sql += ` TABLESPACE ${tablespaceName}`;
439
+ }
440
+ return this.execute(sql, [], callback);
441
+ }
376
442
  }
377
443
 
378
- module.exports = Database;
444
+ export = Database;
@@ -1,10 +1,20 @@
1
- const net = require('net');
2
- const { XdrReader } = require('./serialize');
1
+ import net from 'net';
2
+ import { XdrReader } from './serialize';
3
+ import Const from './const';
4
+
3
5
  const DEFAULT_ENCODING = 'utf8';
4
- const Const = require('./const');
5
6
 
6
7
  class EventConnection {
7
- constructor(host, port, callback, db) {
8
+ db: any;
9
+ emgr: any;
10
+ _isClosed: boolean;
11
+ _isOpened: boolean;
12
+ _socket: net.Socket;
13
+ _xdr?: XdrReader;
14
+ error: any;
15
+ eventcallback: ((err: any, ret?: any) => void) | null;
16
+
17
+ constructor(host: string, port: number, callback: (() => void) | undefined, db: any) {
8
18
  var self = this;
9
19
  this.db = db;
10
20
  this.emgr = null;
@@ -16,7 +26,7 @@ class EventConnection {
16
26
  this.eventcallback;
17
27
  }
18
28
 
19
- _bind_events(host, port, callback) {
29
+ _bind_events(host: string, port: number, callback?: () => void): void {
20
30
  var self = this;
21
31
 
22
32
  self._socket.on('close', function () {
@@ -36,24 +46,24 @@ class EventConnection {
36
46
  callback();
37
47
  });
38
48
 
39
- self._socket.on('data', function (data) {
40
- var xdr, buf;
49
+ self._socket.on('data', function (data: Buffer) {
50
+ var xdr: XdrReader, buf: Buffer | undefined;
41
51
 
42
52
  if (!self._xdr) {
43
53
  xdr = new XdrReader(data);
44
54
  } else {
45
55
  xdr = self._xdr;
46
56
  delete (self._xdr);
47
- buf = Buffer.from(data.length + xdr.buffer.length);
57
+ buf = Buffer.alloc(data.length + xdr.buffer.length);
48
58
  xdr.buffer.copy(buf);
49
59
  data.copy(buf, xdr.buffer.length);
50
60
  xdr.buffer = buf;
51
61
  }
52
62
 
53
63
  try {
54
- var item, op;
64
+
55
65
  var op_pos = xdr.pos;
56
- var tmp_event;
66
+ var tmp_event: Record<string, number>;
57
67
  while (xdr.pos < xdr.buffer.length) {
58
68
  do {
59
69
  var r = xdr.readInt();
@@ -62,7 +72,7 @@ class EventConnection {
62
72
  switch (r) {
63
73
  case Const.op_event:
64
74
  xdr.readInt(); // db handle
65
- var buf = xdr.readArray();
75
+ buf = xdr.readArray();
66
76
  // first byte is always set to 1
67
77
  tmp_event = {};
68
78
  var lst_event = [];
@@ -83,8 +93,13 @@ class EventConnection {
83
93
  xdr.readInt64(); // ignore AST INFO
84
94
  var event_id = xdr.readInt();
85
95
  // set the new count in global event hash
96
+ // Only update events that are still registered; do not
97
+ // re-add events that unregisterEvent() has deleted, since
98
+ // that would cause subscribe() to re-subscribe for them.
86
99
  for (var evt in tmp_event) {
87
- self.emgr.events[evt] = tmp_event[evt];
100
+ if (Object.prototype.hasOwnProperty.call(self.emgr.events, evt)) {
101
+ self.emgr.events[evt] = tmp_event[evt];
102
+ }
88
103
  }
89
104
  if (self.eventcallback)
90
105
  self.eventcallback(null, { eventid: event_id, events: lst_event });
@@ -106,7 +121,7 @@ class EventConnection {
106
121
  })
107
122
  }
108
123
 
109
- throwClosed(callback) {
124
+ throwClosed(callback?: (err: any) => void): this {
110
125
  var err = new Error('Event Connection is closed.');
111
126
  this.db.emit('error', err);
112
127
  if (callback)
@@ -115,4 +130,4 @@ class EventConnection {
115
130
  }
116
131
  }
117
132
 
118
- module.exports = EventConnection;
133
+ export = EventConnection;