| 1 |
'use strict'; |
| 2 |
const EXPIRY_PREFIX = '_EXPIRY_'; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
function SafeStorage( store ) { |
| 19 |
this.store = store; |
| 20 |
|
| 21 |
|
| 22 |
if ( !window.QUnit ) { |
| 23 |
const storage = this; |
| 24 |
setTimeout( () => { |
| 25 |
storage.clearExpired(); |
| 26 |
}, 2000 ); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
SafeStorage.prototype.get = function ( key ) { |
| 38 |
if ( this.isExpired( key ) ) { |
| 39 |
return null; |
| 40 |
} |
| 41 |
try { |
| 42 |
return this.store.getItem( key ); |
| 43 |
} catch ( e ) {} |
| 44 |
return false; |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
SafeStorage.prototype.set = function ( key, value, expiry ) { |
| 56 |
if ( key.slice( 0, EXPIRY_PREFIX.length ) === EXPIRY_PREFIX ) { |
| 57 |
throw new Error( 'Key can\'t have a prefix of ' + EXPIRY_PREFIX ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
if ( this.setExpires( key, expiry ) === false ) { |
| 62 |
|
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
try { |
| 67 |
this.store.setItem( key, value ); |
| 68 |
return true; |
| 69 |
} catch ( e ) {} |
| 70 |
return false; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
SafeStorage.prototype.remove = function ( key ) { |
| 80 |
try { |
| 81 |
this.store.removeItem( key ); |
| 82 |
this.setExpires( key ); |
| 83 |
return true; |
| 84 |
} catch ( e ) {} |
| 85 |
return false; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
SafeStorage.prototype.getObject = function ( key ) { |
| 96 |
const json = this.get( key ); |
| 97 |
|
| 98 |
if ( json === false ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
try { |
| 103 |
return JSON.parse( json ); |
| 104 |
} catch ( e ) {} |
| 105 |
|
| 106 |
return null; |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
SafeStorage.prototype.setObject = function ( key, value, expiry ) { |
| 118 |
let json; |
| 119 |
try { |
| 120 |
json = JSON.stringify( value ); |
| 121 |
return this.set( key, json, expiry ); |
| 122 |
} catch ( e ) {} |
| 123 |
return false; |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
SafeStorage.prototype.setExpires = function ( key, expiry ) { |
| 136 |
if ( expiry ) { |
| 137 |
try { |
| 138 |
this.store.setItem( |
| 139 |
EXPIRY_PREFIX + key, |
| 140 |
Math.floor( Date.now() / 1000 ) + expiry |
| 141 |
); |
| 142 |
return true; |
| 143 |
} catch ( e ) {} |
| 144 |
} else { |
| 145 |
try { |
| 146 |
this.store.removeItem( EXPIRY_PREFIX + key ); |
| 147 |
return true; |
| 148 |
} catch ( e ) {} |
| 149 |
} |
| 150 |
return false; |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
const MIN_WORK_TIME = 3; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
SafeStorage.prototype.clearExpired = function () { |
| 163 |
const storage = this; |
| 164 |
return this.getExpiryKeys().then( ( keys ) => $.Deferred( ( d ) => { |
| 165 |
mw.requestIdleCallback( function iterate( deadline ) { |
| 166 |
while ( keys[ 0 ] !== undefined && deadline.timeRemaining() > MIN_WORK_TIME ) { |
| 167 |
const key = keys.shift(); |
| 168 |
if ( storage.isExpired( key ) ) { |
| 169 |
storage.remove( key ); |
| 170 |
} |
| 171 |
} |
| 172 |
if ( keys[ 0 ] !== undefined ) { |
| 173 |
|
| 174 |
mw.requestIdleCallback( iterate ); |
| 175 |
} else { |
| 176 |
return d.resolve(); |
| 177 |
} |
| 178 |
} ); |
| 179 |
} ) ); |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
SafeStorage.prototype.getExpiryKeys = function () { |
| 190 |
const store = this.store; |
| 191 |
return $.Deferred( ( d ) => { |
| 192 |
mw.requestIdleCallback( ( deadline ) => { |
| 193 |
const prefixLength = EXPIRY_PREFIX.length; |
| 194 |
const keys = []; |
| 195 |
let length = 0; |
| 196 |
try { |
| 197 |
length = store.length; |
| 198 |
} catch ( e ) {} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
for ( let i = 0; ( i < length && deadline.timeRemaining() > MIN_WORK_TIME ); i++ ) { |
| 209 |
let key = null; |
| 210 |
try { |
| 211 |
key = store.key( i ); |
| 212 |
} catch ( e ) {} |
| 213 |
if ( key !== null && key.slice( 0, prefixLength ) === EXPIRY_PREFIX ) { |
| 214 |
keys.push( key.slice( prefixLength ) ); |
| 215 |
} |
| 216 |
} |
| 217 |
d.resolve( keys ); |
| 218 |
} ); |
| 219 |
} ).promise(); |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
SafeStorage.prototype.isExpired = function ( key ) { |
| 230 |
let expiry; |
| 231 |
try { |
| 232 |
expiry = this.store.getItem( EXPIRY_PREFIX + key ); |
| 233 |
} catch ( e ) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
return !!expiry && expiry < Math.floor( Date.now() / 1000 ); |
| 237 |
}; |
| 238 |
|
| 239 |
module.exports = SafeStorage; |