Skip to content

Commit

Permalink
fix(cloudflare_workers): correctly construct RequestInit object when …
Browse files Browse the repository at this point in the history
…calling `fetch`
  • Loading branch information
Ehesp committed Feb 10, 2023
1 parent 4d93cdb commit c75720b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import 'package:js_bindings/js_bindings.dart' as interop;
import 'package:edge_runtime/src/interop/utils_interop.dart';

extension CloudflareWorkersRequestInteropExtension on interop.RequestInit {
set cf(RequestInitCfProperties properties) {
set cf(CloudflareRequestInit properties) {
js_util.setProperty(this, 'cf', properties);
}
}

@anonymous
@JS()
@staticInterop
class RequestInitCfProperties {
external factory RequestInitCfProperties();
class CloudflareRequestInit {
external factory CloudflareRequestInit();
}

extension PropsRequestInitCfProperties on RequestInitCfProperties {
extension PropsCloudflareRequestInit on CloudflareRequestInit {
set cacheEverything(bool? value) {
js_util.setProperty(this, 'cacheEverything', value ?? jsUndefined);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cloudflare_workers/lib/public/request_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class CloudflareRequestInit {
}

extension CloudflareRequestInitExtension on CloudflareRequestInit {
interop.RequestInitCfProperties get delegate {
return interop.RequestInitCfProperties()
interop.CloudflareRequestInit get delegate {
return interop.CloudflareRequestInit()
..cacheEverything = cacheEverything
..cacheKey = cacheKey
..cacheTags = cacheTags
Expand Down
53 changes: 31 additions & 22 deletions packages/cloudflare_workers/lib/public/top.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:js_util';
import 'dart:js_util' as js_util;

import 'package:edge_runtime/edge_runtime.dart' hide fetch;
import 'package:edge_runtime/src/headers.dart';
Expand All @@ -8,6 +8,7 @@ import 'package:edge_runtime/src/abort.dart';
import 'package:js_bindings/js_bindings.dart' as interop;
import 'package:edge_runtime/src/interop/utils_interop.dart' as interop;
import 'package:edge_runtime/src/interop/scope_interop.dart' as interop;
import '../interop/request_init_interop.dart' as interop;

import 'request_init.dart';

Expand All @@ -28,25 +29,33 @@ Future<Response> fetch(
AbortSignal? signal,
interop.RequestDuplex? duplex,
}) async {
return responseFromJsObject(await promiseToFuture(interop.fetch(
interop.requestFromResource(resource),
jsify({
// Cloudflare specific properties
cf: cf ?? interop.jsUndefined,
// Fetch API properties
method: method,
headers: headers?.delegate,
body: body,
referrer: referrer,
referrerPolicy: referrerPolicy,
mode: mode,
credentials: credentials,
cache: cache,
redirect: redirect,
integrity: integrity,
keepalive: keepalive,
signal: signal?.delegate,
duplex: duplex,
}),
)));
final init = interop.RequestInit(
method: method,
headers: headers?.delegate,
body: body,
referrer: referrer,
referrerPolicy: referrerPolicy,
mode: mode,
credentials: credentials,
cache: cache,
redirect: redirect,
integrity: integrity,
keepalive: keepalive,
signal: signal?.delegate,
duplex: duplex,
);

// Attach the non-standard Cloudflare properties to the Request Init object.
if (cf != null) {
init.cf = cf.delegate;
}

return responseFromJsObject(
await js_util.promiseToFuture(
interop.fetch(
interop.requestFromResource(resource),
init,
),
),
);
}
3 changes: 1 addition & 2 deletions packages/edge_runtime/lib/src/interop/scope_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ external void addEventListener(
@js.JS('fetch')
external Promise<interop.Response> fetch(
interop.Request request, [
// Object, since we need to pass a Map in some cases.
Object? init,
interop.RequestInit? init,
]);

@js.JS('atob')
Expand Down

0 comments on commit c75720b

Please sign in to comment.