Skip to content

Commit

Permalink
feat: cf package
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehesp committed Jan 24, 2023
1 parent c6ae8f0 commit 90bf775
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 61 deletions.
3 changes: 1 addition & 2 deletions example/lib/element_handler.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import 'package:v8_runtime/v8_runtime.dart';
import 'package:cloudflare_workers/cloudflare_workers.dart';

import 'main.dart';

Expand Down
4 changes: 3 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math';
import 'dart:typed_data';

import 'package:v8_runtime/v8_runtime.dart';
import 'package:cloudflare_workers/cloudflare_workers.dart';
import 'package:workers_dart_example/element_handler.dart';

const shouldUseCache = true;
Expand Down Expand Up @@ -53,6 +54,7 @@ class Foo extends ElementHandler {
void main() {
addFetchEventListener((FetchEvent event) {
event.respondWith(Future(() async {
print(event.request.cf.city);
if (event.request.url.toString().contains('favicon.ico')) {
return Response('', ResponseInit(status: 404));
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ packages:
source: hosted
version: "1.3.1"
v8_runtime:
dependency: "direct main"
dependency: "direct overridden"
description:
path: "../packages/v8_runtime"
relative: true
Expand Down
1 change: 0 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ environment:
sdk: ">=2.18.5 <3.0.0"

dependencies:
v8_runtime: 0.0.1
cloudflare_workers: 0.0.1

dev_dependencies:
Expand Down
18 changes: 15 additions & 3 deletions packages/cloudflare_workers/lib/cloudflare_workers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import 'cloudflare_interop.dart';
export 'package:v8_runtime/v8_runtime.dart';


// TODO
export './public/cache_storage.dart';
export './public/html_rewriter.dart'
show
HTMLRewriter,
ContentOptions,
ElementHandler,
DocumentHandler,
Doctype,
Comment,
Text,
DocumentEnd,
Element,
EndTag;
export './public/request.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ library cache;
import 'package:js/js.dart' as js;
import 'package:js_bindings/js_bindings.dart' as interop;

// TODO this is Cloudflare specific - move to extension or something?
@js.JS('default')
external interop.Cache get defaultCache;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ library html_rewriter;
import 'dart:js_util' as js_util;
import 'package:js/js.dart';
import 'package:js_bindings/js_bindings.dart' as interop;
import '../interop/promise_interop.dart';
import 'package:v8_runtime/interop/promise_interop.dart';

typedef HandlerMethod<T> = Promise<void> Function(T type);

Expand Down
83 changes: 83 additions & 0 deletions packages/cloudflare_workers/lib/interop/request_interop.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:js_util' as js_util;
import 'package:js/js.dart';
import 'package:js_bindings/js_bindings.dart' as interop;

extension CloudflareWorkersRequestInteropExtension on interop.Request {
IncomingRequestCfProperties get cf => js_util.getProperty(this, 'cf');
}

@JS()
@anonymous
class IncomingRequestCfProperties {
/// (e.g. 395747)
external int get asn;

/// The organization which owns the ASN of the incoming request.
/// (e.g. Google Cloud)
external String get asOrganization;
external IncomingRequestCfPropertiesBotManagement? get botManagement;
external String? get city;
external String? get clientAcceptEncoding;
external num get clientTcpRtt;
external num get clientTrustScore;

/// The three-letter airport code of the data center that the request
/// hit. (e.g. "DFW")
external String get colo;
external String? get continent;

/// The two-letter country code in the request. This is the same value
/// as that provided in the CF-IPCountry header. (e.g. "US")
external String get country;
external String get httpProtocol;
external String? get latitude;
external String? get longitude;

/// DMA metro code from which the request was issued, e.g. "635"
external String? get metroCode;
external String? get postalCode;

/// e.g. "Texas"
external String? get region;

/// e.g. "TX"
external String? get regionCode;

/// e.g. "weight=256;exclusive=1"
external String get requestPriority;

/// e.g. "America/Chicago"
external String? get timezone;
external String get tlsVersion;
external String get tlsCipher;
external IncomingRequestCfPropertiesTLSClientAuth get tlsClientAuth;
}

@JS()
@anonymous
class IncomingRequestCfPropertiesBotManagement {
external int get score;
external bool get staticResource;
external bool get verifiedBot;
}

@JS()
@anonymous
class IncomingRequestCfPropertiesTLSClientAuth {
external String get certIssuerDNLegacy;
external String get certIssuerDN;
external String get certPresented;
external String get certSubjectDNLegacy;
external String get certSubjectDN;

/// In format "Dec 22 19:39:00 2018 GMT"
external String get certNotBefore;

/// In format "Dec 22 19:39:00 2018 GMT"
external String get certNotAfter;
external String get certSerial;
external String get certFingerprintSHA1;

/// "SUCCESS", "FAILED:reason", "NONE"
external String get certVerified;
}
8 changes: 8 additions & 0 deletions packages/cloudflare_workers/lib/public/cache_storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:v8_runtime/v8_runtime.dart';
import 'package:v8_runtime/public/cache.dart';

import '../interop/cache_interop.dart' as interop;

extension CloudflareWorkersCacheStorageExtension on CacheStorage {
Cache get defaultCache => cacheFromJsObject(interop.defaultCache);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// TODO move this to a separate package

import 'dart:async';
import 'dart:js';

import './interop/promise_interop.dart';

import 'interop/cf_html_rewriter_interop.dart' as interop;
import 'public/response.dart';
import 'package:v8_runtime/v8_runtime.dart';
import 'package:v8_runtime/public/response.dart';
import 'package:v8_runtime/interop/promise_interop.dart';
import '../interop/html_rewriter_interop.dart' as interop;

class HTMLRewriter {
final interop.HTMLRewriter _delegate;
Expand Down
93 changes: 93 additions & 0 deletions packages/cloudflare_workers/lib/public/request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'package:v8_runtime/public/request.dart';

import '../interop/request_interop.dart' as interop;

extension CloudflareWorkersRequestExtension on Request {
IncomingRequestCfProperties get cf =>
IncomingRequestCfProperties._(delegate.cf);
}

class IncomingRequestCfProperties {
final interop.IncomingRequestCfProperties _delegate;

IncomingRequestCfProperties._(this._delegate);

/// (e.g. 395747)
int get asn => _delegate.asn;

/// The organization which owns the ASN of the incoming request.
/// (e.g. Google Cloud)
String get asOrganization => _delegate.asOrganization;
IncomingRequestCfPropertiesBotManagement? get botManagement {
final obj = _delegate.botManagement;
if (obj == null) return null;
return IncomingRequestCfPropertiesBotManagement._(obj);
}

String? get city => _delegate.city;
String? get clientAcceptEncoding => _delegate.clientAcceptEncoding;
num get clientTcpRtt => _delegate.clientTcpRtt;
num get clientTrustScore => _delegate.clientTrustScore;

/// The three-letter airport code of the data center that the request
/// hit. (e.g. "DFW")
String get colo => _delegate.colo;
String? get continent => _delegate.continent;

/// The two-letter country code in the request. This is the same value
/// as that provided in the CF-IPCountry header. (e.g. "US")
String get country => _delegate.country;
String get httpProtocol => _delegate.httpProtocol;
String? get latitude => _delegate.latitude;
String? get longitude => _delegate.longitude;

/// DMA metro code from which the request was issued, e.g. "635"
String? get metroCode => _delegate.metroCode;
String? get postalCode => _delegate.postalCode;

/// e.g. "Texas"
String? get region => _delegate.region;

/// e.g. "TX"
String? get regionCode => _delegate.regionCode;

/// e.g. "weight=256;exclusive=1"
String get requestPriority => _delegate.requestPriority;

/// e.g. "America/Chicago"
String? get timezone => _delegate.timezone;
String get tlsVersion => _delegate.tlsVersion;
String get tlsCipher => _delegate.tlsCipher;
IncomingRequestCfPropertiesTLSClientAuth get tlsClientAuth =>
IncomingRequestCfPropertiesTLSClientAuth._(_delegate.tlsClientAuth);
}

class IncomingRequestCfPropertiesBotManagement {
final interop.IncomingRequestCfPropertiesBotManagement _delegate;
IncomingRequestCfPropertiesBotManagement._(this._delegate);
int get score => _delegate.score;
bool get staticResource => _delegate.staticResource;
bool get verifiedBot => _delegate.verifiedBot;
}

class IncomingRequestCfPropertiesTLSClientAuth {
final interop.IncomingRequestCfPropertiesTLSClientAuth _delegate;
IncomingRequestCfPropertiesTLSClientAuth._(this._delegate);

String get certIssuerDNLegacy => _delegate.certIssuerDNLegacy;
String get certIssuerDN => _delegate.certIssuerDN;
String get certPresented => _delegate.certPresented;
String get certSubjectDNLegacy => _delegate.certSubjectDNLegacy;
String get certSubjectDN => _delegate.certSubjectDN;

/// In format "Dec 22 19:39:00 2018 GMT"
String get certNotBefore => _delegate.certNotBefore;

/// In format "Dec 22 19:39:00 2018 GMT"
String get certNotAfter => _delegate.certNotAfter;
String get certSerial => _delegate.certSerial;
String get certFingerprintSHA1 => _delegate.certFingerprintSHA1;

/// "SUCCESS", "FAILED:reason", "NONE"
String get certVerified => _delegate.certVerified;
}
2 changes: 1 addition & 1 deletion packages/cloudflare_workers/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ packages:
source: hosted
version: "0.6.5"
js_bindings:
dependency: transitive
dependency: "direct main"
description:
name: js_bindings
url: "https://pub.dartlang.org"
Expand Down
1 change: 1 addition & 0 deletions packages/cloudflare_workers/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ environment:

dependencies:
v8_runtime: 0.0.1
js_bindings: ^0.0.8
js: ^0.6.5

dev_dependencies:
Expand Down
2 changes: 0 additions & 2 deletions packages/v8_runtime/lib/interop/cache_interop.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:js/js.dart' as js;
import 'package:js_bindings/js_bindings.dart' as interop;

export 'cf_cache_interop.dart' show defaultCache;

@js.JS('caches')
external interop.CacheStorage get caches;
23 changes: 0 additions & 23 deletions packages/v8_runtime/lib/interop/console_interop.dart

This file was deleted.

2 changes: 0 additions & 2 deletions packages/v8_runtime/lib/interop/crypto_interop.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:js/js.dart' as js;
import 'package:js_bindings/js_bindings.dart' as interop;

export 'cf_cache_interop.dart' show defaultCache;

@js.JS('crypto')
external interop.Crypto get crypto;
1 change: 0 additions & 1 deletion packages/v8_runtime/lib/interop/scope_interop.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:js_util' as js_util;
import 'package:js/js.dart' as js;
import 'package:js_bindings/js_bindings.dart' as interop;

Expand Down
2 changes: 0 additions & 2 deletions packages/v8_runtime/lib/public/cache_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class CacheStorage {

CacheStorage._(this._delegate);

Cache get defaultCache => cacheFromJsObject(interop.defaultCache);

Future<bool> delete(String cacheName) async {
return promiseToFuture(_delegate.delete(cacheName));
}
Expand Down
14 changes: 0 additions & 14 deletions packages/v8_runtime/lib/v8_runtime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ library fetch_apis;

export 'dart:async' show FutureOr;

// TODO: this should be in a sep package
export 'cf_html_rewriter.dart'
show
HTMLRewriter,
ContentOptions,
ElementHandler,
DocumentHandler,
Doctype,
Comment,
Text,
DocumentEnd,
Element,
EndTag;

export 'public/top.dart';

export 'public/abort_signal.dart' show AbortSignal;
Expand Down

0 comments on commit 90bf775

Please sign in to comment.