From e1c234eaa59db02bbc76d5b0272b153bb2a238ae Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 4 May 2022 14:58:28 +0200 Subject: [PATCH] feat: `nitro.storage` --- src/nitro.ts | 8 +++++++- src/storage.ts | 7 ++----- src/types/nitro.ts | 6 ++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/nitro.ts b/src/nitro.ts index c273c2cda9..c51b714862 100644 --- a/src/nitro.ts +++ b/src/nitro.ts @@ -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 { // Resolve options @@ -17,9 +18,14 @@ export async function createNitro (config: NitroConfig = {}): Promise { 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 diff --git a/src/storage.ts b/src/storage.ts index 83cc56ed97..fbefc0ea76 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -27,18 +27,15 @@ export async function createStorage (nitro: Nitro) { } export async function snapshotStorage (nitro: Nitro) { - const storage = await createStorage(nitro) const data: Record = {} 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 } diff --git a/src/types/nitro.ts b/src/types/nitro.ts index 389c362588..7648fce401 100644 --- a/src/types/nitro.ts +++ b/src/types/nitro.ts @@ -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, @@ -18,6 +19,7 @@ export interface Nitro { hooks: Hookable unimport?: Unimport logger: Consola + storage: Storage close: () => Promise }