1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 16:19:12 -05:00

API change: Deno.startHttp -> Deno.serveHttp (#10087)

This commit is contained in:
Ryan Dahl 2021-04-09 11:54:27 -04:00 committed by GitHub
parent cd0560210a
commit c6e7a243d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 18 deletions

View file

@ -9,7 +9,7 @@ const body = Deno.core.encode("Hello World");
for await (const conn of listener) {
(async () => {
const requests = Deno.startHttp(conn);
const requests = Deno.serveHttp(conn);
for await (const { respondWith } of requests) {
respondWith(new Response(body));
}

View file

@ -1211,18 +1211,20 @@ declare namespace Deno {
/** **UNSTABLE**: new API, yet to be vetted.
*
* Parse HTTP requests from the given connection
* Services HTTP requests given a TCP or TLS socket.
*
* ```ts
* const httpConn = await Deno.startHttp(conn);
* const { request, respondWith } = await httpConn.next();
* respondWith(new Response("Hello World"));
* const httpConn = Deno.serveHttp(conn);
* const e = await httpConn.nextRequest();
* if (e) {
* e.respondWith(new Response("Hello World"));
* }
* ```
*
* If `httpConn.next()` encounters an error or returns `done == true` then
* the underlying HttpConn resource is closed automatically.
* If `httpConn.nextRequest()` encounters an error or returns `null`
* then the underlying HttpConn resource is closed automatically.
*/
export function startHttp(conn: Conn): HttpConn;
export function serveHttp(conn: Conn): HttpConn;
}
declare function fetch(

View file

@ -12,7 +12,7 @@ unitTest({ perms: { net: true } }, async function httpServerBasic() {
const promise = (async () => {
const listener = Deno.listen({ port: 4501 });
for await (const conn of listener) {
const httpConn = Deno.startHttp(conn);
const httpConn = Deno.serveHttp(conn);
for await (const { request, respondWith } of httpConn) {
assertEquals(await request.text(), "");
respondWith(new Response("Hello World"));
@ -41,7 +41,7 @@ unitTest(
const promise = (async () => {
const listener = Deno.listen({ port: 4501 });
const conn = await listener.accept();
const httpConn = Deno.startHttp(conn);
const httpConn = Deno.serveHttp(conn);
const evt = await httpConn.nextRequest();
assert(evt);
const { request, respondWith } = evt;
@ -70,7 +70,7 @@ unitTest(
const promise = (async () => {
const listener = Deno.listen({ port: 4501 });
const conn = await listener.accept();
const httpConn = Deno.startHttp(conn);
const httpConn = Deno.serveHttp(conn);
const evt = await httpConn.nextRequest();
assert(evt);
const { request, respondWith } = evt;
@ -101,7 +101,7 @@ unitTest({ perms: { net: true } }, async function httpServerStreamDuplex() {
const promise = (async () => {
const listener = Deno.listen({ port: 4501 });
const conn = await listener.accept();
const httpConn = Deno.startHttp(conn);
const httpConn = Deno.serveHttp(conn);
const evt = await httpConn.nextRequest();
assert(evt);
const { request, respondWith } = evt;
@ -136,7 +136,7 @@ unitTest({ perms: { net: true } }, async function httpServerStreamDuplex() {
unitTest({ perms: { net: true } }, async function httpServerClose() {
const listener = Deno.listen({ port: 4501 });
const client = await Deno.connect({ port: 4501 });
const httpConn = Deno.startHttp(await listener.accept());
const httpConn = Deno.serveHttp(await listener.accept());
client.close();
const evt = await httpConn.nextRequest();
assertEquals(evt, null);
@ -147,7 +147,7 @@ unitTest({ perms: { net: true } }, async function httpServerClose() {
unitTest({ perms: { net: true } }, async function httpServerInvalidMethod() {
const listener = Deno.listen({ port: 4501 });
const client = await Deno.connect({ port: 4501 });
const httpConn = Deno.startHttp(await listener.accept());
const httpConn = Deno.serveHttp(await listener.accept());
await client.write(new Uint8Array([1, 2, 3]));
await assertThrowsAsync(
async () => {
@ -175,7 +175,7 @@ unitTest(
keyFile: "cli/tests/tls/localhost.key",
});
const conn = await listener.accept();
const httpConn = Deno.startHttp(conn);
const httpConn = Deno.serveHttp(conn);
const evt = await httpConn.nextRequest();
assert(evt);
const { request, respondWith } = evt;

View file

@ -18,7 +18,7 @@
return entries;
}
function startHttp(conn) {
function serveHttp(conn) {
const rid = Deno.core.jsonOpSync("op_http_start", conn.rid);
return new HttpConn(rid);
}
@ -205,6 +205,6 @@
}
window.__bootstrap.http = {
startHttp,
serveHttp,
};
})(this);

View file

@ -120,7 +120,7 @@
listen: __bootstrap.netUnstable.listen,
connect: __bootstrap.netUnstable.connect,
listenDatagram: __bootstrap.netUnstable.listenDatagram,
startHttp: __bootstrap.http.startHttp,
serveHttp: __bootstrap.http.serveHttp,
startTls: __bootstrap.tls.startTls,
fstatSync: __bootstrap.fs.fstatSync,
fstat: __bootstrap.fs.fstat,