Closed bbigras closed 1 year ago
➕ We are evaluating cloudflare + neon, and are also seeing this issue. Even taking cloudflare out of the mix, and just running a file along the lines below, results in the error (this is on node 18):
// @ts-expect-error no types
import { Client } from '@neondatabase/serverless';
const client = new Client('postgres://{USER}:{PASS}@{ENDPOINT}.us-east-2.aws.neon.tech/{DB}');
await client.connect();
const {
rows: [{ now }],
} = await client.query('select now();');
console.log('NOW!', now);
Any ideas?
Ok, in a node environment it seems one has to manually set neonConfig.webSocketConstructor
to the ws
library, along these lines:
// @ts-expect-error no types
import { Client, neonConfig } from '@neondatabase/serverless';
import ws from 'ws';
neonConfig.webSocketConstructor = ws;
const client = new Client('postgres://{USER}:{PASS}@{ENDPOINT}.us-east-2.aws.neon.tech/{DB}');
await client.connect();
const {
rows: [{ now }],
} = await client.query('select now();');
console.log('NOW!', now);
Right, you get 'invalid upgrade header' if no WebSocket object is available, because then we try the Cloudflare method where you add that header to an ordinary fetch call.
As you've discovered, you can fix this in Node (and possibly elsewhere) by manually supplying a WebSocket constructor function, such as provided by the ws library.
This is new and a bit experimental, which is why it's not documented yet. I'm actually looking now at whether we can skip the config and just use ws automatically if the package is installed. And to provide a more informative error message if not!
@bbigras Does the above suggestion (using webSocketConstructor
) fix things for you too?
YES IT WORKS!!! :tada: Thank you both so much!!!
Note that if you forget to specify the password in the URL, you get:
TypeError: ji.join is not a function
at ve.exports.getFileName (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:2:308)
at Ar.exports (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:2:2063)
at jt._checkPgPass (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:43:25699)
at jt._handleAuthCleartextPassword (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:43:25840)
at hn.<anonymous> (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:43:46157)
at M.emit (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:1:30872)
at G.<anonymous> (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:43:19460)
at Object.Jo (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:1:31814)
at M.emit (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:1:30813)
at G.connect (/home/bbigras/dev/test-qwik-neon/node_modules/@neondatabase/serverless/index.js:43:3222)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Thanks — with the latest version you should get a more helpful error message if you forget to include the password.
Steps to reproduce
src/routes/index.tsx
by the connection string from neon.tech (ex:postgres://bbigras@SOMETHING.us-east-2.aws.neon.tech/neondb
)Is the connection string the right URL?
invalid upgrade header
sounds like a websocket error, so I'm wondering if I was supposed to use the connection string or maybe a URL for the proxy.Expected result
Actual result
Environment
npm: '9.3.1', node: '18.14.0'
Logs, links