npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-domexception

v2.0.1

Published

An implementation of the DOMException class from NodeJS

Downloads

31,543,836

Readme

DOMException

An implementation of the DOMException class from NodeJS

NodeJS has DOMException built in (from v10.5), but it is not easily available... you can't require or import it from somewhere (unless you use node v17 - at which point it got added to global scope)

This package exposes the DOMException class that comes from NodeJS itself. (including all of the legacy codes)

Install

# makes use of atob | require NodeJS 16+
npm install node-domexception

# makes use of worker_thread | require NodeJS 10+
npm install [email protected]

v2.x now depend on global atob to obtain DOMException from a error. which also binds it to NodeJS v16+ (at which point atob become globally available). This NodeJS dependency/export free solution is better for cross env platform solutions. it no longer have any cjs or esm exports and it's attached to globalThis.DOMException only if needed.

v1.x used a older tech which depended on node:worker_threads to obtain DOMException which works all the way down to NodeJS v10.5+

If you are not supporting older NodeJS versions (before v17) then you won't need this package at all. My personal recommendation is that you update to a newer NodeJS version. This pkg will likely be deprecated once v18 becomes LTS

import 'node-domexception'

// You could also do conditional import.
globalThis.DOMException || await import('node-domexception')

/********************************************************************/

import { MessageChannel } from 'worker_threads'

try {
  const port = new MessageChannel().port1
  const ab = new ArrayBuffer()
  port.postMessage(ab, [ab, ab])
} catch (err) {
  console.assert(err.name === 'DataCloneError')
  console.assert(err.code === 25)
  console.assert(err.constructor === DOMException)
}

const e1 = new DOMException('Something went wrong', 'BadThingsError')
console.assert(e1.name === 'BadThingsError')
console.assert(e1.code === 0)

const e2 = new DOMException('Another exciting error message', 'NoModificationAllowedError')
console.assert(e2.name === 'NoModificationAllowedError')
console.assert(e2.code === 7)

console.assert(DOMException.INUSE_ATTRIBUTE_ERR === 10)

Background

The only possible way is to use some web-ish tools that have been introduced into NodeJS that throws a DOMException and catch the constructor. This is exactly what this package does for you and exposes it. This way you will have the same class that NodeJS has and you can check if the error is a instance of DOMException. The instanceof check would not have worked with a custom class such as the DOMException provided by domenic which also is much larger in size since it has to re-construct the whole class from the ground up.

The DOMException is used in many places such as the Fetch API, File & Blobs, PostMessaging and more. Why they decided to call it DOM, I don't know

Please consider sponsoring if you find this helpful