Skip to content

Commit a73f688

Browse files
authored
feat: allow to specify maxSatisfying for /releases api (#42)
1 parent 3f13242 commit a73f688

File tree

5 files changed

+294
-188
lines changed

5 files changed

+294
-188
lines changed

package-lock.json

Lines changed: 31 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"helmet": "^7.1.0",
3434
"http-proxy-middleware": "^3.0.3",
3535
"jose": "^5.2.4",
36+
"lru-cache": "^11.2.2",
3637
"openid-client": "^5.6.5",
3738
"prisma": "^5.13.0",
3839
"semver": "^7.6.3",
@@ -44,7 +45,8 @@
4445
"bufferutil": "^4.0.8"
4546
},
4647
"devDependencies": {
47-
"prettier": "3.2.5",
48-
"@types/semver": "^7.5.8"
48+
"@types/lru-cache": "^7.10.9",
49+
"@types/semver": "^7.5.8",
50+
"prettier": "3.2.5"
4951
}
5052
}

src/helpers.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
import { createHash } from "crypto";
3+
import { type GetObjectCommandOutput } from "@aws-sdk/client-s3";
4+
import { InternalServerError } from "./errors";
5+
import { validRange } from "semver";
6+
7+
// Helper function to convert stream to string
8+
export async function streamToString(stream: any): Promise<string> {
9+
const chunks: Uint8Array[] = [];
10+
11+
for await (const chunk of stream) {
12+
chunks.push(chunk);
13+
}
14+
15+
const result = Buffer.concat(chunks).toString("utf-8");
16+
return result.trimEnd();
17+
}
18+
19+
// Helper function to convert stream to buffer
20+
export async function streamToBuffer(stream: any): Promise<Buffer> {
21+
const chunks = [];
22+
for await (const chunk of stream) {
23+
chunks.push(chunk);
24+
}
25+
return Buffer.concat(chunks);
26+
}
27+
28+
export async function verifyHash(
29+
file: GetObjectCommandOutput,
30+
hashFile: GetObjectCommandOutput,
31+
exception?: string,
32+
): Promise<boolean> {
33+
const content = await streamToBuffer(file.Body);
34+
const remoteHash = await streamToString(hashFile.Body);
35+
const localHash = createHash("sha256").update(content).digest("hex");
36+
37+
const matches = remoteHash.trim() === localHash;
38+
if (!matches && exception) {
39+
throw new InternalServerError(exception);
40+
}
41+
return matches;
42+
}
43+
44+
export function toSemverRange(range?: string) {
45+
if (!range) return "*";
46+
return validRange(range) || "*";
47+
}

0 commit comments

Comments
 (0)