@@ -80,6 +80,27 @@ pub const RequestCredentials = enum {
8080 }
8181};
8282
83+ pub const RequestMode = enum {
84+ cors ,
85+ @"no-cors" ,
86+ @"same-origin" ,
87+ navigate ,
88+
89+ pub fn fromString (str : []const u8 ) ? RequestMode {
90+ for (std .enums .values (RequestMode )) | cache | {
91+ if (std .ascii .eqlIgnoreCase (str , @tagName (cache ))) {
92+ return cache ;
93+ }
94+ } else {
95+ return null ;
96+ }
97+ }
98+
99+ pub fn toString (self : RequestMode ) []const u8 {
100+ return @tagName (self );
101+ }
102+ };
103+
83104// https://developer.mozilla.org/en-US/docs/Web/API/RequestInit
84105pub const RequestInit = struct {
85106 body : ? []const u8 = null ,
@@ -88,6 +109,7 @@ pub const RequestInit = struct {
88109 headers : ? HeadersInit = null ,
89110 integrity : ? []const u8 = null ,
90111 method : ? []const u8 = null ,
112+ mode : ? []const u8 = null ,
91113};
92114
93115// https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
@@ -97,6 +119,8 @@ method: Http.Method,
97119url : [:0 ]const u8 ,
98120cache : RequestCache ,
99121credentials : RequestCredentials ,
122+ // no-cors is default is not built with constructor.
123+ mode : RequestMode = .@"no-cors" ,
100124headers : Headers ,
101125body : ? []const u8 ,
102126body_used : bool = false ,
@@ -115,11 +139,11 @@ pub fn constructor(input: RequestInput, _options: ?RequestInit, page: *Page) !Re
115139 },
116140 };
117141
118- const body = if (options .body ) | body | try arena .dupe (u8 , body ) else null ;
119142 const cache = (if (options .cache ) | cache | RequestCache .fromString (cache ) else null ) orelse RequestCache .default ;
120143 const credentials = (if (options .credentials ) | creds | RequestCredentials .fromString (creds ) else null ) orelse RequestCredentials .@"same-origin" ;
121144 const integrity = if (options .integrity ) | integ | try arena .dupe (u8 , integ ) else "" ;
122145 const headers : Headers = if (options .headers ) | hdrs | try Headers .constructor (hdrs , page ) else .{};
146+ const mode = (if (options .mode ) | mode | RequestMode .fromString (mode ) else null ) orelse RequestMode .cors ;
123147
124148 const method : Http.Method = blk : {
125149 if (options .method ) | given_method | {
@@ -135,11 +159,19 @@ pub fn constructor(input: RequestInput, _options: ?RequestInit, page: *Page) !Re
135159 }
136160 };
137161
162+ // Can't have a body on .GET or .HEAD.
163+ const body : ? []const u8 = blk : {
164+ if (method == .GET or method == .HEAD ) {
165+ break :blk null ;
166+ } else break :blk if (options .body ) | body | try arena .dupe (u8 , body ) else null ;
167+ };
168+
138169 return .{
139170 .method = method ,
140171 .url = url ,
141172 .cache = cache ,
142173 .credentials = credentials ,
174+ .mode = mode ,
143175 .headers = headers ,
144176 .body = body ,
145177 .integrity = integrity ,
@@ -181,6 +213,10 @@ pub fn get_method(self: *const Request) []const u8 {
181213 return @tagName (self .method );
182214}
183215
216+ pub fn get_mode (self : * const Request ) RequestMode {
217+ return self .mode ;
218+ }
219+
184220pub fn get_url (self : * const Request ) []const u8 {
185221 return self .url ;
186222}
@@ -210,10 +246,7 @@ pub fn _bytes(self: *Response, page: *Page) !Env.Promise {
210246 return error .TypeError ;
211247 }
212248
213- const resolver = Env.PromiseResolver {
214- .js_context = page .main_context ,
215- .resolver = v8 .PromiseResolver .init (page .main_context .v8_context ),
216- };
249+ const resolver = page .main_context .createPromiseResolver ();
217250
218251 try resolver .resolve (self .body );
219252 self .body_used = true ;
@@ -225,22 +258,24 @@ pub fn _json(self: *Response, page: *Page) !Env.Promise {
225258 return error .TypeError ;
226259 }
227260
228- const resolver = Env.PromiseResolver {
229- .js_context = page .main_context ,
230- .resolver = v8 .PromiseResolver .init (page .main_context .v8_context ),
231- };
261+ const resolver = page .main_context .createPromiseResolver ();
232262
233- const p = std .json .parseFromSliceLeaky (
234- std .json .Value ,
235- page .call_arena ,
236- self .body ,
237- .{},
238- ) catch | e | {
239- log .info (.browser , "invalid json" , .{ .err = e , .source = "Request" });
240- return error .SyntaxError ;
241- };
263+ if (self .body ) | body | {
264+ const p = std .json .parseFromSliceLeaky (
265+ std .json .Value ,
266+ page .call_arena ,
267+ body ,
268+ .{},
269+ ) catch | e | {
270+ log .info (.browser , "invalid json" , .{ .err = e , .source = "Request" });
271+ return error .SyntaxError ;
272+ };
273+
274+ try resolver .resolve (p );
275+ } else {
276+ try resolver .resolve (null );
277+ }
242278
243- try resolver .resolve (p );
244279 self .body_used = true ;
245280 return resolver .promise ();
246281}
@@ -250,10 +285,7 @@ pub fn _text(self: *Response, page: *Page) !Env.Promise {
250285 return error .TypeError ;
251286 }
252287
253- const resolver = Env.PromiseResolver {
254- .js_context = page .main_context ,
255- .resolver = v8 .PromiseResolver .init (page .main_context .v8_context ),
256- };
288+ const resolver = page .main_context .createPromiseResolver ();
257289
258290 try resolver .resolve (self .body );
259291 self .body_used = true ;
0 commit comments