From 62cab6ad45803f0430fcd19d0e9d6ccba6283d59 Mon Sep 17 00:00:00 2001 From: jhara1418 Date: Fri, 29 Sep 2023 15:13:27 -0700 Subject: [PATCH 1/5] patch --- lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php b/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php index e102237..c2dd7c0 100644 --- a/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php +++ b/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php @@ -176,8 +176,6 @@ public static function encode( $cursor_size = strlen($encoded); $context = $context ?? 'unknown'; - StreamBuilder::getDependencyBag()->getLog() - ->histogramTick('cursor_size', $context, ($cursor_size / 1000.0)); if (($cache_provider instanceof CacheProvider) && ($cursor_size > $cache_size_threshold)) { StreamBuilder::getDependencyBag()->getLog() @@ -190,8 +188,14 @@ public static function encode( $encoded = $cache_codec->encode($cursor); // base64 url encode cache encoded as well, to line up with binary codec encoded logic. $encoded = Helpers::base64UrlEncode($encoded); + + // Update the cursor size + $cursor_size = strlen($encoded); } + StreamBuilder::getDependencyBag()->getLog() + ->histogramTick('cursor_size', $context, ($cursor_size / 1000.0)); + return $encoded; } From 2476171790e68fdbb7b9cedf302f1647ac075865 Mon Sep 17 00:00:00 2001 From: jhara1418 Date: Fri, 29 Sep 2023 15:33:28 -0700 Subject: [PATCH 2/5] update --- lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php b/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php index c2dd7c0..df86bee 100644 --- a/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php +++ b/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php @@ -189,8 +189,10 @@ public static function encode( // base64 url encode cache encoded as well, to line up with binary codec encoded logic. $encoded = Helpers::base64UrlEncode($encoded); - // Update the cursor size - $cursor_size = strlen($encoded); + // This is an implementation detail, but the CacheCodec does not base64 encode the payload before + // writing to cache, so the original cursor_size is misleading. Base64 encoding can increase the + // payload size by 33%. + $cursor_size = strlen(Helpers::json_encode($cursor->to_template())); } StreamBuilder::getDependencyBag()->getLog() From 3f67ddf355c15c3f730e2e5b3c3d31682bffbae7 Mon Sep 17 00:00:00 2001 From: jhara1418 Date: Fri, 29 Sep 2023 15:56:49 -0700 Subject: [PATCH 3/5] update --- lib/Tumblr/StreamBuilder/Codec/BinaryCodec.php | 10 ++++++++-- lib/Tumblr/StreamBuilder/Codec/CacheCodec.php | 11 ++++++++++- lib/Tumblr/StreamBuilder/Codec/Codec.php | 6 +++--- .../StreamBuilder/StreamCursors/StreamCursor.php | 9 +++++---- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/Tumblr/StreamBuilder/Codec/BinaryCodec.php b/lib/Tumblr/StreamBuilder/Codec/BinaryCodec.php index 04727a8..f3a4eef 100644 --- a/lib/Tumblr/StreamBuilder/Codec/BinaryCodec.php +++ b/lib/Tumblr/StreamBuilder/Codec/BinaryCodec.php @@ -103,9 +103,15 @@ public function __construct( /** * @inheritDoc */ - public function encode(Templatable $obj): string + public function encode($obj): string { - $json = Helpers::json_encode($obj->to_template()); + if ($obj instanceof Templatable) { + $json = Helpers::json_encode($obj->to_template()); + } elseif (is_string($obj)) { + $json = $obj; + } else { + throw new \InvalidArgumentException(sprintf("%s type is not supported", get_class($obj))); + } $compressed = gzdeflate($json); $encrypted = openssl_encrypt($compressed, self::CIPHER, $this->encrypt_key, 0, $this->initial_vector); $signature = $this->compute_signature($encrypted); diff --git a/lib/Tumblr/StreamBuilder/Codec/CacheCodec.php b/lib/Tumblr/StreamBuilder/Codec/CacheCodec.php index 5e364f4..68c5a20 100644 --- a/lib/Tumblr/StreamBuilder/Codec/CacheCodec.php +++ b/lib/Tumblr/StreamBuilder/Codec/CacheCodec.php @@ -41,6 +41,12 @@ final class CacheCodec extends Codec */ public const SERIALIZATION_TYPE_JSON = 'JSON'; + /** + * Cache content from a serialized JSON object + * @var string + */ + public const SERIALIZATION_TYPE_JSON_STRING = 'JSON_STRING'; + /** * Cache content serialized/deserialized by doing serialize/unserialize * @var string @@ -80,12 +86,15 @@ public function __construct( /** * @inheritDoc */ - public function encode(Templatable $obj): string + public function encode($obj): string { switch ($this->serialization_type) { case self::SERIALIZATION_TYPE_JSON: $serialized = Helpers::json_encode($obj->to_template()); break; + case self::SERIALIZATION_TYPE_JSON_STRING: + $serialized = $obj; + break; case self::SERIALIZATION_TYPE_PHP_OBJECT: $serialized = serialize($obj); break; diff --git a/lib/Tumblr/StreamBuilder/Codec/Codec.php b/lib/Tumblr/StreamBuilder/Codec/Codec.php index d42cbf4..956fd2c 100644 --- a/lib/Tumblr/StreamBuilder/Codec/Codec.php +++ b/lib/Tumblr/StreamBuilder/Codec/Codec.php @@ -81,11 +81,11 @@ public function __construct(CacheProvider $cache_provider = null) } /** - * Encode a templatable object. - * @param Templatable $obj The object to encode. + * Encode an object. + * @param mixed $obj The object to encode. * @return string The encoded bytes string. */ - abstract public function encode(Templatable $obj): string; + abstract public function encode($obj): string; /** * Decode an encoded templatable object. diff --git a/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php b/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php index df86bee..494a6c2 100644 --- a/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php +++ b/lib/Tumblr/StreamBuilder/StreamCursors/StreamCursor.php @@ -171,7 +171,8 @@ public static function encode( $current_user_id ); - $encoded = $binary_codec->encode($cursor); + $json = Helpers::json_encode($cursor->to_template()); + $encoded = $binary_codec->encode($json); $encoded = Helpers::base64UrlEncode($encoded); $cursor_size = strlen($encoded); @@ -183,16 +184,16 @@ public static function encode( $cache_codec = new CacheCodec( $cache_provider, CacheProvider::OBJECT_TYPE_CURSOR, - CacheCodec::SERIALIZATION_TYPE_JSON + CacheCodec::SERIALIZATION_TYPE_JSON_STRING ); - $encoded = $cache_codec->encode($cursor); + $encoded = $cache_codec->encode($json); // base64 url encode cache encoded as well, to line up with binary codec encoded logic. $encoded = Helpers::base64UrlEncode($encoded); // This is an implementation detail, but the CacheCodec does not base64 encode the payload before // writing to cache, so the original cursor_size is misleading. Base64 encoding can increase the // payload size by 33%. - $cursor_size = strlen(Helpers::json_encode($cursor->to_template())); + $cursor_size = strlen($json); } StreamBuilder::getDependencyBag()->getLog() From 436b3c184c724a274c8380b298878950dde1bfb8 Mon Sep 17 00:00:00 2001 From: kchro3 <62481661+kchro3@users.noreply.github.com> Date: Tue, 3 Oct 2023 15:32:34 -0700 Subject: [PATCH 4/5] Update lib/Tumblr/StreamBuilder/Codec/Codec.php Co-authored-by: Alexey Kopytko --- lib/Tumblr/StreamBuilder/Codec/Codec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Tumblr/StreamBuilder/Codec/Codec.php b/lib/Tumblr/StreamBuilder/Codec/Codec.php index 956fd2c..1787814 100644 --- a/lib/Tumblr/StreamBuilder/Codec/Codec.php +++ b/lib/Tumblr/StreamBuilder/Codec/Codec.php @@ -82,7 +82,7 @@ public function __construct(CacheProvider $cache_provider = null) /** * Encode an object. - * @param mixed $obj The object to encode. + * @param Templatable|string $obj The object to encode, or pre-encoded object. * @return string The encoded bytes string. */ abstract public function encode($obj): string; From 9eb056b6441599bf67a61a294317acf2a865e811 Mon Sep 17 00:00:00 2001 From: kchro3 <62481661+kchro3@users.noreply.github.com> Date: Tue, 3 Oct 2023 15:32:55 -0700 Subject: [PATCH 5/5] Update lib/Tumblr/StreamBuilder/Codec/CacheCodec.php Co-authored-by: cyle gage --- lib/Tumblr/StreamBuilder/Codec/CacheCodec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Tumblr/StreamBuilder/Codec/CacheCodec.php b/lib/Tumblr/StreamBuilder/Codec/CacheCodec.php index 68c5a20..e38ea98 100644 --- a/lib/Tumblr/StreamBuilder/Codec/CacheCodec.php +++ b/lib/Tumblr/StreamBuilder/Codec/CacheCodec.php @@ -42,7 +42,7 @@ final class CacheCodec extends Codec public const SERIALIZATION_TYPE_JSON = 'JSON'; /** - * Cache content from a serialized JSON object + * Cache content from an already-serialized string of JSON. * @var string */ public const SERIALIZATION_TYPE_JSON_STRING = 'JSON_STRING';