mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
Add example Cloudflare Worker and test
This commit is contained in:
parent
07553428e9
commit
7152d4db5d
@ -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:
|
||||
@ -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
|
||||
|
||||
@ -29,10 +29,14 @@
|
||||
"pgpass": "1.x"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20230404.0",
|
||||
"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"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"pg-cloudflare": "1.x"
|
||||
|
||||
31
packages/pg/test/worker/src/index.test.js
Normal file
31
packages/pg/test/worker/src/index.test.js
Normal file
@ -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
packages/pg/test/worker/src/index.ts
Normal file
28
packages/pg/test/worker/src/index.ts
Normal file
@ -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
packages/pg/test/worker/tsconfig.json
Normal file
22
packages/pg/test/worker/tsconfig.json
Normal file
@ -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
packages/pg/test/worker/wrangler.toml
Normal file
5
packages/pg/test/worker/wrangler.toml
Normal file
@ -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…
x
Reference in New Issue
Block a user