-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathensure-payload-size.ts
More file actions
141 lines (119 loc) · 3.33 KB
/
Copy pathensure-payload-size.ts
File metadata and controls
141 lines (119 loc) · 3.33 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Buffer } from 'node:buffer';
import { JsonStreamStringify } from 'json-stream-stringify';
// This specifies which keys of an event payload to potentially redact
// if they are too big
const KEYS_TO_VERIFY = ['state', 'final_state', 'log'];
const replacements: Record<string, any> = {
log: {
message: ['[REDACTED: Message length exceeds payload limit]'],
},
default: {
data: '[REDACTED]',
},
};
export const verify = async (
value: any,
limit_mb: number = 10,
algo: 'stringify' | 'stream' = 'stringify'
) => {
if (value && !isNaN(limit_mb)) {
const limitBytes = limit_mb * 1024 * 1024;
let sizeBytes: number;
if (algo === 'stream') {
sizeBytes = await calculateSizeStream(value, limitBytes);
} else {
sizeBytes = await calculateSizeStringify(value);
}
if (sizeBytes > limitBytes) {
const e = new Error();
// @ts-ignore
e.name = 'PAYLOAD_TOO_LARGE';
e.message = `The payload exceeded the size limit of ${limit_mb}mb`;
throw e;
}
}
};
export const calculateSizeStringify = (value: any): Promise<number> => {
return new Promise((resolve) => {
const str = typeof value === 'string' ? value : JSON.stringify(value);
const size_bytes = Buffer.byteLength(str, 'utf8');
setTimeout(() => resolve(size_bytes), 10);
return size_bytes;
});
};
export const calculateSizeStream = async (
value: any,
limit?: number
): Promise<number> => {
// skip all primitives
if (
!value ||
typeof value === 'number' ||
typeof value === 'boolean' ||
typeof value === 'function'
) {
// Treat as size 0
return 0;
}
let size_bytes = 0;
const stream = new JsonStreamStringify(value);
for await (const chunk of stream) {
// Each chunk is a string token from the JSON output
size_bytes += Buffer.byteLength(chunk, 'utf8');
if (limit !== undefined && size_bytes > limit) {
break;
}
}
stream.destroy();
return size_bytes;
};
export default async (payload: any, limit_mb: number = 10) => {
if (!limit_mb || isNaN(limit_mb)) {
return payload;
}
const newPayload = { ...payload };
for (const key of KEYS_TO_VERIFY) {
if (key in payload) {
try {
await verify(payload[key], limit_mb, 'stringify');
} catch (e: any) {
if (e.name === 'PAYLOAD_TOO_LARGE') {
Object.assign(
newPayload[key],
replacements[key] ?? replacements.default
);
newPayload.redacted = true;
} else {
console.log(e);
}
}
}
}
return newPayload;
};
// export default async (payload: any, limit_mb: number = 10) => {
// return new Promise(async (resolve) => {
// if (!limit_mb || isNaN(limit_mb)) {
// resolve( payload);
// }
// const newPayload = { ...payload };
// for (const key of KEYS_TO_VERIFY) {
// if (key in payload) {
// try {
// await verify(payload[key], limit_mb, 'stream');
// } catch (e) {
// if (e.name === 'PAYLOAD_TOO_LARGE') {
// Object.assign(
// newPayload[key],
// replacements[key] ?? replacements.default
// );
// newPayload.redacted = true;
// }
// }
// }
// }
// setTimeout(() => {
// resolve(newPayload)
// }, 1000)
// })
// };