From 106b2ce73664a26a0ab3e163949b161186bcf1cc Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Wed, 26 Jul 2023 17:08:56 +0300 Subject: [PATCH] Support using memory storage by passing dataDirectory: false Change-type: minor --- lib/index.browser.ts | 23 ++++++++++++++-------- lib/index.ts | 45 ++++++++++++++++++++++++++++++-------------- lib/storage.ts | 6 +++--- lib/types.ts | 4 ++++ 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/lib/index.browser.ts b/lib/index.browser.ts index 4be5571..96aede8 100644 --- a/lib/index.browser.ts +++ b/lib/index.browser.ts @@ -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); }; diff --git a/lib/index.ts b/lib/index.ts index 15728e5..f82e59b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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); }; diff --git a/lib/storage.ts b/lib/storage.ts index 1aa92dd..905f7d6 100644 --- a/lib/storage.ts +++ b/lib/storage.ts @@ -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 diff --git a/lib/types.ts b/lib/types.ts index b84eaef..4f202be 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,3 +1,7 @@ +export interface BalenaSettingsStorageOptions { + dataDirectory?: string | false; +} + export interface BalenaSettingsStorage { set: (name: string, value: any) => Promise; get: (name: string) => Promise;