|
1 | 1 | <?php namespace com\amazon\aws\lambda; |
2 | 2 |
|
3 | 3 | use Throwable as Any; |
4 | | -use lang\Throwable; |
| 4 | +use io\Channel; |
| 5 | +use io\streams\InputStream; |
| 6 | +use lang\{Throwable, IllegalStateException, IllegalArgumentException}; |
5 | 7 | use peer\http\{HttpConnection, HttpRequest, RequestData}; |
6 | 8 | use text\json\Json; |
7 | 9 |
|
|
11 | 13 | * @test com.amazon.aws.lambda.unittest.RuntimeApiTest |
12 | 14 | * @test com.amazon.aws.lambda.unittest.ExceptionTest |
13 | 15 | * @test com.amazon.aws.lambda.unittest.BufferedTest |
| 16 | + * @test com.amazon.aws.lambda.unittest.StreamedTest |
14 | 17 | * @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html |
15 | 18 | */ |
16 | 19 | class RuntimeApi { |
@@ -43,7 +46,87 @@ public function invoke($lambda, $event, $context) { |
43 | 46 | } |
44 | 47 |
|
45 | 48 | /** Returns the streaming invoke mode */ |
46 | | - public function streaming(): InvokeMode { return new Streaming($this); } |
| 49 | + public function streaming(): InvokeMode { |
| 50 | + return new class($this) extends InvokeMode implements Stream { |
| 51 | + private $request= null; |
| 52 | + private $response= null; |
| 53 | + private $stream= null; |
| 54 | + |
| 55 | + private function start() { |
| 56 | + $this->request->setHeader('Lambda-Runtime-Function-Response-Mode', 'streaming'); |
| 57 | + $this->request->setHeader('Transfer-Encoding', 'chunked'); |
| 58 | + return $this->api->stream($this->request); |
| 59 | + } |
| 60 | + |
| 61 | + public function transmit($source, $mimeType= null) { |
| 62 | + if ($this->response) throw new IllegalStateException('Streaming ended'); |
| 63 | + |
| 64 | + if ($source instanceof InputStream) { |
| 65 | + $in= $source; |
| 66 | + } else if ($source instanceof Channel) { |
| 67 | + $in= $source->in(); |
| 68 | + } else { |
| 69 | + throw new IllegalArgumentException('Expected either a channel or an input stream, have '.typeof($source)); |
| 70 | + } |
| 71 | + |
| 72 | + if (null !== $mimeType) { |
| 73 | + $this->request->setHeader('Content-Type', $mimeType); |
| 74 | + } |
| 75 | + |
| 76 | + $this->stream ?? $this->stream= $this->start(); |
| 77 | + try { |
| 78 | + while ($in->available()) { |
| 79 | + $this->stream->write($in->read()); |
| 80 | + $this->stream->flush(); |
| 81 | + } |
| 82 | + } finally { |
| 83 | + $in->close(); |
| 84 | + $this->end(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public function use($mimeType) { |
| 89 | + if ($this->response) throw new IllegalStateException('Streaming ended'); |
| 90 | + |
| 91 | + $this->request->setHeader('Content-Type', $mimeType); |
| 92 | + } |
| 93 | + |
| 94 | + public function write($bytes) { |
| 95 | + if ($this->response) throw new IllegalStateException('Streaming ended'); |
| 96 | + |
| 97 | + $this->stream ?? $this->stream= $this->start(); |
| 98 | + $this->stream->write($bytes); |
| 99 | + $this->stream->flush(); |
| 100 | + } |
| 101 | + |
| 102 | + public function end() { |
| 103 | + if ($this->response) return; // Already ended |
| 104 | + |
| 105 | + $this->stream ?? $this->stream= $this->start(); |
| 106 | + $this->response= $this->api->finish($this->stream); |
| 107 | + $this->response->closeStream(); |
| 108 | + } |
| 109 | + |
| 110 | + public function invoke($lambda, $event, $context) { |
| 111 | + try { |
| 112 | + $this->request= $this->api->request("invocation/{$context->awsRequestId}/response"); |
| 113 | + $lambda($event, $context, $this); |
| 114 | + $this->end(); |
| 115 | + return $this->response; |
| 116 | + } catch (Throwable $t) { |
| 117 | + |
| 118 | + // We can only report errors before starting to stream. |
| 119 | + if (null === $this->stream) { |
| 120 | + return $this->api->report("invocation/{$context->awsRequestId}/error", $t); |
| 121 | + } |
| 122 | + |
| 123 | + // TODO: Use HTTP trailers to report back errors |
| 124 | + $this->end(); |
| 125 | + throw $t; |
| 126 | + } |
| 127 | + } |
| 128 | + }; |
| 129 | + } |
47 | 130 |
|
48 | 131 | /** |
49 | 132 | * Marshals an exception according to the AWS specification. |
|
0 commit comments