Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit dda7511

Browse files
fredemmottusox
authored andcommitted
Support (and require) hsl-experimental 4.52
Upstream changes: - blocking functions renamed - readAsync/writeAsync no longer deal with the whole thing Other changes made here: - do a chunked copy instead of using readAll/writeAll to reduce peak memory - that means we're making multiple calls with different return values, which I don't see how to mock. Use a pipe handle instead of a mock in the unit test.
1 parent 745189a commit dda7511

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
}
1414
],
1515
"require": {
16-
"hhvm": "^4.1",
16+
"hhvm": "^4.52",
1717
"facebook/hack-http-request-response-interfaces": "^0.3",
1818
"hhvm/hsl": "^4.25",
19-
"hhvm/hsl-experimental": "^4.50",
19+
"hhvm/hsl-experimental": "^4.52",
2020
"hhvm/type-assert": "^3.3",
2121
"usox/hack-http-factory-interfaces": "^0.2"
2222
},

src/UploadedFile.hack

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ final class UploadedFile implements Message\UploadedFileInterface {
5252

5353
private async function writeAsync(string $target_path): Awaitable<void> {
5454
await using $target = File\open_write_only($target_path);
55-
await $target->writeAsync($this->stream->rawReadBlocking());
55+
// Doing this a chunk at a time instead of using `readAllAsync()` to reduce
56+
// peak memory usage
57+
do {
58+
/* HHAST_IGNORE_ERROR[DontAwaitInALoop] */
59+
$chunk = await $this->stream->readAsync();
60+
if ($chunk === '') {
61+
break;
62+
}
63+
/* HHAST_IGNORE_ERROR[DontAwaitInALoop] */
64+
await $target->writeAsync($chunk);
65+
} while (true);
5666
if ($this->stream is IO\CloseableHandle) {
5767
await $this->stream->closeAsync();
5868
}

tests/UploadedFileTest.hack

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Usox\HackTTP;
1111

1212
use namespace Facebook\Experimental\Http\Message;
13-
use namespace HH\Lib\{File, IO};
13+
use namespace HH\Lib\{File, IO, OS};
1414
use type Facebook\HackTest\HackTest;
1515
use function Facebook\FBExpect\expect;
1616
use function Usox\HackMock\{mock, prospect};
@@ -85,28 +85,27 @@ class UploadedFileTest extends HackTest {
8585
);
8686
}
8787

88-
public function testMoveToMovesFile(): void {
88+
public async function testMoveToMovesFile(): Awaitable<void> {
8989
$filename = \sys_get_temp_dir().'/'.\bin2hex(\random_bytes(16));
9090
$content = 'some-content';
91-
$read_handle = mock(IO\ReadHandle::class);
92-
93-
prospect($read_handle, 'rawReadBlocking')
94-
->with(null)
95-
->once()
96-
->andReturn($content);
97-
prospect($read_handle, 'closeAsync')
98-
->once();
91+
list($read_handle, $write_handle) = IO\pipe_nd();
92+
$write_handle->write($content);
93+
await $write_handle->closeAsync();
9994

10095
$file = new UploadedFile(
10196
$read_handle,
10297
666
10398
);
10499

105100
$file->moveTo($filename);
101+
$ex = expect(async () ==> await $read_handle->closeAsync())->toThrow(
102+
OS\ErrnoException::class,
103+
);
104+
expect($ex->getErrno())->toEqual(OS\Errno::EBADF);
106105

107106
$target_file = File\open_read_only_nd($filename);
108107

109-
expect($target_file->rawReadBlocking())
108+
expect($target_file->read())
110109
->toBeSame($content);
111110

112111
// after moving the file, the readhandle becomes invalid

0 commit comments

Comments
 (0)