Skip to content

Commit ac4d626

Browse files
author
Joel Denning
authored
Support for integrity in PATCH services endpoint (#172)
1 parent ebaa217 commit ac4d626

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,13 @@ Note that the `packageDirLevel` query param indicates the number of directories
424424

425425
Body:
426426

427+
_integrity field is optional_
428+
427429
```json
428430
{
429431
"service": "my-service",
430-
"url": "http://example.com/path/to/my-service.js"
432+
"url": "http://example.com/path/to/my-service.js",
433+
"integrity": "sha256-example"
431434
}
432435
```
433436

@@ -438,6 +441,9 @@ Response:
438441
"imports": {
439442
"my-service": "http://example.com/path/to/my-service.js",
440443
"my-service/": "http://example.com/path/to/"
444+
},
445+
"integrity": {
446+
"http://example.com/path/to/my-service.js": "sha256-example"
441447
}
442448
}
443449
```

src/modify.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ exports.modifyService = function (
132132
serviceName,
133133
url,
134134
remove,
135-
packageDirLevel = 1
135+
packageDirLevel = 1,
136+
integrity
136137
) {
137138
return modifyLock(env, (json) => {
138139
const map = getMapFromManifest(json);
@@ -158,6 +159,11 @@ exports.modifyService = function (
158159
).pathname;
159160
map[serviceName + "/"] = address;
160161
}
162+
163+
if (integrity) {
164+
json.integrity = json.integrity ?? {};
165+
json.integrity[url] = integrity;
166+
}
161167
}
162168
const alphabetical = !!getConfig().alphabetical;
163169
if (alphabetical) {

src/web-server.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function healthEndpoint(req, res) {
256256

257257
app.patch("/services", function (req, res) {
258258
req.body = JSON.parse(req.body);
259-
let service, url;
259+
let service, url, integrity;
260260
let env = getEnv(req);
261261
if (req.body != undefined && req.body.hasOwnProperty("service")) {
262262
service = req.body.service;
@@ -273,6 +273,17 @@ app.patch("/services", function (req, res) {
273273
} else {
274274
return res.status(400).send("url key is missing");
275275
}
276+
if (req.body?.hasOwnProperty("integrity")) {
277+
if (
278+
typeof req.body.integrity !== "string" ||
279+
req.body.integrity.trim().length === 0
280+
) {
281+
return res
282+
.status(400)
283+
.send(`Invalid integrity - must be a non-empty string`);
284+
}
285+
integrity = req.body.integrity;
286+
}
276287

277288
let packageDirLevel =
278289
req.query.packageDirLevel && req.query.packageDirLevel !== ""
@@ -296,7 +307,7 @@ app.patch("/services", function (req, res) {
296307
verifyValidUrl(req, url)
297308
.then(() => {
298309
modify
299-
.modifyService(env, service, url, false, packageDirLevel)
310+
.modifyService(env, service, url, false, packageDirLevel, integrity)
300311
.then((json) => {
301312
res.send(json);
302313
})

test/import-map.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ describe(`/import-map.json`, () => {
104104
});
105105
});
106106

107+
it(`patches services with integrity`, async () => {
108+
const integrity = "sha256-test";
109+
110+
const response = await request(app)
111+
.patch("/services")
112+
.query({
113+
skip_url_check: true,
114+
})
115+
.set("accept", "json")
116+
.send({
117+
service: "a",
118+
url: "/a-1-updated.mjs",
119+
integrity,
120+
})
121+
.expect(200)
122+
.expect("Content-Type", /json/);
123+
124+
expect(response.body.integrity["/a-1-updated.mjs"]).toBe(integrity);
125+
});
126+
107127
it(`does add trailing slash package`, async () => {
108128
const response = await request(app)
109129
.patch("/services")

0 commit comments

Comments
 (0)