-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { CONFIG } from "../config"; | ||
|
||
// returns the byte length of an utf8 string | ||
const byteLength = (str: string) => { | ||
var s = str.length; | ||
|
||
for (var i = str.length - 1; i >= 0; i--) { | ||
var code = str.charCodeAt(i); | ||
|
||
if (code > 0x7f && code <= 0x7ff) | ||
s++; | ||
else if (code > 0x7ff && code <= 0xffff) | ||
s += 2; | ||
if (code >= 0xDC00 && code <= 0xDFFF) | ||
i--; //trail surrogate | ||
} | ||
|
||
return s; | ||
} | ||
|
||
// Returns true if str byte size is ok, and false otherwise | ||
export const payloadSizeValidator = (str: string) => { | ||
if (str.length < CONFIG.PAYLOAD_LIMITS.MIN_STRING_LENGTH_POSSIBLE_OVERLOAD) | ||
return true; | ||
|
||
return byteLength(str) <= CONFIG.PAYLOAD_LIMITS.MAX_BYTE_SIZE; | ||
} |