console 1.21.0 → 1.22.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36a5fff3c1cafc8686f77440fc2dcb09d3a94d1399b15b9bf6967e4b21e6a511
4
- data.tar.gz: 3c7e6218667002f2a0f745bcfddbeec1dc8b126fdd1b81cec71fa03c53c3eba5
3
+ metadata.gz: 9f868615689827a90fa20cb73c9408598bd6cfc9602b21ee939a318213a3aee1
4
+ data.tar.gz: 44927a1200deac00fab718aa995f0549eec0301b688a440da83ae13fd87edc33
5
5
  SHA512:
6
- metadata.gz: b4d6ebc41b6ea36a711f5f054a30a81ff4a29d06e0f4a2ef6a6554f6c94fd874439428df40e7a098f4bec25a8605452c2380987d1e275e38e6cf522254fdc3b8
7
- data.tar.gz: 693ed3f6a7d6786a87aef0ff42d238a3cbd1fc267c0cafb1683b8236e0f878bb7a8dc316822f00ab84cdba7e6f58212e8474d0e85109d67ff59a65b3003548f3
6
+ metadata.gz: eabb7c42bcb625726e75a7ebc1e7f330b7dcb0b8936b012c96c79b8a5456f0ac2b3fea5d02cdb1511fde9bd696c7ba300b6394952138810824cb18bdbe8e8e6e
7
+ data.tar.gz: 1afa8d02eb8e56c81ac39f925e6a5df7edad41198c8aa255eb418847076913b6965b7d5794ba04ed6d5d35b08a1785e8825e12d32a8398f4203f7197584670c1
checksums.yaml.gz.sig CHANGED
Binary file
@@ -5,6 +5,7 @@
5
5
 
6
6
  module Console
7
7
  module Output
8
+ # @deprecated With no replacement.
8
9
  class Encoder
9
10
  def initialize(output, encoding = ::Encoding::UTF_8)
10
11
  @output = output
@@ -4,16 +4,98 @@
4
4
  # Copyright, 2021-2022, by Samuel Williams.
5
5
 
6
6
  require_relative '../serialized/logger'
7
- require_relative 'encoder'
8
7
 
9
8
  module Console
10
9
  module Output
11
10
  module JSON
11
+ # This is a safe JSON serializer that will not raise exceptions.
12
+ class Safe
13
+ def initialize(limit: 8, encoding: ::Encoding::UTF_8)
14
+ @limit = limit
15
+ @encoding = encoding
16
+ end
17
+
18
+ def dump(object)
19
+ ::JSON.dump(object, @limit)
20
+ rescue => error
21
+ ::JSON.dump(safe_dump(object, error))
22
+ end
23
+
24
+ private
25
+
26
+ def default_objects
27
+ Hash.new.compare_by_identity
28
+ end
29
+
30
+ def safe_dump(object, error)
31
+ object = safe_dump_recurse(object)
32
+
33
+ object[:truncated] = true
34
+ object[:error] = {
35
+ class: safe_dump_recurse(error.class.name),
36
+ message: safe_dump_recurse(error.message),
37
+ }
38
+
39
+ return object
40
+ end
41
+
42
+ def replacement_for(object)
43
+ case object
44
+ when Array
45
+ "[...]"
46
+ when Hash
47
+ "{...}"
48
+ else
49
+ "..."
50
+ end
51
+ end
52
+
53
+ # This will recursively generate a safe version of the object.
54
+ # Nested hashes and arrays will be transformed recursively.
55
+ # Strings will be encoded with the given encoding.
56
+ # Primitive values will be returned as-is.
57
+ # Other values will be converted using `as_json` if available, otherwise `to_s`.
58
+ def safe_dump_recurse(object, limit = @limit, objects = default_objects)
59
+ if limit <= 0 || objects[object]
60
+ return replacement_for(object)
61
+ end
62
+
63
+ case object
64
+ when Hash
65
+ objects[object] = true
66
+
67
+ object.to_h do |key, value|
68
+ [
69
+ String(key).encode(@encoding, invalid: :replace, undef: :replace),
70
+ safe_dump_recurse(value, limit - 1, objects)
71
+ ]
72
+ end
73
+ when Array
74
+ objects[object] = true
75
+
76
+ object.map do |value|
77
+ safe_dump_recurse(value, limit - 1, objects)
78
+ end
79
+ when String
80
+ object.encode(@encoding, invalid: :replace, undef: :replace)
81
+ when Numeric, TrueClass, FalseClass, NilClass
82
+ object
83
+ else
84
+ objects[object] = true
85
+
86
+ # We could do something like this but the chance `as_json` will blow up.
87
+ # We'd need to be extremely careful about it.
88
+ # if object.respond_to?(:as_json)
89
+ # safe_dump_recurse(object.as_json, limit - 1, objects)
90
+ # else
91
+
92
+ safe_dump_recurse(object.to_s, limit - 1, objects)
93
+ end
94
+ end
95
+ end
96
+
12
97
  def self.new(output, **options)
13
- # The output encoder can prevent encoding issues (e.g. invalid UTF-8):
14
- Output::Encoder.new(
15
- Serialized::Logger.new(output, format: ::JSON, **options)
16
- )
98
+ Serialized::Logger.new(output, format: Safe.new, **options)
17
99
  end
18
100
  end
19
101
  end
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2019-2023, by Samuel Williams.
5
5
 
6
6
  module Console
7
- VERSION = "1.21.0"
7
+ VERSION = "1.22.0"
8
8
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.21.0
4
+ version: 1.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -46,7 +46,7 @@ cert_chain:
46
46
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
47
47
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
48
48
  -----END CERTIFICATE-----
49
- date: 2023-08-07 00:00:00.000000000 Z
49
+ date: 2023-08-11 00:00:00.000000000 Z
50
50
  dependencies:
51
51
  - !ruby/object:Gem::Dependency
52
52
  name: fiber-annotation
metadata.gz.sig CHANGED
Binary file