Open in a new tab

The @awacloud/fw/typed entry: createRuntime for a fresh typed runtime, asTyped to re-type the app's runtime, and resolve() narrowed by module name that a JSDoc-aware editor or tsc can check.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

You want completion and type errors on module instances without writing TypeScript: rt.resolve('hex') is a HexAPI, rt.resolve('hxe') is an error in the editor. The typing is zero-cost: asTyped returns its argument.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html and main.js next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.
  4. Enable checkJs (a jsconfig.json with "checkJs": true) so the JSDoc @type annotations are checked; the types ship with the package. @awacloud/fw/typed.js resolves under an import map, a bundler and Node alike.

What it shows

  • createRuntime([hex, b64]): a fresh ModuleRuntime, deep-registered, typed as TypedModuleRuntime.
  • asTyped(fw.runtime): the same runtime object with the narrowed resolve / resolveAll signatures.
  • @type {import('@awacloud/fw/typed').TypedModuleRuntime} on the exports, so the narrowing reaches every consumer.
  • Resolving a module that was not registered still throws at run time: the types describe the catalogue, not what you injected.

Code

main.js

javascriptApache-2.048 lines

// SPDX-License-Identifier: Apache-2.0
import { asTyped, createRuntime } from '@awacloud/fw/typed.js';
import fw from '@awacloud/fw';
import { hex } from '@awacloud/fw/io/codec/hex.js';
import { b64 } from '@awacloud/fw/io/codec/b64.js';
import { crc32 } from '@awacloud/fw/io/calc/crc32.js';

/**
 * A fresh runtime with exactly these modules (and their `deps`), typed so
 * that `resolve('hex')` is a `HexAPI` for the editor and the type-checker.
 * @type {import('@awacloud/fw/typed').TypedModuleRuntime}
 */
export const rt = createRuntime([hex, b64]);

/**
 * The app's own runtime, re-typed: `asTyped` is an identity at run time.
 * @type {import('@awacloud/fw/typed').TypedModuleRuntime}
 */
export const app = asTyped(fw.runtime.registerAllDeep([crc32]));

const codec = rt.resolve('hex');      // HexAPI: toBytes, fromBytes, test
const base64 = rt.resolve('b64');     // B64API
const Crc32 = app.resolve('crc32');   // the CRC-32 constructor

/**
 * @param {string} text
 * @returns {{ hex: string, b64: string, crc: string }}
 */
export function encode(text) {
    const bytes = new TextEncoder().encode(text);
    const crc = new Crc32();
    crc.append(bytes);
    return { hex: codec.fromBytes(bytes), b64: base64.fromBytes(bytes), crc: crc.get().toString(16).padStart(8, '0') };
}

/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLElement} */ (document.getElementById(id)).textContent = value; };
const input = /** @type {HTMLInputElement} */ (document.getElementById('input'));

function update() {
    const out = encode(input.value);
    show('codecs', `${out.hex} · ${out.b64}`);
    show('crc', out.crc);
}

show('fresh', rt.list().map((m) => m.name).sort().join(', '));
input.addEventListener('input', update);
update();

index.html

htmlApache-2.029 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · A typed runtime for editors and type-checkers</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>A typed runtime for editors and type-checkers</h1>
    <label for="input">Text</label>
    <input id="input" type="text" value="fw" autocomplete="off">
    <dl>
      <dt>Modules in the fresh runtime (<code>createRuntime</code>)</dt><dd><output id="fresh"></output></dd>
      <dt>Hex and Base64 of the text, resolved with narrowing</dt><dd><code id="codecs"></code></dd>
      <dt>CRC-32 from the app runtime (<code>asTyped(fw.runtime)</code>)</dt><dd><code id="crc"></code></dd>
    </dl>
    <p>Open <code>main.js</code> in an editor with JSDoc type checking: <code>rt.resolve('hex')</code> is a
      <code>HexAPI</code>, and a misspelt name is flagged.</p>
  </main>
</body>
</html>