console 1.21.0 → 1.23.0

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: 36a5fff3c1cafc8686f77440fc2dcb09d3a94d1399b15b9bf6967e4b21e6a511
4
- data.tar.gz: 3c7e6218667002f2a0f745bcfddbeec1dc8b126fdd1b81cec71fa03c53c3eba5
3
+ metadata.gz: d9f91e3e103d4d8999923473901b90855e7c8ee5a2f1bf5fa7be4b87a435e0e2
4
+ data.tar.gz: 78cc14aa29facd5ef1909fc08bcd255d11c251f1075dbe92692f26a249557cbf
5
5
  SHA512:
6
- metadata.gz: b4d6ebc41b6ea36a711f5f054a30a81ff4a29d06e0f4a2ef6a6554f6c94fd874439428df40e7a098f4bec25a8605452c2380987d1e275e38e6cf522254fdc3b8
7
- data.tar.gz: 693ed3f6a7d6786a87aef0ff42d238a3cbd1fc267c0cafb1683b8236e0f878bb7a8dc316822f00ab84cdba7e6f58212e8474d0e85109d67ff59a65b3003548f3
6
+ metadata.gz: bda52b2ed24d9825905e8e929809d9629ba61755059d7e00ef8ceb693839a191d98647bc13fd2c3a7699f8f664560e4f35c316e21bb9e37c24b8d1265c31dd91
7
+ data.tar.gz: 22a7f6c0be0617140c608b5063c25d3dd220670afc570860f0cd54fea36f889c3b128d168f9dd6bf92c01c271e335b4d73dfd06cfe918f2360911aa8991fcfba
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2023, by Samuel Williams.
5
+
6
+ require 'json'
7
+
8
+ module Console
9
+ module Format
10
+ # This class is used to safely dump objects.
11
+ # It will attempt to dump the object using the given format, but if it fails, it will generate a safe version of the object.
12
+ class Safe
13
+ def initialize(format: ::JSON, limit: 8, encoding: ::Encoding::UTF_8)
14
+ @format = format
15
+ @limit = limit
16
+ @encoding = encoding
17
+ end
18
+
19
+ def dump(object)
20
+ @format.dump(object, @limit)
21
+ rescue => error
22
+ @format.dump(safe_dump(object, error))
23
+ end
24
+
25
+ private
26
+
27
+ def default_objects
28
+ Hash.new.compare_by_identity
29
+ end
30
+
31
+ def safe_dump(object, error)
32
+ object = safe_dump_recurse(object)
33
+
34
+ object[:truncated] = true
35
+ object[:error] = {
36
+ class: safe_dump_recurse(error.class.name),
37
+ message: safe_dump_recurse(error.message),
38
+ }
39
+
40
+ return object
41
+ end
42
+
43
+ def replacement_for(object)
44
+ case object
45
+ when Array
46
+ "[...]"
47
+ when Hash
48
+ "{...}"
49
+ else
50
+ "..."
51
+ end
52
+ end
53
+
54
+ # This will recursively generate a safe version of the object.
55
+ # Nested hashes and arrays will be transformed recursively.
56
+ # Strings will be encoded with the given encoding.
57
+ # Primitive values will be returned as-is.
58
+ # Other values will be converted using `as_json` if available, otherwise `to_s`.
59
+ def safe_dump_recurse(object, limit = @limit, objects = default_objects)
60
+ if limit <= 0 || objects[object]
61
+ return replacement_for(object)
62
+ end
63
+
64
+ case object
65
+ when Hash
66
+ objects[object] = true
67
+
68
+ object.to_h do |key, value|
69
+ [
70
+ String(key).encode(@encoding, invalid: :replace, undef: :replace),
71
+ safe_dump_recurse(value, limit - 1, objects)
72
+ ]
73
+ end
74
+ when Array
75
+ objects[object] = true
76
+
77
+ object.map do |value|
78
+ safe_dump_recurse(value, limit - 1, objects)
79
+ end
80
+ when String
81
+ object.encode(@encoding, invalid: :replace, undef: :replace)
82
+ when Numeric, TrueClass, FalseClass, NilClass
83
+ object
84
+ else
85
+ objects[object] = true
86
+
87
+ # We could do something like this but the chance `as_json` will blow up.
88
+ # We'd need to be extremely careful about it.
89
+ # if object.respond_to?(:as_json)
90
+ # safe_dump_recurse(object.as_json, limit - 1, objects)
91
+ # else
92
+
93
+ safe_dump_recurse(object.to_s, limit - 1, objects)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2023, by Samuel Williams.
5
+
6
+ require_relative 'format/safe'
7
+
8
+ module Console
9
+ module Format
10
+ def self.default
11
+ Safe.new(format: ::JSON)
12
+ end
13
+
14
+ def self.default_json
15
+ self.default
16
+ end
17
+ end
18
+ end
@@ -4,16 +4,12 @@
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
12
11
  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
- )
12
+ Serialized::Logger.new(output, format: Format.default_json, **options)
17
13
  end
18
14
  end
19
15
  end
@@ -5,16 +5,16 @@
5
5
 
6
6
  require_relative '../buffer'
7
7
  require_relative '../filter'
8
+ require_relative '../format'
8
9
 
9
10
  require 'time'
10
- require 'json'
11
11
 
12
12
  require 'fiber/annotation'
13
13
 
14
14
  module Console
15
15
  module Serialized
16
16
  class Logger
17
- def initialize(io = $stderr, format: JSON, verbose: false, **options)
17
+ def initialize(io = $stderr, format: Format.default, verbose: false, **options)
18
18
  @io = io
19
19
  @start = Time.now
20
20
  @format = format
@@ -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.23.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.23.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
@@ -95,10 +95,11 @@ files:
95
95
  - lib/console/event/progress.rb
96
96
  - lib/console/event/spawn.rb
97
97
  - lib/console/filter.rb
98
+ - lib/console/format.rb
99
+ - lib/console/format/safe.rb
98
100
  - lib/console/logger.rb
99
101
  - lib/console/output.rb
100
102
  - lib/console/output/default.rb
101
- - lib/console/output/encoder.rb
102
103
  - lib/console/output/json.rb
103
104
  - lib/console/output/null.rb
104
105
  - lib/console/output/sensitive.rb
metadata.gz.sig CHANGED
@@ -1,6 +1,3 @@
1
-
2
- ��a�o[����y܈�*^�z���:
3
- U.":��
4
- ��]�cf�3��^6�/>�5���*?m��N����V��{�/>��ͱ����0������D~����/�/. @��pJ��m��g�
5
- yy��3�\nъ���vq�Nt9r:� �A�����%d��+
6
- ���d�o�+?���́�jC�\lj��Ѝ%وӨp��{�tr�R���b�������p��L�xH������XwTV� ��?��Wt�m�^B%���i��-//} ���k cX�^S<��寷��a�I��1����R�=����<��F�unZ�s�M���m��B�vE+��L���ǾI�`�ä��IX����R�`(U�Rf�k�="��Nq�5fh
1
+ G)Q�f���&5Te��� �t<��ӗoⰲV��Z��#�o
2
+ #�*O~��Ѩip&;��N��~����q%�t0�<�d��^m5V2�fגQ�~t/��e�el�U@��m�lB��zWx+��\ 2�nτaI{�n*Go��X�(�+5���J$d7�S ���3��~w���5�{`�x�d?";'⹐��Ptпf���t%��
3
+ NR��=�9΃�(Z��H'�`xI(2�Gɂa��u�d��V0�p�Y�v��Ews�����'���� ·���ğ���[�J��bi;q�\N�6�5���Hc|YY�B�o�׏Lbmp؃.>{Q-��E
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2023, by Samuel Williams.
5
-
6
- module Console
7
- module Output
8
- class Encoder
9
- def initialize(output, encoding = ::Encoding::UTF_8)
10
- @output = output
11
- @encoding = encoding
12
- end
13
-
14
- attr :output
15
-
16
- attr :encoding
17
-
18
- def call(subject = nil, *arguments, **options, &block)
19
- subject = encode(subject)
20
- arguments = encode(arguments)
21
- options = encode(options)
22
-
23
- @output.call(subject, *arguments, **options, &block)
24
- end
25
-
26
- def encode(value)
27
- case value
28
- when String
29
- value.encode(@encoding, invalid: :replace, undef: :replace)
30
- when Array
31
- value.map{|item| encode(item)}
32
- when Hash
33
- value.transform_values{|item| encode(item)}
34
- else
35
- value
36
- end
37
- end
38
- end
39
- end
40
- end