-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpStreaming.class.php
executable file
·40 lines (33 loc) · 1.42 KB
/
HttpStreaming.class.php
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
<?php namespace com\amazon\aws\lambda;
use Throwable;
use web\{Error, InternalServerError, Request, Response};
/**
* AWS Lambda with AWS function URLs. Uses streaming as this has lower
* TTFB and memory consumption.
*
* @test com.amazon.aws.lambda.unittest.HttpIntegrationTest
* @see https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-features.html#gettingstarted-features-urls
*/
abstract class HttpStreaming extends HttpIntegration {
/** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */
public function target() {
$app= $this->application();
// Return event handler
return function($event, $stream, $context) use($app) {
$in= new FromApiGateway($event);
$req= new Request($in);
$res= new Response(new StreamingTo($stream));
try {
foreach ($app->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { }
$this->tracing->exchange($req, $res, $res->trace + ['traceId' => $context->traceId]);
$res->end();
} catch (Throwable $t) {
$this->tracing->exchange($req, $res, $res->trace + ['traceId' => $context->traceId, 'error' => $t]);
$e= $t instanceof Error ? $t : new InternalServerError($t);
$res->answer($e->status(), $e->getMessage());
$res->header('x-amzn-ErrorType', nameof($e));
$res->send($e->compoundMessage(), 'text/plain');
}
};
}
}