File tree Expand file tree Collapse file tree 6 files changed +54
-24
lines changed Expand file tree Collapse file tree 6 files changed +54
-24
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ const isFolder = util.isFolder;
4646const cleanPath = util . cleanPath ;
4747const shouldBeTreatedAsBinary = util . shouldBeTreatedAsBinary ;
4848const 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' ) ;
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ const isFolder = util.isFolder;
3333const cleanPath = util . cleanPath ;
3434const shouldBeTreatedAsBinary = util . shouldBeTreatedAsBinary ;
3535const readBinaryData = util . readBinaryData ;
36+ const getJSONFromLocalStorage = util . getJSONFromLocalStorage ;
3637
3738let hasLocalStorage ;
3839
@@ -142,12 +143,7 @@ const GoogleDrive = function (remoteStorage, clientId) {
142143 hasLocalStorage = util . localStorageAvailable ( ) ;
143144
144145 if ( hasLocalStorage ) {
145- let settings ;
146- try {
147- settings = JSON . parse ( localStorage . getItem ( SETTINGS_KEY ) ) ;
148- } catch ( e ) {
149- // no settings stored
150- }
146+ const settings = getJSONFromLocalStorage ( SETTINGS_KEY ) ;
151147 if ( settings ) {
152148 this . configure ( settings ) ;
153149 }
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ const log = require('./log');
1212const Features = require ( './features' ) ;
1313const globalContext = util . getGlobalContext ( ) ;
1414const eventHandling = require ( './eventhandling' ) ;
15+ const getJSONFromLocalStorage = util . getJSONFromLocalStorage ;
1516
1617var 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
Original file line number Diff line number Diff line change @@ -242,6 +242,23 @@ var util = {
242242 }
243243 } ,
244244
245+ /**
246+ * Extract and parse JSON data from localStorage.
247+ *
248+ * @param {string } key - localStorage key
249+ *
250+ * @returns {object } parsed object or undefined
251+ */
252+ getJSONFromLocalStorage ( key ) {
253+ const context = util . getGlobalContext ( ) ;
254+
255+ try {
256+ return JSON . parse ( context . localStorage . getItem ( key ) ) ;
257+ } catch ( e ) {
258+ // no JSON stored
259+ }
260+ } ,
261+
245262 /**
246263 * Decide if data should be treated as binary based on the content
247264 * and content-type.
Original file line number Diff line number Diff line change @@ -75,6 +75,7 @@ if (typeof(ArrayBufferView) === 'function') {
7575const isFolder = util . isFolder ;
7676const cleanPath = util . cleanPath ;
7777const shouldBeTreatedAsBinary = util . shouldBeTreatedAsBinary ;
78+ const getJSONFromLocalStorage = util . getJSONFromLocalStorage ;
7879
7980function 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 ) ;
Original file line number Diff line number Diff 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 ]
You can’t perform that action at this time.
0 commit comments