Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
GITHUB_USER=neondatabase
IMAGE_NAME=wsproxy
PLATFORMS=linux/amd64,linux/arm64
BUILDER_NAME=$(IMAGE_NAME)-builder
REMOTE_TAG=ghcr.io/$(GITHUB_USER)/$(IMAGE_NAME)

build:
docker build -t $(IMAGE_NAME) .

run:
docker run --rm -p 80:80 -p 2112:2112 --name $(IMAGE_NAME) $(IMAGE_NAME)

publish:
docker buildx create --use --name $(BUILDER_NAME) || true
docker buildx build --platform $(PLATFORMS) -t $(REMOTE_TAG) --push .
docker buildx rm $(BUILDER_NAME)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Lightweight websocket->TCP proxy. Look at `main.go` for available configuration options.

Read more about how to deploy with Neon Serverless proxy: https://github.com/neondatabase/serverless/blob/main/DEPLOY.md

Run:

```bash
Expand Down
175 changes: 175 additions & 0 deletions examples/with-unix-socket/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
24 changes: 24 additions & 0 deletions examples/with-unix-socket/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# with-unix-socket

Run Postgres instance with Unix socket and connect to it using Neon's WS Proxy and Drizzle ORM.

This project was created using `bun init` in bun v1.1.27. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

To install dependencies:

```bash
bun install
```

To run:

```bash
docker compose up -d
NEON_WS_PROXY_HOST=$(docker compose port neon 80) bun run index.ts
```

To clean up:

```bash
docker compose down
```
Binary file added examples/with-unix-socket/bun.lockb
Binary file not shown.
28 changes: 28 additions & 0 deletions examples/with-unix-socket/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
pgsql:
image: timescale/timescaledb:latest-pg16
restart: always
tmpfs:
- /var/lib/postgresql/data
volumes:
- pg_socket:/var/run/postgresql
environment:
POSTGRES_DB: neon-db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: neon-password

neon:
image: ghcr.io/flexchar/wsproxy:latest
restart: always
depends_on:
- pgsql
environment:
LOG_CONN_INFO: true
UNIX_SOCKET_PATH: "/var/run/postgresql/.s.PGSQL.5432"
volumes:
- pg_socket:/var/run/postgresql
ports:
- 80

volumes:
pg_socket:
39 changes: 39 additions & 0 deletions examples/with-unix-socket/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Pool, neonConfig } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
import { sql } from 'drizzle-orm';

// Docs
// https://orm.drizzle.team/docs/get-started-postgresql#neon-postgres
// Test with
// NEON_WS_PROXY_HOST=$(docker compose port neon 80) bun run index.ts

const NEON_WS_PROXY_HOST = process.env.NEON_WS_PROXY_HOST;
console.log('NEON_WS_PROXY_HOST', NEON_WS_PROXY_HOST);

if (!NEON_WS_PROXY_HOST) {
throw new Error('NEON_WS_PROXY_HOST is not set');
}

// Set the WebSocket proxy to work with the local instance
neonConfig.wsProxy = () => `${process.env.NEON_WS_PROXY_HOST}/v1`;

// Disable TLS when running on local machine
neonConfig.useSecureWebSocket = false; // or true, if you, for example, expose using Cloudflare Tunnel
// Disable all authentication and encryption
neonConfig.pipelineTLS = false;
neonConfig.pipelineConnect = false;

const connectionString = `pgsql://postgres:neon-password@placeholder/neon-db`;
// "placeholder" can be any string just to satisfy Neon's connection string format,
// because we're using Unix socket, we don't need to specify the host and port at all.

const pool = new Pool({ connectionString });
const db = drizzle(pool);

const result = await db.execute(sql`SELECT version()`);
const res = result.rows[0];

console.log(res);

await pool.end();
process.exit(0);
16 changes: 16 additions & 0 deletions examples/with-unix-socket/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "with-unix-socket",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest",
"drizzle-kit": "^0.24.2"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@neondatabase/serverless": "^0.9.5",
"drizzle-orm": "^0.33.0"
}
}
27 changes: 27 additions & 0 deletions examples/with-unix-socket/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
Loading