Skip to content

Commit

Permalink
Merge pull request #37 from balena-io-modules/data-directory-false
Browse files Browse the repository at this point in the history
Support using memory storage by passing dataDirectory: false
  • Loading branch information
thgreasi committed Jul 28, 2023
2 parents c0b3bb1 + c6c29b0 commit a52ebd3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Documentation
| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | options |
| options.dataDirectory | <code>string</code> | the directory to use for storage in Node.js. Ignored in the browser. |
| [options.dataDirectory] | <code>String</code> \| <code>False</code> | the directory to use for storage in Node.js or false to create an isolated in memory instance. Values other than false are ignored in the browser. |

**Example**
```js
Expand Down
23 changes: 15 additions & 8 deletions lib/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import * as localStore from './stores/local-storage';
import * as virtualStore from './stores/virtual-storage';
import { getStorage as $getStorage } from './storage';
import type { BalenaSettingsStorage } from './types';
import type {
BalenaSettingsStorageOptions,
BalenaSettingsStorage,
} from './types';

export type { BalenaSettingsStorage } from './types';
export type {
BalenaSettingsStorageOptions,
BalenaSettingsStorage,
} from './types';

export const getStorage = (options: {
dataDirectory?: string;
}): BalenaSettingsStorage => {
const store = localStore.isSupported()
? localStore.createStorage(options?.dataDirectory)
: virtualStore.createStore(options?.dataDirectory);
export const getStorage = (
options: BalenaSettingsStorageOptions,
): BalenaSettingsStorage => {
const store =
options?.dataDirectory === false || !localStore.isSupported()
? virtualStore.createStore()
: localStore.createStorage(options?.dataDirectory);
return $getStorage(store);
};
45 changes: 31 additions & 14 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import * as nodeStore from './stores/node-storage';
import { getStorage as $getStorage } from './storage';
import type { BalenaSettingsStorage, StorageLike } from './types';
import type {
BalenaSettingsStorage,
BalenaSettingsStorageOptions,
StorageLike,
} from './types';

export type { BalenaSettingsStorage } from './types';
export type {
BalenaSettingsStorageOptions,
BalenaSettingsStorage,
} from './types';

export const getStorage = (options: {
dataDirectory?: string;
}): BalenaSettingsStorage => {
// use dynamic imports so that node apps have less files to read on startup.
const lazyImport = {
virtual: () =>
require('./stores/virtual-storage') as typeof import('./stores/virtual-storage'),
local: () =>
require('./stores/local-storage') as typeof import('./stores/local-storage'),
node: () =>
require('./stores/node-storage') as typeof import('./stores/node-storage'),
};

export const getStorage = (
options: BalenaSettingsStorageOptions,
): BalenaSettingsStorage => {
let store: StorageLike;
if (typeof window !== 'undefined') {
// use dynamic imports so that node apps have less files to read on startup.
const localStore =
require('./stores/local-storage') as typeof import('./stores/local-storage');
if (options?.dataDirectory === false) {
store = lazyImport.virtual().createStore();
} else if (typeof window !== 'undefined') {
// Even though we specify an alternative file for this in the package.json's `browser` field
// we still need to handle the `isBrowser` case in the default file for the case that the
// bundler doesn't support/use the `browser` field.
const localStore = lazyImport.local();
store = localStore.isSupported()
? localStore.createStorage(options?.dataDirectory)
: (
require('./stores/virtual-storage') as typeof import('./stores/virtual-storage')
).createStore(options?.dataDirectory);
: lazyImport.virtual().createStore(options?.dataDirectory);
} else {
// Fallback to filesystem based storage if not in the browser.
store = nodeStore.createStorage(options?.dataDirectory);
store = lazyImport.node().createStorage(options?.dataDirectory);
}
return $getStorage(store);
};
10 changes: 5 additions & 5 deletions lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { BalenaSettingsStorage, StorageLike } from './types';
import { BalenaSettingsPermissionError } from 'balena-errors';

/**
* @module storage
*/

import { BalenaSettingsStorage, StorageLike } from './types';
import { BalenaSettingsPermissionError } from 'balena-errors';

/**
* @summary Get an instance of storage module
* @function
* @static
* @public
*
* @param {Object?} options - options
* @param {string?} options.dataDirectory - the directory to use for storage in Node.js. Ignored in the browser.
* @param {Object} options - options
* @param {String|False} [options.dataDirectory] - the directory to use for storage in Node.js or false to create an isolated in memory instance. Values other than false are ignored in the browser.
*
* @return {storage}
* @example
Expand Down
4 changes: 4 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface BalenaSettingsStorageOptions {
dataDirectory?: string | false;
}

export interface BalenaSettingsStorage {
set: (name: string, value: any) => Promise<void>;
get: (name: string) => Promise<string | number | object | undefined>;
Expand Down

0 comments on commit a52ebd3

Please sign in to comment.