From 5d37d92a290adcbb06d97c84d1643cac5a061e92 Mon Sep 17 00:00:00 2001 From: Gary Peck Date: Sat, 31 Aug 2024 03:34:17 +0200 Subject: [PATCH] Stop using protobuf.js for encoding/decoding on Kotlin/JS This resolves a bunch of protobuf conformance test failures on Kotlin/JS that were being caused by protobuf.js. It also removes a dependency. There's a possibility that the protobuf.js implementation of low-level protobuf encoding/decoding is more optimized for JS runtimes than pbandk's Kotlin-based implementation. But I'd rather spend time on optimizing pbandk's implementation, which would also benefit other platforms in addition to Kotlin/JS, than on fighting bugs in protobuf.js that aren't going to be fixed any time soon. Also replaced the `utf8Len()` implementation that was based on code from Google's Protobuf Java library with a simpler implementation derived from Okio code. --- CHANGELOG.md | 2 + README.md | 4 +- buildSrc/src/main/kotlin/Versions.kt | 1 - conformance/js/failing_tests.txt | 58 ------- examples/browser-js/kotlin-js-store/yarn.lock | 78 +--------- kotlin-js-store/yarn.lock | 78 +--------- runtime/build.gradle.kts | 6 - .../kotlin/pbandk/internal/binary/Utf8Len.kt | 98 ++++++------ .../jsMain/kotlin/pbandk/internal/Util.js.kt | 15 +- .../binary/BinaryMessageDecoder.js.kt | 9 +- .../binary/BinaryMessageEncoder.js.kt | 4 +- .../kotlin/pbandk/internal/binary/Sizer.js.kt | 2 +- .../ProtobufjsBinaryMessageEncoder.kt | 20 --- .../protobufjs/ProtobufjsBinaryWireDecoder.kt | 81 ---------- .../protobufjs/ProtobufjsBinaryWireEncoder.kt | 145 ------------------ .../jsMain/kotlin/pbandk/protobufjs/Sizer.kt | 7 - .../jsMain/kotlin/pbandk/protobufjs/Util.kt | 32 ---- .../kotlin/pbandk/protobufjs/protobufjs.kt | 83 ---------- 18 files changed, 59 insertions(+), 664 deletions(-) delete mode 100644 runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryMessageEncoder.kt delete mode 100644 runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryWireDecoder.kt delete mode 100644 runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryWireEncoder.kt delete mode 100644 runtime/src/jsMain/kotlin/pbandk/protobufjs/Sizer.kt delete mode 100644 runtime/src/jsMain/kotlin/pbandk/protobufjs/Util.kt delete mode 100644 runtime/src/jsMain/kotlin/pbandk/protobufjs/protobufjs.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c543f84..c6ec3fc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Fixed * Reduced the likelihood of getting a `MethodTooLargeException` when generating code for protobuf messages that contain many fields with custom options. (PR [#277], fixes [#252]) +* Removed protobuf.js dependency on Kotlin/JS. This also fixed a bunch of protobuf conformance-related bugs on Kotlin/JS. (PR [#279]) [#233]: https://github.com/streem/pbandk/issues/233 [#252]: https://github.com/streem/pbandk/issues/252 @@ -28,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), [#273]: https://github.com/streem/pbandk/pull/273 [#276]: https://github.com/streem/pbandk/pull/276 [#277]: https://github.com/streem/pbandk/pull/277 +[#279]: https://github.com/streem/pbandk/pull/279 ## [0.15.0] - 2024-08-05 diff --git a/README.md b/README.md index 7b4e8d83..ec81226e 100644 --- a/README.md +++ b/README.md @@ -389,8 +389,6 @@ dependencies { } ``` -Pbandk has a dependency on the protobuf.js library on the Kotlin/JS platform. On other platforms pbandk uses its own pure-Kotlin protobuf implementation that is heavily based on the Google Protobuf Java library. - ### Service Code Generation Pbandk does not generate gRPC code itself, but offers a `pbandk.gen.ServiceGenerator` interface in @@ -642,3 +640,5 @@ GitHub will build and publish the new release once it sees the new tag: ## Credits This repository was originally forked from https://github.com/cretz/pb-and-k. Many thanks to https://github.com/cretz for creating this library and building the initial feature set. + +pbandk uses its own pure-Kotlin protobuf implementation that is heavily based on the Google Protobuf Java library. diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index b00a9fd0..a3e07080 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -12,6 +12,5 @@ object Versions { const val osDetectorGradlePlugin = "1.7.3" const val protoc = "3.19.1" const val protobufJava = protoc - const val protobufJs = "7.3.2" const val springBootGradlePlugin = "2.4.5" } diff --git a/conformance/js/failing_tests.txt b/conformance/js/failing_tests.txt index fd77c47c..30caaa67 100644 --- a/conformance/js/failing_tests.txt +++ b/conformance/js/failing_tests.txt @@ -1,68 +1,10 @@ ## -## There is a bug reading some ints in the protobuf.js library that pbandk uses -## See https://github.com/dcodeIO/protobuf.js/issues/1067 -## -Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.PackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.PackedInput.PackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.PackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.PackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.PackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.PackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataScalarBinary.INT32[3].ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataScalarBinary.UINT32[3].ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.PackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.PackedInput.PackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.PackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.PackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.PackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.PackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataScalarBinary.INT32[3].ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataScalarBinary.UINT32[3].ProtobufOutput -Required.Proto2.ProtobufInput.RepeatedScalarSelectsLast.INT32.ProtobufOutput -Required.Proto2.ProtobufInput.RepeatedScalarSelectsLast.UINT32.ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataRepeated.INT32.PackedInput.ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataScalar.INT32[3].ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataScalar.UINT32[3].ProtobufOutput -Required.Proto3.ProtobufInput.RepeatedScalarSelectsLast.INT32.JsonOutput -Required.Proto3.ProtobufInput.RepeatedScalarSelectsLast.INT32.ProtobufOutput -Required.Proto3.ProtobufInput.RepeatedScalarSelectsLast.UINT32.JsonOutput -Required.Proto3.ProtobufInput.RepeatedScalarSelectsLast.UINT32.ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.INT32.PackedInput.JsonOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.INT32.PackedInput.ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.JsonOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.JsonOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.JsonOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataScalar.INT32[3].JsonOutput -Required.Proto3.ProtobufInput.ValidDataScalar.INT32[3].ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataScalar.UINT32[3].JsonOutput -Required.Proto3.ProtobufInput.ValidDataScalar.UINT32[3].ProtobufOutput -## ## We don't fully support JSON yet ## See https://github.com/streem/pbandk/issues/72 ## Recommended.FieldMaskNumbersDontRoundTrip.JsonOutput Recommended.FieldMaskPathsDontRoundTrip.JsonOutput Recommended.FieldMaskTooManyUnderscore.JsonOutput -Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput -Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput Recommended.Proto3.JsonInput.StringFieldSurrogateInWrongOrder Recommended.Proto3.JsonInput.StringFieldUnpairedHighSurrogate Recommended.Proto3.JsonInput.StringFieldUnpairedLowSurrogate diff --git a/examples/browser-js/kotlin-js-store/yarn.lock b/examples/browser-js/kotlin-js-store/yarn.lock index 65615f78..b9c3d85c 100644 --- a/examples/browser-js/kotlin-js-store/yarn.lock +++ b/examples/browser-js/kotlin-js-store/yarn.lock @@ -94,59 +94,6 @@ resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33" integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg== -"@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@protobufjs/aspromise/-/aspromise-1.1.2.tgz#9b8b0cc663d669a7d8f6f5d0893a14d348f30fbf" - integrity sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ== - -"@protobufjs/base64@^1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@protobufjs/base64/-/base64-1.1.2.tgz#4c85730e59b9a1f1f349047dbf24296034bb2735" - integrity sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg== - -"@protobufjs/codegen@^2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@protobufjs/codegen/-/codegen-2.0.4.tgz#7ef37f0d010fb028ad1ad59722e506d9262815cb" - integrity sha512-YyFaikqM5sH0ziFZCN3xDC7zeGaB/d0IUb9CATugHWbd1FRFwWwt4ld4OYMPWu5a3Xe01mGAULCdqhMlPl29Jg== - -"@protobufjs/eventemitter@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/eventemitter/-/eventemitter-1.1.0.tgz#355cbc98bafad5978f9ed095f397621f1d066b70" - integrity sha512-j9ednRT81vYJ9OfVuXG6ERSTdEL1xVsNgqpkxMsbIabzSo3goCjDIveeGv5d03om39ML71RdmrGNjG5SReBP/Q== - -"@protobufjs/fetch@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/fetch/-/fetch-1.1.0.tgz#ba99fb598614af65700c1619ff06d454b0d84c45" - integrity sha512-lljVXpqXebpsijW71PZaCYeIcE5on1w5DlQy5WH6GLbFryLUrBD4932W/E2BSpfRJWseIL4v/KPgBFxDOIdKpQ== - dependencies: - "@protobufjs/aspromise" "^1.1.1" - "@protobufjs/inquire" "^1.1.0" - -"@protobufjs/float@^1.0.2": - version "1.0.2" - resolved "https://registry.yarnpkg.com/@protobufjs/float/-/float-1.0.2.tgz#5e9e1abdcb73fc0a7cb8b291df78c8cbd97b87d1" - integrity sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ== - -"@protobufjs/inquire@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/inquire/-/inquire-1.1.0.tgz#ff200e3e7cf2429e2dcafc1140828e8cc638f089" - integrity sha512-kdSefcPdruJiFMVSbn801t4vFK7KB/5gd2fYvrxhuJYg8ILrmn9SKSX2tZdV6V+ksulWqS7aXjBcRXl3wHoD9Q== - -"@protobufjs/path@^1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@protobufjs/path/-/path-1.1.2.tgz#6cc2b20c5c9ad6ad0dccfd21ca7673d8d7fbf68d" - integrity sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA== - -"@protobufjs/pool@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/pool/-/pool-1.1.0.tgz#09fd15f2d6d3abfa9b65bc366506d6ad7846ff54" - integrity sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw== - -"@protobufjs/utf8@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570" - integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw== - "@socket.io/component-emitter@~3.1.0": version "3.1.2" resolved "https://registry.yarnpkg.com/@socket.io/component-emitter/-/component-emitter-3.1.2.tgz#821f8442f4175d8f0467b9daf26e3a18e2d02af2" @@ -264,7 +211,7 @@ dependencies: "@types/node" "*" -"@types/node@*", "@types/node@>=10.0.0", "@types/node@>=13.7.0": +"@types/node@*", "@types/node@>=10.0.0": version "20.14.9" resolved "https://registry.yarnpkg.com/@types/node/-/node-20.14.9.tgz#12e8e765ab27f8c421a1820c99f5f313a933b420" integrity sha512-06OCtnTXtWOZBJlRApleWndH4JsRVs1pDCc8dLSQp+7PpUpX3ePdHyeNSFTeSe7FtKyQkrlPvHwJOW3SLd8Oyg== @@ -1850,11 +1797,6 @@ log4js@^6.4.1: rfdc "^1.3.0" streamroller "^3.1.5" -long@^5.0.0: - version "5.2.3" - resolved "https://registry.yarnpkg.com/long/-/long-5.2.3.tgz#a3ba97f3877cf1d778eccbcb048525ebb77499e1" - integrity sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q== - lru-cache@^10.2.0: version "10.4.3" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.4.3.tgz#410fc8a17b70e598013df257c2446b7f3383f119" @@ -2221,24 +2163,6 @@ process-nextick-args@~2.0.0: resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2" integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag== -protobufjs@^7.3.2: - version "7.3.2" - resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-7.3.2.tgz#60f3b7624968868f6f739430cfbc8c9370e26df4" - integrity sha512-RXyHaACeqXeqAKGLDl68rQKbmObRsTIn4TYVUUug1KfS47YWCo5MacGITEryugIgZqORCvJWEk4l449POg5Txg== - dependencies: - "@protobufjs/aspromise" "^1.1.2" - "@protobufjs/base64" "^1.1.2" - "@protobufjs/codegen" "^2.0.4" - "@protobufjs/eventemitter" "^1.1.0" - "@protobufjs/fetch" "^1.1.0" - "@protobufjs/float" "^1.0.2" - "@protobufjs/inquire" "^1.1.0" - "@protobufjs/path" "^1.1.2" - "@protobufjs/pool" "^1.1.0" - "@protobufjs/utf8" "^1.1.0" - "@types/node" ">=13.7.0" - long "^5.0.0" - proxy-addr@~2.0.7: version "2.0.7" resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025" diff --git a/kotlin-js-store/yarn.lock b/kotlin-js-store/yarn.lock index 933d655f..3a830d8c 100644 --- a/kotlin-js-store/yarn.lock +++ b/kotlin-js-store/yarn.lock @@ -52,59 +52,6 @@ "@jridgewell/resolve-uri" "^3.1.0" "@jridgewell/sourcemap-codec" "^1.4.14" -"@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@protobufjs/aspromise/-/aspromise-1.1.2.tgz#9b8b0cc663d669a7d8f6f5d0893a14d348f30fbf" - integrity sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ== - -"@protobufjs/base64@^1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@protobufjs/base64/-/base64-1.1.2.tgz#4c85730e59b9a1f1f349047dbf24296034bb2735" - integrity sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg== - -"@protobufjs/codegen@^2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@protobufjs/codegen/-/codegen-2.0.4.tgz#7ef37f0d010fb028ad1ad59722e506d9262815cb" - integrity sha512-YyFaikqM5sH0ziFZCN3xDC7zeGaB/d0IUb9CATugHWbd1FRFwWwt4ld4OYMPWu5a3Xe01mGAULCdqhMlPl29Jg== - -"@protobufjs/eventemitter@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/eventemitter/-/eventemitter-1.1.0.tgz#355cbc98bafad5978f9ed095f397621f1d066b70" - integrity sha512-j9ednRT81vYJ9OfVuXG6ERSTdEL1xVsNgqpkxMsbIabzSo3goCjDIveeGv5d03om39ML71RdmrGNjG5SReBP/Q== - -"@protobufjs/fetch@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/fetch/-/fetch-1.1.0.tgz#ba99fb598614af65700c1619ff06d454b0d84c45" - integrity sha512-lljVXpqXebpsijW71PZaCYeIcE5on1w5DlQy5WH6GLbFryLUrBD4932W/E2BSpfRJWseIL4v/KPgBFxDOIdKpQ== - dependencies: - "@protobufjs/aspromise" "^1.1.1" - "@protobufjs/inquire" "^1.1.0" - -"@protobufjs/float@^1.0.2": - version "1.0.2" - resolved "https://registry.yarnpkg.com/@protobufjs/float/-/float-1.0.2.tgz#5e9e1abdcb73fc0a7cb8b291df78c8cbd97b87d1" - integrity sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ== - -"@protobufjs/inquire@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/inquire/-/inquire-1.1.0.tgz#ff200e3e7cf2429e2dcafc1140828e8cc638f089" - integrity sha512-kdSefcPdruJiFMVSbn801t4vFK7KB/5gd2fYvrxhuJYg8ILrmn9SKSX2tZdV6V+ksulWqS7aXjBcRXl3wHoD9Q== - -"@protobufjs/path@^1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@protobufjs/path/-/path-1.1.2.tgz#6cc2b20c5c9ad6ad0dccfd21ca7673d8d7fbf68d" - integrity sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA== - -"@protobufjs/pool@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/pool/-/pool-1.1.0.tgz#09fd15f2d6d3abfa9b65bc366506d6ad7846ff54" - integrity sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw== - -"@protobufjs/utf8@^1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570" - integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw== - "@socket.io/component-emitter@~3.1.0": version "3.1.0" resolved "https://registry.yarnpkg.com/@socket.io/component-emitter/-/component-emitter-3.1.0.tgz#96116f2a912e0c02817345b3c10751069920d553" @@ -148,7 +95,7 @@ resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841" integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA== -"@types/node@*", "@types/node@>=10.0.0", "@types/node@>=13.7.0": +"@types/node@*", "@types/node@>=10.0.0": version "20.12.5" resolved "https://registry.yarnpkg.com/@types/node/-/node-20.12.5.tgz#74c4f31ab17955d0b5808cdc8fd2839526ad00b3" integrity sha512-BD+BjQ9LS/D8ST9p5uqBxghlN+S42iuNxjsUGjeZobe/ciXzk2qb1B6IXc6AnRLS+yFJRpN2IPEHMzwspfDJNw== @@ -1255,11 +1202,6 @@ log4js@^6.4.1: rfdc "^1.3.0" streamroller "^3.1.5" -long@^5.0.0: - version "5.2.3" - resolved "https://registry.yarnpkg.com/long/-/long-5.2.3.tgz#a3ba97f3877cf1d778eccbcb048525ebb77499e1" - integrity sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q== - media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" @@ -1494,24 +1436,6 @@ pkg-dir@^4.2.0: dependencies: find-up "^4.0.0" -protobufjs@^7.3.2: - version "7.3.2" - resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-7.3.2.tgz#60f3b7624968868f6f739430cfbc8c9370e26df4" - integrity sha512-RXyHaACeqXeqAKGLDl68rQKbmObRsTIn4TYVUUug1KfS47YWCo5MacGITEryugIgZqORCvJWEk4l449POg5Txg== - dependencies: - "@protobufjs/aspromise" "^1.1.2" - "@protobufjs/base64" "^1.1.2" - "@protobufjs/codegen" "^2.0.4" - "@protobufjs/eventemitter" "^1.1.0" - "@protobufjs/fetch" "^1.1.0" - "@protobufjs/float" "^1.0.2" - "@protobufjs/inquire" "^1.1.0" - "@protobufjs/path" "^1.1.2" - "@protobufjs/pool" "^1.1.0" - "@protobufjs/utf8" "^1.1.0" - "@types/node" ">=13.7.0" - long "^5.0.0" - punycode@^2.1.0: version "2.3.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.1.tgz#027422e2faec0b25e1549c3e1bd8309b9133b6e5" diff --git a/runtime/build.gradle.kts b/runtime/build.gradle.kts index b5821364..329327fe 100644 --- a/runtime/build.gradle.kts +++ b/runtime/build.gradle.kts @@ -113,12 +113,6 @@ kotlin { api(project(":pbandk-protos")) } } - - jsMain { - dependencies { - implementation(npm("protobufjs", "^${Versions.protobufJs}")) - } - } } } diff --git a/runtime/src/commonMain/kotlin/pbandk/internal/binary/Utf8Len.kt b/runtime/src/commonMain/kotlin/pbandk/internal/binary/Utf8Len.kt index 40741511..71621de6 100644 --- a/runtime/src/commonMain/kotlin/pbandk/internal/binary/Utf8Len.kt +++ b/runtime/src/commonMain/kotlin/pbandk/internal/binary/Utf8Len.kt @@ -1,63 +1,55 @@ -// Copyright 2019 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* + * Copyright (C) 2017 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package pbandk.internal.binary -private class CodePointIterator(private val s: String) : Iterator { - var pos = 0 +// Courtesy of https://github.com/square/okio/blob/37e2f284cf62a1253633ae40e5f84b976c57ea0d/okio/src/commonMain/kotlin/okio/Utf8.kt#L77 +internal fun utf8Len(value: String): Int { + val endIndex = value.length - override fun hasNext(): Boolean = pos < s.length + var result = 0 + var i = 0 + while (i < endIndex) { + val c = value[i].code - override fun next(): Int { - if (pos >= s.length) throw NoSuchElementException() - - val v = s[pos++] - if (v.isHighSurrogate() && pos < s.length) { - val l = s[pos] - if (l.isLowSurrogate()) { - pos++ - return 0x10000 + (v - 0xD800).code * 0x400 + (l - 0xDC00).code + if (c < 0x80) { + // A 7-bit character with 1 byte. + result++ + i++ + } else if (c < 0x800) { + // An 11-bit character with 2 bytes. + result += 2 + i++ + } else if (c < 0xd800 || c > 0xdfff) { + // A 16-bit character with 3 bytes. + result += 3 + i++ + } else { + val low = if (i + 1 < endIndex) value[i + 1].code else 0 + if (c > 0xdbff || low < 0xdc00 || low > 0xdfff) { + // A malformed surrogate, which yields '?'. + result++ + i++ + } else { + // A 21-bit character with 4 bytes. + result += 4 + i += 2 } } - return v.code and 0xffff } -} - -private class CodePointIterable(private val s: String) : Iterable { - override fun iterator(): Iterator = CodePointIterator(s) -} -internal fun utf8Len(value: String) = CodePointIterable(value).sumOf { - when (it) { - in 0..0x7f -> 1 - in 0x80..0x7ff -> 2 - in 0x800..0xffff -> 3 - else -> 4 - }.toInt() + return result } \ No newline at end of file diff --git a/runtime/src/jsMain/kotlin/pbandk/internal/Util.js.kt b/runtime/src/jsMain/kotlin/pbandk/internal/Util.js.kt index a6f3036e..ea329c61 100644 --- a/runtime/src/jsMain/kotlin/pbandk/internal/Util.js.kt +++ b/runtime/src/jsMain/kotlin/pbandk/internal/Util.js.kt @@ -1,16 +1,3 @@ package pbandk.internal -import org.khronos.webgl.Int8Array -import org.khronos.webgl.Uint8Array -import pbandk.PbandkInternal - -internal actual val PlatformUtil: Util = pbandk.protobufjs.ProtobufJsUtil - -@PbandkInternal -@Suppress("NOTHING_TO_INLINE") -public inline fun ByteArray.asUint8Array(): Uint8Array = - unsafeCast().run { Uint8Array(buffer, byteOffset, length) } - -@PbandkInternal -@Suppress("NOTHING_TO_INLINE") -public inline fun Uint8Array.asByteArray(): ByteArray = Int8Array(buffer, byteOffset, length).unsafeCast() \ No newline at end of file +internal actual val PlatformUtil: Util = object : AbstractUtil() {} \ No newline at end of file diff --git a/runtime/src/jsMain/kotlin/pbandk/internal/binary/BinaryMessageDecoder.js.kt b/runtime/src/jsMain/kotlin/pbandk/internal/binary/BinaryMessageDecoder.js.kt index e83e352c..942bc666 100644 --- a/runtime/src/jsMain/kotlin/pbandk/internal/binary/BinaryMessageDecoder.js.kt +++ b/runtime/src/jsMain/kotlin/pbandk/internal/binary/BinaryMessageDecoder.js.kt @@ -1,10 +1,9 @@ package pbandk.internal.binary import pbandk.MessageDecoder -import pbandk.internal.asUint8Array -import pbandk.protobufjs.ProtobufjsBinaryWireDecoder -import pbandk.protobufjs.Reader +import pbandk.internal.binary.kotlin.ByteArrayWireReader +import pbandk.internal.binary.kotlin.KotlinBinaryWireDecoder internal actual fun BinaryMessageDecoder.Companion.fromByteArray(arr: ByteArray): MessageDecoder { - return BinaryMessageDecoder(ProtobufjsBinaryWireDecoder(Reader.create(arr.asUint8Array()))) -} + return BinaryMessageDecoder(KotlinBinaryWireDecoder(ByteArrayWireReader(arr))) +} \ No newline at end of file diff --git a/runtime/src/jsMain/kotlin/pbandk/internal/binary/BinaryMessageEncoder.js.kt b/runtime/src/jsMain/kotlin/pbandk/internal/binary/BinaryMessageEncoder.js.kt index 4195c1bb..55496994 100644 --- a/runtime/src/jsMain/kotlin/pbandk/internal/binary/BinaryMessageEncoder.js.kt +++ b/runtime/src/jsMain/kotlin/pbandk/internal/binary/BinaryMessageEncoder.js.kt @@ -1,7 +1,7 @@ package pbandk.internal.binary -import pbandk.protobufjs.ProtobufjsBinaryMessageEncoder +import pbandk.internal.binary.kotlin.KotlinBinaryMessageEncoder internal actual fun BinaryMessageEncoder.Companion.allocate(size: Int): ByteArrayMessageEncoder { - return ProtobufjsBinaryMessageEncoder.allocate(size) + return KotlinBinaryMessageEncoder.allocate(size) } \ No newline at end of file diff --git a/runtime/src/jsMain/kotlin/pbandk/internal/binary/Sizer.js.kt b/runtime/src/jsMain/kotlin/pbandk/internal/binary/Sizer.js.kt index b4d382d5..e890b9ba 100644 --- a/runtime/src/jsMain/kotlin/pbandk/internal/binary/Sizer.js.kt +++ b/runtime/src/jsMain/kotlin/pbandk/internal/binary/Sizer.js.kt @@ -1,3 +1,3 @@ package pbandk.internal.binary -internal actual val PlatformSizer: Sizer = pbandk.protobufjs.ProtobufJsSizer \ No newline at end of file +internal actual val PlatformSizer: Sizer = object : AbstractSizer() {} diff --git a/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryMessageEncoder.kt b/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryMessageEncoder.kt deleted file mode 100644 index bf36b112..00000000 --- a/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryMessageEncoder.kt +++ /dev/null @@ -1,20 +0,0 @@ -package pbandk.protobufjs - -import pbandk.internal.asByteArray -import pbandk.internal.binary.BinaryMessageEncoder -import pbandk.internal.binary.ByteArrayMessageEncoder - -internal class ProtobufjsBinaryMessageEncoder private constructor( - private val writer: Writer, - private val expectedSize: Int -) : BinaryMessageEncoder(ProtobufjsBinaryWireEncoder(writer)), ByteArrayMessageEncoder { - override fun toByteArray(): ByteArray { - return writer.finish().asByteArray().also { - check(it.size == expectedSize) { "Expected $expectedSize, got ${it.size}" } - } - } - - companion object { - fun allocate(size: Int) = ProtobufjsBinaryMessageEncoder(Writer.create(), size) - } -} \ No newline at end of file diff --git a/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryWireDecoder.kt b/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryWireDecoder.kt deleted file mode 100644 index d65c7a1d..00000000 --- a/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryWireDecoder.kt +++ /dev/null @@ -1,81 +0,0 @@ -package pbandk.protobufjs - -import pbandk.* -import pbandk.internal.asByteArray -import pbandk.internal.binary.BinaryMessageDecoder -import pbandk.internal.binary.BinaryWireDecoder -import pbandk.internal.binary.Tag -import pbandk.internal.binary.WireType - -internal class ProtobufjsBinaryWireDecoder(private val reader: Reader) : BinaryWireDecoder { - private var endPos = reader.len - - override fun readTag(): Tag { - return if (reader.pos == endPos) { - Tag(0) - } else { - val tag = Tag(readUInt32()) - if (tag.fieldNumber == 0) throw InvalidProtocolBufferException("Invalid tag: $tag") - tag - } - } - - override fun readRawBytes(type: WireType): ByteArray { - val oldPos = reader.pos - reader.skipType(type.value) - return reader.buf.subarray(oldPos, reader.pos).asByteArray() - } - - override fun readDouble(): Double = reader.double() - - override fun readFloat(): Float = reader.float() - - override fun readInt32(): Int = reader.int32() - - override fun readInt64(): Long = Long.fromProtobufjsLong(reader.int64()) - - override fun readUInt32(): Int = reader.uint32() - - override fun readUInt64(): Long = Long.fromProtobufjsLong(reader.uint64()) - - override fun readSInt32(): Int = reader.sint32() - - override fun readSInt64(): Long = Long.fromProtobufjsLong(reader.sint64()) - - override fun readFixed32(): Int = reader.fixed32() - - override fun readFixed64(): Long = Long.fromProtobufjsLong(reader.fixed64()) - - override fun readSFixed32(): Int = reader.sfixed32() - - override fun readSFixed64(): Long = Long.fromProtobufjsLong(reader.sfixed64()) - - override fun readBool(): Boolean = reader.bool() - - override fun readString(): String = reader.string() - - override fun readBytes(): ByteArr = ByteArr(reader.bytes().asByteArray()) - - override fun readEnum(enumCompanion: Message.Enum.Companion): T = - enumCompanion.fromValue(reader.int32()) - - private fun readLength() = readUInt32() - - override fun readMessage(messageCompanion: Message.Companion): T { - val oldEndPos = endPos - endPos = readLength() + reader.pos - val message = messageCompanion.decodeWith(BinaryMessageDecoder(this)) - if (reader.pos != endPos) { - throw InvalidProtocolBufferException("Not at the end of the current message limit as expected") - } - endPos = oldEndPos - return message - } - - override fun readPackedRepeated(readFn: BinaryWireDecoder.() -> T): Sequence = sequence { - val length = readLength() - val endPos = reader.pos + length - while (reader.pos < endPos) yield(readFn()) - } - -} \ No newline at end of file diff --git a/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryWireEncoder.kt b/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryWireEncoder.kt deleted file mode 100644 index 93ceb6b0..00000000 --- a/runtime/src/jsMain/kotlin/pbandk/protobufjs/ProtobufjsBinaryWireEncoder.kt +++ /dev/null @@ -1,145 +0,0 @@ -package pbandk.protobufjs - -import pbandk.ByteArr -import pbandk.FieldDescriptor -import pbandk.ListWithSize -import pbandk.Message -import pbandk.internal.asUint8Array -import pbandk.internal.binary.BinaryWireEncoder -import pbandk.internal.binary.Tag -import pbandk.internal.binary.WireType - -private fun Writer.writeValueNoTag(type: FieldDescriptor.Type, value: Any) { - when (type) { - is FieldDescriptor.Type.Primitive.Double -> double(value as Double) - is FieldDescriptor.Type.Primitive.Float -> float(value as Float) - is FieldDescriptor.Type.Primitive.Int64 -> int64((value as Long).protobufjsLong) - is FieldDescriptor.Type.Primitive.UInt64 -> uint64((value as Long).protobufjsLong) - is FieldDescriptor.Type.Primitive.Int32 -> int32(value as Int) - is FieldDescriptor.Type.Primitive.Fixed64 -> fixed64((value as Long).protobufjsLong) - is FieldDescriptor.Type.Primitive.Fixed32 -> fixed32(value as Int) - is FieldDescriptor.Type.Primitive.Bool -> bool(value as Boolean) - is FieldDescriptor.Type.Primitive.String -> string(value as String) - is FieldDescriptor.Type.Primitive.Bytes -> bytes((value as ByteArr).array.asUint8Array()) - is FieldDescriptor.Type.Primitive.UInt32 -> uint32(value as Int) - is FieldDescriptor.Type.Primitive.SFixed32 -> sfixed32(value as Int) - is FieldDescriptor.Type.Primitive.SFixed64 -> sfixed64((value as Long).protobufjsLong) - is FieldDescriptor.Type.Primitive.SInt32 -> sint32(value as Int) - is FieldDescriptor.Type.Primitive.SInt64 -> sint64((value as Long).protobufjsLong) - is FieldDescriptor.Type.Message<*> -> error("writeValueNoTag() should only be called for primitive types") - is FieldDescriptor.Type.Enum<*> -> int32((value as Message.Enum).value) - is FieldDescriptor.Type.Repeated<*> -> error("writeValueNoTag() should only be called for primitive types") - is FieldDescriptor.Type.Map<*, *> -> error("writeValueNoTag() should only be called for primitive types") - } -} - -internal class ProtobufjsBinaryWireEncoder(private val writer: Writer) : BinaryWireEncoder { - private fun writeTag(fieldNum: Int, wireType: WireType) { - writer.uint32(Tag(fieldNum, wireType).value) - } - - override fun writeRawBytes(fieldNum: Int, wireType: WireType, value: ByteArray) { - writeTag(fieldNum, wireType) - writer._push({ bytes, buf, pos -> - buf.asUint8Array().set(bytes, pos) - }, value.size, value.asUint8Array()) - } - - override fun writeLengthDelimitedHeader(fieldNum: Int, protoSize: Int) { - writeTag(fieldNum, WireType.LENGTH_DELIMITED) - writer.uint32(protoSize) - } - - override fun writePackedRepeated(fieldNum: Int, list: List<*>, valueType: FieldDescriptor.Type) { - if (list is ListWithSize && list.protoSize != null) { - writeLengthDelimitedHeader(fieldNum, list.protoSize) - list.forEach { writer.writeValueNoTag(valueType, checkNotNull(it)) } - } else { - writeTag(fieldNum, WireType.LENGTH_DELIMITED) - writer.fork() - list.forEach { writer.writeValueNoTag(valueType, checkNotNull(it)) } - writer.ldelim() - } - } - - override fun writeInt32(fieldNum: Int, value: Int) { - writeTag(fieldNum, WireType.VARINT) - writer.int32(value) - } - - override fun writeInt64(fieldNum: Int, value: Long) { - writeTag(fieldNum, WireType.VARINT) - writer.int64(value.protobufjsLong) - } - - override fun writeUInt32(fieldNum: Int, value: Int) { - writeTag(fieldNum, WireType.VARINT) - writer.uint32(value) - } - - override fun writeUInt64(fieldNum: Int, value: Long) { - writeTag(fieldNum, WireType.VARINT) - writer.uint64(value.protobufjsLong) - } - - override fun writeSInt32(fieldNum: Int, value: Int) { - writeTag(fieldNum, WireType.VARINT) - writer.sint32(value) - } - - override fun writeSInt64(fieldNum: Int, value: Long) { - writeTag(fieldNum, WireType.VARINT) - writer.sint64(value.protobufjsLong) - } - - override fun writeBool(fieldNum: Int, value: Boolean) { - writeTag(fieldNum, WireType.VARINT) - writer.bool(value) - } - - override fun writeEnum(fieldNum: Int, value: Message.Enum) { - writeTag(fieldNum, WireType.VARINT) - writer.int32(value.value) - } - - override fun writeFixed32(fieldNum: Int, value: Int) { - writeTag(fieldNum, WireType.FIXED32) - writer.fixed32(value) - } - - override fun writeFixed64(fieldNum: Int, value: Long) { - writeTag(fieldNum, WireType.FIXED64) - writer.fixed64(value.protobufjsLong) - } - - override fun writeSFixed32(fieldNum: Int, value: Int) { - writeTag(fieldNum, WireType.FIXED32) - writer.sfixed32(value) - } - - override fun writeSFixed64(fieldNum: Int, value: Long) { - writeTag(fieldNum, WireType.FIXED64) - writer.sfixed64(value.protobufjsLong) - } - - override fun writeFloat(fieldNum: Int, value: Float) { - writeTag(fieldNum, WireType.FIXED32) - writer.float(value) - } - - override fun writeDouble(fieldNum: Int, value: Double) { - writeTag(fieldNum, WireType.FIXED64) - writer.double(value) - } - - override fun writeString(fieldNum: Int, value: String) { - writeTag(fieldNum, WireType.LENGTH_DELIMITED) - writer.string(value) - } - - override fun writeBytes(fieldNum: Int, value: ByteArr) { - writeTag(fieldNum, WireType.LENGTH_DELIMITED) - writer.bytes(value.array.asUint8Array()) - } -} - diff --git a/runtime/src/jsMain/kotlin/pbandk/protobufjs/Sizer.kt b/runtime/src/jsMain/kotlin/pbandk/protobufjs/Sizer.kt deleted file mode 100644 index 0a5c52dc..00000000 --- a/runtime/src/jsMain/kotlin/pbandk/protobufjs/Sizer.kt +++ /dev/null @@ -1,7 +0,0 @@ -package pbandk.protobufjs - -import pbandk.internal.binary.AbstractSizer - -internal object ProtobufJsSizer : AbstractSizer() { - override fun stringSize(value: String) = util.utf8.length(value).let { it + uInt32Size(it) } -} \ No newline at end of file diff --git a/runtime/src/jsMain/kotlin/pbandk/protobufjs/Util.kt b/runtime/src/jsMain/kotlin/pbandk/protobufjs/Util.kt deleted file mode 100644 index ba023c7c..00000000 --- a/runtime/src/jsMain/kotlin/pbandk/protobufjs/Util.kt +++ /dev/null @@ -1,32 +0,0 @@ -package pbandk.protobufjs - -import pbandk.internal.AbstractUtil -import pbandk.internal.asUint8Array - -internal val Long.protobufjsLong: dynamic - get() { - val ret = js("{}") - // Kotlin doesn't provide access to the low/high bits that it uses as its internal representation in Kotlin/JS. - // So we have to manually extract them using bit fiddling. - ret.high = (this ushr 32).toInt() - ret.low = (this and 0xffffffffL).toInt() - return ret - } - -internal fun Long.Companion.fromProtobufjsLong(l: dynamic): Long { - return if (l.low == null || l.high == null) { - (l as Int).toLong() - } else { - // Kotlin doesn't provide a Long constructor that takes low/high bits separately, so we have to manually - // construct the Long with bit fiddling. - ((l.high as Int).toLong() shl 32) or ((l.low as Int).toLong() and 0xffffffffL) - } -} - -internal object ProtobufJsUtil : AbstractUtil() { - override fun base64ToBytes(str: String): ByteArray = ByteArray(util.base64.length(str)).also { - util.base64.decode(str, it.asUint8Array(), 0) - } - - override fun bytesToBase64(bytes: ByteArray): String = bytes.asUint8Array().let { util.base64.encode(it, 0, it.length) } -} \ No newline at end of file diff --git a/runtime/src/jsMain/kotlin/pbandk/protobufjs/protobufjs.kt b/runtime/src/jsMain/kotlin/pbandk/protobufjs/protobufjs.kt deleted file mode 100644 index ac00150a..00000000 --- a/runtime/src/jsMain/kotlin/pbandk/protobufjs/protobufjs.kt +++ /dev/null @@ -1,83 +0,0 @@ -@file:JsModule("protobufjs/light") -@file:JsNonModule -package pbandk.protobufjs - -import org.khronos.webgl.Uint8Array - -internal external class Reader { - val buf: Uint8Array - val len: Int - val pos: Int - - fun bool(): Boolean - fun bytes(): Uint8Array - fun double(): Double - fun fixed32(): Int - fun fixed64(): dynamic - fun float(): Float - fun int32(): Int - fun int64(): dynamic - fun sfixed32(): Int - fun sfixed64(): dynamic - fun sint32(): Int - fun sint64(): dynamic - fun string(): String - fun uint32(): Int - fun uint64(): dynamic - - fun skipType(wireType: Int): Reader - - companion object { - fun create(buf: Uint8Array): Reader - } -} - -internal external class Writer { - val len: Int - - fun bool(value: Boolean): Writer - fun bytes(value: Uint8Array): Writer - fun double(value: Double): Writer - fun fixed32(value: Int): Writer - fun fixed64(value: dynamic): Writer - fun float(value: Float): Writer - fun fork(): Writer - fun int32(value: Int): Writer - fun int64(value: dynamic): Writer - fun ldelim(): Writer - fun reset(): Writer - fun sfixed32(value: Int): Writer - fun sfixed64(value: dynamic): Writer - fun sint32(value: Int): Writer - fun sint64(value: dynamic): Writer - fun string(value: String): Writer - fun uint32(value: Int): Writer - fun uint64(value: dynamic): Writer - fun _push(func: (value: T, buf: ByteArray, pos: Int) -> Unit, len: Int, value: T) - - - fun finish(): Uint8Array - - companion object { - fun create(): Writer - } -} - -internal external class util { - class base64 { - companion object { - fun decode(string: String, buffer: Uint8Array, offset: Int): Int - fun encode(buffer: Uint8Array, start: Int, end: Int): String - fun length(string: String): Int - fun test(string: String): Boolean - } - } - - class utf8 { - companion object { - fun length(str: String): Int - fun write(str: String, arr: Uint8Array, offset: Int): Int - fun read(buf: Uint8Array, start: Int, end: Int): String - } - } -} \ No newline at end of file