Skip to content

Instantly share code, notes, and snippets.

@benishouga
Last active May 27, 2020 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benishouga/007d31f8b8be2a5df0df5ec7ab63d756 to your computer and use it in GitHub Desktop.
Save benishouga/007d31f8b8be2a5df0df5ec7ab63d756 to your computer and use it in GitHub Desktop.
社内のもくもく会で Deno を Getting Started 見ながら少し触ってみた

deno 使ってみる会

公式の Getting Started にならって試していきます。

https://deno.land/manual/getting_started

インストール

$ curl -fsSL https://deno.land/x/install/install.sh | sh

Archive:  /home/xxxxx/.deno/bin/deno.zip
  inflating: deno
Deno was installed successfully to /home/xxxxx/.deno/bin/deno
Manually add the directory to your $HOME/.bash_profile (or similar)
  export DENO_INSTALL="/home/xxxxx/.deno"
  export PATH="$DENO_INSTALL/bin:$PATH"
Run '/home/xxxxx/.deno/bin/deno --help' to get started

コメントに従って .bash_profile なり .bashrc に環境変数を追加

(xxxxx は ユーザー名)

export DENO_INSTALL="/home/xxxxx/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"

インストールできているか確認

$ deno --version
deno 1.0.2
v8 8.4.300
typescript 3.9.2

(公式だと)

IDE サポート

公式で VSCode 用が提供されていた。
https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno

JetBrains IDEs や Vim and NeoVim 用も一応あるっぽい。

(入れてはみたけど、どのようなサポートされているのかはいまいちわかっていない)

Deno の標準ライブラリをサポートするようにする

以下のように型定義を出力

$ deno types > ./deno.d.ts

以下のような tsconfig.json を作成し、読み込むようにすれば deno の標準ライブラリは型サポートされるようになった。

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "deno": ["./deno.d.ts"]
    }
  }
}

TODO: http://deno.land/x/xxxxx とかで読み込んだライブラリの型サポートの仕方調べる

コードを実行してみる

ただ走らせる

hello_deno.ts を作る

console.log("Welcome to Deno 🦕");
$ deno run hello_deno.ts
Compile file:///home/xxxxx/work/deno/first.ts
Welcome to Deno 🦕

ちゃんと走る様子。

fetch はそのまま使える様子

hello_fetch.ts を作る

const url = Deno.args[0];
const res = await fetch(url);

const body = new Uint8Array(await res.arrayBuffer());
await Deno.stdout.write(body);

fetch を動かすためには --alow-net フラグが必要

$ deno run --allow-net=example.com hello_fetch.ts https://example.com
<!doctype html>
<html>・・・・・</html>

つけないと以下のように permission エラーになる

$ deno run hello_fetch.ts https://example.com
error: Uncaught PermissionDenied: network access to "https://example.com/", run again with the --allow-net flag
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendAsync ($deno$/ops/dispatch_json.ts:98:10)
    at async fetch ($deno$/web/fetch.ts:591:27)
    at async file:///home/xxxxx/work/deno/hello_fetch.ts:2:13

ファイルを読み込んで見る

hello_file.ts を作る

const filenames = Deno.args;
for (const filename of filenames) {
  const file = await Deno.open(filename);
  await Deno.copy(file, Deno.stdout);
  file.close();
}
$ deno run --allow-read hello_file.ts ./deno.md

...略...

総評

  • まだよくわからない
  • Permission の引数はセキュアかもしれないけど、ものすごくめんどい。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment