Skip to content
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

fix: node 18|20 compatibility #218

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
node-version: "18"
- name: Cache modules
uses: actions/cache@v2
with:
Expand All @@ -35,7 +35,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
node-version: "18"
- name: Cache modules
uses: actions/cache@v2
with:
Expand All @@ -58,7 +58,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
node-version: "18"
- name: Cache npm
uses: actions/cache@v2
with:
Expand All @@ -82,7 +82,7 @@ jobs:

strategy:
matrix:
node-version: [12, 14, 16]
node-version: [16, 18, 20]

steps:
- uses: actions/checkout@v2
Expand Down
14 changes: 14 additions & 0 deletions lib/fake-xhr/blob-and-filereader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

exports.isSupported = (function() {
try {
// eslint-disable-next-line no-unused-vars
const blob = new Blob();
// eslint-disable-next-line no-unused-vars
const fileReader = new FileReader();

return true;
} catch (e) {
return false;
}
})();
9 changes: 0 additions & 9 deletions lib/fake-xhr/blob.js

This file was deleted.

10 changes: 5 additions & 5 deletions lib/fake-xhr/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

var { FormData } = require("formdata-node");
var GlobalTextEncoder =
typeof TextEncoder !== "undefined"
? TextEncoder
Expand All @@ -11,9 +12,8 @@ var extend = require("just-extend");

var supportsProgress = typeof ProgressEvent !== "undefined";
var supportsCustomEvent = typeof CustomEvent !== "undefined";
var supportsFormData = typeof FormData !== "undefined";
var supportsArrayBuffer = typeof ArrayBuffer !== "undefined";
var supportsBlob = require("./blob").isSupported;
var supportsBlobAndFileReader = require("./blob-and-filereader").isSupported;

function getWorkingXHR(globalScope) {
var supportsXHR = typeof globalScope.XMLHttpRequest !== "undefined";
Expand Down Expand Up @@ -117,7 +117,7 @@ function verifyResponseBodyType(body, responseType) {
if (
!isString &&
!(body instanceof ArrayBuffer) &&
supportsBlob &&
supportsBlobAndFileReader &&
!(body instanceof Blob)
) {
error = new Error(
Expand Down Expand Up @@ -368,7 +368,7 @@ function fakeXMLHttpRequestFor(globalScope) {
// Return parsing failure as null
return null;
}
} else if (supportsBlob && responseType === "blob") {
} else if (supportsBlobAndFileReader && responseType === "blob") {
if (body instanceof Blob) {
return body;
}
Expand Down Expand Up @@ -657,7 +657,7 @@ function fakeXMLHttpRequestFor(globalScope) {
this.requestHeaders[
contentType
] = `${value[0]};charset=utf-8`;
} else if (supportsFormData && !(data instanceof FormData)) {
} else if (!(data instanceof FormData)) {
this.requestHeaders["Content-Type"] =
"text/plain;charset=utf-8";
}
Expand Down
72 changes: 35 additions & 37 deletions lib/fake-xhr/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var sinon = require("sinon");
var extend = require("just-extend");
var JSDOM = require("jsdom").JSDOM;

var { FormData } = require("formdata-node");

var GlobalTextEncoder =
typeof TextEncoder !== "undefined"
? TextEncoder
Expand All @@ -20,9 +22,8 @@ var refute = referee.refute;
var globalActiveXObject =
typeof ActiveXObject !== "undefined" ? ActiveXObject : undefined;

var supportsFormData = typeof FormData !== "undefined";
var supportsArrayBuffer = typeof ArrayBuffer !== "undefined";
var supportsBlob = require("./blob").isSupported;
var supportsBlobAndFileReader = require("./blob-and-filereader").isSupported;
var isInBrowser = global.window === global;
var setupDOM = require("jsdom-global");

Expand Down Expand Up @@ -77,15 +78,6 @@ function assertArrayBufferMatches(actual, expected) {
);
}

function assertBlobMatches(actual, expected, done) {
var actualReader = new FileReader();
actualReader.onloadend = function() {
assert.same(actualReader.result, expected);
done();
};
actualReader.readAsText(actual);
}

function assertProgressEvent(event, progress) {
assert.equals(event.loaded, progress);
assert.equals(event.total, progress);
Expand Down Expand Up @@ -639,19 +631,17 @@ describe("FakeXMLHttpRequest", function() {
assert.isNull(this.xhr.requestBody);
});

if (supportsFormData) {
describe("sets mime to text/plain", function() {
it("test", function() {
this.xhr.open("POST", "/");
this.xhr.send("Data");
describe("sets mime to text/plain", function() {
it("test", function() {
this.xhr.open("POST", "/");
this.xhr.send("Data");

assert.equals(
this.xhr.requestHeaders["Content-Type"],
"text/plain;charset=utf-8"
);
});
assert.equals(
this.xhr.requestHeaders["Content-Type"],
"text/plain;charset=utf-8"
);
});
}
});

it("does not override mime", function() {
this.xhr.open("POST", "/");
Expand All @@ -676,18 +666,16 @@ describe("FakeXMLHttpRequest", function() {
);
});

if (supportsFormData) {
describe("does not add 'Content-Type' header if data is FormData", function() {
it("test", function() {
this.xhr.open("POST", "/");
var formData = new FormData();
formData.append("username", "biz");
this.xhr.send("Data");
describe("does not add 'Content-Type' header if data is FormData", function() {
it("test", function() {
this.xhr.open("POST", "/");
var formData = new FormData();
formData.append("username", "biz");
this.xhr.send("Data");

assert.isUndefined(this.xhr.requestHeaders["content-type"]);
});
assert.isUndefined(this.xhr.requestHeaders["content-type"]);
});
}
});

it("sets request body to string data for GET", function() {
this.xhr.open("GET", "/");
Expand Down Expand Up @@ -1032,8 +1020,8 @@ describe("FakeXMLHttpRequest", function() {
});
}

if (supportsBlob) {
describe("with Blob support", function() {
if (supportsBlobAndFileReader) {
describe("with Blob and FileReader support", function() {
it("invokes onreadystatechange handler for each 10 byte chunk when responseType='blob'", function() {
var spy = sinonSpy();
this.xhr.readyStateChange = spy;
Expand Down Expand Up @@ -1778,8 +1766,18 @@ describe("FakeXMLHttpRequest", function() {
});
}

if (supportsBlob) {
describe("with Blob support", function() {
if (supportsBlobAndFileReader) {
// eslint-disable-next-line no-inner-declarations
function assertBlobMatches(actual, expected, done) {
var actualReader = new FileReader();
actualReader.onloadend = function() {
assert.same(actualReader.result, expected);
done();
};
actualReader.readAsText(actual);
}

describe("with Blob and FileReader support", function() {
it("is initially null if responseType === 'blob'", function() {
this.xhr.responseType = "blob";
this.xhr.open("GET", "/");
Expand Down Expand Up @@ -2001,7 +1999,7 @@ describe("FakeXMLHttpRequest", function() {
this.xhr = new FakeXMLHttpRequest();
});

if (supportsBlob) {
if (supportsBlobAndFileReader) {
it("overrides provided MIME-Type", function() {
this.xhr.responseType = "blob";
this.xhr.open("GET", "/");
Expand Down
Loading
Loading