Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Deno

 Deno

A brief introduction for Deno

Mosta

July 14, 2022
Tweet

Other Decks in Programming

Transcript

  1. Den

  2. What is deno? Deno is a runtime for javascript/typescript, created

    by Ryan dahl ( creator of node.js ). It’s not that different from node, it tries to solve many aspects that weren’t considered when node was created.
  3. WHY DENO? • Supports Typescript. • Deno is a single

    executable. • Uses URLs to import modules. • Has a standard library, that is updated/audited/reviewed. • Secure by default, certain actions can’t be executed unless explicitly enabled. • Has many integrated tools like inspector, formatter, bundler, test tools and document generator. • No package.json and dependency hell. • Use Browser standards as much as possible.
  4. Your first deno script You can create you .ts file

    and write console.log(‘Hello world!’), and then type deno run myfile.ts. The expected result should be the print of your string, you could also run code via HTTP, try deno run https://deno.land/[email protected]/examples/welcome.ts The result should be “Welcome to Deno!”
  5. Built-in tools As we said before Deno comes with many

    integrated tools, we don’t need to install linters, webpack, test libs or else. Bundler: Create a single js file given the module as input - deno bundle Dependency inspector: Allow us to inspect module dependencies - deno info Test runner: Utility to help us testing our code - deno test Also linter, code formatter and doc generator.
  6. MODULE IMPORT Deno doesn’t have package.json file, importing modules could

    be done via HTTP. If version number is not specified the latest will be used. P.S. You can also load modules from private repos.
  7. Module IMport good practices Importing everytime the same module in

    different files, isn’t a smart thing to do, a good practice is to collect all the project dependencies into a deps.ts or dev_deps.ts file, resulting in better management.
  8. Can imports get better? We could import all deps into

    deps.ts, but then we will see something like this when imported import { crypto } from "../../../deps.ts"; We don’t know where that crypto is coming from, and you have to jump back and forth every time to know where the heck is coming from. And what if i want to import something that isn’t from Deno but Node?
  9. IMPORT MAPS TO THE RESCUE Import maps are a new

    web platform standard ( Remember we said that Deno use Browser standards as much as possible ), which allow us to do import specifiers like this import { crypto } from 'crypto'; And configuration is done inside import_maps.json With import maps, you don’t need a package management tool, and you could use it also to adapt Node code to work with Deno. P.S. You need to pass import maps file to deno, deno run –import-map=import_map.json file.ts
  10. IMPORT MAPS OTHER BENEFITS Imagine having a big library/module being

    used only in a small part of your application, most of bundlers will group everything in one big file, so the user is loading this code even if it’s NOT USED. Now you could lazy load that code ( without a build tool such as webpack, etc. ), and it’s done like this.
  11. Security You should already notice that we aren’t allowed to

    execute our script with http request, that’s because in order to take certain actions, we should explicitly tell Deno about them, in this case we are trying to access the web, which require –allow-net permission. So let’s try deno run –allow-net main.ts, you will notice that were successful in getting our response from google. P.S. Permissions should be added before the file name
  12. Why Security matters You might install/use a package where it

    will run destructive commands, for example: rm -rf / Deno prevents any sort of unwanted and malicious actions by using permissions. Without giving the permission –allow-run=rm -rf, the malicious code will not be executed. Some Deno permissions are configurable, to give access to certain files, servers, etc.
  13. DENO PROJECT SCAFFOLDING When creating a Deno project, you don’t

    really have a structure, or configuration files. But there are some good practices to follow, for example you might want to group all you code inside src/ folder ( or the lib name ), create a folder for each new tool. And Then within each one, you will create a mod.ts (ex index.js), where you will decide which methods should be exposed to the user and which remain for the internal use. And then you create filename.test.ts where you will be testing all of your exposed functions. Almost every functionality you offer will have this structure.
  14. IDE SETTINGS As mentioned before, Deno has its own linter

    and formatter, there is also an official vscode extension for Deno, that will connect to Deno-CLI, make sure to update you vscode settings as follows. This should be the default config for any Deno project.
  15. TESTING- TESTING - TESTING Deno standard library offers testing module

    with 14 assertions methods. To execute all tests run deno test in your project directory, otherwise you could specify the test file name after.
  16. Assertions Assertion types should cover all tests ( Existence -

    Equality - Instance types - Contains - Regex - Objects - Throws - Reject ). You could also create your OWN assertion, by importing the class AssertionError, and throwing it when the assertion fails. Deno also offer some Hooks like before all, after all, before each and after each. Deno Tests can be grouped inside describe(‘dog’,() => { it(‘bark’,() => myTest() ), it(‘eat’,() => myTest() ) }).
  17. Multithreading with Web-workers All our code runs on the main

    thread, and there are some use cases where multithreading comes in handy. The most famous use case when working on backends, is multithreading HTTP requests, so we can serve more than one request and increase our service performance. Another one, is when you need to do heavy computation on big databases, where having multiple workers reduce the computation time.
  18. WEB-WORKERS & SERVICE-WORKERS Same concept could be used in our

    front-end applications, but for different purposes. In some cases, you don’t want to block the main thread, causing the UI to freeze, and do heavy calculations for you app on other threads. Do not confuse Web-workers with service-workers ( both run on different thread ), which are used to offer offline functionalities for you web apps, making your app works with no internet connection, managing requests and serving lazy loaded chunks of your app.
  19. Watchers Deno can also watch for file system events. Remember

    to add the –allow-read permission in order to access fs. P.S. Results depends on the operating system.
  20. SUBPROCESS Deno can start subprocess, and also communicate with them

    using stdin, stdout and stderr. In order to spawn subprocesses, you will need to add the –allow-run permission, and specify all the allowed commands.
  21. Documentation You could generate your exported members documentation, by using

    deno doc mod.ts which will print the JSDoc documentation, you could add the –json to export them in json.