1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 16:49:18 -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) { for await (const conn of listener) {
(async () => { (async () => {
const requests = Deno.startHttp(conn); const requests = Deno.serveHttp(conn);
for await (const { respondWith } of requests) { for await (const { respondWith } of requests) {
respondWith(new Response(body)); respondWith(new Response(body));
} }

View file

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

View file

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

View file

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

View file

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