@pulse-compute/provider-fastly 1.0.0-beta.1 → 1.0.0-beta.10
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.
- package/README.md +7 -6
- package/dist/config-schema.json +30 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/package.json +11 -10
- package/src/build/canonical-target.js +35 -2
- package/src/build/digest-native.js +25 -0
- package/src/build/effect-invocations.js +49 -0
- package/src/build/embedded-assets.as.ts +32 -0
- package/src/build/entities-native.js +6 -11
- package/src/build/kv-native.as.ts +331 -0
- package/src/build/kv-native.js +27 -0
- package/src/build/native-application-errors.js +159 -0
- package/src/build/native-binary-resources.js +30 -0
- package/src/build/native-body-forwarding.as.ts +341 -0
- package/src/build/native-body-forwarding.js +69 -0
- package/src/build/native-dynamic-backends.as.ts +115 -0
- package/src/build/native-dynamic-backends.js +52 -0
- package/src/build/native-fetch-deadlines.as.ts +164 -0
- package/src/build/native-fetch-deadlines.js +27 -0
- package/src/build/native-fetch-errors.js +38 -0
- package/src/build/native-handle-ownership.js +127 -0
- package/src/build/native-header-lookup.js +50 -0
- package/src/build/native-http-effects.js +155 -32
- package/src/build/native-platform-capabilities.js +755 -210
- package/src/build/native-request-body.js +44 -0
- package/src/build/native-request-headers.js +93 -0
- package/src/build/native-send-error-detail.as.ts +12 -0
- package/src/build/native-stdio.js +45 -0
- package/src/build/native-string-values.js +143 -0
- package/src/build/native-transport-details.as.ts +84 -0
- package/src/build/native-value-budget.js +90 -0
- package/src/build/package-native-application.js +114 -0
- package/src/build/request-budget.as.ts +50 -0
- package/src/build/request-budget.js +47 -0
- package/src/build/s3-body.as.ts +97 -0
- package/src/build/s3-native.as.ts +228 -0
- package/src/build/s3-native.js +30 -0
- package/src/build/schema-admission.js +83 -0
- package/src/build/schema-nested-json.js +93 -0
- package/src/build/schema-scalar-record.js +41 -0
- package/src/build/time-native.js +49 -0
- package/src/build/wasm-budget.js +28 -0
- package/src/config-api.js +6 -0
- package/src/config-schema.json +30 -0
- package/src/index.ts +25 -0
- package/src/javascript/jwt-verifier.js +14 -4
- package/src/javascript/logging.js +11 -2
- package/src/javascript/package-effects.js +5 -2
- package/src/javascript/runtime-host.js +7 -3
- package/src/javascript/source-package.js +4 -2
- package/src/javascript/target-support-policy.js +20 -3
- package/src/javascript/target.js +17 -1
- package/src/javascript/test-runtime.js +1 -0
- package/src/provider-contract.js +22 -6
- package/src/runtime/canonical-api-runtime.js +6 -0
- package/src/testing/conditional-kv-host.js +155 -0
- package/src/testing/native-dynamic-backends-host.js +39 -0
- package/src/testing/native-http-effects-host.js +9 -1
- package/src/testing/native-platform-capabilities-host.js +349 -24
- package/src/toolchain/body-forwarding.js +25 -0
- package/src/toolchain/config.js +16 -0
- package/src/toolchain/index.js +48 -5
- package/src/toolchain/logging.js +18 -0
- package/src/toolchain/native-execution.js +78 -0
- package/src/toolchain/s3.js +26 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// FWD9-02: sequential upload/response pumps. Payload bytes never become Pulse
|
|
2
|
+
// values. Fixed scratch and ABI outputs are reused, including the clock slot.
|
|
3
|
+
// close accepts EOF into Fastly; it is not a delivery/drain receipt.
|
|
4
|
+
@external('fastly_http_req', 'send_async_streaming')
|
|
5
|
+
declare function __fwd_dispatch(request: i32, body: i32, backend: usize, length: i32, pending: usize): i32
|
|
6
|
+
@external('fastly_http_body', 'abandon') declare function __fwd_abandon(body: i32): i32
|
|
7
|
+
@external('fastly_async_io', 'is_ready') declare function __fwd_ready(handle: i32, ready: usize): i32
|
|
8
|
+
@external('fastly_http_resp', 'header_names_get')
|
|
9
|
+
declare function __fwd_names(handle: i32, buffer: usize, size: i32, cursor: i32, end: usize, written: usize): i32
|
|
10
|
+
@external('fastly_http_resp', 'header_values_get')
|
|
11
|
+
declare function __fwd_values(handle: i32, name: usize, length: i32, buffer: usize, size: i32, cursor: i32, end: usize, written: usize): i32
|
|
12
|
+
const __fwd_buffer: usize = memory.data(16384)
|
|
13
|
+
const __fwd_headers: usize = memory.data(16384)
|
|
14
|
+
const __fwd_pairs: usize = memory.data(1024) // 64 x (name ptr/size, value ptr/size)
|
|
15
|
+
const __fwd_io: usize = memory.data(128) // aligned reusable ABI outputs
|
|
16
|
+
const __fwd_detail: usize = __fwd_io + 8
|
|
17
|
+
const __fwd_done: usize = __fwd_detail + __PULSE_SEND_ERROR_DETAIL_BYTES
|
|
18
|
+
const __fwd_response_out: usize = __fwd_done + 4
|
|
19
|
+
const __fwd_body_out: usize = __fwd_response_out + 4 // ends at +40; next scratch field starts there
|
|
20
|
+
let __fwd_source: i32 = -1
|
|
21
|
+
let __fwd_upload: i32 = -1
|
|
22
|
+
let __fwd_request: i32 = -1
|
|
23
|
+
let __fwd_pending: i32 = -1
|
|
24
|
+
let __fwd_response: i32 = -1
|
|
25
|
+
let __fwd_body: i32 = -1
|
|
26
|
+
let __fwd_downstream: i32 = -1
|
|
27
|
+
let __fwd_output: i32 = -1
|
|
28
|
+
let __fwd_index: i32 = -1
|
|
29
|
+
let __fwd_result: i32 = 0
|
|
30
|
+
let __fwd_marker: i32 = 0
|
|
31
|
+
let __fwd_failure: i32 = 0
|
|
32
|
+
let __fwd_committed: bool = false
|
|
33
|
+
let __fwd_reserved: bool = false
|
|
34
|
+
let __fwd_header_count: i32 = 0
|
|
35
|
+
let __fwd_request_length: i32 = -1
|
|
36
|
+
let __fwd_response_length: i32 = -1
|
|
37
|
+
let __fwd_status: i32 = 200
|
|
38
|
+
function host_request_body(): i32 {
|
|
39
|
+
if (__fwd_marker != 0 || __fwd_reserved) { __fwd_fail(400); return 0; }
|
|
40
|
+
const marker = new __PulseFastlyValue(); marker.kind = 90;
|
|
41
|
+
__fwd_marker = __pulse_fastly_put(marker); return __fwd_marker;
|
|
42
|
+
}
|
|
43
|
+
function __fwd_fail(status: i32): void {
|
|
44
|
+
if (__fwd_failure == 0) __fwd_failure = status;
|
|
45
|
+
__pulse_fastly_fail(PULSE_ERROR_HOSTCALL, 280, __fwd_index);
|
|
46
|
+
}
|
|
47
|
+
function __fwd_remaining(): i32 {
|
|
48
|
+
if (__fwd_failure != 0) return 0;
|
|
49
|
+
if (wasi_snapshot_preview1_clock_time_get(1, 1, __fwd_io) != 0) { __fwd_fail(504); return 0; }
|
|
50
|
+
const now = load<i64>(__fwd_io);
|
|
51
|
+
if (now < 0 || now < __request_last_clock || now < __pulse_fetch_last_clock) { __fwd_fail(504); return 0; }
|
|
52
|
+
__request_last_clock = now; __pulse_fetch_last_clock = now;
|
|
53
|
+
let remaining = f64(__request_deadline - now) / 1000000.0;
|
|
54
|
+
if (__request_expired || remaining <= 0) { __request_expired = true; __fwd_fail(504); return 0; }
|
|
55
|
+
if (__fwd_index >= 0) remaining = Math.min(remaining, __pulse_fetch_remaining(__fwd_index, now));
|
|
56
|
+
if (remaining <= 0) { __fwd_fail(504); return 0; }
|
|
57
|
+
return i32(Math.ceil(remaining)); // 1..30000; zero never reaches async.select
|
|
58
|
+
}
|
|
59
|
+
function __fwd_cleanup(): void {
|
|
60
|
+
if (__fwd_source >= 0) { __pulse_fetch_close_body(__fwd_source); __fwd_source = -1; }
|
|
61
|
+
if (__fwd_upload >= 0) { __fwd_abandon(__fwd_upload); __fwd_upload = -1; }
|
|
62
|
+
if (__fwd_request >= 0) { __pulse_fetch_close_request(__fwd_request); __fwd_request = -1; }
|
|
63
|
+
if (__fwd_body >= 0) { __pulse_fetch_close_body(__fwd_body); __fwd_body = -1; }
|
|
64
|
+
if (__fwd_response >= 0) { __pulse_fetch_close_response(__fwd_response); __fwd_response = -1; }
|
|
65
|
+
if (__fwd_output >= 0) { __pulse_fetch_close_response(__fwd_output); __fwd_output = -1; }
|
|
66
|
+
if (__fwd_downstream >= 0) { __fwd_abandon(__fwd_downstream); __fwd_downstream = -1; }
|
|
67
|
+
if (__fwd_pending >= 0) {
|
|
68
|
+
// No pending-request cancel ABI. Poll once without waiting and discard any
|
|
69
|
+
// late handles; an unresolved host operation ends at invocation teardown.
|
|
70
|
+
__pulse_send_error_detail_reset(__fwd_detail);
|
|
71
|
+
store<i32>(__fwd_done, 0);
|
|
72
|
+
const status = __pulse_fetch_poll(__fwd_pending, __fwd_detail, __fwd_done, __fwd_response_out, __fwd_body_out);
|
|
73
|
+
if (status == 0 && load<i32>(__fwd_done) == 1) {
|
|
74
|
+
__pulse_fetch_close_response(load<i32>(__fwd_response_out)); __pulse_fetch_close_body(load<i32>(__fwd_body_out));
|
|
75
|
+
}
|
|
76
|
+
__fwd_pending = -1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function __fwd_error_response(): bool {
|
|
80
|
+
if (__fwd_committed) { __fwd_cleanup(); return true; }
|
|
81
|
+
if (__fwd_failure == 0) return false;
|
|
82
|
+
const status = __fwd_failure;
|
|
83
|
+
__fwd_cleanup(); __request_error_response = true;
|
|
84
|
+
__pulse_fastly_last_error = 0;
|
|
85
|
+
const value = new __PulseFastlyValue(); value.kind = PULSE_VALUE_RESPONSE; value.status = status;
|
|
86
|
+
value.text = status == 413 ? 'Payload Too Large' : status == 504 ? 'Gateway Timeout' : status == 400 ? 'Bad Request' : 'Bad Gateway';
|
|
87
|
+
__fwd_committed = true;
|
|
88
|
+
__pulse_fastly_send_result(__pulse_fastly_put(value)); return true;
|
|
89
|
+
}
|
|
90
|
+
function __fwd_lower(byte: u8): u8 { return byte >= 65 && byte <= 90 ? byte + 32 : byte; }
|
|
91
|
+
function __fwd_equal(ptr: usize, size: i32, text: string): bool {
|
|
92
|
+
if (size != text.length) return false;
|
|
93
|
+
for (let i = 0; i < size; i++) if (__fwd_lower(load<u8>(ptr + i)) != text.charCodeAt(i)) return false;
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
function __fwd_token(byte: u8): bool {
|
|
97
|
+
return (byte >= 48 && byte <= 57) || (byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122)
|
|
98
|
+
|| byte == 33 || byte == 35 || byte == 36 || byte == 37 || byte == 38 || byte == 39 || byte == 42
|
|
99
|
+
|| byte == 43 || byte == 45 || byte == 46 || byte == 94 || byte == 95 || byte == 96 || byte == 124 || byte == 126;
|
|
100
|
+
}
|
|
101
|
+
function __fwd_hop(ptr: usize, size: i32): bool {
|
|
102
|
+
return __fwd_equal(ptr,size,'connection') || __fwd_equal(ptr,size,'keep-alive') || __fwd_equal(ptr,size,'proxy-connection')
|
|
103
|
+
|| __fwd_equal(ptr,size,'transfer-encoding') || __fwd_equal(ptr,size,'te') || __fwd_equal(ptr,size,'trailer')
|
|
104
|
+
|| __fwd_equal(ptr,size,'upgrade') || __fwd_equal(ptr,size,'content-length') || __fwd_equal(ptr,size,'host')
|
|
105
|
+
|| __fwd_equal(ptr,size,'proxy-authenticate') || __fwd_equal(ptr,size,'proxy-authorization');
|
|
106
|
+
}
|
|
107
|
+
// Decimal parsing stops at cap+1: huge declared sizes cannot overflow or wrap.
|
|
108
|
+
function __fwd_length(ptr: usize, size: i32, cap: i32): i32 {
|
|
109
|
+
if (size == 0) return -2;
|
|
110
|
+
let value = 0;
|
|
111
|
+
for (let i = 0; i < size; i++) {
|
|
112
|
+
const byte = load<u8>(ptr + i); if (byte < 48 || byte > 57) return -2;
|
|
113
|
+
value = min(cap + 1, value * 10 + byte - 48);
|
|
114
|
+
}
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
function __fwd_declared_request(): bool {
|
|
118
|
+
const name = memory.data<u8>([99,111,110,116,101,110,116,45,108,101,110,103,116,104]);
|
|
119
|
+
store<i64>(__fwd_io + 40, -1); store<i32>(__fwd_io + 48, 0);
|
|
120
|
+
const status = __pulse_request_header_values(__pulse_fastly_request_handle, name, 14, __fwd_headers, 16384, 0, __fwd_io + 40, __fwd_io + 48);
|
|
121
|
+
const count = load<i32>(__fwd_io + 48);
|
|
122
|
+
if (status != 0 || count < 0 || count > 16384 || (count > 0 && load<i64>(__fwd_io + 40) != -1)) { __fwd_fail(400); return false; }
|
|
123
|
+
if (count == 0) return true;
|
|
124
|
+
if (load<u8>(__fwd_headers + count - 1) != 0) { __fwd_fail(400); return false; }
|
|
125
|
+
__fwd_request_length = __fwd_length(__fwd_headers, count - 1, __FWD_REQUEST_MAX);
|
|
126
|
+
if (__fwd_request_length < 0) { __fwd_fail(400); return false; }
|
|
127
|
+
if (__fwd_request_length > __FWD_REQUEST_MAX) { __fwd_fail(413); return false; }
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
function __fwd_begin(index: i32, payload: __PulseFastlyValue): void {
|
|
131
|
+
__fwd_index = index;
|
|
132
|
+
const init = __pulse_fastly_value(__pulse_fastly_payload_field(payload, 'init'));
|
|
133
|
+
if (__fwd_reserved || __fwd_source < 0 || __pulse_fastly_payload_field(init, 'body') != __fwd_marker || __fwd_marker == 0) { __fwd_fail(400); return; }
|
|
134
|
+
__fwd_reserved = true;
|
|
135
|
+
__pulse_fetch_begin_deadline(index, init);
|
|
136
|
+
if (__pulse_fastly_last_error != 0 || !__fwd_remaining() || !__fwd_declared_request()) return;
|
|
137
|
+
const url = __pulse_fastly_utf8(__pulse_fastly_string(__pulse_fastly_payload_field(payload, 'url')));
|
|
138
|
+
const backend = __pulse_fastly_utf8(__pulse_fastly_backend(index));
|
|
139
|
+
if (fastly_http_req_new(__fwd_io + 52) != 0) { __fwd_fail(502); return; }
|
|
140
|
+
__fwd_request = load<i32>(__fwd_io + 52);
|
|
141
|
+
if (fastly_http_body_new(__fwd_io + 52) != 0) { __fwd_fail(502); return; }
|
|
142
|
+
__fwd_upload = load<i32>(__fwd_io + 52);
|
|
143
|
+
const method = memory.data<u8>([80,79,83,84]);
|
|
144
|
+
if (fastly_http_req_method_set(__fwd_request, method, 4) != 0 || fastly_http_req_uri_set(__fwd_request, changetype<usize>(url), url.byteLength) != 0) { __fwd_fail(502); return; }
|
|
145
|
+
const headers = __pulse_fastly_value(__pulse_fastly_payload_field(init, 'headers'));
|
|
146
|
+
let bytes = 0;
|
|
147
|
+
if (headers.kind == PULSE_VALUE_OBJECT) for (let i = 0; i < headers.keys.length; i++) {
|
|
148
|
+
const name = __pulse_fastly_utf8(headers.keys[i]), value = __pulse_fastly_utf8(__pulse_fastly_string(headers.values[i]));
|
|
149
|
+
bytes += name.byteLength + value.byteLength + 4;
|
|
150
|
+
if (i >= 64 || bytes > 16384 || __fwd_hop(changetype<usize>(name), name.byteLength)) { __fwd_fail(400); return; }
|
|
151
|
+
if (fastly_http_req_header_insert(__fwd_request, changetype<usize>(name), name.byteLength, changetype<usize>(value), value.byteLength) != 0) { __fwd_fail(400); return; }
|
|
152
|
+
}
|
|
153
|
+
if (!__fwd_remaining()) return;
|
|
154
|
+
__pulse_transport_slots[index] = null;
|
|
155
|
+
const status = __fwd_dispatch(__fwd_request, __fwd_upload, changetype<usize>(backend), backend.byteLength, __fwd_io + 52);
|
|
156
|
+
// send may consume the request even on failure. Cleanup makes only a best-effort
|
|
157
|
+
// close on that error path before any handle allocation/reuse can occur.
|
|
158
|
+
if (status != 0) { __pulse_transport_capture(__fwd_index, status, 'send'); __fwd_fail(502); return; }
|
|
159
|
+
__fwd_request = -1; __fwd_pending = load<i32>(__fwd_io + 52);
|
|
160
|
+
__pulse_fastly_pending[index] = __fwd_pending; __pulse_fastly_pending_mode[index] = PULSE_FASTLY_PENDING_ASYNC;
|
|
161
|
+
__fwd_remaining();
|
|
162
|
+
}
|
|
163
|
+
// Adopt early headers before considering readiness of the source or writer.
|
|
164
|
+
function __fwd_poll(): bool {
|
|
165
|
+
if (__fwd_pending < 0 || !__fwd_remaining()) return __fwd_response >= 0;
|
|
166
|
+
__pulse_send_error_detail_reset(__fwd_detail);
|
|
167
|
+
store<i32>(__fwd_done, 0);
|
|
168
|
+
const status = __pulse_fetch_poll(__fwd_pending, __fwd_detail, __fwd_done, __fwd_response_out, __fwd_body_out);
|
|
169
|
+
const done = load<i32>(__fwd_done);
|
|
170
|
+
if (status != 0) { __pulse_transport_capture(__fwd_index, status, 'poll', __fwd_detail); __fwd_pending = -1; __fwd_fail(502); return false; }
|
|
171
|
+
if (done != 0 && done != 1) { __fwd_fail(502); return false; }
|
|
172
|
+
if (done == 1) {
|
|
173
|
+
__fwd_response = load<i32>(__fwd_response_out); __fwd_body = load<i32>(__fwd_body_out); __fwd_pending = -1;
|
|
174
|
+
}
|
|
175
|
+
if (!__fwd_remaining()) return false;
|
|
176
|
+
return done == 1;
|
|
177
|
+
}
|
|
178
|
+
function __fwd_wait_io(handle: i32, upload: bool): bool {
|
|
179
|
+
while (__fwd_remaining() > 0) {
|
|
180
|
+
if (upload && __fwd_poll()) return false;
|
|
181
|
+
if (__fwd_failure != 0) return false;
|
|
182
|
+
store<i32>(__fwd_io + 52, -1);
|
|
183
|
+
if (__fwd_ready(handle, __fwd_io + 52) != 0) { __fwd_fail(502); return false; }
|
|
184
|
+
const ready = load<i32>(__fwd_io + 52);
|
|
185
|
+
if (ready == 1) return __fwd_remaining() > 0;
|
|
186
|
+
if (ready != 0) { __fwd_fail(502); return false; }
|
|
187
|
+
let count = 0;
|
|
188
|
+
if (upload && __fwd_pending >= 0) { store<i32>(__fwd_io + 56, __fwd_pending); count++; }
|
|
189
|
+
store<i32>(__fwd_io + 56 + count * 4, handle); count++;
|
|
190
|
+
const remaining = __fwd_remaining(); if (remaining == 0) return false;
|
|
191
|
+
const status = __pulse_fetch_select(__fwd_io + 56, count, remaining, __fwd_io + 64);
|
|
192
|
+
if (status != 0) { __fwd_fail(502); return false; }
|
|
193
|
+
const selected = load<i32>(__fwd_io + 64);
|
|
194
|
+
if (selected == -1) { __fwd_fail(504); return false; }
|
|
195
|
+
if (selected < 0 || selected >= count) { __fwd_fail(502); return false; }
|
|
196
|
+
}
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
function __fwd_copy(source: i32, sink: i32, cap: i32, declared: i32, upload: bool): bool {
|
|
200
|
+
let total = 0;
|
|
201
|
+
while (true) {
|
|
202
|
+
// Demand first; at most one raw chunk can be read ahead of the writer.
|
|
203
|
+
if (!__fwd_wait_io(sink, upload) || !__fwd_wait_io(source, upload)) return false;
|
|
204
|
+
const size = min(16384, cap - total + 1);
|
|
205
|
+
store<i32>(__fwd_io + 68, -1);
|
|
206
|
+
if (fastly_http_body_read(source, __fwd_buffer, size, __fwd_io + 68) != 0) { __fwd_fail(upload ? 400 : 502); return false; }
|
|
207
|
+
const read = load<i32>(__fwd_io + 68);
|
|
208
|
+
if (!__fwd_remaining()) return false;
|
|
209
|
+
if (read < 0 || read > size) { __fwd_fail(502); return false; }
|
|
210
|
+
if (read == 0) {
|
|
211
|
+
if (declared >= 0 && total != declared) { __fwd_fail(upload ? 400 : 502); return false; }
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
total += read;
|
|
215
|
+
if (total > cap) { __fwd_fail(upload ? 413 : 502); return false; }
|
|
216
|
+
if (declared >= 0 && total > declared) { __fwd_fail(upload ? 400 : 502); return false; }
|
|
217
|
+
let offset = 0;
|
|
218
|
+
while (offset < read) {
|
|
219
|
+
if (!__fwd_wait_io(sink, upload)) return false;
|
|
220
|
+
store<i32>(__fwd_io + 72, -1);
|
|
221
|
+
if (fastly_http_body_write(sink, __fwd_buffer + offset, read - offset, 0, __fwd_io + 72) != 0) { __fwd_fail(502); return false; }
|
|
222
|
+
const written = load<i32>(__fwd_io + 72);
|
|
223
|
+
if (!__fwd_remaining()) return false;
|
|
224
|
+
if (written <= 0 || written > read - offset) { __fwd_fail(502); return false; }
|
|
225
|
+
offset += written;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function __fwd_wait(): i32 {
|
|
230
|
+
if (__fwd_failure != 0 || __fwd_pending < 0) return 0;
|
|
231
|
+
const eof = __fwd_copy(__fwd_source, __fwd_upload, __FWD_REQUEST_MAX, __fwd_request_length, true);
|
|
232
|
+
__pulse_fetch_close_body(__fwd_source); __fwd_source = -1;
|
|
233
|
+
if (eof && __fwd_remaining()) {
|
|
234
|
+
const upload = __fwd_upload; __fwd_upload = -1;
|
|
235
|
+
if (__pulse_fetch_close_body(upload) != 0) __fwd_fail(502);
|
|
236
|
+
} else if (__fwd_upload >= 0) { __fwd_abandon(__fwd_upload); __fwd_upload = -1; }
|
|
237
|
+
while (__fwd_failure == 0 && __fwd_response < 0) {
|
|
238
|
+
if (__fwd_poll()) break;
|
|
239
|
+
if (__fwd_failure != 0 || !__fwd_wait_io(__fwd_pending, false)) break;
|
|
240
|
+
}
|
|
241
|
+
__pulse_fastly_pending_mode[__fwd_index] = PULSE_FASTLY_PENDING_NONE; __pulse_fastly_pending[__fwd_index] = 0;
|
|
242
|
+
if (__fwd_failure != 0 || !__fwd_remaining()) return 0;
|
|
243
|
+
const value = new __PulseFastlyValue(); value.kind = PULSE_VALUE_FETCH;
|
|
244
|
+
value.responseHandle = __fwd_response; value.bodyHandle = __fwd_body;
|
|
245
|
+
__fwd_result = __pulse_fastly_put(value); return __fwd_result;
|
|
246
|
+
}
|
|
247
|
+
function __fwd_response_headers(): bool {
|
|
248
|
+
store<i32>(__fwd_io + 52, 0);
|
|
249
|
+
if (fastly_http_resp_status_get(__fwd_response, __fwd_io + 52) != 0) { __fwd_fail(502); return false; }
|
|
250
|
+
__fwd_status = load<u16>(__fwd_io + 52);
|
|
251
|
+
if (__fwd_status < 200 || __fwd_status > 599) { __fwd_fail(502); return false; }
|
|
252
|
+
store<i64>(__fwd_io + 40, -1); store<i32>(__fwd_io + 48, -1);
|
|
253
|
+
if (__fwd_names(__fwd_response, __fwd_headers, 16384, 0, __fwd_io + 40, __fwd_io + 48) != 0) { __fwd_fail(502); return false; }
|
|
254
|
+
const names = load<i32>(__fwd_io + 48);
|
|
255
|
+
if (names < 0 || names > 16384 || (names > 0 && load<i64>(__fwd_io + 40) != -1)) { __fwd_fail(502); return false; }
|
|
256
|
+
let used = names, start = 0, total = 0, nameCount = 0;
|
|
257
|
+
for (let i = 0; i < names; i++) {
|
|
258
|
+
const byte = load<u8>(__fwd_headers + i);
|
|
259
|
+
if (byte != 0) { if (!__fwd_token(byte)) { __fwd_fail(502); return false; } continue; }
|
|
260
|
+
const name = __fwd_headers + start, length = i - start; start = i + 1;
|
|
261
|
+
if (length == 0 || ++nameCount > 64 || used >= 16384) { __fwd_fail(502); return false; }
|
|
262
|
+
store<i64>(__fwd_io + 40, -1); store<i32>(__fwd_io + 48, -1);
|
|
263
|
+
if (__fwd_values(__fwd_response, name, length, __fwd_headers + used, 16384 - used, 0, __fwd_io + 40, __fwd_io + 48) != 0) { __fwd_fail(502); return false; }
|
|
264
|
+
const size = load<i32>(__fwd_io + 48);
|
|
265
|
+
if (size <= 0 || size > 16384 - used || load<i64>(__fwd_io + 40) != -1) { __fwd_fail(502); return false; }
|
|
266
|
+
let valueStart = 0;
|
|
267
|
+
for (let j = 0; j < size; j++) {
|
|
268
|
+
const item = load<u8>(__fwd_headers + used + j);
|
|
269
|
+
if (item != 0) { if ((item < 32 && item != 9) || item == 127) { __fwd_fail(502); return false; } continue; }
|
|
270
|
+
const value = __fwd_headers + used + valueStart, valueSize = j - valueStart; valueStart = j + 1;
|
|
271
|
+
total += length + valueSize + 4;
|
|
272
|
+
if (__fwd_header_count >= 64 || total > 16384) { __fwd_fail(502); return false; }
|
|
273
|
+
const pair = __fwd_pairs + __fwd_header_count * 16; __fwd_header_count++;
|
|
274
|
+
store<usize>(pair, name); store<i32>(pair + 4, length); store<usize>(pair + 8, value); store<i32>(pair + 12, valueSize);
|
|
275
|
+
if (__fwd_equal(name, length, 'content-length')) {
|
|
276
|
+
if (__fwd_response_length != -1) { __fwd_fail(502); return false; }
|
|
277
|
+
__fwd_response_length = __fwd_length(value, valueSize, __FWD_RESPONSE_MAX);
|
|
278
|
+
if (__fwd_response_length < 0 || (__fwd_status != 304 && __fwd_response_length > __FWD_RESPONSE_MAX)) { __fwd_fail(502); return false; }
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (valueStart != size) { __fwd_fail(502); return false; }
|
|
282
|
+
used += size;
|
|
283
|
+
}
|
|
284
|
+
if (start != names || ((__fwd_status == 204 || __fwd_status == 205) && __fwd_response_length > 0)) { __fwd_fail(502); return false; }
|
|
285
|
+
return __fwd_remaining() > 0;
|
|
286
|
+
}
|
|
287
|
+
function __fwd_nominated(name: usize, length: i32): bool {
|
|
288
|
+
let nominated = false;
|
|
289
|
+
for (let i = 0; i < __fwd_header_count; i++) {
|
|
290
|
+
const pair = __fwd_pairs + i * 16;
|
|
291
|
+
if (!__fwd_equal(load<usize>(pair), load<i32>(pair + 4), 'connection')) continue;
|
|
292
|
+
const value = load<usize>(pair + 8), size = load<i32>(pair + 12);
|
|
293
|
+
let offset = 0;
|
|
294
|
+
while (offset < size) {
|
|
295
|
+
while (offset < size && (load<u8>(value + offset) == 32 || load<u8>(value + offset) == 9)) offset++;
|
|
296
|
+
const start = offset;
|
|
297
|
+
while (offset < size && __fwd_token(load<u8>(value + offset))) offset++;
|
|
298
|
+
const count = offset - start;
|
|
299
|
+
if (count == 0) { __fwd_fail(502); return false; }
|
|
300
|
+
if (count == length) {
|
|
301
|
+
let same = true;
|
|
302
|
+
for (let c = 0; c < count; c++) if (__fwd_lower(load<u8>(value + start + c)) != __fwd_lower(load<u8>(name + c))) same = false;
|
|
303
|
+
if (same) nominated = true;
|
|
304
|
+
}
|
|
305
|
+
while (offset < size && (load<u8>(value + offset) == 32 || load<u8>(value + offset) == 9)) offset++;
|
|
306
|
+
if (offset < size && (load<u8>(value + offset++) != 44 || offset == size)) { __fwd_fail(502); return false; }
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return nominated;
|
|
310
|
+
}
|
|
311
|
+
function __fwd_send(): i32 {
|
|
312
|
+
if (!__fwd_response_headers()) { __fwd_error_response(); return 0; }
|
|
313
|
+
if (fastly_http_resp_new(__fwd_io + 52) != 0) { __fwd_fail(502); __fwd_error_response(); return 0; }
|
|
314
|
+
__fwd_output = load<i32>(__fwd_io + 52);
|
|
315
|
+
if (fastly_http_resp_status_set(__fwd_output, __fwd_status) != 0 || fastly_http_body_new(__fwd_io + 52) != 0) { __fwd_fail(502); __fwd_error_response(); return 0; }
|
|
316
|
+
__fwd_downstream = load<i32>(__fwd_io + 52);
|
|
317
|
+
for (let i = 0; i < __fwd_header_count; i++) {
|
|
318
|
+
const pair = __fwd_pairs + i * 16, name = load<usize>(pair), length = load<i32>(pair + 4);
|
|
319
|
+
const nominated = __fwd_nominated(name, length);
|
|
320
|
+
if (__fwd_failure != 0) break;
|
|
321
|
+
if (__fwd_hop(name, length) || nominated) continue;
|
|
322
|
+
if (fastly_http_resp_header_append(__fwd_output, name, length, load<usize>(pair + 8), load<i32>(pair + 12)) != 0) { __fwd_fail(502); break; }
|
|
323
|
+
}
|
|
324
|
+
__pulse_fetch_close_response(__fwd_response); __fwd_response = -1;
|
|
325
|
+
if (!__fwd_remaining()) { __fwd_error_response(); return 0; }
|
|
326
|
+
// Treat even an ambiguous send failure as committed: never emit a second
|
|
327
|
+
// response. The writable body remains ours until explicit EOF or abandon.
|
|
328
|
+
__fwd_committed = true;
|
|
329
|
+
const output = __fwd_output; __fwd_output = -1;
|
|
330
|
+
if (fastly_http_resp_send_downstream(output, __fwd_downstream, 1) != 0) { __pulse_fetch_close_response(output); __fwd_fail(502); __fwd_cleanup(); return 0; }
|
|
331
|
+
const bodyless = __fwd_status == 204 || __fwd_status == 205 || __fwd_status == 304;
|
|
332
|
+
const eof = __fwd_copy(__fwd_body, __fwd_downstream, bodyless ? 0 : __FWD_RESPONSE_MAX, bodyless ? 0 : __fwd_response_length, false);
|
|
333
|
+
__pulse_fetch_close_body(__fwd_body); __fwd_body = -1;
|
|
334
|
+
if (eof && __fwd_remaining()) {
|
|
335
|
+
const downstream = __fwd_downstream; __fwd_downstream = -1;
|
|
336
|
+
if (__pulse_fetch_close_body(downstream) != 0) __fwd_fail(502);
|
|
337
|
+
// Success is the host-handoff boundary. The handle is gone; there is no
|
|
338
|
+
// final-writer wait, delivery receipt, or post-handoff timeout guarantee.
|
|
339
|
+
}
|
|
340
|
+
__fwd_cleanup(); return 0;
|
|
341
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('node:fs');
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const {normalizeBodyForwarding, forwardingError} = require('../toolchain/body-forwarding.js');
|
|
5
|
+
const IMPORTS = ['fastly_http_req:send_async_streaming', 'fastly_http_body:abandon',
|
|
6
|
+
'fastly_async_io:is_ready', 'fastly_http_resp:header_names_get'];
|
|
7
|
+
const enabled = plan => (plan?.capabilities || []).includes('request.body.forward');
|
|
8
|
+
function field(value, name) { return value?.entries?.find(entry => entry.key?.value === name)?.value; }
|
|
9
|
+
function validate(plan, options) {
|
|
10
|
+
const config = normalizeBodyForwarding(options.bodyForwarding, options.maxDurationMs);
|
|
11
|
+
if (!enabled(plan)) return;
|
|
12
|
+
if (!config) throw forwardingError('Fastly Native incoming forwarding requires fastly.bodyForwarding limits and maxDurationMs.');
|
|
13
|
+
const effects = plan.effects || [], effect = effects[0];
|
|
14
|
+
const init = effect?.inputs?.find(input => input.name === 'init')?.value;
|
|
15
|
+
const body = field(init, 'body'), method = field(init, 'method'), headers = field(init, 'headers');
|
|
16
|
+
if (effects.length !== 1 || effect.kind !== 'fetch' || effect.grouped
|
|
17
|
+
|| effect.result?.mode !== 'return' || effect.result?.valueKind !== 'opaque-response'
|
|
18
|
+
|| effect.resource?.kind !== 'literal' || options.bindings?.dynamicBackends
|
|
19
|
+
|| body?.name !== 'request.body.forward-marker' || method?.kind !== 'literal' || method.value !== 'POST'
|
|
20
|
+
|| init.entries.some(entry => !['method', 'body', 'headers', 'timeoutMs'].includes(entry.key?.value))
|
|
21
|
+
|| (headers && (headers.kind !== 'object' || headers.entries.some(entry => entry.key?.kind !== 'literal' || entry.value?.kind !== 'literal' || typeof entry.value.value !== 'string')))) {
|
|
22
|
+
throw forwardingError('Fastly Native forwarding admits one terminal opaque POST fetch with inline req.body(), a static backend, and optional literal headers/timeoutMs; groups, decoding, reuse, and dynamic backends are unsupported.');
|
|
23
|
+
}
|
|
24
|
+
const entries = headers?.entries || [];
|
|
25
|
+
const hop = /^(connection|keep-alive|proxy-connection|transfer-encoding|te|trailer|upgrade|content-length|host|proxy-authenticate|proxy-authorization)$/i;
|
|
26
|
+
if (entries.length > 64 || entries.reduce((sum, entry) => sum + Buffer.byteLength(entry.key.value) + Buffer.byteLength(entry.value.value) + 4, 0) > 16384
|
|
27
|
+
|| entries.some(entry => hop.test(entry.key.value) || !/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(entry.key.value) || /[\x00-\x08\x0a-\x1f\x7f]/.test(entry.value.value))) {
|
|
28
|
+
throw forwardingError('Forwarding request headers must fit 64 pairs/16 KiB and exclude hop-by-hop, framing and Host fields.');
|
|
29
|
+
}
|
|
30
|
+
// One marker evaluation owns the incoming body. Reject unused/duplicate markers
|
|
31
|
+
// too: they must not be dispatched later through another continuation.
|
|
32
|
+
let markers = 0;
|
|
33
|
+
function walk(value) { if (!value || typeof value !== 'object') return; if (value.name === 'request.body.forward-marker') markers++; for (const item of Object.values(value)) walk(item); }
|
|
34
|
+
walk(plan);
|
|
35
|
+
if (markers !== 1) throw forwardingError('Fastly Native forwarding requires exactly one incoming body consumer.');
|
|
36
|
+
}
|
|
37
|
+
function instrument(input, plan, bindings) {
|
|
38
|
+
if (!enabled(plan)) return input;
|
|
39
|
+
let source = input;
|
|
40
|
+
const replace = (before, after) => {
|
|
41
|
+
if (!source.includes(before)) throw new Error('Fastly body-forwarding boundary drift: ' + before.slice(0, 100));
|
|
42
|
+
source = source.replace(before, after);
|
|
43
|
+
};
|
|
44
|
+
for (const [name, args, result, body] of [
|
|
45
|
+
['__pulse_fastly_fetch_begin', 'effectIndex: i32, payload: __PulseFastlyValue', 'void', '__fwd_begin(effectIndex, payload);'],
|
|
46
|
+
['__pulse_fastly_wait_fetch', 'effectIndex: i32', 'i32', 'return __fwd_wait();']
|
|
47
|
+
]) {
|
|
48
|
+
const line = source.split('\n').find(line => line.startsWith(`function ${name}(`));
|
|
49
|
+
if (!line || !line.endsWith('}')) throw new Error('Fastly forwarding function boundary drift: ' + name);
|
|
50
|
+
replace(line, `function ${name}(${args}): ${result} { ${body} }`);
|
|
51
|
+
}
|
|
52
|
+
replace('function __pulse_fastly_send_result(handle: i32): i32 {',
|
|
53
|
+
'function __pulse_fastly_send_result(handle: i32): i32 { if (handle == __fwd_result && handle != 0) return __fwd_send();');
|
|
54
|
+
replace('function __pulse_fastly_jwt_send_error(): void {',
|
|
55
|
+
'function __pulse_fastly_jwt_send_error(): void { if (__fwd_error_response()) return;');
|
|
56
|
+
replace('function __request_send_timeout(): void {',
|
|
57
|
+
'function __request_send_timeout(): void { if (__fwd_committed) { __fwd_cleanup(); return; }');
|
|
58
|
+
replace(' __pulse_fastly_run_invocation()\n', ' __pulse_fastly_run_invocation()\n __fwd_cleanup()\n');
|
|
59
|
+
replace(' __pulse_fastly_request_body_handle = load<i32>(changetype<usize>(handles) + 4)',
|
|
60
|
+
' __pulse_fastly_request_body_handle = load<i32>(changetype<usize>(handles) + 4)\n __fwd_source = __pulse_fastly_request_body_handle');
|
|
61
|
+
// Generic request-budget code must not wait for headers before driving upload.
|
|
62
|
+
const begin = source.indexOf('function __pulse_fastly_resolve_effect(');
|
|
63
|
+
const end = source.indexOf('\n}', begin);
|
|
64
|
+
if (begin < 0 || end < 0) throw new Error('Fastly forwarding effect boundary drift');
|
|
65
|
+
source = source.slice(0, begin) + 'function __pulse_fastly_resolve_effect(effectIndex: i32): i32 { return __fwd_wait(); }' + source.slice(end + 2);
|
|
66
|
+
return source + '\n' + `const __FWD_REQUEST_MAX: i32 = ${bindings.bodyForwarding.requestMaxBytes}\nconst __FWD_RESPONSE_MAX: i32 = ${bindings.bodyForwarding.responseMaxBytes}\n`
|
|
67
|
+
+ fs.readFileSync(path.join(__dirname, 'native-body-forwarding.as.ts'), 'utf8');
|
|
68
|
+
}
|
|
69
|
+
module.exports = {IMPORTS, enabled, validate, instrument};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Provider-owned, request-local dynamic backend policy. No optional timeout or
|
|
2
|
+
// trust overrides: deadlines and certificate validation retain their owners.
|
|
3
|
+
@external("fastly_http_req", "register_dynamic_backend")
|
|
4
|
+
declare function __pulse_dynamic_register(name: usize, nameLength: i32, target: usize, targetLength: i32, mask: u32, config: usize): i32
|
|
5
|
+
const __pulse_dynamic_origins = new Array<string>()
|
|
6
|
+
const __pulse_dynamic_names = new Array<string>()
|
|
7
|
+
let __pulse_dynamic_attempts: i32 = 0
|
|
8
|
+
let __pulse_dynamic_status: i32 = 0
|
|
9
|
+
let __pulse_dynamic_url: string = ""
|
|
10
|
+
export function pulse_fastly_dynamic_backend_status(): i32 { return __pulse_dynamic_status }
|
|
11
|
+
function __pulse_dynamic_clear(): void {
|
|
12
|
+
__pulse_dynamic_origins.length = 0; __pulse_dynamic_names.length = 0; __pulse_dynamic_url = ""
|
|
13
|
+
}
|
|
14
|
+
function __pulse_dynamic_invalid(index: i32): string {
|
|
15
|
+
__pulse_fastly_fail(PULSE_ERROR_VALUE, 273, index); return ""
|
|
16
|
+
}
|
|
17
|
+
function __pulse_dynamic_select(index: i32, url: string): string {
|
|
18
|
+
__pulse_dynamic_url = url
|
|
19
|
+
const fixed = __pulse_fastly_backend(index)
|
|
20
|
+
if (fixed.length > 0) return fixed
|
|
21
|
+
if (!__pulse_dynamic_check(index)) return ""
|
|
22
|
+
if (url.length > PULSE_FASTLY_BUFFER_BYTES) return __pulse_dynamic_invalid(index)
|
|
23
|
+
for (let i = 0; i < url.length; i++) {
|
|
24
|
+
const c = url.charCodeAt(i)
|
|
25
|
+
if (c <= 32 || c >= 127 || c == 92) return __pulse_dynamic_invalid(index)
|
|
26
|
+
}
|
|
27
|
+
const separator = url.indexOf("://")
|
|
28
|
+
if (separator < 0) return __pulse_dynamic_invalid(index)
|
|
29
|
+
const scheme = url.substring(0, separator).toLowerCase()
|
|
30
|
+
const tls = scheme == "https"
|
|
31
|
+
if (!tls && scheme != "http") return __pulse_dynamic_invalid(index)
|
|
32
|
+
const start = separator + 3
|
|
33
|
+
let end = start
|
|
34
|
+
while (end < url.length && url.charAt(end) != "/" && url.charAt(end) != "?" && url.charAt(end) != "#") end++
|
|
35
|
+
const authority = url.substring(start, end).toLowerCase()
|
|
36
|
+
if (authority.length == 0 || authority.length > 1024 || authority.indexOf("@") >= 0) return __pulse_dynamic_invalid(index)
|
|
37
|
+
const colon = authority.indexOf(":")
|
|
38
|
+
let hostname = colon < 0 ? authority : authority.substring(0, colon)
|
|
39
|
+
let port = tls ? 443 : 80
|
|
40
|
+
if (colon >= 0) {
|
|
41
|
+
const digits = authority.substring(colon + 1)
|
|
42
|
+
if (digits.length == 0 || digits.length > 5) return __pulse_dynamic_invalid(index)
|
|
43
|
+
port = 0
|
|
44
|
+
for (let i = 0; i < digits.length; i++) {
|
|
45
|
+
const c = digits.charCodeAt(i)
|
|
46
|
+
if (c < 48 || c > 57) return __pulse_dynamic_invalid(index)
|
|
47
|
+
port = port * 10 + c - 48
|
|
48
|
+
}
|
|
49
|
+
if (port < 1 || port > 65535) return __pulse_dynamic_invalid(index)
|
|
50
|
+
}
|
|
51
|
+
const dns = hostname.endsWith(".") ? hostname.substring(0, hostname.length - 1) : hostname
|
|
52
|
+
if (dns.length == 0 || dns.length > 253) return __pulse_dynamic_invalid(index)
|
|
53
|
+
const labels = dns.split(".")
|
|
54
|
+
let allNumeric = true
|
|
55
|
+
for (let i = 0; i < labels.length; i++) {
|
|
56
|
+
const label = unchecked(labels[i])
|
|
57
|
+
if (label.length == 0 || label.length > 63 || label.startsWith("xn--") || label.startsWith("-") || label.endsWith("-")) return __pulse_dynamic_invalid(index)
|
|
58
|
+
for (let j = 0; j < label.length; j++) {
|
|
59
|
+
const c = label.charCodeAt(j)
|
|
60
|
+
if (!((c >= 97 && c <= 122) || (c >= 48 && c <= 57) || c == 45)) return __pulse_dynamic_invalid(index)
|
|
61
|
+
if (c < 48 || c > 57) allNumeric = false
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (allNumeric) {
|
|
65
|
+
if (tls || hostname != dns || labels.length != 4) return __pulse_dynamic_invalid(index)
|
|
66
|
+
for (let i = 0; i < 4; i++) {
|
|
67
|
+
const label = unchecked(labels[i])
|
|
68
|
+
if (label.length > 3 || (label.length > 1 && label.startsWith("0"))) return __pulse_dynamic_invalid(index)
|
|
69
|
+
let octet = 0
|
|
70
|
+
for (let j = 0; j < label.length; j++) octet = octet * 10 + label.charCodeAt(j) - 48
|
|
71
|
+
if (octet > 255) return __pulse_dynamic_invalid(index)
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
// WHATWG numeric host aliases must not become a different DNS destination.
|
|
75
|
+
const last = unchecked(labels[labels.length - 1])
|
|
76
|
+
let numeric = true
|
|
77
|
+
for (let i = 0; i < last.length; i++) if (last.charCodeAt(i) < 48 || last.charCodeAt(i) > 57) numeric = false
|
|
78
|
+
if (numeric || last.startsWith("0x")) return __pulse_dynamic_invalid(index)
|
|
79
|
+
}
|
|
80
|
+
const host = hostname + (port == (tls ? 443 : 80) ? "" : ":" + port.toString())
|
|
81
|
+
const origin = scheme + "://" + host
|
|
82
|
+
let rest = url.substring(end)
|
|
83
|
+
const fragment = rest.indexOf("#")
|
|
84
|
+
if (fragment >= 0) rest = rest.substring(0, fragment)
|
|
85
|
+
if (!rest.startsWith("/")) rest = "/" + rest
|
|
86
|
+
__pulse_dynamic_url = origin + rest
|
|
87
|
+
if (__pulse_dynamic_url.length > PULSE_FASTLY_BUFFER_BYTES) return __pulse_dynamic_invalid(index)
|
|
88
|
+
const mapped = __pulse_dynamic_static(origin)
|
|
89
|
+
if (mapped.length > 0) return mapped
|
|
90
|
+
for (let i = 0; i < __pulse_dynamic_origins.length; i++) {
|
|
91
|
+
if (unchecked(__pulse_dynamic_origins[i]) == origin) return unchecked(__pulse_dynamic_names[i])
|
|
92
|
+
}
|
|
93
|
+
if (__pulse_dynamic_attempts >= 32) {
|
|
94
|
+
__pulse_fastly_fail(PULSE_ERROR_VALUE, 276, index); return ""
|
|
95
|
+
}
|
|
96
|
+
const name = "pulse-native-dynamic-" + (__pulse_dynamic_attempts++).toString()
|
|
97
|
+
const n = __pulse_fastly_utf8(name), target = __pulse_fastly_utf8(hostname + ":" + port.toString())
|
|
98
|
+
const h = __pulse_fastly_utf8(host), sni = __pulse_fastly_utf8(hostname)
|
|
99
|
+
const config = new StaticArray<u32>(27)
|
|
100
|
+
config[0] = <u32>changetype<usize>(h); config[1] = h.byteLength
|
|
101
|
+
let mask: u32 = 2
|
|
102
|
+
if (tls) {
|
|
103
|
+
mask |= 32 | 256 | 2048
|
|
104
|
+
config[7] = <u32>changetype<usize>(sni); config[8] = sni.byteLength
|
|
105
|
+
config[13] = <u32>changetype<usize>(sni); config[14] = sni.byteLength
|
|
106
|
+
}
|
|
107
|
+
if (!__pulse_dynamic_check(index)) return ""
|
|
108
|
+
__pulse_dynamic_status = __pulse_dynamic_register(changetype<usize>(n), n.byteLength, changetype<usize>(target), target.byteLength, mask, changetype<usize>(config))
|
|
109
|
+
if (!__pulse_dynamic_check(index)) return ""
|
|
110
|
+
if (__pulse_dynamic_status != 0) {
|
|
111
|
+
__pulse_fastly_fail(PULSE_ERROR_HOSTCALL, __pulse_dynamic_status == 5 ? 274 : 275, index); return ""
|
|
112
|
+
}
|
|
113
|
+
__pulse_dynamic_origins.push(origin); __pulse_dynamic_names.push(name)
|
|
114
|
+
return name
|
|
115
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const policy = Object.freeze({
|
|
6
|
+
optIn: 'bindings.dynamicBackends', maxRegistrationAttempts: 32,
|
|
7
|
+
identity: 'canonical origin with derived Host/TLS policy; request-local',
|
|
8
|
+
precedence: 'effect backend, configured canonical origin, dynamic registration',
|
|
9
|
+
urls: 'absolute ASCII HTTP/HTTPS DNS; canonical IPv4 over HTTP; ports 1–65535; no credentials, IDNA or IPv6',
|
|
10
|
+
tls: 'platform trust; URL DNS hostname for certificate verification and SNI',
|
|
11
|
+
hostPrerequisite: 'Fastly Dynamic Backends service capability',
|
|
12
|
+
stages: Object.freeze({ invalidUrl: 273, unavailable: 274, registrationFailed: 275, attemptsExhausted: 276 })
|
|
13
|
+
});
|
|
14
|
+
function active(bindings) { return (Array.isArray(bindings) ? bindings : bindings.backends || []).some(b => b.dynamic === true); }
|
|
15
|
+
function originMappings(origins) {
|
|
16
|
+
return Object.fromEntries(Object.entries(origins || {}).filter(([, name]) => typeof name === 'string' && name.trim()).map(([origin, name]) => [origin, name.trim()]).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0));
|
|
17
|
+
}
|
|
18
|
+
function describe(origins) { return Object.freeze({ ...policy, staticOrigins: Object.freeze(originMappings(origins)) }); }
|
|
19
|
+
function runtimeSource(backends, platform, origins) {
|
|
20
|
+
if (!active(backends)) return '';
|
|
21
|
+
const mappings = Object.entries(originMappings(origins));
|
|
22
|
+
return fs.readFileSync(path.join(__dirname, 'native-dynamic-backends.as.ts'), 'utf8') + '\n'
|
|
23
|
+
+ `function __pulse_dynamic_static(origin: string): string {\n${mappings.map(([origin, name]) => ` if (origin == ${JSON.stringify(origin)}) return ${JSON.stringify(name.trim())}`).join('\n')}\n return ""\n}\n`
|
|
24
|
+
+ `function __pulse_dynamic_check(index: i32): bool { return ${platform ? '__pulse_fetch_request_check(index) && __pulse_fetch_before_send(index)' : 'true'} }\n`;
|
|
25
|
+
}
|
|
26
|
+
function instrument(source, bindings, platform, origins) {
|
|
27
|
+
if (!active(bindings)) return source;
|
|
28
|
+
const replace = (before, after) => {
|
|
29
|
+
if (!source.includes(before)) throw new Error('Fastly dynamic backend boundary drift: ' + before.slice(0, 100));
|
|
30
|
+
source = source.replace(before, after);
|
|
31
|
+
};
|
|
32
|
+
if (platform) {
|
|
33
|
+
replace('__pulse_fastly_start_request(effectIndex, url, method, headers, body, __pulse_fastly_backend(effectIndex))',
|
|
34
|
+
'const backend = __pulse_dynamic_select(effectIndex, url); if (__pulse_fastly_last_error != 0) return; __pulse_fastly_start_request(effectIndex, __pulse_dynamic_url, method, headers, body, backend)');
|
|
35
|
+
} else {
|
|
36
|
+
replace('function host_effect_begin(effectIndex: i32, payload: i32): void {',
|
|
37
|
+
'function host_effect_begin(effectIndex: i32, payload: i32): void { if (__pulse_fastly_last_error != 0) return;');
|
|
38
|
+
replace(' const requestOut = __pulse_fastly_out_i32(); let status = fastly_http_req_new',
|
|
39
|
+
' const backend = __pulse_dynamic_select(effectIndex, url); if (__pulse_fastly_last_error != 0) return;\n const requestOut = __pulse_fastly_out_i32(); let status = fastly_http_req_new');
|
|
40
|
+
replace('const urlBytes = __pulse_fastly_utf8(url); status = fastly_http_req_uri_set',
|
|
41
|
+
'const urlBytes = __pulse_fastly_utf8(__pulse_dynamic_url); status = fastly_http_req_uri_set');
|
|
42
|
+
replace('const backend = __pulse_fastly_backend(effectIndex); if (backend.length == 0)', 'if (backend.length == 0)');
|
|
43
|
+
}
|
|
44
|
+
const entry = source.includes('export function pulse_fastly_handle(): void {') ? 'pulse_fastly_handle' : '_start';
|
|
45
|
+
replace(`export function ${entry}(): void {`, 'function __pulse_dynamic_start(): void {');
|
|
46
|
+
return source + '\n' + runtimeSource(bindings, platform, origins) + `\nexport function ${entry}(): void {
|
|
47
|
+
__pulse_dynamic_clear(); __pulse_dynamic_attempts = 0; __pulse_dynamic_status = 0
|
|
48
|
+
__pulse_dynamic_start()
|
|
49
|
+
__pulse_dynamic_clear()
|
|
50
|
+
}\n`;
|
|
51
|
+
}
|
|
52
|
+
module.exports = { active, policy, describe, instrument };
|