mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
publish deno_fetch during CI (#7557)
This commit is contained in:
parent
93e8e59a7e
commit
8edf099485
5 changed files with 23 additions and 32 deletions
3
.github/workflows/ci.yml
vendored
3
.github/workflows/ci.yml
vendored
|
@ -231,6 +231,9 @@ jobs:
|
||||||
cd ../op_crates/web
|
cd ../op_crates/web
|
||||||
cargo publish
|
cargo publish
|
||||||
sleep 30
|
sleep 30
|
||||||
|
cd ../op_crates/fetch
|
||||||
|
cargo publish
|
||||||
|
sleep 30
|
||||||
cd ../../cli
|
cd ../../cli
|
||||||
sleep 30
|
sleep 30
|
||||||
cargo publish
|
cargo publish
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
#![deny(warnings)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
name = "deno_fetch"
|
name = "deno_fetch"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "fetch Web API"
|
description = "provides fetch Web API to deno_core"
|
||||||
authors = ["the Deno authors"]
|
authors = ["the Deno authors"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
1
op_crates/fetch/README.md
Normal file
1
op_crates/fetch/README.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
This crate provides the web standard fetch API to `deno_core`.
|
|
@ -1,5 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
#![deny(warnings)]
|
||||||
|
|
||||||
use deno_core::error::bad_resource_id;
|
use deno_core::error::bad_resource_id;
|
||||||
use deno_core::error::type_error;
|
use deno_core::error::type_error;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
|
@ -14,10 +16,9 @@ use deno_core::BufVec;
|
||||||
use deno_core::JsRuntime;
|
use deno_core::JsRuntime;
|
||||||
use deno_core::OpState;
|
use deno_core::OpState;
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
use reqwest::header::HeaderMap;
|
|
||||||
use reqwest::header::HeaderName;
|
use reqwest::header::HeaderName;
|
||||||
use reqwest::header::HeaderValue;
|
use reqwest::header::HeaderValue;
|
||||||
use reqwest::header::USER_AGENT;
|
|
||||||
use reqwest::redirect::Policy;
|
use reqwest::redirect::Policy;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
|
@ -31,6 +32,8 @@ use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
pub use reqwest; // Re-export reqwest
|
||||||
|
|
||||||
pub fn init(isolate: &mut JsRuntime) {
|
pub fn init(isolate: &mut JsRuntime) {
|
||||||
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
||||||
let files = vec![
|
let files = vec![
|
||||||
|
@ -63,15 +66,6 @@ pub fn get_declaration() -> PathBuf {
|
||||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_fetch.d.ts")
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_fetch.d.ts")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
struct FetchArgs {
|
|
||||||
method: Option<String>,
|
|
||||||
url: String,
|
|
||||||
headers: Vec<(String, String)>,
|
|
||||||
client_rid: Option<u32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn op_fetch<FP>(
|
pub async fn op_fetch<FP>(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: Value,
|
args: Value,
|
||||||
|
@ -80,6 +74,15 @@ pub async fn op_fetch<FP>(
|
||||||
where
|
where
|
||||||
FP: FetchPermissions + 'static,
|
FP: FetchPermissions + 'static,
|
||||||
{
|
{
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct FetchArgs {
|
||||||
|
method: Option<String>,
|
||||||
|
url: String,
|
||||||
|
headers: Vec<(String, String)>,
|
||||||
|
client_rid: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
let args: FetchArgs = serde_json::from_value(args)?;
|
let args: FetchArgs = serde_json::from_value(args)?;
|
||||||
let url = args.url;
|
let url = args.url;
|
||||||
|
|
||||||
|
@ -181,23 +184,13 @@ pub async fn op_fetch_read(
|
||||||
let mut chunk_fut = response.chunk().boxed_local();
|
let mut chunk_fut = response.chunk().boxed_local();
|
||||||
let r = ready!(chunk_fut.poll_unpin(cx))?;
|
let r = ready!(chunk_fut.poll_unpin(cx))?;
|
||||||
if let Some(chunk) = r {
|
if let Some(chunk) = r {
|
||||||
|
// TODO(ry) This is terribly inefficient. Make this zero-copy.
|
||||||
Ok(json!({ "chunk": &*chunk })).into()
|
Ok(json!({ "chunk": &*chunk })).into()
|
||||||
} else {
|
} else {
|
||||||
Ok(json!({ "chunk": null })).into()
|
Ok(json!({ "chunk": null })).into()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
f.await
|
f.await
|
||||||
/*
|
|
||||||
// I'm programming this as I want it to be programmed, even though it might be
|
|
||||||
// incorrect, normally we would use poll_fn here. We need to make this await pattern work.
|
|
||||||
let chunk = response.chunk().await?;
|
|
||||||
if let Some(chunk) = chunk {
|
|
||||||
// TODO(ry) This is terribly inefficient. Make this zero-copy.
|
|
||||||
Ok(json!({ "chunk": &*chunk }))
|
|
||||||
} else {
|
|
||||||
Ok(json!({ "chunk": null }))
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HttpClientResource {
|
struct HttpClientResource {
|
||||||
|
@ -245,21 +238,13 @@ where
|
||||||
/// Create new instance of async reqwest::Client. This client supports
|
/// Create new instance of async reqwest::Client. This client supports
|
||||||
/// proxies and doesn't follow redirects.
|
/// proxies and doesn't follow redirects.
|
||||||
fn create_http_client(ca_file: Option<&str>) -> Result<Client, AnyError> {
|
fn create_http_client(ca_file: Option<&str>) -> Result<Client, AnyError> {
|
||||||
let mut headers = HeaderMap::new();
|
let mut builder = Client::builder().redirect(Policy::none()).use_rustls_tls();
|
||||||
// TODO(ry) set the verison correctly.
|
|
||||||
headers.insert(USER_AGENT, format!("Deno/{}", "x.x.x").parse().unwrap());
|
|
||||||
let mut builder = Client::builder()
|
|
||||||
.redirect(Policy::none())
|
|
||||||
.default_headers(headers)
|
|
||||||
.use_rustls_tls();
|
|
||||||
|
|
||||||
if let Some(ca_file) = ca_file {
|
if let Some(ca_file) = ca_file {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
File::open(ca_file)?.read_to_end(&mut buf)?;
|
File::open(ca_file)?.read_to_end(&mut buf)?;
|
||||||
let cert = reqwest::Certificate::from_pem(&buf)?;
|
let cert = reqwest::Certificate::from_pem(&buf)?;
|
||||||
builder = builder.add_root_certificate(cert);
|
builder = builder.add_root_certificate(cert);
|
||||||
}
|
}
|
||||||
|
|
||||||
builder
|
builder
|
||||||
.build()
|
.build()
|
||||||
.map_err(|_| deno_core::error::generic_error("Unable to build http client"))
|
.map_err(|_| deno_core::error::generic_error("Unable to build http client"))
|
||||||
|
|
Loading…
Reference in a new issue