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
or:
deno -h
for short.
We can also write:
deno --help
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);
Then when we run:
deno run index.ts a b -c --quiet
We get:
[ "a", "b", "-c", "--quiet" ]
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);
We run it with:
deno run --allow-net index.ts https://yesno.wtf/api
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));
Then when we run:
deno run --unstable index.ts
We see:
PermissionStatus { state: "prompt" }
PermissionStatus { state: "prompt" }
PermissionStatus { state: "prompt" }
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);
Then we get:
PermissionStatus { state: "prompt" }
displayed.
Conclusion
We can get and set permissions with Deno.