Skip to content

Commit

Permalink
Support using memory storage by passing dataDirectory: false
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
thgreasi committed Jul 27, 2023
1 parent 1061df2 commit 106b2ce
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
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);
};
6 changes: 3 additions & 3 deletions lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ 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
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 106b2ce

Please sign in to comment.