Skip to content

Commit

Permalink
chore(net): soft-remove Deno.serveHttp() (#25451)
Browse files Browse the repository at this point in the history
Towards #22079

---------

Co-authored-by: Bartek Iwańczuk <[email protected]>
  • Loading branch information
iuioiua and bartlomieju authored Sep 5, 2024
1 parent 6919f33 commit 7937ae3
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 167 deletions.
135 changes: 15 additions & 120 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4777,110 +4777,6 @@ declare namespace Deno {
mtime: number | Date,
): Promise<void>;

/** The event yielded from an {@linkcode HttpConn} which represents an HTTP
* request from a remote client.
*
* @category HTTP Server
*
* @deprecated This will be removed in Deno 2.0. See the
* {@link https://docs.deno.com/runtime/manual/advanced/migrate_deprecations | Deno 1.x to 2.x Migration Guide}
* for migration instructions.
*/
export interface RequestEvent {
/** The request from the client in the form of the web platform
* {@linkcode Request}. */
readonly request: Request;
/** The method to be used to respond to the event. The response needs to
* either be an instance of {@linkcode Response} or a promise that resolves
* with an instance of `Response`.
*
* When the response is successfully processed then the promise returned
* will be resolved. If there are any issues with sending the response,
* the promise will be rejected. */
respondWith(r: Response | PromiseLike<Response>): Promise<void>;
}

/**
* The async iterable that is returned from {@linkcode serveHttp} which
* yields up {@linkcode RequestEvent} events, representing individual
* requests on the HTTP server connection.
*
* @category HTTP Server
*
* @deprecated This will be removed in Deno 2.0. See the
* {@link https://docs.deno.com/runtime/manual/advanced/migrate_deprecations | Deno 1.x to 2.x Migration Guide}
* for migration instructions.
*/
export interface HttpConn extends AsyncIterable<RequestEvent>, Disposable {
/** The resource ID associated with this connection. Generally users do not
* need to be aware of this identifier. */
readonly rid: number;

/** An alternative to the async iterable interface which provides promises
* which resolve with either a {@linkcode RequestEvent} when there is
* another request or `null` when the client has closed the connection. */
nextRequest(): Promise<RequestEvent | null>;
/** Initiate a server side closure of the connection, indicating to the
* client that you refuse to accept any more requests on this connection.
*
* Typically the client closes the connection, which will result in the
* async iterable terminating or the `nextRequest()` method returning
* `null`. */
close(): void;
}

/**
* Provides an interface to handle HTTP request and responses over TCP or TLS
* connections. The method returns an {@linkcode HttpConn} which yields up
* {@linkcode RequestEvent} events, which utilize the web platform standard
* {@linkcode Request} and {@linkcode Response} objects to handle the request.
*
* ```ts
* const conn = Deno.listen({ port: 80 });
* const httpConn = Deno.serveHttp(await conn.accept());
* const e = await httpConn.nextRequest();
* if (e) {
* e.respondWith(new Response("Hello World"));
* }
* ```
*
* Alternatively, you can also use the async iterator approach:
*
* ```ts
* async function handleHttp(conn: Deno.Conn) {
* for await (const e of Deno.serveHttp(conn)) {
* e.respondWith(new Response("Hello World"));
* }
* }
*
* for await (const conn of Deno.listen({ port: 80 })) {
* handleHttp(conn);
* }
* ```
*
* If `httpConn.nextRequest()` encounters an error or returns `null` then the
* underlying {@linkcode HttpConn} resource is closed automatically.
*
* Also see the experimental Flash HTTP server {@linkcode Deno.serve} which
* provides a ground up rewrite of handling of HTTP requests and responses
* within the Deno CLI.
*
* Note that this function *consumes* the given connection passed to it, thus
* the original connection will be unusable after calling this. Additionally,
* you need to ensure that the connection is not being used elsewhere when
* calling this function in order for the connection to be consumed properly.
*
* For instance, if there is a `Promise` that is waiting for read operation on
* the connection to complete, it is considered that the connection is being
* used elsewhere. In such a case, this function will fail.
*
* @category HTTP Server
* @deprecated This will be soft-removed in Deno 2.0. See the
* {@link https://docs.deno.com/runtime/manual/advanced/migrate_deprecations | Deno 1.x to 2.x Migration Guide}
* for migration instructions.
*/
export function serveHttp(conn: Conn): HttpConn;

/** The object that is returned from a {@linkcode Deno.upgradeWebSocket}
* request.
*
Expand Down Expand Up @@ -4923,22 +4819,21 @@ declare namespace Deno {
* with the returned response for the websocket upgrade to be successful.
*
* ```ts
* const conn = Deno.listen({ port: 80 });
* const httpConn = Deno.serveHttp(await conn.accept());
* const e = await httpConn.nextRequest();
* if (e) {
* const { socket, response } = Deno.upgradeWebSocket(e.request);
* socket.onopen = () => {
* socket.send("Hello World!");
* };
* socket.onmessage = (e) => {
* console.log(e.data);
* socket.close();
* };
* socket.onclose = () => console.log("WebSocket has been closed.");
* socket.onerror = (e) => console.error("WebSocket error:", e);
* e.respondWith(response);
* }
* Deno.serve((req) => {
* if (req.headers.get("upgrade") !== "websocket") {
* return new Response(null, { status: 501 });
* }
* const { socket, response } = Deno.upgradeWebSocket(req);
* socket.addEventListener("open", () => {
* console.log("a client connected!");
* });
* socket.addEventListener("message", (event) => {
* if (event.data === "ping") {
* socket.send("pong");
* }
* });
* return response;
* });
* ```
*
* If the request body is disturbed (read from) before the upgrade is
Expand Down
7 changes: 1 addition & 6 deletions ext/http/01_http.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { core, internals, primordials } from "ext:core/mod.js";
import { core, primordials } from "ext:core/mod.js";
const {
BadResourcePrototype,
InterruptedPrototype,
Expand Down Expand Up @@ -396,11 +396,6 @@ function createRespondWith(
}

function serveHttp(conn) {
internals.warnOnDeprecatedApi(
"Deno.serveHttp()",
new Error().stack,
"Use `Deno.serve()` instead.",
);
const rid = op_http_start(conn[internalRidSymbol]);
return new HttpConn(rid, conn.remoteAddr, conn.localAddr);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/run/error_for_await.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ for await (const conn of listener) {
}

function handleConn(conn: Deno.Conn) {
const httpConn = Deno.serveHttp(conn);
const httpConn = (Deno as any).serveHttp(conn);
for await (const event of httpConn) {
event.respondWith(new Response("html", { status: 200 }));
}
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/run/http2_request_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const listener = Deno.listen({
console.log("READY");

for await (const conn of listener) {
// @ts-ignore `Deno.serveHttp()` was soft-removed in Deno 2.
for await (const { request, respondWith } of Deno.serveHttp(conn)) {
const href = new URL(request.url).href;
respondWith(new Response(href));
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/run/websocket_server_idletimeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const closeDeferred = Promise.withResolvers<void>();

const listener = Deno.listen({ port: 4509 });
console.log("READY");
// @ts-ignore `Deno.serveHttp()` was soft-removed in Deno 2.
const httpConn = Deno.serveHttp(await listener.accept());
const { request, respondWith } = (await httpConn.nextRequest())!;
const { response, socket } = Deno.upgradeWebSocket(request, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { promise, resolve } = Promise.withResolvers<void>();
const listener = Deno.listen({ port: 4319 });
console.log("READY");
const conn = await listener.accept();
// @ts-ignore `Deno.serveHttp()` was soft-removed in Deno 2.
const httpConn = Deno.serveHttp(conn);
const { request, respondWith } = (await httpConn.nextRequest())!;
const {
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/workers/http_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const listener = Deno.listen({ hostname: "127.0.0.1", port: 4506 });
postMessage("ready");
for await (const conn of listener) {
(async () => {
// @ts-ignore `Deno.serveHttp()` was soft-removed in Deno 2.
const requests = Deno.serveHttp(conn);
for await (const { respondWith } of requests) {
respondWith(new Response("Hello world"));
Expand Down
Loading

0 comments on commit 7937ae3

Please sign in to comment.