sinatra 3.0.6 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/Gemfile +7 -0
- data/README.md +113 -2
- data/VERSION +1 -1
- data/examples/lifecycle_events.rb +20 -0
- data/examples/stream.ru +0 -1
- data/lib/sinatra/base.rb +66 -10
- data/lib/sinatra/version.rb +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 315dee3d85d7778ffa48e041bd60370e9e89ecd3c01dbf0cf9450e41c62dc0d8
|
4
|
+
data.tar.gz: '083a914c0b520684471e2715ec091c67ca8121bfb8b5d4cf410307d6e14e4ac5'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2790a4d58595f0f89667c5fd4530dde2de532ddaf02628f373162cc47d393732b1c9475ab59775c756750dfb52e60e7091118936ac8e70739fb266b46f82d3bd
|
7
|
+
data.tar.gz: eb74f34bf0b9a681557809c656754312eb266e66c3b68fcabbf95699c3273cf4d998e3969224b09df9127ff90f5d34fba5bc93b1ff0c321b4a7646ddfcc0b6b7
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,26 @@
|
|
2
2
|
|
3
3
|
* _Your new feature here._
|
4
4
|
|
5
|
+
## 3.1.0 / 2023-08-07
|
6
|
+
|
7
|
+
* New: Add sass support via sass-embedded [#1911] by なつき
|
8
|
+
|
9
|
+
* New: Add start and stop callbacks [#1913] by Jevin Sew
|
10
|
+
|
11
|
+
* New: Warn on dropping sessions [#1900] by Jonathan del Strother
|
12
|
+
|
13
|
+
* New: Make Puma the default server [#1924] by Patrik Ragnarsson
|
14
|
+
|
15
|
+
* Fix: Remove use of Tilt::Cache [#1922] by Jeremy Evans (allows use of Tilt 2.2.0 without deprecation warning)
|
16
|
+
|
17
|
+
* Fix: rack-protection: specify rack version requirement [#1932] by Patrik Ragnarsson
|
18
|
+
|
19
|
+
[#1913]: https://github.com/sinatra/sinatra/pull/1913
|
20
|
+
[#1900]: https://github.com/sinatra/sinatra/pull/1900
|
21
|
+
[#1924]: https://github.com/sinatra/sinatra/pull/1924
|
22
|
+
[#1922]: https://github.com/sinatra/sinatra/pull/1922
|
23
|
+
[#1932]: https://github.com/sinatra/sinatra/pull/1932
|
24
|
+
|
5
25
|
## 3.0.6 / 2023-04-11
|
6
26
|
|
7
27
|
* Fix: Add support to keep open streaming connections with Puma [#1858](https://github.com/sinatra/sinatra/pull/1858) by Jordan Owens
|
data/Gemfile
CHANGED
@@ -31,6 +31,12 @@ gem 'yard'
|
|
31
31
|
gem 'rack-protection', path: 'rack-protection'
|
32
32
|
gem 'sinatra-contrib', path: 'sinatra-contrib'
|
33
33
|
|
34
|
+
# traces 0.10.0 started to use Ruby 2.7 syntax without specifying required Ruby version
|
35
|
+
# https://github.com/socketry/traces/pull/8#discussion_r1237988182
|
36
|
+
# async-http 0.60.2 added traces 0.10.0 as dependency
|
37
|
+
# https://github.com/socketry/async-http/pull/124/files#r1237988899
|
38
|
+
gem 'traces', '< 0.10.0' if RUBY_VERSION >= '2.6.0' && RUBY_VERSION < '2.7.0'
|
39
|
+
|
34
40
|
gem 'activesupport', '~> 6.1'
|
35
41
|
|
36
42
|
gem 'asciidoctor'
|
@@ -50,6 +56,7 @@ gem 'rainbows', platforms: [:mri] # uses #fork
|
|
50
56
|
gem 'rdiscount', platforms: [:ruby]
|
51
57
|
gem 'rdoc'
|
52
58
|
gem 'redcarpet', platforms: [:ruby]
|
59
|
+
gem 'sass-embedded', '~> 1.54'
|
53
60
|
gem 'simplecov', require: false
|
54
61
|
gem 'slim', '~> 4'
|
55
62
|
gem 'yajl-ruby', platforms: [:ruby]
|
data/README.md
CHANGED
@@ -54,6 +54,8 @@ pick up if available.
|
|
54
54
|
- [Erb Templates](#erb-templates)
|
55
55
|
- [Builder Templates](#builder-templates)
|
56
56
|
- [Nokogiri Templates](#nokogiri-templates)
|
57
|
+
- [Sass Templates](#sass-templates)
|
58
|
+
- [Scss Templates](#scss-templates)
|
57
59
|
- [Liquid Templates](#liquid-templates)
|
58
60
|
- [Markdown Templates](#markdown-templates)
|
59
61
|
- [RDoc Templates](#rdoc-templates)
|
@@ -93,6 +95,7 @@ pick up if available.
|
|
93
95
|
- [Configuration](#configuration)
|
94
96
|
- [Configuring attack protection](#configuring-attack-protection)
|
95
97
|
- [Available Settings](#available-settings)
|
98
|
+
- [Lifecycle Events](#lifecycle-events)
|
96
99
|
- [Environments](#environments)
|
97
100
|
- [Error Handling](#error-handling)
|
98
101
|
- [Not Found](#not-found)
|
@@ -649,6 +652,39 @@ It also takes a block for inline templates (see [example](#inline-templates)).
|
|
649
652
|
|
650
653
|
It also takes a block for inline templates (see [example](#inline-templates)).
|
651
654
|
|
655
|
+
#### Sass Templates
|
656
|
+
|
657
|
+
<table>
|
658
|
+
<tr>
|
659
|
+
<td>Dependency</td>
|
660
|
+
<td><a href="https://github.com/ntkme/sass-embedded-host-ruby" title="sass-embedded">sass-embedded</a></td>
|
661
|
+
</tr>
|
662
|
+
<tr>
|
663
|
+
<td>File Extension</td>
|
664
|
+
<td><tt>.sass</tt></td>
|
665
|
+
</tr>
|
666
|
+
<tr>
|
667
|
+
<td>Example</td>
|
668
|
+
<td><tt>sass :stylesheet, :style => :expanded</tt></td>
|
669
|
+
</tr>
|
670
|
+
</table>
|
671
|
+
|
672
|
+
#### Scss Templates
|
673
|
+
|
674
|
+
<table>
|
675
|
+
<tr>
|
676
|
+
<td>Dependency</td>
|
677
|
+
<td><a href="https://github.com/ntkme/sass-embedded-host-ruby" title="sass-embedded">sass-embedded</a></td>
|
678
|
+
</tr>
|
679
|
+
<tr>
|
680
|
+
<td>File Extension</td>
|
681
|
+
<td><tt>.scss</tt></td>
|
682
|
+
</tr>
|
683
|
+
<tr>
|
684
|
+
<td>Example</td>
|
685
|
+
<td><tt>scss :stylesheet, :style => :expanded</tt></td>
|
686
|
+
</tr>
|
687
|
+
</table>
|
652
688
|
|
653
689
|
#### Liquid Templates
|
654
690
|
|
@@ -2089,9 +2125,13 @@ set :protection, :session => true
|
|
2089
2125
|
|
2090
2126
|
<dt>raise_errors</dt>
|
2091
2127
|
<dd>
|
2092
|
-
Raise
|
2128
|
+
Raise unhandled errors (will stop application). Enabled by default when
|
2093
2129
|
<tt>environment</tt> is set to <tt>"test"</tt>, disabled otherwise.
|
2094
2130
|
</dd>
|
2131
|
+
<dd>
|
2132
|
+
Any explicitly defined error handlers always override this setting. See
|
2133
|
+
the "Error" section below.
|
2134
|
+
</dd>
|
2095
2135
|
|
2096
2136
|
<dt>run</dt>
|
2097
2137
|
<dd>
|
@@ -2184,6 +2224,24 @@ set :protection, :session => true
|
|
2184
2224
|
</dd>
|
2185
2225
|
</dl>
|
2186
2226
|
|
2227
|
+
## Lifecycle Events
|
2228
|
+
|
2229
|
+
There are 2 lifecycle events currently exposed by Sinatra. One when the server starts and one when it stops.
|
2230
|
+
|
2231
|
+
They can be used like this:
|
2232
|
+
|
2233
|
+
```ruby
|
2234
|
+
on_start do
|
2235
|
+
puts "===== Booting up ====="
|
2236
|
+
end
|
2237
|
+
|
2238
|
+
on_stop do
|
2239
|
+
puts "===== Shutting down ====="
|
2240
|
+
end
|
2241
|
+
```
|
2242
|
+
|
2243
|
+
Note that these callbacks only work when using Sinatra to start the web server.
|
2244
|
+
|
2187
2245
|
## Environments
|
2188
2246
|
|
2189
2247
|
There are three predefined `environments`: `"development"`,
|
@@ -2240,6 +2298,14 @@ show exceptions option to `:after_handler`:
|
|
2240
2298
|
set :show_exceptions, :after_handler
|
2241
2299
|
```
|
2242
2300
|
|
2301
|
+
A catch-all error handler can be defined with `error` and a block:
|
2302
|
+
|
2303
|
+
```ruby
|
2304
|
+
error do
|
2305
|
+
'Sorry there was a nasty error'
|
2306
|
+
end
|
2307
|
+
```
|
2308
|
+
|
2243
2309
|
The exception object can be obtained from the `sinatra.error` Rack variable:
|
2244
2310
|
|
2245
2311
|
```ruby
|
@@ -2248,7 +2314,7 @@ error do
|
|
2248
2314
|
end
|
2249
2315
|
```
|
2250
2316
|
|
2251
|
-
|
2317
|
+
Pass an error class as an argument to create handlers for custom errors:
|
2252
2318
|
|
2253
2319
|
```ruby
|
2254
2320
|
error MyCustomError do
|
@@ -2294,6 +2360,51 @@ Sinatra installs special `not_found` and `error` handlers when
|
|
2294
2360
|
running under the development environment to display nice stack traces
|
2295
2361
|
and additional debugging information in your browser.
|
2296
2362
|
|
2363
|
+
### Behavior with `raise_errors` option
|
2364
|
+
|
2365
|
+
When `raise_errors` option is `true`, errors that are unhandled are raised
|
2366
|
+
outside of the application. Additionally, any errors that would have been
|
2367
|
+
caught by the catch-all error handler are raised.
|
2368
|
+
|
2369
|
+
For example, consider the following configuration:
|
2370
|
+
|
2371
|
+
```ruby
|
2372
|
+
# First handler
|
2373
|
+
error MyCustomError do
|
2374
|
+
'A custom message'
|
2375
|
+
end
|
2376
|
+
|
2377
|
+
# Second handler
|
2378
|
+
error do
|
2379
|
+
'A catch-all message'
|
2380
|
+
end
|
2381
|
+
```
|
2382
|
+
|
2383
|
+
If `raise_errors` is `false`:
|
2384
|
+
|
2385
|
+
* When `MyCustomError` or descendant is raised, the first handler is invoked.
|
2386
|
+
The HTTP response body will contain `"A custom message"`.
|
2387
|
+
* When any other error is raised, the second handler is invoked. The HTTP
|
2388
|
+
response body will contain `"A catch-all message"`.
|
2389
|
+
|
2390
|
+
If `raise_errors` is `true`:
|
2391
|
+
|
2392
|
+
* When `MyCustomError` or descendant is raised, the behavior is identical to
|
2393
|
+
when `raise_errors` is `false`, described above.
|
2394
|
+
* When any other error is raised, the second handler is *not* invoked, and
|
2395
|
+
the error is raised outside of the application.
|
2396
|
+
* If the environment is `production`, the HTTP response body will contain
|
2397
|
+
a generic error message, e.g. `"An unhandled lowlevel error occurred. The
|
2398
|
+
application logs may have details."`
|
2399
|
+
* If the environment is not `production`, the HTTP response body will contain
|
2400
|
+
the verbose error backtrace.
|
2401
|
+
* Regardless of environment, if `show_exceptions` is set to `:after_handler`,
|
2402
|
+
the HTTP response body will contain the verbose error backtrace.
|
2403
|
+
|
2404
|
+
In the `test` environment, `raise_errors` is set to `true` by default. This
|
2405
|
+
means that in order to write a test for a catch-all error handler,
|
2406
|
+
`raise_errors` must temporarily be set to `false` for that particular test.
|
2407
|
+
|
2297
2408
|
## Rack Middleware
|
2298
2409
|
|
2299
2410
|
Sinatra rides on [Rack](https://rack.github.io/), a minimal standard
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0
|
1
|
+
3.1.0
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby -I ../lib -I lib
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'sinatra'
|
5
|
+
|
6
|
+
get('/') do
|
7
|
+
'This shows how lifecycle events work'
|
8
|
+
end
|
9
|
+
|
10
|
+
on_start do
|
11
|
+
puts "=============="
|
12
|
+
puts " Booting up"
|
13
|
+
puts "=============="
|
14
|
+
end
|
15
|
+
|
16
|
+
on_stop do
|
17
|
+
puts "================="
|
18
|
+
puts " Shutting down"
|
19
|
+
puts "================="
|
20
|
+
end
|
data/examples/stream.ru
CHANGED
data/lib/sinatra/base.rb
CHANGED
@@ -726,7 +726,7 @@ module Sinatra
|
|
726
726
|
# Possible options are:
|
727
727
|
# :content_type The content type to use, same arguments as content_type.
|
728
728
|
# :layout If set to something falsy, no layout is rendered, otherwise
|
729
|
-
# the specified layout is used
|
729
|
+
# the specified layout is used (Ignored for `sass`)
|
730
730
|
# :layout_engine Engine to use for rendering the layout.
|
731
731
|
# :locals A hash with local variables that should be available
|
732
732
|
# in the template
|
@@ -752,6 +752,20 @@ module Sinatra
|
|
752
752
|
render(:haml, template, options, locals, &block)
|
753
753
|
end
|
754
754
|
|
755
|
+
def sass(template, options = {}, locals = {})
|
756
|
+
options[:default_content_type] = :css
|
757
|
+
options[:exclude_outvar] = true
|
758
|
+
options[:layout] = nil
|
759
|
+
render :sass, template, options, locals
|
760
|
+
end
|
761
|
+
|
762
|
+
def scss(template, options = {}, locals = {})
|
763
|
+
options[:default_content_type] = :css
|
764
|
+
options[:exclude_outvar] = true
|
765
|
+
options[:layout] = nil
|
766
|
+
render :scss, template, options, locals
|
767
|
+
end
|
768
|
+
|
755
769
|
def builder(template = nil, options = {}, locals = {}, &block)
|
756
770
|
options[:default_content_type] = :xml
|
757
771
|
render_ruby(:builder, template, options, locals, &block)
|
@@ -862,7 +876,11 @@ module Sinatra
|
|
862
876
|
catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
|
863
877
|
end
|
864
878
|
|
865
|
-
|
879
|
+
if content_type
|
880
|
+
# sass-embedded returns a frozen string
|
881
|
+
output = +output
|
882
|
+
output.extend(ContentTyped).content_type = content_type
|
883
|
+
end
|
866
884
|
output
|
867
885
|
end
|
868
886
|
|
@@ -914,6 +932,35 @@ module Sinatra
|
|
914
932
|
end
|
915
933
|
end
|
916
934
|
|
935
|
+
# Extremely simple template cache implementation.
|
936
|
+
# * Not thread-safe.
|
937
|
+
# * Size is unbounded.
|
938
|
+
# * Keys are not copied defensively, and should not be modified after
|
939
|
+
# being passed to #fetch. More specifically, the values returned by
|
940
|
+
# key#hash and key#eql? should not change.
|
941
|
+
#
|
942
|
+
# Implementation copied from Tilt::Cache.
|
943
|
+
class TemplateCache
|
944
|
+
def initialize
|
945
|
+
@cache = {}
|
946
|
+
end
|
947
|
+
|
948
|
+
# Caches a value for key, or returns the previously cached value.
|
949
|
+
# If a value has been previously cached for key then it is
|
950
|
+
# returned. Otherwise, block is yielded to and its return value
|
951
|
+
# which may be nil, is cached under key and returned.
|
952
|
+
def fetch(*key)
|
953
|
+
@cache.fetch(key) do
|
954
|
+
@cache[key] = yield
|
955
|
+
end
|
956
|
+
end
|
957
|
+
|
958
|
+
# Clears the cache.
|
959
|
+
def clear
|
960
|
+
@cache = {}
|
961
|
+
end
|
962
|
+
end
|
963
|
+
|
917
964
|
# Base class for all Sinatra applications and middleware.
|
918
965
|
class Base
|
919
966
|
include Rack::Utils
|
@@ -928,7 +975,7 @@ module Sinatra
|
|
928
975
|
def initialize(app = nil, **_kwargs)
|
929
976
|
super()
|
930
977
|
@app = app
|
931
|
-
@template_cache =
|
978
|
+
@template_cache = TemplateCache.new
|
932
979
|
@pinned_response = nil # whether a before! filter pinned the content-type
|
933
980
|
yield self if block_given?
|
934
981
|
end
|
@@ -1230,7 +1277,7 @@ module Sinatra
|
|
1230
1277
|
%r{zeitwerk/kernel\.rb} # Zeitwerk kernel#require decorator
|
1231
1278
|
].freeze
|
1232
1279
|
|
1233
|
-
attr_reader :routes, :filters, :templates, :errors
|
1280
|
+
attr_reader :routes, :filters, :templates, :errors, :on_start_callback, :on_stop_callback
|
1234
1281
|
|
1235
1282
|
def callers_to_ignore
|
1236
1283
|
CALLERS_TO_IGNORE
|
@@ -1423,6 +1470,14 @@ module Sinatra
|
|
1423
1470
|
filters[type] << compile!(type, path, block, **options)
|
1424
1471
|
end
|
1425
1472
|
|
1473
|
+
def on_start(&on_start_callback)
|
1474
|
+
@on_start_callback = on_start_callback
|
1475
|
+
end
|
1476
|
+
|
1477
|
+
def on_stop(&on_stop_callback)
|
1478
|
+
@on_stop_callback = on_stop_callback
|
1479
|
+
end
|
1480
|
+
|
1426
1481
|
# Add a route condition. The route is considered non-matching when the
|
1427
1482
|
# block returns false.
|
1428
1483
|
def condition(name = "#{caller.first[/`.*'/]} condition", &block)
|
@@ -1512,12 +1567,14 @@ module Sinatra
|
|
1512
1567
|
warn '== Sinatra has ended his set (crowd applauds)' unless suppress_messages?
|
1513
1568
|
set :running_server, nil
|
1514
1569
|
set :handler_name, nil
|
1570
|
+
|
1571
|
+
on_stop_callback.call unless on_stop_callback.nil?
|
1515
1572
|
end
|
1516
1573
|
|
1517
1574
|
alias stop! quit!
|
1518
1575
|
|
1519
1576
|
# Run the Sinatra app as a self-hosted server using
|
1520
|
-
# Puma, Falcon,
|
1577
|
+
# Puma, Falcon, or WEBrick (in that order). If given a block, will call
|
1521
1578
|
# with the constructed handler once we have taken the stage.
|
1522
1579
|
def run!(options = {}, &block)
|
1523
1580
|
return if running?
|
@@ -1599,7 +1656,7 @@ module Sinatra
|
|
1599
1656
|
set :running_server, server
|
1600
1657
|
set :handler_name, handler_name
|
1601
1658
|
server.threaded = settings.threaded if server.respond_to? :threaded=
|
1602
|
-
|
1659
|
+
on_start_callback.call unless on_start_callback.nil?
|
1603
1660
|
yield server if block_given?
|
1604
1661
|
end
|
1605
1662
|
end
|
@@ -1870,11 +1927,10 @@ module Sinatra
|
|
1870
1927
|
|
1871
1928
|
ruby_engine = defined?(RUBY_ENGINE) && RUBY_ENGINE
|
1872
1929
|
|
1873
|
-
server.unshift 'puma'
|
1874
|
-
server.unshift 'falcon' if ruby_engine != 'jruby'
|
1875
|
-
server.unshift 'mongrel' if ruby_engine.nil?
|
1876
1930
|
server.unshift 'thin' if ruby_engine != 'jruby'
|
1931
|
+
server.unshift 'falcon' if ruby_engine != 'jruby'
|
1877
1932
|
server.unshift 'trinidad' if ruby_engine == 'jruby'
|
1933
|
+
server.unshift 'puma'
|
1878
1934
|
|
1879
1935
|
set :absolute_redirects, true
|
1880
1936
|
set :prefixed_redirects, false
|
@@ -1991,7 +2047,7 @@ module Sinatra
|
|
1991
2047
|
delegate :get, :patch, :put, :post, :delete, :head, :options, :link, :unlink,
|
1992
2048
|
:template, :layout, :before, :after, :error, :not_found, :configure,
|
1993
2049
|
:set, :mime_type, :enable, :disable, :use, :development?, :test?,
|
1994
|
-
:production?, :helpers, :settings, :register
|
2050
|
+
:production?, :helpers, :settings, :register, :on_start, :on_stop
|
1995
2051
|
|
1996
2052
|
class << self
|
1997
2053
|
attr_accessor :target
|
data/lib/sinatra/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Mizerany
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2023-
|
14
|
+
date: 2023-08-07 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: mustermann
|
@@ -53,14 +53,14 @@ dependencies:
|
|
53
53
|
requirements:
|
54
54
|
- - '='
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: 3.0
|
56
|
+
version: 3.1.0
|
57
57
|
type: :runtime
|
58
58
|
prerelease: false
|
59
59
|
version_requirements: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
61
|
- - '='
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version: 3.0
|
63
|
+
version: 3.1.0
|
64
64
|
- !ruby/object:Gem::Dependency
|
65
65
|
name: tilt
|
66
66
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- SECURITY.md
|
111
111
|
- VERSION
|
112
112
|
- examples/chat.rb
|
113
|
+
- examples/lifecycle_events.rb
|
113
114
|
- examples/rainbows.conf
|
114
115
|
- examples/rainbows.rb
|
115
116
|
- examples/simple.rb
|
@@ -154,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
155
|
- !ruby/object:Gem::Version
|
155
156
|
version: '0'
|
156
157
|
requirements: []
|
157
|
-
rubygems_version: 3.4.
|
158
|
+
rubygems_version: 3.4.18
|
158
159
|
signing_key:
|
159
160
|
specification_version: 4
|
160
161
|
summary: Classy web-development dressed in a DSL
|