1
1
import { primordials , core } from "ext:core/mod.js" ;
2
- import { readableStreamForRid , writableStreamForRid } from ' ext:deno_web/06_streams.js' ;
3
- import { getSupabaseTag } from ' ext:sb_core_main_js/js/http.js' ;
2
+ import { readableStreamForRid , writableStreamForRid } from " ext:deno_web/06_streams.js" ;
3
+ import { getSupabaseTag } from " ext:sb_core_main_js/js/http.js" ;
4
4
5
5
const ops = core . ops ;
6
- const {
7
- InterruptedPrototype,
8
- } = core ;
9
- const {
10
- TypeError,
11
- ObjectPrototypeIsPrototypeOf,
12
- StringPrototypeIncludes,
13
- } = primordials ;
6
+
7
+ const { TypeError } = primordials ;
8
+
14
9
const {
15
10
op_user_worker_fetch_send,
16
11
op_user_worker_create,
@@ -33,11 +28,11 @@ class UserWorker {
33
28
this . key = key ;
34
29
}
35
30
36
- async fetch ( req , opts = { } ) {
37
- const tag = getSupabaseTag ( req ) ;
31
+ async fetch ( request , options = { } ) {
32
+ const tag = getSupabaseTag ( request ) ;
38
33
39
- const { method, url, headers, body, bodyUsed } = req ;
40
- const { signal } = opts ;
34
+ const { method, url, headers, body, bodyUsed } = request ;
35
+ const { signal } = options ;
41
36
42
37
signal ?. throwIfAborted ( ) ;
43
38
@@ -60,62 +55,56 @@ class UserWorker {
60
55
) ;
61
56
62
57
// stream the request body
63
- let reqBodyPromise = null ;
58
+ let requestBodyPromise = null ;
59
+
64
60
if ( hasBody ) {
65
61
let writableStream = writableStreamForRid ( requestBodyRid ) ;
66
- reqBodyPromise = body . pipeTo ( writableStream , { signal } ) ;
62
+ requestBodyPromise = body . pipeTo ( writableStream , { signal } ) ;
67
63
}
68
64
69
- const resPromise = op_user_worker_fetch_send (
65
+ const responsePromise = op_user_worker_fetch_send (
70
66
this . key ,
71
67
requestRid ,
72
68
requestBodyRid ,
73
69
tag . streamRid ,
74
70
tag . watcherRid
75
71
) ;
76
72
77
- let [ sent , res ] = await Promise . allSettled ( [ reqBodyPromise , resPromise ] ) ;
78
-
79
- if ( sent . status === "rejected" ) {
80
- if ( res . status === "fulfilled" ) {
81
- res = res . value ;
82
- } else {
83
- if (
84
- ObjectPrototypeIsPrototypeOf ( InterruptedPrototype , sent . reason ) ||
85
- StringPrototypeIncludes ( sent . reason . message , "operation canceled" )
86
- ) {
87
- throw res . reason ;
88
- } else {
89
- throw sent . reason ;
90
- }
91
- }
92
- } else if ( res . status === "rejected" ) {
93
- throw res . reason ;
94
- } else {
95
- res = res . value ;
73
+ const [ requestBodyPromiseResult , responsePromiseResult ] = await Promise . allSettled ( [
74
+ requestBodyPromise ,
75
+ responsePromise
76
+ ] ) ;
77
+
78
+ if ( requestBodyPromiseResult . status === "rejected" ) {
79
+ // console.warn(requestBodyPromiseResult.reason);
96
80
}
97
81
82
+ if ( responsePromiseResult . status === "rejected" ) {
83
+ throw responsePromiseResult . reason ;
84
+ }
85
+
86
+ const result = responsePromiseResult . value ;
98
87
const response = {
99
- headers : res . headers ,
100
- status : res . status ,
101
- statusText : res . statusText ,
88
+ headers : result . headers ,
89
+ status : result . status ,
90
+ statusText : result . statusText ,
102
91
body : null ,
103
92
} ;
104
93
105
94
// TODO: add a test
106
- if ( nullBodyStatus ( res . status ) || redirectStatus ( res . status ) ) {
107
- core . close ( res . bodyRid ) ;
95
+ if ( nullBodyStatus ( result . status ) || redirectStatus ( result . status ) ) {
96
+ core . tryClose ( result . bodyRid ) ;
108
97
} else {
109
- if ( req . method === 'HEAD' || req . method === 'CONNECT' ) {
110
- response . body = null ;
111
- core . close ( res . bodyRid ) ;
98
+ if ( request . method === "HEAD" || request . method === "CONNECT" ) {
99
+ core . tryClose ( result . bodyRid ) ;
112
100
} else {
113
- const bodyStream = readableStreamForRid ( res . bodyRid ) ;
101
+ const stream = readableStreamForRid ( result . bodyRid ) ;
114
102
115
- signal ?. addEventListener ( ' abort' , ( ) => {
116
- core . tryClose ( res . bodyRid ) ;
103
+ signal ?. addEventListener ( " abort" , ( ) => {
104
+ core . tryClose ( result . bodyRid ) ;
117
105
} ) ;
118
- response . body = bodyStream ;
106
+
107
+ response . body = stream ;
119
108
}
120
109
}
121
110
@@ -148,8 +137,8 @@ class UserWorker {
148
137
149
138
const { servicePath, maybeEszip } = readyOptions ;
150
139
151
- if ( ! maybeEszip && ( ! servicePath || servicePath === '' ) ) {
152
- throw new TypeError ( ' service path must be defined' ) ;
140
+ if ( ! maybeEszip && ( ! servicePath || servicePath === "" ) ) {
141
+ throw new TypeError ( " service path must be defined" ) ;
153
142
}
154
143
155
144
const key = await op_user_worker_create ( readyOptions ) ;
@@ -159,4 +148,5 @@ class UserWorker {
159
148
}
160
149
161
150
const SUPABASE_USER_WORKERS = UserWorker ;
151
+
162
152
export { SUPABASE_USER_WORKERS } ;
0 commit comments