neondatabase / serverless

Connect to Neon PostgreSQL from serverless/worker/edge functions
https://www.npmjs.com/package/@neondatabase/serverless
MIT License
321 stars 13 forks source link

0.4.5 Error - Cannot read properties of undefined (reading 'subtle') #19

Closed v-moravec closed 1 year ago

v-moravec commented 1 year ago

Steps to reproduce

npx nuxi init \<project-name> yarn add drizzle-orm @neondatabase/serverless yarn add -D drizzle-kit

db.ts

import {neonConfig, Pool} from '@neondatabase/serverless';
import {drizzle, NeonDatabase} from 'drizzle-orm/neon-serverless';
import * as schema from '~/server/drizzle/schema';

// if we're running locally
if (!process.env.VERCEL_ENV) {
  // Set the WebSocket proxy to work with the local instance
  neonConfig.wsProxy = (host) => `${host}:5433/v1`
  // Disable all authentication and encryption
  neonConfig.useSecureWebSocket = false
  neonConfig.pipelineTLS = false
  neonConfig.pipelineConnect = false
}

const config = useRuntimeConfig()

if (!config.dbUrl) {
  throw new Error('POSTGRES_URL is not set')
}

let db: NeonDatabase<typeof schema> | undefined = undefined

export const useDB = () => {
  if (db) {
    return db
  }
  const pool = new Pool({ connectionString: config.dbUrl })
  db = drizzle(pool, { schema })
  return db
}

db = useDB()

Expected result

Should work

Actual result

Instead throws error

Environment

Nuxt 3.5 @neondatabase/serverless 0.4.5 Node v18.12.0

Logs, links

ERROR [worker reload] [worker init] Cannot read properties of undefined (reading 'subtle') 3:07:35 PM

at node_modules/@neondatabase/serverless/index.js:1:59242 at node_modules/@neondatabase/serverless/index.js:1:347 at node_modules/@neondatabase/serverless/index.js:1:60107 at node_modules/@neondatabase/serverless/index.js:1:347 at node_modules/@neondatabase/serverless/index.js:46:2299 at node_modules/@neondatabase/serverless/index.js:1:347 at node_modules/@neondatabase/serverless/index.js:46:25042 at node_modules/@neondatabase/serverless/index.js:1:347 at Object. (node_modules/@neondatabase/serverless/index.js:46:25975) at Module._compile (node:internal/modules/cjs/loader:1159:14)

jawj commented 1 year ago

Sounds like the driver is trying to access SubtleCrypto in a context where it's not available.

import crypto from "crypto";
globalThis.crypto = crypto;
const { Pool } = await import('@neondatabase/serverless');
v-moravec commented 1 year ago

Well, it seems to be fixed with 0.4.9 (also works in 0.4.4 and lower). However with 0.4.9 I'm getting this now on Cloudflare Pages (using Workers under the hood) - Uncaught TypeError: Pool is not a constructor, locally it works ok with yarn dev, but not with building it with wrangler and wrangler pages dev dist/.

jawj commented 1 year ago

OK, good to hear. I think changes in the underlying pg package, which I'd previously omitted to pin to a specific version, must have been responsible for the earlier issues, then.

I've not encountered this new error before. Are you able to provide a simple repro?

v-moravec commented 1 year ago

Sure, here it is - https://github.com/v-moravec/drizzle-cf-error, hope it helps. I turned off minification so error messages are clear. Everything else that needs to be setup is in readme.

jawj commented 1 year ago

OK, I think the relevant change in 0.4.4 is that we introduced a default export (we're testing something here that will be launched soon). Anyway, it seems the default import is breaking named imports in this particular context.

I'm not yet sure where the fault lies, if anywhere, because judging by these esbuild issues (https://github.com/evanw/esbuild/issues/1719, https://github.com/evanw/esbuild/issues/532), default import interoperability is a minefield.

So I worry that fixing this may break something else. But anyway, I'm continuing to investigate ...

jawj commented 1 year ago

I've just released 0.4.10, which I believe will fix your import problem. Please do let me know either way.

v-moravec commented 1 year ago

I can confirm everything works as expected now.