Skip to content

Commit 74de6d8

Browse files
committed
refactor: ensure no content stream is correct
1 parent df82c1a commit 74de6d8

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Diff for: packages/core/index.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,12 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {}) => {
6868
if (handlerBody._readableState) {
6969
handlerStream = handlerBody
7070
} else if (typeof handlerBody === 'string') {
71-
function * iterator (input) {
72-
const size = 16384 // 16 * 1024 // Node.js default
73-
let position = 0
74-
const length = input.length
75-
while (position <= length) {
76-
yield input.substring(position, position + size)
77-
position += size
78-
}
79-
}
80-
handlerStream = Readable.from(iterator(handlerBody))
71+
// #1189
72+
handlerStream = Readable.from(
73+
handlerBody.length < stringIteratorSize
74+
? handlerBody
75+
: stringIterator(handlerBody)
76+
)
8177
}
8278

8379
if (!handlerStream) {
@@ -129,6 +125,16 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {}) => {
129125
return middy
130126
}
131127

128+
const stringIteratorSize = 16384 // 16 * 1024 // Node.js default
129+
function * stringIterator (input) {
130+
let position = 0
131+
const length = input.length
132+
while (position < length) {
133+
yield input.substring(position, position + stringIteratorSize)
134+
position += stringIteratorSize
135+
}
136+
}
137+
132138
// shared AbortController, because it's slow
133139
let handlerAbort = new AbortController()
134140
const runRequest = async (

0 commit comments

Comments
 (0)