Skip to content

Commit 7152d4d

Browse files
Add example Cloudflare Worker and test
1 parent 0755342 commit 7152d4d

File tree

7 files changed

+853
-4
lines changed

7 files changed

+853
-4
lines changed

packages/pg/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ help:
1717

1818
test: test-unit
1919

20-
test-all: test-missing-native test-unit test-integration test-native
20+
test-all: test-missing-native test-unit test-integration test-native test-worker
2121

2222

2323
update-npm:
@@ -59,3 +59,7 @@ test-binary: test-connection
5959

6060
test-pool:
6161
@find test/integration/connection-pool -name "*.js" | $(node-command) binary
62+
63+
test-worker:
64+
@echo "***Testing Cloudflare Worker support***"
65+
@node test/worker/src/index.test.js

packages/pg/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
"pgpass": "1.x"
3030
},
3131
"devDependencies": {
32+
"@cloudflare/workers-types": "^4.20230404.0",
3233
"async": "2.6.4",
3334
"bluebird": "3.5.2",
3435
"co": "4.6.0",
35-
"pg-copy-streams": "0.3.0"
36+
"pg-copy-streams": "0.3.0",
37+
"typescript": "^4.0.3",
38+
"workerd": "^1.20230419.0",
39+
"wrangler": "^2.16.0"
3640
},
3741
"optionalDependencies": {
3842
"pg-cloudflare": "1.x"
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
if (parseInt(process.versions.node.split('.')[0]) < 16) {
2+
process.exit(0)
3+
}
4+
var helper = require('../../test-helper')
5+
const path = require('path')
6+
const { unstable_dev } = require('wrangler')
7+
8+
var suite = new helper.Suite()
9+
10+
suite.testAsync('Can run in Cloudflare Worker?', test())
11+
12+
async function test() {
13+
const worker = await unstable_dev(path.resolve(__dirname, './index.ts'), {
14+
config: path.resolve(__dirname, '../wrangler.toml'),
15+
vars: {
16+
...process.env,
17+
},
18+
experimental: {
19+
experimentalLocal: true,
20+
disableExperimentalWarning: true,
21+
},
22+
logLevel: 'ERROR',
23+
})
24+
try {
25+
const resp = await worker.fetch('/')
26+
const { rows } = await resp.json()
27+
assert.same(rows[0].text, 'Hello, World!')
28+
} finally {
29+
await worker.stop()
30+
}
31+
}

packages/pg/test/worker/src/index.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Client } from 'pg'
2+
3+
export interface Env {
4+
USER: string
5+
PGUSER: string
6+
PGPASSWORD: string
7+
}
8+
9+
export default {
10+
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
11+
const url = new URL(request.url)
12+
if (url.pathname === '/favicon.ico') return new Response(null, { status: 404 })
13+
14+
const params = url.searchParams
15+
const ssl = params.has('ssl')
16+
17+
var client = new Client({
18+
user: env.PGUSER || env.USER,
19+
password: env.PGPASSWORD,
20+
ssl,
21+
})
22+
await client.connect()
23+
const resp = Response.json(await client.query('SELECT $1::text', ['Hello, World!']))
24+
// Clean up the client, ensuring we don't kill the worker before that is completed.
25+
ctx.waitUntil(client.end())
26+
return resp
27+
},
28+
}

packages/pg/test/worker/tsconfig.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2021",
4+
"lib": [
5+
"es2021"
6+
],
7+
"module": "es2022",
8+
"moduleResolution": "node",
9+
"types": [
10+
"@cloudflare/workers-types"
11+
],
12+
"resolveJsonModule": true,
13+
"allowJs": true,
14+
"checkJs": false,
15+
"noEmit": true,
16+
"isolatedModules": true,
17+
"allowSyntheticDefaultImports": true,
18+
"forceConsistentCasingInFileNames": true,
19+
"strict": true,
20+
"skipLibCheck": true,
21+
}
22+
}

packages/pg/test/worker/wrangler.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name = "pg-cf-test"
2+
main = "src/index.ts"
3+
compatibility_date = "2023-04-04"
4+
compatibility_flags = ["tcp_sockets_support"]
5+
node_compat = true

0 commit comments

Comments
 (0)