File tree Expand file tree Collapse file tree 7 files changed +853
-4
lines changed Expand file tree Collapse file tree 7 files changed +853
-4
lines changed Original file line number Diff line number Diff line change 1717
1818test : 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
2323update-npm :
@@ -59,3 +59,7 @@ test-binary: test-connection
5959
6060test-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
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments