1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-09 07:39:15 -05:00

Fix some examples in the manual (#3084)

This commit is contained in:
Ryan Dahl 2019-10-07 18:57:44 -04:00 committed by GitHub
parent a4b27db21a
commit 3882c9d19a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -270,14 +270,12 @@ In this program each command-line argument is assumed to be a filename, the file
is opened, and printed to stdout.
```ts
(async () => {
for (let i = 1; i < Deno.args.length; i++) {
let filename = Deno.args[i];
let file = await Deno.open(filename);
await Deno.copy(Deno.stdout, file);
file.close();
}
})();
```
The `copy()` function here actually makes no more than the necessary kernel ->
@ -297,17 +295,12 @@ This is an example of a simple server which accepts connections on port 8080,
and returns to the client anything it sends.
```ts
const { listen, copy } = Deno;
(async () => {
const addr = "0.0.0.0:8080";
const listener = listen("tcp", addr);
console.log("listening on", addr);
const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
while (true) {
const conn = await listener.accept();
copy(conn, conn);
Deno.copy(conn, conn);
}
})();
```
When this program is started, the user is prompted for permission to listen on
@ -346,7 +339,6 @@ presented to the user.
```ts
const { permissions, revokePermission, open, remove } = Deno;
(async () => {
// lookup a permission
if (!permissions().write) {
throw new Error("need write permission");
@ -364,7 +356,6 @@ const { permissions, revokePermission, open, remove } = Deno;
// this will prompt for the write permission or fail.
await remove("request.log");
})();
```
### File server
@ -412,9 +403,7 @@ $ deno --allow-read=/etc https://deno.land/std/examples/cat.ts /etc/passwd
This is an example to restrict host.
```ts
(async () => {
const result = await fetch("https://deno.land/std/examples/echo_server.ts");
})();
```
```shell
@ -428,7 +417,6 @@ $ deno --allow-net=deno.land allow-net-whitelist-example.ts
Example:
```ts
window.onload = async function() {
// create subprocess
const p = Deno.run({
args: ["echo", "hello"]
@ -436,7 +424,6 @@ window.onload = async function() {
// await its completion
await p.status();
};
```
Run it:
@ -456,9 +443,6 @@ By default when you use `Deno.run()` subprocess inherits `stdin`, `stdout` and
you can use `"piped"` option.
```ts
window.onload = async function() {
const decoder = new TextDecoder();
const fileNames = Deno.args.slice(1);
const p = Deno.run({
@ -480,12 +464,11 @@ window.onload = async function() {
await Deno.stdout.write(rawOutput);
} else {
const rawError = await p.stderrOutput();
const errorString = decoder.decode(rawError);
const errorString = new TextDecoder().decode(rawError);
console.log(errorString);
}
Deno.exit(code);
};
```
When you run it: