Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/denoland/deno into fix_fast…
Browse files Browse the repository at this point in the history
…_check_exports
  • Loading branch information
dsherret committed Mar 12, 2024
2 parents f3afa2e + 61d86b9 commit e8d6e93
Show file tree
Hide file tree
Showing 25 changed files with 693 additions and 78 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions cli/tools/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ use std::sync::Arc;
use std::task::Poll;
use std::time::Duration;
use std::time::Instant;
use std::time::SystemTime;
use tokio::signal;

mod channel;
Expand Down Expand Up @@ -769,7 +768,7 @@ async fn run_tests_for_worker_inner(
// We always capture stats, regardless of sanitization state
let before = stats.clone().capture(&filter);

let earlier = SystemTime::now();
let earlier = Instant::now();
let call = worker.js_runtime.call(&function);
let result = match worker
.js_runtime
Expand Down Expand Up @@ -807,7 +806,7 @@ async fn run_tests_for_worker_inner(
let (formatted, trailer_notes) = format_sanitizer_diff(diff);
if !formatted.is_empty() {
let failure = TestFailure::Leaked(formatted, trailer_notes);
let elapsed = SystemTime::now().duration_since(earlier)?.as_millis();
let elapsed = earlier.elapsed().as_millis();
sender.send(TestEvent::Result(
desc.id,
TestResult::Failed(failure),
Expand All @@ -823,7 +822,7 @@ async fn run_tests_for_worker_inner(
if matches!(result, TestResult::Failed(_)) {
fail_fast_tracker.add_failure();
}
let elapsed = SystemTime::now().duration_since(earlier)?.as_millis();
let elapsed = earlier.elapsed().as_millis();
sender.send(TestEvent::Result(desc.id, result, elapsed as u64))?;
}
Ok(())
Expand Down
1 change: 1 addition & 0 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ fn create_web_worker_callback(
stdio: stdio.clone(),
cache_storage_dir,
feature_checker,
maybe_worker_metadata: args.maybe_worker_metadata,
};

WebWorker::bootstrap_from_options(
Expand Down
2 changes: 2 additions & 0 deletions ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ deno_core::extension!(deno_node,
ops::v8::op_vm_run_in_new_context,
ops::idna::op_node_idna_domain_to_ascii,
ops::idna::op_node_idna_domain_to_unicode,
ops::idna::op_node_idna_punycode_to_ascii,
ops::idna::op_node_idna_punycode_to_unicode,
ops::idna::op_node_idna_punycode_decode,
ops::idna::op_node_idna_punycode_encode,
ops::zlib::op_zlib_new,
Expand Down
141 changes: 136 additions & 5 deletions ext/node/ops/idna.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,126 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use deno_core::error::AnyError;
use deno_core::anyhow::Error;
use deno_core::error::range_error;
use deno_core::op2;

use std::borrow::Cow;

// map_domain, to_ascii and to_unicode are based on the punycode implementation in node.js
// https://github.com/nodejs/node/blob/73025c4dec042e344eeea7912ed39f7b7c4a3991/lib/punycode.js

const PUNY_PREFIX: &str = "xn--";

fn invalid_input_err() -> Error {
range_error("Invalid input")
}

fn not_basic_err() -> Error {
range_error("Illegal input >= 0x80 (not a basic code point)")
}

/// map a domain by mapping each label with the given function
fn map_domain<E>(
domain: &str,
f: impl Fn(&str) -> Result<Cow<'_, str>, E>,
) -> Result<String, E> {
let mut result = String::with_capacity(domain.len());
let mut domain = domain;

// if it's an email, leave the local part as is
let mut parts = domain.split('@');
if let (Some(local), Some(remaining)) = (parts.next(), parts.next()) {
result.push_str(local);
result.push('@');
domain = remaining;
}

// split into labels and map each one
for (i, label) in domain.split('.').enumerate() {
if i > 0 {
result.push('.');
}
result.push_str(&f(label)?);
}
Ok(result)
}

/// Maps a unicode domain to ascii by punycode encoding each label
///
/// Note this is not IDNA2003 or IDNA2008 compliant, rather it matches node.js's punycode implementation
fn to_ascii(input: &str) -> Result<String, Error> {
if input.is_ascii() {
return Ok(input.into());
}

let mut result = String::with_capacity(input.len()); // at least as long as input

let rest = map_domain(input, |label| {
if label.is_ascii() {
Ok(label.into())
} else {
idna::punycode::encode_str(label)
.map(|encoded| [PUNY_PREFIX, &encoded].join("").into()) // add the prefix
.ok_or_else(|| {
Error::msg("Input would take more than 63 characters to encode") // only error possible per the docs
})
}
})?;

result.push_str(&rest);
Ok(result)
}

/// Maps an ascii domain to unicode by punycode decoding each label
///
/// Note this is not IDNA2003 or IDNA2008 compliant, rather it matches node.js's punycode implementation
fn to_unicode(input: &str) -> Result<String, Error> {
map_domain(input, |s| {
if let Some(puny) = s.strip_prefix(PUNY_PREFIX) {
// it's a punycode encoded label
Ok(
idna::punycode::decode_to_string(&puny.to_lowercase())
.ok_or_else(invalid_input_err)?
.into(),
)
} else {
Ok(s.into())
}
})
}

/// Converts a domain to unicode with behavior that is
/// compatible with the `punycode` module in node.js
#[op2]
#[string]
pub fn op_node_idna_punycode_to_ascii(
#[string] domain: String,
) -> Result<String, Error> {
to_ascii(&domain)
}

/// Converts a domain to ASCII with behavior that is
/// compatible with the `punycode` module in node.js
#[op2]
#[string]
pub fn op_node_idna_punycode_to_unicode(
#[string] domain: String,
) -> Result<String, Error> {
to_unicode(&domain)
}

/// Converts a domain to ASCII as per the IDNA spec
/// (specifically UTS #46)
#[op2]
#[string]
pub fn op_node_idna_domain_to_ascii(
#[string] domain: String,
) -> Result<String, AnyError> {
Ok(idna::domain_to_ascii(&domain)?)
) -> Result<String, Error> {
idna::domain_to_ascii(&domain).map_err(|e| e.into())
}

/// Converts a domain to Unicode as per the IDNA spec
/// (specifically UTS #46)
#[op2]
#[string]
pub fn op_node_idna_domain_to_unicode(#[string] domain: String) -> String {
Expand All @@ -19,8 +129,29 @@ pub fn op_node_idna_domain_to_unicode(#[string] domain: String) -> String {

#[op2]
#[string]
pub fn op_node_idna_punycode_decode(#[string] domain: String) -> String {
idna::punycode::decode_to_string(&domain).unwrap_or_default()
pub fn op_node_idna_punycode_decode(
#[string] domain: String,
) -> Result<String, Error> {
if domain.is_empty() {
return Ok(domain);
}

// all code points before the last delimiter must be basic
// see https://github.com/nodejs/node/blob/73025c4dec042e344eeea7912ed39f7b7c4a3991/lib/punycode.js#L215-L227
let last_dash = domain.len()
- 1
- domain
.bytes()
.rev()
.position(|b| b == b'-')
.unwrap_or(domain.len() - 1);

if !domain[..last_dash].is_ascii() {
return Err(not_basic_err());
}

idna::punycode::decode_to_string(&domain)
.ok_or_else(|| deno_core::error::range_error("Invalid input"))
}

#[op2]
Expand Down
3 changes: 2 additions & 1 deletion ext/node/polyfills/02_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function initialize(
usesLocalNodeModulesDir,
argv0,
runningOnMainThread,
maybeWorkerMetadata,
) {
if (initialized) {
throw Error("Node runtime already initialized");
Expand All @@ -38,7 +39,7 @@ function initialize(
// FIXME(bartlomieju): not nice to depend on `Deno` namespace here
// but it's the only way to get `args` and `version` and this point.
internals.__bootstrapNodeProcess(argv0, Deno.args, Deno.version);
internals.__initWorkerThreads(runningOnMainThread);
internals.__initWorkerThreads(runningOnMainThread, maybeWorkerMetadata);
internals.__setupChildProcessIpcChannel();
// `Deno[Deno.internal].requireImpl` will be unreachable after this line.
delete internals.requireImpl;
Expand Down
12 changes: 9 additions & 3 deletions ext/node/polyfills/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import {
GetAddrInfoReqWrap,
QueryReqWrap,
} from "ext:deno_node/internal_binding/cares_wrap.ts";
import { toASCII } from "node:punycode";
import { domainToASCII } from "ext:deno_node/internal/idna.ts";
import { notImplemented } from "ext:deno_node/_utils.ts";

function onlookup(
Expand Down Expand Up @@ -264,7 +264,13 @@ export function lookup(
req.hostname = hostname;
req.oncomplete = all ? onlookupall : onlookup;

const err = getaddrinfo(req, toASCII(hostname), family, hints, verbatim);
const err = getaddrinfo(
req,
domainToASCII(hostname),
family,
hints,
verbatim,
);

if (err) {
nextTick(
Expand Down Expand Up @@ -332,7 +338,7 @@ function resolver(bindingName: keyof ChannelWrapQuery) {

req.ttl = !!(options && (options as ResolveOptions).ttl);

const err = this._handle[bindingName](req, toASCII(name));
const err = this._handle[bindingName](req, domainToASCII(name));

if (err) {
throw dnsException(err, bindingName, name);
Expand Down
19 changes: 19 additions & 0 deletions ext/node/polyfills/internal/idna.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@

"use strict";

import {
op_node_idna_domain_to_ascii,
op_node_idna_domain_to_unicode,
} from "ext:core/ops";

/**
* Creates an array containing the numeric code points of each Unicode
* character in the string. While JavaScript uses UCS-2 internally,
Expand Down Expand Up @@ -105,3 +110,17 @@ export const ucs2 = {
decode: ucs2decode,
encode: ucs2encode,
};

/**
* Converts a domain to ASCII as per the IDNA spec
*/
export function domainToASCII(domain: string) {
return op_node_idna_domain_to_ascii(domain);
}

/**
* Converts a domain to Unicode as per the IDNA spec
*/
export function domainToUnicode(domain: string) {
return op_node_idna_domain_to_unicode(domain);
}
24 changes: 18 additions & 6 deletions ext/node/polyfills/punycode.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import {
op_node_idna_domain_to_ascii,
op_node_idna_domain_to_unicode,
op_node_idna_punycode_decode,
op_node_idna_punycode_encode,
op_node_idna_punycode_to_ascii,
op_node_idna_punycode_to_unicode,
} from "ext:core/ops";

import { deprecate } from "node:util";

import { ucs2 } from "ext:deno_node/internal/idna.ts";

// deno-lint-ignore no-explicit-any
function punyDeprecated(fn: any) {
return deprecate(
fn,
"The `punycode` module is deprecated. Please use a userland " +
"alternative instead.",
"DEP0040",
);
}

function toASCII(domain) {
return op_node_idna_domain_to_ascii(domain);
return punyDeprecated(op_node_idna_punycode_to_ascii)(domain);
}

function toUnicode(domain) {
return op_node_idna_domain_to_unicode(domain);
return punyDeprecated(op_node_idna_punycode_to_unicode)(domain);
}

function decode(domain) {
return op_node_idna_punycode_decode(domain);
return punyDeprecated(op_node_idna_punycode_decode)(domain);
}

function encode(domain) {
return op_node_idna_punycode_encode(domain);
return punyDeprecated(op_node_idna_punycode_encode)(domain);
}

export { decode, encode, toASCII, toUnicode, ucs2 };
Expand Down
Loading

0 comments on commit e8d6e93

Please sign in to comment.