Skip to content

Commit ada25bd

Browse files
author
Daniele Briggi
committed
feat(token): add to config
1 parent b449bd0 commit ada25bd

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
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.438",
3+
"version": "1.0.455",
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/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export interface SQLiteCloudConfig {
2626
password_hashed?: boolean
2727
/** API key can be provided instead of username and password */
2828
apikey?: string
29+
/** Access Token provided in place of API Key or username/password */
30+
token?: string
2931

3032
/** Host name is required unless connectionstring is provided, eg: xxx.sqlitecloud.io */
3133
host?: string

src/drivers/utilities.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
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
98
import { 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

Comments
 (0)