Skip to content

Commit

Permalink
Add example Cloudflare Worker and test
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed May 10, 2023
1 parent 4ca56e2 commit 8249ba2
Show file tree
Hide file tree
Showing 7 changed files with 1,159 additions and 7 deletions.
6 changes: 5 additions & 1 deletion packages/pg/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ help:

test: test-unit

test-all: test-missing-native test-unit test-integration test-native
test-all: test-missing-native test-unit test-integration test-native test-worker


update-npm:
Expand Down Expand Up @@ -59,3 +59,7 @@ test-binary: test-connection

test-pool:
@find test/integration/connection-pool -name "*.js" | $(node-command) binary

test-worker:
@echo "***Testing Cloudflare Worker support***"
@node test/worker/src/index.test.js
7 changes: 6 additions & 1 deletion packages/pg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@
"pgpass": "1.x"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230404.0",
"@miniflare/tre": "^3.0.0-next.13",
"async": "2.6.4",
"bluebird": "3.5.2",
"co": "4.6.0",
"pg-copy-streams": "0.3.0"
"pg-copy-streams": "0.3.0",
"typescript": "^4.0.3",
"workerd": "^1.20230419.0",
"wrangler": "^2.16.0"
},
"peerDependencies": {
"pg-native": ">=3.0.1",
Expand Down
31 changes: 31 additions & 0 deletions packages/pg/test/worker/src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if (parseInt(process.versions.node.split('.')[0]) < 16) {
process.exit(0)
}
var helper = require('../../test-helper')
const path = require('path')
const { unstable_dev } = require('wrangler')

var suite = new helper.Suite()

suite.testAsync('Can run in Cloudflare Worker?', test())

async function test() {
const worker = await unstable_dev(path.resolve(__dirname, './index.ts'), {
config: path.resolve(__dirname, '../wrangler.toml'),
vars: {
...process.env,
},
experimental: {
experimentalLocal: true,
disableExperimentalWarning: true,
},
logLevel: 'ERROR',
})
try {
const resp = await worker.fetch('/')
const { rows } = await resp.json()
assert.same(rows[0].text, 'Hello, World!')
} finally {
await worker.stop()
}
}
28 changes: 28 additions & 0 deletions packages/pg/test/worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Client } from 'pg'

export interface Env {
USER: string
PGUSER: string
PGPASSWORD: string
}

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url)
if (url.pathname === '/favicon.ico') return new Response(null, { status: 404 })

const params = url.searchParams
const ssl = params.has('ssl')

var client = new Client({
user: env.PGUSER || env.USER,
password: env.PGPASSWORD,
ssl,
})
await client.connect()
const resp = Response.json(await client.query('SELECT $1::text', ['Hello, World!']))
// Clean up the client, ensuring we don't kill the worker before that is completed.
ctx.waitUntil(client.end())
return resp
},
}
22 changes: 22 additions & 0 deletions packages/pg/test/worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "es2021",
"lib": [
"es2021"
],
"module": "es2022",
"moduleResolution": "node",
"types": [
"@cloudflare/workers-types"
],
"resolveJsonModule": true,
"allowJs": true,
"checkJs": false,
"noEmit": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
}
}
5 changes: 5 additions & 0 deletions packages/pg/test/worker/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "pg-cf-test"
main = "src/index.ts"
compatibility_date = "2023-04-04"
compatibility_flags = ["tcp_sockets_support"]
node_compat = true
Loading

0 comments on commit 8249ba2

Please sign in to comment.