Skip to content

🎁 A bundled polyfill for the Minimum Common Web Platform API

License

Notifications You must be signed in to change notification settings

web4more/common-min-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🚧 Under construction πŸ‘·β€β™‚οΈ

Minimum Common Web Platform API polyfill

🎁 A bundled polyfill for the Minimum Common Web Platform API

πŸ“‹ Use structuredClone() everywhere
🌐 Call fetch() with no fear
πŸ–₯️ Works in Node.js
πŸ¦• Works in Deno
πŸ§… Works in Bun

Installation

npm Yarn pnpm

You can install this locally using npm, Yarn, or pnpm:

npm install @jcbhmr/common-min-api

You can use this package with Node.js, Deno, or Bun to fill in their own gaps in the Minimum Common Web Platform API. Make sure you're using a relatively modern version of your runtime so that you can use the least amount of polyfill code!

βš–οΈ Don't worry! Each sub-polyfill uses an if-gated import() so you only pay for what wasn't already implemented in your environment.

Usage

Node.js Deno Bun

import "@jcbhmr/common-min-api";

const b = structuredClone({ n: 1 });
console.log(b.n);
//=> 1
setTimeout(() => reportError(new Error("πŸŒ‹ Oh no!")), 1000);
const r = crypto.getRandomValues(new Uint8Array(10));
console.info(r);
//=> Uint8Array(10)Β [...]
const f = new File(["foo"], "foo.txt", { type: "text/plain" });
const u = new URL("https://example.com/foo");
if (new URLPattern("https://example.com/*").test(u)) {
  const response = await fetch(u);
  const text = await response.text();
  console.log(text);
}

πŸ‘΄ This polyfill bundle is not intended for ancient environments like IE 11. The purpose of this polyfill bundle is to make it easier to write isomorphic modern code that uses popular web platform APIs that may not exist on certain platforms yet.

What's included?

This package is literally just a shortcut to installing and import-ing all of these packages in your project:

Here's an excerpt of exactly what we are trying to expose for your application to use:

3. Common API Index

All Web-interoperable Runtimes conforming to this specification SHALL implement each of the following Web Platform APIs in accordance to their normative requirements except where modified here. Where any conforming runtime environment chooses (either by necessity or otherwise) to diverge from a normative requirement of the specification, clear explanations of such divergence MUST be made clearly and readily available in documentation.

Interfaces:

  • AbortController
  • AbortSignal
  • Blob
  • ByteLengthQueuingStrategy
  • CompressionStream
  • CountQueuingStrategy
  • Crypto
  • CryptoKey
  • DecompressionStream
  • DOMException
  • Event
  • EventTarget
  • File
  • ReadableByteStreamController
  • ReadableStream
  • ReadableStreamBYOBReader
  • ReadableStreamBYOBRequest
  • ReadableStreamDefaultController
  • ReadableStreamDefaultReader
  • SubtleCrypto
  • TextDecoder
  • TextDecoderStream
  • TextEncoder
  • TextEncoderStream
  • TransformStream
  • TransformStreamDefaultController
  • URL
  • URLPattern
  • URLSearchParams
  • WritableStream
  • WritableStreamDefaultController

Global methods / properties:

  • globalThis ECMAScript-related
  • globalThis.atob()
  • globalThis.btoa()
  • globalThis.console
  • globalThis.crypto
  • globalThis.navigator.userAgent
  • globalThis.queueMicrotask()
  • globalThis.setTimeout() / globalThis.clearTimeout()
  • globalThis.setInterval() / globalThis.clearInterval()
  • globalThis.structuredClone()

β€” Common API Index | Minimum Common Web Platform API

Development

JavaScript Node.js

This package is literally just a bunch of import statements. 🀣 You can test Node.js compatibility by running npm test. You'll need to manually check for Deno and Bun compatibility.

npm test

Regarding versioning: Each child polyfill package should be pinned to a specific version. On patch updates, release a patch update of this package. On minor updates, release a minor update of this package. On major updates, release a major update of this package. This is to ensure that the polyfill bundle is stable and predictable, while also letting ^ and ~ ranges work as expected.

Oh, and we also use bundleDependencies to make sure that the you only need to fetch this package to get all of the dependencies. This is to make it faster to download.

Releases

No releases published

Packages

No packages published