Corepack

and Node.js LTS 16

JaxNode October 2021

Topics

  • Node.js 16 LTS
  • Corepack
  • Yarn
  • PNPM

About Me

fek.io/blog

youtube.com/c/polyglotengineer

github.com/davidfekke

@jaxnode @polyglotengine1

Node 16 LTS

  • Node 16 is the stable version
  • Node 17 will be R&D version

LTS Schedule

Node.js 17

  • OpenSSL 3.0
  • V8 v9.5
  • Readline Promise API
import * as readline from 'node:readline/promises';
import { stdin as inp, stdout as outp } from 'process';

const rdln = readline.createInterface({ inp, outp });

const answer = await rdln.question('What do you think of JaxNode? ');

console.log(`Thank you for your valuable feedback: ${answer}`);

rdln.close();

Readline Promise API

Node 16 Features

  • Apple ARM64 support
  • New V8
  • New Regex functions
  • Stable Timer Promises API
  • Web Crypto API
  • New Node-API C methods
  • AbortController API
  • atob and btoa support
  • Source Maps v3

V8 9.5

  • New Regex Indices /d
  • Faster Js-to-Wasm calls
  • Use experimental --turbo-inline-js-wasm-calls

Stable Timer Promises

  • Exists under 'timer/promises'
  • Use this for using timers within Async/Await
import { setTimeout } from 'timers/promises';

async function doSomething() {
  console.log('doSomething started!');
  await setTimeout(2000);
  console.log('Delayed Timers!');
}
doSomething();

Web Crypto API

  • Newer version of the Crypto API
  • All new features under `subtle` interface
  • Many versions of `Crypto` 
  • Standardizing and a single Standard

Web Crypto Example

import { webcrypto } from 'crypto';
const { subtle } = webcrypto;

(async function() {

  const key = await subtle.generateKey({
    name: 'HMAC',
    hash: 'SHA-256',
    length: 256
  }, true, ['sign', 'verify']);

  const digest = await subtle.sign({
    name: 'HMAC'
  }, key, 'I love node.js');

  console.log(digest);
})();

N-API now Node-API

  • The way you extend Node with C/C++
  • napi_add_async_cleanup_hook
  • napi_object_freeze
  • napi_object_seal
  • napi_type_tag_object
  • napi_check_object_type_tag
  • napi_type_tag

AbortController

  • Actually part of Node 15
  • Use { once: true } option in Event Listeners
const abortC = new AbortController();
abortC.signal.addEventListener('abort', () => {
    console.log('Just cancelled')
}, { once: true });
abortC.abort();
console.log(abortC.signal.aborted);

atob and btoa Added

  • atob and btoa added to Node for JS compatibility
  • Used for converting Base64
  • DO NOT USE THESE NEW FUNCTIONS
  • There is a better way
const str = 'Hello JavaScript Developer!';

const strBuf = Buffer.from(str, 'utf8');
console.log(strBuf);
// <Buffer 1d e9 65 a0 96 af 69 27 2b 8a 9b 43 7a f7 a5 a2 97 ab>

const base64Buf = Buffer.from(strBuf, 'base64');
const base64Str = base64Buf.toString('base64');
console.log(base64Str);
// SGVsbG8gSmF2YVNjcmlwdCBEZXZlbG9wZXIh

const bufFromBase64Str = Buffer.from(base64Str, 'base64');

const decodedStr = bufFromBase64Str.toString('utf-8');
console.log(decodedStr);
// Hello JavaScript Developer!
console.log(str === decodedStr);
// true

Other Features

  • npm v8.0.0
  • Source Maps v3
  • process.binding() has been deprecated

Corepack

  • Zero runtime dependency Node script
  • Allows third party package managers with out having to have them preinstalled
  • Support for Yarn, PNPM and NPM

Enabling Corepack

  • > corepack enable
  • Can also enable specific package manager
  • > corepack enable yarn
  • > corepack diable

Activating Corepack

  • Can activate a specific package manager
  • > corepack prepare yarn@1.22.11 --activate

Using pkg managers

  • corepack yarn or pnpm
  • > yarn add axios
  • becomes
  • > corepack yarn add axios 

Package.json setting

  • Specify package manager and version in the package.json file
  • New key "packageManager"
{
  "name": "sampleproject",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "license": "MIT",
  "scripts": {
    "start": "node index.js"
  },
  "packageManager": "yarn@1.22.11",
  "dependencies": {
    "axios": "^0.21.4"
  }
}

Archiving and Distribution

  • Use prepare command
  • > corepack prepare yarn@1.22.11 --activate -o
  • Will Create compressed TGZ file called corepack.tgz
  • When deploying use `hydrate` command to deploy corepack archive
  • > corepack hydrate --activate corepack.tgz 

Resources

Questions?

Corepack

By David Fekke

Corepack

These are the slides for the JaxNode presentation on Corepack and Node.js 16 LTS

  • 742