summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/http.ts
diff options
context:
space:
mode:
authorIgor Zinkovsky <igor@deno.com>2024-02-29 14:56:04 -0800
committerGitHub <noreply@github.com>2024-02-29 17:56:04 -0500
commitdc16c996dd83164011f3931a8bb49f25624601af (patch)
treee85359f121bfa8034ee90af439c57a1d4faa7d8a /ext/node/polyfills/http.ts
parent627c49c9d8bd06aac374932582596c866650666d (diff)
fix(ext/node) add node http methods (#22630)
fixes #22627 This PR fixes a node compat issue that is preventing `serverless-http` and `serverless-express` npm modules from working with Deno. These modules are useful for running apps on AWS Lambda (and other serverless infra). --------- Signed-off-by: Igor Zinkovsky <igor@deno.com>
Diffstat (limited to 'ext/node/polyfills/http.ts')
-rw-r--r--ext/node/polyfills/http.ts34
1 files changed, 33 insertions, 1 deletions
diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts
index 28b7f15b2..bcbf6674f 100644
--- a/ext/node/polyfills/http.ts
+++ b/ext/node/polyfills/http.ts
@@ -51,6 +51,7 @@ import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts";
import {
connResetException,
ERR_HTTP_HEADERS_SENT,
+ ERR_HTTP_SOCKET_ASSIGNED,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_HTTP_TOKEN,
ERR_INVALID_PROTOCOL,
@@ -1340,6 +1341,8 @@ export class ServerResponse extends NodeWritable {
headersSent = false;
#firstChunk: Chunk | null = null;
#resolve: (value: Response | PromiseLike<Response>) => void;
+ // deno-lint-ignore no-explicit-any
+ #socketOverride: any | null = null;
static #enqueue(controller: ReadableStreamDefaultController, chunk: Chunk) {
if (typeof chunk === "string") {
@@ -1369,7 +1372,11 @@ export class ServerResponse extends NodeWritable {
autoDestroy: true,
defaultEncoding: "utf-8",
emitClose: true,
- write: (chunk, _encoding, cb) => {
+ write: (chunk, encoding, cb) => {
+ if (this.#socketOverride && this.#socketOverride.writable) {
+ this.#socketOverride.write(chunk, encoding);
+ return cb();
+ }
if (!this.headersSent) {
if (this.#firstChunk === null) {
this.#firstChunk = chunk;
@@ -1418,6 +1425,9 @@ export class ServerResponse extends NodeWritable {
getHeaderNames() {
return Array.from(this.#headers.keys());
}
+ getHeaders() {
+ return Object.fromEntries(this.#headers.entries());
+ }
hasHeader(name: string) {
return this.#headers.has(name);
}
@@ -1483,6 +1493,20 @@ export class ServerResponse extends NodeWritable {
_implicitHeader() {
this.writeHead(this.statusCode);
}
+
+ assignSocket(socket) {
+ if (socket._httpMessage) {
+ throw new ERR_HTTP_SOCKET_ASSIGNED();
+ }
+ socket._httpMessage = this;
+ this.#socketOverride = socket;
+ }
+
+ detachSocket(socket) {
+ assert(socket._httpMessage === this);
+ socket._httpMessage = null;
+ this.#socketOverride = null;
+ }
}
// TODO(@AaronO): optimize
@@ -1534,6 +1558,10 @@ export class IncomingMessageForServer extends NodeReadable {
return "1.1";
}
+ set httpVersion(val) {
+ assert(val === "1.1");
+ }
+
get headers() {
if (!this.#headers) {
this.#headers = {};
@@ -1546,6 +1574,10 @@ export class IncomingMessageForServer extends NodeReadable {
return this.#headers;
}
+ set headers(val) {
+ this.#headers = val;
+ }
+
get upgrade(): boolean {
return Boolean(
this.#req.headers.get("connection")?.toLowerCase().includes("upgrade") &&