-
-
Notifications
You must be signed in to change notification settings - Fork 659
Description
I wanted to discuss regarding devs filing issues and complain about undici not working in node 18.
First of all to the devs, with issues in node 18:
Maybe you can not upgrade to node 20+, for various reasons. We dont judge you.
You can mitigate the issue by writing a simple wrapper.
In cjs:
undici-wrapper.cjs:
'use strict';
const { toUSVString } = require('util')
const { Blob, File } = require('buffer');
if (typeof globalThis.Blob === 'undefined') {
globalThis.Blob = Blob;
}
if (typeof globalThis.File === 'undefined') {
globalThis.File = File;
}
if (String.prototype.toWellFormed === undefined) {
String.prototype.toWellFormed = function () {
return toUSVString(this);
}
}
if (String.prototype.isWellFormed === undefined) {
String.prototype.isWellFormed = function () {
return toUSVString(this) === this;
}
}
const undici = require('undici');
module.exports = undici;
fetch.cjs:
'use strict';
const { fetch } = require('./undici-wrapper.cjs');
async function run () {
const response = await fetch('https://google.com')
await response.blob()
}
run()
similar esm:
undici-wrapper.mjs
import { toUSVString } from 'util';
import { Blob, File } from 'buffer';
if (typeof globalThis.Blob === 'undefined') {
globalThis.Blob = Blob;
}
if (typeof globalThis.File === 'undefined') {
globalThis.File = File;
}
if (String.prototype.toWellFormed === undefined) {
String.prototype.toWellFormed = function () {
return toUSVString(this);
}
}
if (String.prototype.isWellFormed === undefined) {
String.prototype.isWellFormed = function () {
return toUSVString(this) === this;
}
}
const undici = await import('undici');
export default undici;
fetch.mjs
import undici from './undici-wrapper.mjs';
const { fetch } = undici;
const response = await fetch('https://google.com')
await response.blob()
I tested it locally and it worked accordingly. You can of course modify it to your needs, for example by explicitly exporting fetch so that you can make a named import. I think you get the gist of it.
As you can see... we modify the prototype of String! This will result in performance degredation!
I actually wonder why nobody reported a breaking change, as node 18 does not have isWellFormed
or toWellFormed
on the String.prototype
.
So assess the options: upgrade node and use latest undici or stick with the version of undici which works for you.
But you should have now the tools to continue your work.
Regarding the clarity:
Undici does not support node 18 since version 7.0.0. In the release notes to v7 on github, it is not clear for the "regular" dev. We just have a PR for dropping node 18 from the ci pipeline. For a regular dev, it is not clear, that we stop supporting node 18.
See https://github.com/nodejs/undici/releases/tag/v7.0.0
We should maybe consider to put it more dominant at the top of the release notes. We can still modify the v7 release notes and explicityl state that we dropped v18 support. For undici v8, we should make it more clear listing the breaking changes in the top of the release notes.
Also we should consider putting a section in the readme.md, clarifying which node versions we support.