thunderbolt 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a505429fd8ef2a2bc599354c42a8884261f1e2c7dac2763ec8e7a2994c6d5fff
4
+ data.tar.gz: 6f9402b1b97988597a920b38b20ea6d19598edc4888549237920d15977074039
5
+ SHA512:
6
+ metadata.gz: 0a9b4ce5c31f73cac0569926a9ce2cb9ecc64ed65a603ecf72ab141947fd3d89acab65edef9254121e2d302f6d6f237ed74e9061759955e5b092aa5aa4a0485f
7
+ data.tar.gz: 37e0be69244b75d10b55fdd9b65e94433c771d5c3bc4d90b73a25357cea6f6cbeb875cfb81c87e9a5a3406b2524ac21a6a404c85a6506f4bbe4e4ebeb9af35bf
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-10-19
4
+
5
+ - Initial release
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Thunderbolt
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/thunderbolt`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/thunderbolt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/thunderbolt/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Thunderbolt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/thunderbolt/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thunderbolt
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "thunderbolt/version"
4
+ require "rubygems"
5
+ require "eventmachine"
6
+ require "debug"
7
+ require "json"
8
+ require "active_support/all"
9
+
10
+ module Thunderbolt
11
+ class Error < StandardError; end
12
+ # Your code goes here...
13
+
14
+ module WebSocketServer
15
+ def self.connections
16
+ @connections ||= []
17
+ end
18
+
19
+ def post_init
20
+ WebSocketServer.connections << self
21
+ end
22
+
23
+ def receive_data(data)
24
+ puts "Data:"
25
+ puts data
26
+
27
+ headers = parse_headers(data)
28
+
29
+ frames = headers == {} ? parse_frames(data) : []
30
+
31
+ frames.each do |e|
32
+ puts e[:decoded_payload]
33
+ end
34
+
35
+ send_data build_response(headers) if frames.empty?
36
+ end
37
+
38
+ def parse_headers(data)
39
+ return {} unless data.lines[0].include?("HTTP/1.1")
40
+
41
+ found_data_separator = false
42
+
43
+ data.lines[1..].map do |line|
44
+ found_data_separator = line == "\r\n"
45
+
46
+ found_data_separator ? nil : line.chomp.split(": ", 2)
47
+ end.compact.to_h
48
+ end
49
+
50
+ def parse_frames(data)
51
+ first_frame = parse_frame(data)
52
+
53
+ result = (parse_frames(first_frame[:packed_byte_array]) unless first_frame[:packed_byte_array].blank?)
54
+
55
+ [first_frame, result].compact.flatten
56
+ end
57
+
58
+ def parse_frame(data)
59
+ return if data.empty?
60
+
61
+ byte_array = data.unpack("C*")
62
+
63
+ info_byte_int = byte_array.shift(1).first
64
+ length_byte_int = byte_array.shift(1).first
65
+ mask_bytes = byte_array.shift(4)
66
+
67
+ length = (length_byte_int & 0b01111111)
68
+
69
+ encoded_payload = byte_array.shift(length)
70
+
71
+ decoded_payload = encoded_payload.map.with_index { |a, i| a ^ mask_bytes[i % 4] }.pack("C*")
72
+
73
+ {
74
+ info_byte_int:,
75
+ length_byte_int:,
76
+ mask_bytes:,
77
+ length:,
78
+ byte_array:,
79
+ packed_byte_array: byte_array.pack("C*"),
80
+ decoded_payload:
81
+ }
82
+ end
83
+
84
+ def build_response(headers)
85
+ accept_key = Digest::SHA1.base64digest "#{headers["Sec-WebSocket-Key"]}258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
86
+
87
+ <<~RESPONSE
88
+ HTTP/1.1 101 Switching Protocols\r
89
+ Upgrade: websocket\r
90
+ Connection: Upgrade\r
91
+ Sec-WebSocket-Accept: #{accept_key}\r
92
+ \r
93
+ RESPONSE
94
+ end
95
+
96
+ def unbind
97
+ WebSocketServer.connections.delete(self)
98
+ end
99
+ end
100
+
101
+ EventMachine.run do
102
+ EventMachine.start_server "127.0.0.1", 8081, WebSocketServer
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thunderbolt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - albertalef
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: The best ruby web framework
13
+ email:
14
+ - albertalef@protonmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG.md
20
+ - README.md
21
+ - Rakefile
22
+ - lib/thunderbolt.rb
23
+ - lib/thunderbolt/version.rb
24
+ homepage: https://avantsoft.com.br/pt-br/
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ allowed_push_host: https://rubygems.org
29
+ homepage_uri: https://avantsoft.com.br/pt-br/
30
+ source_code_uri: https://avantsoft.com.br/pt-br/
31
+ changelog_uri: https://avantsoft.com.br/pt-br/
32
+ rubygems_mfa_required: 'true'
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.7.2
48
+ specification_version: 4
49
+ summary: The best ruby web framework
50
+ test_files: []