vigiles 0.1.0.pre.beta9 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/vigiles/archive/request.rb +2 -2
- data/lib/vigiles/conversation_recorders/application_json.rb +0 -1
- data/lib/vigiles/middleware/record_conversation.rb +36 -10
- data/lib/vigiles/options.rb +19 -0
- data/lib/vigiles/types.rb +4 -2
- data/lib/vigiles/utilities/json.rb +17 -0
- data/lib/vigiles/version.rb +1 -1
- data/lib/vigiles.rb +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa4d66607b27003c6f7aa0e7a2371d97e2b982ca63d829791d7cc2b042aa75a1
|
4
|
+
data.tar.gz: c2a5a05744d8ef06ba60ee4185e8fa80b9c7f76f35d2dbfd0f4503e3abe70539
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77578a9fce6e3acb35e99c0d4d8323ec6485681a80f0236de6960d8e52cb1f6c501b065fa96447f6c09e6cddd5d011eaa9475806d96544236b15a0336e9dc7a1
|
7
|
+
data.tar.gz: 7496e7124c6f4511893d5299fa4918913203d060ba946c109e0eb712bc4f685a6e74c1ea440b902097fd8dcff053f73e929ae6e019ec887c86028e6a3ddf44c4
|
@@ -48,7 +48,7 @@ module Vigiles
|
|
48
48
|
preferred_headers = Vigiles.spec.request_headers
|
49
49
|
available_headers = request.original_headers
|
50
50
|
recorded_headers = (available_headers if preferred_headers.empty?)
|
51
|
-
recorded_headers ||= preferred_headers.to_h {
|
51
|
+
recorded_headers ||= preferred_headers.to_h { [_1, available_headers[_1]] }
|
52
52
|
unfucked_headers = recorded_headers.transform_keys { best_effort_unfuck_http_header _1 }
|
53
53
|
|
54
54
|
Request.new(
|
@@ -59,7 +59,7 @@ module Vigiles
|
|
59
59
|
protocol: request.protocol,
|
60
60
|
headers: unfucked_headers,
|
61
61
|
origin: request.origin || "unknown_origin_url",
|
62
|
-
payload: request.body.read,
|
62
|
+
payload: Utilities::JSON.parse_benignly(request.body.read),
|
63
63
|
http_method: Types::HttpMethod.deserialize(request.method),
|
64
64
|
path: request.path,
|
65
65
|
url: Utilities::URI.parse_into_http_or_https(request.url),
|
@@ -1,16 +1,24 @@
|
|
1
1
|
# typed: strict
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
require "logger"
|
5
4
|
module Vigiles
|
6
5
|
module Middleware
|
7
6
|
class RecordConversation
|
8
|
-
sig {
|
9
|
-
|
10
|
-
|
7
|
+
sig { returns(Vigiles::Options) }
|
8
|
+
attr_reader :options
|
9
|
+
|
10
|
+
delegate \
|
11
|
+
:capture_exception,
|
12
|
+
:logger,
|
13
|
+
to: :options
|
14
|
+
|
15
|
+
sig { params(app: T.untyped, options: Vigiles::Options).void }
|
16
|
+
def initialize(app, options=Vigiles::Options.make_default_options)
|
17
|
+
@app = app
|
18
|
+
@options = options
|
11
19
|
end
|
12
20
|
|
13
|
-
sig { params(env: T.untyped).returns(T.untyped) }
|
21
|
+
sig { params(env: T::Hash[T.untyped, T.untyped]).returns(T.untyped) }
|
14
22
|
def call(env)
|
15
23
|
req = ActionDispatch::Request.new(env)
|
16
24
|
record_conversation(req) do
|
@@ -20,11 +28,29 @@ module Vigiles
|
|
20
28
|
|
21
29
|
sig { params(req: ActionDispatch::Request, blk: T.proc.returns(T.untyped)).returns(T.untyped) }
|
22
30
|
private def record_conversation(req, &blk)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
rack_response = blk.call
|
32
|
+
begin
|
33
|
+
res = Rack::Response[*rack_response]
|
34
|
+
convo = Vigiles.maybe_record_conversation(req:, res:)
|
35
|
+
logger.info \
|
36
|
+
"[vigiles] conversation recorder: " \
|
37
|
+
"conversation=#{convo.nil? ? "not_recorded" : convo.id} " \
|
38
|
+
"request=#{req.request_id}"
|
39
|
+
rescue => e
|
40
|
+
capture_exception.call(e)
|
41
|
+
logger.warn \
|
42
|
+
"[vigiles] conversation recorder error: " \
|
43
|
+
"error_message=#{e.message} " \
|
44
|
+
"error_class=#{e.class}"
|
45
|
+
ensure
|
46
|
+
# no matter what happens we shouldn't prevent the rack
|
47
|
+
# response from being returned. at this point, the only
|
48
|
+
# thing that could throw execution into this branch is
|
49
|
+
# an exception when the `capture_exception` proc is
|
50
|
+
# invoked.
|
51
|
+
rack_response
|
52
|
+
end
|
53
|
+
rack_response
|
28
54
|
end
|
29
55
|
end
|
30
56
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# typed: strict
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "logger"
|
5
|
+
|
6
|
+
module Vigiles
|
7
|
+
class Options < T::Struct
|
8
|
+
const :capture_exception, T.proc.params(a0: StandardError).void
|
9
|
+
const :logger, ::Logger
|
10
|
+
|
11
|
+
sig { returns(Options) }
|
12
|
+
def self.make_default_options
|
13
|
+
Options.new(
|
14
|
+
logger: T.unsafe(Rails).logger, # you should be using this within rails.
|
15
|
+
capture_exception: ->(e) { e } # a no-op exception capturer.
|
16
|
+
)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/vigiles/types.rb
CHANGED
@@ -24,10 +24,12 @@ module Vigiles
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
Headers = T.type_alias { T::Hash[String, T.untyped] }
|
28
|
-
JsonPayload = T.type_alias { T::Hash[T.untyped, T.untyped] }
|
29
27
|
HtmlPayload = String
|
28
|
+
|
29
|
+
UntypedHash = T.type_alias { T::Hash[T.untyped, T.untyped] }
|
30
|
+
JsonPayload = T.type_alias { UntypedHash }
|
30
31
|
Payload = T.type_alias { T.any(JsonPayload, HtmlPayload) }
|
32
|
+
Headers = T.type_alias { T::Hash[String, T.untyped] }
|
31
33
|
|
32
34
|
ContentTypeRecorder = T.type_alias do
|
33
35
|
T::Hash[String, T.proc.params(arg0: ActionDispatch::Response).returns(Vigiles::Archive::Conversation)]
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# typed: strict
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module Vigies
|
7
|
+
module Utilities
|
8
|
+
module JSON
|
9
|
+
sig { params(text: String).returns(T.any(String, Vigiles::Types::UntypedHash)) }
|
10
|
+
def self.parse_benignly(text)
|
11
|
+
::JSON.parse(text)
|
12
|
+
rescue StandardError
|
13
|
+
text
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/vigiles/version.rb
CHANGED
data/lib/vigiles.rb
CHANGED
@@ -3,11 +3,11 @@
|
|
3
3
|
|
4
4
|
require "zeitwerk"
|
5
5
|
require "sorbet-runtime"
|
6
|
-
require_relative "core_ext"
|
7
6
|
require "action_dispatch"
|
7
|
+
require_relative "core_ext"
|
8
8
|
|
9
9
|
loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
10
|
-
loader.inflector.inflect("uri" => "URI")
|
10
|
+
loader.inflector.inflect("uri" => "URI", "json" => "JSON")
|
11
11
|
loader.ignore("#{__dir__}/generators")
|
12
12
|
loader.ignore("#{__dir__}/core_ext.rb")
|
13
13
|
loader.ignore("#{__dir__}/core_ext")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vigiles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yaw Boakye
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-05-
|
11
|
+
date: 2024-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sorbet-runtime
|
@@ -225,8 +225,10 @@ files:
|
|
225
225
|
- lib/vigiles/conversation_recorders/application_json.rb
|
226
226
|
- lib/vigiles/conversation_recorders/unknown.rb
|
227
227
|
- lib/vigiles/middleware/record_conversation.rb
|
228
|
+
- lib/vigiles/options.rb
|
228
229
|
- lib/vigiles/spec.rb
|
229
230
|
- lib/vigiles/types.rb
|
231
|
+
- lib/vigiles/utilities/json.rb
|
230
232
|
- lib/vigiles/utilities/uri.rb
|
231
233
|
- lib/vigiles/version.rb
|
232
234
|
- sorbet/config
|