Skip to content

Commit

Permalink
fix(npm-dist-tag): Improve robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Jan 5, 2019
1 parent 6c0d1d8 commit 63a7a89
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
20 changes: 17 additions & 3 deletions utils/npm-dist-tag/__tests__/npm-dist-tag.test.js
Expand Up @@ -29,7 +29,7 @@ describe("npmDistTag.add()", () => {
"added-tag": "1.0.1",
});
expect(fetch).toHaveBeenLastCalledWith(
"-/package/@scope%2fsome-pkg/dist-tags/added-tag",
"/-/package/@scope%2fsome-pkg/dist-tags/added-tag",
expect.figgyPudding({
method: "PUT",
body: JSON.stringify("1.0.1"),
Expand Down Expand Up @@ -92,7 +92,7 @@ describe("npmDistTag.remove()", () => {

expect(tags).not.toHaveProperty("removed-tag");
expect(fetch).toHaveBeenLastCalledWith(
"-/package/@scope%2fsome-pkg/dist-tags/removed-tag",
"/-/package/@scope%2fsome-pkg/dist-tags/removed-tag",
expect.figgyPudding({
method: "DELETE",
})
Expand All @@ -118,6 +118,7 @@ describe("npmDistTag.list()", () => {
Promise.resolve({
latest: "1.0.0",
"other-tag": "1.0.1",
_etag: "should-be-removed",
})
);

Expand All @@ -129,12 +130,25 @@ describe("npmDistTag.list()", () => {
"other-tag": "1.0.1",
});
expect(fetch.json).toHaveBeenLastCalledWith(
"-/package/@scope%2fsome-pkg/dist-tags",
"/-/package/@scope%2fsome-pkg/dist-tags",
expect.figgyPudding({
"prefer-online": true,
spec: expect.objectContaining({
name: "@scope/some-pkg",
}),
})
);
});

it("handles disastrous results gracefully", async () => {
fetch.json.mockImplementationOnce(() =>
// i mean, wut
Promise.resolve(null)
);

const opts = new Map(baseOptions);
const tags = await npmDistTag.list("@scope/some-pkg", opts);

expect(tags).toEqual({});
});
});
24 changes: 19 additions & 5 deletions utils/npm-dist-tag/npm-dist-tag.js
Expand Up @@ -40,7 +40,7 @@ function add(spec, tag, _opts) {
return tags;
}

const uri = `-/package/${opts.spec.escapedName}/dist-tags/${cleanTag}`;
const uri = `/-/package/${opts.spec.escapedName}/dist-tags/${encodeURIComponent(cleanTag)}`;
const payload = opts.concat({
method: "PUT",
body: JSON.stringify(version),
Expand All @@ -49,6 +49,7 @@ function add(spec, tag, _opts) {
// so we manually set the required content-type
"content-type": "application/json",
},
spec: opts.spec,
});

// success returns HTTP 204, thus no JSON to parse
Expand Down Expand Up @@ -78,7 +79,7 @@ function remove(spec, tag, _opts) {
return tags;
}

const uri = `-/package/${opts.spec.escapedName}/dist-tags/${tag}`;
const uri = `/-/package/${opts.spec.escapedName}/dist-tags/${encodeURIComponent(tag)}`;
const payload = opts.concat({
method: "DELETE",
});
Expand All @@ -104,7 +105,20 @@ function list(spec, _opts) {
}

function fetchTags(opts) {
const uri = `-/package/${opts.spec.escapedName}/dist-tags`;

return fetch.json(uri, opts);
return fetch
.json(
`/-/package/${opts.spec.escapedName}/dist-tags`,
opts.concat({
"prefer-online": true,
spec: opts.spec,
})
)
.then(data => {
if (data && typeof data === "object") {
// eslint-disable-next-line no-param-reassign, no-underscore-dangle
delete data._etag;
}

return data || {};
});
}

0 comments on commit 63a7a89

Please sign in to comment.