node-firebird 2.3.4 → 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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-firebird",
3
- "version": "2.3.4",
3
+ "version": "2.4.0",
4
4
  "description": "Pure JavaScript and Asynchronous Firebird client for Node.js.",
5
5
  "keywords": [
6
6
  "firebird",
@@ -21,18 +21,26 @@
21
21
  "Popa Marius Adrian <mapopa@gmail.com>",
22
22
  "Peter Širka <petersirka@gmail.com>"
23
23
  ],
24
- "main": "./lib",
24
+ "main": "./lib/index.js",
25
25
  "types": "./lib/index.d.ts",
26
+ "files": [
27
+ "lib",
28
+ "src"
29
+ ],
26
30
  "license": "MPL-2.0",
27
31
  "scripts": {
32
+ "build": "tsc -p tsconfig.json && node -e \"require('fs').copyFileSync('src/firebird.msg','lib/firebird.msg')\"",
33
+ "prepare": "npm run build",
34
+ "pretest": "npm run build",
35
+ "typecheck": "tsc -p tsconfig.test.json",
28
36
  "test": "vitest run",
29
37
  "lint": "oxlint"
30
38
  },
31
- "dependencies": {
32
- },
33
39
  "devDependencies": {
40
+ "@types/node": "^26.1.1",
34
41
  "coveralls-next": "^6.0.1",
35
42
  "oxlint": "^0.15.11",
43
+ "typescript": "^7.0.2",
36
44
  "vitest": "^4.0.18"
37
45
  }
38
46
  }
@@ -0,0 +1,55 @@
1
+ /** A single Firebird status-vector entry (gds code plus optional parameters). */
2
+ export interface FbStatusItem {
3
+ gdscode: number;
4
+ params?: any[];
5
+ }
6
+
7
+ /** Error-like object produced by the wire protocol layer. */
8
+ export interface FbStatusObject {
9
+ status?: FbStatusItem[];
10
+ message?: string;
11
+ sqlcode?: number;
12
+ }
13
+
14
+ /** An Error enriched with the Firebird gds code and its parameters. */
15
+ export interface FbError extends Error {
16
+ gdscode?: number;
17
+ gdsparams?: any[];
18
+ }
19
+
20
+ export type SimpleCallback = (err?: any) => void;
21
+ export type Callback<T = any> = (err?: any, result?: T) => void;
22
+
23
+ export function doError(obj: any, callback?: (...args: any[]) => void): void {
24
+ if (callback)
25
+ callback(obj)
26
+ }
27
+
28
+ function isError(obj: any): obj is FbStatusObject {
29
+ return Boolean(
30
+ obj != null && typeof obj === "object" && !Array.isArray(obj) && obj.status
31
+ );
32
+ }
33
+
34
+ export function doCallback<T>(obj: T, callback?: Callback<T>): void {
35
+
36
+ if (!callback)
37
+ return;
38
+
39
+ if (obj instanceof Error) {
40
+ callback(obj);
41
+ return;
42
+ }
43
+
44
+ if (isError(obj)) {
45
+ var error: FbError = new Error(obj.message);
46
+ var status = obj.status && obj.status.length && obj.status[0] || {} as FbStatusItem;
47
+ error.gdscode = status.gdscode; // main error gds code
48
+ error.gdsparams = status.params; // parameters (constraint name, table, etc.)
49
+ callback(error);
50
+ return;
51
+ }
52
+
53
+ callback(undefined, obj);
54
+
55
+ }
@@ -1522,10 +1522,10 @@ const GDSCode = {
1522
1522
  FBSVCMGR_SWITCH_UNKNOWN: 336986118
1523
1523
  };
1524
1524
 
1525
- +function makeEnum(_enum) {
1526
- for (k in _enum)
1525
+ +function makeEnum(_enum: any) {
1526
+ for (var k in _enum)
1527
1527
  _enum[_enum[k]] = k;
1528
1528
  Object.freeze(_enum);
1529
1529
  }(GDSCode);
1530
1530
 
1531
- module.exports = { GDSCode };
1531
+ export { GDSCode };
@@ -41,7 +41,7 @@ const DECIMAL128_SNAN_HIGH = 0x7E00000000000000n;
41
41
  * @param {number|string|BigInt} value - The value to encode
42
42
  * @returns {Buffer} - 8-byte buffer containing the Decimal64 encoding
43
43
  */
44
- function encodeDecimal64(value) {
44
+ export function encodeDecimal64(value: number | string | bigint | Buffer | null | undefined): Buffer {
45
45
  // Handle special cases
46
46
  if (value === null || value === undefined) {
47
47
  return Buffer.alloc(8);
@@ -156,7 +156,7 @@ function encodeDecimal64(value) {
156
156
  * @param {Buffer} buffer - 8-byte buffer containing the Decimal64 encoding
157
157
  * @returns {string|number} - Decoded value as string or special value
158
158
  */
159
- function decodeDecimal64(buffer) {
159
+ export function decodeDecimal64(buffer: Buffer): string | number {
160
160
  if (buffer.length !== 8) {
161
161
  throw new Error('Decimal64 buffer must be 8 bytes');
162
162
  }
@@ -240,7 +240,7 @@ function decodeDecimal64(buffer) {
240
240
  * @param {number|string|BigInt} value - The value to encode
241
241
  * @returns {Buffer} - 16-byte buffer containing the Decimal128 encoding
242
242
  */
243
- function encodeDecimal128(value) {
243
+ export function encodeDecimal128(value: number | string | bigint | Buffer | null | undefined): Buffer {
244
244
  // Handle special cases
245
245
  if (value === null || value === undefined) {
246
246
  return Buffer.alloc(16);
@@ -366,7 +366,7 @@ function encodeDecimal128(value) {
366
366
  * @param {Buffer} buffer - 16-byte buffer containing the Decimal128 encoding
367
367
  * @returns {string|number} - Decoded value as string or special value
368
368
  */
369
- function decodeDecimal128(buffer) {
369
+ export function decodeDecimal128(buffer: Buffer): string | number {
370
370
  if (buffer.length !== 16) {
371
371
  throw new Error('Decimal128 buffer must be 16 bytes');
372
372
  }
@@ -454,7 +454,7 @@ function decodeDecimal128(buffer) {
454
454
  * @param {number} size - Buffer size in bytes
455
455
  * @returns {Buffer}
456
456
  */
457
- function bigIntToBuffer(value, size) {
457
+ function bigIntToBuffer(value: bigint, size: number): Buffer {
458
458
  const buf = Buffer.alloc(size);
459
459
  let val = BigInt(value.toString());
460
460
  for (let i = size - 1; i >= 0; i--) {
@@ -469,7 +469,7 @@ function bigIntToBuffer(value, size) {
469
469
  * @param {Buffer} buffer
470
470
  * @returns {BigInt}
471
471
  */
472
- function bufferToBigInt(buffer) {
472
+ function bufferToBigInt(buffer: Buffer): bigint {
473
473
  let result = 0n;
474
474
  for (let i = 0; i < buffer.length; i++) {
475
475
  result = (result << 8n) | BigInt(buffer[i]);
@@ -484,17 +484,10 @@ function bufferToBigInt(buffer) {
484
484
  * @param {number} offset
485
485
  * @param {number} size
486
486
  */
487
- function bufferWriteBigInt(buffer, value, offset, size) {
487
+ function bufferWriteBigInt(buffer: Buffer, value: bigint, offset: number, size: number): void {
488
488
  let val = BigInt(value.toString());
489
489
  for (let i = offset + size - 1; i >= offset; i--) {
490
490
  buffer[i] = Number(val & 0xFFn);
491
491
  val >>= 8n;
492
492
  }
493
493
  }
494
-
495
- module.exports = {
496
- encodeDecimal64,
497
- decodeDecimal64,
498
- encodeDecimal128,
499
- decodeDecimal128
500
- };
package/src/index.ts ADDED
@@ -0,0 +1,157 @@
1
+ import Const from './wire/const';
2
+ import { doError, doCallback } from './callback';
3
+ import Connection from './wire/connection';
4
+ import Pool from './pool';
5
+ import { escape as escapeValue } from './utils';
6
+ import type {
7
+ Options,
8
+ SvcMgrOptions,
9
+ DatabaseCallback,
10
+ ServiceManagerCallback,
11
+ SimpleCallback,
12
+ ConnectionPool,
13
+ } from './types';
14
+
15
+ export * from './types';
16
+ export { GDSCode } from './gdscodes';
17
+
18
+ if (typeof(setImmediate) === 'undefined') {
19
+ (global as any).setImmediate = function(cb: () => void) {
20
+ process.nextTick(cb);
21
+ };
22
+ }
23
+
24
+ export const AUTH_PLUGIN_LEGACY: string = Const.AUTH_PLUGIN_LEGACY;
25
+ export const AUTH_PLUGIN_SRP: string = Const.AUTH_PLUGIN_SRP;
26
+ export const AUTH_PLUGIN_SRP256: string = Const.AUTH_PLUGIN_SRP256;
27
+ export const AUTH_PLUGIN_SRP384: string = Const.AUTH_PLUGIN_SRP384;
28
+ export const AUTH_PLUGIN_SRP512: string = Const.AUTH_PLUGIN_SRP512;
29
+
30
+ export const WIRE_CRYPT_DISABLE: number = Const.WIRE_CRYPT_DISABLE;
31
+ export const WIRE_CRYPT_ENABLE: number = Const.WIRE_CRYPT_ENABLE;
32
+
33
+ /** A transaction sees changes done by uncommitted transactions. */
34
+ export const ISOLATION_READ_UNCOMMITTED: number[] = Const.ISOLATION_READ_UNCOMMITTED;
35
+ /** A transaction sees only data committed before the statement has been executed. */
36
+ export const ISOLATION_READ_COMMITTED: number[] = Const.ISOLATION_READ_COMMITTED;
37
+ /** A transaction sees during its lifetime only data committed before the transaction has been started. */
38
+ export const ISOLATION_REPEATABLE_READ: number[] = Const.ISOLATION_REPEATABLE_READ;
39
+ /**
40
+ * This is the strictest isolation level, which enforces transaction serialization.
41
+ * Data accessed in the context of a serializable transaction cannot be accessed by any other transaction.
42
+ */
43
+ export const ISOLATION_SERIALIZABLE: number[] = Const.ISOLATION_SERIALIZABLE;
44
+ export const ISOLATION_READ_COMMITTED_READ_ONLY: number[] = Const.ISOLATION_READ_COMMITTED_READ_ONLY;
45
+
46
+ export const escape = escapeValue;
47
+
48
+ /**
49
+ * The most recent Connection created by attach()/create()/attachOrCreate().
50
+ * Kept for backwards compatibility with the previous CommonJS module where
51
+ * the connection was stored on the module object itself.
52
+ */
53
+ export let connection: Connection | undefined;
54
+
55
+ export function attach(options: Options, callback: DatabaseCallback): void;
56
+ export function attach(options: SvcMgrOptions, callback: ServiceManagerCallback): void;
57
+ export function attach(options: any, callback: any): void {
58
+ var host = options.host || Const.DEFAULT_HOST;
59
+ var port = options.port || Const.DEFAULT_PORT;
60
+ var manager = options.manager || false;
61
+ var cnx = connection = new Connection(host, port, function(err: any) {
62
+
63
+ if (err) {
64
+ doError(err, callback);
65
+ return;
66
+ }
67
+
68
+ cnx.connect(options, function(err: any) {
69
+ if (err) {
70
+ doError(err, callback);
71
+ } else {
72
+ if (manager)
73
+ cnx.svcattach(options, callback);
74
+ else
75
+ cnx.attach(options, callback);
76
+ }
77
+ });
78
+
79
+ }, options);
80
+ }
81
+
82
+ export function drop(options: Options, callback: SimpleCallback): void {
83
+ attach(options, function(err: any, db: any) {
84
+ if (err) {
85
+ callback({ error: err, message: "Drop error" });
86
+ return;
87
+ }
88
+
89
+ db.drop(callback);
90
+ });
91
+ }
92
+
93
+ export function create(options: Options, callback: DatabaseCallback): void {
94
+ var host = options.host || Const.DEFAULT_HOST;
95
+ var port = options.port || Const.DEFAULT_PORT;
96
+ var cnx = connection = new Connection(host, port, function(err: any) {
97
+
98
+ var self = cnx;
99
+
100
+ if (err) {
101
+ callback({ error: err, message: "Connect error" }, undefined as any);
102
+ return;
103
+ }
104
+
105
+ cnx.connect(options, function(err: any) {
106
+ if (err) {
107
+ if (self.db) self.db.emit('error', err);
108
+ doError(err, callback);
109
+ return;
110
+ }
111
+
112
+ cnx.createDatabase(options, callback);
113
+ });
114
+ }, options);
115
+ }
116
+
117
+ export function attachOrCreate(options: Options, callback: DatabaseCallback): void {
118
+
119
+ var host = options.host || Const.DEFAULT_HOST;
120
+ var port = options.port || Const.DEFAULT_PORT;
121
+
122
+ var cnx = connection = new Connection(host, port, function(err: any) {
123
+
124
+ var self = cnx;
125
+
126
+ if (err) {
127
+ callback({ error: err, message: "Connect error" }, undefined as any);
128
+ return;
129
+ }
130
+
131
+ cnx.connect(options, function(err: any) {
132
+
133
+ if (err) {
134
+ doError(err, callback);
135
+ return;
136
+ }
137
+
138
+ cnx.attach(options, function(err: any, ret: any) {
139
+
140
+ if (!err) {
141
+ if (self.db)
142
+ self.db.emit('connect', ret);
143
+ doCallback(ret, callback);
144
+ return;
145
+ }
146
+
147
+ cnx.createDatabase(options, callback);
148
+ });
149
+ });
150
+
151
+ }, options);
152
+ }
153
+
154
+ // Pooling
155
+ export function pool(max: number, options: Options): ConnectionPool {
156
+ return new Pool(attach, max, Object.assign({}, options, { isPool: true }));
157
+ }
@@ -1,162 +1,163 @@
1
- var fs = require('fs');
2
-
3
- const
4
- //ISC_MASK = 0x14000000, // Defines the code as a valid ISC code
5
- FAC_MASK = 0x00FF0000, // Specifies the facility where the code is located
6
- CODE_MASK = 0x0000FFFF, // Specifies the code in the message file
7
- CLASS_MASK = 0xF0000000; // Defines the code as warning, error, info, or other
8
-
9
- var msgNumber = exports.msgNumber = function(facility, code) {
10
- return (facility * 10000 + code);
11
- };
12
-
13
- var getCode = exports.getCode = function(code) {
14
- return (code & CODE_MASK)
15
- };
16
-
17
- var getFacility = exports.getFacility = function(code) {
18
- return (code & FAC_MASK) >> 16;
19
- };
20
-
21
- exports.getClass = function(code) {
22
- return (code & CLASS_MASK) >> 28;
23
- };
24
-
25
- exports.lookupMessages = function(status, messageFile, callback){
26
-
27
- var handle;
28
- var bucket_size;
29
- var top_tree;
30
- var levels;
31
- var buffer;
32
-
33
- function lookup(item, callback) {
34
-
35
- var code = msgNumber(getFacility(item.gdscode), getCode(item.gdscode));
36
-
37
- function readIndex(stackSize, position) {
38
-
39
- function readNode(from) {
40
- var ret = {};
41
- ret.code = buffer.readUInt32LE(from);
42
- ret.seek = buffer.readUInt32LE(from + 4);
43
- return ret;
44
- }
45
-
46
- fs.read(handle, buffer, 0, bucket_size, position, function(err, bufferSize) {
47
-
48
- if (bufferSize <= 0) {
49
- callback();
50
- return;
51
- }
52
-
53
- if (stackSize === levels) {
54
- search();
55
- return;
56
- }
57
-
58
- var from = 0;
59
- var node = readNode(from);
60
-
61
- while (true) {
62
-
63
- if (node.code >= code)
64
- {
65
- readIndex(stackSize + 1, node.seek);
66
- break;
67
- }
68
-
69
- from += 8;
70
- if (from >= bufferSize)
71
- {
72
- callback();
73
- break;
74
- }
75
-
76
- node = readNode(from);
77
- }
78
- });
79
- }
80
-
81
- function search() {
82
-
83
- function readRec(from) {
84
-
85
- function align(v) {
86
- return (v + 3) & ~3;
87
- }
88
-
89
- var ret = {};
90
- ret.code = buffer.readUInt32LE(from);
91
- ret.length = buffer.readUInt16LE(from + 4);
92
-
93
- if (ret.code == code){
94
- from += 8;
95
- ret.text = buffer.toString(undefined, from, from + ret.length);
96
- } else
97
- ret.seek = from + align(8 + ret.length, 4);
98
-
99
- return ret;
100
- }
101
-
102
- var rec = readRec(0);
103
-
104
- while (rec.seek) {
105
- if (rec.seek >= buffer.length)
106
- break;
107
- else
108
- rec = readRec(rec.seek);
109
- }
110
-
111
- var str = rec.text;
112
- if (item.params) {
113
- for (var i = 0; i < item.params.length; i++)
114
- str = str.replace('@' + String(i+1), item.params[i]);
115
- }
116
-
117
- callback(str);
118
- }
119
-
120
- readIndex(1, top_tree);
121
- }
122
-
123
- fs.open(messageFile, 'r', function(err, h) {
124
-
125
- if (!h) {
126
- callback();
127
- return;
128
- }
129
-
130
- buffer = Buffer.alloc(14);
131
- fs.read(h, buffer, 0, 14, 0, function(){
132
-
133
- handle = h;
134
- bucket_size = buffer.readUInt16LE(2);
135
- top_tree = buffer.readUInt32LE(4);
136
- levels = buffer.readUInt16LE(12);
137
- buffer = Buffer.alloc(bucket_size);
138
-
139
- var i = 0;
140
- var text;
141
-
142
- function loop() {
143
- lookup(status[i], function(line) {
144
- if (text)
145
- text = text + ', ' + line
146
- else
147
- text = line;
148
-
149
- if (i === status.length - 1) {
150
- fs.closeSync(handle);
151
- callback(text);
152
- } else {
153
- i++;
154
- loop();
155
- }
156
- });
157
- }
158
-
159
- loop(0);
160
- });
161
- });
162
- };
1
+ import fs from 'fs';
2
+ import type { FbStatusItem } from './callback';
3
+
4
+ const
5
+ //ISC_MASK = 0x14000000, // Defines the code as a valid ISC code
6
+ FAC_MASK = 0x00FF0000, // Specifies the facility where the code is located
7
+ CODE_MASK = 0x0000FFFF, // Specifies the code in the message file
8
+ CLASS_MASK = 0xF0000000; // Defines the code as warning, error, info, or other
9
+
10
+ export const msgNumber = function(facility: number, code: number): number {
11
+ return (facility * 10000 + code);
12
+ };
13
+
14
+ export const getCode = function(code: number): number {
15
+ return (code & CODE_MASK)
16
+ };
17
+
18
+ export const getFacility = function(code: number): number {
19
+ return (code & FAC_MASK) >> 16;
20
+ };
21
+
22
+ export const getClass = function(code: number): number {
23
+ return (code & CLASS_MASK) >> 28;
24
+ };
25
+
26
+ export const lookupMessages = function(status: FbStatusItem[], messageFile: string, callback: (text?: string) => void): void {
27
+
28
+ var handle: number;
29
+ var bucket_size: number;
30
+ var top_tree: number;
31
+ var levels: number;
32
+ var buffer: Buffer;
33
+
34
+ function lookup(item: FbStatusItem, callback: (text?: string) => void) {
35
+
36
+ var code = msgNumber(getFacility(item.gdscode), getCode(item.gdscode));
37
+
38
+ function readIndex(stackSize: number, position: number) {
39
+
40
+ function readNode(from: number) {
41
+ var ret: any = {};
42
+ ret.code = buffer.readUInt32LE(from);
43
+ ret.seek = buffer.readUInt32LE(from + 4);
44
+ return ret;
45
+ }
46
+
47
+ fs.read(handle, buffer, 0, bucket_size, position, function(err, bufferSize) {
48
+
49
+ if (bufferSize <= 0) {
50
+ callback();
51
+ return;
52
+ }
53
+
54
+ if (stackSize === levels) {
55
+ search();
56
+ return;
57
+ }
58
+
59
+ var from = 0;
60
+ var node = readNode(from);
61
+
62
+ while (true) {
63
+
64
+ if (node.code >= code)
65
+ {
66
+ readIndex(stackSize + 1, node.seek);
67
+ break;
68
+ }
69
+
70
+ from += 8;
71
+ if (from >= bufferSize)
72
+ {
73
+ callback();
74
+ break;
75
+ }
76
+
77
+ node = readNode(from);
78
+ }
79
+ });
80
+ }
81
+
82
+ function search() {
83
+
84
+ function readRec(from: number) {
85
+
86
+ function align(v: number) {
87
+ return (v + 3) & ~3;
88
+ }
89
+
90
+ var ret: any = {};
91
+ ret.code = buffer.readUInt32LE(from);
92
+ ret.length = buffer.readUInt16LE(from + 4);
93
+
94
+ if (ret.code == code){
95
+ from += 8;
96
+ ret.text = buffer.toString(undefined, from, from + ret.length);
97
+ } else
98
+ ret.seek = from + align(8 + ret.length);
99
+
100
+ return ret;
101
+ }
102
+
103
+ var rec = readRec(0);
104
+
105
+ while (rec.seek) {
106
+ if (rec.seek >= buffer.length)
107
+ break;
108
+ else
109
+ rec = readRec(rec.seek);
110
+ }
111
+
112
+ var str = rec.text;
113
+ if (item.params) {
114
+ for (var i = 0; i < item.params.length; i++)
115
+ str = str.replace('@' + String(i+1), item.params[i]);
116
+ }
117
+
118
+ callback(str);
119
+ }
120
+
121
+ readIndex(1, top_tree);
122
+ }
123
+
124
+ fs.open(messageFile, 'r', function(err, h) {
125
+
126
+ if (!h) {
127
+ callback();
128
+ return;
129
+ }
130
+
131
+ buffer = Buffer.alloc(14);
132
+ fs.read(h, buffer, 0, 14, 0, function(){
133
+
134
+ handle = h;
135
+ bucket_size = buffer.readUInt16LE(2);
136
+ top_tree = buffer.readUInt32LE(4);
137
+ levels = buffer.readUInt16LE(12);
138
+ buffer = Buffer.alloc(bucket_size);
139
+
140
+ var i = 0;
141
+ var text;
142
+
143
+ function loop() {
144
+ lookup(status[i], function(line) {
145
+ if (text)
146
+ text = text + ', ' + line
147
+ else
148
+ text = line;
149
+
150
+ if (i === status.length - 1) {
151
+ fs.closeSync(handle);
152
+ callback(text);
153
+ } else {
154
+ i++;
155
+ loop();
156
+ }
157
+ });
158
+ }
159
+
160
+ loop();
161
+ });
162
+ });
163
+ };