UGA Boxxx

つぶやきの延長のつもりで、知ったこと思ったこと書いてます

【Deno】v1.0が正式リリースされたので少し勉強してみる

DenoというNodeに代わるJavaScriptとTypeScriptのランタイム(v1.0)が5/13にリリースされたらしい

動向などまったく追ってなかったが節目に出会えたのでどういうものなのかブログを読んでみて触ってみる

deno.land

ブログから抜粋した特徴

  • Denoは、JavaScriptとTypeScriptをWebブラウザーの外部で実行するための新しいランタイム
  • Webブラウザーのように、外部コードをフェッチする方法がある
  • スクリプトは、ハードドライブにアクセスしたり、ネットワーク接続を開いたり、その他の潜在的に悪意のある操作を許可なしに行うことはない
  • 標準化されたブラウザーJavaScript APIから逸脱しない(ように注意している)
  • 追加のツールなしでTypeScriptをサポート
  • 非同期処理のバックプレッシャーに関する問題をRustのfuture(Rust 1.36から導入された、非同期処理を抽象化したクラス)ベースのAPIJavaScriptのpromiseに簡単にバインドできるようすることで解決している
    ※ここはちょっとよくわからないので後日調べる
  • Node(npm)パッケージと互換性はない

試してみる

インストール

brew install deno

用意されたサンプル

$ deno run https://deno.land/std/examples/welcome.ts
Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using master branch https://deno.land/std/examples/welcome.ts
Compile https://deno.land/std/examples/welcome.ts
Welcome to Deno 🦕

Welcome to Deno 🦕が表示された

Hello World

hello.tsファイルに以下を記述

console.log('Hello, World')
$ deno run hello.ts
Compile file:///Users/uga/playground/deno-example/hello.ts
Hello, World

サーバー起動

server.tsファイルに以下を記述

import { serve } from "https://deno.land/std@0.50.0/http/server.ts";
for await (const req of serve({ port: 8080 })) {
  req.respond({ body: "Hello World\n" });
}
$ deno run --allow-net server.ts
Compile file:///Users/uga/playground/deno-example/hello_http.ts
Download https://deno.land/std@0.50.0/http/server.ts
Download https://deno.land/std@0.50.0/encoding/utf8.ts
Download https://deno.land/std@0.50.0/io/bufio.ts
Download https://deno.land/std@0.50.0/testing/asserts.ts
Download https://deno.land/std@0.50.0/async/mod.ts
Download https://deno.land/std@0.50.0/http/_io.ts
Download https://deno.land/std@0.50.0/io/util.ts
Download https://deno.land/std@0.50.0/path/mod.ts
Download https://deno.land/std@0.50.0/path/win32.ts
Download https://deno.land/std@0.50.0/path/posix.ts
Download https://deno.land/std@0.50.0/path/common.ts
Download https://deno.land/std@0.50.0/path/separator.ts
Download https://deno.land/std@0.50.0/path/interface.ts
Download https://deno.land/std@0.50.0/path/glob.ts
Download https://deno.land/std@0.50.0/path/_constants.ts
Download https://deno.land/std@0.50.0/path/_util.ts
Download https://deno.land/std@0.50.0/fmt/colors.ts
Download https://deno.land/std@0.50.0/testing/diff.ts
Download https://deno.land/std@0.50.0/path/_globrex.ts
Download https://deno.land/std@0.50.0/async/deferred.ts
Download https://deno.land/std@0.50.0/async/delay.ts
Download https://deno.land/std@0.50.0/async/mux_async_iterator.ts
Download https://deno.land/std@0.50.0/textproto/mod.ts
Download https://deno.land/std@0.50.0/http/http_status.ts
Download https://deno.land/std@0.50.0/bytes/mod.ts

localhost:8080Hello Worldが表示された

おわり

進化中とのことで、乗り換えを考えたりするのはまだ時期相応っぽい
また、より理解するためにはRustの知識も必要みたいなのでRustも触ってみたい

他参考

https://qiita.com/so99ynoodles/items/c3ba2a528052827e3b3c