edgeway 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/README.md +43 -19
  4. data/js/cgi.js +11 -20
  5. data/js/cloudflare.js +25 -0
  6. data/js/jwt.js +1 -1
  7. data/js/runtime.js +6 -4
  8. data/lib/edgeway/cache/store.rb +2 -2
  9. data/lib/edgeway/cli/build/app_files.rb +22 -18
  10. data/lib/edgeway/cli/build/class_source.rb +21 -0
  11. data/lib/edgeway/cli/build/cloudflare_platform.rb +56 -0
  12. data/lib/edgeway/cli/build/dispatch_compiler.rb +71 -0
  13. data/lib/edgeway/cli/build/dispatch_probe.rb +52 -0
  14. data/lib/edgeway/cli/build/js_files.rb +12 -4
  15. data/lib/edgeway/cli/build/platform_config.rb +36 -0
  16. data/lib/edgeway/cli/build/view_compiler.rb +2 -11
  17. data/lib/edgeway/cli/build.rb +67 -28
  18. data/lib/edgeway/cli/cloudflare_test.rb +5 -2
  19. data/lib/edgeway/cli/generate.rb +11 -10
  20. data/lib/edgeway/cli/test.rb +11 -3
  21. data/lib/edgeway/cli/toolchain.rb +3 -3
  22. data/lib/edgeway/cli.rb +1 -1
  23. data/lib/edgeway/controller/base.rb +4 -2
  24. data/lib/edgeway/controller/{authentication.rb → bearer_token_authentication.rb} +4 -5
  25. data/lib/edgeway/controller/caching.rb +1 -2
  26. data/lib/edgeway/controller/dispatcher.rb +11 -21
  27. data/lib/edgeway/controller/rate_limiting.rb +1 -2
  28. data/lib/edgeway/errors.rb +1 -1
  29. data/lib/edgeway/generators/app/templates/README.md.tt +1 -1
  30. data/lib/edgeway/generators/app/templates/config/edgeway.yml +2 -0
  31. data/lib/edgeway/generators/app/templates/mise.toml.tt +1 -1
  32. data/lib/edgeway/http/form_body.rb +31 -0
  33. data/lib/edgeway/http/request.rb +3 -13
  34. data/lib/edgeway/model/attributes.rb +11 -6
  35. data/lib/edgeway/routing/mapper.rb +5 -5
  36. data/lib/edgeway/routing/route_set.rb +2 -2
  37. data/lib/edgeway/service/jwt.rb +30 -0
  38. data/lib/edgeway/version.rb +1 -1
  39. data/lib/edgeway.rb +2 -2
  40. metadata +11 -4
  41. data/lib/edgeway/service/cloudflare/jwt.rb +0 -32
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 191edfc2697fdff2f0571a3e9ee1cb27b0fbebc885ecfabbfa643e9418533c37
4
- data.tar.gz: 1c22ffe618aa3f3b51c28033708cc98a40ecd167c50ab7a4a2d159c613e43965
3
+ metadata.gz: ee9317f07873b75ea0a76dca4b498b5e319643e5e479a071986e299d157595a7
4
+ data.tar.gz: 011bcdf4b38d6ea129beec52ab932b25e94edc8eb04037610ff1a5af13e49661
5
5
  SHA512:
6
- metadata.gz: 71b96facf9a650bb524bbc0baf2b9caf28c94e92365daa88bb36844f7087c5d350b4c9876ef95e4f4964aa971cf0b60b3712c7ab8bf1a21228cf0ad893f05b4f
7
- data.tar.gz: ef01cf54fb275025d8f1d7d447a623f150072e399bf235891cac5fb1d6bb44511d75e76b8aafb754e1500f58123e11b5c7ad6b06851a1407ac23a98fcd7138c1
6
+ metadata.gz: 816f1091e2e2f42e3060a8e8a185c7128a051b9a2fefba7492cecc2f28b8f7917f8cd399edffddbe8ba38a77db6df24cd509ffa15485bb56635a7eac55c70c4a
7
+ data.tar.gz: 5284a3a401eb745a775e137ce41952dbb60af81490141c186030736212c028531876b6ae290eb49f1073d63d36638262a2045cce646128c6a517f82600946c25
data/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v0.4.0
6
+
7
+ ### Added
8
+
9
+ - `edgeway generate` takes a `binary` field type.
10
+ - `config/edgeway.yml` with `platform:` (only `cloudflare` for now, and the default without the file). `edgeway new` writes it.
11
+
12
+ ### Changed
13
+
14
+ - A route to a controller that is not defined, or to an action that is not a public method of it (private methods and `Edgeway::Controller::Base`'s own public methods are not actions), is a build error, not a 404 at run time.
15
+ - `edgeway build` and `edgeway test` now load the app's files in CRuby to read the routes (`edgeway routes` loads only `config/routes.rb`).
16
+ The library files that use Spinel's `ffi_*` declarations can't load there and are skipped, so an app file that uses their classes when it is loaded fails the build.
17
+ - `:bigint` values can be passed as Integers as well as Strings; an Integer beyond 32 bits no longer raises `RangeError` in a wasm build.
18
+
19
+ ### Fixed
20
+
21
+ - `Edgeway::Model.batch` left with `break` or `return` now sends the queued writes and ends the batch. Before, the writes were lost and every later write was queued and never run.
22
+
5
23
  ## v0.3.0
6
24
 
7
25
  ### Added
data/README.md CHANGED
@@ -101,7 +101,7 @@ bin/build
101
101
  pnpm exec wrangler dev
102
102
  edgeway routes
103
103
 
104
- # bin/build --release (one unit, without --incremental), remote D1 migrations,
104
+ # bin/build --release (one unit, without --incremental, debug info stripped), remote D1 migrations,
105
105
  # then pnpm exec wrangler deploy
106
106
  bin/deploy
107
107
  ```
@@ -138,7 +138,7 @@ edgeway generate controller Todos index show
138
138
  edgeway generate scaffold Todo title:string done:boolean
139
139
  ```
140
140
 
141
- - Field types are `string` (the default), `text`, `integer`, `float`, `boolean` and `datetime`.
141
+ - Field types are `string` (the default), `text`, `integer`, `float`, `boolean`, `datetime` and `binary` (a BLOB; its values are base64 Strings in requests and JSON).
142
142
  - A model gets `timestamps` and an `id`, `created_at` and `updated_at` column.
143
143
  - The migration is numbered one above the highest in `db/migrations`.
144
144
  - Each action of a controller is a stub that renders `{}`.
@@ -156,16 +156,23 @@ edgeway generate scaffold Todo title:string done:boolean
156
156
  and the WASI SDK is pointed at by `WASI_SDK`.
157
157
  - Without mise, set `SPINEL` (or put `spinel` on `PATH`) and `WASI_SDK`.
158
158
  - `SPINEL` wins over `spinel` on `PATH`, which is how to use a Spinel checkout instead of a release.
159
-
160
- See `todo-api` in [edgeway-examples](https://github.com/y-yagi/edgeway-examples) for an example (checked with its `check.sh`).
159
+ - The build (and `edgeway test`) reads the routes by loading the library files `build/main.rb` requires, the app's files under `app/` and `config/routes.rb` in CRuby,
160
+ and generates the code that creates each routed controller and calls each routed action (`build/dispatch.rb`, `tmp/dispatch.rb`),
161
+ so the wasm holds only the controllers and actions the routes use.
162
+ A route to a controller that isn't defined, or to an action that isn't a public method of it
163
+ (a private method, or a public method of `Edgeway::Controller::Base` itself, is not an action), is a build error, not a 404 at run time.
164
+ - That makes loading the app's files in CRuby a requirement of the build, which `edgeway routes` (it loads only `config/routes.rb`) doesn't have.
165
+ The library files that use Spinel's `ffi_*` declarations (the D1 adapter, `edgeway/service/jwt`, `fetch` and `rate_limit`) can't load in CRuby and are skipped,
166
+ so an app file that uses one of their classes (`Edgeway::JWTVerifier`, ...) when it is loaded, instead of when a request runs, fails the build.
161
167
 
162
168
  ### Spinel version
163
169
 
164
- Edgeway needs a Spinel newer than the 2026.09.12 release
165
- (that release refuses `send` with a computed name, which `Callbacks#run_before_actions` uses).
170
+ Edgeway needs a Spinel at or after the pinned commit below.
171
+ No release has all the fixes Edgeway relies on yet
172
+ (the 2026.09.12 release, for one, refuses `send` with a computed name, which `Callbacks#run_before_actions` uses).
166
173
 
167
174
  - `mise.toml` (Edgeway's own and the one `edgeway new` writes) pins a commit of [matz/spinel](https://github.com/matz/spinel),
168
- `ec0ad13e4`, which mise-spinel builds from source.
175
+ `1138767c4`, which mise-spinel builds from source.
169
176
  A commit rather than `master`, so that an install is the Spinel that was tested.
170
177
  - The WASI SDK (`wasi-sdk-34`) is installed first,
171
178
  because mise-spinel builds Spinel's wasm32-wasi runtime only when it finds one.
@@ -173,6 +180,18 @@ Edgeway needs a Spinel newer than the 2026.09.12 release
173
180
 
174
181
  ## Configuration
175
182
 
183
+ `config/edgeway.yml` sets the platform the app is built for:
184
+
185
+ ```yaml
186
+ platform: cloudflare
187
+ ```
188
+
189
+ - `cloudflare` is the only platform, and the default: without the file the platform is `cloudflare`.
190
+ - It is one setting for the whole app, not one per environment.
191
+ - An unknown platform or key fails `edgeway build`.
192
+ - `edgeway build` fails when the platform does not support the database adapter or cache store the environment uses.
193
+ - `edgeway new` writes the file.
194
+
176
195
  Two files configure the database, each for its own side:
177
196
 
178
197
  - `config/database.yml` is Edgeway's: for each environment (`edgeway build --env ENV`),
@@ -235,12 +254,15 @@ Anything else, such as a key the Worker holds for another purpose, is never visi
235
254
  and it renders in JSON as a String.
236
255
  - D1 hands rows over as JavaScript numbers, so a value beyond 2^53 (9007199254740991) loses precision before Ruby sees it.
237
256
  Keep values below 2^53, or store them as `:string`.
238
- - Pass bigint values as Strings: `Todo.find("3000000000")`, `where(id: "3000000000")`, `create!(owner_id: "5000000000")`,
239
- `Model.execute("... WHERE id = ?", ["3000000000"])`. An Integer literal beyond 32 bits in a Hash raises `RangeError` in a wasm build (a Spinel limitation).
257
+ - Pass bigint values as Integers or Strings: `Todo.find(3000000000)` and `Todo.find("3000000000")` are the same, and so are
258
+ `where(id: 3000000000)`, `create!(owner_id: 5000000000)` and `where(id: [3000000000, 4000000000])`. The attribute holds the String either way.
259
+ Only a Range with a bound beyond 32 bits needs Strings: `where(total: "3000000000".."9007199254740993")` (Spinel can't build the Integer one in a wasm build).
260
+ - A bind in raw SQL (`Model.execute`, `Model.select_all`, `find_by_sql`, `where("...", value)`) goes to D1 as a JavaScript number,
261
+ so an Integer beyond 2^53 is rounded there: pass a String (`Model.execute("... WHERE id = ?", ["9007199254740993"])`).
240
262
  - `attribute :id, :bigint` makes the primary key a bigint, and `id` is a String. A foreign key to it is `attribute :owner_id, :bigint`,
241
263
  declared before `belongs_to` so it keeps its type.
242
- - Raw SQL rows (`Model.select_all`) hold what the database gave: an INTEGER beyond 32 bits is a Bignum in a wasm build, and `render json:` of it raises.
243
- Select it as text (`SELECT CAST(id AS TEXT) AS id ...`) or convert it with `Edgeway::Model.cast(:bigint, value)`.
264
+ - Raw SQL rows (`Model.select_all`) hold what the database gave: an INTEGER beyond 32 bits is a Bignum in a wasm build.
265
+ To get a String like a `:bigint` attribute, select it as text (`SELECT CAST(id AS TEXT) AS id ...`) or use `Edgeway::Model.cast(:bigint, value)`.
244
266
  - `Model.execute` answers `"last_insert_id"` as an Integer, or as a decimal String when the rowid is beyond 32 bits.
245
267
 
246
268
  #### `:binary`
@@ -344,7 +366,8 @@ Edgeway::Model.exec("DELETE FROM tags\nDELETE FROM todos") # nil
344
366
  - An Array is `IN`; each value is cast by the column type. An empty Array matches nothing (`1 = 0`).
345
367
  A `nil` in an Array also matches `NULL`: `(position IN (?) OR position IS NULL)`.
346
368
  - A Range is `>=` and `<=` (`<` for `1...5`); an endless or beginless Range has one side.
347
- Spinel can't build a Range of Times: use ISO 8601 Strings (`"2026-10-28"..."2026-10-29"`) or a fragment.
369
+ A Range of Times is bound as UTC Strings (`"2026-10-28T05:00:00.000Z"`).
370
+ Spinel can't build a Range with an Integer bound beyond 32 bits in a wasm build: use Strings for a `:bigint` column ([Attribute types](#attribute-types)).
348
371
  - A String is a raw SQL fragment, in parentheses and ANDed with the rest; its binds follow it.
349
372
  It is raw SQL: put user input in the binds, never interpolate it.
350
373
  Its binds are not cast by column type: only booleans (1/0) and Times (ISO 8601) are converted.
@@ -401,6 +424,7 @@ end
401
424
  - A `create` is refused with `ArgumentError`, because it needs the new row's id.
402
425
  - The counts of a queued write are not known (`delete_all` answers 0).
403
426
  - A block that raises sends nothing, and a nested `batch` joins the outer one.
427
+ - A block left with `break` or `return` sends the writes like one that finishes.
404
428
 
405
429
  ### Associations
406
430
 
@@ -541,7 +565,7 @@ so it sees the action's `@variables`, `params` and the controller's methods.
541
565
  Safety is not kept across string operations (`"<b>".html_safe + x` is not defined): build the HTML in a template,
542
566
  or escape the pieces with `Edgeway::View.escape` and mark the result.
543
567
  `<% code %>`, `<%# comment %>`, `<%%` (a literal `<%`) and `-%>` (drops the newline after the tag) work as in ERB.
544
- - The public methods of `Edgeway::Controller::Base` (`render`, `head`, `params`, `raw`, ...) are not actions; a route to one is a 404.
568
+ - The public methods of `Edgeway::Controller::Base` (`render`, `head`, `params`, `raw`, ...) are not actions; a route to one is a build error.
545
569
  - An action that renders nothing renders its template, or answers 204 when it has none.
546
570
  - `render :name` / `render "name"` renders another template of the controller, `render html: value` a string (HTML-escaped; a `SafeHTML` is output as is);
547
571
  both take `status:` and `layout: false`.
@@ -655,7 +679,7 @@ A misconfigured setting answers every request 500, with the details only in the
655
679
  A token needs a `kid`, and a JWK with `key_ops` must list `verify`.
656
680
  - `JWT_SECRET` must be at least 32 bytes for `HS256`, 48 for `HS384` and 64 for `HS512` (RFC 7518 3.2).
657
681
  - `exp` is required, and a token with a `crit` header is refused (no extension is understood, RFC 7515 4.1.11).
658
- - In tests, use `Edgeway::Auth::StubVerifier` (`edgeway/http/auth/stub`; see `test/auth.rb`).
682
+ - In tests, use `Edgeway::Auth::StubVerifier` (`edgeway/http/auth/stub`; see `test/auth_test.rb`).
659
683
  - Like D1, this needs JSPI.
660
684
  - An example is `GET /me` in `todo-api` of [edgeway-examples](https://github.com/y-yagi/edgeway-examples).
661
685
 
@@ -1050,7 +1074,7 @@ What differs:
1050
1074
  - It is wrangler's local D1 (miniflare), not Cloudflare's: its limits and timing are not reproduced, and neither is read replication
1051
1075
  (`sessions: true` is passed on, but there is one local replica).
1052
1076
  - The app is called directly (`get`, `post`), not through `js/cloudflare.js` and the Worker: no CORS, no `X-D1-Bookmark`.
1053
- - A JWT's signature is checked only when the test sets `Edgeway::Auth.verifier = Edgeway::Cloudflare::JWTVerifier.new`
1077
+ - A JWT's signature is checked only when the test sets `Edgeway::Auth.verifier = Edgeway::JWTVerifier.new`
1054
1078
  (`JWT_SECRET` and the like come from the Worker variables, `.dev.vars` included); otherwise use `Edgeway::Auth::StubVerifier`.
1055
1079
  - `db.log` has the SQL the test ran, but not the migrations'.
1056
1080
 
@@ -1074,11 +1098,10 @@ With SQLite the tests are native builds, while production is wasm32-wasi on Work
1074
1098
  With `adapter: memory` in `test:` they are a native build, and D1 and JSPI are not covered either (see [Testing an app](#testing-an-app)).
1075
1099
  - Not verified on a production Cloudflare deploy yet: CPU and wall time on a cold start and on a JWKS cache miss,
1076
1100
  and read replication with real replicas (local D1 has none, and D1 itself accepts any bookmark there). Everything was checked under `wrangler dev`.
1077
- - Integers are 32 bits in a wasm build: use `:bigint` for larger ones and pass them as Strings,
1101
+ - Integers are 32 bits in a wasm build: use `:bigint` for larger ones (as Integers or Strings),
1078
1102
  and keep them below 2^53, as D1 hands rows over as JavaScript numbers (see [Attribute types](#attribute-types)).
1079
1103
  - A `Model.batch` runs on one database: there is no transaction across databases ([Multiple databases](#multiple-databases)).
1080
1104
  - `Model.exec` does not work on a database with `sessions: true`: a D1 session has no `exec()` ([Queries](#queries)).
1081
- - Spinel can't build a Range of Times: use ISO 8601 Strings or a SQL fragment in `where` ([Queries](#queries)).
1082
1105
  - `first()` and `raw()` are not used and `dump()` is not supported; Time Travel and export/import are `wrangler d1` commands ([D1 API coverage](#d1-api-coverage)).
1083
1106
 
1084
1107
  ## Development
@@ -1088,10 +1111,11 @@ With SQLite the tests are native builds, while production is wasm32-wasi on Work
1088
1111
  bin/setup
1089
1112
 
1090
1113
  # needs libsqlite3-dev (test/spinel/sqlite.rb) and, for the Node.js tests, node;
1091
- # runs test/*.rb under CRuby and as Spinel native builds, and compares with .expected
1114
+ # runs test/*_test.rb (spinel_test-unit) under CRuby and as a Spinel native build,
1115
+ # plus test/spinel/*.rb as Spinel builds, test/cli/*.rb and test/js/*_test.mjs
1092
1116
  bundle exec rake test
1093
1117
 
1094
- # CRuby only
1118
+ # the unit tests under CRuby only
1095
1119
  bundle exec rake test:cruby
1096
1120
  ```
1097
1121
 
data/js/cgi.js CHANGED
@@ -95,21 +95,13 @@ function decodePath(path) {
95
95
  }).join(""));
96
96
  }
97
97
 
98
- // The request.cf fields passed on, as non-standard CF_* variables.
99
- const CF_FIELDS = {
100
- CF_COUNTRY: "country", CF_CONTINENT: "continent", CF_REGION: "region", CF_REGION_CODE: "regionCode",
101
- CF_CITY: "city", CF_TIMEZONE: "timezone", CF_COLO: "colo", CF_ASN: "asn",
102
- };
103
- // Precise location (latitude, longitude, postal code) is deliberately not passed;
104
- // an app that needs it can read request.cf in the worker.
105
-
106
98
  // The CGI environment for a request: an object of strings (no undefined or
107
99
  // null; WASI env needs strings). `body` is the request body as a Uint8Array.
108
- // - REMOTE_ADDR comes from CF-Connecting-IP; it is "" when that is missing
109
- // (local wrangler dev may not send it), so the variable is always set.
100
+ // - SERVER_PROTOCOL and REMOTE_ADDR default to "HTTP/1.1" and "", so they are
101
+ // always set; the platform can supply the real values in `platformEnv`.
110
102
  // - CONTENT_LENGTH is always set ("0" without a body); CONTENT_TYPE only
111
103
  // when the request has one.
112
- // - Header values go in as the JS strings workerd gives (a UTF-8 value from the
104
+ // - Header values go in as JS strings (a UTF-8 value from the
113
105
  // client arrives decoded; the WASI shim encodes env as UTF-8, so the app
114
106
  // sees the client's bytes). Content-Type and Content-Length are not HTTP_* (RFC 3875); every other
115
107
  // header is HTTP_<NAME> (upper case, "-" -> "_"; a name that already holds
@@ -118,15 +110,15 @@ const CF_FIELDS = {
118
110
  // the one that handles the request.
119
111
  // - The Proxy header is not passed: HTTP_PROXY is read by HTTP clients as
120
112
  // proxy config (httpoxy, CVE-2016-5385).
121
- // - CF_* are not CGI; they come from request.cf, which is absent locally
122
- // in some setups, and absent fields are skipped.
123
- export function cgiEnv(request, body) {
113
+ // - `platformEnv` holds the variables the platform adds for this request
114
+ // (SERVER_SOFTWARE, non-standard ones...). They go in last, so they replace
115
+ // any other value of the same name, and a null or undefined one is skipped.
116
+ export function cgiEnv(request, body, { platformEnv = {} } = {}) {
124
117
  const url = new URL(request.url);
125
118
  const https = url.protocol === "https:";
126
119
  const env = {
127
120
  GATEWAY_INTERFACE: "CGI/1.1",
128
- SERVER_SOFTWARE: "Cloudflare-Workers",
129
- SERVER_PROTOCOL: request.cf?.httpProtocol ? String(request.cf.httpProtocol) : "HTTP/1.1",
121
+ SERVER_PROTOCOL: "HTTP/1.1",
130
122
  SERVER_NAME: url.hostname,
131
123
  SERVER_PORT: url.port || (https ? "443" : "80"),
132
124
  REQUEST_SCHEME: https ? "https" : "http",
@@ -135,7 +127,7 @@ export function cgiEnv(request, body) {
135
127
  PATH_INFO: decodePath(url.pathname),
136
128
  QUERY_STRING: url.search.slice(1),
137
129
  REQUEST_URI: url.pathname + url.search,
138
- REMOTE_ADDR: request.headers.get("cf-connecting-ip") ?? "",
130
+ REMOTE_ADDR: "",
139
131
  CONTENT_LENGTH: String(body.length),
140
132
  };
141
133
  if (https) env.HTTPS = "on";
@@ -149,9 +141,8 @@ export function cgiEnv(request, body) {
149
141
  if (name.includes("_")) return;
150
142
  env["HTTP_" + name.toUpperCase().replace(/-/g, "_")] = value;
151
143
  });
152
- for (const [key, field] of Object.entries(CF_FIELDS)) {
153
- const v = request.cf?.[field];
154
- if (v != null && v !== "") env[key] = String(v);
144
+ for (const [name, value] of Object.entries(platformEnv)) {
145
+ if (value != null) env[name] = String(value);
155
146
  }
156
147
  return env;
157
148
  }
data/js/cloudflare.js CHANGED
@@ -201,6 +201,30 @@ export function logFieldsOf(request) {
201
201
  return ray ? { ray } : {};
202
202
  }
203
203
 
204
+ // The request.cf fields passed on, as non-standard CF_* variables. Precise
205
+ // location (latitude, longitude, postal code) is deliberately not passed; an
206
+ // app that needs it can read request.cf in the worker.
207
+ const CF_FIELDS = {
208
+ CF_COUNTRY: "country", CF_CONTINENT: "continent", CF_REGION: "region", CF_REGION_CODE: "regionCode",
209
+ CF_CITY: "city", CF_TIMEZONE: "timezone", CF_COLO: "colo", CF_ASN: "asn",
210
+ };
211
+
212
+ // The CGI variables this platform adds to a request. request.cf, and so the CF_*
213
+ // variables, may be absent in local development, and absent fields are skipped.
214
+ // REMOTE_ADDR is "" without CF-Connecting-IP, so it is always set.
215
+ export function platformEnvOf(request) {
216
+ const env = {
217
+ SERVER_SOFTWARE: "Cloudflare-Workers",
218
+ REMOTE_ADDR: request.headers.get("cf-connecting-ip") ?? "",
219
+ };
220
+ if (request.cf?.httpProtocol) env.SERVER_PROTOCOL = String(request.cf.httpProtocol);
221
+ for (const [name, field] of Object.entries(CF_FIELDS)) {
222
+ const value = request.cf?.[field];
223
+ if (value != null && value !== "") env[name] = String(value);
224
+ }
225
+ return env;
226
+ }
227
+
204
228
  // appModule: the WebAssembly.Module that `import app from "...app.wasm"` gives.
205
229
  export function createWorker(appModule) {
206
230
  return {
@@ -209,6 +233,7 @@ export function createWorker(appModule) {
209
233
  const response = await runCgi(appModule, request, {
210
234
  extraEnv: { EDGEWAY_ENV: config.env, ...varsOf(env) },
211
235
  logFields: logFieldsOf(request),
236
+ platformEnv: platformEnvOf(request),
212
237
  // Every module is always supplied, whether or not the app uses it:
213
238
  // the wasm instance needs every import it declares.
214
239
  imports: (getInstance) => ({
data/js/jwt.js CHANGED
@@ -207,7 +207,7 @@ export function jwtVerifier(env, { fetch = globalThis.fetch, now = Date.now, cac
207
207
  };
208
208
  }
209
209
 
210
- // The wasm import module "edgeway_jwt" (lib/edgeway/service/cloudflare/jwt.rb):
210
+ // The wasm import module "edgeway_jwt" (lib/edgeway/service/jwt.rb):
211
211
  // verify(token) -> {"header": {...}, "payload": {...}}
212
212
  // | {"error": "message"} the token is bad
213
213
  // | {"error": "message", "unavailable": true} it can't be checked
data/js/runtime.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Runs a Spinel-built app.wasm once per request, CGI style: the request
2
2
  // becomes CGI environment variables and STDIN, the program's stdout becomes
3
- // the Response. Nothing here is specific to a platform; cloudflare.js
4
- // supplies the imports a platform adds (D1, ...) and the environment.
3
+ // the Response. Nothing here is specific to a platform: the
4
+ // caller supplies the imports a platform adds and its part of the environment.
5
5
  import { WASI } from "./wasi-shim.js";
6
6
  import { cgiEnv, cgiResponse, concatBytes } from "./cgi.js";
7
7
 
@@ -90,11 +90,13 @@ function internalError() {
90
90
  // Response.
91
91
  // extraEnv: more CGI environment variables (strings), such as the app's
92
92
  // settings and EDGEWAY_ENV.
93
+ // platformEnv: CGI variables the platform supplies for this request,
94
+ // such as SERVER_SOFTWARE and REMOTE_ADDR; they replace the defaults.
93
95
  // logFields: fields added to every structured log line the app writes.
94
96
  // imports: (getInstance) => { moduleName: { name: fn } }, the wasm import
95
97
  // modules besides WASI. A function wrapped in
96
98
  // WebAssembly.Suspending may await (JSPI).
97
- export async function runCgi(appModule, request, { extraEnv = {}, imports = () => ({}), logFields = {} } = {}) {
99
+ export async function runCgi(appModule, request, { extraEnv = {}, imports = () => ({}), logFields = {}, platformEnv = {} } = {}) {
98
100
  const body = request.method === "GET" || request.method === "HEAD"
99
101
  ? new Uint8Array(0)
100
102
  : await readBody(request);
@@ -104,7 +106,7 @@ export async function runCgi(appModule, request, { extraEnv = {}, imports = () =
104
106
  const stderr = lineSplitter((line) => logLine(line, logFields));
105
107
  const wasi = new WASI({
106
108
  args: ["app"],
107
- env: { ...extraEnv, ...cgiEnv(request, body), SPINEL_GC_SLAB_MB: SLAB_MB },
109
+ env: { ...extraEnv, ...cgiEnv(request, body, { platformEnv }), SPINEL_GC_SLAB_MB: SLAB_MB },
108
110
  stdin: body,
109
111
  onStdoutBytes: (b) => out.push(b),
110
112
  onStderr: (text) => stderr.push(text),
@@ -78,7 +78,7 @@ module Edgeway
78
78
  end
79
79
 
80
80
  def expired?(entry)
81
- entry["e"].is_a?(Integer) && Integer(entry["e"]) <= current_time
81
+ entry["e"].is_a?(Integer) && entry["e"] <= current_time
82
82
  end
83
83
 
84
84
  def envelope(value, seconds)
@@ -92,7 +92,7 @@ module Edgeway
92
92
  end
93
93
 
94
94
  def checked_expires_in(seconds)
95
- unless seconds.is_a?(Integer) && Integer(seconds) > 0 && Integer(seconds) <= MAX_EXPIRES_IN
95
+ unless seconds.is_a?(Integer) && seconds > 0 && seconds <= MAX_EXPIRES_IN
96
96
  raise ArgumentError, "expires_in must be a whole number of seconds, 1 to #{MAX_EXPIRES_IN}"
97
97
  end
98
98
 
@@ -8,31 +8,35 @@ module Edgeway
8
8
  @root = root
9
9
  end
10
10
 
11
- def require_lines(views: nil)
12
- lines = []
13
- app = File.join(@root, "app")
14
- dirs = Dir.exist?(app) ? Dir.children(app).sort.select { |d| File.directory?(File.join(app, d)) } : []
15
- dirs.each do |d|
16
- next if %w[models controllers].include?(d)
17
-
18
- lines.concat(requires("app/#{d}"))
19
- end
20
- lines.concat(requires("app/models"))
21
- lines.concat(requires("app/controllers"))
11
+ def require_lines(views: nil, dispatch: nil)
12
+ lines = files.map { |file| require_line(file) }
22
13
  lines << "require_relative #{views.dump}" if views
14
+ lines << "require_relative #{dispatch.dump}" if dispatch
23
15
  lines << 'require_relative "../config/routes"'
24
16
  lines
25
17
  end
26
18
 
27
- # require_relative lines for the .rb files under dir, application_*.rb first.
28
- def requires(dir)
29
- full = File.join(@root, dir)
19
+ def files
20
+ app = File.join(@root, "app")
21
+ directories = Dir.exist?(app) ? Dir.children(app).sort.select { |name| File.directory?(File.join(app, name)) } : []
22
+ generic = directories.reject { |name| %w[models controllers].include?(name) }
23
+ (generic.map { |name| "app/#{name}" } + ["app/models", "app/controllers"]).flat_map { |directory| files_in(directory) }
24
+ end
25
+
26
+ private
27
+
28
+ def files_in(directory)
29
+ full = File.join(@root, directory)
30
30
  return [] unless Dir.exist?(full)
31
31
 
32
- files = Dir.glob("**/*.rb", base: full).map { |f| File.join(dir, f) }
33
- first, rest = files.partition { |f| File.basename(f).start_with?("application_") }
34
- # String#dump quotes a path with spaces, quotes or "#{" safely.
35
- (first.sort + rest.sort).map { |f| "require_relative #{"../#{f.delete_suffix(".rb")}".dump}" }
32
+ paths = Dir.glob("**/*.rb", base: full).map { |path| File.join(directory, path) }
33
+ first, rest = paths.partition { |path| File.basename(path).start_with?("application_") }
34
+ first.sort + rest.sort
35
+ end
36
+
37
+ # String#dump quotes a path with spaces, quotes or "#{" safely.
38
+ def require_line(file)
39
+ "require_relative #{"../#{file.delete_suffix(".rb")}".dump}"
36
40
  end
37
41
  end
38
42
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edgeway
4
+ module CLI
5
+ class Build
6
+ module ClassSource
7
+ def self.wrap(class_name, body)
8
+ *modules, name = class_name.split("::")
9
+ depth = modules.size
10
+ lines = [""]
11
+ modules.each_with_index { |module_name, index| lines << "#{" " * index}module #{module_name}" }
12
+ lines << "#{" " * depth}class #{name}"
13
+ body.each { |line| lines << (line.empty? ? "" : "#{" " * (depth + 1)}#{line}") }
14
+ lines << "#{" " * depth}end"
15
+ depth.times { |index| lines << "#{" " * (depth - 1 - index)}end" }
16
+ lines
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "js_files"
4
+
5
+ module Edgeway
6
+ module CLI
7
+ class Build
8
+ class CloudflarePlatform
9
+ ADAPTERS = %w[d1 memory].freeze
10
+ STORES = %w[kv memory null].freeze
11
+
12
+ def name = "cloudflare"
13
+
14
+ def adapters = ADAPTERS
15
+
16
+ def stores = STORES
17
+
18
+ # Always, whatever the app uses: js/cloudflare.js supplies the
19
+ # edgeway_jwt, edgeway_fetch and edgeway_ratelimit imports to every app,
20
+ # and each is only called by an app that authenticates, calls out or
21
+ # rate limits.
22
+ def service_lines
23
+ [
24
+ 'require "edgeway/service/jwt"',
25
+ 'require "edgeway/service/fetch"',
26
+ 'require "edgeway/service/cloudflare/rate_limit"'
27
+ ]
28
+ end
29
+
30
+ def setup_lines
31
+ [
32
+ "Edgeway::Auth.verifier = Edgeway::JWTVerifier.new",
33
+ "Edgeway::HTTP.client = Edgeway::FetchTransport.new"
34
+ ]
35
+ end
36
+
37
+ def rate_limiter_lines
38
+ [
39
+ "Edgeway::Controller.rate_limiter = Edgeway::Cloudflare::RateLimiter.new(transport: Edgeway::Cloudflare::RateLimitImportTransport.new)"
40
+ ]
41
+ end
42
+
43
+ def adapter_expression(adapter, binding_name)
44
+ case adapter
45
+ when "d1" then "Edgeway::Cloudflare::D1Adapter.new(binding: #{binding_name.inspect})"
46
+ else "Edgeway::DB::MemoryAdapter.new"
47
+ end
48
+ end
49
+
50
+ def js_paths(tests: false) = JsFiles.paths(name, tests)
51
+ end
52
+
53
+ PlatformConfig::PLATFORMS["cloudflare"] = CloudflarePlatform
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open3"
5
+ require "tempfile"
6
+ require "rbconfig"
7
+ require_relative "class_source"
8
+
9
+ module Edgeway
10
+ module CLI
11
+ class Build
12
+ class DispatchCompiler
13
+ PROBE = File.join(__dir__, "dispatch_probe.rb")
14
+
15
+ def initialize(root, library_lines)
16
+ @root = root
17
+ @library_lines = library_lines
18
+ end
19
+
20
+ def compile
21
+ controllers = routed_actions
22
+ return nil if controllers.empty?
23
+
24
+ lines = ["# Generated by `edgeway build` from config/routes.rb; do not edit."]
25
+ controllers.each { |name, actions| lines.concat(ClassSource.wrap(name, run_action_source(actions))) }
26
+ lines.concat(instantiate_source(controllers.keys))
27
+ lines.join("\n") + "\n"
28
+ end
29
+
30
+ private
31
+
32
+ def routed_actions
33
+ result = probe
34
+ problems = result["problems"]
35
+ raise Error, "config/routes.rb:\n #{problems.join("\n ")}" if problems
36
+
37
+ result.fetch("controllers")
38
+ end
39
+
40
+ def probe
41
+ Tempfile.create("dispatch") do |result|
42
+ _output, error, status = Open3.capture3(*probe_command(result.path))
43
+ raise Error, "could not load the app to read its routes:\n#{error}" unless status.success?
44
+
45
+ JSON.parse(File.read(result.path))
46
+ end
47
+ rescue JSON::ParserError => parse_error
48
+ raise Error, "could not read the routes of the app (#{parse_error.message})"
49
+ end
50
+
51
+ def probe_command(result_path)
52
+ [RbConfig.ruby, "-I", File.join(CLI.gem_dir, "lib"), PROBE, result_path, @root,
53
+ @library_lines.join("\n"), *AppFiles.new(@root).files]
54
+ end
55
+
56
+ def run_action_source(actions)
57
+ lines = ["def _run_action(action)", " case action"]
58
+ actions.each { |action| lines << " when #{action.dump} then self.#{action}" }
59
+ lines << " end" << "end"
60
+ end
61
+
62
+ def instantiate_source(names)
63
+ lines = ["", "module Edgeway", " module Controller", " module Dispatcher",
64
+ " def self.instantiate(name, request)", " case name"]
65
+ names.each { |name| lines << " when #{name.dump} then #{name}.new(request)" }
66
+ lines << " end" << " end" << " end" << " end" << "end"
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "edgeway"
5
+
6
+ module Edgeway
7
+ module DispatchProbe
8
+ ACTION_NAME = /\A[a-z_][a-zA-Z0-9_]*[!?]?\z/
9
+
10
+ def self.route_problem(route)
11
+ where = "#{route.verb} #{route.pattern} (#{route.controller_path}##{route.action})"
12
+ name = route.controller_class_name
13
+ return "#{where}: #{name} is not defined (app/controllers/#{route.controller_path}_controller.rb)" unless Object.const_defined?(name)
14
+
15
+ controller_class = Object.const_get(name)
16
+ return "#{where}: #{name} is a #{controller_class.class.name.downcase}, not a class" unless controller_class.is_a?(Class)
17
+ return "#{where}: #{name} is not an Edgeway::Controller::Base" unless controller_class < Edgeway::Controller::Base
18
+ return "#{where}: #{route.action.inspect} is not a valid action name" unless ACTION_NAME.match?(route.action)
19
+ return nil if controller_class.allocate.action?(route.action)
20
+
21
+ "#{where}: #{name} has no action #{route.action} (a public method that is not one of Edgeway::Controller::Base's)"
22
+ end
23
+
24
+ def self.actions_by_controller(routes)
25
+ routes.each_with_object({}) do |route, grouped|
26
+ actions = (grouped[route.controller_class_name] ||= [])
27
+ actions << route.action unless actions.include?(route.action)
28
+ end
29
+ end
30
+
31
+ def self.load_library(lines)
32
+ lines.each_line do |line|
33
+ TOPLEVEL_BINDING.eval(line)
34
+ rescue NoMethodError => error
35
+ raise unless error.name.to_s.start_with?("ffi_")
36
+ end
37
+ end
38
+
39
+ def self.run(result_path, root, library_lines, files)
40
+ load_library(library_lines)
41
+ files.each { |file| require File.join(root, file) }
42
+ load File.join(root, "config/routes.rb")
43
+ routes = Edgeway.application.routes.list
44
+ problems = routes.map { |route| route_problem(route) }.compact
45
+ result = problems.empty? ? { "controllers" => actions_by_controller(routes) } : { "problems" => problems }
46
+ File.write(result_path, JSON.generate(result))
47
+ end
48
+ end
49
+ end
50
+
51
+ result_path, root, library_lines, *files = ARGV
52
+ Edgeway::DispatchProbe.run(result_path, root, library_lines, files)