Skip to content

Commit

Permalink
#464 Remove usage 'radix_encoding' since it has too many transitive d…
Browse files Browse the repository at this point in the history
…ependencies
  • Loading branch information
pepijnve committed Jun 16, 2024
1 parent 11a08f0 commit 2704fe6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Enhancements::

* Issue #423: Add support for loading JSyntrax via the `asciidoctor-diagram-jsyntrax` gem (@inponomarev)
* Issue #464: Replace usage of 'base64' gem with 'radix_encoding' to resolve Ruby 3.3+ compatibility issues
* Issue #464: Remove usage of 'base64' gem to resolve Ruby 3.3+ compatibility issues

Bugfixes::

Expand Down
1 change: 0 additions & 1 deletion asciidoctor-diagram.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'asciidoctor', '>= 1.5.7', '< 3.x'
s.add_runtime_dependency 'asciidoctor-diagram-ditaamini', '~> 1.0'
s.add_runtime_dependency 'asciidoctor-diagram-plantuml', '~> 1.2021'
s.add_runtime_dependency 'radix_encoding', '0.2.0'
s.add_runtime_dependency 'rexml'
end
26 changes: 9 additions & 17 deletions lib/asciidoctor-diagram/http/converter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative '../diagram_converter'
require_relative '../util/base64'
require_relative '../util/platform'

require 'radix_encoding'
require 'net/http'
require 'uri'
require 'zlib'
Expand All @@ -12,18 +12,6 @@ module Diagram
class HttpConverter
DEFAULT_MAX_GET_SIZE = 1024

PLANTUML_ENCODING = RadixEncoding::Encoding.new(
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_",
radix: 64,
padding: "="
)

BASE64_URL_SAFE = RadixEncoding::Encoding.new(
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
radix: 64,
padding: "="
)

include DiagramConverter

def initialize(base_uri, type, converter)
Expand Down Expand Up @@ -59,16 +47,20 @@ def convert(source, format, options)
compressed = deflate.deflate(code, Zlib::FINISH)
deflate.close



data = PLANTUML_ENCODING.encode(compressed)
data = Base64.urlsafe_encode(compressed)
# See https://plantuml.com/text-encoding
# PlantUML uses a different alphabet than the one from RFC 4648
data.tr!(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
)

path = uri.path.dup
path << '/' unless path.end_with? '/'
path << format.to_s
when :kroki_io
compressed = Zlib.deflate(code, Zlib::BEST_COMPRESSION)
data = BASE64_URL_SAFE.encode(compressed)
data = Base64.urlsafe_encode(compressed)

path = uri.path.dup
path << '/' unless path.end_with? '/'
Expand Down
3 changes: 2 additions & 1 deletion lib/asciidoctor-diagram/http/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require_relative '../diagram_source'
require_relative '../graphviz/converter'
require_relative '../util/base64'
require_relative '../util/which'

module Asciidoctor
Expand All @@ -13,7 +14,7 @@ class Server < Sinatra::Base
type = params['type']
accepts = lambda { |t| params['format'].downcase.to_sym == t }
raw_source = params['source']
decoded_source = Asciidoctor::Diagram::HttpConverter::BASE64_URL_SAFE.decode(raw_source)
decoded_source = Base64.urlsafe_decode(raw_source)
decompressed_source = Zlib::Inflate.inflate(decoded_source)
source = decompressed_source
render_diagram(type, accepts, source, {})
Expand Down
25 changes: 25 additions & 0 deletions lib/asciidoctor-diagram/util/base64.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Asciidoctor
module Diagram
module Base64
def self.urlsafe_encode(bin, padding: true)
str = [bin].pack("m0")
str.chomp!("==") or str.chomp!("=") unless padding
str.tr!("+/", "-_")
str
end

def self.urlsafe_decode(str)
# NOTE: RFC 4648 does say nothing about unpadded input, but says that
# "the excess pad characters MAY also be ignored", so it is inferred that
# unpadded input is also acceptable.
if !str.end_with?("=") && str.length % 4 != 0
str = str.ljust((str.length + 3) & ~3, "=")
str.tr!("-_", "+/")
else
str = str.tr("-_", "+/")
end
str.unpack1("m0")
end
end
end
end

0 comments on commit 2704fe6

Please sign in to comment.