Skip to content

Commit bb881b0

Browse files
author
Sebastian Kippe
authored
Merge pull request #1106 from remotestorage/refactorings
Refactorings
2 parents 3bf56c8 + 7f266e0 commit bb881b0

File tree

6 files changed

+71
-30
lines changed

6 files changed

+71
-30
lines changed

src/dropbox.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const isFolder = util.isFolder;
4646
const cleanPath = util.cleanPath;
4747
const shouldBeTreatedAsBinary = util.shouldBeTreatedAsBinary;
4848
const readBinaryData = util.readBinaryData;
49+
const getJSONFromLocalStorage = util.getJSONFromLocalStorage;
4950

5051
/**
5152
* Map a local path to a path in Dropbox.
@@ -180,16 +181,11 @@ var Dropbox = function (rs) {
180181
hasLocalStorage = util.localStorageAvailable();
181182

182183
if (hasLocalStorage){
183-
var settings;
184-
try {
185-
settings = JSON.parse(localStorage.getItem(SETTINGS_KEY));
186-
} catch(e) { /* ok to ignore, probably no data in localStorage */ }
184+
const settings = getJSONFromLocalStorage(SETTINGS_KEY);
187185
if (settings) {
188186
this.configure(settings);
189187
}
190-
try {
191-
this._itemRefs = JSON.parse(localStorage.getItem(SETTINGS_KEY+':shares')) || {};
192-
} catch(e) { /* ok to ignore, no shares in localStorage */ }
188+
this._itemRefs = getJSONFromLocalStorage(`${SETTINGS_KEY}:shares`) || {};
193189
}
194190
if (this.connected) {
195191
setTimeout(this._emit.bind(this), 0, 'connected');

src/googledrive.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const isFolder = util.isFolder;
3232
const cleanPath = util.cleanPath;
3333
const shouldBeTreatedAsBinary = util.shouldBeTreatedAsBinary;
3434
const readBinaryData = util.readBinaryData;
35+
const getJSONFromLocalStorage = util.getJSONFromLocalStorage;
3536

3637
let hasLocalStorage;
3738

@@ -92,6 +93,18 @@ function googleDrivePath (path) {
9293
return cleanPath(`${PATH_PREFIX}/${path}`);
9394
}
9495

96+
/**
97+
* Remove surrounding quotes from a string.
98+
*
99+
* @param {string} string - string with surrounding quotes
100+
* @returns {string} string without surrounding quotes
101+
*
102+
* @private
103+
*/
104+
function removeQuotes (string) {
105+
return string.replace(/^["'](.*)["']$/, "$1");
106+
}
107+
95108
/**
96109
* Internal cache object for storing Google file IDs.
97110
*
@@ -129,12 +142,7 @@ const GoogleDrive = function (remoteStorage, clientId) {
129142
hasLocalStorage = util.localStorageAvailable();
130143

131144
if (hasLocalStorage){
132-
let settings;
133-
try {
134-
settings = JSON.parse(localStorage.getItem(SETTINGS_KEY));
135-
} catch(e) {
136-
// no settings stored
137-
}
145+
const settings = getJSONFromLocalStorage(SETTINGS_KEY);
138146
if (settings) {
139147
this.configure(settings);
140148
}
@@ -258,7 +266,7 @@ GoogleDrive.prototype = {
258266
function putDone(response) {
259267
if (response.status >= 200 && response.status < 300) {
260268
const meta = JSON.parse(response.responseText);
261-
const etagWithoutQuotes = meta.etag.substring(1, meta.etag.length-1);
269+
const etagWithoutQuotes = removeQuotes(meta.etag);
262270
return Promise.resolve({statusCode: 200, contentType: meta.mimeType, revision: etagWithoutQuotes});
263271
} else if (response.status === 412) {
264272
return Promise.resolve({statusCode: 412, revision: 'conflict'});
@@ -302,7 +310,7 @@ GoogleDrive.prototype = {
302310
return this._getMeta(id).then((meta) => {
303311
let etagWithoutQuotes;
304312
if ((typeof meta === 'object') && (typeof meta.etag === 'string')) {
305-
etagWithoutQuotes = meta.etag.substring(1, meta.etag.length-1);
313+
etagWithoutQuotes = removeQuotes(meta.etag);
306314
}
307315
if (options && options.ifMatch && (options.ifMatch !== etagWithoutQuotes)) {
308316
return {statusCode: 412, revision: etagWithoutQuotes};
@@ -430,14 +438,13 @@ GoogleDrive.prototype = {
430438
return this._getMeta(id).then((meta) => {
431439
let etagWithoutQuotes;
432440
if (typeof(meta) === 'object' && typeof(meta.etag) === 'string') {
433-
etagWithoutQuotes = meta.etag.substring(1, meta.etag.length-1);
441+
etagWithoutQuotes = removeQuotes(meta.etag);
434442
}
435443

436444
if (options && options.ifNoneMatch && (etagWithoutQuotes === options.ifNoneMatch)) {
437445
return Promise.resolve({statusCode: 304});
438446
}
439447

440-
const options2 = {};
441448
if (!meta.downloadUrl) {
442449
if (meta.exportLinks && meta.exportLinks['text/html']) {
443450
// Documents that were generated inside GoogleDocs have no
@@ -450,7 +457,7 @@ GoogleDrive.prototype = {
450457
}
451458
}
452459

453-
return this._request('GET', meta.downloadUrl, options2).then((response) => {
460+
return this._request('GET', meta.downloadUrl, {}).then((response) => {
454461
let body = response.response;
455462
if (meta.mimeType.match(/^application\/json/)) {
456463
try {
@@ -514,7 +521,7 @@ GoogleDrive.prototype = {
514521

515522
itemsMap = {};
516523
for (const item of data.items) {
517-
etagWithoutQuotes = item.etag.substring(1, item.etag.length-1);
524+
etagWithoutQuotes = removeQuotes(item.etag);
518525
if (item.mimeType === GD_DIR_MIME_TYPE) {
519526
this._fileIdCache.set(path + item.title + '/', item.id);
520527
itemsMap[item.title + '/'] = {

src/remotestorage.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const log = require('./log');
1212
const Features = require('./features');
1313
const globalContext = util.getGlobalContext();
1414
const eventHandling = require('./eventhandling');
15+
const getJSONFromLocalStorage = util.getJSONFromLocalStorage;
1516

1617
var hasLocalStorage;
1718

@@ -90,11 +91,7 @@ var RemoteStorage = function (cfg) {
9091
hasLocalStorage = util.localStorageAvailable();
9192

9293
if (hasLocalStorage) {
93-
try {
94-
this.apiKeys = JSON.parse(localStorage.getItem('remotestorage:api-keys')) || {};
95-
} catch(exc) {
96-
// ignored
97-
}
94+
this.apiKeys = getJSONFromLocalStorage('remotestorage:api-keys') || {};
9895
this.setBackend(localStorage.getItem('remotestorage:backend') || 'remotestorage');
9996
}
10097

src/util.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ var util = {
205205
}
206206
},
207207

208+
/**
209+
* Extract and parse JSON data from localStorage.
210+
*
211+
* @param {string} key - localStorage key
212+
*
213+
* @returns {object} parsed object or undefined
214+
*/
215+
getJSONFromLocalStorage (key) {
216+
const context = util.getGlobalContext();
217+
218+
try {
219+
return JSON.parse(context.localStorage.getItem(key));
220+
} catch(e) {
221+
// no JSON stored
222+
}
223+
},
224+
208225
/**
209226
* Decide if data should be treated as binary based on the content
210227
* and content-type.

src/wireclient.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ if (typeof(ArrayBufferView) === 'function') {
7575
const isFolder = util.isFolder;
7676
const cleanPath = util.cleanPath;
7777
const shouldBeTreatedAsBinary = util.shouldBeTreatedAsBinary;
78+
const getJSONFromLocalStorage = util.getJSONFromLocalStorage;
7879

7980
function addQuotes(str) {
8081
if (typeof(str) !== 'string') {
@@ -167,12 +168,7 @@ var WireClient = function WireClient(rs) {
167168
eventHandling(this, 'connected', 'not-connected');
168169

169170
if (hasLocalStorage) {
170-
var settings;
171-
try {
172-
settings = JSON.parse(localStorage[SETTINGS_KEY]);
173-
} catch(e) {
174-
// no settings stored
175-
}
171+
const settings = getJSONFromLocalStorage(SETTINGS_KEY);
176172
if (settings) {
177173
setTimeout(function () {
178174
this.configure(settings);

test/unit/util-suite.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,34 @@ define(['./src/util'], function (util) {
151151

152152
test.assert(util.localStorageAvailable(), false);
153153
}
154+
},
155+
156+
{
157+
desc: "getJSONFromLocalStorage returns the object for the given key",
158+
run: function(env, test) {
159+
160+
global.localStorage = {
161+
getItem: function() {
162+
return '{ "foo": "bar" }';
163+
}
164+
};
165+
166+
test.assert(util.getJSONFromLocalStorage('somekey'), { foo: 'bar' });
167+
}
168+
},
169+
170+
{
171+
desc: "getJSONFromLocalStorage returns null when there is no object for the given key",
172+
run: function(env, test) {
173+
174+
global.localStorage = {
175+
getItem: function() {
176+
return null;
177+
}
178+
};
179+
180+
test.assert(util.getJSONFromLocalStorage('somekey'), null);
181+
}
154182
}
155183

156184
]

0 commit comments

Comments
 (0)