Skip to content

Commit 60fd197

Browse files
committed
Make Stream class implement io.streams.OutputStream
1 parent 481cf91 commit 60fd197

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

src/main/php/com/amazon/aws/lambda/RuntimeApi.class.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ public function write($bytes) {
105105
$this->stream->flush();
106106
}
107107

108+
public function flush() {
109+
// All writes flush, NOOP
110+
}
111+
112+
public function close() {
113+
$this->end();
114+
}
115+
108116
public function end() {
109117
if ($this->response) return; // Already ended
110118

src/main/php/com/amazon/aws/lambda/Stream.class.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php namespace com\amazon\aws\lambda;
22

3-
interface Stream {
3+
use io\streams\OutputStream;
4+
5+
interface Stream extends OutputStream {
46

57
/**
68
* Transmits a given source to the output asynchronously.

src/main/php/xp/lambda/LocalRuntime.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function transmit($source, $mime= null) {
3535
public function use($mime) { /** NOOP */ }
3636
public function write($bytes) { $this->api->out->write($bytes); }
3737
public function end() { /** NOOP */ }
38+
public function flush() { /** NOOP */ }
39+
public function close() { /** NOOP */ }
3840
};
3941
$lambda($event, $stream, $context);
4042
}

src/test/php/com/amazon/aws/lambda/unittest/StreamingTest.class.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace com\amazon\aws\lambda\unittest;
22

33
use com\amazon\aws\lambda\{Context, Streaming};
4-
use io\streams\MemoryInputStream;
4+
use io\streams\{MemoryInputStream, StreamTransfer};
55
use lang\IllegalStateException;
66
use test\{Assert, Expect, Test, Values};
77

@@ -126,6 +126,26 @@ public function transmit() {
126126
);
127127
}
128128

129+
#[Test]
130+
public function can_be_used_as_output_stream() {
131+
$response= $this->invoke(function($event, $stream, $context) {
132+
$stream->use('application/json');
133+
(new StreamTransfer(new MemoryInputStream('{"test":true}'), $stream))->transferAll();
134+
});
135+
136+
Assert::equals(
137+
"POST /2018-06-01/runtime/invocation/3e1afeb0-cde4-1d0e-c3c0-66b15046bb88/response HTTP/1.1\r\n".
138+
"Connection: close\r\n".
139+
"Host: test\r\n".
140+
"Content-Type: application/json\r\n".
141+
"Lambda-Runtime-Function-Response-Mode: streaming\r\n".
142+
"Transfer-Encoding: chunked\r\n".
143+
"\r\n".
144+
"{\"test\":true}",
145+
$response
146+
);
147+
}
148+
129149
#[Test]
130150
public function invoke_handler_multiple_times() {
131151
$lambda= function($event, $stream, $context) { $stream->write('Test'); };

src/test/php/com/amazon/aws/lambda/unittest/TestStream.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ public function use($mime) { /** NOOP */ }
1111

1212
public function write($bytes) { $this->written.= $bytes; }
1313

14+
public function flush() { /** NOOP */ }
15+
16+
public function close() { /** NOOP */ }
17+
1418
public function end() { /** NOOP */ }
1519
}

0 commit comments

Comments
 (0)