skyward-api 0.3.0 → 0.4.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/LICENSE +21 -0
- data/README.md +60 -56
- data/lib/skyward/api/assembly/boot.rb +239 -91
- data/lib/skyward/api/assembly/builder.rb +2 -2
- data/lib/skyward/api/assembly/dispatcher.rb +12 -12
- data/lib/skyward/api/assembly/static.rb +36 -36
- data/lib/skyward/api/cli/app_file.rb +2 -2
- data/lib/skyward/api/cli/check.rb +9 -9
- data/lib/skyward/api/cli/routes.rb +2 -2
- data/lib/skyward/api/cli/serve/reloader.rb +6 -6
- data/lib/skyward/api/cli/serve.rb +15 -15
- data/lib/skyward/api/cli/serve.ru +2 -2
- data/lib/skyward/api/commands.rb +10 -10
- data/lib/skyward/api/endpoints/declarations.rb +7 -3
- data/lib/skyward/api/endpoints/route.rb +3 -1
- data/lib/skyward/api/inputs/coercer.rb +53 -20
- data/lib/skyward/api/inputs.rb +9 -9
- data/lib/skyward/api/middleware/request_id.rb +3 -3
- data/lib/skyward/api/middleware/request_log.rb +5 -5
- data/lib/skyward/api/middleware/rescue.rb +7 -7
- data/lib/skyward/api/openapi/docs.rb +5 -5
- data/lib/skyward/api/openapi.rb +52 -50
- data/lib/skyward/api/request.rb +8 -8
- data/lib/skyward/api/routing/percent_encoding.rb +1 -1
- data/lib/skyward/api/routing/router.rb +68 -27
- data/lib/skyward/api/serializer/writers.rb +27 -16
- data/lib/skyward/api/serializer.rb +39 -21
- data/lib/skyward/api/test/client.rb +25 -25
- data/lib/skyward/api/version.rb +1 -1
- data/lib/skyward/api.rb +15 -14
- data/sig/generated/skyward/api/assembly/boot.rbs +99 -29
- data/sig/generated/skyward/api/endpoints/route.rbs +8 -4
- data/sig/generated/skyward/api/inputs/coercer.rbs +20 -2
- data/sig/generated/skyward/api/routing/router.rbs +52 -9
- data/sig/generated/skyward/api/serializer/writers.rbs +6 -0
- data/sig/generated/skyward/api/serializer.rbs +13 -1
- metadata +19 -4
- /data/sig/{manual/zeitwerk/loader.rbs → _private/zeitwerk.rbs} +0 -0
|
@@ -17,10 +17,10 @@ class Skyward::API::Middleware::RequestId
|
|
|
17
17
|
# @rbs env: Hash[String, untyped]
|
|
18
18
|
# @rbs return: Skyward::API::rack_response
|
|
19
19
|
def call(env)
|
|
20
|
-
incoming = env[
|
|
20
|
+
incoming = env['HTTP_X_REQUEST_ID']
|
|
21
21
|
# Matched as binary, because Regexp#match? raises on a String that isn't valid UTF-8.
|
|
22
22
|
id = VALID.match?(incoming&.b) ? incoming : @generate.call
|
|
23
|
-
status, headers, body = @app.call(env.merge(
|
|
24
|
-
[status, headers.merge(
|
|
23
|
+
status, headers, body = @app.call(env.merge('skyward.request_id' => id))
|
|
24
|
+
[status, headers.merge('x-request-id' => id), body]
|
|
25
25
|
end
|
|
26
26
|
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
# Time#iso8601 is core only from Ruby 3.4.
|
|
5
|
-
require
|
|
5
|
+
require 'time'
|
|
6
6
|
|
|
7
7
|
class Skyward::API::Middleware::RequestLog
|
|
8
8
|
# @rbs @app: Skyward::API::_RackApp
|
|
@@ -24,9 +24,9 @@ class Skyward::API::Middleware::RequestLog
|
|
|
24
24
|
def call(env)
|
|
25
25
|
time = @now.call.getutc.iso8601(3)
|
|
26
26
|
# Read before the app runs, because middleware can rewrite the env in place.
|
|
27
|
-
method = env[
|
|
27
|
+
method = env['REQUEST_METHOD']
|
|
28
28
|
# Rack's path is raw bytes from the client, and JSON.generate raises on invalid UTF-8.
|
|
29
|
-
path = String.new("#{env[
|
|
29
|
+
path = String.new("#{env['SCRIPT_NAME']}#{env['PATH_INFO']}", encoding: Encoding::UTF_8).scrub
|
|
30
30
|
started = @clock_ms.call
|
|
31
31
|
response = @app.call(env)
|
|
32
32
|
@logger.info(
|
|
@@ -37,7 +37,7 @@ class Skyward::API::Middleware::RequestLog
|
|
|
37
37
|
path:,
|
|
38
38
|
status: response.first,
|
|
39
39
|
duration_ms: (@clock_ms.call - started).round(2),
|
|
40
|
-
request_id: env[
|
|
40
|
+
request_id: env['skyward.request_id']
|
|
41
41
|
}
|
|
42
42
|
)
|
|
43
43
|
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
|
|
5
5
|
class Skyward::API::Middleware::Rescue
|
|
6
6
|
# @rbs @app: Skyward::API::_RackApp
|
|
@@ -22,14 +22,14 @@ class Skyward::API::Middleware::Rescue
|
|
|
22
22
|
# @rbs return: Skyward::API::rack_response
|
|
23
23
|
def call(env)
|
|
24
24
|
# Read before the app runs, because middleware can rewrite the env in place.
|
|
25
|
-
verb = env[
|
|
25
|
+
verb = env['REQUEST_METHOD']
|
|
26
26
|
@app.call(env)
|
|
27
27
|
rescue StandardError => e
|
|
28
|
-
request_id = env[
|
|
28
|
+
request_id = env['skyward.request_id']
|
|
29
29
|
log(e, request_id)
|
|
30
30
|
report(e, env, request_id)
|
|
31
|
-
body = verb ==
|
|
32
|
-
[500, {
|
|
31
|
+
body = verb == 'HEAD' ? [] : [JSON.generate(problem(e, request_id))] #: Array[String]
|
|
32
|
+
[500, { 'content-type' => 'application/problem+json' }, body]
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
private
|
|
@@ -41,7 +41,7 @@ class Skyward::API::Middleware::Rescue
|
|
|
41
41
|
@logger&.info(
|
|
42
42
|
JSON.generate(
|
|
43
43
|
{
|
|
44
|
-
level:
|
|
44
|
+
level: 'error',
|
|
45
45
|
request_id:,
|
|
46
46
|
class: exception.class.name,
|
|
47
47
|
message: message(exception),
|
|
@@ -65,7 +65,7 @@ class Skyward::API::Middleware::Rescue
|
|
|
65
65
|
# @rbs request_id: String?
|
|
66
66
|
# @rbs return: Hash[Symbol, untyped]
|
|
67
67
|
def problem(exception, request_id)
|
|
68
|
-
problem = { type:
|
|
68
|
+
problem = { type: 'about:blank', title: 'Internal Server Error', status: 500, request_id: }
|
|
69
69
|
# An exception's message can carry data, so only development shows it.
|
|
70
70
|
return problem unless @development
|
|
71
71
|
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'cgi/escape'
|
|
4
4
|
|
|
5
5
|
module Skyward::API::OpenAPI::Docs
|
|
6
6
|
extend self
|
|
7
7
|
|
|
8
|
-
PATH =
|
|
8
|
+
PATH = '/docs' #: String
|
|
9
9
|
|
|
10
|
-
HTML_TYPE =
|
|
10
|
+
HTML_TYPE = 'text/html; charset=utf-8' #: String
|
|
11
11
|
|
|
12
|
-
SCALAR_URL =
|
|
12
|
+
SCALAR_URL = 'https://cdn.jsdelivr.net/npm/@scalar/api-reference@1.75.1/dist/browser/standalone.js' #: String
|
|
13
13
|
|
|
14
14
|
# Recompute from the file whenever SCALAR_URL's version changes.
|
|
15
|
-
SCALAR_INTEGRITY =
|
|
15
|
+
SCALAR_INTEGRITY = 'sha384-G+2Q2i9l3PPfmYkk0b4BAIUrs7xb/DNaTL1wCYYab4c9dIKlCyiqzC0Qx3eq9Wf6' #: String
|
|
16
16
|
|
|
17
17
|
# @rbs title: String
|
|
18
18
|
# @rbs scalar_url: String
|
data/lib/skyward/api/openapi.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
|
|
5
5
|
module Skyward::API::OpenAPI
|
|
6
6
|
extend self
|
|
@@ -15,46 +15,47 @@ module Skyward::API::OpenAPI
|
|
|
15
15
|
:schema #: schema?
|
|
16
16
|
)
|
|
17
17
|
|
|
18
|
-
PATH =
|
|
18
|
+
PATH = '/openapi.json' #: String
|
|
19
19
|
|
|
20
20
|
SERVED = {
|
|
21
|
-
PATH =>
|
|
22
|
-
Docs::PATH =>
|
|
21
|
+
PATH => 'the OpenAPI document',
|
|
22
|
+
Docs::PATH => 'the API reference'
|
|
23
23
|
}.freeze #: Hash[String, String]
|
|
24
24
|
|
|
25
25
|
# OpenAPI's pattern for the name of a component.
|
|
26
26
|
NAME = /\A[A-Za-z0-9._-]+\z/ #: Regexp
|
|
27
27
|
|
|
28
|
-
JSON_TYPE =
|
|
28
|
+
JSON_TYPE = 'application/json' #: String
|
|
29
29
|
|
|
30
|
-
PROBLEM_TYPE =
|
|
30
|
+
PROBLEM_TYPE = 'application/problem+json' #: String
|
|
31
31
|
|
|
32
32
|
SCALARS = {
|
|
33
|
-
integer: { type:
|
|
34
|
-
float: { type:
|
|
35
|
-
string: { type:
|
|
36
|
-
bool: { type:
|
|
37
|
-
date: { type:
|
|
38
|
-
time: { type:
|
|
33
|
+
integer: { type: 'integer' },
|
|
34
|
+
float: { type: 'number' },
|
|
35
|
+
string: { type: 'string' },
|
|
36
|
+
bool: { type: 'boolean' },
|
|
37
|
+
date: { type: 'string', format: 'date' },
|
|
38
|
+
time: { type: 'string', format: 'date-time' },
|
|
39
|
+
decimal: { type: 'string', format: 'decimal' },
|
|
39
40
|
untyped: {} #: schema
|
|
40
41
|
}.freeze #: Hash[Skyward::Core::Types::scalar, schema]
|
|
41
42
|
|
|
42
|
-
NULL = { type:
|
|
43
|
+
NULL = { type: 'null' }.freeze #: schema
|
|
43
44
|
|
|
44
|
-
PROBLEM = {
|
|
45
|
+
PROBLEM = { '$ref': '#/components/schemas/Problem' }.freeze #: schema
|
|
45
46
|
|
|
46
47
|
PROBLEM_SCHEMA = {
|
|
47
|
-
type:
|
|
48
|
+
type: 'object',
|
|
48
49
|
properties: {
|
|
49
|
-
type: { type:
|
|
50
|
-
title: { type:
|
|
51
|
-
status: { type:
|
|
52
|
-
detail: { type:
|
|
50
|
+
type: { type: 'string' },
|
|
51
|
+
title: { type: 'string' },
|
|
52
|
+
status: { type: 'integer' },
|
|
53
|
+
detail: { type: 'string' },
|
|
53
54
|
errors: {
|
|
54
|
-
type:
|
|
55
|
+
type: 'array',
|
|
55
56
|
items: {
|
|
56
|
-
type:
|
|
57
|
-
properties: { at: { type:
|
|
57
|
+
type: 'object',
|
|
58
|
+
properties: { at: { type: 'string' }, message: { type: 'string' } },
|
|
58
59
|
required: %w[at message]
|
|
59
60
|
}
|
|
60
61
|
}
|
|
@@ -62,30 +63,30 @@ module Skyward::API::OpenAPI
|
|
|
62
63
|
required: %w[type title status]
|
|
63
64
|
}.freeze #: schema
|
|
64
65
|
|
|
65
|
-
NO_CONTENT = Answer.new(status:
|
|
66
|
+
NO_CONTENT = Answer.new(status: '204', media_type: nil, schema: nil) #: Answer
|
|
66
67
|
|
|
67
68
|
# A redirect's status is chosen when it's made, so it's documented as the range.
|
|
68
|
-
REDIRECT = Answer.new(status:
|
|
69
|
+
REDIRECT = Answer.new(status: '3XX', media_type: nil, schema: nil) #: Answer
|
|
69
70
|
|
|
70
|
-
RACK = Answer.new(status:
|
|
71
|
+
RACK = Answer.new(status: 'default', media_type: nil, schema: nil) #: Answer
|
|
71
72
|
|
|
72
|
-
INVALID = Answer.new(status:
|
|
73
|
+
INVALID = Answer.new(status: '422', media_type: PROBLEM_TYPE, schema: PROBLEM) #: Answer
|
|
73
74
|
|
|
74
75
|
REFUSED = [
|
|
75
|
-
Answer.new(status:
|
|
76
|
-
Answer.new(status:
|
|
76
|
+
Answer.new(status: '413', media_type: PROBLEM_TYPE, schema: PROBLEM),
|
|
77
|
+
Answer.new(status: '415', media_type: PROBLEM_TYPE, schema: PROBLEM)
|
|
77
78
|
].freeze #: Array[Answer]
|
|
78
79
|
|
|
79
|
-
LOCATION = { location: { schema: { type:
|
|
80
|
+
LOCATION = { location: { schema: { type: 'string' } } }.freeze #: Hash[Symbol, untyped]
|
|
80
81
|
|
|
81
82
|
DESCRIPTIONS = {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
'200' => 'OK',
|
|
84
|
+
'201' => 'Created',
|
|
85
|
+
'204' => 'No Content',
|
|
86
|
+
'3XX' => 'Redirect',
|
|
86
87
|
**Skyward::API::Serializer::PROBLEMS.values.to_h { |problem| [problem.status.to_s, problem.title] },
|
|
87
88
|
**Skyward::API::Assembly::Dispatcher::INPUT_TITLES.transform_keys(&:to_s),
|
|
88
|
-
|
|
89
|
+
'default' => "A Rack response, whose status and content are known only when it's sent"
|
|
89
90
|
}.freeze #: Hash[String, String]
|
|
90
91
|
|
|
91
92
|
# Takes the document unbuilt, so a route or static file at /openapi.json or /docs is refused first.
|
|
@@ -182,7 +183,7 @@ module Skyward::API::OpenAPI
|
|
|
182
183
|
first = same.fetch(0)
|
|
183
184
|
second = same.fetch(1)
|
|
184
185
|
"#{second.label}\n Its OpenAPI operation ID, #{id}, is #{first.label}'s too, " \
|
|
185
|
-
|
|
186
|
+
'but each operation needs its own. With docs: true, route each method once, and mount each class once.'
|
|
186
187
|
end
|
|
187
188
|
|
|
188
189
|
# @rbs types: Array[Skyward::Core::Types::Named]
|
|
@@ -193,16 +194,16 @@ module Skyward::API::OpenAPI
|
|
|
193
194
|
invalid, name = chosen.find { |_, named| !NAME.match?(named) }
|
|
194
195
|
if invalid
|
|
195
196
|
return "#{invalid}'s schema name, #{name.inspect}, isn't one OpenAPI allows: use only letters, digits, " \
|
|
196
|
-
|
|
197
|
+
'., - and _. Name it with Skyward.app(schema_names:).'
|
|
197
198
|
end
|
|
198
199
|
|
|
199
|
-
taken = [[
|
|
200
|
+
taken = [['Problem', "Skyward API's problem+json"], *chosen.map { |klass, named| [named, klass.to_s] }]
|
|
200
201
|
same = taken.group_by(&:first).values.find { |shared| shared.size > 1 }
|
|
201
202
|
return chosen unless same
|
|
202
203
|
|
|
203
204
|
(name, first), (_, second) = same.fetch(0), same.fetch(1)
|
|
204
205
|
"#{first} and #{second} both have the schema name #{name}, but each schema needs its own. " \
|
|
205
|
-
|
|
206
|
+
'Name one of them with Skyward.app(schema_names:).'
|
|
206
207
|
end
|
|
207
208
|
|
|
208
209
|
# @rbs endpoints: Array[Skyward::API::Routing::Router::Endpoint]
|
|
@@ -212,7 +213,7 @@ module Skyward::API::OpenAPI
|
|
|
212
213
|
# @rbs return: schema
|
|
213
214
|
def assembled(endpoints, types, info, names)
|
|
214
215
|
{
|
|
215
|
-
openapi:
|
|
216
|
+
openapi: '3.1.0',
|
|
216
217
|
info:,
|
|
217
218
|
tags: endpoints.uniq { |endpoint| endpoint.api.class }.map do |endpoint|
|
|
218
219
|
{ name: tag(endpoint), description: endpoint.api.class.class_description }.compact
|
|
@@ -287,10 +288,10 @@ module Skyward::API::OpenAPI
|
|
|
287
288
|
def answer(member, names)
|
|
288
289
|
case member
|
|
289
290
|
when Skyward::API::Routing::Router::Created
|
|
290
|
-
Answer.new(status:
|
|
291
|
+
Answer.new(status: '201', media_type: JSON_TYPE, schema: schema(member.type, :response, names))
|
|
291
292
|
when Skyward::API::Routing::Router::Response then response_value(member.klass)
|
|
292
293
|
when Skyward::API::Routing::Router::Rack then RACK
|
|
293
|
-
else Answer.new(status:
|
|
294
|
+
else Answer.new(status: '200', media_type: JSON_TYPE, schema: schema(member, :response, names))
|
|
294
295
|
end
|
|
295
296
|
end
|
|
296
297
|
|
|
@@ -328,7 +329,7 @@ module Skyward::API::OpenAPI
|
|
|
328
329
|
requested = endpoints.flat_map { |endpoint| described(endpoint).flat_map { |keyword| named(keyword.type) } }
|
|
329
330
|
types.to_h do |type|
|
|
330
331
|
[names.fetch(type.klass), object(type.fields, requested.include?(type) ? :request : :response, names)]
|
|
331
|
-
end.merge(
|
|
332
|
+
end.merge('Problem' => PROBLEM_SCHEMA).sort_by(&:first).to_h
|
|
332
333
|
end
|
|
333
334
|
|
|
334
335
|
# @rbs type: Skyward::Core::Types::t
|
|
@@ -339,12 +340,13 @@ module Skyward::API::OpenAPI
|
|
|
339
340
|
case type
|
|
340
341
|
when Skyward::Core::Types::Scalar then SCALARS.fetch(type.name)
|
|
341
342
|
when Skyward::Core::Types::Literal then type.value.nil? ? NULL : { const: type.value }
|
|
342
|
-
when Skyward::Core::Types::Enum then { type:
|
|
343
|
+
when Skyward::Core::Types::Enum then { type: 'string', enum: type.symbols.map(&:name) }
|
|
343
344
|
when Skyward::Core::Types::Nullable then nullable(schema(type.type, form, names))
|
|
344
|
-
when Skyward::Core::Types::ArrayOf then { type:
|
|
345
|
-
when Skyward::Core::Types::HashOf then { type:
|
|
345
|
+
when Skyward::Core::Types::ArrayOf then { type: 'array', items: schema(type.type, form, names) }
|
|
346
|
+
when Skyward::Core::Types::HashOf then { type: 'object', additionalProperties: schema(type.type, form, names) }
|
|
346
347
|
when Skyward::Core::Types::Record then object(type.fields, form, names)
|
|
347
|
-
when Skyward::Core::Types::Named then {
|
|
348
|
+
when Skyward::Core::Types::Named then { '$ref': "#/components/schemas/#{names.fetch(type.klass)}" }
|
|
349
|
+
when Skyward::Core::Types::Refined then schema(type.type, form, names)
|
|
348
350
|
else { oneOf: type.types.map { |member| schema(member, form, names) } }
|
|
349
351
|
end
|
|
350
352
|
end
|
|
@@ -353,7 +355,7 @@ module Skyward::API::OpenAPI
|
|
|
353
355
|
# @rbs return: schema
|
|
354
356
|
def nullable(schema)
|
|
355
357
|
type = schema[:type]
|
|
356
|
-
type.is_a?(String) && !schema.key?(:enum) ? schema.merge(type: [type,
|
|
358
|
+
type.is_a?(String) && !schema.key?(:enum) ? schema.merge(type: [type, 'null']) : { anyOf: [schema, NULL] }
|
|
357
359
|
end
|
|
358
360
|
|
|
359
361
|
# @rbs fields: Array[Skyward::Core::Types::Field]
|
|
@@ -362,7 +364,7 @@ module Skyward::API::OpenAPI
|
|
|
362
364
|
# @rbs return: schema
|
|
363
365
|
def object(fields, form, names)
|
|
364
366
|
{
|
|
365
|
-
type:
|
|
367
|
+
type: 'object',
|
|
366
368
|
properties: fields.to_h { |field| [field.name, schema(field.type, form, names)] },
|
|
367
369
|
required: fields.filter_map { |field| field.name if required?(field.required, field.type, form) }
|
|
368
370
|
}
|
|
@@ -377,7 +379,7 @@ module Skyward::API::OpenAPI
|
|
|
377
379
|
|
|
378
380
|
# @rbs pattern: Array[String | Symbol]
|
|
379
381
|
# @rbs return: String
|
|
380
|
-
def path(pattern) = pattern.map { |part| part.is_a?(Symbol) ? "{#{part}}" : part }.join(
|
|
382
|
+
def path(pattern) = pattern.map { |part| part.is_a?(Symbol) ? "{#{part}}" : part }.join('/')
|
|
381
383
|
|
|
382
384
|
# @rbs endpoint: Skyward::API::Routing::Router::Endpoint
|
|
383
385
|
# @rbs return: String
|
|
@@ -389,5 +391,5 @@ module Skyward::API::OpenAPI
|
|
|
389
391
|
|
|
390
392
|
# @rbs name: String
|
|
391
393
|
# @rbs return: String
|
|
392
|
-
def dotted(name) = name.gsub(
|
|
394
|
+
def dotted(name) = name.gsub('::', '.')
|
|
393
395
|
end
|
data/lib/skyward/api/request.rb
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
class Skyward::API::Request
|
|
4
4
|
# Rack passes these two headers without the HTTP_ prefix the others get.
|
|
5
|
-
UNPREFIXED = {
|
|
5
|
+
UNPREFIXED = { 'CONTENT_TYPE' => 'content-type', 'CONTENT_LENGTH' => 'content-length' }.freeze #: Hash[String, String]
|
|
6
6
|
|
|
7
|
-
OCTET =
|
|
7
|
+
OCTET = '(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])' #: String
|
|
8
8
|
|
|
9
9
|
# Rack's trusted proxies: loopback, the private ranges and Unix sockets.
|
|
10
10
|
TRUSTED_PROXY = Regexp.union(
|
|
@@ -66,9 +66,9 @@ class Skyward::API::Request
|
|
|
66
66
|
def initialize(env)
|
|
67
67
|
@env = env
|
|
68
68
|
@headers = read_headers(env)
|
|
69
|
-
@cookies = read_cookies(env[
|
|
70
|
-
@ip = client_ip(addresses(env[
|
|
71
|
-
@request_id = env.fetch(
|
|
69
|
+
@cookies = read_cookies(env['HTTP_COOKIE'])
|
|
70
|
+
@ip = client_ip(addresses(env['REMOTE_ADDR']), addresses(env['HTTP_X_FORWARDED_FOR']).map { |entry| bare(entry) })
|
|
71
|
+
@request_id = env.fetch('skyward.request_id')
|
|
72
72
|
freeze
|
|
73
73
|
end
|
|
74
74
|
|
|
@@ -78,7 +78,7 @@ class Skyward::API::Request
|
|
|
78
78
|
# @rbs return: Hash[String, String]
|
|
79
79
|
def read_headers(env)
|
|
80
80
|
env.filter_map do |key, value|
|
|
81
|
-
name = UNPREFIXED[key] || (key.delete_prefix(
|
|
81
|
+
name = UNPREFIXED[key] || (key.delete_prefix('HTTP_').downcase.tr('_', '-') if key.start_with?('HTTP_'))
|
|
82
82
|
[name, value] if name
|
|
83
83
|
end.to_h.freeze
|
|
84
84
|
end
|
|
@@ -89,7 +89,7 @@ class Skyward::API::Request
|
|
|
89
89
|
# Split as bytes, because String#split raises on a String that isn't valid
|
|
90
90
|
# UTF-8, and a client can send one.
|
|
91
91
|
pairs = header.to_s.b.split(/; */).reject(&:empty?).map do |cookie|
|
|
92
|
-
name, value = cookie.split(
|
|
92
|
+
name, value = cookie.split('=', 2)
|
|
93
93
|
[String.new(name.to_s, encoding: Encoding::UTF_8), Skyward::API::Routing::PercentEncoding.decode(value.to_s)]
|
|
94
94
|
end
|
|
95
95
|
# The first of two cookies with one name wins, as in Rack.
|
|
@@ -104,7 +104,7 @@ class Skyward::API::Request
|
|
|
104
104
|
# @rbs entry: String
|
|
105
105
|
# @rbs return: String?
|
|
106
106
|
def bare(entry)
|
|
107
|
-
wrapped = !entry.start_with?(
|
|
107
|
+
wrapped = !entry.start_with?('[') && entry.count(':') > 1 ? "[#{entry}]" : entry
|
|
108
108
|
wrapped[AUTHORITY, :address]
|
|
109
109
|
end
|
|
110
110
|
|
|
@@ -10,6 +10,6 @@ module Skyward::API::Routing::PercentEncoding
|
|
|
10
10
|
# @rbs text: String
|
|
11
11
|
# @rbs return: String
|
|
12
12
|
def decode(text)
|
|
13
|
-
text.b.gsub(ESCAPE) { |escape| escape.delete_prefix(
|
|
13
|
+
text.b.gsub(ESCAPE) { |escape| escape.delete_prefix('%').hex.chr }.force_encoding(Encoding::UTF_8)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
@@ -54,62 +54,103 @@ class Skyward::API::Routing::Router
|
|
|
54
54
|
:serializer #: Skyward::API::Serializer
|
|
55
55
|
)
|
|
56
56
|
|
|
57
|
+
Target = Data.define(
|
|
58
|
+
:index, #: Integer
|
|
59
|
+
:endpoint, #: Endpoint
|
|
60
|
+
:params #: Array[[Symbol, Integer]]
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# First is the lowest index in or below the node, so a search can skip a node that can't beat what it has found.
|
|
64
|
+
Node = Data.define(
|
|
65
|
+
:literals, #: Hash[String, Node]
|
|
66
|
+
:param, #: Node?
|
|
67
|
+
:target, #: Target?
|
|
68
|
+
:first #: Integer
|
|
69
|
+
)
|
|
70
|
+
|
|
57
71
|
METHODS = %w[GET HEAD POST PUT PATCH DELETE OPTIONS].freeze #: Array[String]
|
|
58
72
|
|
|
59
|
-
# @rbs @
|
|
73
|
+
# @rbs @trees: Hash[String, Node]
|
|
60
74
|
|
|
61
75
|
# @rbs endpoints: Array[Endpoint]
|
|
62
76
|
# @rbs return: void
|
|
63
77
|
def initialize(endpoints)
|
|
64
|
-
|
|
78
|
+
targets = endpoints.each_with_index.map do |endpoint, index|
|
|
79
|
+
Target.new(index:, endpoint:, params: params_of(endpoint.pattern).freeze)
|
|
80
|
+
end
|
|
81
|
+
@trees = targets.group_by { |target| target.endpoint.verb }.transform_values { |group| build(group, 0) }.freeze
|
|
65
82
|
end
|
|
66
83
|
|
|
67
84
|
# @rbs verb: String
|
|
68
85
|
# @rbs path: String
|
|
69
86
|
# @rbs return: Match?
|
|
70
87
|
def match(verb, path)
|
|
71
|
-
segments = path.split(
|
|
72
|
-
|
|
73
|
-
|
|
88
|
+
segments = path.split('/', -1)
|
|
89
|
+
target = search(@trees.fetch(verb == 'HEAD' ? 'GET' : verb, nil), segments, 0, nil)
|
|
90
|
+
return unless target
|
|
91
|
+
|
|
92
|
+
endpoint = target.endpoint
|
|
93
|
+
params = target.params.to_h do |name, position|
|
|
94
|
+
[name, Skyward::API::Routing::PercentEncoding.decode(segments[position])]
|
|
95
|
+
end
|
|
96
|
+
Match.new(api: endpoint.api, to: endpoint.to, inputs: endpoint.inputs, params:, serializer: endpoint.serializer)
|
|
74
97
|
end
|
|
75
98
|
|
|
76
99
|
# @rbs path: String
|
|
77
100
|
# @rbs return: Array[String]
|
|
78
101
|
def allowed(path)
|
|
79
|
-
segments = path.split(
|
|
80
|
-
verbs = @
|
|
102
|
+
segments = path.split('/', -1)
|
|
103
|
+
verbs = @trees.filter_map { |verb, tree| verb if search(tree, segments, 0, nil) }
|
|
81
104
|
return [] if verbs.empty?
|
|
82
105
|
|
|
83
|
-
implied = verbs.include?(
|
|
106
|
+
implied = verbs.include?('GET') ? %w[HEAD OPTIONS] : %w[OPTIONS]
|
|
84
107
|
METHODS & (verbs + implied)
|
|
85
108
|
end
|
|
86
109
|
|
|
87
110
|
private
|
|
88
111
|
|
|
89
|
-
# @rbs
|
|
90
|
-
# @rbs
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return unless endpoint.verb == verb
|
|
95
|
-
|
|
96
|
-
params = params_for(endpoint.pattern, segments)
|
|
97
|
-
return unless params
|
|
112
|
+
# @rbs pattern: Array[String | Symbol]
|
|
113
|
+
# @rbs return: Array[[Symbol, Integer]]
|
|
114
|
+
def params_of(pattern)
|
|
115
|
+
pattern.each_with_index.filter_map { |part, position| [part, position] if part.is_a?(Symbol) }
|
|
116
|
+
end
|
|
98
117
|
|
|
99
|
-
|
|
118
|
+
# @rbs targets: Array[Target]
|
|
119
|
+
# @rbs depth: Integer
|
|
120
|
+
# @rbs return: Node
|
|
121
|
+
def build(targets, depth)
|
|
122
|
+
ending, deeper = targets.partition { |target| target.endpoint.pattern.length == depth }
|
|
123
|
+
params, literals = deeper.partition { |target| target.endpoint.pattern.fetch(depth).is_a?(Symbol) }
|
|
124
|
+
by_segment = literals.group_by { |target| target.endpoint.pattern.fetch(depth).to_s }
|
|
125
|
+
# Targets come in index order, so a node's first target is its earliest.
|
|
126
|
+
Node.new(
|
|
127
|
+
literals: by_segment.transform_values { |group| build(group, depth + 1) }.freeze,
|
|
128
|
+
param: params.empty? ? nil : build(params, depth + 1),
|
|
129
|
+
target: ending.first,
|
|
130
|
+
first: targets.fetch(0).index
|
|
131
|
+
)
|
|
100
132
|
end
|
|
101
133
|
|
|
102
|
-
# @rbs
|
|
134
|
+
# @rbs node: Node?
|
|
103
135
|
# @rbs segments: Array[String]
|
|
104
|
-
# @rbs
|
|
105
|
-
|
|
106
|
-
|
|
136
|
+
# @rbs depth: Integer
|
|
137
|
+
# @rbs best: Target?
|
|
138
|
+
# @rbs return: Target?
|
|
139
|
+
def search(node, segments, depth, best)
|
|
140
|
+
return best unless node && (best.nil? || node.first < best.index)
|
|
141
|
+
return earlier(node.target, best) if depth == segments.length
|
|
142
|
+
|
|
143
|
+
segment = segments[depth]
|
|
144
|
+
found = search(node.literals[segment], segments, depth + 1, best)
|
|
145
|
+
segment.empty? ? found : search(node.param, segments, depth + 1, found)
|
|
146
|
+
end
|
|
107
147
|
|
|
108
|
-
|
|
109
|
-
|
|
148
|
+
# @rbs target: Target?
|
|
149
|
+
# @rbs best: Target?
|
|
150
|
+
# @rbs return: Target?
|
|
151
|
+
def earlier(target, best)
|
|
152
|
+
return best unless target && (best.nil? || target.index < best.index)
|
|
110
153
|
|
|
111
|
-
|
|
112
|
-
[part, Skyward::API::Routing::PercentEncoding.decode(segment)] if part.is_a?(Symbol)
|
|
113
|
-
end.to_h
|
|
154
|
+
target
|
|
114
155
|
end
|
|
115
156
|
end
|