Skip to content

Commit 4d3deb0

Browse files
refactor: Drop headers support of Utils.fetchLite()
The `accept` header is important for Registry NPM, but unfortunately it has a "bug" that makes it currently useless (npm/feedback#831).
1 parent ed6fcd4 commit 4d3deb0

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

src/NPM.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,15 @@ export const getPackageVersions = async (
3535
// This avoids loading processes via `npm view`.
3636
// The process is cached if it is triggered quickly, within lifetime.
3737
const execPromise = new Promise<string[] | null>((resolve) =>
38-
fetchLite<NPMRegistryPackage>(
39-
`https://registry.npmjs.org/${name}`,
40-
undefined,
41-
{ Accept: "application/vnd.npm.install-v1+json" } // Optimal version (~50% smaller response).
42-
).then((data) => {
43-
if (data?.versions) {
44-
return resolve(Object.keys(data.versions))
45-
}
38+
fetchLite<NPMRegistryPackage>(`https://registry.npmjs.org/${name}`).then(
39+
(data) => {
40+
if (data?.versions) {
41+
return resolve(Object.keys(data.versions))
42+
}
4643

47-
return resolve(null)
48-
})
44+
return resolve(null)
45+
}
46+
)
4947
)
5048

5149
packagesCache.set(name, new Cache(execPromise))
@@ -152,7 +150,6 @@ export const getPackagesAdvisories = async (
152150
const responseAdvisories = await fetchLite<PackagesAdvisories | undefined>(
153151
"https://registry.npmjs.org/-/npm/v1/security/advisories/bulk",
154152
"post",
155-
undefined,
156153
packages
157154
)
158155

src/Utils.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ describe("utils", () => {
139139
const fetchSuccess = await fetchLite(
140140
"https://registry.npmjs.org/-/npm/v1/security/advisories/bulk",
141141
"post",
142-
undefined,
143142
{ "npm-outdated": ["2.0.3"] }
144143
)
145144

@@ -150,9 +149,7 @@ describe("utils", () => {
150149
expect.assertions(1)
151150

152151
const fetchSuccess = await fetchLite(
153-
"https://registry.npmjs.org/node-fetch",
154-
undefined,
155-
{ Accept: "application/vnd.npm.install-v1+json" }
152+
"https://registry.npmjs.org/node-fetch"
156153
)
157154

158155
expect(fetchSuccess).toBeInstanceOf(Object)

src/Utils.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ export const cacheEnabled = (): boolean => true
125125
export const fetchLite = <T>(
126126
paramUrl: string,
127127
method?: "get" | "post" | undefined,
128-
paramHeaders?: Record<string, string> | undefined,
129128
body?: object | undefined
130129
): Promise<T | undefined> => {
131130
return new Promise<T | undefined>((resolve) => {
@@ -135,29 +134,26 @@ export const fetchLite = <T>(
135134
const thisReq = https.request(
136135
{ headers, hostname, method: method ?? "get", path },
137136
(response: IncomingMessage) => {
138-
const respondeBuffers: Buffer[] = []
137+
const responseBuffers: Buffer[] = []
139138

140-
response.on("data", (data: Buffer) => respondeBuffers.push(data))
139+
response.on("data", (data: Buffer) => responseBuffers.push(data))
141140
// istanbul ignore next
142141
response.on("error", () => resolve(undefined))
143-
response.on("end", () =>
144-
zlib.gunzip(Buffer.concat(respondeBuffers), (_error, contents) => {
145-
resolve(JSON.parse(contents.toString()))
146-
})
147-
)
142+
response.on("end", () => {
143+
return zlib.gunzip(
144+
Buffer.concat(responseBuffers),
145+
(_error, contents) => {
146+
resolve(JSON.parse(contents.toString()))
147+
}
148+
)
149+
})
148150
}
149151
)
150152

151153
thisReq.setHeader("Content-Type", "application/json")
152154
thisReq.setHeader("Content-Encoding", "gzip")
153155
thisReq.setHeader("Accept-Encoding", "gzip")
154156

155-
if (paramHeaders) {
156-
Object.entries(headers).forEach(([headerName, headerValue]) =>
157-
thisReq.setHeader(headerName, headerValue)
158-
)
159-
}
160-
161157
if (body !== undefined) {
162158
const bodyStringify = zlib.gzipSync(JSON.stringify(body))
163159

0 commit comments

Comments
 (0)