Skip to content

Commit e6117d8

Browse files
authored
chore(deps): replace got with fetch (#13225)
1 parent 37469a3 commit e6117d8

File tree

9 files changed

+55
-195
lines changed

9 files changed

+55
-195
lines changed

build/flaws/index.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from "node:path";
22
import chalk from "chalk";
3-
import { RequestError } from "got";
43

54
import { Document } from "../../content/index.js";
65
import {
@@ -238,19 +237,6 @@ export async function fixFixableFlaws(doc: Partial<Doc>, options, document) {
238237
console.log(`Downloaded ${flaw.src} to ${destination}`);
239238
newSrc = path.basename(destination);
240239
} catch (error) {
241-
if (error instanceof RequestError) {
242-
if (error.response.statusCode === 404) {
243-
console.log(chalk.yellow(`Skipping ${flaw.src} (404)`));
244-
continue;
245-
} else if (
246-
error.code === "ETIMEDOUT" ||
247-
error.code === "ENOTFOUND"
248-
) {
249-
console.log(chalk.yellow(`Skipping ${flaw.src} (${error.code})`));
250-
continue;
251-
}
252-
}
253-
254240
console.error(error);
255241
throw error;
256242
}

build/spas.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import path from "node:path";
33

44
import frontmatter from "front-matter";
55
import { fdir, PathsOutput } from "fdir";
6-
import got from "got";
76

87
import { m2h } from "../markdown/index.js";
98
import * as kumascript from "../kumascript/index.js";
@@ -487,7 +486,9 @@ async function fetchGitHubPRs(repo, count = 5) {
487486
].join("+");
488487
const pullRequestUrl = `https://api.github.com/search/issues?q=${pullRequestsQuery}&per_page=${count}`;
489488
try {
490-
const pullRequestsData = (await got(pullRequestUrl).json()) as {
489+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
490+
const response = await fetch(pullRequestUrl);
491+
const pullRequestsData = (await response.json()) as {
491492
items: any[];
492493
};
493494
const prDataRepo = pullRequestsData.items.map((item) => ({

build/utils.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import path from "node:path";
55
import { cwd } from "node:process";
66

77
import * as cheerio from "cheerio";
8-
import got from "got";
98
import { fileTypeFromBuffer } from "file-type";
109
import imagemin from "imagemin";
1110
import imageminPngquant from "imagemin-pngquant";
@@ -61,19 +60,15 @@ export async function downloadAndResizeImage(
6160
out: string,
6261
basePath: string
6362
) {
64-
const imageResponse = await got(forceExternalURL(src), {
65-
responseType: "buffer",
66-
timeout: { request: 10000 },
67-
retry: { limit: 3 },
68-
});
69-
const imageBuffer = imageResponse.body;
63+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
64+
const response = await fetch(forceExternalURL(src));
65+
const arrayBuffer = await response.arrayBuffer();
66+
const imageBuffer = Buffer.from(arrayBuffer);
7067
let fileType = await fileTypeFromBuffer(imageBuffer);
7168
if (
7269
!fileType &&
7370
src.toLowerCase().endsWith(".svg") &&
74-
imageResponse.headers["content-type"]
75-
.toLowerCase()
76-
.startsWith("image/svg+xml")
71+
response.headers["content-type"].toLowerCase().startsWith("image/svg+xml")
7772
) {
7873
// If the SVG doesn't have the `<?xml version="1.0" encoding="UTF-8"?>`
7974
// and/or the `<!DOCTYPE svg PUBLIC ...` in the first couple of bytes

kumascript/src/api/mdn.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import got from "got";
21
import { KumaThis } from "../environment.js";
32
import * as util from "./util.js";
43

@@ -144,13 +143,11 @@ const mdn = {
144143
async fetchWebExtExamples() {
145144
if (!webExtExamples) {
146145
try {
147-
webExtExamples = await got(
148-
"https://raw.githubusercontent.com/mdn/webextensions-examples/master/examples.json",
149-
{
150-
timeout: { request: 1000 },
151-
retry: { limit: 5 },
152-
}
153-
).json();
146+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
147+
const response = await fetch(
148+
"https://raw.githubusercontent.com/mdn/webextensions-examples/master/examples.json"
149+
);
150+
webExtExamples = await response.json();
154151
} catch (error) {
155152
webExtExamples = error;
156153
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
"file-type": "^21.0.0",
119119
"front-matter": "^4.0.2",
120120
"fs-extra": "^11.3.0",
121-
"got": "^13.0.0",
122121
"he": "^1.2.0",
123122
"http-proxy-middleware": "^2.0.9",
124123
"image-size": "^1.2.1",

testing/tests/developing.spec.ts

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
/* eslint-disable n/no-unsupported-features/node-builtins */
12
import { test, expect } from "@playwright/test";
2-
import got from "got";
33

44
export {};
55

@@ -71,9 +71,11 @@ test.describe("Testing the kitchensink page", () => {
7171
test.skip(withDevelop());
7272

7373
// Loading the index.json doesn't require a headless browser
74-
const { doc } = await got(
74+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
75+
const response = await fetch(
7576
serverURL("/en-US/docs/MDN/Kitchensink/index.json")
76-
).json<any>();
77+
);
78+
const { doc } = await response.json();
7779

7880
expect(doc.title).toBe("The MDN Content Kitchensink");
7981
});
@@ -87,84 +89,80 @@ test.describe("Testing the Express server", () => {
8789
test("redirect without any useful headers", async () => {
8890
test.skip(withDevelop());
8991

90-
let response = await got(serverURL("/"), { followRedirect: false });
91-
expect(response.statusCode).toBe(302);
92-
expect(response.headers.location).toBe("/en-US/");
92+
let response = await fetch(serverURL("/"), { redirect: "manual" });
93+
expect(response.status).toBe(302);
94+
expect(response.headers.get("location")).toBe("/en-US/");
9395

94-
response = await got(serverURL("/docs/Web"), {
95-
followRedirect: false,
96-
});
97-
expect(response.statusCode).toBe(302);
98-
expect(response.headers.location).toBe("/en-US/docs/Web");
96+
response = await fetch(serverURL("/docs/Web"), { redirect: "manual" });
97+
expect(response.status).toBe(302);
98+
expect(response.headers.get("location")).toBe("/en-US/docs/Web");
9999

100100
// Trailing slashed
101-
response = await got(serverURL("/docs/Web/"), {
102-
followRedirect: false,
103-
});
104-
expect(response.statusCode).toBe(302);
105-
expect(response.headers.location).toBe("/en-US/docs/Web");
101+
response = await fetch(serverURL("/docs/Web/"), { redirect: "manual" });
102+
expect(response.status).toBe(302);
103+
expect(response.headers.get("location")).toBe("/en-US/docs/Web");
106104
});
107105

108106
test("redirect by preferred locale cookie", async () => {
109107
test.skip(withDevelop());
110108

111-
let response = await got(serverURL("/"), {
112-
followRedirect: false,
109+
let response = await fetch(serverURL("/"), {
110+
redirect: "manual",
113111
headers: {
114112
// Note! Case insensitive
115113
Cookie: "preferredlocale=zH-cN; foo=bar",
116114
},
117115
});
118-
expect(response.statusCode).toBe(302);
119-
expect(response.headers.location).toBe("/zh-CN/");
116+
expect(response.status).toBe(302);
117+
expect(response.headers.get("location")).toBe("/zh-CN/");
120118

121119
// Some bogus locale we definitely don't recognized
122-
response = await got(serverURL("/"), {
123-
followRedirect: false,
120+
response = await fetch(serverURL("/"), {
121+
redirect: "manual",
124122
headers: {
125123
Cookie: "preferredlocale=xyz; foo=bar",
126124
},
127125
});
128-
expect(response.statusCode).toBe(302);
129-
expect(response.headers.location).toBe("/en-US/");
126+
expect(response.status).toBe(302);
127+
expect(response.headers.get("location")).toBe("/en-US/");
130128
});
131129

132130
test("redirect by 'Accept-Language' header", async () => {
133131
test.skip(withDevelop());
134132

135-
let response = await got(serverURL("/"), {
136-
followRedirect: false,
133+
let response = await fetch(serverURL("/"), {
134+
redirect: "manual",
137135
headers: {
138136
// Based on https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language
139137
"Accept-language": "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
140138
},
141139
});
142-
expect(response.statusCode).toBe(302);
143-
expect(response.headers.location).toBe("/fr/");
140+
expect(response.status).toBe(302);
141+
expect(response.headers.get("location")).toBe("/fr/");
144142

145143
// Some bogus locale we definitely don't recognized
146-
response = await got(serverURL("/"), {
147-
followRedirect: false,
144+
response = await fetch(serverURL("/"), {
145+
redirect: "manual",
148146
headers: {
149147
"accept-language": "xyz, X;q=0.9, Y;q=0.8, Z;q=0.7, *;q=0.5",
150148
},
151149
});
152-
expect(response.statusCode).toBe(302);
153-
expect(response.headers.location).toBe("/en-US/");
150+
expect(response.status).toBe(302);
151+
expect(response.headers.get("location")).toBe("/en-US/");
154152
});
155153

156154
test("redirect by cookie trumps", async () => {
157155
test.skip(withDevelop());
158156

159-
const response = await got(serverURL("/"), {
160-
followRedirect: false,
157+
const response = await fetch(serverURL("/"), {
158+
redirect: "manual",
161159
headers: {
162160
Cookie: "preferredlocale=ja",
163161
"Accept-language": "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
164162
},
165163
});
166-
expect(response.statusCode).toBe(302);
167-
expect(response.headers.location).toBe("/ja/");
164+
expect(response.status).toBe(302);
165+
expect(response.headers.get("location")).toBe("/ja/");
168166
});
169167
});
170168

testing/tests/redirects.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import got from "got";
21
import braces from "braces";
32

43
function serverURL(pathname = "/") {
@@ -12,13 +11,11 @@ function url_test(from, to, { statusCode = 301 } = {}) {
1211
return expanded.map((f) => [
1312
f,
1413
async () => {
15-
const res = await got(serverURL(f), {
16-
followRedirect: false,
17-
throwHttpErrors: false,
18-
});
19-
expect(res.statusCode).toBe(statusCode);
14+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
15+
const res = await fetch(serverURL(f), { redirect: "manual" });
16+
expect(res.status).toBe(statusCode);
2017
if (to) {
21-
expect((res.headers.location || "").toLowerCase()).toBe(
18+
expect((res.headers.get("location") || "").toLowerCase()).toBe(
2219
encodeURI(to).toLowerCase()
2320
);
2421
}

tool/popularities.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import fs from "node:fs";
99

1010
import * as csv from "@fast-csv/parse";
11-
import got from "got";
1211

1312
const CURRENT_URL = "https://popularities.mdn.mozilla.net/current.csv";
1413

1514
async function fetchPopularities() {
16-
const { body: csv } = await got(CURRENT_URL);
15+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
16+
const response = await fetch(CURRENT_URL);
17+
const csv = await response.text();
1718
return csv;
1819
}
1920

0 commit comments

Comments
 (0)