-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test workflow #5
Conversation
Hmm, mostly hope/rely on that NodeJS have tested When NodeJS stop supporting v16 and v18 becomes LTS then i will probably mark this a legacy and deprecate the NPM package. This package was really only meant for running inside of NodeJS v10.5.x - 16.x.x. in other words i wanted it to be more of a polyfill rather than a ponyfill that had it's own class instance. Any way. it feels pointless to run the test on NodeJS v17+ when there is a built in global DOMException available but now instead do it with the built in --experimental-network-imports flag instead WPT stuff can change If we are using http import then we don't have to creating any git submodule or anything complicated (their repo is really large). Then we could always use the newest and latest test cases, and i wouldn't mind if they added something new that suddenly wasn't covered by us. it would just let me know that there is something that needs fixing or i could otherwise just disable some certain rules. import 'https://wpt.live/webidl/ecmascript-binding/es-exceptions/DOMException-constants.any.worker-module.js'
import 'https://wpt.live/webidl/ecmascript-binding/es-exceptions/DOMException-constructor-and-prototype.any.worker-module.js'
import 'https://wpt.live/webidl/ecmascript-binding/es-exceptions/DOMException-constructor-behavior.any.worker-module.js'
import 'https://wpt.live/webidl/ecmascript-binding/es-exceptions/DOMException-custom-bindings.any.worker-module.js' but it requires some bit more of a pre-work to set up so it can run in NodeJS. eg you need to add But if we are limiting ourself to use The engine i currently support is 10.5+ "engines": {
"node": ">=10.5.0"
}, |
Anyhow, adding test cases with files & folders, github workflow, and a package lock file when there isn't any dependencies feels like over doing it. Feel like a good recommendation for ppl would be if they did do: globalThis.DOMException || require('node-domexception')
globalThis.DOMException || await import('node-domexception') |
👍
Would it be prudent to add an export condition to block all other runtimes? or no? like: "exports": {
"deno": "./DNE",
"bun": "./DNE",
"node": "./index.js",
"default": "./DNE"
}, https://github.com/jcbhmr/node-45981 This would hard-fail when bundling with something like Vite since it would be unable to find the file instead of giving a runtime error/no-op or whatever later after successfully building. I think compile-time failures like that are better than runtime "nodejsthing is not defined" errors. Don't think the errors thing is applicable here, but the I like the idea of a no-op for browsers #6
as for the exports suggestion, don't worry you can do 👍 I can make it test v10 v12 v14 and v16 if that sounds good? I was interested in incorporating this node-domexception polyfill into a Minimum Common Web Platform https://common-min-api.proposal.wintercg.org/ bundle polyfill that doesn't seem to exist yet on google To do this, I'd wanted to make it as "pseudo-universal" as possible and kinda wanted to do these things to this package. Of them, the tests were the first thing that seemed easiest:
Basically make it more universal than just a Node.js drop-in. |
That sounds interesting. if you are only going to support NodeJS v16.7+ then i may suggest a alternative solution that i have found in the undici package. it works similar to my package (Uses web api that throws DOM error and catches it) it dose not involve any built in built in NodeJS specific node modules like using const DOMException = globalThis.DOMException || (() => {
try { atob(1) } catch (err) { return err.constructor }
})()
// or
globalThis.DOMException ??= (() => {
try { atob(1) } catch (err) { return err.constructor }
})() i think this is so small that you could just as well inline it without using any dependencies. My solution is for something that works in much earlier NodeJS versions. And the reason i think you should only support node v16.7+ is so that you can depend on the undici package to bring in their fetch impl into common-min-api polyfill |
Hmm, i had some 2nd toughs on what i want to do right now. i might just want to do something like: export default globalThis.DOMException ??= (() => {
try { atob(1) } catch (err) { return err.constructor }
})() Create a "version": "2.x.x",
"type" "module",
"engines": {
"node": ">=16.0.0"
}, And then change the readme to say something in terms of: globalThis.DOMException ??= (() => {
try { atob(1) } catch (err) { return err.constructor }
})() And just don't export anything. then this could work with both |
I had a 2nd look at what some CDN deliveries did to my package: esm.shimport DOMException from 'https://esm.sh/[email protected]' /* esm.sh - [email protected] */
export * from "https://esm.sh/v124/[email protected]/es2022/node-domexception.mjs";
export { default } from "https://esm.sh/v124/[email protected]/es2022/node-domexception.mjs"; /* esm.sh - esbuild bundle([email protected]) es2022 production */
import __1$ from "/error.js?type=unsupported-nodejs-builtin-module&name=worker_threads&importer=node-domexception";var x=Object.create;var c=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,g=Object.prototype.hasOwnProperty;var h=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(o,t)=>(typeof require<"u"?require:o)[t]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw new Error('Dynamic require of "'+e+'" is not supported')});var m=(e,o)=>()=>(o||e((o={exports:{}}).exports,o),o.exports),b=(e,o)=>{for(var t in o)c(e,t,{get:o[t],enumerable:!0})},a=(e,o,t,l)=>{if(o&&typeof o=="object"||typeof o=="function")for(let s of M(o))!g.call(e,s)&&s!==t&&c(e,s,{get:()=>o[s],enumerable:!(l=d(o,s))||l.enumerable});return e},n=(e,o,t)=>(a(e,o,"default"),t&&a(t,o,"default")),p=(e,o,t)=>(t=e!=null?x(_(e)):{},a(o||!e||!e.__esModule?c(t,"default",{value:e,enumerable:!0}):t,e));var i=m((w,f)=>{if(!globalThis.DOMException)try{let{MessageChannel:e}=__1$,o=new e().port1,t=new ArrayBuffer;o.postMessage(t,[t,t])}catch(e){e.constructor.name==="DOMException"&&(globalThis.DOMException=e.constructor)}f.exports=globalThis.DOMException});var r={};b(r,{default:()=>O});var D=p(i());n(r,p(i()));var{default:u,...E}=D,O=u!==void 0?u:E;export{O as default};
/*! Bundled license information:
node-domexception/index.js:
(*! node-domexception. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> *)
*/
//# sourceMappingURL=node-domexception.mjs.map it resulted in a error due to it trying to load: // Which contained this code:
throw new Error("[esm.sh] " + "unsupported nodejs builtin module \"worker_threads\" (Imported by \"node-domexception\")");
export default null; esm.sh failed to see jsdeliverjsdeliver dose a okey job when doing import DOMException from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm' the jspm.devOh man... this one have tried shipping a browserified version of /*! node-domexception. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
if (!globalThis.DOMException) {
try {
const { MessageChannel } = await import('worker_threads'),
port = new MessageChannel().port1,
ab = new ArrayBuffer()
port.postMessage(ab, [ab, ab])
} catch (err) {
err.constructor.name === 'DOMException' && (
globalThis.DOMException = err.constructor
)
}
}
export default globalThis.DOMException I kind of wish that every CDN delivery did this kind of conversion and used top-level await instead... unpkg.comloading node-domexception would fail b/c it would not do any conversion. And then throw on |
My conclusioni think if i did release this package as a true ESM-only package and i did this conditional top level await import of either way this could be solved very gracefully if i don't depend on |
@jcbhmr what do you think i should do? Use the Should this be converted to a ESM only module by adding Or should it still be possible to use cjs/require? ditching the |
I think that ESM is the way forward as cited by the famous sindre https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c As for the atob() solution, I think that it's a great solution! Supporting LTS-only versions of Node.js is a good move IMO. I like using modern stuff, but I understand that if you have legitimate consumers of this package using old Node.js versions that this could be a bad tradeoff to make and sticking with something older (like the current worker_threads postMessage()) might be better. As for the polyfill-only package vs the other ponyfill stuff, I think that comes down to your preference since you're the package author. I know I like to try and make polyfills "composable" into libraries if possible so that a lib author can use But since this is such aa tiny polyfill that might not be needed. In summary:
|
That seems good to me. require('node-domexception/polyfill.js') where as ESM author could use either: import 'node-domexception/polyfill.js' // or
import { DOMException } from 'node-domexception/ponyfill.js' The main file could still be pointed to the old legacy worker_thread solution and the new where as the main legacy worker_thread solution dose not change at all. and still supports v10.5 |
BLOCKED by #3 since it uses
npm test
in CIThis PR would...
UNDRAFT this when/if #3 is merged