1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

feat: top-level-for-await (#3212)

This commit is contained in:
Andy Hayden 2019-10-27 06:04:42 -07:00 committed by Ry Dahl
parent 51dd91a3cc
commit aec5a646c9
18 changed files with 257 additions and 263 deletions

View file

@ -1,5 +1,6 @@
cli/tests/error_syntax.js
cli/tests/badly_formatted.js
cli/tests/top_level_for_await.js
std/**/testdata
std/**/vendor
std/node_modules
std/node_modules

View file

@ -616,6 +616,9 @@ window.compilerMain = function compilerMain(): void {
diagnostics = ts.getPreEmitDiagnostics(program).filter(
({ code }): boolean => {
// TS1103: 'for-await-of' statement is only allowed within an async
// function or async generator.
if (code === 1103) return false;
// TS1308: 'await' expression is only allowed within an async
// function.
if (code === 1308) return false;

View file

@ -1,7 +1,3 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function main(): Promise<void> {
const res = await fetch("http://localhost:4545/std/examples/colors.ts");
console.log(`Response http: ${await res.text()}`);
}
main();
const res = await fetch("http://localhost:4545/std/examples/colors.ts");
console.log(`Response http: ${await res.text()}`);

View file

@ -55,11 +55,7 @@ async function testModuleDownload(): Promise<void> {
http.close();
}
async function main(): Promise<void> {
proxyServer();
await testFetch();
await testModuleDownload();
Deno.exit(0);
}
main();
proxyServer();
await testFetch();
await testModuleDownload();
Deno.exit(0);

View file

@ -597,3 +597,13 @@ itest!(top_level_await_ts {
args: "--allow-read top_level_await.ts",
output: "top_level_await.out",
});
itest!(top_level_for_await {
args: "top_level_for_await.js",
output: "top_level_for_await.out",
});
itest!(top_level_for_await_ts {
args: "top_level_for_await.ts",
output: "top_level_for_await.out",
});

View file

@ -0,0 +1,10 @@
async function* asyncGenerator() {
let i = 0;
while (i < 3) {
yield i++;
}
}
for await (const num of asyncGenerator()) {
console.log(num);
};

View file

@ -0,0 +1,3 @@
0
1
2

View file

@ -0,0 +1,10 @@
async function* asyncGenerator(): AsyncIterableIterator<number> {
let i = 0;
while (i < 3) {
yield i++;
}
}
for await (const num of asyncGenerator()) {
console.log(num);
}

View file

@ -1,10 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function cat(filenames: string[]): Promise<void> {
for (const filename of filenames) {
const file = await Deno.open(filename);
await Deno.copy(Deno.stdout, file);
file.close();
}
const filenames = Deno.args.slice(1);
for (const filename of filenames) {
const file = await Deno.open(filename);
await Deno.copy(Deno.stdout, file);
file.close();
}
cat(Deno.args.slice(1));

View file

@ -79,32 +79,28 @@ function print(data: any): void {
}
}
async function main(): Promise<void> {
const parsedArgs = parse(Deno.args.slice(1));
const parsedArgs = parse(Deno.args.slice(1));
if (parsedArgs.h || parsedArgs.help || parsedArgs._.length === 0) {
console.log("Usage: catj [-h|--help] [file...]");
console.log();
console.log("Examples:");
console.log();
console.log("// print file:\n catj file.json");
console.log();
console.log("// print multiple files:\n catj file1.json file2.json");
console.log();
console.log("// print from stdin:\n cat file.json | catj -");
}
if (parsedArgs._[0] === "-") {
const contents = await Deno.readAll(Deno.stdin);
const json = JSON.parse(decoder.decode(contents));
print(json);
} else {
for (const fileName of parsedArgs._) {
const fileContents = await Deno.readFile(fileName);
const json = JSON.parse(decoder.decode(fileContents));
print(json);
}
}
if (parsedArgs.h || parsedArgs.help || parsedArgs._.length === 0) {
console.log("Usage: catj [-h|--help] [file...]");
console.log();
console.log("Examples:");
console.log();
console.log("// print file:\n catj file.json");
console.log();
console.log("// print multiple files:\n catj file1.json file2.json");
console.log();
console.log("// print from stdin:\n cat file.json | catj -");
}
main();
if (parsedArgs._[0] === "-") {
const contents = await Deno.readAll(Deno.stdin);
const json = JSON.parse(decoder.decode(contents));
print(json);
} else {
for (const fileName of parsedArgs._) {
const fileContents = await Deno.readFile(fileName);
const json = JSON.parse(decoder.decode(fileContents));
print(json);
}
}

View file

@ -1,8 +1,4 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function curl(url: string): Promise<void> {
const res = await fetch(url);
await Deno.copy(Deno.stdout, res.body);
}
await curl(Deno.args[1]);
Deno.exit(0);
const url = Deno.args[1];
const res = await fetch(url);
await Deno.copy(Deno.stdout, res.body);

View file

@ -56,14 +56,11 @@ console.log("Set-Cookie:", cookieHeader);
```typescript
import { serve } from "https://deno.land/std/http/server.ts";
const s = serve("0.0.0.0:8000");
const body = new TextEncoder().encode("Hello World\n");
async function main() {
for await (const req of s) {
req.respond({ body: new TextEncoder().encode("Hello World\n") });
}
for await (const req of s) {
req.respond({ body });
}
main();
```
### File Server

View file

@ -2,10 +2,8 @@
// This is an example of a server that responds with an empty body
import { serve } from "../server.ts";
window.onload = async function main() {
const addr = "0.0.0.0:4502";
console.log(`Simple server listening on ${addr}`);
for await (const req of serve(addr)) {
req.respond({});
}
const addr = "0.0.0.0:4502";
console.log(`Simple server listening on ${addr}`);
for await (const req of serve(addr)) {
req.respond({});
}

View file

@ -937,12 +937,10 @@ Example:
import { serve } from "http/server.ts";
window.onload = async function() {
const body = new TextEncoder().encode("Hello World\n");
for await (const req of serve(":8000")) {
req.respond({ body });
}
};
const body = new TextEncoder().encode("Hello World\n");
for await (const req of serve(":8000")) {
req.respond({ body });
}
```
```shell

View file

@ -15,61 +15,56 @@ import {
WebSocket
} from "https://deno.land/std/ws/mod.ts";
/** websocket echo server */
const port = Deno.args[1] || "8080";
async function main(): Promise<void> {
console.log(`websocket server is running on :${port}`);
for await (const req of serve(`:${port}`)) {
const { headers, conn } = req;
acceptWebSocket({
conn,
headers,
bufReader: req.r,
bufWriter: req.w
})
.then(
async (sock: WebSocket): Promise<void> => {
console.log("socket connected!");
const it = sock.receive();
while (true) {
try {
const { done, value } = await it.next();
if (done) {
break;
}
const ev = value;
if (typeof ev === "string") {
// text message
console.log("ws:Text", ev);
await sock.send(ev);
} else if (ev instanceof Uint8Array) {
// binary message
console.log("ws:Binary", ev);
} else if (isWebSocketPingEvent(ev)) {
const [, body] = ev;
// ping
console.log("ws:Ping", body);
} else if (isWebSocketCloseEvent(ev)) {
// close
const { code, reason } = ev;
console.log("ws:Close", code, reason);
}
} catch (e) {
console.error(`failed to receive frame: ${e}`);
await sock.close(1000).catch(console.error);
console.log(`websocket server is running on :${port}`);
for await (const req of serve(`:${port}`)) {
const { headers, conn } = req;
acceptWebSocket({
conn,
headers,
bufReader: req.r,
bufWriter: req.w
})
.then(
async (sock: WebSocket): Promise<void> => {
console.log("socket connected!");
const it = sock.receive();
while (true) {
try {
const { done, value } = await it.next();
if (done) {
break;
}
const ev = value;
if (typeof ev === "string") {
// text message
console.log("ws:Text", ev);
await sock.send(ev);
} else if (ev instanceof Uint8Array) {
// binary message
console.log("ws:Binary", ev);
} else if (isWebSocketPingEvent(ev)) {
const [, body] = ev;
// ping
console.log("ws:Ping", body);
} else if (isWebSocketCloseEvent(ev)) {
// close
const { code, reason } = ev;
console.log("ws:Close", code, reason);
}
} catch (e) {
console.error(`failed to receive frame: ${e}`);
await sock.close(1000).catch(console.error);
}
}
)
.catch(
(err: Error): void => {
console.error(`failed to accept websocket: ${err}`);
}
);
}
}
if (import.meta.main) {
main();
}
)
.catch(
(err: Error): void => {
console.error(`failed to accept websocket: ${err}`);
}
);
}
```
@ -88,46 +83,49 @@ import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts";
import { blue, green, red, yellow } from "https://deno.land/std/fmt/colors.ts";
const endpoint = Deno.args[1] || "ws://127.0.0.1:8080";
async function main(): Promise<void> {
const sock = await connectWebSocket(endpoint);
console.log(green("ws connected! (type 'close' to quit)"));
(async function(): Promise<void> {
for await (const msg of sock.receive()) {
if (typeof msg === "string") {
console.log(yellow("< " + msg));
} else if (isWebSocketPingEvent(msg)) {
console.log(blue("< ping"));
} else if (isWebSocketPongEvent(msg)) {
console.log(blue("< pong"));
} else if (isWebSocketCloseEvent(msg)) {
console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`));
}
/** simple websocket cli */
const sock = await connectWebSocket(endpoint);
console.log(green("ws connected! (type 'close' to quit)"));
(async function(): Promise<void> {
for await (const msg of sock.receive()) {
if (typeof msg === "string") {
console.log(yellow("< " + msg));
} else if (isWebSocketPingEvent(msg)) {
console.log(blue("< ping"));
} else if (isWebSocketPongEvent(msg)) {
console.log(blue("< pong"));
} else if (isWebSocketCloseEvent(msg)) {
console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`));
}
})();
const tpr = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
await Deno.stdout.write(encode("> "));
const [line, err] = await tpr.readLine();
if (err) {
console.error(red(`failed to read line from stdin: ${err}`));
break;
}
if (line === "close") {
break;
} else if (line === "ping") {
await sock.ping();
} else {
await sock.send(line);
}
await new Promise((resolve): number => setTimeout(resolve, 0));
}
await sock.close(1000);
Deno.exit(0);
}
})();
if (import.meta.main) {
main();
const tpr = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
await Deno.stdout.write(encode("> "));
const [line, err] = await tpr.readLine();
if (err) {
console.error(red(`failed to read line from stdin: ${err}`));
break;
}
if (line === "close") {
break;
} else if (line === "ping") {
await sock.ping();
} else {
await sock.send(line);
}
// FIXME: Without this,
// sock.receive() won't resolved though it is readable...
await new Promise(
(resolve): void => {
setTimeout(resolve, 0);
}
);
}
await sock.close(1000);
// FIXME: conn.close() won't shutdown process...
Deno.exit(0);
```
## API

View file

@ -11,50 +11,45 @@ import { blue, green, red, yellow } from "../fmt/colors.ts";
const endpoint = Deno.args[1] || "ws://127.0.0.1:8080";
/** simple websocket cli */
async function main(): Promise<void> {
const sock = await connectWebSocket(endpoint);
console.log(green("ws connected! (type 'close' to quit)"));
(async function(): Promise<void> {
for await (const msg of sock.receive()) {
if (typeof msg === "string") {
console.log(yellow("< " + msg));
} else if (isWebSocketPingEvent(msg)) {
console.log(blue("< ping"));
} else if (isWebSocketPongEvent(msg)) {
console.log(blue("< pong"));
} else if (isWebSocketCloseEvent(msg)) {
console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`));
}
const sock = await connectWebSocket(endpoint);
console.log(green("ws connected! (type 'close' to quit)"));
(async function(): Promise<void> {
for await (const msg of sock.receive()) {
if (typeof msg === "string") {
console.log(yellow("< " + msg));
} else if (isWebSocketPingEvent(msg)) {
console.log(blue("< ping"));
} else if (isWebSocketPongEvent(msg)) {
console.log(blue("< pong"));
} else if (isWebSocketCloseEvent(msg)) {
console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`));
}
})();
const tpr = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
await Deno.stdout.write(encode("> "));
const [line, err] = await tpr.readLine();
if (err) {
console.error(red(`failed to read line from stdin: ${err}`));
break;
}
if (line === "close") {
break;
} else if (line === "ping") {
await sock.ping();
} else {
await sock.send(line);
}
// FIXME: Without this,
// sock.receive() won't resolved though it is readable...
await new Promise(
(resolve): void => {
setTimeout(resolve, 0);
}
);
}
await sock.close(1000);
// FIXME: conn.close() won't shutdown process...
Deno.exit(0);
}
})();
if (import.meta.main) {
main();
const tpr = new TextProtoReader(new BufReader(Deno.stdin));
while (true) {
await Deno.stdout.write(encode("> "));
const [line, err] = await tpr.readLine();
if (err) {
console.error(red(`failed to read line from stdin: ${err}`));
break;
}
if (line === "close") {
break;
} else if (line === "ping") {
await sock.ping();
} else {
await sock.send(line);
}
// FIXME: Without this,
// sock.receive() won't resolved though it is readable...
await new Promise(
(resolve): void => {
setTimeout(resolve, 0);
}
);
}
await sock.close(1000);
// FIXME: conn.close() won't shutdown process...
Deno.exit(0);

View file

@ -9,58 +9,52 @@ import {
/** websocket echo server */
const port = Deno.args[1] || "8080";
async function main(): Promise<void> {
console.log(`websocket server is running on :${port}`);
for await (const req of serve(`:${port}`)) {
const { headers, conn } = req;
acceptWebSocket({
conn,
headers,
bufReader: req.r,
bufWriter: req.w
})
.then(
async (sock: WebSocket): Promise<void> => {
console.log("socket connected!");
const it = sock.receive();
while (true) {
try {
const { done, value } = await it.next();
if (done) {
break;
}
const ev = value;
if (typeof ev === "string") {
// text message
console.log("ws:Text", ev);
await sock.send(ev);
} else if (ev instanceof Uint8Array) {
// binary message
console.log("ws:Binary", ev);
} else if (isWebSocketPingEvent(ev)) {
const [, body] = ev;
// ping
console.log("ws:Ping", body);
} else if (isWebSocketCloseEvent(ev)) {
// close
const { code, reason } = ev;
console.log("ws:Close", code, reason);
}
} catch (e) {
console.error(`failed to receive frame: ${e}`);
await sock.close(1000).catch(console.error);
console.log(`websocket server is running on :${port}`);
for await (const req of serve(`:${port}`)) {
const { headers, conn } = req;
acceptWebSocket({
conn,
headers,
bufReader: req.r,
bufWriter: req.w
})
.then(
async (sock: WebSocket): Promise<void> => {
console.log("socket connected!");
const it = sock.receive();
while (true) {
try {
const { done, value } = await it.next();
if (done) {
break;
}
const ev = value;
if (typeof ev === "string") {
// text message
console.log("ws:Text", ev);
await sock.send(ev);
} else if (ev instanceof Uint8Array) {
// binary message
console.log("ws:Binary", ev);
} else if (isWebSocketPingEvent(ev)) {
const [, body] = ev;
// ping
console.log("ws:Ping", body);
} else if (isWebSocketCloseEvent(ev)) {
// close
const { code, reason } = ev;
console.log("ws:Close", code, reason);
}
} catch (e) {
console.error(`failed to receive frame: ${e}`);
await sock.close(1000).catch(console.error);
}
}
)
.catch(
(err: Error): void => {
console.error(`failed to accept websocket: ${err}`);
}
);
}
}
if (import.meta.main) {
main();
}
)
.catch(
(err: Error): void => {
console.error(`failed to accept websocket: ${err}`);
}
);
}

View file

@ -5,13 +5,6 @@ const addr = Deno.args[1] || "127.0.0.1:4500";
const originAddr = Deno.args[2] || "127.0.0.1:4501";
const server = serve(addr);
async function main(): Promise<void> {
console.log(`Proxy listening on http://${addr}/`);
for await (const req of server) {
proxyRequest(req);
}
}
async function proxyRequest(req: ServerRequest): Promise<void> {
const url = `http://${originAddr}${req.url}`;
const resp = await fetch(url, {
@ -21,4 +14,7 @@ async function proxyRequest(req: ServerRequest): Promise<void> {
req.respond(resp);
}
main();
console.log(`Proxy listening on http://${addr}/`);
for await (const req of server) {
proxyRequest(req);
}