1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-05 13:59:01 -05:00

Merge branch 'main' into support_create_connection

This commit is contained in:
Yoshiya Hinosawa 2024-10-29 20:25:28 +09:00 committed by GitHub
commit eaa52272ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 295 additions and 75 deletions

4
Cargo.lock generated
View file

@ -1356,9 +1356,9 @@ dependencies = [
[[package]]
name = "deno_cache_dir"
version = "0.13.0"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "186a102b13b4512841f5f40784cd25822042d22954afe3b5b070d406d15eb4f2"
checksum = "693ca429aebf945de5fef30df232044f9f80be4cc5a5e7c8d767226c43880f5a"
dependencies = [
"base32",
"deno_media_type",

View file

@ -111,7 +111,7 @@ console_static_text = "=0.8.1"
dashmap = "5.5.3"
data-encoding = "2.3.3"
data-url = "=0.3.0"
deno_cache_dir = "=0.13.0"
deno_cache_dir = "=0.13.1"
deno_package_json = { version = "0.1.2", default-features = false }
dlopen2 = "0.6.1"
ecb = "=0.1.2"

View file

@ -3,6 +3,7 @@
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_npm::npm_rc::RegistryConfig;
use http::header;
@ -36,17 +37,21 @@ pub fn maybe_auth_header_for_npm_registry(
}
if username.is_some() && password.is_some() {
// The npm client does some double encoding when generating the
// bearer token value, see
// https://github.com/npm/cli/blob/780afc50e3a345feb1871a28e33fa48235bc3bd5/workspaces/config/lib/index.js#L846-L851
let pw_base64 = BASE64_STANDARD
.decode(password.unwrap())
.with_context(|| "The password in npmrc is an invalid base64 string")?;
let bearer = BASE64_STANDARD.encode(format!(
"{}:{}",
username.unwrap(),
String::from_utf8_lossy(&pw_base64)
));
return Ok(Some((
header::AUTHORIZATION,
header::HeaderValue::from_str(&format!(
"Basic {}",
BASE64_STANDARD.encode(format!(
"{}:{}",
username.unwrap(),
password.unwrap()
))
))
.unwrap(),
header::HeaderValue::from_str(&format!("Basic {}", bearer)).unwrap(),
)));
}

View file

@ -173,6 +173,7 @@ pub struct RemoteModulesStoreBuilder {
impl RemoteModulesStoreBuilder {
pub fn add(&mut self, specifier: &Url, media_type: MediaType, data: Vec<u8>) {
log::debug!("Adding '{}' ({})", specifier, media_type);
let specifier = specifier.to_string();
self.specifiers.push((specifier, self.data_byte_len));
self.data_byte_len += 1 + 8 + data.len() as u64; // media type (1 byte), data length (8 bytes), data
@ -182,6 +183,7 @@ impl RemoteModulesStoreBuilder {
pub fn add_redirects(&mut self, redirects: &BTreeMap<Url, Url>) {
self.redirects.reserve(redirects.len());
for (from, to) in redirects {
log::debug!("Adding redirect '{}' -> '{}'", from, to);
let from = from.to_string();
let to = to.to_string();
self.redirects_len += (4 + from.len() + 4 + to.len()) as u64;
@ -354,17 +356,17 @@ impl RemoteModulesStore {
pub fn read<'a>(
&'a self,
specifier: &'a Url,
original_specifier: &'a Url,
) -> Result<Option<DenoCompileModuleData<'a>>, AnyError> {
let mut count = 0;
let mut current = specifier;
let mut specifier = original_specifier;
loop {
if count > 10 {
bail!("Too many redirects resolving '{}'", specifier);
bail!("Too many redirects resolving '{}'", original_specifier);
}
match self.specifiers.get(current) {
match self.specifiers.get(specifier) {
Some(RemoteModulesStoreSpecifierValue::Redirect(to)) => {
current = to;
specifier = to;
count += 1;
}
Some(RemoteModulesStoreSpecifierValue::Data(offset)) => {

View file

@ -24,32 +24,29 @@ pub fn init_project(init_flags: InitFlags) -> Result<(), AnyError> {
create_file(
&dir,
"main.ts",
r#"import { type Route, route, serveDir } from "@std/http";
r#"import { serveDir } from "@std/http";
const routes: Route[] = [
{
pattern: new URLPattern({ pathname: "/" }),
handler: () => new Response("Home page"),
},
{
pattern: new URLPattern({ pathname: "/users/:id" }),
handler: (_req, _info, params) => new Response(params?.pathname.groups.id),
},
{
pattern: new URLPattern({ pathname: "/static/*" }),
handler: (req) => serveDir(req),
},
];
function defaultHandler(_req: Request) {
return new Response("Not found", { status: 404 });
}
const handler = route(routes, defaultHandler);
const userPagePattern = new URLPattern({ pathname: "/users/:id" });
const staticPathPattern = new URLPattern({ pathname: "/static/*" });
export default {
fetch(req) {
return handler(req);
const url = new URL(req.url);
if (url.pathname === "/") {
return new Response("Home page");
}
const userPageMatch = userPagePattern.exec(url);
if (userPageMatch) {
return new Response(userPageMatch.pathname.groups.id);
}
if (staticPathPattern.test(url)) {
return serveDir(req);
}
return new Response("Not found", { status: 404 });
},
} satisfies Deno.ServeDefaultExport;
"#,

View file

@ -1337,18 +1337,12 @@ delete Object.prototype.__proto__;
"console",
"Console",
"ErrorConstructor",
"exports",
"gc",
"Global",
"ImportMeta",
"localStorage",
"module",
"NodeModule",
"NodeRequire",
"process",
"queueMicrotask",
"RequestInit",
"require",
"ResponseInit",
"sessionStorage",
"setImmediate",

View file

@ -62,6 +62,8 @@ export function processTicksAndRejections() {
callback(...args);
}
}
} catch (e) {
reportError(e);
} finally {
// FIXME(bartlomieju): Deno currently doesn't support async hooks
// if (destroyHooksExist())

View file

@ -1,6 +1,9 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials
import { notImplemented } from "ext:deno_node/_utils.ts";
import { urlToHttpOptions } from "ext:deno_node/internal/url.ts";
import {
@ -14,14 +17,6 @@ import { type ServerHandler, ServerImpl as HttpServer } from "node:http";
import { validateObject } from "ext:deno_node/internal/validators.mjs";
import { kEmptyObject } from "ext:deno_node/internal/util.mjs";
import { Buffer } from "node:buffer";
import { primordials } from "ext:core/mod.js";
const {
ArrayPrototypeShift,
ArrayPrototypeUnshift,
ArrayIsArray,
ObjectPrototypeIsPrototypeOf,
ObjectAssign,
} = primordials;
export class Server extends HttpServer {
constructor(opts, requestListener?: ServerHandler) {
@ -34,11 +29,11 @@ export class Server extends HttpServer {
validateObject(opts, "options");
}
if (opts.cert && ArrayIsArray(opts.cert)) {
if (opts.cert && Array.isArray(opts.cert)) {
notImplemented("https.Server.opts.cert array type");
}
if (opts.key && ArrayIsArray(opts.key)) {
if (opts.key && Array.isArray(opts.key)) {
notImplemented("https.Server.opts.key array type");
}
@ -47,12 +42,10 @@ export class Server extends HttpServer {
_additionalServeOptions() {
return {
cert: ObjectPrototypeIsPrototypeOf(Buffer, this._opts.cert)
// deno-lint-ignore prefer-primordials
cert: this._opts.cert instanceof Buffer
? this._opts.cert.toString()
: this._opts.cert,
key: ObjectPrototypeIsPrototypeOf(Buffer, this._opts.key)
// deno-lint-ignore prefer-primordials
key: this._opts.key instanceof Buffer
? this._opts.key.toString()
: this._opts.key,
};
@ -166,18 +159,18 @@ export function request(...args: any[]) {
let options = {};
if (typeof args[0] === "string") {
const urlStr = ArrayPrototypeShift(args);
const urlStr = args.shift();
options = urlToHttpOptions(new URL(urlStr));
} else if (ObjectPrototypeIsPrototypeOf(URL, args[0])) {
options = urlToHttpOptions(ArrayPrototypeShift(args));
} else if (args[0] instanceof URL) {
options = urlToHttpOptions(args.shift());
}
if (args[0] && typeof args[0] !== "function") {
ObjectAssign(options, ArrayPrototypeShift(args));
Object.assign(options, args.shift());
}
options._defaultAgent = globalAgent;
ArrayPrototypeUnshift(args, options);
args.unshift(options);
return new HttpsClientRequest(args[0], args[1]);
}

View file

@ -321,6 +321,48 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec<FixSuggestion> {
]),
FixSuggestion::docs("https://docs.deno.com/go/commonjs"),
];
} else if msg.contains("__filename is not defined") {
return vec![
FixSuggestion::info(cstr!(
"<u>__filename</> global is not available in ES modules."
)),
FixSuggestion::hint(cstr!("Use <u>import.meta.filename</> instead.")),
];
} else if msg.contains("__dirname is not defined") {
return vec![
FixSuggestion::info(cstr!(
"<u>__dirname</> global is not available in ES modules."
)),
FixSuggestion::hint(cstr!("Use <u>import.meta.dirname</> instead.")),
];
} else if msg.contains("Buffer is not defined") {
return vec![
FixSuggestion::info(cstr!(
"<u>Buffer</> is not available in the global scope in Deno."
)),
FixSuggestion::hint(cstr!("Import it explicitly with <u>import { Buffer } from \"node:buffer\";</>.")),
];
} else if msg.contains("clearImmediate is not defined") {
return vec![
FixSuggestion::info(cstr!(
"<u>clearImmediate</> is not available in the global scope in Deno."
)),
FixSuggestion::hint(cstr!("Import it explicitly with <u>import { clearImmediate } from \"node:timers\";</>.")),
];
} else if msg.contains("setImmediate is not defined") {
return vec![
FixSuggestion::info(cstr!(
"<u>setImmediate</> is not available in the global scope in Deno."
)),
FixSuggestion::hint(cstr!("Import it explicitly with <u>import { setImmediate } from \"node:timers\";</>.")),
];
} else if msg.contains("global is not defined") {
return vec![
FixSuggestion::info(cstr!(
"<u>global</> is not available in the global scope in Deno."
)),
FixSuggestion::hint(cstr!("Use <u>globalThis</> instead, or assign <u>globalThis.global = globalThis</>.")),
];
} else if msg.contains("openKv is not a function") {
return vec![
FixSuggestion::info("Deno.openKv() is an unstable API."),

View file

@ -212,3 +212,7 @@ itest!(unhandled_rejection_web_process {
envs: env_vars_for_npm_tests(),
http_server: true,
});
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// The itest macro is deprecated. Please move your new test to ~/tests/specs.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

View file

@ -43,9 +43,6 @@
// TODO(littledivy): windows ipc streams not yet implemented
"test-child-process-fork-ref.js",
"test-child-process-fork-ref2.js",
// TODO(bartlomieju): this test is very flaky on CI
// https://github.com/denoland/deno/issues/25855
// "test-child-process-ipc-next-tick.js",
"test-child-process-ipc.js",
"test-child-process-spawnsync-env.js",
"test-child-process-stdio-inherit.js",
@ -240,6 +237,7 @@
"test-child-process-execfilesync-maxbuf.js",
"test-child-process-execsync-maxbuf.js",
"test-child-process-flush-stdio.js",
"test-child-process-ipc-next-tick.js",
"test-child-process-kill.js",
"test-child-process-set-blocking.js",
"test-child-process-spawn-args.js",

View file

@ -280,7 +280,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
- [parallel/test-child-process-fork3.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-child-process-fork3.js)
- [parallel/test-child-process-http-socket-leak.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-child-process-http-socket-leak.js)
- [parallel/test-child-process-internal.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-child-process-internal.js)
- [parallel/test-child-process-ipc-next-tick.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-child-process-ipc-next-tick.js)
- [parallel/test-child-process-ipc.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-child-process-ipc.js)
- [parallel/test-child-process-no-deprecation.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-child-process-no-deprecation.js)
- [parallel/test-child-process-pipe-dataflow.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-child-process-pipe-dataflow.js)

View file

@ -0,0 +1,46 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.12.1
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const NUM_MESSAGES = 10;
const values = [];
for (let i = 0; i < NUM_MESSAGES; ++i) {
values[i] = i;
}
if (process.argv[2] === 'child') {
const received = values.map(() => { return false; });
process.on('uncaughtException', common.mustCall((err) => {
received[err] = true;
const done = received.every((element) => { return element === true; });
if (done)
process.disconnect();
}, NUM_MESSAGES));
process.on('message', (msg) => {
// If messages are handled synchronously, throwing should break the IPC
// message processing.
throw msg;
});
process.send('ready');
} else {
const child = cp.fork(__filename, ['child']);
child.on('message', common.mustCall((msg) => {
assert.strictEqual(msg, 'ready');
values.forEach((value) => {
child.send(value);
});
}));
}

View file

@ -0,0 +1,22 @@
{
"tempDir": true,
"steps": [{
"if": "unix",
"args": "compile -A --output main main.ts",
"output": "[WILDCARD]"
}, {
"if": "unix",
"commandName": "./main",
"args": [],
"output": "main.out"
}, {
"if": "windows",
"args": "compile -A --output main.exe main.ts",
"output": "[WILDCARD]"
}, {
"if": "windows",
"commandName": "./main.exe",
"args": [],
"output": "main.out"
}]
}

View file

@ -0,0 +1 @@
Hello

View file

@ -0,0 +1 @@
import "http://localhost:4546/run/003_relative_import.ts";

View file

@ -2,3 +2,6 @@ error: Uncaught (in promise) ReferenceError: setImmediate is not defined
const _foo = setImmediate;
^
at [WILDCARD]main.ts:3:14
info: setImmediate is not available in the global scope in Deno.
hint: Import it explicitly with import { setImmediate } from "node:timers";.

View file

@ -0,0 +1,4 @@
{
"args": "run main.ts",
"output": "main.out"
}

View file

@ -0,0 +1,2 @@
caught Error: thrown from next tick
at file:///[WILDCARD]/specs/node/next_tick_uncaught_exception/main.ts:4:15

View file

@ -0,0 +1,13 @@
import process from "node:process";
import { strictEqual } from "node:assert";
const error = new Error("thrown from next tick");
process.on("uncaughtException", (caught) => {
strictEqual(caught, error);
console.log("caught", caught);
});
process.nextTick(() => {
throw error;
});

View file

@ -1,4 +1,4 @@
{
"args": "run --allow-read --check=all compare_globals/main.ts",
"output": "compare_globals/main.out"
"args": "run --allow-read --check=all main.ts",
"output": "main.out"
}

View file

@ -8,10 +8,11 @@ Download http://localhost:4260/@denotest/globals/1.0.0.tgz
Download http://localhost:4260/@types/node/node-22.5.4.tgz
Download http://localhost:4260/undici-types/undici-types-6.19.8.tgz
[UNORDERED_END]
Check file:///[WILDCARD]/compare_globals/main.ts
Check file:///[WILDCARD]/main.ts
true
true
[]
process equals process true
setTimeout 1 false
setTimeout 2 function
setTimeout 3 function

View file

@ -5,10 +5,17 @@ console.log(globals.global === globals.globalThis);
// @ts-expect-error even though these are the same object, they have different types
console.log(globals.globalThis === globalThis);
console.log(globals.process.execArgv);
console.log("process equals process", process === globals.process);
type AssertTrue<T extends true> = never;
type _TestNoProcessGlobal = AssertTrue<
typeof globalThis extends { process: any } ? false : true
type _TestHasProcessGlobal = AssertTrue<
typeof globalThis extends { process: any } ? true : false
>;
type _TestProcessGlobalVersion = AssertTrue<
typeof process.versions.node extends string ? true : false
>;
type _TestNoBufferGlogal = AssertTrue<
typeof globalThis extends { Buffer: any } ? false : true
>;
type _TestHasNodeJsGlobal = NodeJS.Architecture;

View file

@ -1,6 +1,8 @@
@denotest:registry=http://localhost:4261/
//localhost:4261/:username=deno
//localhost:4261/:_password=land
# base64 of land
//localhost:4261/:_password=bGFuZA==
@denotest2:registry=http://localhost:4262/
//localhost:4262/:username=deno
//localhost:4262/:_password=land2
# base64 of land2
//localhost:4262/:_password=bGFuZDI=

View file

@ -0,0 +1,34 @@
{
"tests": {
"__dirname": {
"args": "run dirname.js",
"output": "dirname.out",
"exitCode": 1
},
"__filename": {
"args": "run filename.js",
"output": "filename.out",
"exitCode": 1
},
"clearImmediate": {
"args": "run clear_immediate.js",
"output": "clear_immediate.out",
"exitCode": 1
},
"buffer": {
"args": "run buffer.js",
"output": "buffer.out",
"exitCode": 1
},
"global": {
"args": "run global.js",
"output": "global.out",
"exitCode": 1
},
"setImmediate": {
"args": "run set_immediate.js",
"output": "set_immediate.out",
"exitCode": 1
}
}
}

View file

@ -0,0 +1 @@
Buffer;

View file

@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: Buffer is not defined
Buffer;
^
at [WILDCARD]buffer.js:1:1
info: Buffer is not available in the global scope in Deno.
hint: Import it explicitly with import { Buffer } from "node:buffer";.

View file

@ -0,0 +1 @@
clearImmediate;

View file

@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: clearImmediate is not defined
clearImmediate;
^
at [WILDCARD]clear_immediate.js:1:1
info: clearImmediate is not available in the global scope in Deno.
hint: Import it explicitly with import { clearImmediate } from "node:timers";.

View file

@ -0,0 +1 @@
__dirname;

View file

@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: __dirname is not defined
__dirname;
^
at [WILDCARD]dirname.js:1:1
info: __dirname global is not available in ES modules.
hint: Use import.meta.dirname instead.

View file

@ -0,0 +1 @@
__filename;

View file

@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: __filename is not defined
__filename;
^
at [WILDCARD]filename.js:1:1
info: __filename global is not available in ES modules.
hint: Use import.meta.filename instead.

View file

@ -0,0 +1 @@
global;

View file

@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: global is not defined
global;
^
at [WILDCARD]global.js:1:1
info: global is not available in the global scope in Deno.
hint: Use globalThis instead, or assign globalThis.global = globalThis.

View file

@ -0,0 +1 @@
setImmediate;

View file

@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: setImmediate is not defined
setImmediate;
^
at [WILDCARD]set_immediate.js:1:1
info: setImmediate is not available in the global scope in Deno.
hint: Import it explicitly with import { setImmediate } from "node:timers";.