-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
178 lines (132 loc) · 4.25 KB
/
Copy pathparser.js
File metadata and controls
178 lines (132 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
const { Buffer } = require('buffer');
const EventEmitter = require('events').EventEmitter;
const { readString, readInt32BE, readUInt32BE, readInt16BE } = require('./helpers.js');
let processors = {};
processors['p'] = function(d) {
let result = {};
let offset = 0;
result.data = d.slice( offset ) ;
return result;
};
processors['R'] = function(d){
let result = {};
let offset = 0;
let type = readInt32BE(d, offset); offset += 4;
result.type = type;
//AuthenticationSASL
if (type === 10){
result.algorithms = [];
do {
let alg = readString(d, offset); offset += alg.length + 1;
result.algorithms.push(alg);
} while ( d[offset] !== 0 )
}
//AuthenticationSASLContinue
if (type === 11){
result.data = d.slice( offset ) ;
}
//AuthenticationSASLFinal
if (type === 12){
result.data = d.slice( offset ) ;
}
return result;
};
processors['T'] = function(d){
let result = { fields : [] };
let offset = 0;
let fields = readInt16BE(d, offset); offset += 2;
for ( let i = 0 ; i < fields ; i++ ){
let name = readString(d, offset); offset += name.length + 1;
let table_id = readInt32BE(d, offset); offset += 4;
let column_id = readInt16BE(d, offset); offset += 2;
let type_id = readInt32BE(d, offset); offset += 4;
let type_size = readInt16BE(d, offset); offset += 2;
let type_mod = readInt32BE(d, offset); offset += 4;
let fmt_code = readInt16BE(d, offset); offset += 2;
result.fields.push({
name,
table_id,
column_id,
type_id,
type_size,
type_mod,
fmt_code
}
)
}
return result;
};
function app(){
this.buffer = [];
this.message = [];
this.type = 0;
this.length = 0;
EventEmitter.call(this);
const that = this;
this.append = (data) => {
that.buffer.push( ...data );
};
this.parse = () => {
let offset=0;
if ( that.buffer.length === 0 )
return null;
// Read the Header info
if ( that.length === 0 ) {
that.type = 0;
// Read Type
if (that.buffer[0] !== 0 ) {
that.type = String.fromCharCode(that.buffer[offset++]);
}
// Read Length
if (that.buffer.length > 1) {
that.length = readUInt32BE( that.buffer, offset );
offset += 4;
that.length -= 4;
}
// We have 2 types here, SSL upgrade request and Initial Connection
if ( that.type === 0 ){
/*
let data = readUInt32BE( buffer, offset );
if ( data === 80877103 ){
// SSL upgrade request
type = 80877103
}
if ( data === 196608 ){
// Initial Login Data
type = 196608
}
*/
}
// Remove the header from the buffer
that.buffer = that.buffer.slice(offset);
}
if (that.length > 0 || that.type !== 0){
let needed = that.length - that.message.length;
if ( needed > 0 ) {
let block = that.buffer.slice(0, Math.min( needed, that.buffer.length ) );
that.message = that.message.concat( block );
that.buffer = that.buffer.slice(needed);
}
if ( that.message.length === that.length ){
let parsed = {};
if ( processors[that.type] !== undefined ){
parsed = processors[that.type](that.message);
}
let d = {
type: that.type,
message: that.message,
parsed: parsed
};
that.emit('message', d);
that.message = [];
that.length = 0;
that.type = 0;
return d;
}
}
return null;
}
}
app.prototype = Object.create(EventEmitter.prototype);
module.exports = app;