-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support using memory storage by passing dataDirectory: false
Change-type: minor
- Loading branch information
Showing
5 changed files
with
56 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters