22// utilities.ts - utility methods to manipulate SQL statements
33//
44
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'
76
87// explicitly importing these libraries to allow cross-platform support by replacing them
98import { URL } from 'whatwg-url'
10- import { Buffer } from 'buffer'
119
1210//
1311// determining running environment, thanks to browser-or-node
@@ -44,6 +42,7 @@ export function getInitializationCommands(config: SQLiteCloudConfig): string {
4442 // then we bring back linearizability unless specified otherwise
4543 let commands = 'SET CLIENT KEY NONLINEARIZABLE TO 1; '
4644
45+ // TODO: include authentication via token
4746 // first user authentication, then all other commands
4847 if ( config . apikey ) {
4948 commands += `AUTH APIKEY ${ config . apikey } ; `
@@ -177,16 +176,19 @@ export function validateConfiguration(config: SQLiteCloudConfig): SQLiteCloudCon
177176 config . non_linearizable = parseBoolean ( config . non_linearizable )
178177 config . insecure = parseBoolean ( config . insecure )
179178
180- const hasCredentials = ( config . username && config . password ) || config . apikey
179+ const hasCredentials = ( config . username && config . password ) || config . apikey || config . token
181180 if ( ! config . host || ! hasCredentials ) {
182181 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' } )
184183 }
185184
186185 if ( ! config . connectionstring ) {
187186 // build connection string from configuration, values are already validated
187+ config . connectionstring = `sqlitecloud://${ config . host } :${ config . port } /${ config . database || '' } `
188188 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 } `
190192 } else {
191193 config . connectionstring = `sqlitecloud://${ encodeURIComponent ( config . username || '' ) } :${ encodeURIComponent ( config . password || '' ) } @${ config . host } :${
192194 config . port
@@ -215,7 +217,7 @@ export function parseconnectionstring(connectionstring: string): SQLiteCloudConf
215217 // all lowecase options
216218 const options : { [ key : string ] : string } = { }
217219 url . searchParams . forEach ( ( value , key ) => {
218- options [ key . toLowerCase ( ) . replace ( / - / g, '_' ) ] = value
220+ options [ key . toLowerCase ( ) . replace ( / - / g, '_' ) ] = value . trim ( )
219221 } )
220222
221223 const config : SQLiteCloudConfig = {
@@ -243,6 +245,10 @@ export function parseconnectionstring(connectionstring: string): SQLiteCloudConf
243245
244246 // either you use an apikey or username and password
245247 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+ }
246252 if ( config . username || config . password ) {
247253 console . warn ( 'SQLiteCloudConnection.parseconnectionstring - apikey and username/password are both specified, using apikey' )
248254 }
@@ -257,7 +263,7 @@ export function parseconnectionstring(connectionstring: string): SQLiteCloudConf
257263
258264 return config
259265 } catch ( error ) {
260- throw new SQLiteCloudError ( `Invalid connection string: ${ connectionstring } ` )
266+ throw new SQLiteCloudError ( `Invalid connection string: ${ connectionstring } - error: ${ error } ` )
261267 }
262268}
263269
0 commit comments