hyraft 0.1.0.alpha1

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.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/CODE_OF_CONDUCT.md +132 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +231 -0
  6. data/exe/hyraft +5 -0
  7. data/lib/hyraft/boot/asset_preloader.rb +185 -0
  8. data/lib/hyraft/boot/preloaded_static.rb +46 -0
  9. data/lib/hyraft/boot/preloader.rb +206 -0
  10. data/lib/hyraft/cli.rb +187 -0
  11. data/lib/hyraft/compiler/compiler.rb +34 -0
  12. data/lib/hyraft/compiler/html_purifier.rb +181 -0
  13. data/lib/hyraft/compiler/javascript_library.rb +281 -0
  14. data/lib/hyraft/compiler/javascript_obfuscator.rb +141 -0
  15. data/lib/hyraft/compiler/parser.rb +27 -0
  16. data/lib/hyraft/compiler/renderer.rb +217 -0
  17. data/lib/hyraft/engine/circuit.rb +35 -0
  18. data/lib/hyraft/engine/port.rb +17 -0
  19. data/lib/hyraft/engine/source.rb +19 -0
  20. data/lib/hyraft/engine.rb +11 -0
  21. data/lib/hyraft/router/api_router.rb +65 -0
  22. data/lib/hyraft/router/web_router.rb +136 -0
  23. data/lib/hyraft/system_info.rb +26 -0
  24. data/lib/hyraft/version.rb +5 -0
  25. data/lib/hyraft.rb +48 -0
  26. data/templates/do_app/Gemfile +50 -0
  27. data/templates/do_app/Rakefile +88 -0
  28. data/templates/do_app/adapter-intake/web-app/display/pages/home/home.hyr +174 -0
  29. data/templates/do_app/adapter-intake/web-app/request/home_web_adapter.rb +19 -0
  30. data/templates/do_app/boot.rb +41 -0
  31. data/templates/do_app/framework/adapters/server/server_api_adapter.rb +51 -0
  32. data/templates/do_app/framework/adapters/server/server_web_adapter.rb +178 -0
  33. data/templates/do_app/framework/compiler/style_resolver.rb +33 -0
  34. data/templates/do_app/framework/errors/error_handler.rb +75 -0
  35. data/templates/do_app/framework/errors/templates/304.html +22 -0
  36. data/templates/do_app/framework/errors/templates/400.html +22 -0
  37. data/templates/do_app/framework/errors/templates/401.html +22 -0
  38. data/templates/do_app/framework/errors/templates/403.html +22 -0
  39. data/templates/do_app/framework/errors/templates/404.html +62 -0
  40. data/templates/do_app/framework/errors/templates/500.html +73 -0
  41. data/templates/do_app/framework/middleware/cors_middleware.rb +37 -0
  42. data/templates/do_app/infra/config/environment.rb +86 -0
  43. data/templates/do_app/infra/config/error_config.rb +80 -0
  44. data/templates/do_app/infra/config/routes/api_routes.rb +2 -0
  45. data/templates/do_app/infra/config/routes/web_routes.rb +10 -0
  46. data/templates/do_app/infra/database/sequel_connection.rb +62 -0
  47. data/templates/do_app/infra/gems/database.rb +7 -0
  48. data/templates/do_app/infra/gems/load_all.rb +4 -0
  49. data/templates/do_app/infra/gems/utilities.rb +1 -0
  50. data/templates/do_app/infra/gems/web.rb +3 -0
  51. data/templates/do_app/infra/server/api-server.ru +13 -0
  52. data/templates/do_app/infra/server/web-server.ru +32 -0
  53. data/templates/do_app/package.json +9 -0
  54. data/templates/do_app/public/favicon.ico +0 -0
  55. data/templates/do_app/public/icons/docs.svg +10 -0
  56. data/templates/do_app/public/icons/expli.svg +13 -0
  57. data/templates/do_app/public/icons/git-repo.svg +13 -0
  58. data/templates/do_app/public/icons/hexagonal-arch.svg +15 -0
  59. data/templates/do_app/public/icons/template-engine.svg +26 -0
  60. data/templates/do_app/public/images/hyr-logo.png +0 -0
  61. data/templates/do_app/public/images/hyr-logo.webp +0 -0
  62. data/templates/do_app/public/index.html +22 -0
  63. data/templates/do_app/public/styles/css/main.css +418 -0
  64. data/templates/do_app/public/styles/css/spa.css +171 -0
  65. data/templates/do_app/shared/helpers/pagination_helper.rb +44 -0
  66. data/templates/do_app/shared/helpers/response_formatter.rb +25 -0
  67. data/templates/do_app/test/acceptance/api/articles_api_acceptance_test.rb +43 -0
  68. data/templates/do_app/test/acceptance/web/articles_acceptance_test.rb +31 -0
  69. data/templates/do_app/test/acceptance/web/home_acceptance_test.rb +17 -0
  70. data/templates/do_app/test/db.rb +106 -0
  71. data/templates/do_app/test/integration/adapter-exhaust/data-gateway/sequel_articles_gateway_test.rb +79 -0
  72. data/templates/do_app/test/integration/adapter-intake/api-app/request/articles_api_adapter_test.rb +61 -0
  73. data/templates/do_app/test/integration/adapter-intake/web-app/request/articles_web_adapter_test.rb +20 -0
  74. data/templates/do_app/test/integration/adapter-intake/web-app/request/home_web_adapter_test.rb +17 -0
  75. data/templates/do_app/test/integration/database/migration_test.rb +35 -0
  76. data/templates/do_app/test/support/mock_api_adapter.rb +82 -0
  77. data/templates/do_app/test/support/mock_articles_gateway.rb +41 -0
  78. data/templates/do_app/test/support/mock_web_adapter.rb +85 -0
  79. data/templates/do_app/test/support/test_patches.rb +33 -0
  80. data/templates/do_app/test/test_helper.rb +526 -0
  81. data/templates/do_app/test/unit/engine/circuit/articles_circuit_test.rb +167 -0
  82. data/templates/do_app/test/unit/engine/port/articles_gateway_port_test.rb +12 -0
  83. data/templates/do_app/test/unit/engine/source/article_test.rb +37 -0
  84. metadata +291 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3f65d6ee969c94c23ae99ca813f71286ecb5264bece1b4f6b87ddbc3fa1a0218
4
+ data.tar.gz: 9996948d832a17aa74f1a33193088ef5356e2b42ca3c2bd334dd761bcacceebf
5
+ SHA512:
6
+ metadata.gz: db8bb8e19e7f11b291a9cd486db101db9883f009e78cb56e58d1177cd7ab226c39402bd01182389a2a9571616592e113a3f47624a1f8f1a754bbbc898de2605c
7
+ data.tar.gz: 175e8fb071450b12431e195cd9f6ccab32f5c7ad21c425baa9fc047964dbee79476f37d3d6037c7e8da02ceb6b4e8c684357ea3cb9066dca08e259bb0cf76eec
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-10-17
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Hyraft: Demjhon Silver
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,231 @@
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/demjhonsilver/hyraft/main/img/logo.png" alt="Logo" width="70" height="70"/>
3
+ </p>
4
+ <div align="center">
5
+
6
+ # Hyraft
7
+
8
+ [![Gem Version](https://badge.fury.io/rb/hyraft.svg?icon=si%3Arubygems&icon_color=%23ffffff)](https://badge.fury.io/rb/hyraft)
9
+ ![Downloads](https://img.shields.io/gem/dt/hyraft)
10
+ ![License](https://img.shields.io/github/license/demjhonsilver/hyraft)
11
+ ![Ruby Version](https://img.shields.io/badge/ruby-%3E%3D%203.4.0-red)
12
+ ![Tests](https://github.com/demjhonsilver/hyraft/actions/workflows/ci.yml/badge.svg)
13
+
14
+
15
+ </div>
16
+
17
+
18
+ **A high-performance Ruby web framework with hexagonal architecture**
19
+
20
+ Hyraft is a modern Ruby web framework that combines excellent performance with clean hexagonal architecture. Built for developers who want both speed and maintainability in their web applications.
21
+
22
+
23
+ ## Features
24
+
25
+ - **High Performance**: 37-55ms page renders, faster than most Ruby frameworks
26
+ - **Hexagonal Architecture**: Clean separation with ports, adapters, and use cases
27
+ - **Custom Template Engine**: `.hyr` files with metadata, displayers, transmuters, and manifestors
28
+ - **Multi-App Support**: Run multiple applications under one framework
29
+ - **Dual Server Mode**: Web server (port 1091) + API server (port 1092) simultaneously
30
+ - **Auto-Discovery**: Automatic template and component discovery across apps
31
+ - **Lightweight**: Minimal dependencies, maximum performance
32
+
33
+
34
+ ## Hyraft Components
35
+
36
+ - **Hyraft** - Framework`(Main)`
37
+ - **Hyraft-server** - Server `(Switch Server)`
38
+ - **Hyraft-rule** - CLI `(Commands)`
39
+
40
+
41
+ ## Installation
42
+
43
+ Install the gem and add to your application's Gemfile:
44
+
45
+ ```bash
46
+ gem install hyraft
47
+ ```
48
+
49
+ ## Create a new Hyraft application:
50
+
51
+ ```bash
52
+ hyraft do myapp
53
+
54
+ cd myapp
55
+
56
+ bundle install && npm install
57
+ ```
58
+ Run server:
59
+
60
+ ```bash
61
+
62
+ hyraft-server thin
63
+
64
+ or
65
+
66
+ hyr s thin
67
+ ```
68
+
69
+ Visit:
70
+
71
+ ```bash
72
+ http://localhost:1091
73
+ ```
74
+
75
+ ## Project Structure
76
+
77
+ ```bash
78
+ myapp/
79
+ ├── adapter-intake/ # Input adapters
80
+ │ ├── multi-app/
81
+ │ ├── admin-app/
82
+ │ ├── api-app/
83
+ │ │ └── request/ # API controllers/adapters
84
+ │ │ └── articles_api_adapter.rb
85
+ │ └── web-app/
86
+ │ ├── request/ # Web controllers/adapters
87
+ │ │ ├── home_web_adapter.rb
88
+ │ │ └── articles_web_adapter.rb
89
+ │ │
90
+ │ └── display/ # Hyraft templates (.hyr files)
91
+ │ └── pages/
92
+ │ ├── home/
93
+ │ │ └── home.hyr
94
+ │ └── articles/
95
+ │ ├── index.hyr
96
+ │ ├── show.hyr
97
+ │ ├── new.hyr
98
+ │ └── edit.hyr
99
+
100
+ ├── adapter-exhaust/ # Output adapters
101
+ │ └── data-gateway/ # Database access implementations
102
+ │ └── sequel_articles_gateway.rb
103
+
104
+ ├── engine/ # Business logic (framework-independent)
105
+ │ ├── source/ # Domain entities
106
+ │ │ └── article.rb
107
+ │ │
108
+ │ ├── circuit/ # Use cases/business processes
109
+ │ │ └── articles_circuit.rb
110
+ │ │
111
+ │ └── port/ # Interfaces (abstract)
112
+ │ └── articles_gateway_port.rb
113
+
114
+ ├── framework/ # Framework tools
115
+ │ ├── adapters/
116
+ │ ├── compiler/
117
+ │ ├── errors/
118
+ │ └── middleware/
119
+
120
+ ├── shared/ # Shared files
121
+ │ └── helpers/
122
+ │ ├── pagination_helper.rb
123
+ │ └── response_formatter.rb
124
+
125
+ ├── infra/ # Infrastructure
126
+ │ ├── config/
127
+ │ │ ├── routes/ # Route definitions
128
+ │ │ │ ├── api_routes.rb # API
129
+ │ │ │ └── web_routes.rb # Web
130
+ │ │ │
131
+ │ │ ├── environment.rb
132
+ │ │ └── error_config.rb
133
+ │ │
134
+ │ ├── database/
135
+ │ │ ├── migrations/ # Database schema
136
+ │ │ │ ├── 001_create_articles.rb
137
+ │ │ │ └── 002_add_image_to_articles.rb
138
+ │ │ │
139
+ │ │ └── sequel_connection.rb # Database configuration
140
+ │ │
141
+ │ ├── gems/ # Third party gems to import
142
+ │ │ ├── database.rb # Database gems
143
+ │ │ ├── load_all.rb # Load all gems
144
+ │ │ ├── utilities.rb # Utilities
145
+ │ │ └── web.rb # Web gems
146
+ │ │
147
+ │ └── server/
148
+ │ ├── api-server.ru # Rack api configuration
149
+ │ └── web-server.ru # Rack web configuration
150
+
151
+ ├── public/ # Static assets
152
+ │ ├── uploads/ # File uploads (images, etc.)
153
+ │ ├── icons/
154
+ │ ├── styles/
155
+ │ │ └── css/
156
+ │ │ ├── main.css
157
+ │ │ └── spa.css
158
+ │ ├── images/
159
+ │ │ ├── hyr-logo.png
160
+ │ │ └── hyr-logo.webp
161
+ │ ├── favicon.ico
162
+ │ │
163
+ │ └── index.html
164
+
165
+ ├── boot.rb
166
+ ├── env.yml
167
+ ├── Gemfile
168
+ └── package.json
169
+
170
+
171
+ ```
172
+
173
+ ## # For template files ( .hyr )
174
+
175
+
176
+ ## VS Code Extension
177
+ Enhanced development experience with Hyraft extension for VS Code.
178
+
179
+ Install from VS Code:
180
+
181
+ ```bash
182
+ # Open VS Code then press (ctrl + shift + x)
183
+ # Search for "Hyraft"
184
+ # Click Install
185
+ ```
186
+
187
+ ## VS Code Extension ( snippets )
188
+
189
+ Prefix:
190
+
191
+ Type 3 letters: hyr
192
+
193
+
194
+ It will display the following snippet options:
195
+
196
+ ----------
197
+
198
+ - hyr-component
199
+ - hyr-embed
200
+ - hyr-html-method
201
+ - hyr-interp
202
+ - hyr-template
203
+
204
+ ----------
205
+
206
+ * In a VS Code extension, snippets are predefined pieces of code or text that can be inserted quickly using a trigger (a prefix) in the editor.
207
+
208
+
209
+ ## Official site
210
+
211
+ Visit: [https://hyraft.com](https://hyraft.com)
212
+
213
+
214
+
215
+ ## Development
216
+
217
+ After checking out the repo, run `bundle install` to install dependencies.
218
+
219
+ Then, run `rake test` to run the tests.
220
+
221
+ ## Contributing
222
+
223
+ Bug reports and pull requests are welcome on GitHub at https://github.com/demjhonsilver/hyraft. 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/demjhonsilver/hyraft/blob/master/CODE_OF_CONDUCT.md).
224
+
225
+ ## License
226
+
227
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
228
+
229
+ ## Code of Conduct
230
+
231
+ Everyone interacting in the Hyraft project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/demjhonsilver/hyraft/blob/master/CODE_OF_CONDUCT.md).
data/exe/hyraft ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "hyraft"
4
+ Hyraft::CLI.start(ARGV)
5
+
@@ -0,0 +1,185 @@
1
+ # lib/hyraft/boot/asset_preloader.rb
2
+ module Hyraft
3
+ module AssetPreloader
4
+ COLORS = {
5
+ green: "\e[32m",
6
+ cyan: "\e[36m",
7
+ yellow: "\e[33m",
8
+ red: "\e[31m",
9
+ orange: "\e[38;5;214m",
10
+ blue: "\e[34m",
11
+ lightblue: "\e[94m",
12
+ reset: "\e[0m"
13
+ }
14
+
15
+ @preloaded_assets = {}
16
+ @stats = {
17
+ total_assets: 0,
18
+ total_bytes: 0,
19
+ compressed_bytes: 0,
20
+ load_time: 0.0
21
+ }
22
+
23
+ def self.preload_assets(public_path)
24
+ puts "#{COLORS[:green]} Hyraft Asset Preloader: Scanning for assets...#{COLORS[:reset]}"
25
+
26
+ start_time = Time.now
27
+ assets_found = discover_assets(public_path)
28
+
29
+ puts "Found #{assets_found.size} asset files"
30
+
31
+ assets_found.each do |asset_path|
32
+ preload_asset(asset_path, public_path)
33
+ end
34
+
35
+ @stats[:load_time] = Time.now - start_time
36
+ print_stats
37
+ end
38
+
39
+ def self.discover_assets(public_path)
40
+ return [] unless File.exist?(public_path)
41
+
42
+ assets = []
43
+
44
+ # Look for assets in common locations
45
+ asset_patterns = [
46
+ "styles/css/**/*.css",
47
+ "css/**/*.css",
48
+ "images/**/*.{jpg,jpeg,png,gif,svg,webp,ico}",
49
+ "js/**/*.js",
50
+ "favicon.ico"
51
+ ]
52
+
53
+ asset_patterns.each do |pattern|
54
+ full_pattern = File.join(public_path, pattern)
55
+ assets += Dir.glob(full_pattern)
56
+ end
57
+
58
+ assets.sort
59
+ end
60
+
61
+ def self.preload_asset(asset_path, public_path)
62
+ relative_path = asset_path.sub(public_path + '/', '')
63
+
64
+ begin
65
+ content = File.read(asset_path, mode: 'rb')
66
+ compressed = compress_asset(content, File.extname(asset_path))
67
+
68
+ @preloaded_assets[relative_path] = {
69
+ content: content,
70
+ compressed: compressed,
71
+ size: content.bytesize,
72
+ compressed_size: compressed.bytesize,
73
+ mtime: File.mtime(asset_path),
74
+ content_type: content_type_for(asset_path)
75
+ }
76
+
77
+ @stats[:total_assets] += 1
78
+ @stats[:total_bytes] += content.bytesize
79
+ @stats[:compressed_bytes] += compressed.bytesize
80
+
81
+ compression_ratio = ((1 - compressed.bytesize.to_f / content.bytesize) * 100).round(2)
82
+
83
+ puts " #{COLORS[:green]}✓#{COLORS[:reset]} #{COLORS[:lightblue]}#{relative_path}#{COLORS[:reset]} " \
84
+ "(#{COLORS[:yellow]}#{(content.bytesize / 1024.0).round(2)} KB#{COLORS[:reset]} → " \
85
+ "#{COLORS[:cyan]}#{(compressed.bytesize / 1024.0).round(2)} KB#{COLORS[:reset]} " \
86
+ "#{COLORS[:green]}#{compression_ratio}%#{COLORS[:reset]})"
87
+
88
+ rescue => e
89
+ puts " #{COLORS[:red]}✗#{COLORS[:reset]} #{COLORS[:cyan]}#{relative_path}#{COLORS[:reset]} " \
90
+ "#{COLORS[:red]}Error: #{e.message}#{COLORS[:reset]}"
91
+ end
92
+ end
93
+
94
+ def self.compress_asset(content, extname)
95
+ case extname.downcase
96
+ when '.css'
97
+ compress_css(content)
98
+ when '.js'
99
+ compress_js(content)
100
+ else
101
+ content
102
+ end
103
+ end
104
+
105
+ def self.compress_css(css)
106
+ css.gsub(/\/\*.*?\*\//m, '')
107
+ .gsub(/\s+/, ' ')
108
+ .gsub(/;\s*}/, '}')
109
+ .gsub(/\s*{\s*/, '{')
110
+ .gsub(/\s*}\s*/, '}')
111
+ .gsub(/:\s+/, ':')
112
+ .gsub(/,\s+/, ',')
113
+ .strip
114
+ end
115
+
116
+ def self.compress_js(js)
117
+ js.gsub(/\/\/.*?$/, '')
118
+ .gsub(/\/\*.*?\*\//m, '')
119
+ .gsub(/\s+/, ' ')
120
+ .gsub(/\s*([=+\-\/*{}()\[\],;:])\s*/, '\1')
121
+ .strip
122
+ end
123
+
124
+ def self.content_type_for(file_path)
125
+ case File.extname(file_path).downcase
126
+ when '.css' then 'text/css'
127
+ when '.js' then 'application/javascript'
128
+ when '.jpg', '.jpeg' then 'image/jpeg'
129
+ when '.png' then 'image/png'
130
+ when '.gif' then 'image/gif'
131
+ when '.svg' then 'image/svg+xml'
132
+ when '.webp' then 'image/webp'
133
+ when '.ico' then 'image/x-icon'
134
+ else 'application/octet-stream'
135
+ end
136
+ end
137
+
138
+ def self.get_asset(asset_path)
139
+ @preloaded_assets[asset_path]
140
+ end
141
+
142
+ def self.asset_preloaded?(asset_path)
143
+ @preloaded_assets.key?(asset_path)
144
+ end
145
+
146
+ def self.serve_asset(asset_path)
147
+ return nil unless asset_preloaded?(asset_path)
148
+ get_asset(asset_path)
149
+ end
150
+
151
+ def self.stats
152
+ @stats.dup
153
+ end
154
+
155
+ private
156
+
157
+ def self.print_stats
158
+ puts "\n#{COLORS[:green]}Asset Preload Statistics:#{COLORS[:reset]}"
159
+ puts " #{COLORS[:cyan]}Assets:#{COLORS[:reset]} #{@stats[:total_assets]}"
160
+ puts " #{COLORS[:cyan]}Original Size:#{COLORS[:reset]} #{(@stats[:total_bytes] / 1024.0).round(2)} KB"
161
+ puts " #{COLORS[:cyan]}Compressed Size:#{COLORS[:reset]} #{(@stats[:compressed_bytes] / 1024.0).round(2)} KB"
162
+ puts " #{COLORS[:cyan]}Total Savings:#{COLORS[:reset]} #{((@stats[:total_bytes] - @stats[:compressed_bytes]) / 1024.0).round(2)} KB"
163
+ puts " #{COLORS[:cyan]}Load Time:#{COLORS[:reset]} #{@stats[:load_time].round(3)}s"
164
+ puts " #{COLORS[:cyan]}Memory:#{COLORS[:reset]} #{memory_usage} MB"
165
+ puts " #{COLORS[:cyan]}Status:#{COLORS[:reset]} #{COLORS[:green]}ASSETS PRELOADED#{COLORS[:reset]}\n\n"
166
+ end
167
+
168
+ def self.memory_usage
169
+ if Gem.win_platform?
170
+ # Windows (no `ps -o rss=`)
171
+ get_windows_memory
172
+ else
173
+ # Linux/Mac
174
+ (`ps -o rss= -p #{Process.pid}`.to_i / 1024.0).round(2)
175
+ end
176
+ end
177
+
178
+ def self.get_windows_memory
179
+ memory_kb = `tasklist /FI "PID eq #{Process.pid}" /FO CSV /NH`
180
+ .split(",")[4].to_s.gsub('"','').gsub(/[^0-9]/, '').to_i
181
+ (memory_kb / 1024.0).round(2)
182
+ end
183
+
184
+ end
185
+ end
@@ -0,0 +1,46 @@
1
+
2
+ require 'digest'
3
+
4
+ module Hyraft
5
+ class PreloadedStatic
6
+ def initialize(app, options = {})
7
+ @app = app
8
+ @urls = options[:urls] || ["/styles", "/css", "/images", "/js", "/favicon.ico"]
9
+ end
10
+
11
+ def call(env)
12
+ path = env['PATH_INFO']
13
+
14
+ # Check if this is a static asset we should handle
15
+ if @urls.any? { |url| path.start_with?(url) }
16
+ serve_preloaded_asset(path)
17
+ else
18
+ @app.call(env)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def serve_preloaded_asset(path)
25
+ # Normalize path (remove leading slash)
26
+ asset_path = path.start_with?('/') ? path[1..-1] : path
27
+
28
+ if Hyraft::AssetPreloader.asset_preloaded?(asset_path)
29
+ # ULTRA FAST: Serve from memory
30
+ asset_data = Hyraft::AssetPreloader.serve_asset(asset_path)
31
+
32
+ headers = {
33
+ 'Content-Type' => asset_data[:content_type],
34
+ 'Content-Length' => asset_data[:compressed_size].to_s,
35
+ 'Cache-Control' => 'public, max-age=86400',
36
+ 'ETag' => Digest::MD5.hexdigest(asset_data[:compressed])
37
+ }
38
+
39
+ [200, headers, [asset_data[:compressed]]]
40
+ else
41
+ # Fallback to original request
42
+ @app.call(env)
43
+ end
44
+ end
45
+ end
46
+ end