Skip to content

Commit

Permalink
Possible fix to #13, improves handling of splitChar for correct behav…
Browse files Browse the repository at this point in the history
…ior if appears later in string
  • Loading branch information
mclark-newvistas committed Oct 7, 2016
1 parent fc0b5c8 commit f1abf20
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class Connector extends EventEmitter {
this._tableManager = new TableManager( this._connection )
this._defaultTable = options.defaultTable || 'deepstream_records'
this._splitChar = options.splitChar || null
this._tableMatch = this._splitChar
? new RegExp( '^(\\w+)' + this._escapeRegExp( this._splitChar ) )
: null
this._primaryKey = options.primaryKey || PRIMARY_KEY
}

Expand Down Expand Up @@ -99,6 +102,7 @@ class Connector extends EventEmitter {
rethinkdb.table( params.table ).get( params.id ).run( this._connection.get(), ( error, entry ) => {
if( entry ) {
delete entry[ this._primaryKey ]
delete entry.__key // in case is set
entry = dataTransform.transformValueFromStorage( entry )
}
callback( error, entry )
Expand Down Expand Up @@ -157,13 +161,18 @@ class Connector extends EventEmitter {
* @returns {Object} params
*/
_getParams( key ) {
const parts = key.split( this._splitChar )
const params = parts.length === 2
? { table: parts[ 0 ], id: parts[ 1 ] }
: { table: this._defaultTable, id: key }
const table = key.match( this._tableMatch )
var params = { table: this._defaultTable, id: key }

if (params.id.length > 127) {
params.id = crypto.createHash('sha1').update(params.id).digest("hex")
if( table ) {
params.table = table[1]
params.id = key.substr(table[1].length + 1)
}

// rethink can't have a key > 127 bytes; hash key and store alongside
if( params.id.length > 127 ) {
params.fullKey = params.id;
params.id = crypto.createHash( 'sha256' ).update( params.id ).digest( 'hex' );
}

return params
Expand All @@ -181,6 +190,9 @@ class Connector extends EventEmitter {
*/
_insert( params, value, callback ) {
value[ this._primaryKey ] = params.id
if( params.fullKey ) {
value.__key = params.fullKey
}

rethinkdb
.table( params.table )
Expand All @@ -205,5 +217,20 @@ class Connector extends EventEmitter {
throw new Error( 'Missing option port' )
}
}
}

/**
* Escapes user input for use in a regular expression
*
* @param {String} string the user input
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
* @copyright public domain
*
* @private
* @returns {String} escaped user input
*/
_escapeRegExp( string ) {
return string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ) // $& means the whole matched string
}
}
module.exports = Connector

0 comments on commit f1abf20

Please sign in to comment.