node-firebird 2.3.1 → 2.3.3

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 (55) hide show
  1. package/README.md +218 -31
  2. package/lib/index.d.ts +22 -0
  3. package/lib/pool.js +97 -10
  4. package/lib/srp.js +12 -1
  5. package/lib/wire/connection.js +40 -7
  6. package/lib/wire/socket.js +13 -1
  7. package/lib/wire/xsqlvar.js +69 -6
  8. package/package.json +1 -1
  9. package/poc/README.md +160 -0
  10. package/poc/helpers.js +59 -0
  11. package/poc/node_modules/.package-lock.json +14 -0
  12. package/poc/node_modules/node-firebird/.eslintrc.json +12 -0
  13. package/poc/node_modules/node-firebird/.github/workflows/codeql.yml +76 -0
  14. package/poc/node_modules/node-firebird/.github/workflows/node.js.yml +95 -0
  15. package/poc/node_modules/node-firebird/BIGINT_MIGRATION.md +374 -0
  16. package/poc/node_modules/node-firebird/CI_DEBUGGING_GUIDE.md +148 -0
  17. package/poc/node_modules/node-firebird/ENCRYPTION_CALLBACK.md +152 -0
  18. package/poc/node_modules/node-firebird/FIREBIRD_LOG_FEATURE.md +145 -0
  19. package/poc/node_modules/node-firebird/LICENSE +373 -0
  20. package/poc/node_modules/node-firebird/MINIMAL_CHANGES_SUMMARY.md +136 -0
  21. package/poc/node_modules/node-firebird/PR_SUMMARY.md +96 -0
  22. package/poc/node_modules/node-firebird/README.md +794 -0
  23. package/poc/node_modules/node-firebird/ROADMAP.md +223 -0
  24. package/poc/node_modules/node-firebird/SRP_PROTOCOL.md +482 -0
  25. package/poc/node_modules/node-firebird/lib/callback.js +38 -0
  26. package/poc/node_modules/node-firebird/lib/firebird.msg +0 -0
  27. package/poc/node_modules/node-firebird/lib/firebird.msg.json +1371 -0
  28. package/poc/node_modules/node-firebird/lib/gdscodes.d.ts +1524 -0
  29. package/poc/node_modules/node-firebird/lib/gdscodes.js +1531 -0
  30. package/poc/node_modules/node-firebird/lib/ieee754-decimal.js +500 -0
  31. package/poc/node_modules/node-firebird/lib/index.d.ts +316 -0
  32. package/poc/node_modules/node-firebird/lib/index.js +128 -0
  33. package/poc/node_modules/node-firebird/lib/messages.js +162 -0
  34. package/poc/node_modules/node-firebird/lib/pool.js +108 -0
  35. package/poc/node_modules/node-firebird/lib/srp.js +299 -0
  36. package/poc/node_modules/node-firebird/lib/unix-crypt.js +343 -0
  37. package/poc/node_modules/node-firebird/lib/utils.js +164 -0
  38. package/poc/node_modules/node-firebird/lib/wire/connection.js +2510 -0
  39. package/poc/node_modules/node-firebird/lib/wire/const.js +807 -0
  40. package/poc/node_modules/node-firebird/lib/wire/database.js +378 -0
  41. package/poc/node_modules/node-firebird/lib/wire/eventConnection.js +118 -0
  42. package/poc/node_modules/node-firebird/lib/wire/fbEventManager.js +326 -0
  43. package/poc/node_modules/node-firebird/lib/wire/serialize.js +588 -0
  44. package/poc/node_modules/node-firebird/lib/wire/service.js +1058 -0
  45. package/poc/node_modules/node-firebird/lib/wire/socket.js +175 -0
  46. package/poc/node_modules/node-firebird/lib/wire/statement.js +48 -0
  47. package/poc/node_modules/node-firebird/lib/wire/transaction.js +206 -0
  48. package/poc/node_modules/node-firebird/lib/wire/xsqlvar.js +703 -0
  49. package/poc/node_modules/node-firebird/package.json +38 -0
  50. package/poc/node_modules/node-firebird/vitest.config.js +24 -0
  51. package/poc/package-lock.json +21 -0
  52. package/poc/package.json +12 -0
  53. package/poc/reproduce-fixed.js +150 -0
  54. package/poc/reproduce.js +133 -0
  55. package/vitest.config.js +4 -1
@@ -0,0 +1,316 @@
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
+ }
@@ -0,0 +1,128 @@
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
+ };
@@ -0,0 +1,162 @@
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
+ };