2017 © Pedro Peláez
 

library byte-stream

A stream abstraction to make working with non-blocking I/O simple.

image

amphp/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  • Tuesday, July 24, 2018
  • by kelunik
  • Repository
  • 5 Watchers
  • 34 Stars
  • 60,370 Installations
  • PHP
  • 21 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 30 Versions
  • 27 % Grown

The README.md

amphp/byte-stream

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. amphp/byte-stream specifically provides a stream abstraction to ease working with various byte streams., (*1)

Installation

This package can be installed as a Composer dependency., (*2)

composer require amphp/byte-stream

Requirements

This package requires PHP 8.1 or later., (*3)

Usage

Streams are an abstraction over ordered sequences of bytes. This package provides the fundamental interfaces ReadableStream and WritableStream., (*4)

Note Previous versions used the terms InputStream and OutputStream, but these terms can be confusing depending on the use case., (*5)

ReadableStream

ReadableStream offers a primary method: read(). It returns a string or null. null indicates that the stream has ended., (*6)

The following example shows a ReadableStream consumption that buffers the complete stream contents., (*7)

$stream = ...;
$buffer = "";

while (($chunk = $stream->read()) !== null) {
    $buffer .= $chunk;
}

// do something with $buffer

Note Amp\ByteStream\buffer($stream) can be used instead, but we'd like to demonstrate manual consumption here., (*8)

This package offers some basic implementations, other libraries might provide even more implementations, such as amphp/socket., (*9)

Payload

Payload implements ReadableStream while also providing a buffer() method for buffering the entire contents. This allows consuming a message either in chunks (streaming) or consume everything at once (buffering). When the object is destructed, any remaining data in the stream is automatically consumed and discarded. This class is useful for small payloads or when the entire contents of a stream is needed before any processing can be done., (*10)

Buffering

Buffering a complete readable stream can be accomplished using the buffer() method., (*11)

$payload = new Payload($inputStream);
$content = $payload->buffer();

Streaming

Sometimes it's useful / possible to consume a payload in chunks rather than first buffering it completely, e.g. streaming a large HTTP response body directly to disk., (*12)

while (null !== $chunk = $payload->read()) {
    // Use $chunk here, works just like any other ReadableStream
}

ReadableBuffer

An ReadableBuffer allows creating a ReadableStream from a single known string chunk. This is helpful if the complete stream contents are already known., (*13)

$stream = new ReadableBuffer("foobar");

It also allows creating a stream without any chunks by passing null as chunk / omitting the constructor argument:, (*14)

$stream = new ReadableBuffer;

// The stream ends immediately
assert(null === $stream->read());

ReadableIterableStream

ReadableIterableStream allows converting an iterable that yields strings into a ReadableStream:, (*15)

$inputStream = new Amp\ByteStream\ReadableIterableStream((function () {
    for ($i = 0; $i < 10; $i++) {
        Amp\delay(1);
        yield $emit(".");
    }
})());

ReadableResourceStream

This package abstracts PHP's stream resources with ReadableResourceStream and WritableResourceStream. They automatically set the passed resource to non-blocking mode and allow reading and writing like any other ReadableStream / WritableStream. They also handle backpressure automatically by disabling the read watcher in case there's no read request and only activate a writability watcher if the underlying write buffer is already full, which makes them very efficient., (*16)

DecompressingReadableStream

This package implements compression based on Zlib. CompressingWritableStream can be used for compression, while DecompressingReadableStream can be used for decompression. Both can simply wrap an existing stream to apply them. Both accept an $encoding and $options parameter in their constructor., (*17)

$readableStream = new ReadableResourceStream(STDIN);
$decompressingReadableStream = new DecompressingReadableStream($readableStream, \ZLIB_ENCODING_GZIP);

while (null !== $chunk = $decompressingReadableStream) {
    print $chunk;
}

See also: ./examples/gzip-decompress.php, (*18)

WritableStream

WritableStream offers two primary methods: write() and end()., (*19)

WritableStream::write

write() writes the given string to the stream. Waiting for completion allows writing only as fast as the underlying stream can write and potentially send over a network. TCP streams will return immediately as long as the write buffer isn't full., (*20)

The writing order is always ensured, even if the writer doesn't wait for completion before issuing another write., (*21)

WritableStream::end

end() marks the stream as ended. TCP streams might close the underlying stream for writing, but MUST NOT close it. Instead, all resources should be freed and actual resource handles be closed by PHP's garbage collection process., (*22)

The following example uses the previous example to read from a stream and writes all data to a WritableStream:, (*23)

$readableStream = ...;
$writableStream = ...;
$buffer = "";

while (($chunk = $readableStream->read()) !== null) {
    $writableStream->write($chunk);
}

$writableStream->end();

Note Amp\ByteStream\pipe($readableStream, $writableStream) can be used instead, but we'd like to demonstrate manual consumption / writing here., (*24)

This package offers some basic implementations, other libraries might provide even more implementations, such as amphp/socket., (*25)

WritableResourceStream

This package abstracts PHP's stream resources with ReadableResourceStream and WritableResourceStream. They automatically set the passed resource to non-blocking mode and allow reading and writing like any other ReadableStream / WritableStream. They also handle backpressure automatically by disabling the read watcher in case there's no read request and only activate a writability watcher if the underlying write buffer is already full, which makes them very efficient., (*26)

CompressingWritableStream

This package implements compression based on Zlib. CompressingWritableStream can be used for compression, while DecompressingReadableStream can be used for decompression. Both can simply wrap an existing stream to apply them. Both accept an $encoding and $options parameter in their constructor., (*27)

$writableStream = new WritableResourceStream(STDOUT);
$compressedWritableStream = new CompressingWritableStream($writableStream, \ZLIB_ENCODING_GZIP);

for ($i = 0; $i < 100; $i++) {
    $compressedWritableStream->write(bin2hex(random_bytes(32));
}

$compressedWritableStream->end();

See also: ./examples/gzip-compress.php, (*28)

Versioning

amphp/byte-stream follows the semver semantic versioning specification like all other amphp packages., (*29)

Security

If you discover any security related issues, please email me@kelunik.com instead of using the issue tracker., (*30)

License

The MIT License (MIT). Please see LICENSE for more information., (*31)

The Versions

24/07 2018

dev-ext-async

dev-ext-async http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

16/07 2018

dev-segfault-2018-07-16

dev-segfault-2018-07-16 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

23/05 2018

dev-large-chunks

dev-large-chunks http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

16/05 2018

dev-master

9999999-dev http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

04/04 2018

v1.3.1

1.3.1.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

04/04 2018

dev-fix-40

dev-fix-40 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

20/03 2018

dev-ext-fiber

dev-ext-fiber http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

13/03 2018

v1.3.0

1.3.0.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

11/03 2018

v1.2.5

1.2.5.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

09/03 2018

v1.2.4

1.2.4.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

08/03 2018

v1.2.3

1.2.3.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

12/01 2018

v1.2.2

1.2.2.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

12/01 2018

dev-stdout-stderr

dev-stdout-stderr http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

22/12 2017

dev-message-onresolve-deprecation

dev-message-onresolve-deprecation http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

10/12 2017

v1.2.1

1.2.1.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

05/12 2017

v1.2.0

1.2.0.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

17/10 2017

v1.1.6

1.1.6.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

08/10 2017

v1.1.5

1.1.5.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

07/10 2017

v1.1.4

1.1.4.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

06/10 2017

dev-fix-artax-issue-138

dev-fix-artax-issue-138 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

15/09 2017

v1.1.3

1.1.3.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

15/07 2017

v1.1.2

1.1.2.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

28/06 2017

dev-issue-18

dev-issue-18 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

28/06 2017

v1.1.1

1.1.1.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

23/06 2017

v1.1.0

1.1.0.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

19/06 2017

v1.0.3

1.0.3.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

19/06 2017

v1.0.2

1.0.2.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

17/06 2017

v1.0.1

1.0.1.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

15/06 2017

v1.0.0

1.0.0.0 http://amphp.org/byte-stream

A stream abstraction to make working with non-blocking I/O simple.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp io amphp

05/05 2017

dev-end-only-writable

dev-end-only-writable https://github.com/amphp/byte-stream

Async streams for the amp concurrency framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Aaron Piotrowski

async non-blocking stream amp