module FirefoxJson::JsFile
Saves and restores files
Constants
- MAX_SIZE
- MOZ_ID
- MOZ_PREFIX
- OFFSETS
Public Class Methods
compress(string)
click to toggle source
# File lib/firefox-json/js_file.rb, line 50 def self.compress(string) size = string.bytesize if size > MAX_SIZE raise FileTooLarge, 'Content over 4GB!' end sstr = OFFSETS.map do |offset| part = size / offset size %= offset part end.reverse.pack('c*') MOZ_ID + sstr + LZ4.block_encode(string) end
decompress(string)
click to toggle source
# File lib/firefox-json/js_file.rb, line 38 def self.decompress(string) string.force_encoding(Encoding::BINARY) size = string[8, 4].bytes.zip(OFFSETS.reverse).map do |byte, offset| byte * offset end.sum string = LZ4.block_decode(string[MOZ_PREFIX..-1]) if string.bytesize != size raise "Expected size #{size} != #{string.bytesize}" end string.force_encoding(Encoding::UTF_8) end
load(string)
click to toggle source
# File lib/firefox-json/js_file.rb, line 18 def self.load(string) if string[0, MOZ_ID.size] == MOZ_ID string = decompress(string) end Oj.load(string, mode: :strict) end
load_file(path)
click to toggle source
# File lib/firefox-json/js_file.rb, line 14 def self.load_file(path) load(IO.read(path)) end
save(path, data)
click to toggle source
# File lib/firefox-json/js_file.rb, line 25 def self.save(path, data) string = Oj.dump(data, mode: :strict) if path.end_with?('jsonlz4') File.open(path, 'wb') do |file| file.write(compress(string)) end else File.open(path, 'w') do |file| file.write(string) end end end