2
2
// utilities.ts - utility methods to manipulate SQL statements
3
3
//
4
4
5
- import { SQLiteCloudConfig , SQLiteCloudError , SQLiteCloudDataTypes , DEFAULT_PORT , DEFAULT_TIMEOUT } from './types'
6
- import { SQLiteCloudArrayType } from './types'
5
+ import { DEFAULT_PORT , DEFAULT_TIMEOUT , SQLiteCloudArrayType , SQLiteCloudConfig , SQLiteCloudDataTypes , SQLiteCloudError } from './types'
7
6
8
7
// explicitly importing these libraries to allow cross-platform support by replacing them
9
8
import { URL } from 'whatwg-url'
10
- import { Buffer } from 'buffer'
11
9
12
10
//
13
11
// determining running environment, thanks to browser-or-node
@@ -44,6 +42,7 @@ export function getInitializationCommands(config: SQLiteCloudConfig): string {
44
42
// then we bring back linearizability unless specified otherwise
45
43
let commands = 'SET CLIENT KEY NONLINEARIZABLE TO 1; '
46
44
45
+ // TODO: include authentication via token
47
46
// first user authentication, then all other commands
48
47
if ( config . apikey ) {
49
48
commands += `AUTH APIKEY ${ config . apikey } ; `
@@ -177,16 +176,19 @@ export function validateConfiguration(config: SQLiteCloudConfig): SQLiteCloudCon
177
176
config . non_linearizable = parseBoolean ( config . non_linearizable )
178
177
config . insecure = parseBoolean ( config . insecure )
179
178
180
- const hasCredentials = ( config . username && config . password ) || config . apikey
179
+ const hasCredentials = ( config . username && config . password ) || config . apikey || config . token
181
180
if ( ! config . host || ! hasCredentials ) {
182
181
console . error ( 'SQLiteCloudConnection.validateConfiguration - missing arguments' , config )
183
- throw new SQLiteCloudError ( 'The user, password and host arguments or the ?apikey = must be specified.' , { errorCode : 'ERR_MISSING_ARGS' } )
182
+ throw new SQLiteCloudError ( 'The user, password and host arguments, the ?apikey= or the ?token = must be specified.' , { errorCode : 'ERR_MISSING_ARGS' } )
184
183
}
185
184
186
185
if ( ! config . connectionstring ) {
187
186
// build connection string from configuration, values are already validated
187
+ config . connectionstring = `sqlitecloud://${ config . host } :${ config . port } /${ config . database || '' } `
188
188
if ( config . apikey ) {
189
- config . connectionstring = `sqlitecloud://${ config . host } :${ config . port } /${ config . database || '' } ?apikey=${ config . apikey } `
189
+ config . connectionstring += `?apikey=${ config . apikey } `
190
+ } else if ( config . token ) {
191
+ config . connectionstring += `?token=${ config . token } `
190
192
} else {
191
193
config . connectionstring = `sqlitecloud://${ encodeURIComponent ( config . username || '' ) } :${ encodeURIComponent ( config . password || '' ) } @${ config . host } :${
192
194
config . port
@@ -215,7 +217,7 @@ export function parseconnectionstring(connectionstring: string): SQLiteCloudConf
215
217
// all lowecase options
216
218
const options : { [ key : string ] : string } = { }
217
219
url . searchParams . forEach ( ( value , key ) => {
218
- options [ key . toLowerCase ( ) . replace ( / - / g, '_' ) ] = value
220
+ options [ key . toLowerCase ( ) . replace ( / - / g, '_' ) ] = value . trim ( )
219
221
} )
220
222
221
223
const config : SQLiteCloudConfig = {
@@ -243,6 +245,10 @@ export function parseconnectionstring(connectionstring: string): SQLiteCloudConf
243
245
244
246
// either you use an apikey or username and password
245
247
if ( config . apikey ) {
248
+ if ( config . token ) {
249
+ console . error ( 'SQLiteCloudConnection.parseconnectionstring - apikey and token cannot be both specified' )
250
+ throw new SQLiteCloudError ( 'apikey and token cannot be both specified' )
251
+ }
246
252
if ( config . username || config . password ) {
247
253
console . warn ( 'SQLiteCloudConnection.parseconnectionstring - apikey and username/password are both specified, using apikey' )
248
254
}
@@ -257,7 +263,7 @@ export function parseconnectionstring(connectionstring: string): SQLiteCloudConf
257
263
258
264
return config
259
265
} catch ( error ) {
260
- throw new SQLiteCloudError ( `Invalid connection string: ${ connectionstring } ` )
266
+ throw new SQLiteCloudError ( `Invalid connection string: ${ connectionstring } - error: ${ error } ` )
261
267
}
262
268
}
263
269
0 commit comments