aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Derrick Farris <mr.dcfarris@gmail.com> 2023-03-01 20:11:38 -0600
committerGravatar GitHub <noreply@github.com> 2023-03-01 18:11:38 -0800
commit7c81d97684e39e1fb37bef7f39ea13c936f6b99a (patch)
treee3534bfed041969c6df35f4303c8f3beee7e9806 /src/bun.js
parent6bc075e377a1f4dcb49ea27c9224117c6cbada70 (diff)
downloadbun-7c81d97684e39e1fb37bef7f39ea13c936f6b99a.tar.gz
bun-7c81d97684e39e1fb37bef7f39ea13c936f6b99a.tar.zst
bun-7c81d97684e39e1fb37bef7f39ea13c936f6b99a.zip
fix(node:http/https): fix passing `URL` objs to `http.request`(#2253) (#2258)
* fix(node:http/https): fix passing `URL` objs to `http.request`(#2253) * fix(node:http): hoist debug env var * fix(node:http): make body `undefined` when falsy
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/http.exports.js31
1 files changed, 16 insertions, 15 deletions
diff --git a/src/bun.js/http.exports.js b/src/bun.js/http.exports.js
index 0cc51af62..9ea6fec8c 100644
--- a/src/bun.js/http.exports.js
+++ b/src/bun.js/http.exports.js
@@ -1,12 +1,15 @@
const { EventEmitter } = import.meta.require("node:events");
const { Readable, Writable } = import.meta.require("node:stream");
+const { URL } = import.meta.require("node:url");
const { newArrayWithSize, String, Object, Array } = import.meta.primordials;
const globalReportError = globalThis.reportError;
const setTimeout = globalThis.setTimeout;
const fetch = Bun.fetch;
const nop = () => {};
-const debug = process.env.BUN_JS_DEBUG ? (...args) => console.log("node:http", ...args) : nop;
+
+const __DEBUG__ = process.env.BUN_JS_DEBUG;
+const debug = __DEBUG__ ? (...args) => console.log("node:http", ...args) : nop;
const kEmptyObject = Object.freeze(Object.create(null));
const kOutHeaders = Symbol.for("kOutHeaders");
@@ -236,13 +239,13 @@ export class Server extends EventEmitter {
if (typeof port === "function") {
onListen = port;
} else if (typeof port === "object") {
- port?.signal?.addEventListener("abort", ()=> {
+ port?.signal?.addEventListener("abort", () => {
this.close();
});
host = port?.host;
port = port?.port;
-
+
if (typeof port?.callback === "function") onListen = port?.callback;
}
const ResponseClass = this.#options.ServerResponse || ServerResponse;
@@ -549,7 +552,6 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
return true;
}
-
export class OutgoingMessage extends Writable {
#headers;
headersSent = false;
@@ -951,19 +953,19 @@ export class ClientRequest extends OutgoingMessage {
_final(callback) {
this.#finished = true;
this[kAbortController] = new AbortController();
- this[kAbortController].signal.addEventListener("abort", ()=> {
- this[kClearTimeout]();
+ this[kAbortController].signal.addEventListener("abort", () => {
+ this[kClearTimeout]();
});
- if(this.#signal?.aborted){
+ if (this.#signal?.aborted) {
this[kAbortController].abort();
}
this.#fetchRequest = fetch(`${this.#protocol}//${this.#host}:${this.#port}${this.#path}`, {
method: this.#method,
headers: this.getHeaders(),
- body: this.#body,
+ body: this.#body || undefined,
redirect: "manual",
- verbose: Boolean(process.env.BUN_JS_DEBUG),
- signal: this[kAbortController].signal
+ verbose: Boolean(__DEBUG__),
+ signal: this[kAbortController].signal,
})
.then(response => {
var res = (this.#res = new IncomingMessage(response, {
@@ -972,7 +974,7 @@ export class ClientRequest extends OutgoingMessage {
this.emit("response", res);
})
.catch(err => {
- if (process.env.BUN_JS_DEBUG) globalReportError(err);
+ if (__DEBUG__) globalReportError(err);
this.emit("error", err);
})
.finally(() => {
@@ -999,7 +1001,7 @@ export class ClientRequest extends OutgoingMessage {
if (typeof input === "string") {
const urlStr = input;
input = urlToHttpOptions(new URL(urlStr));
- } else if (input && input[searchParamsSymbol] && input[searchParamsSymbol][searchParamsSymbol]) {
+ } else if (input && typeof input === "object" && input instanceof URL) {
// url.URL instance
input = urlToHttpOptions(input);
} else {
@@ -1045,13 +1047,12 @@ export class ClientRequest extends OutgoingMessage {
this.#socketPath = options.socketPath;
- if (options.timeout !== undefined)
- this.setTimeout(options.timeout, null);
+ if (options.timeout !== undefined) this.setTimeout(options.timeout, null);
const signal = options.signal;
if (signal) {
//We still want to control abort function and timeout so signal call our AbortController
- signal.addEventListener("abort", ()=> {
+ signal.addEventListener("abort", () => {
this[kAbortController]?.abort();
});
this.#signal = signal;