Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ gem 'webauthn', '~> 2.5.2'
gem 'xmldsig', '~> 0.6'
gem 'xmlenc', '~> 0.7', '>= 0.7.1'
gem 'yard', require: false
gem 'zlib', require: false
Comment thread
zachmargolis marked this conversation as resolved.

# This version of the zxcvbn gem matches the data and behavior of the zxcvbn NPM package.
# It should not be updated without verifying that the behavior still matches JS version 4.4.2.
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ GEM
nokogiri (~> 1.8)
yard (0.9.34)
zeitwerk (2.6.12)
zlib (3.0.0)
zonebie (0.6.1)
zxcvbn (0.1.9)

Expand Down Expand Up @@ -825,6 +826,7 @@ DEPENDENCIES
xmldsig (~> 0.6)
xmlenc (~> 0.7, >= 0.7.1)
yard
zlib
zonebie
zxcvbn (= 0.1.9)

Expand Down
20 changes: 19 additions & 1 deletion lib/script_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ def initialize(argv:, stdout:, stderr:, subtask_class:, banner:)
:format,
:show_help,
:requesting_issuers,
:deflate,
keyword_init: true,
) do
alias_method :include_missing?, :include_missing
alias_method :show_help?, :show_help
alias_method :deflate?, :deflate
end

def config
Expand All @@ -37,6 +39,7 @@ def config
format: :table,
show_help: false,
requesting_issuers: [],
deflate: false,
)
end

Expand All @@ -59,13 +62,23 @@ def run
stderr.puts "*Messages*:\n#{result.messages.map { |message| " • #{message}" }.join("\n")}"
end

if result.json
if config.deflate?
require 'zlib'
require 'base64'
stdout.puts Base64.encode64(
Zlib::Deflate.deflate(
(result.json || result.table).to_json,
Zlib::BEST_COMPRESSION,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did BEST_COMPRESSION make a significant difference from the defaults?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have MessagePack if we'd rather avoid the Base64 encoding

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: BEST_COMPRESSION, I didn't try others
re: messagepack, I will give it a shot! I thought it was a binary format for some reason

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirmed that message pack is binary, I think it'll run into the same issue that the binary DEFLATE data had, which is that SSM command seems to eat it.

The original reason I chose JSON was this blog post that seemed to indicate JSON + GZIP yielded smaller payloads than MSGPACK

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah got it, sounds good!

),
)
elsif result.json
stdout.puts result.json.to_json
else
self.class.render_output(result.table, format: config.format, stdout: stdout)
end
end

# rubocop:disable Metrics/BlockLength
def option_parser
@option_parser ||= OptionParser.new do |opts|
opts.banner = banner
Expand All @@ -92,13 +105,18 @@ def option_parser
config.format = :json
end

opts.on('--deflate', 'Use DEFLATE compression on the output') do
config.deflate = true
end

opts.on('--[no-]include-missing', <<~STR) do |include_missing|
Whether or not to add rows in the output for missing inputs, defaults to on
STR
config.include_missing = include_missing
end
end
end
# rubocop:enable Metrics/BlockLength

# @param [Array<Array<String>>] rows
def self.render_output(rows, format:, stdout: STDOUT)
Expand Down
48 changes: 48 additions & 0 deletions spec/lib/script_base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'
require 'script_base'

RSpec.describe ScriptBase do
let(:subtask_class) do
Class.new do
def run(args:, config:) # rubocop:disable Lint/UnusedMethodArgument
ScriptBase::Result.new(
table: [
%w[header1 header2],
%w[value1 value2],
],
subtask: 'example-subtask',
uuids: %w[a b c],
)
end
end
end

let(:stdout) { StringIO.new }
let(:stderr) { StringIO.new }

describe '#run' do
let(:argv) { [] }

subject(:base) do
ScriptBase.new(
argv:,
stdout:,
stderr:,
subtask_class:,
banner: '',
)
end

context 'with --deflate' do
let(:argv) { %w[--deflate] }

it 'applies DEFLATE compression to the output' do
base.run

table = subtask_class.new.run(args: nil, config: nil).table

expect(JSON.parse(Zlib::Inflate.inflate(Base64.decode64(stdout.string)))).to eq(table)
end
end
end
end