DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

Deno — Command-Line and Permissions

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

Deno is a new server-side runtime environment for running JavaScript and TypeScript apps.

In this article, we’ll take a look at how to get started with developing apps for Deno.

Command-Line Interface

We can get help by running:

deno help
Enter fullscreen mode Exit fullscreen mode

or:

deno -h
Enter fullscreen mode Exit fullscreen mode

for short.

We can also write:

deno --help
Enter fullscreen mode Exit fullscreen mode

to do the same thing.

Script Arguments

We can get command-line arguments in our script with the Deno.args property.

For example, we can write:

index.ts

console.log(Deno.args);
Enter fullscreen mode Exit fullscreen mode

Then when we run:

deno run index.ts a b -c --quiet
Enter fullscreen mode Exit fullscreen mode

We get:

[ "a", "b", "-c", "--quiet" ]
Enter fullscreen mode Exit fullscreen mode

from the console output.

Permissions

Permission is required for running code that requires access to various resources.

They have to enabled specifically by default.

The following permissions available:

  • -A, — allow-all — Allow all permissions. This disables all security.
  •  — allow-env — Allow environment access for things like getting and setting of environment variables.
  •  — allow-hrtime — Allow high-resolution time measurement. High-resolution time can be used in timing attacks and fingerprinting.
  •  — allow-net=<url> — Allow network access. We can specify an optional, comma-separated list of domains to provide an allow-list of allowed domains.
  •  — allow-plugin — Allow loading plugins. Please note that — allow-plugin is an unstable feature.
  •  — allow-read=<allow-read> — Allow file system read access. We can specify an optional, comma-separated list of directories or files to provide an allow-list of allowed file system access.
  •  — allow-run — Allow running subprocesses. Be aware that subprocesses are not run in a sandbox and therefore do not have the same security restrictions as the Deno process. Therefore, use with caution.
  •  — allow-write=<allow-write> — Allow file system write access. You can specify an optional, comma-separated list of directories or files to provide an allow-list of allowed file system access.

For example, if we have a program that makes HTTP requests like:

const [url] = Deno.args;
const res = await fetch(url);
const body = new Uint8Array(await res.arrayBuffer());
await Deno.stdout.write(body);
Enter fullscreen mode Exit fullscreen mode

We run it with:

deno run --allow-net index.ts https://yesno.wtf/api
Enter fullscreen mode Exit fullscreen mode

to let the program access the Internet.

Permission APIs

We can query form permissions by using the Deno.permissions.query method with an object that has the permissions we want to query.

For example, we can write:

index.ts

const desc1 = { name: "read", path: "/foo" } as const;
console.log(await Deno.permissions.query(desc1));

const desc2 = { name: "read", path: "/foo/bar" } as const;
console.log(await Deno.permissions.query(desc2));

const desc3 = { name: "read", path: "/bar" } as const;
console.log(await Deno.permissions.query(desc3));
Enter fullscreen mode Exit fullscreen mode

Then when we run:

deno run --unstable index.ts
Enter fullscreen mode Exit fullscreen mode

We see:

PermissionStatus { state: "prompt" }
PermissionStatus { state: "prompt" }
PermissionStatus { state: "prompt" }
Enter fullscreen mode Exit fullscreen mode

logged in the console.

We need the --unstable option to make the Deno.permissions.query method available.

We can revoke permissions by using the Deno.permissions.revoke method:

const desc = { name: "read", path: "/foo/bar" } as const;
console.log(await Deno.permissions.revoke(desc));

const strongDesc = { name: "read", path: "/foo" } as const;
await Deno.permissions.revoke(strongDesc);
Enter fullscreen mode Exit fullscreen mode

Then we get:

PermissionStatus { state: "prompt" }
Enter fullscreen mode Exit fullscreen mode

displayed.

Conclusion

We can get and set permissions with Deno.

Top comments (1)

Collapse
 
zdev1official profile image
ZDev1Official

Nice