Skip to content

Commit 9b6057c

Browse files
author
Daniele Briggi
committed
fix: only token, api key or user/pass can be set at once
1 parent 2f873ff commit 9b6057c

File tree

6 files changed

+27
-26
lines changed

6 files changed

+27
-26
lines changed

lib/drivers/utilities.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,10 @@ function parseconnectionstring(connectionstring) {
203203
const config = Object.assign(Object.assign({}, options), { username: decodeURIComponent(url.username), password: decodeURIComponent(url.password), password_hashed: options.password_hashed ? parseBoolean(options.password_hashed) : undefined, host: url.hostname,
204204
// type cast values
205205
port: url.port ? parseInt(url.port) : undefined, insecure: options.insecure ? parseBoolean(options.insecure) : undefined, timeout: options.timeout ? parseInt(options.timeout) : undefined, zerotext: options.zerotext ? parseBoolean(options.zerotext) : undefined, create: options.create ? parseBoolean(options.create) : undefined, memory: options.memory ? parseBoolean(options.memory) : undefined, compression: options.compression ? parseBoolean(options.compression) : undefined, non_linearizable: options.non_linearizable ? parseBoolean(options.non_linearizable) : undefined, noblob: options.noblob ? parseBoolean(options.noblob) : undefined, maxdata: options.maxdata ? parseInt(options.maxdata) : undefined, maxrows: options.maxrows ? parseInt(options.maxrows) : undefined, maxrowset: options.maxrowset ? parseInt(options.maxrowset) : undefined, usewebsocket: options.usewebsocket ? parseBoolean(options.usewebsocket) : undefined, verbose: options.verbose ? parseBoolean(options.verbose) : undefined });
206-
// either you use an apikey or username and password
207-
if (config.apikey) {
208-
if (config.token) {
209-
console.error('SQLiteCloudConnection.parseconnectionstring - apikey and token cannot be both specified');
210-
throw new types_1.SQLiteCloudError('apikey and token cannot be both specified');
211-
}
212-
if (config.username || config.password) {
213-
console.warn('SQLiteCloudConnection.parseconnectionstring - apikey and username/password are both specified, using apikey');
214-
}
215-
delete config.username;
216-
delete config.password;
206+
// either you use an apikey, token or username and password
207+
if (Number(!!config.apikey) + Number(!!config.token) + Number(!!(config.username || config.password)) > 1) {
208+
console.error('SQLiteCloudConnection.parseconnectionstring - choose between apikey, token or username/password');
209+
throw new types_1.SQLiteCloudError('Choose between apikey, token or username/password');
217210
}
218211
const database = url.pathname.replace('/', ''); // pathname is database name, remove the leading slash
219212
if (database) {

lib/sqlitecloud.drivers.dev.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/sqlitecloud.drivers.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.492",
3+
"version": "1.0.493",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/utilities.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function getInitializationCommands(config: SQLiteCloudConfig): string {
8282
}
8383
commands += `USE DATABASE ${config.database};`
8484
}
85-
85+
8686
return commands
8787
}
8888

@@ -244,17 +244,10 @@ export function parseconnectionstring(connectionstring: string): SQLiteCloudConf
244244
verbose: options.verbose ? parseBoolean(options.verbose) : undefined
245245
}
246246

247-
// either you use an apikey or username and password
248-
if (config.apikey) {
249-
if (config.token) {
250-
console.error('SQLiteCloudConnection.parseconnectionstring - apikey and token cannot be both specified')
251-
throw new SQLiteCloudError('apikey and token cannot be both specified')
252-
}
253-
if (config.username || config.password) {
254-
console.warn('SQLiteCloudConnection.parseconnectionstring - apikey and username/password are both specified, using apikey')
255-
}
256-
delete config.username
257-
delete config.password
247+
// either you use an apikey, token or username and password
248+
if (Number(!!config.apikey) + Number(!!config.token) + Number(!!(config.username || config.password)) > 1) {
249+
console.error('SQLiteCloudConnection.parseconnectionstring - choose between apikey, token or username/password')
250+
throw new SQLiteCloudError('Choose between apikey, token or username/password')
258251
}
259252

260253
const database = url.pathname.replace('/', '') // pathname is database name, remove the leading slash

test/utilities.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ describe('parseconnectionstring', () => {
162162

163163
expect(config.timeout).toBe(123)
164164
})
165+
166+
it('expect error when both user/pass and api key are set', () => {
167+
const connectionstring = 'sqlitecloud://user:password@host:1234/database?apikey=yyy'
168+
expect(() => parseconnectionstring(connectionstring)).toThrowError('Choose between apikey, token or username/password')
169+
})
170+
171+
it('expect error when both user/pass and token are set', () => {
172+
const connectionstring = 'sqlitecloud://user:password@host:1234/database?token=yyy'
173+
expect(() => parseconnectionstring(connectionstring)).toThrowError('Choose between apikey, token or username/password')
174+
})
175+
176+
it('expect error when both apikey and token are set', () => {
177+
const connectionstring = 'sqlitecloud://host:1234/database?apikey=xxx&token=yyy'
178+
expect(() => parseconnectionstring(connectionstring)).toThrowError('Choose between apikey, token or username/password')
179+
})
165180
})
166181

167182
describe('getTestingDatabaseName', () => {

0 commit comments

Comments
 (0)