Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
puklipo committed Dec 25, 2024
1 parent ccd5eb2 commit 17ce5db
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/Casts/AtBytes.php → src/Casts/AsAtBytesObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Revolution\Bluesky\Core\CBOR\BytesWrapper;
use Revolution\Bluesky\Core\CBOR\AtBytes;

class AtBytes implements CastsAttributes
class AsAtBytesObject implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): BytesWrapper
public function get(Model $model, string $key, mixed $value, array $attributes): AtBytes
{
return new BytesWrapper($value);
return new AtBytes($value);
}

/**
Expand All @@ -27,6 +27,6 @@ public function get(Model $model, string $key, mixed $value, array $attributes):
*/
public function set(Model $model, string $key, mixed $value, array $attributes): string
{
return $value instanceof BytesWrapper ? $value->bytes() : $value;
return $value instanceof AtBytes ? $value->toBytes() : $value;
}
}
20 changes: 15 additions & 5 deletions src/Core/CBOR/BytesWrapper.php → src/Core/CBOR/AtBytes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@
/**
* @link https://github.com/mary-ext/atcute/blob/trunk/packages/utilities/cbor/lib/bytes.ts
*/
final readonly class BytesWrapper implements Arrayable, Jsonable
final readonly class AtBytes implements Arrayable, Jsonable
{
public function __construct(protected string $bytes)
{
}

public function bytes(): string
/**
* ```
* $bytes = AtBytes::fromArray(['$bytes' => 'base64'])->toBytes();
* ```
*/
public static function fromArray(array $array): self
{
return $this->bytes;
return new self(base64_decode($array['$bytes']));
}

public function encode(): string
public function toBytes(): string
{
return base64_encode($this->bytes);
return $this->bytes;
}

public function toArray(): array
Expand All @@ -35,4 +40,9 @@ public function toJson($options = 0): string
{
return json_encode($this->toArray(), $options);
}

protected function encode(): string
{
return base64_encode($this->bytes);
}
}
4 changes: 2 additions & 2 deletions src/Core/CBOR/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ private function readString(int $length): string
return $this->stream->read(min($length, $this->stream_size));
}

private function readBytes(int $length): BytesWrapper
private function readBytes(int $length): AtBytes
{
return new BytesWrapper($this->stream->read(min($length, $this->stream_size)));
return new AtBytes($this->stream->read(min($length, $this->stream_size)));
}

private function readCid(int $length): CIDLinkWrapper
Expand Down
6 changes: 3 additions & 3 deletions src/Core/CBOR/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function __invoke(mixed $data): mixed
}

// Only here is in ['$bytes' => 'base64'] format
if ($key === 'sig' && $item instanceof BytesWrapper) {
if ($key === 'sig' && $item instanceof AtBytes) {
return $item->toArray();
}

if ($item instanceof BytesWrapper) {
return $item->bytes();
if ($item instanceof AtBytes) {
return $item->toBytes();
}

return $this($item);
Expand Down
2 changes: 1 addition & 1 deletion src/Labeler/AbstractLabeler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class AbstractLabeler
abstract public function labels(): array;

/**
* @return iterable<null|LabelMessage>
* @return iterable<null|SubscribeLabelMessage>
*
* @throw LabelerException
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Labeler/Labeler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Support\Facades\Config;
use InvalidArgumentException;
use Revolution\Bluesky\Core\CBOR;
use Revolution\Bluesky\Core\CBOR\BytesWrapper;
use Revolution\Bluesky\Core\CBOR\AtBytes;
use Revolution\Bluesky\Crypto\K256;
use Revolution\Bluesky\FeedGenerator\ValidateAuth;
use RuntimeException;
Expand Down Expand Up @@ -47,7 +47,7 @@ public static function getLabelDefinitions(): array
}

/**
* @return iterable<null|LabelMessage>
* @return iterable<null|SubscribeLabelMessage>
*
* @throws LabelerException
*/
Expand Down Expand Up @@ -125,7 +125,7 @@ public static function signLabel(array $label): array

$sign = K256::load($key)->privateKey()->sign($bytes);

$label = Arr::add($label, 'sig', new BytesWrapper($sign));
$label = Arr::add($label, 'sig', new AtBytes($sign));

return [$label, $sign];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Labeler/LabelerServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function start(): void
});
};

$this->ws->onWebSocketConnect = function (TcpConnection $connection, Request $request) {
$this->ws->onWebSocketConnected = function (TcpConnection $connection, Request $request) {
$connection->websocketType = Websocket::BINARY_TYPE_ARRAYBUFFER;

$cursor = $request->get('cursor');
Expand All @@ -104,7 +104,7 @@ public function start(): void

try {
foreach (Labeler::subscribeLabels($cursor) as $label) {
if ($label instanceof LabelMessage) {
if ($label instanceof SubscribeLabelMessage) {
$bytes = $label->toBytes();
info('subscribeLabels: '.$bytes);
$connection->send($bytes);
Expand Down
28 changes: 28 additions & 0 deletions src/Labeler/SavedLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Revolution\Bluesky\Labeler;

use Revolution\Bluesky\Core\CBOR\AtBytes;

class SavedLabel extends SignedLabel
{
public function __construct(
public int $id,
SignedLabel $signed,
) {
parent::__construct(
$signed->toUnsigned(),
$signed->sig,
);
}

/**
* @return array{id: int, uri: string, cid: ?string, val: string, src: string, cts: string, exp: ?string, sig: AtBytes}
*/
public function toArray(): array
{
return get_object_vars($this);
}
}
32 changes: 32 additions & 0 deletions src/Labeler/SignedLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Revolution\Bluesky\Labeler;

use Revolution\Bluesky\Core\CBOR\AtBytes;

class SignedLabel extends UnsignedLabel
{
public function __construct(
UnsignedLabel $unsigned,
public AtBytes $sig,
) {
parent::__construct(
...$unsigned->toArray(),
);
}

public function toUnsigned(): UnsignedLabel
{
return new UnsignedLabel($this->uri, $this->cid, $this->val, $this->src, $this->cts, $this->exp);
}

/**
* @return array{uri: string, cid: ?string, val: string, src: string, cts: string, exp: ?string, sig: AtBytes}
*/
public function toArray(): array
{
return get_object_vars($this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Revolution\Bluesky\Core\CBOR;

class LabelMessage
class SubscribeLabelMessage
{
public function __construct(
protected int $seq,
Expand Down
38 changes: 38 additions & 0 deletions src/Labeler/UnsignedLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Revolution\Bluesky\Labeler;

use Illuminate\Contracts\Support\Arrayable;
use Revolution\Bluesky\Core\CBOR\AtBytes;

class UnsignedLabel implements Arrayable
{
public function __construct(
public string $uri,
public ?string $cid,
public string $val,
public string $src,
public string $cts,
public ?string $exp,
) {
}

public function toSigned(string|AtBytes $sig): SignedLabel
{
if (is_string($sig)) {
$sig = new AtBytes($sig);
}

return new SignedLabel($this, $sig);
}

/**
* @return array{uri: string, cid: ?string, val: string, src: string, cts: string, exp: ?string}
*/
public function toArray(): array
{
return get_object_vars($this);
}
}
4 changes: 2 additions & 2 deletions tests/Feature/Labeler/LabelerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tests\Feature\Labeler;

use Revolution\Bluesky\Core\CBOR;
use Revolution\Bluesky\Core\CBOR\BytesWrapper;
use Revolution\Bluesky\Core\CBOR\AtBytes;
use Revolution\Bluesky\Core\CBOR\MapKeySort;
use Tests\TestCase;

Expand All @@ -21,7 +21,7 @@ public function test_labeler_cbor_encode(): void
'src' => 'did',
'cts' => now()->toISOString(),
'exp' => null,
'sig' => new BytesWrapper('sig'),
'sig' => new AtBytes('sig'),
];

$label = CBOR::normalize($label);
Expand Down
4 changes: 2 additions & 2 deletions workbench/app/Labeler/ArtisanLabeler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Revolution\Bluesky\Labeler\EmitEventResponse;
use Revolution\Bluesky\Labeler\LabelDefinition;
use Revolution\Bluesky\Labeler\LabelLocale;
use Revolution\Bluesky\Labeler\LabelMessage;
use Revolution\Bluesky\Labeler\SubscribeLabelMessage;

class ArtisanLabeler extends AbstractLabeler
{
Expand All @@ -29,7 +29,7 @@ public function labels(): array
}

/**
* @return iterable<null|LabelMessage>
* @return iterable<null|SubscribeLabelMessage>
*
* @throw LabelerException
*/
Expand Down

0 comments on commit 17ce5db

Please sign in to comment.