Skip to content

Commit

Permalink
Graceful fallback if the renderer process can't access the remote module
Browse files Browse the repository at this point in the history
Right now this module will crash when being ran on a renderer process
that cannot access the `remote` module for determining a default data
path. After this commit, the module will gracefully require users to
call `.setDataPath()` if `remote.app` cannot be accessed.

See: #155
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti committed Feb 18, 2021
1 parent d07b2c0 commit 4255dfc
Show file tree
Hide file tree
Showing 4 changed files with 4,524 additions and 37 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Documentation


* [storage](#module_storage)
* [.getDefaultDataPath()](#module_storage.getDefaultDataPath) ⇒ <code>String</code>
* [.getDefaultDataPath()](#module_storage.getDefaultDataPath) ⇒ <code>String</code> \| <code>Null</code>
* [.setDataPath(directory)](#module_storage.setDataPath)
* [.getDataPath()](#module_storage.getDataPath) ⇒ <code>String</code>
* [.get(key, [options], callback)](#module_storage.get)
Expand All @@ -77,10 +77,15 @@ Documentation

<a name="module_storage.getDefaultDataPath"></a>

### storage.getDefaultDataPath() ⇒ <code>String</code>
### storage.getDefaultDataPath() ⇒ <code>String</code> \| <code>Null</code>
This function will return `null` when running in the
renderer process without support for the `remote` IPC
mechanism. You have to explicitly set a data path using
`.setDataPath()` in these cases.

**Kind**: static method of [<code>storage</code>](#module_storage)
**Summary**: Get the default data path
**Returns**: <code>String</code> - default data path
**Returns**: <code>String</code> \| <code>Null</code> - default data path
**Access**: public
**Example**
```js
Expand Down Expand Up @@ -222,6 +227,7 @@ storage.getAll(function(error, data) {
| [options] | <code>Object</code> | options |
| [options.dataPath] | <code>String</code> | data path |
| [options.validate] | <code>String</code> | validate writes by reading the data back |
| [options.prettyPrinting] | <code>boolean</code> | adds line breaks and spacing to the written data |
| callback | <code>function</code> | callback (error) |

**Example**
Expand Down
8 changes: 7 additions & 1 deletion lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ const readFile = function(fileName, callback, times) {
* @function
* @public
*
* @returns {String} default data path
* @description
* This function will return `null` when running in the
* renderer process without support for the `remote` IPC
* mechanism. You have to explicitly set a data path using
* `.setDataPath()` in these cases.
*
* @returns {(String|Null)} default data path
*
* @example
* const defaultDataPath = storage.getDefaultDataPath()
Expand Down
13 changes: 11 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
const _ = require('lodash');
const path = require('path');
const electron = require('electron');
const app = electron.app || electron.remote.app;
const app = electron.app || (electron.remote && electron.remote.app) || null;

/**
* @summary Get the default data path
Expand All @@ -40,6 +40,10 @@ const app = electron.app || electron.remote.app;
* const defaultDataPath = utils.getDefaultDataPath()
*/
exports.getDefaultDataPath = function() {
if (!app) {
return null
}

return path.join(app.getPath('userData'), 'storage');
};

Expand Down Expand Up @@ -123,7 +127,12 @@ exports.getFileName = function(key, options) {
const escapedFileName = encodeURIComponent(keyFileName)
.replace(/\*/g, '-').replace(/%20/g, ' ');

return path.join(options.dataPath || exports.getDataPath(), escapedFileName);
const dataPath = options.dataPath || exports.getDataPath()
if (!dataPath) {
throw new Error('You must explicitly set a data path')
}

return path.join(dataPath, escapedFileName);
};

/**
Expand Down
Loading

0 comments on commit 4255dfc

Please sign in to comment.