Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
v-moravec opened this issue May 26, 2023 · 7 comments
Closed

Comments

@v-moravec
Copy link

v-moravec commented May 26, 2023

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)

@v-moravec v-moravec changed the title 0.4.5 Error 0.4.5 Error - Cannot read properties of undefined (reading 'subtle') May 26, 2023
@jawj
Copy link
Collaborator

jawj commented May 28, 2023

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

  • If you have ?sslmode=require at the end of your connection string, are you able to take that off (e.g. if connecting to a local DB in development)?
  • Can you use Node 20 instead of Node 18 (where crypto.subtle is defined without needing to import anything)?
  • Can you run Node 18 with the --experimental-global-webcrypto flag?
  • Can you import crypto as a global before you dynamically import the driver? That would look something like:
import crypto from "crypto";
globalThis.crypto = crypto;
const { Pool } = await import('@neondatabase/serverless');

@v-moravec
Copy link
Author

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
Copy link
Collaborator

jawj commented May 31, 2023

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
Copy link
Author

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
Copy link
Collaborator

jawj commented Jun 6, 2023

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 (evanw/esbuild#1719, evanw/esbuild#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
Copy link
Collaborator

jawj commented Jun 9, 2023

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

@v-moravec
Copy link
Author

I can confirm everything works as expected now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants