This repository has been archived by the owner on May 5, 2023. It is now read-only.
/
connectionstringparser.js
217 lines (183 loc) · 5.14 KB
/
connectionstringparser.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Copyright (c) Microsoft. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var util = require('../../util/util');
// Expose 'ConnectionStringParser'.
exports = module.exports;
/**
* Creates a new 'ConnectionString' instance.
*
* @constructor
* @param {string} connectionString The connection string to be parsed.
*/
function ConnectionStringParser(connectionString) {
this._value = connectionString;
this._pos = 0;
this._state = 'ExpectKey';
}
/**
* Parses a connection string into an object.
*
* @return {object} The query string object.
*/
ConnectionStringParser.prototype._parse = function () {
var key = null;
var value = null;
var parsedConnectionString = { };
for (; ;) {
this._skipWhitespaces();
if (this._pos === this._value.length && this._state !== 'ExpectValue')
{
// Not stopping after the end has been reached and a value is expected
// results in creating an empty value, which we expect.
break;
}
switch (this._state) {
case 'ExpectKey':
key = this._extractKey();
this._state = 'ExpectAssignment';
break;
case 'ExpectAssignment':
this._skipOperator('=');
this._state = 'ExpectValue';
break;
case 'ExpectValue':
value = this._extractValue();
this._state = 'ExpectSeparator';
parsedConnectionString[key.toLowerCase()] = value;
key = null;
value = null;
break;
default:
this._skipOperator(';');
this._state = 'ExpectKey';
break;
}
}
if (this._state === 'ExpectAssignment') {
// Must end parsing in the valid state (expected key or separator)
throw new Error('Missing character "="');
}
return parsedConnectionString;
};
/**
* Skips whitespaces at the current position.
*/
ConnectionStringParser.prototype._skipWhitespaces = function () {
while (this._pos < this._value.length && this._value[this._pos] === ' ')
{
this._pos++;
}
};
/**
* Extracts key at the current position.
*
* @return {string} Key.
*/
ConnectionStringParser.prototype._extractKey = function () {
var key = null;
var firstPos = this._pos;
var ch = this._value[this._pos++];
if (ch === '"' || ch === '\'') {
key = this._extractString(ch);
} else if (ch === ';' || ch === '=') {
// Key name was expected.
throw new Error('Missing key');
} else {
while (this._pos < this._value.length) {
ch = this._value[this._pos];
if (ch === '=') {
break;
}
this._pos++;
}
key = this._value.substring(firstPos, this._pos);
}
if (key.length === 0) {
// Empty key name.
throw new Error('Empty key name');
}
return key;
};
/**
* Extracts the string until the given quotation mark.
*
* @param {string} quote Quotation mark terminating the string.
* @return {string} string.
*/
ConnectionStringParser.prototype._extractString = function (quote) {
var firstPos = this._pos;
while (this._pos < this._value.length && this._value[this._pos] !== quote)
{
this._pos++;
}
if (this._pos === this._value.length) {
// Runaway string.
throw new Error('Unterminated string starting at position ' + firstPos);
}
return this._value.substring(firstPos, this._pos++);
};
/**
* Skips specified operator.
*
* @param {string} operatorChar The oeprator to skip.
*/
ConnectionStringParser.prototype._skipOperator = function (operatorChar) {
if (this._value[this._pos] != operatorChar) {
// Character was expected.
throw new Error('expecting ' + operatorChar + ' but instead got ' + currentChar + ' at position ' + this._pos);
}
this._pos++;
};
/**
* Extracts key's value.
*
* @return {string} The key value.
*/
ConnectionStringParser.prototype._extractValue = function () {
var value = '';
if (this._pos < this._value.length) {
var ch = this._value[this._pos];
if (ch === '\'' || ch === '"') {
this._pos++;
value = this._extractString(ch);
} else {
var firstPos = this._pos;
var isFound = false;
while (this._pos < this._value.length && !isFound) {
ch = this._value[this._pos];
switch (ch) {
case ';':
isFound = true;
break;
default:
this._pos++;
break;
}
}
value = this._value.substring(firstPos, this._pos);
}
}
return value;
};
/**
* Parses a connection string.
*
* @param {number} connectionString The connection string to be parsed.
* @return {object} The query string object.
*/
exports.parse = function (connectionString) {
var connectionStringParser = new ConnectionStringParser(connectionString);
return connectionStringParser._parse();
};