-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example Cloudflare Worker and test
- Loading branch information
1 parent
0755342
commit 7152d4d
Showing
7 changed files
with
853 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.