fieldnotes 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 051a7d164daa3ba6ba44017ff456fd3a22ead9099214d42246c6a32a899774e3
4
- data.tar.gz: 36341a205392979e4b19326e9df0f0dbe98edbc08d23cc209e175082678ffba5
3
+ metadata.gz: 27817e1f4e717e5f7a608fbb693af125382bf7586bca6fcb624bb1c5255f2063
4
+ data.tar.gz: a8d23cbde0b053e558e290703df8d4ad7e6da8626e2e4b2d95f9d2fee30ae23e
5
5
  SHA512:
6
- metadata.gz: b59af62097b48f82c47dbc523e6f0f67329fd938a56f302c8acc86f54929b8997152e2e1aa3990b2b35118a79806b37f2078bee1389804d0d90b2985eb68f760
7
- data.tar.gz: b0a0dad4f36a5d34d54f77e105963b97dc1f82054eb2cf0744507ba1624b4d5bf0d36cbecda10c31bd6d75ac727957724c599af015079f5ed602e0bf5bd2970a
6
+ metadata.gz: d88d5b8836d6258533da8971a111f4b34b9226d4ac05cae1bf1cce76e3035331d4d0fa8d0753387b6f6af0ee199bd572415e178fab9a2641c9240083e7a06138
7
+ data.tar.gz: c1f1e5922146ad11dc7709c8624175f55dd7d42ca61e0f31f8a197ca5e7d5dab10667c92f7075875dabaada63b7164996b1c0ad578073476e6d2c996a9a0722a
@@ -50,8 +50,33 @@ module FieldNotes
50
50
  out.force_encoding("UTF-8")
51
51
  end
52
52
 
53
+ WIRE_MAX = 255
54
+
55
+ # Raw payload bytes that still fit one name: a name is limited to 255 bytes on the wire, and the
56
+ # zone plus the dots between labels are fixed costs. Cutting the message at 1200 characters
57
+ # produced names that could not be sent at all.
58
+ def self.payload_budget(zone = ZONE, max_label = 63)
59
+ clean = zone.to_s.sub(/\A\./, "")
60
+ wire = WIRE_MAX - (clean.empty? ? 0 : clean.bytesize + 1)
61
+ wire.downto(1) do |b32|
62
+ labels = [(b32 + max_label - 1) / max_label, 1].max
63
+ return (b32 * 5) / 8 if b32 + labels - 1 <= wire
64
+ end
65
+ 0
66
+ end
67
+
68
+ # Cut to a byte budget without splitting a character: byteslice may land inside a multi-byte
69
+ # sequence, and scrub drops the broken tail.
70
+ def self.truncate_bytes(text, limit)
71
+ raw = text.to_s.encode(Encoding::UTF_8)
72
+ return raw if raw.bytesize <= limit
73
+
74
+ raw.byteslice(0, limit).to_s.scrub("")
75
+ end
76
+
53
77
  def self.encode_name(from, msg, zone: ZONE)
54
- payload = b32encode("#{from.to_s[0, 40]}|#{msg.to_s[0, 1200]}")
78
+ text = "#{from.to_s[0, 40]}|#{msg.to_s[0, 1200]}"
79
+ payload = b32encode(truncate_bytes(text, payload_budget(zone)))
55
80
  "#{payload.scan(/.{1,63}/).join('.')}.#{zone}"
56
81
  end
57
82
 
@@ -1,3 +1,3 @@
1
1
  module FieldNotes
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fieldnotes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - field-notes