datastar 1.0.0 → 1.0.2
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 +4 -4
- data/LICENSE.md +4 -16
- data/README.md +101 -14
- data/benchmarks/compression.rb +251 -0
- data/examples/hello-world/Gemfile +1 -1
- data/examples/hello-world/Gemfile.lock +3 -3
- data/examples/hello-world/hello-world.html +5 -5
- data/examples/progress/progress.ru +307 -0
- data/examples/threads/Gemfile +1 -1
- data/examples/threads/Gemfile.lock +3 -3
- data/examples/threads/threads.ru +3 -3
- data/lib/datastar/async_executor.rb +3 -1
- data/lib/datastar/compression_config.rb +167 -0
- data/lib/datastar/compressor/brotli.rb +56 -0
- data/lib/datastar/compressor/gzip.rb +60 -0
- data/lib/datastar/configuration.rb +6 -0
- data/lib/datastar/dispatcher.rb +54 -11
- data/lib/datastar/server_sent_event_generator.rb +52 -10
- data/lib/datastar/version.rb +1 -1
- data/lib/datastar.rb +1 -2
- metadata +9 -5
- data/lib/datastar/consts.rb +0 -57
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 885e73382637f598ee1feeea492556e30f4dea1dcdd9eae1627649a8dbadc0bc
|
|
4
|
+
data.tar.gz: 2945bedd152787c39ae3843f2cf2c937bdba3d7d8a0ea0f6b9a4d0ac2bf34a18
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bc0bb40586da1e305aa87b6c4b830baeaacd671e259aa7105a043ceaebab8659c23df8b12665d1a1bf1b9ca09aa518e275cdc30b5978627da6d818e818cc14f8
|
|
7
|
+
data.tar.gz: 5ae556f5ce37d9805fe920c68ab5164464ec6fabbd2f4355d9d17139dbf381bd0dde65b5c33bf8f0e4de6eda431ece4cc7b20a41080f3362fea9f7daedad580d
|
data/LICENSE.md
CHANGED
|
@@ -1,19 +1,7 @@
|
|
|
1
|
-
Copyright
|
|
1
|
+
Copyright © Star Federation
|
|
2
2
|
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
in the Software without restriction, including without limitation the rights
|
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
furnished to do so, subject to the following conditions:
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
9
4
|
|
|
10
|
-
The above copyright notice and this permission notice shall be included in all
|
|
11
|
-
copies or substantial portions of the Software.
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
6
|
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
-
SOFTWARE.
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Datastar Ruby SDK
|
|
2
2
|
|
|
3
|
-
Implement the [
|
|
3
|
+
Implement the [Datastar SSE procotocol](https://data-star.dev/reference/sse_events) in Ruby. It can be used in any Rack handler, and Rails controllers.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -13,7 +13,7 @@ gem 'datastar'
|
|
|
13
13
|
Or point your `Gemfile` to the source
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
gem 'datastar',
|
|
16
|
+
gem 'datastar', github: 'starfederation/datastar-ruby'
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
@@ -160,7 +160,7 @@ sse.execute_script(%(alert('Hello World!')), auto_remove: false)
|
|
|
160
160
|
```
|
|
161
161
|
|
|
162
162
|
#### `signals`
|
|
163
|
-
See https://data-star.dev/guide/
|
|
163
|
+
See https://data-star.dev/guide/reactive_signals
|
|
164
164
|
|
|
165
165
|
Returns signals sent by the browser.
|
|
166
166
|
|
|
@@ -270,13 +270,105 @@ Datastar.configure do |config|
|
|
|
270
270
|
config.on_error do |exception|
|
|
271
271
|
Sentry.notify(exception)
|
|
272
272
|
end
|
|
273
|
-
|
|
273
|
+
|
|
274
274
|
# Global heartbeat interval (or false, to disable)
|
|
275
|
-
#
|
|
275
|
+
# Can be overriden on specific instances
|
|
276
276
|
config.heartbeat = 0.3
|
|
277
|
+
|
|
278
|
+
# Enable compression for SSE streams (default: false)
|
|
279
|
+
# See the Compression section below for details
|
|
280
|
+
config.compression = true
|
|
277
281
|
end
|
|
278
282
|
```
|
|
279
283
|
|
|
284
|
+
### Compression
|
|
285
|
+
|
|
286
|
+
SSE data (JSON + HTML) is highly compressible, and long-lived connections benefit significantly from compression. This SDK supports opt-in Brotli and gzip compression for SSE streams.
|
|
287
|
+
|
|
288
|
+
#### Enabling compression
|
|
289
|
+
|
|
290
|
+
Per-instance:
|
|
291
|
+
|
|
292
|
+
```ruby
|
|
293
|
+
datastar = Datastar.new(request:, response:, view_context:, compression: true)
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
Or globally:
|
|
297
|
+
|
|
298
|
+
```ruby
|
|
299
|
+
Datastar.configure do |config|
|
|
300
|
+
config.compression = true
|
|
301
|
+
end
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
When enabled, the SDK negotiates compression with the client via the `Accept-Encoding` header and sets the appropriate `Content-Encoding` response header. If the client does not support compression, responses are sent uncompressed.
|
|
305
|
+
|
|
306
|
+
#### Brotli vs gzip
|
|
307
|
+
|
|
308
|
+
Brotli (`:br`) is preferred by default as it offers better compression ratios. It requires the host app to require the [`brotli`](https://github.com/miyucy/brotli) gem. Gzip uses Ruby built-in `zlib` and requires no extra dependencies.
|
|
309
|
+
|
|
310
|
+
To use Brotli, add the gem to your `Gemfile`:
|
|
311
|
+
|
|
312
|
+
```ruby
|
|
313
|
+
gem 'brotli'
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
#### Configuration options
|
|
317
|
+
|
|
318
|
+
```ruby
|
|
319
|
+
Datastar.configure do |config|
|
|
320
|
+
# Enable compression (default: false)
|
|
321
|
+
# true enables both :br and :gzip (br preferred)
|
|
322
|
+
config.compression = true
|
|
323
|
+
|
|
324
|
+
# Or pass an array of encodings (first = preferred)
|
|
325
|
+
config.compression = [:br, :gzip]
|
|
326
|
+
|
|
327
|
+
# Per-encoder options via [symbol, options] pairs
|
|
328
|
+
config.compression = [[:br, { quality: 5 }], :gzip]
|
|
329
|
+
end
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
You can also set these per-instance:
|
|
333
|
+
|
|
334
|
+
```ruby
|
|
335
|
+
datastar = Datastar.new(
|
|
336
|
+
request:, response:, view_context:,
|
|
337
|
+
compression: [:gzip] # only gzip, no brotli
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
# Or with per-encoder options
|
|
341
|
+
datastar = Datastar.new(
|
|
342
|
+
request:, response:, view_context:,
|
|
343
|
+
compression: [[:gzip, { level: 1 }]]
|
|
344
|
+
)
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
#### Per-encoder options
|
|
348
|
+
|
|
349
|
+
Options are passed directly to the underlying compressor via the array form. Available options depend on the encoder.
|
|
350
|
+
|
|
351
|
+
**Gzip** (via `Zlib::Deflate`):
|
|
352
|
+
|
|
353
|
+
| Option | Default | Description |
|
|
354
|
+
| ------------ | --------------------------- | ------------------------------------------------------------ |
|
|
355
|
+
| `:level` | `Zlib::DEFAULT_COMPRESSION` | Compression level (0-9). 0 = none, 1 = fastest, 9 = smallest. `Zlib::BEST_SPEED` and `Zlib::BEST_COMPRESSION` also work. |
|
|
356
|
+
| `:mem_level` | `8` | Memory usage (1-9). Higher uses more memory for better compression. |
|
|
357
|
+
| `:strategy` | `Zlib::DEFAULT_STRATEGY` | Algorithm strategy. Alternatives: `Zlib::FILTERED`, `Zlib::HUFFMAN_ONLY`, `Zlib::RLE`, `Zlib::FIXED`. |
|
|
358
|
+
|
|
359
|
+
**Brotli** (via `Brotli::Compressor`, requires the `brotli` gem):
|
|
360
|
+
|
|
361
|
+
| Option | Default | Description |
|
|
362
|
+
| ---------- | ---------- | ------------------------------------------------------------ |
|
|
363
|
+
| `:quality` | `11` | Compression quality (0-11). Lower is faster, higher compresses better. |
|
|
364
|
+
| `:lgwin` | `22` | Base-2 log of sliding window size (10-24). |
|
|
365
|
+
| `:lgblock` | `0` (auto) | Base-2 log of max input block size (16-24, or 0 for auto). |
|
|
366
|
+
| `:mode` | `:generic` | Compression mode: `:generic`, `:text`, or `:font`. `:text` is a good choice for SSE (UTF-8 HTML/JSON). |
|
|
367
|
+
|
|
368
|
+
#### Proxy considerations
|
|
369
|
+
|
|
370
|
+
Even with `X-Accel-Buffering: no` (set by default), some proxies like Nginx may buffer compressed responses. You may need to add `proxy_buffering off` to your Nginx configuration when using compression with SSE.
|
|
371
|
+
|
|
280
372
|
### Rendering Rails templates
|
|
281
373
|
|
|
282
374
|
In Rails, make sure to initialize Datastar with the `view_context` in a controller.
|
|
@@ -348,13 +440,14 @@ bundle install
|
|
|
348
440
|
From this library's root, run the bundled-in test Rack app:
|
|
349
441
|
|
|
350
442
|
```bash
|
|
351
|
-
bundle puma examples/test.ru
|
|
443
|
+
bundle puma -p 8000 examples/test.ru
|
|
352
444
|
```
|
|
353
445
|
|
|
354
|
-
|
|
446
|
+
From the main [Datastar](https://github.com/starfederation/datastar) repo (you'll need Go installed)
|
|
355
447
|
|
|
356
448
|
```bash
|
|
357
|
-
|
|
449
|
+
cd sdk/tests
|
|
450
|
+
go run ./cmd/datastar-sdk-tests -server http://localhost:8000
|
|
358
451
|
```
|
|
359
452
|
|
|
360
453
|
## Development
|
|
@@ -363,12 +456,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
|
363
456
|
|
|
364
457
|
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).
|
|
365
458
|
|
|
366
|
-
### Building
|
|
367
|
-
|
|
368
|
-
To build `consts.rb` file from template, run Docker and run `make task build`
|
|
369
|
-
|
|
370
|
-
The template is located at `build/consts_ruby.gtpl`.
|
|
371
|
-
|
|
372
459
|
## Contributing
|
|
373
460
|
|
|
374
461
|
Bug reports and pull requests are welcome on GitHub at https://github.com/starfederation/datastar.
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Benchmark: SSE compression payload sizes
|
|
5
|
+
#
|
|
6
|
+
# Compares bytes-over-the-wire for no compression, gzip, and brotli
|
|
7
|
+
# when streaming large HTML elements via Datastar's SSE protocol.
|
|
8
|
+
#
|
|
9
|
+
# Usage:
|
|
10
|
+
# bundle exec ruby benchmarks/compression.rb
|
|
11
|
+
#
|
|
12
|
+
# The benchmark patches realistic HTML payloads of increasing size
|
|
13
|
+
# through the full Datastar SSE pipeline (ServerSentEventGenerator →
|
|
14
|
+
# CompressedSocket → raw socket) and reports the resulting byte sizes
|
|
15
|
+
# and compression ratios.
|
|
16
|
+
|
|
17
|
+
require 'bundler/setup'
|
|
18
|
+
require 'datastar'
|
|
19
|
+
require 'datastar/compressor/gzip'
|
|
20
|
+
require 'datastar/compressor/brotli'
|
|
21
|
+
|
|
22
|
+
# --- Payload generators ---------------------------------------------------
|
|
23
|
+
|
|
24
|
+
# A user-row partial, repeated N times inside a <tbody>.
|
|
25
|
+
# Realistic: IDs, data attributes, mixed text, Tailwind-style classes.
|
|
26
|
+
def html_table(row_count)
|
|
27
|
+
rows = row_count.times.map do |i|
|
|
28
|
+
<<~HTML
|
|
29
|
+
<tr id="user-row-#{i}" class="border-b border-gray-200 hover:bg-gray-50 transition-colors duration-150" data-user-id="#{i}" data-signal-selected="false">
|
|
30
|
+
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">#{i + 1}</td>
|
|
31
|
+
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">user-#{i}@example.com</td>
|
|
32
|
+
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">#{%w[Admin Editor Viewer].sample}</td>
|
|
33
|
+
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">2025-01-#{(i % 28 + 1).to_s.rjust(2, '0')}</td>
|
|
34
|
+
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
|
|
35
|
+
<button class="text-indigo-600 hover:text-indigo-900 mr-3" data-on-click="$$put('/users/#{i}/edit')">Edit</button>
|
|
36
|
+
<button class="text-red-600 hover:text-red-900" data-on-click="$$delete('/users/#{i}')">Delete</button>
|
|
37
|
+
</td>
|
|
38
|
+
</tr>
|
|
39
|
+
HTML
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
<<~HTML
|
|
43
|
+
<tbody id="users-table-body">
|
|
44
|
+
#{rows.join}
|
|
45
|
+
</tbody>
|
|
46
|
+
HTML
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# A dashboard card with nested elements — charts placeholder, stats, lists.
|
|
50
|
+
def html_dashboard(card_count)
|
|
51
|
+
cards = card_count.times.map do |i|
|
|
52
|
+
<<~HTML
|
|
53
|
+
<div id="card-#{i}" class="bg-white overflow-hidden shadow-lg rounded-2xl border border-gray-100 p-6 flex flex-col gap-4">
|
|
54
|
+
<div class="flex items-center justify-between">
|
|
55
|
+
<h3 class="text-lg font-semibold text-gray-900">Metric #{i + 1}</h3>
|
|
56
|
+
<span class="inline-flex items-center rounded-full bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-inset ring-green-600/20">+#{rand(1..99)}%</span>
|
|
57
|
+
</div>
|
|
58
|
+
<div class="text-3xl font-bold text-gray-900">#{rand(1_000..99_999).to_s.chars.each_slice(3).map(&:join).join(',')}</div>
|
|
59
|
+
<div class="h-32 bg-gradient-to-r from-indigo-50 to-indigo-100 rounded-lg flex items-end gap-1 p-2">
|
|
60
|
+
#{8.times.map { |j| "<div class=\"bg-indigo-#{[400, 500, 600].sample} rounded-t w-full\" style=\"height: #{rand(20..100)}%\"></div>" }.join("\n ")}
|
|
61
|
+
</div>
|
|
62
|
+
<ul class="divide-y divide-gray-100">
|
|
63
|
+
#{5.times.map { |j| "<li class=\"flex justify-between py-2 text-sm\"><span class=\"text-gray-500\">Region #{j + 1}</span><span class=\"font-medium text-gray-900\">#{rand(100..9_999)}</span></li>" }.join("\n ")}
|
|
64
|
+
</ul>
|
|
65
|
+
</div>
|
|
66
|
+
HTML
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
<<~HTML
|
|
70
|
+
<div id="dashboard-grid" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 p-6">
|
|
71
|
+
#{cards.join}
|
|
72
|
+
</div>
|
|
73
|
+
HTML
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# --- Socket that counts bytes --------------------------------------------
|
|
77
|
+
|
|
78
|
+
class ByteCountingSocket
|
|
79
|
+
attr_reader :total_bytes
|
|
80
|
+
|
|
81
|
+
def initialize
|
|
82
|
+
@total_bytes = 0
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def <<(data)
|
|
86
|
+
@total_bytes += data.bytesize
|
|
87
|
+
self
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def close; end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# --- Helpers --------------------------------------------------------------
|
|
94
|
+
|
|
95
|
+
# Pipe an HTML payload through the full SSE + compression stack
|
|
96
|
+
# and return the byte count that would go over the wire.
|
|
97
|
+
def measure_bytes(html, encoding)
|
|
98
|
+
socket = ByteCountingSocket.new
|
|
99
|
+
|
|
100
|
+
wrapped = case encoding
|
|
101
|
+
when :none then socket
|
|
102
|
+
when :gzip then Datastar::Compressor::Gzip::CompressedSocket.new(socket)
|
|
103
|
+
when :br then Datastar::Compressor::Brotli::CompressedSocket.new(socket, mode: :text)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
generator = Datastar::ServerSentEventGenerator.new(
|
|
107
|
+
wrapped,
|
|
108
|
+
signals: {},
|
|
109
|
+
view_context: nil
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
generator.patch_elements(html)
|
|
113
|
+
wrapped.close unless encoding == :none
|
|
114
|
+
|
|
115
|
+
socket.total_bytes
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def format_bytes(bytes)
|
|
119
|
+
if bytes >= 1024 * 1024
|
|
120
|
+
"%.1f MB" % (bytes / (1024.0 * 1024))
|
|
121
|
+
elsif bytes >= 1024
|
|
122
|
+
"%.1f KB" % (bytes / 1024.0)
|
|
123
|
+
else
|
|
124
|
+
"#{bytes} B"
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def ratio(original, compressed)
|
|
129
|
+
"%.1f%%" % ((1.0 - compressed.to_f / original) * 100)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# --- Run benchmarks -------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
SCENARIOS = [
|
|
135
|
+
["Table 10 rows", -> { html_table(10) }],
|
|
136
|
+
["Table 50 rows", -> { html_table(50) }],
|
|
137
|
+
["Table 200 rows", -> { html_table(200) }],
|
|
138
|
+
["Table 1000 rows", -> { html_table(1000) }],
|
|
139
|
+
["Dashboard 5 cards", -> { html_dashboard(5) }],
|
|
140
|
+
["Dashboard 20 cards",-> { html_dashboard(20) }],
|
|
141
|
+
["Dashboard 50 cards",-> { html_dashboard(50) }],
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
ENCODINGS = %i[none gzip br]
|
|
145
|
+
|
|
146
|
+
# Header
|
|
147
|
+
puts "Datastar SSE Compression Benchmark"
|
|
148
|
+
puts "=" * 90
|
|
149
|
+
puts
|
|
150
|
+
puts format(
|
|
151
|
+
"%-22s %12s %12s %8s %12s %8s",
|
|
152
|
+
"Scenario", "No Compress", "Gzip", "Saved", "Brotli", "Saved"
|
|
153
|
+
)
|
|
154
|
+
puts "-" * 90
|
|
155
|
+
|
|
156
|
+
SCENARIOS.each do |name, generator|
|
|
157
|
+
html = generator.call
|
|
158
|
+
results = ENCODINGS.map { |enc| [enc, measure_bytes(html, enc)] }.to_h
|
|
159
|
+
none = results[:none]
|
|
160
|
+
|
|
161
|
+
puts format(
|
|
162
|
+
"%-22s %12s %12s %8s %12s %8s",
|
|
163
|
+
name,
|
|
164
|
+
format_bytes(none),
|
|
165
|
+
format_bytes(results[:gzip]),
|
|
166
|
+
ratio(none, results[:gzip]),
|
|
167
|
+
format_bytes(results[:br]),
|
|
168
|
+
ratio(none, results[:br])
|
|
169
|
+
)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# --- Streaming: multiple SSE events over one connection -------------------
|
|
173
|
+
|
|
174
|
+
# Simulates a long-lived SSE connection where rows are patched individually
|
|
175
|
+
# (e.g. a live-updating table). The compressor stays open across events,
|
|
176
|
+
# so repeated structure (CSS classes, attribute patterns) compresses
|
|
177
|
+
# increasingly well as the dictionary builds up.
|
|
178
|
+
|
|
179
|
+
def measure_streaming_bytes(payloads, encoding)
|
|
180
|
+
socket = ByteCountingSocket.new
|
|
181
|
+
|
|
182
|
+
wrapped = case encoding
|
|
183
|
+
when :none then socket
|
|
184
|
+
when :gzip then Datastar::Compressor::Gzip::CompressedSocket.new(socket)
|
|
185
|
+
when :br then Datastar::Compressor::Brotli::CompressedSocket.new(socket, mode: :text)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
generator = Datastar::ServerSentEventGenerator.new(
|
|
189
|
+
wrapped,
|
|
190
|
+
signals: {},
|
|
191
|
+
view_context: nil
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
payloads.each { |html| generator.patch_elements(html) }
|
|
195
|
+
wrapped.close unless encoding == :none
|
|
196
|
+
|
|
197
|
+
socket.total_bytes
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def table_rows(count)
|
|
201
|
+
count.times.map do |i|
|
|
202
|
+
<<~HTML
|
|
203
|
+
<tr id="user-row-#{i}" class="border-b border-gray-200 hover:bg-gray-50 transition-colors duration-150" data-user-id="#{i}" data-signal-selected="false">
|
|
204
|
+
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">#{i + 1}</td>
|
|
205
|
+
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">user-#{i}@example.com</td>
|
|
206
|
+
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">#{%w[Admin Editor Viewer].sample}</td>
|
|
207
|
+
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">2025-01-#{(i % 28 + 1).to_s.rjust(2, '0')}</td>
|
|
208
|
+
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
|
|
209
|
+
<button class="text-indigo-600 hover:text-indigo-900 mr-3" data-on-click="$$put('/users/#{i}/edit')">Edit</button>
|
|
210
|
+
<button class="text-red-600 hover:text-red-900" data-on-click="$$delete('/users/#{i}')">Delete</button>
|
|
211
|
+
</td>
|
|
212
|
+
</tr>
|
|
213
|
+
HTML
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
puts
|
|
218
|
+
puts
|
|
219
|
+
puts "Streaming: individual row patches over one SSE connection"
|
|
220
|
+
puts "=" * 90
|
|
221
|
+
puts
|
|
222
|
+
puts format(
|
|
223
|
+
"%-22s %12s %12s %8s %12s %8s",
|
|
224
|
+
"Scenario", "No Compress", "Gzip", "Saved", "Brotli", "Saved"
|
|
225
|
+
)
|
|
226
|
+
puts "-" * 90
|
|
227
|
+
|
|
228
|
+
[10, 50, 200, 1000].each do |count|
|
|
229
|
+
payloads = table_rows(count)
|
|
230
|
+
results = ENCODINGS.map { |enc| [enc, measure_streaming_bytes(payloads, enc)] }.to_h
|
|
231
|
+
none = results[:none]
|
|
232
|
+
|
|
233
|
+
puts format(
|
|
234
|
+
"%-22s %12s %12s %8s %12s %8s",
|
|
235
|
+
"#{count} row patches",
|
|
236
|
+
format_bytes(none),
|
|
237
|
+
format_bytes(results[:gzip]),
|
|
238
|
+
ratio(none, results[:gzip]),
|
|
239
|
+
format_bytes(results[:br]),
|
|
240
|
+
ratio(none, results[:br])
|
|
241
|
+
)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
puts
|
|
245
|
+
puts "Notes:"
|
|
246
|
+
puts " - Single-event sizes include full SSE framing (event: / data: prefixes)"
|
|
247
|
+
puts " - Gzip: default compression level, gzip framing (window_bits=31)"
|
|
248
|
+
puts " - Brotli: default quality (11) with mode: :text"
|
|
249
|
+
puts " - Streaming rows: each row is a separate patch_elements SSE event"
|
|
250
|
+
puts " over one persistent compressed connection. The compressor dictionary"
|
|
251
|
+
puts " builds up across events, improving ratios for repetitive markup."
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
|
-
remote:
|
|
2
|
+
remote: ../../..
|
|
3
3
|
specs:
|
|
4
|
-
datastar (1.0.0
|
|
4
|
+
datastar (1.0.0)
|
|
5
5
|
json
|
|
6
6
|
logger
|
|
7
7
|
rack (>= 3.1.14)
|
|
@@ -9,7 +9,7 @@ PATH
|
|
|
9
9
|
GEM
|
|
10
10
|
remote: https://rubygems.org/
|
|
11
11
|
specs:
|
|
12
|
-
json (2.
|
|
12
|
+
json (2.16.0)
|
|
13
13
|
logger (1.7.0)
|
|
14
14
|
nio4r (2.7.4)
|
|
15
15
|
puma (6.6.0)
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
<head>
|
|
6
6
|
<title>Datastar SDK Demo</title>
|
|
7
7
|
<script src="https://unpkg.com/@tailwindcss/browser@4"></script>
|
|
8
|
-
<script type="module" src="https://cdn.jsdelivr.net/gh/starfederation/datastar@
|
|
8
|
+
<script type="module" src="https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.0-RC.6/bundles/datastar.js"></script>
|
|
9
9
|
</head>
|
|
10
10
|
<body class="bg-white dark:bg-gray-900 text-lg max-w-xl mx-auto my-16">
|
|
11
|
-
<div data-signals
|
|
11
|
+
<div data-signals:delay="400" class="bg-white dark:bg-gray-800 text-gray-500 dark:text-gray-400 rounded-lg px-6 py-8 ring shadow-xl ring-gray-900/5 space-y-2">
|
|
12
12
|
<div class="flex justify-between items-center">
|
|
13
13
|
<h1 class="text-gray-900 dark:text-white text-3xl font-semibold">
|
|
14
14
|
Datastar SDK Demo
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
<label for="delay">
|
|
23
23
|
Delay in milliseconds
|
|
24
24
|
</label>
|
|
25
|
-
<input data-bind
|
|
25
|
+
<input data-bind:delay id="delay" type="number" step="100" min="0" class="w-36 rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:border-sky-500 focus:outline focus:outline-sky-500 dark:disabled:border-gray-700 dark:disabled:bg-gray-800/20" />
|
|
26
26
|
</div>
|
|
27
|
-
<button data-on
|
|
27
|
+
<button data-on:click="@get('/hello-world')" class="rounded-md bg-sky-500 px-5 py-2.5 leading-5 font-semibold text-white hover:bg-sky-700 hover:text-gray-100 cursor-pointer">
|
|
28
28
|
Start
|
|
29
29
|
</button>
|
|
30
30
|
</div>
|
|
@@ -32,4 +32,4 @@
|
|
|
32
32
|
<div id="message">Hello, world!</div>
|
|
33
33
|
</div>
|
|
34
34
|
</body>
|
|
35
|
-
</html>
|
|
35
|
+
</html>
|