diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f13295c..6294066 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -128,9 +128,9 @@ jobs: composer config repositories.server '{"type":"path","url":"../server","options":{"symlink":false}}' --json if [ "${{ matrix.scenario }}" = 'both' ] then - composer require php-skir/server:dev-main php-skir/client:dev-main --no-interaction --no-progress + composer require "php-skir/server:*@dev" "php-skir/client:*@dev" --no-interaction --no-progress else - composer require php-skir/server:dev-main --no-interaction --no-progress + composer require "php-skir/server:*@dev" --no-interaction --no-progress fi composer require laravel/boost:^2.4 --dev --no-interaction --no-progress test ! -L vendor/php-skir/server diff --git a/README.md b/README.md index 3ac5d77..bf9a05a 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ SkirRPC offers many of the same benefits as gRPC—shared method definitions, ge - Route procedures to attributed or invokable Laravel controllers with native dependency injection, middleware, authorization, and Form Requests. See [Routing](docs/routing.md). - Keep generated providers and manual handlers available for non-controller layouts. See [Routing](docs/routing.md#generated-procedure-providers). - Inspect an endpoint's procedures through its opt-in Studio. See [Studio](docs/routing.md#studio). -- Configure dense JSON, standard JSON, base64 dense JSON, or CBOR package-wide or per endpoint. See [Codecs](docs/codecs.md). +- Configure dense JSON, standard JSON, or base64 dense JSON package-wide or per endpoint. See [Codecs](docs/codecs.md). ## Quick start diff --git a/composer.json b/composer.json index 8786d8a..ec8bc7b 100644 --- a/composer.json +++ b/composer.json @@ -25,15 +25,14 @@ "php": "^8.4", "laravel/framework": "^13.0", "nikic/php-parser": "^5.8", - "php-skir/runtime": "^0.1@dev", + "php-skir/runtime": "^0.1@dev|^0.2@dev", "symfony/process": "^7.4|^8.0" }, "require-dev": { "laravel/pint": "^1.29", "orchestra/testbench": "^11.0", - "php-skir/client": "^0.1@dev", - "phpunit/phpunit": "^12.5", - "spomky-labs/cbor-php": "^3.2" + "php-skir/client": "^0.1@dev|^0.2@dev", + "phpunit/phpunit": "^12.5" }, "autoload": { "psr-4": { @@ -68,16 +67,13 @@ ] }, "branch-alias": { - "dev-main": "0.1.x-dev" + "dev-main": "0.2.x-dev" } }, "scripts": { "format": "pint", "test": "phpunit" }, - "suggest": { - "spomky-labs/cbor-php": "Required when using SkirCodecs::cbor() for application/cbor endpoints." - }, "config": { "sort-packages": true }, diff --git a/docs/codecs.md b/docs/codecs.md index c167488..314b0bf 100644 --- a/docs/codecs.md +++ b/docs/codecs.md @@ -20,7 +20,7 @@ return [ ]; ``` -The codec class is resolved through Laravel's container and must implement `Skir\Server\Codecs\SkirCodec`. The built-in codec classes are `DenseJsonCodec`, `StandardJsonCodec`, `Base64DenseJsonCodec`, and `CborCodec`, all in the `Skir\Server\Codecs` namespace. An explicit codec passed as the third argument to `Route::skirRpc()` overrides the configured default for that route. +The codec class is resolved through Laravel's container and must implement `Skir\Server\Codecs\SkirCodec`. The built-in codec classes are `DenseJsonCodec`, `StandardJsonCodec`, and `Base64DenseJsonCodec`, all in the `Skir\Server\Codecs` namespace. An explicit codec passed as the third argument to `Route::skirRpc()` overrides the configured default for that route. ## Dense JSON @@ -71,23 +71,3 @@ Route::skirRpc('/api/skir-base64', [ ``` The outer SkirRPC envelope remains JSON; only the request and response values are represented as base64-encoded dense JSON. - -## CBOR - -CBOR accepts an `application/cbor` request body containing the SkirRPC `method` and dense `request` values, then returns an `application/cbor` response body: - -```php -use Skir\Server\Codecs\SkirCodecs; - -Route::skirRpc('/api/skir-cbor', [ - Skir::controller(UserController::class), -], SkirCodecs::cbor()); -``` - -CBOR support is optional. Install its runtime dependency in the consuming Laravel application before selecting this codec: - -```bash -composer require spomky-labs/cbor-php -``` - -Calling `SkirCodecs::cbor()` without the dependency throws a `SkirServerException` while the route is being built. diff --git a/resources/boost/guidelines/core.blade.php b/resources/boost/guidelines/core.blade.php index 3af1489..23d58fc 100644 --- a/resources/boost/guidelines/core.blade.php +++ b/resources/boost/guidelines/core.blade.php @@ -9,8 +9,7 @@ `authorize()` and `rules()`; scaffolded Form Requests deny access and validate nothing by default. Method middleware does not protect Studio. Put middleware on the outer Skir route when Studio or the -complete endpoint must be private. Keep the server codec aligned with clients, and treat CBOR as -serialization rather than security. +complete endpoint must be private. Keep the server codec aligned with clients. Use the `skir-server-development` skill for procedure routing, scaffolding, Studio, codec, or testing work. diff --git a/resources/boost/skills/skir-server-development/SKILL.md b/resources/boost/skills/skir-server-development/SKILL.md index 747f149..7059784 100644 --- a/resources/boost/skills/skir-server-development/SKILL.md +++ b/resources/boost/skills/skir-server-development/SKILL.md @@ -17,7 +17,7 @@ boundaries. - Choosing or implementing generated standard PHP objects, Spatie Laravel Data objects, or Simple Data Objects—including input hydration and validation: read [structures and validation](references/structures-and-validation.md). -- Studio exposure, codecs, CBOR, failures, or test coverage: read +- Studio exposure, codecs, failures, or test coverage: read [Studio, codecs, and testing](references/studio-codecs-and-testing.md). - Tasks crossing these areas: read every relevant reference before changing code. diff --git a/resources/boost/skills/skir-server-development/references/studio-codecs-and-testing.md b/resources/boost/skills/skir-server-development/references/studio-codecs-and-testing.md index ef311ff..966d9dd 100644 --- a/resources/boost/skills/skir-server-development/references/studio-codecs-and-testing.md +++ b/resources/boost/skills/skir-server-development/references/studio-codecs-and-testing.md @@ -21,13 +21,10 @@ method invocation. The configured `codec` must implement `SkirCodec`. Laravel resolves the class through the container; an explicit third argument to `Route::skirRpc()` overrides the package default for that route. -Built-in implementations are `DenseJsonCodec`, `StandardJsonCodec`, `Base64DenseJsonCodec`, and -`CborCodec`. The matching factories are available through `SkirCodecs`. Clients and servers must use +Built-in implementations are `DenseJsonCodec`, `StandardJsonCodec`, and `Base64DenseJsonCodec`. +The matching factories are available through `SkirCodecs`. Clients and servers must use the same codec. -CBOR uses `application/cbor` and requires `spomky-labs/cbor-php`. It is binary serialization, not -privacy or encryption. Selecting it without the optional dependency fails while the route is built. - ## Required behavior coverage Use PHPUnit feature tests at the Laravel boundary and never call a real external service. Cover the @@ -38,7 +35,7 @@ paths applicable to the change: - authorization HTTP 403 with `skir_authorization_failed`; - unauthenticated and unauthorized method calls; - protected and intentionally public Studio behavior; -- malformed JSON, malformed CBOR, missing method, unknown method, and invalid payload envelopes; +- malformed JSON, missing method, unknown method, and invalid payload envelopes; - duplicate method registration from controllers, providers, or manual handlers; - configured and route-specific codec success plus invalid codec configuration; - controller middleware ordering, short-circuit responses, and the terminable middleware rejection; @@ -46,7 +43,7 @@ paths applicable to the change: Assert status, Skir error code/details, response content type/body, authorization boundary, and side effects. For Studio, assert both rendered procedure metadata and protection on the outer route. For -codec changes, use genuine encoded request/response bodies rather than treating JSON and CBOR as +codec changes, use genuine encoded request/response bodies rather than treating codecs as interchangeable. When a test fails, first identify whether the mismatch belongs to route protection, method diff --git a/src/Codecs/CborCodec.php b/src/Codecs/CborCodec.php deleted file mode 100644 index 64de85b..0000000 --- a/src/Codecs/CborCodec.php +++ /dev/null @@ -1,31 +0,0 @@ -requestType, $request); - } - - public function encodeResponse(MethodDescriptor $descriptor, mixed $response): string - { - return Cbor::encodeValue($descriptor->responseType, $response); - } - - public function contentType(): string - { - return 'application/cbor'; - } -} diff --git a/src/Codecs/SkirCodecs.php b/src/Codecs/SkirCodecs.php index 084a4f2..a7d7895 100644 --- a/src/Codecs/SkirCodecs.php +++ b/src/Codecs/SkirCodecs.php @@ -4,20 +4,8 @@ namespace Skir\Server\Codecs; -use Skir\Runtime\Cbor; -use Skir\Server\Exceptions\SkirServerException; - final class SkirCodecs { - public static function cbor(): SkirCodec - { - if (! Cbor::available()) { - throw SkirServerException::missingCborDependency(); - } - - return new CborCodec; - } - public static function denseJson(): SkirCodec { return new DenseJsonCodec; diff --git a/src/Codecs/SkirHttpCodec.php b/src/Codecs/SkirHttpCodec.php deleted file mode 100644 index 56fc5c1..0000000 --- a/src/Codecs/SkirHttpCodec.php +++ /dev/null @@ -1,18 +0,0 @@ -> $errors */ public static function validationFailed(array $errors): self { diff --git a/src/Http/Controllers/SkirRpcController.php b/src/Http/Controllers/SkirRpcController.php index b5ed47b..53a3792 100644 --- a/src/Http/Controllers/SkirRpcController.php +++ b/src/Http/Controllers/SkirRpcController.php @@ -12,7 +12,6 @@ use JsonException; use Skir\Runtime\Exceptions\SkirRuntimeException; use Skir\Server\Codecs\SkirCodec; -use Skir\Server\Codecs\SkirHttpCodec; use Skir\Server\Exceptions\SkirServerException; use Skir\Server\Http\Requests\SkirMethodRequestScope; use Skir\Server\PreparedProcedure; @@ -49,7 +48,7 @@ public function __invoke(Request $request): Response } $codec = $server->codec(); - $payload = $this->payloadFromRequest($request, $codec); + $payload = $this->payloadFromRequest($request); $method = $this->methodFromPayload($payload); $procedure = $server->procedure($method); $requestPayload = array_key_exists('request', $payload) ? $payload['request'] : 0; @@ -100,11 +99,6 @@ private function encodeResponse( ): Response { $encodedResponse = $codec->encodeResponse($procedure->descriptor, $result); - if ($codec instanceof SkirHttpCodec) { - return response($encodedResponse, Response::HTTP_OK) - ->header('content-type', $codec->contentType()); - } - return response()->json($encodedResponse, Response::HTTP_OK); } @@ -153,16 +147,12 @@ private function studioEnabled(Request $request): bool * request?: mixed * } */ - private function payloadFromRequest(Request $request, SkirCodec $codec): array + private function payloadFromRequest(Request $request): array { if ($this->usesQueryString($request)) { return $this->payloadFromQueryString($request); } - if ($codec instanceof SkirHttpCodec) { - return $codec->decodePayload($request->getContent()); - } - try { $decodedPayload = json_decode($request->getContent(), flags: JSON_THROW_ON_ERROR); } catch (JsonException $exception) { diff --git a/tests/Feature/BoostAssetsTest.php b/tests/Feature/BoostAssetsTest.php index 1536088..2c02c3b 100644 --- a/tests/Feature/BoostAssetsTest.php +++ b/tests/Feature/BoostAssetsTest.php @@ -130,7 +130,6 @@ public function it_keeps_server_guidance_anchored_to_the_released_package(): voi 'DenseJsonCodec', 'StandardJsonCodec', 'Base64DenseJsonCodec', - 'CborCodec', 'skir_validation_failed', 'skir_authorization_failed', 'malformed', @@ -156,14 +155,11 @@ public function it_keeps_server_guidance_portable_and_non_executable(): void 'make composer', 'make npm', 'make test', - 'CBOR encrypts', - 'CBOR is secure', 'edit generated', ] as $forbiddenGuidance) { $this->assertStringNotContainsStringIgnoringCase($forbiddenGuidance, $guidance); } - $this->assertStringContainsString('serialization', $guidance); $this->assertStringContainsString('Do not edit', $guidance); $this->assertSame( [self::GUIDELINE, self::SKILL, ...self::REFERENCES], diff --git a/tests/Feature/ClientServerCodecContractTest.php b/tests/Feature/ClientServerCodecContractTest.php index edf0fe5..521acde 100644 --- a/tests/Feature/ClientServerCodecContractTest.php +++ b/tests/Feature/ClientServerCodecContractTest.php @@ -73,7 +73,6 @@ public static function codecs(): iterable yield 'dense JSON' => ['dense-json']; yield 'standard JSON' => ['standard-json']; yield 'base64 dense JSON' => ['base64-dense-json']; - yield 'CBOR' => ['cbor']; } private function clientCodec(string $codecName): SkirClientCodec @@ -82,7 +81,6 @@ private function clientCodec(string $codecName): SkirClientCodec 'dense-json' => SkirClientCodecs::denseJson(), 'standard-json' => SkirClientCodecs::standardJson(), 'base64-dense-json' => SkirClientCodecs::base64DenseJson(), - 'cbor' => SkirClientCodecs::cbor(), }; } @@ -92,7 +90,6 @@ private function serverCodec(string $codecName): SkirCodec 'dense-json' => SkirCodecs::denseJson(), 'standard-json' => SkirCodecs::standardJson(), 'base64-dense-json' => SkirCodecs::base64DenseJson(), - 'cbor' => SkirCodecs::cbor(), }; } diff --git a/tests/Feature/SkirCodecFailureTest.php b/tests/Feature/SkirCodecFailureTest.php index 71c8bd7..2c088a2 100644 --- a/tests/Feature/SkirCodecFailureTest.php +++ b/tests/Feature/SkirCodecFailureTest.php @@ -7,11 +7,9 @@ use Illuminate\Support\Facades\Route; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; -use Skir\Runtime\Cbor; use Skir\Runtime\MethodDescriptor; use Skir\Runtime\Type; use Skir\Server\Codecs\Base64DenseJsonCodec; -use Skir\Server\Codecs\CborCodec; use Skir\Server\Codecs\DenseJsonCodec; use Skir\Server\Codecs\SkirCodecs; use Skir\Server\Codecs\StandardJsonCodec; @@ -53,109 +51,12 @@ public function it_rejects_a_missing_base64_request_value(): void ->assertJsonPath('error.code', 'skir_invalid_request'); } - #[Test] - public function it_rejects_malformed_cbor(): void - { - Route::skirRpc('/cbor-malformed', [ - new CodecSquareProvider, - ], SkirCodecs::cbor()); - - $this - ->call( - 'POST', - '/cbor-malformed', - server: ['CONTENT_TYPE' => 'application/cbor'], - content: "\xff", - ) - ->assertUnprocessable() - ->assertJsonPath('error.code', 'skir_invalid_request'); - } - - #[Test] - public function it_rejects_non_map_cbor_envelopes(): void - { - Route::skirRpc('/cbor-map', [ - new CodecSquareProvider, - ], SkirCodecs::cbor()); - - $this - ->call( - 'POST', - '/cbor-map', - server: ['CONTENT_TYPE' => 'application/cbor'], - content: Cbor::encode(5), - ) - ->assertUnprocessable() - ->assertJsonPath('error.code', 'skir_invalid_request'); - } - - #[Test] - public function it_rejects_cbor_envelopes_without_a_method(): void - { - Route::skirRpc('/cbor-method', [ - new CodecSquareProvider, - ], SkirCodecs::cbor()); - - $this - ->call( - 'POST', - '/cbor-method', - server: ['CONTENT_TYPE' => 'application/cbor'], - content: Cbor::encode(['request' => 5.0]), - ) - ->assertUnprocessable() - ->assertJsonPath('error.code', 'skir_invalid_request'); - } - - #[Test] - public function it_returns_the_package_error_for_unknown_cbor_methods(): void - { - Route::skirRpc('/cbor-unknown', [ - new CodecSquareProvider, - ], SkirCodecs::cbor()); - - $this - ->call( - 'POST', - '/cbor-unknown', - server: ['CONTENT_TYPE' => 'application/cbor'], - content: Cbor::encode([ - 'method' => 'Missing', - 'request' => 5.0, - ]), - ) - ->assertNotFound() - ->assertJsonPath('error.code', 'skir_method_not_found'); - } - - #[Test] - public function it_rejects_cbor_values_that_do_not_match_the_request_type(): void - { - Route::skirRpc('/cbor-type', [ - new CodecSquareProvider, - ], SkirCodecs::cbor()); - - $this - ->call( - 'POST', - '/cbor-type', - server: ['CONTENT_TYPE' => 'application/cbor'], - content: Cbor::encode([ - 'method' => 'CountItems', - 'request' => 'five', - ]), - ) - ->assertUnprocessable() - ->assertJsonPath('error.code', 'skir_invalid_request'); - } - #[Test] public function codec_factories_return_the_expected_codec_types(): void { $this->assertInstanceOf(DenseJsonCodec::class, SkirCodecs::denseJson()); $this->assertInstanceOf(StandardJsonCodec::class, SkirCodecs::standardJson()); $this->assertInstanceOf(Base64DenseJsonCodec::class, SkirCodecs::base64DenseJson()); - $this->assertInstanceOf(CborCodec::class, SkirCodecs::cbor()); } /** diff --git a/tests/Feature/SkirRpcControllerTest.php b/tests/Feature/SkirRpcControllerTest.php index 111e71a..259a2ec 100644 --- a/tests/Feature/SkirRpcControllerTest.php +++ b/tests/Feature/SkirRpcControllerTest.php @@ -4,9 +4,6 @@ namespace Skir\Server\Tests\Feature; -use CBOR\Decoder; -use CBOR\Encoder; -use CBOR\StringStream; use Illuminate\Http\Request; use Illuminate\Routing\Route as LaravelRoute; use Illuminate\Support\Facades\Route; @@ -352,48 +349,6 @@ public function it_can_use_base64_encoded_dense_json_payloads_for_an_endpoint(): ->assertContent(json_encode(base64_encode(DenseJson::toJson(Type::float32(), 25.0)), JSON_THROW_ON_ERROR)); } - #[Test] - public function it_can_use_cbor_payloads_for_an_endpoint(): void - { - Route::skirRpc('/cbor-rpc', [ - RenameUserProcedureProvider::class, - ], SkirCodecs::cbor()); - - $userType = Type::struct([ - Field::value('id', 0, Type::int32()), - Field::value('name', 1, Type::string()), - ]); - - $response = $this->call( - 'POST', - '/cbor-rpc', - server: [ - 'CONTENT_TYPE' => 'application/cbor', - 'HTTP_ACCEPT' => 'application/cbor', - ], - content: (new Encoder)->encode([ - 'method' => 'RenameUser', - 'request' => DenseJson::encode($userType, [ - 'id' => 42, - 'name' => 'Ruben', - ]), - ]), - ); - - $response - ->assertOk() - ->assertHeader('content-type', 'application/cbor'); - - $decodedResponse = Decoder::create() - ->decode(StringStream::create($response->getContent())) - ->normalize(); - - $this->assertSame([ - 'id' => 42, - 'name' => 'Ruben updated', - ], DenseJson::decode($userType, $decodedResponse)); - } - #[Test] public function it_injects_the_primary_skir_context(): void { diff --git a/tests/Unit/SkirServerExceptionTest.php b/tests/Unit/SkirServerExceptionTest.php index bf56bc8..5cd0eec 100644 --- a/tests/Unit/SkirServerExceptionTest.php +++ b/tests/Unit/SkirServerExceptionTest.php @@ -76,12 +76,6 @@ public static function packageExceptions(): array 'Configured Skir codec ['.InvalidConfiguredCodec::class.'] must implement ' .'[Skir\\Server\\Codecs\\SkirCodec].', ], - 'missing CBOR dependency' => [ - SkirServerException::missingCborDependency(), - 'skir_missing_cbor_dependency', - 500, - 'Skir CBOR support requires the [spomky-labs/cbor-php] Composer package.', - ], ]; } }