-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathhelpers.ts
More file actions
59 lines (50 loc) · 1.68 KB
/
helpers.ts
File metadata and controls
59 lines (50 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createHash } from "crypto";
import { type GetObjectCommandOutput } from "@aws-sdk/client-s3";
import { InternalServerError } from "./errors";
import { validRange } from "semver";
// Helper function to convert stream to string
export async function streamToString(stream: any): Promise<string> {
const chunks: Uint8Array[] = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
const result = Buffer.concat(chunks).toString("utf-8");
return result.trimEnd();
}
// Helper function to convert stream to buffer
export async function streamToBuffer(stream: any): Promise<Buffer> {
const chunks = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
return Buffer.concat(chunks);
}
export async function verifyHash(
file: GetObjectCommandOutput,
hashFile: GetObjectCommandOutput,
exception?: string,
): Promise<boolean> {
const content = await streamToBuffer(file.Body);
const remoteHash = await streamToString(hashFile.Body);
const localHash = createHash("sha256")
.update(new Uint8Array(content))
.digest("hex");
const matches = remoteHash.trim() === localHash;
if (!matches && exception) {
throw new InternalServerError(exception);
}
return matches;
}
export function toSemverRange(range?: string) {
if (!range) return "*";
return validRange(range) || "*";
}
/**
* Computes a deterministic rollout bucket (0-99) for a device ID.
* Used to decide if a device is eligible for a staged rollout.
*/
export function getDeviceRolloutBucket(deviceId: string): number {
const hash = createHash("md5").update(deviceId).digest("hex");
const hashPrefix = hash.substring(0, 8);
return parseInt(hashPrefix, 16) % 100;
}