Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use for-await in examples and manual #3217

Merged
merged 3 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions std/bundle/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import { evaluate, instantiate, load } from "./utils.ts";

async function main(args: string[]): Promise<void> {
const text = await load(args);
const result = evaluate(text);
instantiate(...result);
}

main(Deno.args);
const args = Deno.args;
const text = await load(args);
const result = evaluate(text);
instantiate(...result);
4 changes: 2 additions & 2 deletions std/examples/curl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
const url = Deno.args[1];
const res = await fetch(url);
const url_ = Deno.args[1];
const res = await fetch(url_);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. This is currently an error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is to say, with url it didn't compile. #2888

This was one file I didn't actually verify (thought it was a sure thing).

await Deno.copy(Deno.stdout, res.body);
3 changes: 1 addition & 2 deletions std/examples/echo_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const hostname = "0.0.0.0";
const port = 8080;
const listener = Deno.listen({ hostname, port });
console.log(`Listening on ${hostname}:${port}`);
while (true) {
const conn = await listener.accept();
for await (const conn of listener) {
Deno.copy(conn, conn);
}
96 changes: 45 additions & 51 deletions std/examples/gist.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,59 @@
#!/usr/bin/env -S deno --allow-net --allow-env
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

const { args, env, exit, readFile } = Deno;
import { parse } from "https://deno.land/std/flags/mod.ts";

function pathBase(p: string): string {
const parts = p.split("/");
return parts[parts.length - 1];
}

async function main(): Promise<void> {
const token = env()["GIST_TOKEN"];
if (!token) {
console.error("GIST_TOKEN environmental variable not set.");
console.error("Get a token here: https://github.com/settings/tokens");
exit(1);
}

const parsedArgs = parse(args.slice(1));

if (parsedArgs._.length === 0) {
console.error(
"Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " +
"[next_file]"
);
exit(1);
}

const files = {};
for (const filename of parsedArgs._) {
const base = pathBase(filename);
const content = await readFile(filename);
const contentStr = new TextDecoder().decode(content);
files[base] = { content: contentStr };
}
const token = Deno.env()["GIST_TOKEN"];
if (!token) {
console.error("GIST_TOKEN environmental variable not set.");
console.error("Get a token here: https://github.com/settings/tokens");
Deno.exit(1);
}

const content = {
description: parsedArgs.title || parsedArgs.t || "Example",
public: false,
files: files
};
const body = JSON.stringify(content);
const parsedArgs = parse(Deno.args.slice(1));

const res = await fetch("https://api.github.com/gists", {
method: "POST",
headers: [
["Content-Type", "application/json"],
["User-Agent", "Deno-Gist"],
["Authorization", `token ${token}`]
],
body
});
if (parsedArgs._.length === 0) {
console.error(
"Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " +
"[next_file]"
);
Deno.exit(1);
}

if (res.ok) {
const resObj = await res.json();
console.log("Success");
console.log(resObj["html_url"]);
} else {
const err = await res.text();
console.error("Failure to POST", err);
}
const files = {};
for (const filename of parsedArgs._) {
const base = pathBase(filename);
const content = await Deno.readFile(filename);
const contentStr = new TextDecoder().decode(content);
files[base] = { content: contentStr };
}

main();
const content = {
description: parsedArgs.title || parsedArgs.t || "Example",
public: false,
files: files
};
const body = JSON.stringify(content);

const res = await fetch("https://api.github.com/gists", {
method: "POST",
headers: [
["Content-Type", "application/json"],
["User-Agent", "Deno-Gist"],
["Authorization", `token ${token}`]
],
body
});

if (res.ok) {
const resObj = await res.json();
console.log("Success");
console.log(resObj["html_url"]);
} else {
const err = await res.text();
console.error("Failure to POST", err);
}
10 changes: 3 additions & 7 deletions std/http/http_bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ const addr = Deno.args[1] || "127.0.0.1:4500";
const server = serve(addr);
const body = new TextEncoder().encode("Hello World");

async function main(): Promise<void> {
console.log(`http://${addr}/`);
for await (const req of server) {
req.respond({ body });
}
console.log(`http://${addr}/`);
for await (const req of server) {
req.respond({ body });
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer.


main();
50 changes: 23 additions & 27 deletions std/http/racing_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,28 @@ async function largeRespond(request: ServerRequest, c: string): Promise<void> {
await request.respond({ status: 200, body: b });
}

async function main(): Promise<void> {
let step = 1;
for await (const request of server) {
switch (step) {
case 1:
// Try to wait long enough.
// For pipelining, this should cause all the following response
// to block.
delayedRespond(request);
break;
case 2:
// HUGE body.
largeRespond(request, "a");
break;
case 3:
// HUGE body.
largeRespond(request, "b");
break;
default:
request.respond({ status: 200, body: body4 });
break;
}
step++;
console.log("Racing server listening...\n");

let step = 1;
for await (const request of server) {
switch (step) {
case 1:
// Try to wait long enough.
// For pipelining, this should cause all the following response
// to block.
delayedRespond(request);
break;
case 2:
// HUGE body.
largeRespond(request, "a");
break;
case 3:
// HUGE body.
largeRespond(request, "b");
break;
default:
request.respond({ status: 200, body: body4 });
break;
}
step++;
}

main();

console.log("Racing server listening...\n");
15 changes: 6 additions & 9 deletions std/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ and returns to the client anything it sends.
```ts
const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
while (true) {
const conn = await listener.accept();
for await (const conn of listener) {
Deno.copy(conn, conn);
}
```
Expand Down Expand Up @@ -353,26 +352,24 @@ Sometimes a program may want to revoke previously granted permissions. When a
program, at a later stage, needs those permissions, it will fail.

```ts
const { permissions, open, remove } = Deno;

// lookup a permission
const status = await permissions.query({ name: "write" });
const status = await Deno.permissions.query({ name: "write" });
if (status.state !== "granted") {
throw new Error("need write permission");
}

const log = await open("request.log", "a+");
const log = await Deno.open("request.log", "a+");

// revoke some permissions
await permissions.revoke({ name: "read" });
await permissions.revoke({ name: "write" });
await Deno.permissions.revoke({ name: "read" });
await Deno.permissions.revoke({ name: "write" });

// use the log file
const encoder = new TextEncoder();
await log.write(encoder.encode("hello\n"));

// this will fail.
await remove("request.log");
await Deno.remove("request.log");
```

### File server
Expand Down
11 changes: 3 additions & 8 deletions tools/deno_tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ async function handle(conn: Deno.Conn): Promise<void> {
}
}

async function main(): Promise<void> {
console.log("Listening on", addr);
while (true) {
const conn = await listener.accept();
handle(conn);
}
console.log("Listening on", addr);
for await (const conn of listener) {
handle(conn);
}

main();
11 changes: 3 additions & 8 deletions tools/deno_tcp_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ async function handle(conn: Deno.Conn): Promise<void> {
}
}

async function main(): Promise<void> {
console.log(`Proxy listening on http://${addr}/`);
while (true) {
const conn = await listener.accept();
handle(conn);
}
console.log(`Proxy listening on http://${addr}/`);
for await (const conn of listener) {
handle(conn);
}

main();