Deh, Deh...Deno

A secure runtime for JavaScript and TypeScript.

What is Deno?

  • It's like NodeJs
  • Deeply improved in many ways
  • Was announced in Jun 6, 2018 By Ryan Dahl as JSConf EU

Probably every talk about Deno begins like this

deno run https://deno.land/std/examples/welcome.ts
deno repl
Deno 1.5.3
exit using ctrl+d or close()
>

REPL ( read–eval–print loop)

Static file

Why start this project?

  • No Promise API
  • Security/Sandbox
  • Build system (GYP)
  • NPM (bundled in node & package.json)
  • node_modules (not isomorphic, algorithm resolution, etc)
  • require() without the extension
  • ....
  • Type script

Accorning to Ryan Dahl

...go and see the video

Let's talk about features

  • Also, build on top of V8
  • Modern features of javascript
  • Has an extensive standard library
  • Typescript at its core (no need for tsc, ts-node, etc.)
  • Default ES modules (no esm, not experimental flags, no babel...)
  • No Package Manager (no NPM)
    • Wait! What?!?!?
  • First-class await (frigging finally...)
  • Built-in testing capabilities
  • Isomorphic features - aims to be browser-compatible (built-in fetch, window)
  • Integrated tooling
  • More to come
const raw = await fetch('https://jsonplaceholder.typicode.com/users');
const asJson = await raw.json();
console.info(asJson);

Check this...

Is something strange? Missing?

  1. Node API was initially callback-based
  2. Then we got Promises
  3. Then we got promisify()
  4. Then we got Async await
  5. Now we have first class await

Kewl...? so why switch?

  • Check the video
  • NPM is not secured
  • Typescript is great
  • We did some early mistakes (callback)

Node killer?

  • No
  • They work together - influence each other
  • Node is here to stay - it's huge

Similar

  • V8 Chromium 
  • A runtime environment for javascript
  • Written in C++ and Javascript
  • Official package manager (NPM)
  • Used CommonJS - require()
  • No Sandboxing
  • No single executable

Different

  • Written in Rust and Typescript
  • No package manager
  • ES6 module support
  • Sandboxing as a default
  • A single executable 

No NPM

  • Rely on GitHub scripts
  • The good
    • Very flexible
    • No need to publish
  • The bad
    • Might not be reliable 
    • To fix this you can use https://deno.land/x

Lets demo this

No centralized package manager 

Tools

deno --help
deno 1.5.3
A secure JavaScript and TypeScript runtime

Docs: https://deno.land/manual
Modules: https://deno.land/std/ https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues

SUBCOMMANDS:
    bundle         Bundle module and dependencies into single file
    cache          Cache the dependencies
    completions    Generate shell completions
    doc            Show documentation for a module
    eval           Eval script
    fmt            Format source files
    help           Prints this message or the help of the given subcommand(s)
    info           Show info about cache or info related to source file
    install        Install script as an executable
    lint           Lint source files
    repl           Read Eval Print Loop
    run            Run a program given a filename or url to the module. Use '-' as a filename to read from stdin.
    test           Run tests
    types          Print runtime TypeScript declarations
    upgrade        Upgrade deno executable to given version

fmt

test

bundle

deno server

import { serve } from 'https://deno.land/std@0.80.0/http/server.ts';

const HTTP_OK = 200;
const DEFAULT_PORT = 8080;

const server = serve({ hostname: '0.0.0.0', port: DEFAULT_PORT });
console.log(`HTTP webserver running.  Access it at:  http://localhost:8080/`);

for await (const request of server) {
  let bodyContent = 'Your user-agent is:\n\n';
  bodyContent += request.headers.get('user-agent') || 'Unknown';
  bodyContent += `\nroute: ${request.url}`;
  const body = Array.from(request.headers.keys()).reduce((acc, value) => {
    return `${acc}\n${value}: ${request.headers.get(value)}`;
  }, bodyContent);
  request.respond({ status: HTTP_OK, body });
}

vscode extention

Native modules

Popular servers

Github

Documentation

Performance

The end

...or is it?

Deh, Deh...Deno

By Eyal Mrejen

Deh, Deh...Deno

  • 203