mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
Use top-level for-await in various places (#3217)
This commit is contained in:
parent
71efe6f2c5
commit
f484776384
9 changed files with 94 additions and 125 deletions
|
@ -2,10 +2,7 @@
|
||||||
|
|
||||||
import { evaluate, instantiate, load } from "./utils.ts";
|
import { evaluate, instantiate, load } from "./utils.ts";
|
||||||
|
|
||||||
async function main(args: string[]): Promise<void> {
|
const args = Deno.args;
|
||||||
const text = await load(args);
|
const text = await load(args);
|
||||||
const result = evaluate(text);
|
const result = evaluate(text);
|
||||||
instantiate(...result);
|
instantiate(...result);
|
||||||
}
|
|
||||||
|
|
||||||
main(Deno.args);
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
const url = Deno.args[1];
|
const url_ = Deno.args[1];
|
||||||
const res = await fetch(url);
|
const res = await fetch(url_);
|
||||||
await Deno.copy(Deno.stdout, res.body);
|
await Deno.copy(Deno.stdout, res.body);
|
||||||
|
|
|
@ -3,7 +3,6 @@ const hostname = "0.0.0.0";
|
||||||
const port = 8080;
|
const port = 8080;
|
||||||
const listener = Deno.listen({ hostname, port });
|
const listener = Deno.listen({ hostname, port });
|
||||||
console.log(`Listening on ${hostname}:${port}`);
|
console.log(`Listening on ${hostname}:${port}`);
|
||||||
while (true) {
|
for await (const conn of listener) {
|
||||||
const conn = await listener.accept();
|
|
||||||
Deno.copy(conn, conn);
|
Deno.copy(conn, conn);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
#!/usr/bin/env -S deno --allow-net --allow-env
|
#!/usr/bin/env -S deno --allow-net --allow-env
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// 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";
|
import { parse } from "https://deno.land/std/flags/mod.ts";
|
||||||
|
|
||||||
function pathBase(p: string): string {
|
function pathBase(p: string): string {
|
||||||
|
@ -9,28 +7,27 @@ function pathBase(p: string): string {
|
||||||
return parts[parts.length - 1];
|
return parts[parts.length - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
const token = Deno.env()["GIST_TOKEN"];
|
||||||
const token = env()["GIST_TOKEN"];
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
console.error("GIST_TOKEN environmental variable not set.");
|
console.error("GIST_TOKEN environmental variable not set.");
|
||||||
console.error("Get a token here: https://github.com/settings/tokens");
|
console.error("Get a token here: https://github.com/settings/tokens");
|
||||||
exit(1);
|
Deno.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsedArgs = parse(args.slice(1));
|
const parsedArgs = parse(Deno.args.slice(1));
|
||||||
|
|
||||||
if (parsedArgs._.length === 0) {
|
if (parsedArgs._.length === 0) {
|
||||||
console.error(
|
console.error(
|
||||||
"Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " +
|
"Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file " +
|
||||||
"[next_file]"
|
"[next_file]"
|
||||||
);
|
);
|
||||||
exit(1);
|
Deno.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const files = {};
|
const files = {};
|
||||||
for (const filename of parsedArgs._) {
|
for (const filename of parsedArgs._) {
|
||||||
const base = pathBase(filename);
|
const base = pathBase(filename);
|
||||||
const content = await readFile(filename);
|
const content = await Deno.readFile(filename);
|
||||||
const contentStr = new TextDecoder().decode(content);
|
const contentStr = new TextDecoder().decode(content);
|
||||||
files[base] = { content: contentStr };
|
files[base] = { content: contentStr };
|
||||||
}
|
}
|
||||||
|
@ -60,6 +57,3 @@ async function main(): Promise<void> {
|
||||||
const err = await res.text();
|
const err = await res.text();
|
||||||
console.error("Failure to POST", err);
|
console.error("Failure to POST", err);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
|
|
|
@ -5,11 +5,7 @@ const addr = Deno.args[1] || "127.0.0.1:4500";
|
||||||
const server = serve(addr);
|
const server = serve(addr);
|
||||||
const body = new TextEncoder().encode("Hello World");
|
const body = new TextEncoder().encode("Hello World");
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
|
||||||
console.log(`http://${addr}/`);
|
console.log(`http://${addr}/`);
|
||||||
for await (const req of server) {
|
for await (const req of server) {
|
||||||
req.respond({ body });
|
req.respond({ body });
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
|
|
|
@ -19,7 +19,8 @@ async function largeRespond(request: ServerRequest, c: string): Promise<void> {
|
||||||
await request.respond({ status: 200, body: b });
|
await request.respond({ status: 200, body: b });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
console.log("Racing server listening...\n");
|
||||||
|
|
||||||
let step = 1;
|
let step = 1;
|
||||||
for await (const request of server) {
|
for await (const request of server) {
|
||||||
switch (step) {
|
switch (step) {
|
||||||
|
@ -43,8 +44,3 @@ async function main(): Promise<void> {
|
||||||
}
|
}
|
||||||
step++;
|
step++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
|
|
||||||
console.log("Racing server listening...\n");
|
|
||||||
|
|
|
@ -312,8 +312,7 @@ and returns to the client anything it sends.
|
||||||
```ts
|
```ts
|
||||||
const listener = Deno.listen({ port: 8080 });
|
const listener = Deno.listen({ port: 8080 });
|
||||||
console.log("listening on 0.0.0.0:8080");
|
console.log("listening on 0.0.0.0:8080");
|
||||||
while (true) {
|
for await (const conn of listener) {
|
||||||
const conn = await listener.accept();
|
|
||||||
Deno.copy(conn, conn);
|
Deno.copy(conn, conn);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -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.
|
program, at a later stage, needs those permissions, it will fail.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const { permissions, open, remove } = Deno;
|
|
||||||
|
|
||||||
// lookup a permission
|
// lookup a permission
|
||||||
const status = await permissions.query({ name: "write" });
|
const status = await Deno.permissions.query({ name: "write" });
|
||||||
if (status.state !== "granted") {
|
if (status.state !== "granted") {
|
||||||
throw new Error("need write permission");
|
throw new Error("need write permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
const log = await open("request.log", "a+");
|
const log = await Deno.open("request.log", "a+");
|
||||||
|
|
||||||
// revoke some permissions
|
// revoke some permissions
|
||||||
await permissions.revoke({ name: "read" });
|
await Deno.permissions.revoke({ name: "read" });
|
||||||
await permissions.revoke({ name: "write" });
|
await Deno.permissions.revoke({ name: "write" });
|
||||||
|
|
||||||
// use the log file
|
// use the log file
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
await log.write(encoder.encode("hello\n"));
|
await log.write(encoder.encode("hello\n"));
|
||||||
|
|
||||||
// this will fail.
|
// this will fail.
|
||||||
await remove("request.log");
|
await Deno.remove("request.log");
|
||||||
```
|
```
|
||||||
|
|
||||||
### File server
|
### File server
|
||||||
|
|
|
@ -24,12 +24,7 @@ async function handle(conn: Deno.Conn): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
|
||||||
console.log("Listening on", addr);
|
console.log("Listening on", addr);
|
||||||
while (true) {
|
for await (const conn of listener) {
|
||||||
const conn = await listener.accept();
|
|
||||||
handle(conn);
|
handle(conn);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
|
|
|
@ -24,12 +24,7 @@ async function handle(conn: Deno.Conn): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
|
||||||
console.log(`Proxy listening on http://${addr}/`);
|
console.log(`Proxy listening on http://${addr}/`);
|
||||||
while (true) {
|
for await (const conn of listener) {
|
||||||
const conn = await listener.accept();
|
|
||||||
handle(conn);
|
handle(conn);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue