monkrb 0.23.0 → 0.23.1
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/CHANGELOG.md +32 -0
- data/README.md +1 -1
- data/lib/monk/base.rb +51 -3
- data/lib/monk/context.rb +6 -1
- data/lib/monk/persistence/pg/model.rb +19 -6
- data/lib/monk/version.rb +1 -1
- data/lib/monk/views.rb +28 -13
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ef86c4755d55dba598019b3964d0c9abd29176f4a6bf2807a699e1ac883202e5
|
|
4
|
+
data.tar.gz: 6b7082937eb3b58f03ebe309df0ad6a2eed1722347d38ffce2e4d24cfdd4a92a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 65464f1edfc7ec5edcb77a4f2fed00b0a94ba4e51f534b7e3f089b9e90b578a352d58d32b58f77e9657aa1329ca26d2446beb118bce311652fbe25c0ff220d9c
|
|
7
|
+
data.tar.gz: 9801a314ff3b7a299626fd4085752a87079bfd6b1a4110021f117906a5e4c4c5040fb82c0ea3dad74510dd34491ae71211124cd6ba9b0db5bdea35285b3622db
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,38 @@ All notable changes to this project are documented here. Format is loosely
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/); versions are as released
|
|
5
5
|
in `lib/monk/version.rb`.
|
|
6
6
|
|
|
7
|
+
## 0.23.1 - 2026-11-10
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- **Fewer allocations per request in models and views.** On Ruby 4.0
|
|
12
|
+
every GC stops every worker Ractor, so allocations are what limit a
|
|
13
|
+
process's scaling. A board page (50 messages through a partial) goes
|
|
14
|
+
from 2,157 objects to 1,063, and a JSON read of the same messages
|
|
15
|
+
from 998 to 748, with byte-identical responses; GC time per request
|
|
16
|
+
falls about 30%.
|
|
17
|
+
- `Pg::Model` asks pg for Symbol-keyed rows (`field_name_type`)
|
|
18
|
+
instead of transforming String keys: one Hash per row, not two.
|
|
19
|
+
- An Array condition in `where` (and `find_all`) is one array
|
|
20
|
+
parameter, `col = ANY($1)`, instead of `col IN ($1, ..., $n)`;
|
|
21
|
+
a list of Integers is encoded without a String per id. Same rows,
|
|
22
|
+
nil items included.
|
|
23
|
+
- `<%= %>` escapes with `ERB::Escape.html_escape`, which returns
|
|
24
|
+
text with nothing to escape as is, and appends the result without
|
|
25
|
+
wrapping it in `Raw`; a template's buffer is a `Raw` from the start
|
|
26
|
+
rather than copied into one. `h` uses `ERB::Escape` too.
|
|
27
|
+
- `Context#render` no longer allocates its header key per template.
|
|
28
|
+
- **Route blocks run as methods.** `freeze!` defines each route and
|
|
29
|
+
`error` block as a private method of `Monk::Context`, once, and a
|
|
30
|
+
request calls it instead of `instance_exec`-ing the block. Same `self`,
|
|
31
|
+
ivars and return value; a block taking the context (`|c|`) still gets
|
|
32
|
+
it, and one needing two or more arguments keeps `instance_exec`. On
|
|
33
|
+
Ruby 4.1.0-preview1, `instance_exec` in a worker Ractor stops that
|
|
34
|
+
Ractor's own GCs: the heap grew to 1–3.8 GB between global,
|
|
35
|
+
stop-the-world GCs, and 4 workers served 64K req/s. As methods: 148K
|
|
36
|
+
req/s at 225 MB, against 120K at 86 MB on 4.0.7 (`GET /health`, 4
|
|
37
|
+
cores, locally). No change on 4.0.
|
|
38
|
+
|
|
7
39
|
## 0.23.0 - 2026-10-26
|
|
8
40
|
|
|
9
41
|
### Added
|
data/README.md
CHANGED
|
@@ -89,6 +89,6 @@ More (server modes, Docker) in [`docs/development.md`](docs/development.md).
|
|
|
89
89
|
|
|
90
90
|
## Status
|
|
91
91
|
|
|
92
|
-
Monk is **pre-1.0** (see `lib/monk/version.rb` and the [changelog](CHANGELOG.md)): the API may still change between minor versions. The core is described in [`docs/history/core-plan.md`](docs/history/core-plan.md). Features to add are listed in [`docs/roadmap.md`](docs/roadmap.md). Done so far, each with its own design doc and plan: persistence, migrations, HTML templating and static assets, auth and sessions, WebSocket with Redis fan-out, log levels, live updates (`Monk::Live`, as of 2026-09-19), deployment support (Dockerfile scaffolding, `docs/guides/deploying.md`), the RubyGems release as `monkrb` (both 2026-09-22), and `Monk::WebSocket::PgFanout`/`monk new --live --postgres` — the same cross-process fan-out over Postgres `LISTEN`/`NOTIFY` instead of Redis, for an app that doesn't want a second piece of infrastructure (`docs/design/live-pg-fanout.md`, 2026-09-24), the built-in mailer `Monk::Mail` (0.17, 2026-09-26, ADR 0012), and background jobs on Postgres, `Monk::Jobs`, with mail sent from jobs (0.18, 2026-09-29, ADRs 0013 and 0014). In 0.19 (2026-10-01): `monk new` lays the app out under `app/`, one directory per role (ADR 0015); a WebSocket fanout listens only in the process that holds the sockets (`#listen!`); `authenticate: :optional` lets visitors' sockets in anonymously, for Live's rules to decide; and the WebSocket server stops cleanly on `TERM` (`docs/design/websocket.md`). In 0.20 (2026-10-05): Monk recovers from a dropped database connection (a Postgres restart, a failover) without a process restart. `checkout` reconnects a dead connection before running its block, never retrying the block itself; both fanouts' listeners reconnect and then make every open page resync; and a route that raises logs why (`docs/history/plan-pg-reconnect.md`). Also new: connection pools for Ractors that shouldn't each hold a connection (`Monk::Persistence::Pg.pool`). The WebSocket server's session checks, Live's subscribe rules and `PgFanout`'s publisher can run through one, so open pages share a few connections instead of holding one each: 1,000 open sockets on 4 connections (`docs/guides/persistence.md`, "Pools"; `docs/history/plan-pg-pool.md`). In 0.21 (2026-10-09): scaffolding by module. `monk new` makes the core app, and `monk add` adds a module (Postgres, Redis, mail, auth, jobs, WebSocket, Live) to any app, new or existing, with its wiring, commented examples, a test, and its sections of `SETUP.md` and an `AGENTS.md` for coding agents; `--json` for agents and scripts. Redis and WebSocket become modules of their own, the WebSocket transport is chosen once per app, and `monk new`'s module flags are gone (ADR 0017, `docs/guides/scaffolding.md`, `docs/history/plan-scaffold.md`). In 0.22 (2026-10-13): recurring jobs on a cron schedule, `Monk::Jobs.recurring` in `config/recurring.rb`, enqueued once per occurrence however many `bin/jobs` run (ADR 0016, `docs/guides/jobs.md`); auth housekeeping, with a login-email rate limit in the database, an index on `sessions (subject)` and a nightly cleanup job (`docs/guides/auth.md`); and an upgrade guide for apps made with an earlier Monk (`docs/guides/upgrading.md`). In 0.22.1 (2026-10-20): the WebSocket server queues a connect burst instead of collapsing under it, refuses a handshake it can't check with a `503`, and takes more connects a second with thousands of sockets open: fewer allocations a connect, GC settings in `bin/websocket_server`, and `WS_PROCESSES` for several server processes on one port (`docs/guides/websocket.md`, `docs/plan-ws-connect-ceiling.md`). In 0.22.2 (2026-10-22): a fix for a handshake that could die when two connections were the first after boot (`docs/plan-digest-preload.md`). In 0.23 (2026-10-26): file storage, `Monk::Storage`, in any S3-compatible bucket (Hetzner, R2, B2, AWS) through Monk's own SigV4 signer, with local files in development and test that behave like a bucket. Browsers upload straight to the bucket and download through a redirect to a signed link, so no worker Ractor waits on the bytes, and `monk add storage` scaffolds the flow (ADR 0018, `docs/guides/storage.md`, `docs/history/plan-storage.md`).
|
|
92
|
+
Monk is **pre-1.0** (see `lib/monk/version.rb` and the [changelog](CHANGELOG.md)): the API may still change between minor versions. The core is described in [`docs/history/core-plan.md`](docs/history/core-plan.md). Features to add are listed in [`docs/roadmap.md`](docs/roadmap.md). Done so far, each with its own design doc and plan: persistence, migrations, HTML templating and static assets, auth and sessions, WebSocket with Redis fan-out, log levels, live updates (`Monk::Live`, as of 2026-09-19), deployment support (Dockerfile scaffolding, `docs/guides/deploying.md`), the RubyGems release as `monkrb` (both 2026-09-22), and `Monk::WebSocket::PgFanout`/`monk new --live --postgres` — the same cross-process fan-out over Postgres `LISTEN`/`NOTIFY` instead of Redis, for an app that doesn't want a second piece of infrastructure (`docs/design/live-pg-fanout.md`, 2026-09-24), the built-in mailer `Monk::Mail` (0.17, 2026-09-26, ADR 0012), and background jobs on Postgres, `Monk::Jobs`, with mail sent from jobs (0.18, 2026-09-29, ADRs 0013 and 0014). In 0.19 (2026-10-01): `monk new` lays the app out under `app/`, one directory per role (ADR 0015); a WebSocket fanout listens only in the process that holds the sockets (`#listen!`); `authenticate: :optional` lets visitors' sockets in anonymously, for Live's rules to decide; and the WebSocket server stops cleanly on `TERM` (`docs/design/websocket.md`). In 0.20 (2026-10-05): Monk recovers from a dropped database connection (a Postgres restart, a failover) without a process restart. `checkout` reconnects a dead connection before running its block, never retrying the block itself; both fanouts' listeners reconnect and then make every open page resync; and a route that raises logs why (`docs/history/plan-pg-reconnect.md`). Also new: connection pools for Ractors that shouldn't each hold a connection (`Monk::Persistence::Pg.pool`). The WebSocket server's session checks, Live's subscribe rules and `PgFanout`'s publisher can run through one, so open pages share a few connections instead of holding one each: 1,000 open sockets on 4 connections (`docs/guides/persistence.md`, "Pools"; `docs/history/plan-pg-pool.md`). In 0.21 (2026-10-09): scaffolding by module. `monk new` makes the core app, and `monk add` adds a module (Postgres, Redis, mail, auth, jobs, WebSocket, Live) to any app, new or existing, with its wiring, commented examples, a test, and its sections of `SETUP.md` and an `AGENTS.md` for coding agents; `--json` for agents and scripts. Redis and WebSocket become modules of their own, the WebSocket transport is chosen once per app, and `monk new`'s module flags are gone (ADR 0017, `docs/guides/scaffolding.md`, `docs/history/plan-scaffold.md`). In 0.22 (2026-10-13): recurring jobs on a cron schedule, `Monk::Jobs.recurring` in `config/recurring.rb`, enqueued once per occurrence however many `bin/jobs` run (ADR 0016, `docs/guides/jobs.md`); auth housekeeping, with a login-email rate limit in the database, an index on `sessions (subject)` and a nightly cleanup job (`docs/guides/auth.md`); and an upgrade guide for apps made with an earlier Monk (`docs/guides/upgrading.md`). In 0.22.1 (2026-10-20): the WebSocket server queues a connect burst instead of collapsing under it, refuses a handshake it can't check with a `503`, and takes more connects a second with thousands of sockets open: fewer allocations a connect, GC settings in `bin/websocket_server`, and `WS_PROCESSES` for several server processes on one port (`docs/guides/websocket.md`, `docs/plan-ws-connect-ceiling.md`). In 0.22.2 (2026-10-22): a fix for a handshake that could die when two connections were the first after boot (`docs/plan-digest-preload.md`). In 0.23 (2026-10-26): file storage, `Monk::Storage`, in any S3-compatible bucket (Hetzner, R2, B2, AWS) through Monk's own SigV4 signer, with local files in development and test that behave like a bucket. Browsers upload straight to the bucket and download through a redirect to a signed link, so no worker Ractor waits on the bytes, and `monk add storage` scaffolds the flow (ADR 0018, `docs/guides/storage.md`, `docs/history/plan-storage.md`). In 0.23.1 (2026-11-10): fewer allocations per request in models and views, about half as many objects for a page of 50 rows through a partial, so less GC on Ruby 4.0, where every GC stops every worker; responses are byte-identical. Route blocks also run as methods of `Monk::Context` instead of through `instance_exec`, the same on 4.0 and what lets Ruby 4.1's per-Ractor GC work.
|
|
93
93
|
|
|
94
94
|
[Made with Love ❤️, Ruby 💎 and AI 🤖](docs/ai_usage_disclaimer.md)
|
data/lib/monk/base.rb
CHANGED
|
@@ -111,6 +111,8 @@ module Monk
|
|
|
111
111
|
end
|
|
112
112
|
end
|
|
113
113
|
|
|
114
|
+
define_block_methods!
|
|
115
|
+
|
|
114
116
|
Monk.freeze!
|
|
115
117
|
|
|
116
118
|
# Settled once here, not read from ENV per request in
|
|
@@ -126,6 +128,7 @@ module Monk
|
|
|
126
128
|
Ractor.make_shareable(@static_routes)
|
|
127
129
|
Ractor.make_shareable(@dynamic_routes)
|
|
128
130
|
Ractor.make_shareable(error_handlers)
|
|
131
|
+
Ractor.make_shareable(@block_methods)
|
|
129
132
|
self
|
|
130
133
|
end
|
|
131
134
|
|
|
@@ -168,14 +171,14 @@ module Monk
|
|
|
168
171
|
context = Context.new(params, env)
|
|
169
172
|
catch(:monk_halt) do
|
|
170
173
|
begin
|
|
171
|
-
body =
|
|
174
|
+
body = run_block(context, route[:block])
|
|
172
175
|
[200, context.headers, [body]]
|
|
173
176
|
rescue StandardError => e
|
|
174
177
|
handler = error_handlers.find { |matcher, _| matcher.is_a?(Class) && e.is_a?(matcher) }
|
|
175
178
|
log_exception(env, e, handled: !handler.nil?)
|
|
176
179
|
if handler
|
|
177
180
|
context.status = 500
|
|
178
|
-
body =
|
|
181
|
+
body = run_block(context, handler.last)
|
|
179
182
|
[500, context.headers, [body]]
|
|
180
183
|
else
|
|
181
184
|
[500, { "content-type" => "application/json" }, ['{"error":"Internal Server Error"}']]
|
|
@@ -184,6 +187,51 @@ module Monk
|
|
|
184
187
|
end
|
|
185
188
|
end
|
|
186
189
|
|
|
190
|
+
# Each route and error-handler block becomes a private method of
|
|
191
|
+
# Context, once, here, rather than running through instance_exec on
|
|
192
|
+
# every request. Same self, same ivars, same return value. On Ruby
|
|
193
|
+
# 4.1 (preview1) an instance_exec in a worker Ractor stops that
|
|
194
|
+
# Ractor's own, cheap GCs: its heap grows to GBs between global,
|
|
195
|
+
# stop-the-world ones, and adding workers makes the app slower. A
|
|
196
|
+
# method call doesn't, and on 4.0 the two cost the same. Blocks are
|
|
197
|
+
# made shareable before this, so the methods run in any Ractor.
|
|
198
|
+
#
|
|
199
|
+
# A method's arity is strict, as a lambda's: a block with no
|
|
200
|
+
# positional parameter is called with none, one that can take the
|
|
201
|
+
# context (|c|, |c = nil|, |*args|) with it. A block that needs two
|
|
202
|
+
# or more (instance_exec passes nil for the rest) keeps
|
|
203
|
+
# instance_exec.
|
|
204
|
+
def define_block_methods!
|
|
205
|
+
@block_methods = {}.compare_by_identity
|
|
206
|
+
blocks = routes.map { |route| route[:block] } + error_handlers.map(&:last)
|
|
207
|
+
blocks.each_with_index do |block, i|
|
|
208
|
+
name = :"__monk_block_#{object_id}_#{i}"
|
|
209
|
+
Context.define_method(name, &block)
|
|
210
|
+
Context.send(:private, name)
|
|
211
|
+
|
|
212
|
+
takes_context = block.parameters.any? { |type, _| %i[req opt rest].include?(type) }
|
|
213
|
+
arity = Context.instance_method(name).arity
|
|
214
|
+
if !takes_context
|
|
215
|
+
@block_methods[block] = [name, false].freeze
|
|
216
|
+
elsif arity == 1 || arity.between?(-2, -1)
|
|
217
|
+
@block_methods[block] = [name, true].freeze
|
|
218
|
+
else
|
|
219
|
+
Context.send(:remove_method, name)
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def run_block(context, block)
|
|
225
|
+
name, with_context = @block_methods[block]
|
|
226
|
+
if name.nil?
|
|
227
|
+
context.instance_exec(context, &block)
|
|
228
|
+
elsif with_context
|
|
229
|
+
context.__send__(name, context)
|
|
230
|
+
else
|
|
231
|
+
context.__send__(name)
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
187
235
|
# The request line alone ("GET /x -> 500") says nothing about why.
|
|
188
236
|
# Unhandled: an error, with the first backtrace lines. Handled by an
|
|
189
237
|
# `error` block: the app chose the response, so one info line --
|
|
@@ -218,7 +266,7 @@ module Monk
|
|
|
218
266
|
|
|
219
267
|
context = Context.new({}, env, status: 404)
|
|
220
268
|
catch(:monk_halt) do
|
|
221
|
-
body =
|
|
269
|
+
body = run_block(context, handler.last)
|
|
222
270
|
[404, context.headers, [body]]
|
|
223
271
|
end
|
|
224
272
|
end
|
data/lib/monk/context.rb
CHANGED
|
@@ -17,6 +17,11 @@ module Monk
|
|
|
17
17
|
include Monk::Views::Compiled
|
|
18
18
|
include Monk::HeadTags
|
|
19
19
|
|
|
20
|
+
# Frozen once here: as literals in #render, the key was a new String
|
|
21
|
+
# for every template rendered, partials included.
|
|
22
|
+
CONTENT_TYPE = "content-type".freeze
|
|
23
|
+
HTML_CONTENT_TYPE = "text/html; charset=utf-8".freeze
|
|
24
|
+
|
|
20
25
|
attr_reader :params, :env, :headers
|
|
21
26
|
attr_accessor :status
|
|
22
27
|
|
|
@@ -60,7 +65,7 @@ module Monk
|
|
|
60
65
|
# `self` bound to this same Context), and the `locals` hash passed
|
|
61
66
|
# here, which is what a partial rendered inside a loop wants.
|
|
62
67
|
def render(name, layout: :default, **locals)
|
|
63
|
-
headers[
|
|
68
|
+
headers[CONTENT_TYPE] ||= HTML_CONTENT_TYPE
|
|
64
69
|
Monk::Views.render(self, name, locals, layout: layout)
|
|
65
70
|
end
|
|
66
71
|
|
|
@@ -19,6 +19,13 @@ module Monk
|
|
|
19
19
|
ne: "<>",
|
|
20
20
|
}.freeze
|
|
21
21
|
|
|
22
|
+
# An Array condition's values as one array parameter (#in_clause).
|
|
23
|
+
# The Integer one, for a list of ids, writes each number straight
|
|
24
|
+
# into the literal; the generic one makes a String of each item
|
|
25
|
+
# first. Shareable: worker Ractors encode with them.
|
|
26
|
+
ARRAY_ENCODER = Ractor.make_shareable(PG::TextEncoder::Array.new)
|
|
27
|
+
INTEGER_ARRAY_ENCODER = Ractor.make_shareable(PG::TextEncoder::Array.new(elements_type: PG::TextEncoder::Integer.new))
|
|
28
|
+
|
|
22
29
|
class << self
|
|
23
30
|
def create(data)
|
|
24
31
|
Monk::Persistence::Pg.checkout(db_name) do |conn|
|
|
@@ -196,14 +203,17 @@ module Monk
|
|
|
196
203
|
"#{ident} #{operator} $#{values.size}"
|
|
197
204
|
end
|
|
198
205
|
|
|
206
|
+
# `= ANY($n)` with the whole list as one Postgres array literal,
|
|
207
|
+
# not `IN ($1, ..., $n)`: a placeholder String and an encoded
|
|
208
|
+
# parameter per item made a 40-id lookup ~160 objects. Postgres
|
|
209
|
+
# types the parameter from the column, as it typed each `$n`,
|
|
210
|
+
# and a nil item matches nothing in either form.
|
|
199
211
|
def in_clause(ident, list, values)
|
|
200
212
|
return "1 = 0" if list.empty?
|
|
201
213
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
end
|
|
206
|
-
"#{ident} IN (#{placeholders.join(", ")})"
|
|
214
|
+
integers = list.all? { |item| item.nil? || item.is_a?(Integer) }
|
|
215
|
+
values << (integers ? INTEGER_ARRAY_ENCODER : ARRAY_ENCODER).encode(list)
|
|
216
|
+
"#{ident} = ANY($#{values.size})"
|
|
207
217
|
end
|
|
208
218
|
|
|
209
219
|
def order_clause(conn, order)
|
|
@@ -219,8 +229,11 @@ module Monk
|
|
|
219
229
|
" ORDER BY #{clause}"
|
|
220
230
|
end
|
|
221
231
|
|
|
232
|
+
# Symbol keys straight from pg, not String keys transformed: one
|
|
233
|
+
# Hash per row instead of two.
|
|
222
234
|
def to_rows(result)
|
|
223
|
-
result.
|
|
235
|
+
result.field_name_type = :symbol
|
|
236
|
+
result.to_a
|
|
224
237
|
end
|
|
225
238
|
|
|
226
239
|
def to_row(result)
|
data/lib/monk/version.rb
CHANGED
|
@@ -5,5 +5,5 @@ module Monk
|
|
|
5
5
|
# class of bug .freeze! guards against for routes/error handlers/models,
|
|
6
6
|
# just on a top-level constant that every worker Ractor reads on boot
|
|
7
7
|
# (found live under kino: GET /hello 500'd until this was frozen).
|
|
8
|
-
VERSION = "0.23.
|
|
8
|
+
VERSION = "0.23.1".freeze
|
|
9
9
|
end
|
data/lib/monk/views.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
require "erb"
|
|
2
|
-
require "
|
|
2
|
+
require "erb/escape"
|
|
3
3
|
|
|
4
4
|
require_relative "freeze_hooks"
|
|
5
5
|
require_relative "errors"
|
|
@@ -36,6 +36,9 @@ module Monk
|
|
|
36
36
|
|
|
37
37
|
DEFAULT_ROOT = "views".freeze
|
|
38
38
|
|
|
39
|
+
# What every compiled template's buffer starts as a dup of.
|
|
40
|
+
EMPTY = Raw.new("").freeze
|
|
41
|
+
|
|
39
42
|
class << self
|
|
40
43
|
# Every reader here is a plain attr_reader over an ivar initialized
|
|
41
44
|
# eagerly below, never `@x ||= ...`: a lazy reader *writes* on first
|
|
@@ -46,17 +49,27 @@ module Monk
|
|
|
46
49
|
|
|
47
50
|
# Escapes unless the value is already Raw, and returns Raw either
|
|
48
51
|
# way -- so an explicit `<%= h(x) %>` isn't escaped twice by the
|
|
49
|
-
# implicit
|
|
50
|
-
#
|
|
51
|
-
# byte-identical output: ERB's is not flagged
|
|
52
|
-
# worker calling it raises Ractor::UnsafeError
|
|
53
|
-
# method called from not main ractor"). Found by
|
|
52
|
+
# implicit escape the compiler wraps every `<%= %>` in.
|
|
53
|
+
# ERB::Escape.html_escape, not ERB::Util.html_escape, though they
|
|
54
|
+
# produce byte-identical output: ERB::Util's is not flagged
|
|
55
|
+
# Ractor-safe, so a worker calling it raises Ractor::UnsafeError
|
|
56
|
+
# ("ractor unsafe method called from not main ractor"). Found by
|
|
54
57
|
# test/ractor_integration_test.rb, in a real worker, not by reading
|
|
55
|
-
# either implementation.
|
|
58
|
+
# either implementation. ERB::Escape's is, and unlike
|
|
59
|
+
# CGI.escapeHTML it returns its argument when there's nothing to
|
|
60
|
+
# escape instead of a copy.
|
|
56
61
|
def h(value)
|
|
57
62
|
return value if value.is_a?(Raw)
|
|
58
63
|
|
|
59
|
-
Raw.new(
|
|
64
|
+
Raw.new(ERB::Escape.html_escape(value.to_s))
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The implicit escape around every `<%= %>` (ERB has already called
|
|
68
|
+
# #to_s): #h without the Raw wrapper, since the result is appended
|
|
69
|
+
# to the buffer and dropped. For text with nothing to escape, the
|
|
70
|
+
# common case, it allocates nothing.
|
|
71
|
+
def escape(value)
|
|
72
|
+
value.is_a?(Raw) ? value : ERB::Escape.html_escape(value)
|
|
60
73
|
end
|
|
61
74
|
|
|
62
75
|
def compile(name, source, path = "(erb)")
|
|
@@ -143,14 +156,16 @@ module Monk
|
|
|
143
156
|
|
|
144
157
|
# `<%= %>` escapes by default -- a deliberate break from stock ERB,
|
|
145
158
|
# where it doesn't. ERB::Compiler emits `insert_cmd((expr).to_s)`
|
|
146
|
-
# for every insertion, so pointing insert_cmd at Views.
|
|
147
|
-
# whole implementation; `raw(x)` opts back out.
|
|
159
|
+
# for every insertion, so pointing insert_cmd at Views.escape is the
|
|
160
|
+
# whole implementation; `raw(x)` opts back out. The buffer is a Raw
|
|
161
|
+
# from the start (a dup of EMPTY), so the template returns it as is
|
|
162
|
+
# rather than copying it into one.
|
|
148
163
|
def compiled_source(source)
|
|
149
164
|
compiler = ERB::Compiler.new("-")
|
|
150
|
-
compiler.pre_cmd = ["_erbout = +
|
|
151
|
-
compiler.post_cmd = ["
|
|
165
|
+
compiler.pre_cmd = ["_erbout = +Monk::Views::EMPTY"]
|
|
166
|
+
compiler.post_cmd = ["_erbout"]
|
|
152
167
|
compiler.put_cmd = "_erbout.<<"
|
|
153
|
-
compiler.insert_cmd = "_erbout.<< Monk::Views.
|
|
168
|
+
compiler.insert_cmd = "_erbout.<< Monk::Views.escape"
|
|
154
169
|
script, encoding, = compiler.compile(source)
|
|
155
170
|
# ERB::Compiler#compile scans the source as ASCII-8BIT (see its
|
|
156
171
|
# own String#b) and hands that tag straight through to any code
|