Skip to content

Commit

Permalink
implement deno apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehesp committed Feb 21, 2023
1 parent b419084 commit b06ea05
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 106 deletions.
1 change: 1 addition & 0 deletions packages/deno_deploy/lib/deno_deploy.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'src/deno.dart';
export 'src/env.dart' show Env;
78 changes: 39 additions & 39 deletions packages/deno_deploy/lib/src/deno.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import 'dart:typed_data';

import 'package:edge_runtime/src/abort.dart';

import 'interop/deno_interop.dart' as interop;

import 'env.dart';
import 'fs.dart';

class Deno {
static get env => Env._(interop.Env());
/// An interface containing methods to interact with the process
/// environment variables.
static Env get env => envFromJsObject(interop.Deno.env);

static Future<TcpConn> connect({
required int port,
Expand Down Expand Up @@ -30,21 +39,34 @@ class Deno {
// Multiple types
static resolveDns() => {};

static Uri cwd(Uri uri) => Uri.parse(interop.cwd());
/// Return a Uri representing the current working directory.
///
/// If the current directory can be reached via multiple paths
/// (due to symbolic links), cwd() may return any one of them.
static Uri cwd() => Uri.parse(interop.Deno.cwd());

static Stream<DirEntry> readDir(Uri uri) async* {
final asyncIterator = interop.readDir(uri.toString());
while (true) {
final result = await asyncIterator.next();
if (result.done) break;
yield DirEntry._(result.value);
}
/// Reads the directory given by path and returns a [Stream] of [DirEntry].
static Stream<DirEntry> readDir(Uri uri) {
return interop.Deno.readDir(uri.toString()).map(dirEntryFromJsObject);
}

static readFile(Uri uri) => {};
static Future<ByteBuffer> readFile(
Uri uri, {
AbortSignal? signal,
}) =>
interop.Deno.readFile(
uri.toString(),
interop.ReadFileOptions(signal: signal?.delegate),
);

// Options
static readTextFile(Uri uri) => {};
static Future<String> readTextFile(
Uri uri, {
AbortSignal? signal,
}) =>
interop.Deno.readTextFile(
uri.toString(),
interop.ReadFileOptions(signal: signal?.delegate),
);

static open(
Uri uri, {
Expand All @@ -56,35 +78,13 @@ class Deno {
bool createNew = false,
num? mode,
}) =>
{};
throw UnimplementedError();

static stat(Uri uri) => {};
static lstat(Uri uri) => {};
static Future<String> realPath(Uri uri) async => 'todo';
static stat(Uri uri) => throw UnimplementedError();
static lstat(Uri uri) => throw UnimplementedError();
static Future<String> realPath(Uri uri) =>
interop.Deno.realPath(uri.toString());
static Future<String> readLink(Uri uri) async => 'todo';
}

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

String? get(String key) => _delegate.get(key);
void set(String key, String value) => _delegate.set(key, value);
void delete(String key) => _delegate.delete(key);
bool has(String key) => _delegate.has(key);

// Probs needs dartify
Map<String, String> toJson() => _delegate.toObject();
}

class TcpConn {}

class DirEntry {
final interop.DirEntry _delegate;

DirEntry._(this._delegate);
String get name => _delegate.name;
bool get isFile => _delegate.isFile;
bool get isDirectory => _delegate.isDirectory;
bool get isSymlink => _delegate.isSymlink;
}
28 changes: 28 additions & 0 deletions packages/deno_deploy/lib/src/env.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'interop/deno_interop.dart' as interop;

class Env {
final interop.Env _delegate;

Env._(this._delegate);

/// Retrieve the value of an environment variable.
///
/// Returns `null` if the supplied environment variable is not defined.
String? get(String key) => _delegate.get(key);

/// Set the value of an environment variable.
void set(String key, String value) => _delegate.set(key, value);

/// Delete the value of an environment variable.
void delete(String key) => _delegate.delete(key);

/// Check whether an environment variable is present or not.
bool has(String key) => _delegate.has(key);

/// Returns a snapshot of the environment variables at invocation as a simple Map of keys and values.
Map<String, String> toJson() => _delegate.toObject();
}

Env envFromJsObject(interop.Env env) {
return Env._(env);
}
29 changes: 29 additions & 0 deletions packages/deno_deploy/lib/src/fs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'interop/deno_interop.dart' as interop;

/// Information about a directory entry.
///
/// See https://deno.land/api@v1.30.3?s=Deno.DirEntry
class DirEntry {
final interop.DirEntry _delegate;

DirEntry._(this._delegate);

/// The file name of the entry. It is just the entity name and does not
/// include the full path.
String get name => _delegate.name;

/// True if this is info for a regular file. Mutually exclusive to
/// DirEntry.isDirectory and DirEntry.isSymlink.
bool get isFile => _delegate.isFile;

/// True if this is info for a regular directory. Mutually exclusive to
/// DirEntry.isFile and DirEntry.isSymlink.
bool get isDirectory => _delegate.isDirectory;

/// True if this is info for a symlink. Mutually exclusive to DirEntry.isFile
/// and DirEntry.isDirectory.
bool get isSymlink => _delegate.isSymlink;
}

DirEntry dirEntryFromJsObject(interop.DirEntry jsObject) =>
DirEntry._(jsObject);
107 changes: 70 additions & 37 deletions packages/deno_deploy/lib/src/interop/deno_interop.dart
Original file line number Diff line number Diff line change
@@ -1,57 +1,29 @@
import 'dart:typed_data';

import 'package:js/js.dart';
import 'dart:js_util' as js_util;

@anonymous
@JS()
@staticInterop
class AsyncIterator<T> {
external factory AsyncIterator();
}

extension PropsAsyncIterator<T> on AsyncIterator {
Future<IteratorResult<T>> next() => js_util.promiseToFuture(
js_util.callMethod(this, 'next', []),
);
}
import 'package:js_bindings/js_bindings.dart' as interop;
import 'package:edge_runtime/src/interop/utils_interop.dart';
import 'package:edge_runtime/src/interop/promise_interop.dart';
import 'package:edge_runtime/src/interop/async_iterator_interop.dart';

@anonymous
@JS()
@staticInterop
class IteratorResult<T> {
external factory IteratorResult();
}

extension PropsIteratorResult<T> on IteratorResult {
bool get done => js_util.getProperty(this, 'done');
T get value => js_util.getProperty(this, 'value');
}

@JS('Deno.env')
@staticInterop
external Env get env;

@JS('Deno.cwd')
@staticInterop
external String cwd();

@JS('Deno.readDir')
@staticInterop
external AsyncIterator<DirEntry> readDir(String path);

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

extension PropsEnv on Env {
String? get(String key) => js_util.callMethod(this, 'get', [key]);

void set(String key, String value) =>
js_util.callMethod(this, 'set', [key, value]);
void delete(String key) => js_util.callMethod(this, 'delete', [key]);
bool has(String key) => js_util.callMethod(this, 'has', [key]);
dynamic toObject() => js_util.callMethod(this, 'toObject', []);
Map<String, String> toObject() => Map<String, String>.from(
dartify(js_util.callMethod(this, 'toObject', [])));
}

@JS()
Expand All @@ -67,3 +39,64 @@ extension PropsDirEntry on DirEntry {
bool get isDirectory => js_util.getProperty(this, 'isDirectory');
bool get isSymlink => js_util.getProperty(this, 'isSymlink');
}

@JS('Deno.env')
@staticInterop
external Env get _env;

@JS('Deno.cwd')
@staticInterop
external String _cwd();

@JS('Deno.readDir')
@staticInterop
external AsyncIterator<DirEntry> _readDir(String path);

@JS('Deno.readFile')
@staticInterop
external Promise<ByteBuffer> _readFile(String path, ReadFileOptions options);

@JS('Deno.readTextFile')
@staticInterop
external Promise<String> _readTextFile(String path, ReadFileOptions options);

@JS('Deno.realPath')
@staticInterop
external Promise<String> _realPath(String path);

@JS('Deno.readLink')
@staticInterop
external Promise<String> _readLink(String path);

class Deno {
static Env get env => _env;
static String cwd() => _cwd();
static Stream<DirEntry> readDir(String path) {
return _readDir(path).toStream<DirEntry>();
}

static Future<ByteBuffer> readFile(String path, ReadFileOptions options) {
return js_util.promiseToFuture(_readFile(path, options));
}

static Future<String> readTextFile(String path, ReadFileOptions options) {
return js_util.promiseToFuture(_readTextFile(path, options));
}

static Future<String> realPath(String path) {
return js_util.promiseToFuture(_realPath(path));
}

static Future<String> readLink(String path) {
return js_util.promiseToFuture(_readLink(path));
}
}

@JS()
@staticInterop
@anonymous
class ReadFileOptions {
external factory ReadFileOptions({
interop.AbortSignal? signal,
});
}
31 changes: 31 additions & 0 deletions packages/deno_deploy/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
collection:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
url: "https://pub.dev"
source: hosted
version: "1.17.1"
edge_runtime:
dependency: "direct main"
description:
path: "../edge_runtime"
relative: true
source: path
version: "0.0.1+3"
freezed_annotation:
dependency: transitive
description:
name: freezed_annotation
sha256: aeac15850ef1b38ee368d4c53ba9a847e900bb2c53a4db3f6881cbb3cb684338
url: "https://pub.dev"
source: hosted
version: "2.2.0"
js:
dependency: "direct main"
description:
Expand All @@ -17,6 +40,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.1.0"
json_annotation:
dependency: transitive
description:
name: json_annotation
sha256: c33da08e136c3df0190bd5bbe51ae1df4a7d96e7954d1d7249fea2968a72d317
url: "https://pub.dev"
source: hosted
version: "4.8.0"
meta:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions packages/deno_deploy/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ environment:
dependencies:
js: ^0.6.5
js_bindings: ^0.1.0
edge_runtime: ^0.0.1+3
4 changes: 4 additions & 0 deletions packages/deno_deploy/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# melos_managed_dependency_overrides: edge_runtime
dependency_overrides:
edge_runtime:
path: ../edge_runtime
23 changes: 0 additions & 23 deletions packages/edge/lib/src/commands/build.dart

This file was deleted.

2 changes: 2 additions & 0 deletions packages/edge/lib/src/commands/build_command.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'base_command.dart';
import 'build_commands/build_vercel.dart';
import 'build_commands/build_cloudflare.dart';
import 'build_commands/build_supabase.dart';

class BuildCommand extends BaseCommand {
@override
Expand All @@ -15,5 +16,6 @@ class BuildCommand extends BaseCommand {
}) {
addSubcommand(CloudflareBuildCommand(logger: logger));
addSubcommand(VercelBuildCommand(logger: logger));
addSubcommand(SupabaseBuildCommand(logger: logger));
}
}
Loading

0 comments on commit b06ea05

Please sign in to comment.