-
Notifications
You must be signed in to change notification settings - Fork 294
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
support neon's sql-over-websocket protocol #955
base: master
Are you sure you want to change the base?
Conversation
tcp.readyState = "opening"; | ||
const rootURL = host + "/v2" + "?address=" + host + ":" + port; | ||
const socketURL = "wss://" + rootURL; | ||
tcp.ws = new WebSocket(socketURL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obviously, the big change here is that we're using WebSocket instead of cloudflare's "connect" utility.
} | ||
|
||
async function startTls(host) { | ||
throw new Error("Postgres SSL connections are not supported yet"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The connection goes over secure web sockets (wss://
) so we don't need to double encrypt the content. As such, we don't support tls from postgres.
keep_alive && socket.setKeepAlive && socket.setKeepAlive(true, 1000 * keep_alive) | ||
const s = StartupMessage() | ||
write(s) | ||
AuthenticationCleartextPassword() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This utilizes neon's support for "pipelining" these initial messages to the db. We send the cleartext password and ready for query events before the database actually requests those messages. This cuts down the number of round trips needed to the initialize the connection.
You read more about this here: https://neon.tech/blog/quicker-serverless-postgres
x === 49 ? ParseComplete : // 1 | ||
x === 116 ? ParameterDescription : // t | ||
x === 84 ? RowDescription : // T | ||
x === 82 ? noop : // R |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can safely ignore the "Authentication" request because we've already sent the cleartext password.
Any chance to get this PR approved ? It would be a great addition and help to use postgres.js with neon, Thanks for the effort so far :) |
FWIW, we are using this in production. You can get it here: |
Thanks for the heads-up! I assume you meant this repo? The link you shared returns a 404—perhaps it's private? 🔗 https://github.com/brevitybuilder/postgres-fork It would be great if the repo's author could review this PR so we can work towards including it in the next release. Let us know if there's anything we can do to help make that happen! |
+1 This would be great. |
Neon is a popular postgres provider that is an open source alternative to AWS Aurora. In order to support connecting to their database in more runtimes (like cloudflare workers), they have implemented a "sql-over-websocket" protocol that these runtimes can use to connect to their database with less "cold-start" latency than other approaches (like hyperdrive).
If you're open to landing this change in this repo, that would be awesome! That said, I understand that this use-case might be niche enough that you may not want to sign up for the ongoing maintenance. If that is the case, I will likely just publish a fork to our own npm namespace to support this use-case.
Notes
Potential future work