π 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
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.
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.
This package is literally just a shortcut to installing and import
-ing all of
these packages in your project:
- jcbhmr/webidl-domexception
- jcbhmr/html-timers
- jcbhmr/html-microtask-queuing
- jcbhmr/html-structured-clonin
- jcbhmr/html-btoa
- jcbhmr/console
- jcbhmr/html-system-state
- jcbhmr/encoding
- jcbhmr/streams
- jcbhmr/fileapi
- jcbhmr/url
- jcbhmr/dom-aborting-ongoing-activities
- jcbhmr/fetch
- jcbhmr/urlpattern
- jcbhmr/webcrypto
- jcbhmr/compression
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:
globalThisECMAScript-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
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.