Skip to content

Commit

Permalink
Merge pull request #128 from electron-userland/retry-size-zero
Browse files Browse the repository at this point in the history
Retry write if resulting file size is zero when using validate=true
  • Loading branch information
jviotti committed Jul 9, 2020
2 parents fff1ba6 + c668e1f commit 964d876
Show file tree
Hide file tree
Showing 5 changed files with 571 additions and 474 deletions.
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ Documentation
<a name="module_storage.getDefaultDataPath"></a>

### storage.getDefaultDataPath() ⇒ <code>String</code>
**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Get the default data path
**Returns**: <code>String</code> - default data path
**Access:** public
**Access**: public
**Example**
```js
const defaultDataPath = storage.getDefaultDataPath()
Expand All @@ -60,13 +60,13 @@ const defaultDataPath = storage.getDefaultDataPath()
### storage.setDataPath(directory)
The default value will be used if the directory is undefined.

**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Set current data path
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| directory | <code>String</code> &#124; <code>Undefined</code> | directory |
| directory | <code>String</code> \| <code>Undefined</code> | directory |

**Example**
```js
Expand All @@ -81,10 +81,10 @@ storage.setDataPath(os.tmpdir());
Returns the current data path. It defaults to a directory called
"storage" inside Electron's `userData` path.

**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Get current user data path
**Returns**: <code>String</code> - the user data path
**Access:** public
**Access**: public
**Example**
```js
const storage = require('electron-json-storage');
Expand All @@ -103,9 +103,9 @@ Passing an extension other than `.json` will result in a file created
with both extensions. For example, the key `foo.data` will result in a file
called `foo.data.json`.

**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Read user data
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -130,9 +130,9 @@ storage.get('foobar', function(error, data) {
This function returns an object with the data of all the passed keys.
If one of the keys doesn't exist, an empty object is returned for it.

**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Read many user data keys
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -157,9 +157,9 @@ storage.getMany([ 'foobar', 'barbaz' ], function(error, data) {
### storage.getAll([options], callback)
This function returns an empty object if there is no data to be read.

**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Read all user data
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -180,16 +180,17 @@ storage.getAll(function(error, data) {
<a name="module_storage.set"></a>

### storage.set(key, json, [options], callback)
**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Write user data
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| key | <code>String</code> | key |
| json | <code>Object</code> | json object |
| [options] | <code>Object</code> | options |
| [options.dataPath] | <code>String</code> | data path |
| [options.validate] | <code>String</code> | validate writes by reading the data back |
| callback | <code>function</code> | callback (error) |

**Example**
Expand All @@ -203,9 +204,9 @@ storage.set('foobar', { foo: 'bar' }, function(error) {
<a name="module_storage.has"></a>

### storage.has(key, [options], callback)
**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Check if a key exists
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -229,9 +230,9 @@ storage.has('foobar', function(error, hasKey) {
<a name="module_storage.keys"></a>

### storage.keys([options], callback)
**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Get the list of saved keys
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -257,9 +258,9 @@ storage.keys(function(error, keys) {
Notice this function does nothing, nor throws any error
if the key doesn't exist.

**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Remove a key
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
Expand All @@ -279,9 +280,9 @@ storage.remove('foobar', function(error) {
<a name="module_storage.clear"></a>

### storage.clear([options], callback)
**Kind**: static method of <code>[storage](#module_storage)</code>
**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Clear all stored data in the current user data path
**Access:** public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
Expand Down
33 changes: 31 additions & 2 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ exports.getAll = function(options, callback) {
* @param {Object} json - json object
* @param {Object} [options] - options
* @param {String} [options.dataPath] - data path
* @param {String} [options.validate] - validate writes by reading the data back
* @param {Function} callback - callback (error)
*
* @example
Expand All @@ -293,7 +294,11 @@ exports.getAll = function(options, callback) {
* if (error) throw error;
* });
*/
exports.set = function(key, json, options, callback) {
exports.set = function(key, json, options, callback, retries) {
if (!_.isNumber(retries)) {
retries = 10;
}

if (_.isFunction(options)) {
callback = options;
}
Expand Down Expand Up @@ -337,7 +342,31 @@ exports.set = function(key, json, options, callback) {
return callback(error);
}

return callback(lockError);
if (!options.validate) {
return callback(lockError);
}

// Check that the writes were actually successful
// after a little bit
setTimeout(function() {
exports.get(key, {
dataPath: options.dataPath
}, function(getError, data) {
if (getError) {
return callback(getError);
}

if (!_.isEqual(data, json)) {
if (retries <= 0) {
throw new Error('Couldn\'t ensure data was written correctly');
}

return exports.set(key, json, options, callback, retries - 1);
}

return callback();
});
}, 100);
});
});
};
Expand Down
Loading

0 comments on commit 964d876

Please sign in to comment.