-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.rb
More file actions
964 lines (887 loc) · 26.8 KB
/
util.rb
File metadata and controls
964 lines (887 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# frozen_string_literal: true
module Imagekitio
module Internal
# @api private
module Util
# @api private
#
# @return [Float]
def self.monotonic_secs = Process.clock_gettime(Process::CLOCK_MONOTONIC)
# @api private
#
# @param ns [Module, Class]
#
# @return [Enumerable<Module, Class>]
def self.walk_namespaces(ns)
ns.constants(false).lazy.flat_map do
case (c = ns.const_get(_1, false))
in Module | Class
walk_namespaces(c)
else
[]
end
end
.chain([ns])
end
class << self
# @api private
#
# @return [String]
def arch
case (arch = RbConfig::CONFIG["arch"])&.downcase
in nil
"unknown"
in /aarch64|arm64/
"arm64"
in /x86_64/
"x64"
in /arm/
"arm"
else
"other:#{arch}"
end
end
# @api private
#
# @return [String]
def os
case (host = RbConfig::CONFIG["host_os"])&.downcase
in nil
"Unknown"
in /linux/
"Linux"
in /darwin/
"MacOS"
in /freebsd/
"FreeBSD"
in /openbsd/
"OpenBSD"
in /mswin|mingw|cygwin|ucrt/
"Windows"
else
"Other:#{host}"
end
end
end
class << self
# @api private
#
# @param input [Object]
#
# @return [Boolean]
def primitive?(input)
case input
in true | false | Numeric | Symbol | String
true
else
false
end
end
# @api private
#
# @param input [String, Boolean]
#
# @return [Boolean, Object]
def coerce_boolean(input)
case input.is_a?(String) ? input.downcase : input
in "true"
true
in "false"
false
else
input
end
end
# @api private
#
# @param input [String, Boolean]
#
# @raise [ArgumentError]
# @return [Boolean, nil]
def coerce_boolean!(input)
case coerce_boolean(input)
in true | false | nil => coerced
coerced
else
raise ArgumentError.new("Unable to coerce #{input.inspect} into boolean value")
end
end
# @api private
#
# @param input [String, Integer]
#
# @return [Integer, Object]
def coerce_integer(input)
Integer(input, exception: false) || input
end
# @api private
#
# @param input [String, Integer, Float]
#
# @return [Float, Object]
def coerce_float(input)
Float(input, exception: false) || input
end
# @api private
#
# @param input [Object]
#
# @return [Hash{Object=>Object}, Object]
def coerce_hash(input)
case input
in NilClass | Array | Set | Enumerator | StringIO | IO
input
else
input.respond_to?(:to_h) ? input.to_h : input
end
end
# @api private
#
# @param input [Object]
#
# @raise [ArgumentError]
# @return [Hash{Object=>Object}, nil]
def coerce_hash!(input)
case coerce_hash(input)
in Hash | nil => coerced
coerced
else
message = "Expected a #{Hash} or #{Imagekitio::Internal::Type::BaseModel}, got #{input.inspect}"
raise ArgumentError.new(message)
end
end
end
class << self
# @api private
#
# @param lhs [Object]
# @param rhs [Object]
# @param concat [Boolean]
#
# @return [Object]
private def deep_merge_lr(lhs, rhs, concat: false)
case [lhs, rhs, concat]
in [Hash, Hash, _]
lhs.merge(rhs) { deep_merge_lr(_2, _3, concat: concat) }
in [Array, Array, true]
lhs.concat(rhs)
else
rhs
end
end
# @api private
#
# Recursively merge one hash with another. If the values at a given key are not
# both hashes, just take the new value.
#
# @param values [Array<Object>]
#
# @param sentinel [Object, nil] the value to return if no values are provided.
#
# @param concat [Boolean] whether to merge sequences by concatenation.
#
# @return [Object]
def deep_merge(*values, sentinel: nil, concat: false)
case values
in [value, *values]
values.reduce(value) do |acc, val|
deep_merge_lr(acc, val, concat: concat)
end
else
sentinel
end
end
# @api private
#
# @param data [Hash{Symbol=>Object}, Array<Object>, Object]
# @param pick [Symbol, Integer, Array<Symbol, Integer>, Proc, nil]
# @param blk [Proc, nil]
#
# @return [Object, nil]
def dig(data, pick, &blk)
case [data, pick]
in [_, nil]
data
in [Hash, Symbol] | [Array, Integer]
data.fetch(pick) { blk&.call }
in [Hash | Array, Array]
pick.reduce(data) do |acc, key|
case acc
in Hash if acc.key?(key)
acc.fetch(key)
in Array if key.is_a?(Integer) && key < acc.length
acc[key]
else
return blk&.call
end
end
in [_, Proc]
pick.call(data)
else
blk&.call
end
end
end
# @type [Regexp]
#
# https://www.rfc-editor.org/rfc/rfc3986.html#section-3.3
RFC_3986_NOT_PCHARS = /[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/
class << self
# @api private
#
# @param uri [URI::Generic]
#
# @return [String]
def uri_origin(uri)
"#{uri.scheme}://#{uri.host}#{":#{uri.port}" unless uri.port == uri.default_port}"
end
# @api private
#
# @param path [String, Integer]
#
# @return [String]
def encode_path(path)
path.to_s.gsub(Imagekitio::Internal::Util::RFC_3986_NOT_PCHARS) { ERB::Util.url_encode(_1) }
end
# @api private
#
# @param path [String, Array<String>]
#
# @return [String]
def interpolate_path(path)
case path
in String
path
in []
""
in [String => p, *interpolations]
encoded = interpolations.map { encode_path(_1) }
format(p, *encoded)
end
end
end
class << self
# @api private
#
# @param query [String, nil]
#
# @return [Hash{String=>Array<String>}]
def decode_query(query)
CGI.parse(query.to_s)
end
# @api private
#
# @param query [Hash{String=>Array<String>, String, nil}, nil]
#
# @return [String, nil]
def encode_query(query)
query.to_h.empty? ? nil : URI.encode_www_form(query)
end
end
class << self
# @api private
#
# @param url [URI::Generic, String]
#
# @return [Hash{Symbol=>String, Integer, nil}]
def parse_uri(url)
parsed = URI::Generic.component.zip(URI.split(url)).to_h
{**parsed, query: decode_query(parsed.fetch(:query))}
end
# @api private
#
# @param parsed [Hash{Symbol=>String, Integer, nil}] .
#
# @option parsed [String, nil] :scheme
#
# @option parsed [String, nil] :host
#
# @option parsed [Integer, nil] :port
#
# @option parsed [String, nil] :path
#
# @option parsed [Hash{String=>Array<String>}] :query
#
# @return [URI::Generic]
def unparse_uri(parsed)
URI::Generic.build(**parsed, query: encode_query(parsed.fetch(:query)))
end
# @api private
#
# @param lhs [Hash{Symbol=>String, Integer, nil}] .
#
# @option lhs [String, nil] :scheme
#
# @option lhs [String, nil] :host
#
# @option lhs [Integer, nil] :port
#
# @option lhs [String, nil] :path
#
# @option lhs [Hash{String=>Array<String>}] :query
#
# @param rhs [Hash{Symbol=>String, Integer, nil}] .
#
# @option rhs [String, nil] :scheme
#
# @option rhs [String, nil] :host
#
# @option rhs [Integer, nil] :port
#
# @option rhs [String, nil] :path
#
# @option rhs [Hash{String=>Array<String>}] :query
#
# @return [URI::Generic]
def join_parsed_uri(lhs, rhs)
base_path, base_query = lhs.fetch_values(:path, :query)
slashed = base_path.end_with?("/") ? base_path : "#{base_path}/"
merged = {**parse_uri(rhs.fetch(:path)), **rhs.except(:path, :query)}
parsed_path, parsed_query = merged.fetch_values(:path, :query)
override = URI::Generic.build(**merged.slice(:scheme, :host, :port), path: parsed_path)
joined = URI.join(URI::Generic.build(lhs.except(:path, :query)), slashed, override)
query = deep_merge(
joined.path == base_path ? base_query : {},
parsed_query,
rhs[:query].to_h,
concat: true
)
joined.query = encode_query(query)
joined
end
end
class << self
# @api private
#
# @param headers [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}]
#
# @return [Hash{String=>String}]
def normalized_headers(*headers)
{}.merge(*headers.compact).to_h do |key, val|
value =
case val
in Array
val.filter_map { _1&.to_s&.strip }.join(", ")
else
val&.to_s&.strip
end
[key.downcase, value]
end
end
end
# @api private
#
# An adapter that satisfies the IO interface required by `::IO.copy_stream`
class ReadIOAdapter
# @api private
#
# @return [Boolean, nil]
def close? = @closing
# @api private
def close
case @stream
in Enumerator
Imagekitio::Internal::Util.close_fused!(@stream)
in IO if close?
@stream.close
else
end
end
# @api private
#
# @param max_len [Integer, nil]
#
# @return [String]
private def read_enum(max_len)
case max_len
in nil
@stream.to_a.join
in Integer
@buf << @stream.next while @buf.length < max_len
@buf.slice!(..max_len)
end
rescue StopIteration
@stream = nil
@buf.slice!(0..)
end
# @api private
#
# @param max_len [Integer, nil]
# @param out_string [String, nil]
#
# @return [String, nil]
def read(max_len = nil, out_string = nil)
case @stream
in nil
nil
in IO | StringIO
@stream.read(max_len, out_string)
in Enumerator
read = read_enum(max_len)
case out_string
in String
out_string.replace(read)
in nil
read
end
end
.tap(&@blk)
end
# @api private
#
# @param src [String, Pathname, StringIO, Enumerable<String>]
# @param blk [Proc]
#
# @yieldparam [String]
def initialize(src, &blk)
@stream =
case src
in String
StringIO.new(src)
in Pathname
@closing = true
src.open(binmode: true)
else
src
end
@buf = String.new
@blk = blk
end
end
class << self
# @param blk [Proc]
#
# @yieldparam [Enumerator::Yielder]
# @return [Enumerable<String>]
def writable_enum(&blk)
Enumerator.new do |y|
y.define_singleton_method(:write) do
self << _1.dup
_1.bytesize
end
blk.call(y)
end
end
end
# @type [Regexp]
JSON_CONTENT = %r{^application/(?:[a-zA-Z0-9.-]+\+)?json(?!l)}
# @type [Regexp]
JSONL_CONTENT = %r{^application/(:?x-(?:n|l)djson)|(:?(?:x-)?jsonl)}
class << self
# @api private
#
# @param query [Hash{Symbol=>Object}]
#
# @return [Hash{Symbol=>Object}]
def encode_query_params(query)
out = {}
query.each { write_query_param_element!(out, _1, _2) }
out
end
# @api private
#
# @param collection [Hash{Symbol=>Object}]
# @param key [String]
# @param element [Object]
#
# @return [nil]
private def write_query_param_element!(collection, key, element)
case element
in Hash
element.each do |name, value|
write_query_param_element!(collection, "#{key}[#{name}]", value)
end
in Array
collection[key] = element.map(&:to_s).join(",")
else
collection[key] = element.to_s
end
end
# @api private
#
# @param y [Enumerator::Yielder]
# @param val [Object]
# @param closing [Array<Proc>]
# @param content_type [String, nil]
private def write_multipart_content(y, val:, closing:, content_type: nil)
content_line = "Content-Type: %s\r\n\r\n"
case val
in Imagekitio::FilePart
return write_multipart_content(
y,
val: val.content,
closing: closing,
content_type: val.content_type
)
in Pathname
y << format(content_line, content_type || "application/octet-stream")
io = val.open(binmode: true)
closing << io.method(:close)
IO.copy_stream(io, y)
in IO
y << format(content_line, content_type || "application/octet-stream")
IO.copy_stream(val, y)
in StringIO
y << format(content_line, content_type || "application/octet-stream")
y << val.string
in -> { primitive?(_1) }
y << format(content_line, content_type || "text/plain")
y << val.to_s
else
y << format(content_line, content_type || "application/json")
y << JSON.generate(val)
end
y << "\r\n"
end
# @api private
#
# @param y [Enumerator::Yielder]
# @param boundary [String]
# @param key [Symbol, String]
# @param val [Object]
# @param closing [Array<Proc>]
private def write_multipart_chunk(y, boundary:, key:, val:, closing:)
y << "--#{boundary}\r\n"
y << "Content-Disposition: form-data"
unless key.nil?
y << "; name=\"#{key}\""
end
case val
in Imagekitio::FilePart unless val.filename.nil?
filename = encode_path(val.filename)
y << "; filename=\"#{filename}\""
in Pathname | IO
filename = encode_path(::File.basename(val.to_path))
y << "; filename=\"#{filename}\""
else
end
y << "\r\n"
write_multipart_content(y, val: val, closing: closing)
end
# @api private
#
# https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.1.md#special-considerations-for-multipart-content
#
# @param body [Object]
#
# @return [Array(String, Enumerable<String>)]
private def encode_multipart_streaming(body)
# RFC 1521 Section 7.2.1 says we should have 70 char maximum for boundary length
boundary = SecureRandom.urlsafe_base64(46)
closing = []
strio = writable_enum do |y|
case body
in Hash
body.each do |key, val|
case val
in Array if val.all? { primitive?(_1) }
val.each do |v|
write_multipart_chunk(y, boundary: boundary, key: key, val: v, closing: closing)
end
else
write_multipart_chunk(y, boundary: boundary, key: key, val: val, closing: closing)
end
end
else
write_multipart_chunk(y, boundary: boundary, key: nil, val: body, closing: closing)
end
y << "--#{boundary}--\r\n"
end
fused_io = fused_enum(strio) { closing.each(&:call) }
[boundary, fused_io]
end
# @api private
#
# @param headers [Hash{String=>String}]
# @param body [Object]
#
# @return [Object]
def encode_content(headers, body)
# rubocop:disable Style/CaseEquality
# rubocop:disable Layout/LineLength
content_type = headers["content-type"]
case [content_type, body]
in [Imagekitio::Internal::Util::JSON_CONTENT, Hash | Array | -> { primitive?(_1) }]
[headers, JSON.generate(body)]
in [Imagekitio::Internal::Util::JSONL_CONTENT, Enumerable] unless Imagekitio::Internal::Type::FileInput === body
[headers, body.lazy.map { JSON.generate(_1) }]
in [%r{^multipart/form-data}, Hash | Imagekitio::Internal::Type::FileInput]
boundary, strio = encode_multipart_streaming(body)
headers = {**headers, "content-type" => "#{content_type}; boundary=#{boundary}"}
[headers, strio]
in [_, Symbol | Numeric]
[headers, body.to_s]
in [_, StringIO]
[headers, body.string]
in [_, Imagekitio::FilePart]
[headers, body.content]
else
[headers, body]
end
# rubocop:enable Layout/LineLength
# rubocop:enable Style/CaseEquality
end
# @api private
#
# https://www.iana.org/assignments/character-sets/character-sets.xhtml
#
# @param content_type [String]
# @param text [String]
def force_charset!(content_type, text:)
charset = /charset=([^;\s]+)/.match(content_type)&.captures&.first
return unless charset
begin
encoding = Encoding.find(charset)
text.force_encoding(encoding)
rescue ArgumentError
nil
end
end
# @api private
#
# Assumes each chunk in stream has `Encoding::BINARY`.
#
# @param headers [Hash{String=>String}]
# @param stream [Enumerable<String>]
# @param suppress_error [Boolean]
#
# @raise [JSON::ParserError]
# @return [Object]
def decode_content(headers, stream:, suppress_error: false)
case (content_type = headers["content-type"])
in Imagekitio::Internal::Util::JSON_CONTENT
return nil if (json = stream.to_a.join).empty?
begin
JSON.parse(json, symbolize_names: true)
rescue JSON::ParserError => e
raise e unless suppress_error
json
end
in Imagekitio::Internal::Util::JSONL_CONTENT
lines = decode_lines(stream)
chain_fused(lines) do |y|
lines.each do
next if _1.empty?
y << JSON.parse(_1, symbolize_names: true)
end
end
in %r{^text/event-stream}
lines = decode_lines(stream)
decode_sse(lines)
else
text = stream.to_a.join
force_charset!(content_type, text: text)
StringIO.new(text)
end
end
end
class << self
# @api private
#
# https://doc.rust-lang.org/std/iter/trait.FusedIterator.html
#
# @param enum [Enumerable<Object>]
# @param external [Boolean]
# @param close [Proc]
#
# @return [Enumerable<Object>]
def fused_enum(enum, external: false, &close)
fused = false
iter = Enumerator.new do |y|
next if fused
fused = true
if external
loop { y << enum.next }
else
enum.each(&y)
end
ensure
close&.call
close = nil
end
iter.define_singleton_method(:rewind) do
fused = true
self
end
iter
end
# @api private
#
# @param enum [Enumerable<Object>, nil]
def close_fused!(enum)
return unless enum.is_a?(Enumerator)
# rubocop:disable Lint/UnreachableLoop
enum.rewind.each { break }
# rubocop:enable Lint/UnreachableLoop
end
# @api private
#
# @param enum [Enumerable<Object>, nil]
# @param blk [Proc]
#
# @yieldparam [Enumerator::Yielder]
# @return [Enumerable<Object>]
def chain_fused(enum, &blk)
iter = Enumerator.new { blk.call(_1) }
fused_enum(iter) { close_fused!(enum) }
end
end
class << self
# @api private
#
# Assumes Strings have been forced into having `Encoding::BINARY`.
#
# This decoder is responsible for reassembling lines split across multiple
# fragments.
#
# @param enum [Enumerable<String>]
#
# @return [Enumerable<String>]
def decode_lines(enum)
re = /(\r\n|\r|\n)/
buffer = String.new
cr_seen = nil
chain_fused(enum) do |y|
enum.each do |row|
offset = buffer.bytesize
buffer << row
while (match = re.match(buffer, cr_seen&.to_i || offset))
case [match.captures.first, cr_seen]
in ["\r", nil]
cr_seen = match.end(1)
next
in ["\r" | "\r\n", Integer]
y << buffer.slice!(..(cr_seen.pred))
else
y << buffer.slice!(..(match.end(1).pred))
end
offset = 0
cr_seen = nil
end
end
y << buffer.slice!(..(cr_seen.pred)) unless cr_seen.nil?
y << buffer unless buffer.empty?
end
end
# @api private
#
# https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
#
# Assumes that `lines` has been decoded with `#decode_lines`.
#
# @param lines [Enumerable<String>]
#
# @return [Enumerable<Hash{Symbol=>Object}>]
def decode_sse(lines)
# rubocop:disable Metrics/BlockLength
chain_fused(lines) do |y|
blank = {event: nil, data: nil, id: nil, retry: nil}
current = {}
lines.each do |line|
case line.sub(/\R$/, "")
in ""
next if current.empty?
y << {**blank, **current}
current = {}
in /^:/
next
in /^([^:]+):\s?(.*)$/
field, value = Regexp.last_match.captures
case field
in "event"
current.merge!(event: value)
in "data"
(current[:data] ||= String.new) << (value << "\n")
in "id" unless value.include?("\0")
current.merge!(id: value)
in "retry" if /^\d+$/ =~ value
current.merge!(retry: Integer(value))
else
end
else
end
end
# rubocop:enable Metrics/BlockLength
y << {**blank, **current} unless current.empty?
end
end
end
# @api private
module SorbetRuntimeSupport
class MissingSorbetRuntimeError < ::RuntimeError
end
# @api private
#
# @return [Hash{Symbol=>Object}]
private def sorbet_runtime_constants = @sorbet_runtime_constants ||= {}
# @api private
#
# @param name [Symbol]
def const_missing(name)
super unless sorbet_runtime_constants.key?(name)
unless Object.const_defined?(:T)
message = "Trying to access a Sorbet constant #{name.inspect} without `sorbet-runtime`."
raise MissingSorbetRuntimeError.new(message)
end
sorbet_runtime_constants.fetch(name).call
end
# @api private
#
# @param name [Symbol]
#
# @return [Boolean]
def sorbet_constant_defined?(name) = sorbet_runtime_constants.key?(name)
# @api private
#
# @param name [Symbol]
# @param blk [Proc]
def define_sorbet_constant!(name, &blk) = sorbet_runtime_constants.store(name, blk)
# @api private
#
# @return [Object]
def to_sorbet_type = raise NotImplementedError
class << self
# @api private
#
# @param type [Imagekitio::Internal::Util::SorbetRuntimeSupport, Object]
#
# @return [Object]
def to_sorbet_type(type)
case type
in Imagekitio::Internal::Util::SorbetRuntimeSupport
type.to_sorbet_type
in Class | Module
type
in true | false
T::Boolean
else
type.class
end
end
end
end
extend Imagekitio::Internal::Util::SorbetRuntimeSupport
define_sorbet_constant!(:ParsedUri) do
T.type_alias do
{
scheme: T.nilable(String),
host: T.nilable(String),
port: T.nilable(Integer),
path: T.nilable(String),
query: T::Hash[String, T::Array[String]]
}
end
end
define_sorbet_constant!(:ServerSentEvent) do
T.type_alias do
{
event: T.nilable(String),
data: T.nilable(String),
id: T.nilable(String),
retry: T.nilable(Integer)
}
end
end
end
end
end