Commit 7152d4d 1 parent 0755342 commit 7152d4d Copy full SHA for 7152d4d
File tree 7 files changed +853
-4
lines changed
7 files changed +853
-4
lines changed Original file line number Diff line number Diff line change 17
17
18
18
test : test-unit
19
19
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
21
21
22
22
23
23
update-npm :
@@ -59,3 +59,7 @@ test-binary: test-connection
59
59
60
60
test-pool :
61
61
@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 29
29
"pgpass" : " 1.x"
30
30
},
31
31
"devDependencies" : {
32
+ "@cloudflare/workers-types" : " ^4.20230404.0" ,
32
33
"async" : " 2.6.4" ,
33
34
"bluebird" : " 3.5.2" ,
34
35
"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"
36
40
},
37
41
"optionalDependencies" : {
38
42
"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