mssql 7.2.0 → 8.0.1

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.
@@ -1,289 +0,0 @@
1
- 'use strict'
2
-
3
- const URL = require('url').URL
4
-
5
- const IGNORE_KEYS = ['stream']
6
- const oror = function () {
7
- for (let i = 0, l = arguments.length; i < l; i++) {
8
- if (arguments[i] !== null && arguments[i] !== undefined) {
9
- return arguments[i]
10
- }
11
- }
12
- }
13
-
14
- const parseConnectionURI = function (uri) {
15
- const parsed = new URL(uri)
16
- let instance
17
- let user
18
- let password
19
-
20
- const path = parsed.pathname.substr(1).split('/')
21
- if (path.length > 1) {
22
- instance = path.shift()
23
- }
24
-
25
- if (parsed.username) {
26
- const auth = [parsed.username, parsed.password]
27
- user = decodeURIComponent(auth.shift())
28
- password = decodeURIComponent(auth.join(':'))
29
- }
30
-
31
- const port = parsed.port ? `,${parsed.port}` : (instance ? `\\${instance}` : '')
32
- const object = {
33
- server: `${parsed.hostname}${port}`,
34
- uid: user || '',
35
- pwd: password || '',
36
- database: path[0]
37
- }
38
-
39
- if (parsed.searchParams) {
40
- parsed.searchParams.forEach((value, key) => {
41
- if (key === 'domain') {
42
- object.uid = `${value}\\${object.uid}`
43
- } else {
44
- object[key] = value
45
- }
46
- })
47
- }
48
-
49
- Object.defineProperty(object, 'toString', {
50
- value () {
51
- const out = []
52
- for (const key in this) {
53
- if (IGNORE_KEYS.indexOf(key) === -1) {
54
- out.push(`${key}={${this[key]}}`)
55
- }
56
- }
57
- return out.join(';')
58
- }
59
- })
60
-
61
- return object
62
- }
63
-
64
- const parseConnectionString = function (string) {
65
- let cursor = 0
66
- let parsing = 'name'
67
- let param = null
68
- let buffer = ''
69
- let quotes = null
70
- const parsed = {}
71
- const original = {}
72
-
73
- Object.defineProperty(parsed, '__original__', { value: original })
74
- Object.defineProperty(parsed, 'toString', {
75
- value () {
76
- const out = []
77
- for (const key in this) {
78
- if (IGNORE_KEYS.indexOf(key) === -1) {
79
- const esc = original[key].escape || ['', '']
80
- out.push(`${original[key].name}=${esc[0] || ''}${this[key]}${esc[1] || ''}`)
81
- }
82
- }
83
- return out.join(';')
84
- }
85
- })
86
-
87
- while (cursor < string.length) {
88
- const char = string.charAt(cursor)
89
- switch (char) {
90
- case '=':
91
- if (parsing === 'name') {
92
- buffer = buffer.trim()
93
- param = buffer.toLowerCase()
94
- original[param] = { name: buffer }
95
- parsing = 'value'
96
- buffer = ''
97
- } else {
98
- buffer += char
99
- }
100
- break
101
-
102
- case '\'': case '"':
103
- if (parsing === 'value') {
104
- if (!buffer.trim().length) {
105
- // value is wrapped in qotes
106
- original[param].escape = [char, char]
107
- quotes = char
108
- buffer = ''
109
- } else if (quotes) {
110
- if (char === quotes) {
111
- // found same char as used for wrapping quotes
112
- if (char === string.charAt(cursor + 1)) {
113
- // escaped quote
114
- buffer += char
115
- cursor++
116
- } else {
117
- // end of value
118
- parsed[param] = buffer
119
- param = null
120
- parsing = null
121
- buffer = ''
122
- quotes = null
123
- }
124
- } else {
125
- buffer += char
126
- }
127
- } else {
128
- buffer += char
129
- }
130
- } else {
131
- throw new Error('Invalid connection string.')
132
- }
133
- break
134
-
135
- case '{':
136
- if (parsing === 'value') {
137
- if (buffer.trim().length === 0) {
138
- // value is wrapped in qotes
139
- original[param].escape = ['{', '}']
140
- quotes = '{}'
141
- buffer = ''
142
- } else {
143
- buffer += char
144
- }
145
- } else {
146
- throw new Error('Invalid connection string.')
147
- }
148
- break
149
-
150
- case '}':
151
- if (parsing === 'value') {
152
- if (quotes === '{}') {
153
- // end of value
154
- parsed[param] = buffer
155
- param = null
156
- parsing = null
157
- buffer = ''
158
- quotes = null
159
- } else {
160
- buffer += char
161
- }
162
- } else {
163
- throw new Error('Invalid connection string.')
164
- }
165
- break
166
-
167
- case ';':
168
- if (parsing === 'value') {
169
- if (quotes) {
170
- buffer += char
171
- } else {
172
- // end of value
173
- parsed[param] = buffer
174
- param = null
175
- parsing = 'name'
176
- buffer = ''
177
- }
178
- } else {
179
- buffer = ''
180
- parsing = 'name'
181
- }
182
- break
183
-
184
- default:
185
- buffer += char
186
- }
187
-
188
- cursor++
189
- }
190
-
191
- if (parsing === 'value') {
192
- // end of value
193
- parsed[param] = buffer
194
- }
195
-
196
- return parsed
197
- }
198
-
199
- const resolveConnectionString = function (string, driver) {
200
- const parsed = /^(mssql|tedious|msnodesql|tds):\/\//i.test(string) ? parseConnectionURI(string) : parseConnectionString(string)
201
- const stream = (parsed.stream || '').toLowerCase()
202
- const encrypt = (parsed.encrypt || '').toLowerCase()
203
-
204
- if (driver === 'msnodesqlv8') {
205
- parsed.driver = 'SQL Server Native Client 11.0'
206
-
207
- if (parsed.__original__) {
208
- parsed.__original__.driver = { name: 'Driver', escape: ['{', '}'] }
209
- }
210
-
211
- return { connectionString: parsed.toString() }
212
- }
213
-
214
- let user = parsed.uid || parsed.uid || parsed['user id']
215
- let server = parsed.server || parsed.address || parsed.addr || parsed['data source'] || parsed['network address']
216
-
217
- const config = {
218
- password: oror(parsed.pwd, parsed.password),
219
- database: oror(parsed.database, parsed['initial catalog']),
220
- connectionTimeout: oror(parsed.connectionTimeout, parsed.timeout, parsed['connect timeout'], parsed['connection timeout']),
221
- requestTimeout: oror(parsed.requestTimeout, parsed['request timeout']),
222
- stream: stream === 'true' || stream === 'yes' || stream === '1',
223
- options: {
224
- readOnlyIntent: parsed.applicationintent && parsed.applicationintent.toLowerCase() === 'readonly',
225
- encrypt: encrypt === 'true' || encrypt === 'yes' || encrypt === '1'
226
- }
227
- }
228
-
229
- if (parsed.trustservercertificate && ['true', '1', 'yes'].includes(parsed.trustservercertificate.toLowerCase())) {
230
- Object.assign(config.options, {
231
- trustServerCertificate: true
232
- })
233
- }
234
-
235
- if (parsed.useUTC != null) {
236
- const utc = parsed.useUTC.toLowerCase()
237
- config.options.useUTC = utc === 'true' || utc === 'yes' || utc === '1'
238
- }
239
- if (config.connectionTimeout != null) {
240
- config.connectionTimeout = parseInt(config.connectionTimeout, 10) * 1000
241
- }
242
- if (config.requestTimeout != null) {
243
- config.requestTimeout = parseInt(config.requestTimeout, 10)
244
- }
245
-
246
- if (parsed.multisubnetfailover != null) {
247
- config.options.multiSubnetFailover = parsed.multisubnetfailover.toLowerCase() === 'true'
248
- }
249
-
250
- if (/^(.*)\\(.*)$/.exec(user)) {
251
- config.domain = RegExp.$1
252
- user = RegExp.$2
253
- }
254
-
255
- if (server) {
256
- server = server.trim()
257
-
258
- if (/^np:/i.test(server)) {
259
- throw new Error('Connection via Named Pipes is not supported.')
260
- }
261
-
262
- if (/^tcp:/i.test(server)) {
263
- server = server.substr(4)
264
- }
265
-
266
- if (/^(.*)\\(.*)$/.exec(server)) {
267
- server = RegExp.$1
268
- config.options.instanceName = RegExp.$2
269
- }
270
-
271
- if (/^(.*),(.*)$/.exec(server)) {
272
- server = RegExp.$1.trim()
273
- config.port = parseInt(RegExp.$2.trim(), 10)
274
- }
275
-
276
- if (server === '.' || server === '(.)' || server.toLowerCase() === '(localdb)' || server.toLowerCase() === '(local)') {
277
- server = 'localhost'
278
- }
279
- }
280
-
281
- config.user = user
282
- config.server = server
283
- return config
284
- }
285
-
286
- module.exports = {
287
- parse: parseConnectionString,
288
- resolve: resolveConnectionString
289
- }