Skip to content

Commit

Permalink
feat: nitro.storage
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 4, 2022
1 parent 929322b commit e1c234e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createUnimport } from 'unimport'
import consola from 'consola'
import type { NitroConfig, Nitro } from './types'
import { loadOptions } from './options'
import { createStorage } from './storage'

export async function createNitro (config: NitroConfig = {}): Promise<Nitro> {
// Resolve options
Expand All @@ -17,9 +18,14 @@ export async function createNitro (config: NitroConfig = {}): Promise<Nitro> {
vfs: {},
logger: consola.withTag('nitro'),
scannedHandlers: [],
close: () => nitro.hooks.callHook('close')
close: () => nitro.hooks.callHook('close'),
storage: undefined
}

// Storage
nitro.storage = await createStorage(nitro)
nitro.hooks.hook('close', async () => { await nitro.storage.dispose() })

// Logger config
if (nitro.options.logLevel !== undefined) {
nitro.logger.level = nitro.options.logLevel
Expand Down
7 changes: 2 additions & 5 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ export async function createStorage (nitro: Nitro) {
}

export async function snapshotStorage (nitro: Nitro) {
const storage = await createStorage(nitro)
const data: Record<string, any> = {}

const allKeys = Array.from(new Set(await Promise.all(
nitro.options.bundledStorage.map(base => storage.getKeys(base))
nitro.options.bundledStorage.map(base => nitro.storage.getKeys(base))
).then(r => r.flat())))

await Promise.all(allKeys.map(async (key) => {
data[key] = await storage.getItem(key)
data[key] = await nitro.storage.getItem(key)
}))

await storage.dispose()

return data
}
6 changes: 4 additions & 2 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import type { Unimport, UnimportOptions } from 'unimport'
import type { PluginVisualizerOptions } from 'rollup-plugin-visualizer'
import type { NestedHooks, Hookable } from 'hookable'
import type { Consola, LogLevel } from 'consola'
import { WatchOptions } from 'chokidar'
import type { WatchOptions } from 'chokidar'
import type { RollupCommonJSOptions } from '@rollup/plugin-commonjs'
import type { Storage } from 'unstorage'
import type { NodeExternalsOptions } from '../rollup/plugins/externals'
import type { RollupConfig } from '../rollup/config'
import type { Options as EsbuildOptions } from '../rollup/plugins/esbuild'
import { NitroErrorHandler, NitroDevEventHandler, NitroEventHandler } from './handler'
import type { NitroErrorHandler, NitroDevEventHandler, NitroEventHandler } from './handler'

export interface Nitro {
options: NitroOptions,
Expand All @@ -18,6 +19,7 @@ export interface Nitro {
hooks: Hookable<NitroHooks>
unimport?: Unimport
logger: Consola
storage: Storage
close: () => Promise<void>
}

Expand Down

0 comments on commit e1c234e

Please sign in to comment.