Skip to content

Commit aee376b

Browse files
Merge branch 'main' into issue/optimize
2 parents 544029e + 339ab24 commit aee376b

File tree

5 files changed

+65
-25
lines changed

5 files changed

+65
-25
lines changed

lib/connectionConfig.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
////@ts-check
22
//connectionConfig
33
const typedef = require('./typedef')
4+
const {encodeURISegment} = require("./utils")
45

56
/**
67
* @file Terminus DB connection configuration
@@ -83,7 +84,9 @@ ConnectionConfig.prototype.update = function(params) {
8384
//if (typeof params.credential !== 'undefined')this.setTokenParameter(params.credential)
8485
if (typeof params.db !== 'undefined') this.setDB(params.db)
8586
//this to set the author and the jwt in creation
86-
if (typeof params.jwt !== 'undefined') this.setLocalBasicAuth(params.jwt, params.user, 'jwt')
87+
if (typeof params.token !== 'undefined') this.setLocalBasicAuth(params.token, params.user, 'apikey')
88+
//this is for jwt
89+
else if (typeof params.jwt !== 'undefined') this.setLocalBasicAuth(params.jwt, params.user, 'jwt')
8790
//this is basic authO
8891
else if (typeof params.key !== 'undefined') this.setLocalBasicAuth(params.key, params.user)
8992
//If I set only the user I have to set the
@@ -225,7 +228,8 @@ ConnectionConfig.prototype.parseServerURL = function(str) {
225228
ConnectionConfig.prototype.serverUrlEncoding = function(str){
226229
const orgArr = str.split("/")
227230
if(orgArr.length>3){
228-
const org = encodeURIComponent(orgArr[3])
231+
//const org = encodeURI(orgArr[3])
232+
const org = encodeURISegment(orgArr[3])
229233
return str.replace(orgArr[3], org)
230234
}
231235
return str
@@ -309,7 +313,7 @@ ConnectionConfig.prototype.setRemoteBasicAuth = function(remoteKey, remoteUserID
309313
* set the local database connection credential
310314
* @param {string} [userKey] - basic auth api key
311315
* @param {string} [userId] - user id
312-
* @param {string} [type] - basic|jwt
316+
* @param {string} [type] - basic|jwt|apikey
313317
*/
314318
//to be review this is ok for the basic Auth
315319
ConnectionConfig.prototype.setLocalBasicAuth = function(userKey, userId = 'admin', type= 'basic') {
@@ -358,7 +362,7 @@ ConnectionConfig.prototype.dbURL = function() {
358362

359363
ConnectionConfig.prototype.userURL = function(user) {
360364
let url = `${this.apiURL()}user`
361-
if (user) url += `/${encodeURIComponent(user)}`
365+
if (user) url += `/${encodeURISegment(user)}`
362366
return url
363367
}
364368

@@ -368,11 +372,11 @@ ConnectionConfig.prototype.userURL = function(user) {
368372
* @param {string} [action] - the organization id
369373
* @returns {string}
370374
*/
371-
375+
//encodeURIComponent
372376
ConnectionConfig.prototype.organizationURL = function(orgId, action) {
373377
let url = `${this.apiURL()}organization`
374-
if (orgId) url += `/${encodeURIComponent(orgId)}`
375-
if (action) url += `/${encodeURIComponent(action)}`
378+
if (orgId) url += `/${encodeURISegment(orgId)}`
379+
if (action) url += `/${encodeURISegment(action)}`
376380
return url
377381
}
378382

@@ -561,7 +565,7 @@ ConnectionConfig.prototype.commitDescriptorUrl = function(commitId) {
561565
/**
562566
* Generate URL for optimizing db branch
563567
*/
564-
568+
//encodeURI() will not encode: ~!@#$&*()=:/,;?+'
565569
ConnectionConfig.prototype.optimizeBranchUrl = function(branchId) {
566570
//let o = this.optimizeBase()
567571
const dbBase = this.dbBase('optimize')
@@ -628,9 +632,10 @@ ConnectionConfig.prototype.branchBase = function(action) {
628632
* (unless dbid = system dbname in which case there is no organization)
629633
* @returns {string|boolean}
630634
*/
635+
//encodeURIComponent
631636
ConnectionConfig.prototype.dbURLFragment = function() {
632637
if (this.db() === this.system_db) return this.db()
633-
return encodeURIComponent(this.organization()) + '/' + encodeURIComponent(this.db())
638+
return encodeURISegment(this.organization()) + '/' + encodeURISegment(this.db())
634639
}
635640

636641
/**
@@ -650,12 +655,13 @@ ConnectionConfig.prototype.documentURL = function(params){
650655
ConnectionConfig.prototype.queryParameter = function(params){
651656
if(!params || typeof params!=='object')return ''
652657
let queryString = Object.keys(params).map((key) => {
653-
return key + '=' + encodeURIComponent(params[key])
658+
return key + '=' + encodeURISegment(params[key])
654659
}).join('&');
655660

656661
return `?${queryString}`;
657662
}
658663

664+
659665
ConnectionConfig.prototype.jsonSchemaURL = function(params){
660666
const paramsStr=this.queryParameter(params)
661667
return this.branchBase('schema')+paramsStr

lib/dispatchRequest.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
5757
options.headers = {Authorization: `Basic ${encoded}`}
5858
} else if (local_auth && local_auth.type === 'jwt') {
5959
options.headers = {Authorization: `Bearer ${local_auth.key}`}
60-
}
60+
} else if (local_auth && local_auth.type === 'apikey') {
61+
options.headers = {API_TOKEN: local_auth.key}
62+
}
6163

6264
/*
6365
* pass the Authorization information of another

lib/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55
*/
66
const Utils = {}
77

8+
//ˆˆˆˆˆ&&**(((()*(*#&#&&#$*#*(#(#(#)#)#)_
9+
//'Doc%2F%3D%26test'
10+
//
11+
//%CB%86%CB%86%CB%86%CB%86%CB%86&&**(((()*(*%23&%23&&%23$*%23*(%23(%23(%23)%23)%23)_
12+
//encodeURI() will not encode: ~!@#$&*()=:/,;?+
13+
//%3D%25team =%team
14+
Utils.encodeURISegment = function(str){
15+
if(typeof str !== 'string') return str
16+
str = encodeURI(str)
17+
str = str.replaceAll('?','%3F')
18+
str = str.replaceAll('=','%3D')
19+
str = str.replaceAll('&','%26')
20+
21+
return str
22+
}
23+
//%253D%25team
24+
825
/**
926
* default set of prefixes that will be used for URL compression
1027
* @type {object}

lib/viewer/woqlRule.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,26 @@ WOQLRule.prototype.matchCell = function(rules, row, key, rownum, action) {
121121
return matches
122122
}
123123

124-
WOQLRule.prototype.matchColumn = function(rules, key, action) {
125-
var matches = []
124+
/**
125+
*
126+
* @param {array} rules
127+
* @param {string} key - column name
128+
* @param {string} ruleName - the rule name like header, width etc...
129+
* @returns {array}
130+
*/
131+
WOQLRule.prototype.matchColumn = function(rules, key, ruleName) {
132+
const matches = []
126133
for (var i = 0; i < rules.length; i++) {
127-
if (action && this.rule && typeof this.rule[action] == 'undefined') continue
128-
if (rules[i].pattern.matchColumn(key)) {
129-
matches.push(rules[i])
134+
if (ruleName && this.rule && typeof this.rule[ruleName] == undefined) continue
135+
const ruleRow = rules[i]
136+
/*
137+
* if it is not the rule that I'm looking for
138+
*/
139+
if(ruleName && ruleRow.rule[ruleName] === undefined){
140+
continue
141+
}
142+
if(ruleRow.pattern.matchColumn(key)) {
143+
matches.push(ruleRow)
130144
}
131145
}
132146
return matches

lib/viewer/woqlTable.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,17 @@ WOQLTable.prototype.getColumnsToRender = function() {
129129

130130
WOQLTable.prototype.getColumnHeaderContents = function(colid) {
131131
colid = UTILS.removeNamespaceFromVariable(colid)
132-
let hr = new WOQLRule().matchColumn(this.config.rules, colid, 'header')
133-
if (hr && hr.length) {
134-
let h = hr[hr.length - 1].rule.header
135-
if (typeof h == 'string') {
136-
return h // document.createTextNode(h);
137-
} else if (typeof h == 'function') {
138-
return h(colid)
139-
} else return h
132+
let rules = new WOQLRule().matchColumn(this.config.rules, colid, 'header')
133+
//hr is an array header in not always the last item
134+
if (rules.length) {
135+
const header = rules[rules.length -1].rule ? rules[rules.length -1].rule.header : null
136+
if (typeof header === 'string') {
137+
return header
138+
} else if (typeof header === 'function') {
139+
return header(colid)
140+
} else return header
140141
}
141-
if(colid[0] == "_") return " "
142+
if(colid[0] === "_") return " "
142143
return UTILS.labelFromVariable(colid)
143144
//return document.createTextNode(clab);
144145
}

0 commit comments

Comments
 (0)