-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: allow a limit on payload size
- Loading branch information
1 parent
5c72cb6
commit 116a554
Showing
10 changed files
with
193 additions
and
21 deletions.
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
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
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,16 @@ | ||
export default (payload: string, limit_mb?: number) => { | ||
// @ts-ignore | ||
if (!isNaN(limit_mb)) { | ||
const limit = limit_mb as number; | ||
const size_bytes = Buffer.byteLength(payload, 'utf8'); | ||
const size_mb = size_bytes / 1024 / 1024; | ||
if (size_mb > limit) { | ||
const e = new Error(); | ||
// @ts-ignore | ||
e.severity = 'kill'; | ||
e.name = 'PAYLOAD_TOO_LARGE'; | ||
e.message = `The payload exceeded the size limit of ${limit}mb`; | ||
throw e; | ||
} | ||
} | ||
}; |
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,52 @@ | ||
import test from 'ava'; | ||
import ensurePayloadSize from '../../src/util/ensure-payload-size'; | ||
|
||
const mb = (bytes: number) => bytes / 1024 / 1024; | ||
|
||
test('throw limit 0, payload 1 byte', (t) => { | ||
t.throws(() => ensurePayloadSize('x', 0), { | ||
name: 'PAYLOAD_TOO_LARGE', | ||
}); | ||
}); | ||
|
||
test('ok for limit 1byte, payload 1 byte', (t) => { | ||
t.notThrows(() => ensurePayloadSize('x', mb(1))); | ||
}); | ||
|
||
test('throw for limit 1byte, payload 2 bytes', (t) => { | ||
t.throws(() => ensurePayloadSize('xy', mb(1)), { | ||
name: 'PAYLOAD_TOO_LARGE', | ||
}); | ||
}); | ||
|
||
test('ok for short string, limit 1mb', (t) => { | ||
t.notThrows(() => ensurePayloadSize('hello world', 1)); | ||
}); | ||
|
||
test('ok for 1mb string, limit 1mb', (t) => { | ||
const str = new Array(1024 * 1024).fill('z').join(''); | ||
t.notThrows(() => ensurePayloadSize(str, 1)); | ||
}); | ||
|
||
test('throw for 1mb string + 1 byte, limit 1mb', (t) => { | ||
const str = new Array(1024 * 1024 + 1).fill('z').join(''); | ||
t.throws(() => ensurePayloadSize(str, 1), { | ||
name: 'PAYLOAD_TOO_LARGE', | ||
}); | ||
}); | ||
|
||
test('ok if no limit', (t) => { | ||
const str = new Array(1024 * 1024 + 1).fill('z').join(''); | ||
t.notThrows(() => ensurePayloadSize(str)); | ||
}); | ||
|
||
test('error shape', (t) => { | ||
try { | ||
const str = new Array(1024 * 1024 + 1).fill('z').join(''); | ||
ensurePayloadSize(str, 1); | ||
} catch (e: any) { | ||
t.is(e.severity, 'kill'); | ||
t.is(e.name, 'PAYLOAD_TOO_LARGE'); | ||
t.is(e.message, 'The payload exceeded the size limit of 1mb'); | ||
} | ||
}); |