@@ -30,6 +30,10 @@ api::Engine *ENGINE;
30
30
JS::Result<host_api::CacheLookupOptions> parseLookupOptions (JSContext *cx,
31
31
JS::HandleValue options_val) {
32
32
host_api::CacheLookupOptions options;
33
+ // options parameter is optional
34
+ // options is meant to be an object with an optional headers field,
35
+ // the headers field can be:
36
+ // Headers | string[][] | Record<string, string>;
33
37
if (!options_val.isUndefined ()) {
34
38
if (!options_val.isObject ()) {
35
39
JS_ReportErrorASCII (cx, " options argument must be an object" );
@@ -42,28 +46,29 @@ JS::Result<host_api::CacheLookupOptions> parseLookupOptions(JSContext *cx,
42
46
}
43
47
// headers property is optional
44
48
if (!headers_val.isUndefined ()) {
45
- JS::RootedObject headersInstance (
46
- cx, JS_NewObjectWithGivenProto (cx, &Headers::class_, Headers::proto_obj));
47
- if (!headersInstance) {
49
+ if (!headers_val.isObject ()) {
50
+ JS_ReportErrorASCII (
51
+ cx, " Failed to construct Headers object. If defined, the first argument must be either "
52
+ " a [ ['name', 'value'], ... ] sequence, or a { 'name' : 'value', ... } record." );
48
53
return JS::Result<host_api::CacheLookupOptions>(JS::Error ());
49
54
}
50
- auto headers = Headers::create (cx, headersInstance, Headers::Mode::Standalone, nullptr ,
51
- headers_val, true );
52
- if (!headers) {
55
+ JS::RootedObject request_opts (cx, JS_NewPlainObject (cx));
56
+ if (!JS_SetProperty (cx, request_opts, " headers" , headers_val)) {
53
57
return JS::Result<host_api::CacheLookupOptions>(JS::Error ());
54
58
}
55
- JS::RootedValue headers_val (cx, JS::ObjectValue (*headers));
56
59
JS::RootedObject requestInstance (cx, Request::create_instance (cx));
57
60
if (!requestInstance) {
58
61
return JS::Result<host_api::CacheLookupOptions>(JS::Error ());
59
62
}
60
63
64
+ JS::RootedValue request_opts_val (cx, ObjectValue (*request_opts));
65
+
61
66
// We need to convert the supplied HeadersInit in the `headers` property into a host-backed
62
67
// Request which contains the same headers Request::create does exactly that
63
68
// however, it also expects a fully valid URL for the Request. We don't ever use the Request
64
69
// URL, so we hard-code a valid URL
65
70
JS::RootedValue input (cx, JS::StringValue (JS_NewStringCopyZ (cx, " http://example.com" )));
66
- JS::RootedObject request (cx, Request::create (cx, requestInstance, input, headers_val ));
71
+ JS::RootedObject request (cx, Request::create (cx, requestInstance, input, request_opts_val ));
67
72
options.request_headers = host_api::HttpReq (Request::request_handle (request));
68
73
}
69
74
}
@@ -326,28 +331,28 @@ JS::Result<host_api::CacheWriteOptions> parseInsertOptions(JSContext *cx,
326
331
}
327
332
// headers property is optional
328
333
if (!headers_val.isUndefined ()) {
329
- JS::RootedObject headersInstance (
330
- cx, JS_NewObjectWithGivenProto (cx, &Headers::class_, Headers::proto_obj));
331
- if (!headersInstance) {
334
+ if (!headers_val.isObject ()) {
335
+ JS_ReportErrorASCII (
336
+ cx, " Failed to construct Headers object. If defined, the first argument must be either "
337
+ " a [ ['name', 'value'], ... ] sequence, or a { 'name' : 'value', ... } record." );
332
338
return JS::Result<host_api::CacheWriteOptions>(JS::Error ());
333
339
}
334
- auto headers =
335
- Headers::create (cx, headersInstance, Headers::Mode::Standalone, nullptr , headers_val, true );
336
- if (!headers) {
340
+ JS::RootedObject request_opts (cx, JS_NewPlainObject (cx));
341
+ if (!JS_SetProperty (cx, request_opts, " headers" , headers_val)) {
337
342
return JS::Result<host_api::CacheWriteOptions>(JS::Error ());
338
343
}
339
- JS::RootedValue headers_val (cx, JS::ObjectValue (*headers));
340
344
JS::RootedObject requestInstance (cx, Request::create_instance (cx));
341
345
if (!requestInstance) {
342
346
return JS::Result<host_api::CacheWriteOptions>(JS::Error ());
343
347
}
348
+ JS::RootedValue request_opts_val (cx, ObjectValue (*request_opts));
344
349
345
350
// We need to convert the supplied HeadersInit in the `headers` property into a host-backed
346
351
// Request which contains the same headers Request::create does exactly that however,
347
352
// it also expects a fully valid URL for the Request. We don't ever use the Request URL, so we
348
353
// hard-code a valid URL
349
354
JS::RootedValue input (cx, JS::StringValue (JS_NewStringCopyZ (cx, " http://example.com" )));
350
- JS::RootedObject request (cx, Request::create (cx, requestInstance, input, headers_val ));
355
+ JS::RootedObject request (cx, Request::create (cx, requestInstance, input, request_opts_val ));
351
356
options.request_headers = host_api::HttpReq (Request::request_handle (request));
352
357
}
353
358
return options;
@@ -1055,49 +1060,11 @@ bool CoreCache::transactionLookup(JSContext *cx, unsigned argc, JS::Value *vp) {
1055
1060
return false ;
1056
1061
}
1057
1062
1058
- host_api::CacheLookupOptions options;
1059
- auto options_val = args.get (1 );
1060
- // options parameter is optional
1061
- // options is meant to be an object with an optional headers field,
1062
- // the headers field can be:
1063
- // Headers | string[][] | Record<string, string>;
1064
- if (!options_val.isUndefined ()) {
1065
- if (!options_val.isObject ()) {
1066
- JS_ReportErrorASCII (cx, " options argument must be an object" );
1067
- return false ;
1068
- }
1069
- JS::RootedObject options_obj (cx, &options_val.toObject ());
1070
- JS::RootedValue headers_val (cx);
1071
- if (!JS_GetProperty (cx, options_obj, " headers" , &headers_val)) {
1072
- return false ;
1073
- }
1074
- // headers property is optional
1075
- if (!headers_val.isUndefined ()) {
1076
- JS::RootedObject headersInstance (
1077
- cx, JS_NewObjectWithGivenProto (cx, &Headers::class_, Headers::proto_obj));
1078
- if (!headersInstance) {
1079
- return false ;
1080
- }
1081
- auto headers = Headers::create (cx, headersInstance, Headers::Mode::Standalone, nullptr ,
1082
- headers_val, true );
1083
- if (!headers) {
1084
- return false ;
1085
- }
1086
- JS::RootedValue headers_val (cx, JS::ObjectValue (*headers));
1087
- JS::RootedObject requestInstance (cx, Request::create_instance (cx));
1088
- if (!requestInstance) {
1089
- return false ;
1090
- }
1091
-
1092
- // We need to convert the supplied HeadersInit in the `headers` property into a host-backed
1093
- // Request which contains the same headers Request::create does exactly that
1094
- // however, it also expects a fully valid URL for the Request. We don't ever use the Request
1095
- // URL, so we hard-code a valid URL
1096
- JS::RootedValue input (cx, JS::StringValue (JS_NewStringCopyZ (cx, " http://example.com" )));
1097
- JS::RootedObject request (cx, Request::create (cx, requestInstance, input, headers_val));
1098
- options.request_headers = host_api::HttpReq (Request::request_handle (request));
1099
- }
1063
+ auto options_result = parseLookupOptions (cx, args.get (1 ));
1064
+ if (options_result.isErr ()) {
1065
+ return false ;
1100
1066
}
1067
+ auto options = options_result.unwrap ();
1101
1068
1102
1069
auto res = host_api::CacheHandle::transaction_lookup (key, options);
1103
1070
if (auto *err = res.to_err ()) {
0 commit comments