BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Deno Improves Support for Web APIs: Cryptography, Messaging, Networking, and More

Deno Improves Support for Web APIs: Cryptography, Messaging, Networking, and More

This item in japanese

The recent versions of Deno improved web API support in the cryptography, networking, and messaging areas. Deno 1.11 introduced support for the Web Crypto APIs and BroadcastChannel APIs. Deno 1.12 added support for the MessageChannel and MessagePort portions of the Channel Messaging API. Deno 1.13 implements the navigator.hardwareConcurrency API.

Deno 1.11 introduced support for the Web Crypto API. The Web Crypto API is an interface allowing a script to use low-level cryptographic primitives to build secure systems. Deno 1.0 already supported crypto.getRandomValues(). Deno 1.11 implements the SubtleCrypto.digest API that computes a digest of the data passed as a parameter:

import { encodeToString } from "https://deno.land/std@0.97.0/encoding/hex.ts";
const data = new TextEncoder().encode("Deno 1.11 has been released!");
const digest = await crypto.subtle.digest("sha-256", data.buffer);
console.log("Digest:", encodeToString(new Uint8Array(digest)));

Deno 1.11 also implements the crypto.randomUUID API that generates RFC 4122 version 4 identifiers (for example 6e4decd0-6066-4a25-98e3-0227317cda52). Standardizing a UUID method, which dictates that a cryptographically-secure pseudorandom number generator must be used, helps protect developers from security pitfalls.

console.log("Random UUID:", crypto.randomUUID());

Deno 1.12 further supports the generateKey, sign, and verify web crypto APIs:

const keyPair = await crypto.subtle.generateKey(
  {
    name: "RSA-PSS",
    modulusLength: 2048,
    publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
    hash: { name: "SHA-256" },
  },
  true,
  ["sign", "verify"],
);

const data = new TextEncoder().encode("hello world");
const signature = await crypto.subtle.sign(
  { name: "RSA-PSS", saltLength: 32 },
  keyPair.privateKey,
  data,
);

const isValid = await crypto.subtle.verify(
  { name: "RSA-PSS", saltLength: 32 },
  keyPair.publicKey,
  signature,
  data,
);

Deno 1.13 further implements the crypto.subtle.importKey() and crypto.subtle.exportKey().

Deno 1.11 supports the BroadcastChannel browser API to broadcast messages amongst web workers. The Deno 1.11 release note however warns of the current limitations of the implementation:

In this release we are prototyping support for BroadcastChannel under --unstable. BroadcastChannel can be used to broadcast messages consisting of complex JavaScript objects between workers. Currently, this is restricted to workers in the same process […]

Deno 1.12 further supports the Channel Messaging API. The Channel Messaging API allows two separate scripts running in different browsing contexts attached to the same document (e.g., two IFrames, or the main document and an IFrame, two documents via a SharedWorker, or two workers) to communicate directly, passing messages between one another through two-way channels (or pipes) with a port at each end. The current Deno implementation however is restricted to workers in the same process.

The navigator.hardwareConcurrency API gives access to the number of logical processors (two or four cores are typical) available to run threads on the user’s computer. The API can be used to determine the number of threads which can effectively be run at once without them having to context switch.

Developers can upgrade to the latest Deno version by running:

deno upgrade

Rate this Article

Adoption
Style

BT