-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathhelpers.ts
More file actions
70 lines (59 loc) · 1.97 KB
/
Copy pathhelpers.ts
File metadata and controls
70 lines (59 loc) · 1.97 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
60
61
62
63
64
65
66
67
68
69
70
import { randomBytes, createHash } from "crypto";
import type { Context } from "@emulators/core";
import type { ContentfulStatusCode } from "@emulators/core";
const ACCOUNT_ID = "123456789012";
const DEFAULT_REGION = "us-east-1";
export function generateAwsId(prefix: string): string {
return prefix + randomBytes(8).toString("hex").toUpperCase();
}
export function generateMessageId(): string {
return [
randomBytes(4).toString("hex"),
randomBytes(2).toString("hex"),
randomBytes(2).toString("hex"),
randomBytes(2).toString("hex"),
randomBytes(6).toString("hex"),
].join("-");
}
export function generateReceiptHandle(): string {
return randomBytes(48).toString("base64url");
}
export function md5(content: string | Buffer | Uint8Array): string {
return createHash("md5").update(content).digest("hex");
}
export function getAccountId(): string {
return ACCOUNT_ID;
}
export function getDefaultRegion(): string {
return DEFAULT_REGION;
}
export function awsXmlResponse(c: Context, xml: string, status: ContentfulStatusCode = 200) {
return c.text(xml, status, { "Content-Type": "application/xml" });
}
export function awsErrorXml(c: Context, code: string, message: string, status: ContentfulStatusCode = 400) {
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<ErrorResponse>
<Error>
<Code>${escapeXml(code)}</Code>
<Message>${escapeXml(message)}</Message>
</Error>
<RequestId>${generateMessageId()}</RequestId>
</ErrorResponse>`;
return c.text(xml, status, { "Content-Type": "application/xml" });
}
export function escapeXml(str: string): string {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
export function parseQueryString(body: string): Record<string, string> {
const params = new URLSearchParams(body);
const result: Record<string, string> = {};
for (const [key, value] of params) {
result[key] = value;
}
return result;
}