Repository navigation
Expand file tree
/
Copy pathcommon.ts
More file actions
51 lines (43 loc) · 1.58 KB
/
Copy pathcommon.ts
File metadata and controls
51 lines (43 loc) · 1.58 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
const streamToArray = require("stream-to-array");
const crypto = require("crypto");
import { Readable } from "stream";
function toSnakeCase(str: string): string {
return str
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
.replace(/([a-z])([A-Z])/g, "$1_$2")
.toLowerCase();
}
export function convertObjectToSnakeCase(obj: any): any {
if (typeof obj !== "object" || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((item: any) => convertObjectToSnakeCase(item));
}
return Object.keys(obj).reduce((acc, key) => {
const snakeCaseKey: string = toSnakeCase(key);
acc[snakeCaseKey] = convertObjectToSnakeCase(obj[key]);
return acc;
}, {} as any);
}
// Resolves the Buffer itself rather than its `.buffer`: Node carves small Buffers out of a shared pool, so the backing
// ArrayBuffer of one under 4 KB holds other allocations too, and uploading it stored 8 KB of unrelated bytes.
export async function streamToBuffer(readableStream: Readable): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
streamToArray(readableStream, (err: Error | null, arr: Array<Buffer | Uint8Array>) => {
if (err) {
reject(err);
} else {
const buffers = arr.map((chunk) => (Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
resolve(Buffer.concat(buffers));
}
});
});
}
export function hashWithSHA256(input: string): string {
const hash = crypto.createHash("sha256");
hash.update(input);
return hash.digest("hex");
}