Comments (3)
Directly to Function URL or through API Gateway?
Empty body cause the Lambda to respond with 200 instead if 301
This is an AWS issue, Middy doesn't override statusCodes within core
. Please open a support ticket with AWS, and point them here.
If AWS signals that they won't fix, we could change https://github.com/middyjs/middy/blob/main/packages/core/index.js#L58 to default to
as a workaround. I'll have to think on If it should always or only when statusCode is 204, 300 - 399, or others.
PS I recall 3xx statusCodes used to return 500s when it was first released. So, they may be willing to fix.
from middy.
@willfarrell Thanks for checking. Itβs through a Function URL. Iβll raise a case a AWS and then update this issue with the response.
from middy.
While working on the AWS case, I dug into the middy code more closely. I think I found a potential problem in how middy handles strings with no content. Basically, when there's no content in the iterator generator does not yield and then the pipeline then does not process the HttpResponseStream. As far as I can see.
To fix this, we can make a simple change. By ensuring the generator always yields content (even if it's just once with no content), we can resolve the issue.
use while (position <= length)
instead of while (position < length)
.
Here's a sample code without middy, but with the algorithm to generate a string response in a stream-like manner.
import util from 'util';
import stream from 'stream';
const {Readable} = stream;
const pipeline = util.promisify(stream.pipeline);
export const handler = awslambda.streamifyResponse(async (event, responseStream, _context) => {
responseStream = awslambda.HttpResponseStream.from(responseStream, {
statusCode: 301, headers: {
location: 'https://example.com'
}
});
let handlerBody = null;
handlerBody = handlerBody ?? ''
// Source @datastream/core (MIT)
let handlerStream
if (handlerBody._readableState) {
handlerStream = handlerBody
console.log('its a stream')
} else if (typeof handlerBody === 'string') {
function* iterator(input) {
const size = 16384 // 16 * 1024 // Node.js default
let position = 0
const length = input.length
while (position <= length) {
yield input.substring(position, position + size)
position += size
}
}
console.log('its a string')
handlerStream = Readable.from(iterator(handlerBody))
}
if (!handlerStream) {
throw new Error('handler response not a ReadableStream')
}
await pipeline(handlerStream, responseStream);
});
I had some trouble setting up ava tests on my computer, so I couldn't create a pull request yet. π
from middy.
Related Issues (20)
- Please give more descriptive error when throwing "Failed to resolve internal values" in secrets-manager module HOT 2
- middy/http-cors disable defaults and don't add headers HOT 1
- `http-error-handler` to support Problem Details (RFC 9457) HOT 1
- http-error-handler : set custom test to return error instead of `statusCode` HOT 2
- http-error-handler: guidance/bug on using lambda-authorizer with v5 HOT 1
- error-logger: Call implementation function with await prefix HOT 1
- ApproximateReceiveCount limit on SqsPartialBatch HOT 5
- Conditional middleware HOT 3
- (Aware of other issues) No "exports" main defined in /var/task/node_modules/@middy/core/package.json HOT 7
- input-output-logger: replacer function is not called HOT 3
- Error: No "exports" main defined in /path/to/the/repo/node_modules/@middy/core/package.json HOT 1
- @middy/http-content-encoding not awaiting onError HOT 1
- Consistent multipart body parser response format HOT 1
- β No "exports" main defined in ***i/node_modules/@middy/core/package.json
- `ws-response`: Invalid URL format gave to `ApiGatewayManagementApiClient` HOT 4
- `ws-response`: Invalid `request.response` given to API Gateway HOT 1
- Latest version of @middy/core breaks types in the handler function HOT 9
- TypeScript unable to infer types HOT 4
- TypeScript unable to infer types HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from middy.