Skip to content

Custom drivers #3847

@maximelebreton

Description

@maximelebreton

Hello Nitro team!

Expected behavior

I would expect Nitro to support custom driver functions or objects in the storage configuration, not just string identifiers.

Or clarify the documentation that only string identifiers for built-in drivers are supported in nuxt.config.ts: https://unstorage.unjs.io/guide/custom-driver


Describe the bug

When configuring a custom storage driver for Nitro in nuxt.config.ts using defineDriver, Nitro tries to resolve the driver function or object as a package. This results in an error like:

// with driver: upstashCustomDriver()
Cannot find package [object Object]' imported from node_modules/.../nitropack/dist/core/index.mjs
// with driver: upstashCustomDriver
Cannot find package '(opts = {}) => { … }' imported from node_modules/.../nitropack/dist/core/index.mjs

Currently, only string identifiers for built-in drivers (e.g. 'redis', 'memory') work in the config.


Environment

  • Nuxt / Nitro version: 3.x / 2.x
  • Deployment: Vercel serverless
  • Storage: Custom Unstorage driver based on Upstash

Reproduction

// drivers/upstashCustomDriver.ts
import { defineDriver } from 'unstorage';
import { upstashDriver } from 'unstorage/drivers';

export const upstashCustomDriver = defineDriver((opts = {}) => {
  const driver = upstashDriver(opts);
  return {
    ...driver,
    async setItem(key: string, value: any, opts?: { ttl?: number }) {
      const modifiedOpts = opts?.ttl ? { ...opts, ttl: Math.ceil(opts.ttl / 1000) } : opts;
      return driver.setItem!(key, value, modifiedOpts);
    }
  };
});
// nuxt.config.ts
import { upstashCustomDriver } from './drivers/upstashCustomDriver';

export default defineNuxtConfig({
  nitro: {
    storage: {
      upstash: {
        driver: upstashCustomDriver(), // ❌ causes error on Vercel
        //driver: upstashCustomDriver, // ❌ causes error too
        url: process.env.KV_REST_API_URL,
        token: process.env.KV_REST_API_TOKEN
      }
    }
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions