Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,11 @@ dump(
$nc->drain();
```

- An empty object (`''` or an empty reader) is stored as meta without chunks.
- If the reader or a publish fails in the middle of `put()`, the chunks already published are purged and the error is rethrown; the previous version of the object stays as it was.
- `get()` reads the chunks through a temporary consumer, deleted after the object is read to the end. It throws `Nats\Exception\ObjectIsInvalid` at once if the stream holds fewer chunks than the meta tells. The size and the SHA-256 digest of the chunks are checked against the meta: on a mismatch, or when the chunks disappear while reading, the read of the stream throws `Nats\Exception\ObjectIsInvalid`.
- `info()->mtime` is the time the server stored the meta, with sub-second precision.

#### Watch Object Store

```php
Expand Down
105 changes: 105 additions & 0 deletions src/JetStream/ObjectStore/Internal/ChunkCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Thesis\Nats\JetStream\ObjectStore\Internal;

use Amp\Cancellation;
use Amp\DeferredCancellation;
use Amp\Pipeline\Queue;
use Thesis\Nats\Exception\ObjectIsInvalid;
use Thesis\Nats\JetStream\Delivery;
use Thesis\Nats\JetStream\Internal\IdleListener;
use Thesis\Nats\JetStream\ObjectStore\ObjectInfo;
use Thesis\Nats\Subscription;

/**
* Passes the chunks of an object to its reader and verifies the size and the digest once the last chunk arrives.
*
* @internal
*/
final class ChunkCollector implements IdleListener
{
private readonly DigestCalculator $digest;

private readonly DeferredCancellation $exhausted;

/** @var non-negative-int */
private int $size = 0;

/** @var ?string the binary digest of the received chunks, known once the last chunk arrived */
private ?string $checksum = null;

/**
* @param Queue<string> $chunks
*/
public function __construct(
private readonly ObjectInfo $info,
private readonly Queue $chunks,
) {
$this->digest = DigestCalculator::sha256();
$this->exhausted = new DeferredCancellation();
}

/**
* Cancelled when the server has nothing more to deliver before the last chunk: the chunks are gone.
*/
public function exhausted(): Cancellation
{
return $this->exhausted->getCancellation();
}

public function idle(): void
{
if ($this->checksum === null) {
$this->exhausted->cancel();
}
}

public function __invoke(Delivery $delivery, Subscription $subscription): void
{
if ($this->checksum !== null) {
return;
}

// The reader dropped the object before reading it to the end: nobody needs the rest.
if ($this->chunks->isDisposed()) {
$subscription->stop();

return;
}

$payload = $delivery->message->payload ?? '';

if ($payload !== '') {
$this->digest->update($payload);
$this->size += \strlen($payload);

$pushed = $this->chunks->pushAsync($payload);
$pushed->ignore();
}

if ($delivery->metadata?->pending === 0) {
$this->checksum = $this->digest->finish();
$subscription->stop();
}
}

/**
* @throws ObjectIsInvalid
*/
public function verify(): void
{
if ($this->checksum === null) {
throw new ObjectIsInvalid("Object '{$this->info->name}' is missing chunks: received {$this->size} of {$this->info->size} bytes.");
}

if ($this->size !== $this->info->size) {
throw new ObjectIsInvalid("Object '{$this->info->name}' size mismatch: expected {$this->info->size} bytes, received {$this->size}.");
}

if (!hash_equals(DigestCalculator::decode($this->info->digest ?? ''), $this->checksum)) {
throw new ObjectIsInvalid("Object '{$this->info->name}' digest mismatch.");
}
}
}
20 changes: 20 additions & 0 deletions src/JetStream/ObjectStore/Internal/DigestCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Thesis\Nats\JetStream\ObjectStore\Internal;

use Thesis\Nats\Exception\ObjectIsInvalid;

/**
* @internal
*/
Expand All @@ -27,6 +29,24 @@ public static function sha256(): self
return new self(self::ALGO_SHA256);
}

/**
* Decodes "SHA-256=<padded url-safe base64>" like nats.go DecodeObjectDigest.
*
* @throws ObjectIsInvalid
*/
public static function decode(string $digest): string
{
$parts = explode('=', $digest, 2);

$decoded = \count($parts) === 2 ? base64_decode(strtr($parts[1], '-_', '+/'), true) : false;

if ($decoded === false || $decoded === '') {
throw new ObjectIsInvalid("Invalid object digest format '{$digest}'.");
}

return $decoded;
}

/**
* @param non-empty-string $data
*/
Expand Down
Loading
Loading