@tishlang/tish-format 1.0.12 → 1.0.13
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/Cargo.toml +49 -0
- package/LICENSE +13 -0
- package/README.md +138 -0
- package/bin/tish-format +0 -0
- package/crates/js_to_tish/Cargo.toml +11 -0
- package/crates/js_to_tish/README.md +18 -0
- package/crates/js_to_tish/src/error.rs +55 -0
- package/crates/js_to_tish/src/lib.rs +11 -0
- package/crates/js_to_tish/src/span_util.rs +35 -0
- package/crates/js_to_tish/src/transform/expr.rs +610 -0
- package/crates/js_to_tish/src/transform/stmt.rs +503 -0
- package/crates/js_to_tish/src/transform.rs +60 -0
- package/crates/tish/Cargo.toml +54 -0
- package/crates/tish/src/cargo_native_registry.rs +32 -0
- package/crates/tish/src/cli_help.rs +565 -0
- package/crates/tish/src/main.rs +781 -0
- package/crates/tish/src/repl_completion.rs +200 -0
- package/crates/tish/tests/cargo_example_compile.rs +67 -0
- package/crates/tish/tests/fixtures/cargo_example_project/Cargo.toml +3 -0
- package/crates/tish/tests/fixtures/cargo_example_project/crates/demo-shim/Cargo.toml +11 -0
- package/crates/tish/tests/fixtures/cargo_example_project/crates/demo-shim/src/lib.rs +12 -0
- package/crates/tish/tests/fixtures/cargo_example_project/package.json +10 -0
- package/crates/tish/tests/fixtures/cargo_example_project/src/main.tish +3 -0
- package/crates/tish/tests/integration_test.rs +1095 -0
- package/crates/tish/tests/run_optimize_stdout_parity.rs +50 -0
- package/crates/tish/tests/shortcircuit.rs +65 -0
- package/crates/tish_ast/Cargo.toml +9 -0
- package/crates/tish_ast/src/ast.rs +620 -0
- package/crates/tish_ast/src/lib.rs +5 -0
- package/crates/tish_build_utils/Cargo.toml +11 -0
- package/crates/tish_build_utils/src/lib.rs +577 -0
- package/crates/tish_builtins/Cargo.toml +20 -0
- package/crates/tish_builtins/src/array.rs +441 -0
- package/crates/tish_builtins/src/construct.rs +159 -0
- package/crates/tish_builtins/src/globals.rs +213 -0
- package/crates/tish_builtins/src/helpers.rs +35 -0
- package/crates/tish_builtins/src/lib.rs +16 -0
- package/crates/tish_builtins/src/math.rs +89 -0
- package/crates/tish_builtins/src/object.rs +36 -0
- package/crates/tish_builtins/src/string.rs +647 -0
- package/crates/tish_builtins/src/symbol.rs +83 -0
- package/crates/tish_bytecode/Cargo.toml +17 -0
- package/crates/tish_bytecode/src/chunk.rs +96 -0
- package/crates/tish_bytecode/src/compiler.rs +1760 -0
- package/crates/tish_bytecode/src/encoding.rs +100 -0
- package/crates/tish_bytecode/src/lib.rs +19 -0
- package/crates/tish_bytecode/src/opcode.rs +142 -0
- package/crates/tish_bytecode/src/peephole.rs +189 -0
- package/crates/tish_bytecode/src/serialize.rs +163 -0
- package/crates/tish_bytecode/tests/break_continue_bytecode.rs +44 -0
- package/crates/tish_bytecode/tests/constant_folding.rs +84 -0
- package/crates/tish_bytecode/tests/sort_optimization.rs +31 -0
- package/crates/tish_compile/Cargo.toml +26 -0
- package/crates/tish_compile/src/codegen.rs +5332 -0
- package/crates/tish_compile/src/infer.rs +292 -0
- package/crates/tish_compile/src/lib.rs +164 -0
- package/crates/tish_compile/src/resolve.rs +1388 -0
- package/crates/tish_compile/src/types.rs +501 -0
- package/crates/tish_compile_js/Cargo.toml +18 -0
- package/crates/tish_compile_js/examples/jsx_vdom_smoke.tish +8 -0
- package/crates/tish_compile_js/src/codegen.rs +871 -0
- package/crates/tish_compile_js/src/error.rs +20 -0
- package/crates/tish_compile_js/src/lib.rs +26 -0
- package/crates/tish_compile_js/src/tests_jsx.rs +350 -0
- package/crates/tish_compiler_wasm/Cargo.toml +21 -0
- package/crates/tish_compiler_wasm/src/lib.rs +57 -0
- package/crates/tish_compiler_wasm/src/resolve_virtual.rs +473 -0
- package/crates/tish_core/Cargo.toml +26 -0
- package/crates/tish_core/src/console_style.rs +160 -0
- package/crates/tish_core/src/json.rs +387 -0
- package/crates/tish_core/src/lib.rs +17 -0
- package/crates/tish_core/src/macros.rs +36 -0
- package/crates/tish_core/src/uri.rs +118 -0
- package/crates/tish_core/src/value.rs +696 -0
- package/crates/tish_core/src/vmref.rs +178 -0
- package/crates/tish_cranelift/Cargo.toml +19 -0
- package/crates/tish_cranelift/src/lib.rs +43 -0
- package/crates/tish_cranelift/src/link.rs +117 -0
- package/crates/tish_cranelift/src/lower.rs +85 -0
- package/crates/tish_cranelift_runtime/Cargo.toml +25 -0
- package/crates/tish_cranelift_runtime/src/lib.rs +45 -0
- package/crates/tish_eval/Cargo.toml +45 -0
- package/crates/tish_eval/src/eval.rs +3717 -0
- package/crates/tish_eval/src/http.rs +188 -0
- package/crates/tish_eval/src/lib.rs +99 -0
- package/crates/tish_eval/src/natives.rs +399 -0
- package/crates/tish_eval/src/promise.rs +179 -0
- package/crates/tish_eval/src/regex.rs +299 -0
- package/crates/tish_eval/src/timers.rs +120 -0
- package/crates/tish_eval/src/value.rs +318 -0
- package/crates/tish_eval/src/value_convert.rs +111 -0
- package/crates/tish_fmt/Cargo.toml +16 -0
- package/crates/tish_fmt/src/bin/tish-fmt.rs +41 -0
- package/crates/tish_fmt/src/lib.rs +2101 -0
- package/crates/tish_jsx_web/Cargo.toml +9 -0
- package/crates/tish_jsx_web/README.md +5 -0
- package/crates/tish_jsx_web/src/lib.rs +2 -0
- package/crates/tish_lexer/Cargo.toml +9 -0
- package/crates/tish_lexer/src/lib.rs +716 -0
- package/crates/tish_lexer/src/token.rs +163 -0
- package/crates/tish_lint/Cargo.toml +18 -0
- package/crates/tish_lint/src/bin/tish-lint.rs +195 -0
- package/crates/tish_lint/src/lib.rs +289 -0
- package/crates/tish_llvm/Cargo.toml +13 -0
- package/crates/tish_llvm/src/lib.rs +115 -0
- package/crates/tish_lsp/Cargo.toml +25 -0
- package/crates/tish_lsp/README.md +26 -0
- package/crates/tish_lsp/src/builtin_goto.rs +362 -0
- package/crates/tish_lsp/src/import_goto.rs +562 -0
- package/crates/tish_lsp/src/main.rs +1046 -0
- package/crates/tish_native/Cargo.toml +16 -0
- package/crates/tish_native/src/build.rs +427 -0
- package/crates/tish_native/src/config.rs +48 -0
- package/crates/tish_native/src/lib.rs +416 -0
- package/crates/tish_opt/Cargo.toml +13 -0
- package/crates/tish_opt/src/lib.rs +943 -0
- package/crates/tish_parser/Cargo.toml +11 -0
- package/crates/tish_parser/src/lib.rs +332 -0
- package/crates/tish_parser/src/parser.rs +2304 -0
- package/crates/tish_pg/Cargo.toml +34 -0
- package/crates/tish_pg/README.md +38 -0
- package/crates/tish_pg/src/error.rs +52 -0
- package/crates/tish_pg/src/lib.rs +955 -0
- package/crates/tish_resolve/Cargo.toml +13 -0
- package/crates/tish_resolve/src/lib.rs +3561 -0
- package/crates/tish_resolve/src/pos.rs +141 -0
- package/crates/tish_runtime/Cargo.toml +96 -0
- package/crates/tish_runtime/src/http.rs +1298 -0
- package/crates/tish_runtime/src/http_fetch.rs +471 -0
- package/crates/tish_runtime/src/http_hyper.rs +418 -0
- package/crates/tish_runtime/src/http_prefork.rs +189 -0
- package/crates/tish_runtime/src/lib.rs +1192 -0
- package/crates/tish_runtime/src/native_promise.rs +15 -0
- package/crates/tish_runtime/src/promise.rs +248 -0
- package/crates/tish_runtime/src/promise_io.rs +38 -0
- package/crates/tish_runtime/src/timers.rs +166 -0
- package/crates/tish_runtime/src/ws.rs +761 -0
- package/crates/tish_runtime/tests/fetch_readable_stream.rs +102 -0
- package/crates/tish_ui/Cargo.toml +17 -0
- package/crates/tish_ui/src/jsx.rs +682 -0
- package/crates/tish_ui/src/lib.rs +20 -0
- package/crates/tish_ui/src/runtime/hooks.rs +569 -0
- package/crates/tish_ui/src/runtime/mod.rs +180 -0
- package/crates/tish_vm/Cargo.toml +47 -0
- package/crates/tish_vm/src/lib.rs +39 -0
- package/crates/tish_vm/src/vm.rs +2192 -0
- package/crates/tish_vm/tests/fixtures/or_string_cmd.tish +2 -0
- package/crates/tish_vm/tests/lexical_scope_declare.rs +34 -0
- package/crates/tish_vm/tests/peephole_jump_chain_logical_or.rs +150 -0
- package/crates/tish_wasm/Cargo.toml +15 -0
- package/crates/tish_wasm/src/lib.rs +424 -0
- package/crates/tish_wasm_runtime/Cargo.toml +37 -0
- package/crates/tish_wasm_runtime/src/gpu.rs +413 -0
- package/crates/tish_wasm_runtime/src/lib.rs +42 -0
- package/crates/tishlang_cargo_bindgen/Cargo.toml +26 -0
- package/crates/tishlang_cargo_bindgen/src/classify.rs +263 -0
- package/crates/tishlang_cargo_bindgen/src/discover.rs +125 -0
- package/crates/tishlang_cargo_bindgen/src/infer.rs +382 -0
- package/crates/tishlang_cargo_bindgen/src/lib.rs +349 -0
- package/crates/tishlang_cargo_bindgen/src/main.rs +167 -0
- package/crates/tishlang_cargo_bindgen/src/metadata.rs +117 -0
- package/justfile +268 -0
- package/package.json +1 -1
- package/platform/darwin-arm64/tish-fmt +0 -0
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
//! Web Fetch–aligned Response, ReadableStream, reader.read(), text()/json().
|
|
2
|
+
|
|
3
|
+
use std::pin::Pin;
|
|
4
|
+
use std::sync::{Arc, Mutex};
|
|
5
|
+
|
|
6
|
+
use tishlang_core::VmRef;
|
|
7
|
+
|
|
8
|
+
use bytes::Bytes;
|
|
9
|
+
use futures::Stream;
|
|
10
|
+
use futures::StreamExt;
|
|
11
|
+
use tishlang_core::{NativeFn, ObjectMap, TishOpaque, TishPromise, Value};
|
|
12
|
+
|
|
13
|
+
use crate::http::{build_error_response, extract_body, extract_headers, extract_method};
|
|
14
|
+
|
|
15
|
+
// --- Promises (Send payloads only; Value built on awaiting thread) ---
|
|
16
|
+
|
|
17
|
+
struct FetchResponsePromise {
|
|
18
|
+
rx: Mutex<Option<tokio::sync::oneshot::Receiver<Result<reqwest::Response, String>>>>,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
impl TishPromise for FetchResponsePromise {
|
|
22
|
+
fn block_until_settled(&self) -> std::result::Result<Value, Value> {
|
|
23
|
+
let rx = self.rx.lock().unwrap().take();
|
|
24
|
+
if let Some(rx) = rx {
|
|
25
|
+
let r = crate::http::block_on_http(rx);
|
|
26
|
+
match r {
|
|
27
|
+
Ok(Ok(resp)) => Ok(response_value_from_reqwest(resp)),
|
|
28
|
+
Ok(Err(e)) => Ok(build_error_response(&e)),
|
|
29
|
+
Err(_) => Err(Value::String("Promise dropped".into())),
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
Err(Value::String("Promise already consumed".into()))
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
struct FetchAllResponsesPromise {
|
|
38
|
+
rx: Mutex<
|
|
39
|
+
Option<
|
|
40
|
+
tokio::sync::oneshot::Receiver<Result<Vec<Result<reqwest::Response, String>>, String>>,
|
|
41
|
+
>,
|
|
42
|
+
>,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
impl TishPromise for FetchAllResponsesPromise {
|
|
46
|
+
fn block_until_settled(&self) -> std::result::Result<Value, Value> {
|
|
47
|
+
let rx = self.rx.lock().unwrap().take();
|
|
48
|
+
if let Some(rx) = rx {
|
|
49
|
+
let r = crate::http::block_on_http(rx);
|
|
50
|
+
match r {
|
|
51
|
+
Ok(Ok(vec)) => {
|
|
52
|
+
let out: Vec<Value> = vec
|
|
53
|
+
.into_iter()
|
|
54
|
+
.map(|x| {
|
|
55
|
+
x.map(response_value_from_reqwest)
|
|
56
|
+
.unwrap_or_else(|e| build_error_response(&e))
|
|
57
|
+
})
|
|
58
|
+
.collect();
|
|
59
|
+
Ok(Value::Array(VmRef::new(out)))
|
|
60
|
+
}
|
|
61
|
+
Ok(Err(e)) => Ok(build_error_response(&e)),
|
|
62
|
+
Err(_) => Err(Value::String("Promise dropped".into())),
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
Err(Value::String("Promise already consumed".into()))
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
enum ReadChunk {
|
|
71
|
+
Done,
|
|
72
|
+
Bytes(Vec<u8>),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
struct ReadChunkPromise {
|
|
76
|
+
rx: Mutex<Option<tokio::sync::oneshot::Receiver<Result<ReadChunk, String>>>>,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
impl TishPromise for ReadChunkPromise {
|
|
80
|
+
fn block_until_settled(&self) -> std::result::Result<Value, Value> {
|
|
81
|
+
let rx = self.rx.lock().unwrap().take();
|
|
82
|
+
if let Some(rx) = rx {
|
|
83
|
+
let r = crate::http::block_on_http(rx);
|
|
84
|
+
match r {
|
|
85
|
+
Ok(Ok(ReadChunk::Done)) => {
|
|
86
|
+
let mut o = ObjectMap::default();
|
|
87
|
+
o.insert(Arc::from("done"), Value::Bool(true));
|
|
88
|
+
o.insert(Arc::from("value"), Value::Null);
|
|
89
|
+
Ok(Value::object(o))
|
|
90
|
+
}
|
|
91
|
+
Ok(Ok(ReadChunk::Bytes(b))) => {
|
|
92
|
+
let arr: Vec<Value> = b.iter().map(|u| Value::Number(*u as f64)).collect();
|
|
93
|
+
let mut o = ObjectMap::default();
|
|
94
|
+
o.insert(Arc::from("done"), Value::Bool(false));
|
|
95
|
+
o.insert(Arc::from("value"), Value::Array(VmRef::new(arr)));
|
|
96
|
+
Ok(Value::object(o))
|
|
97
|
+
}
|
|
98
|
+
Ok(Err(e)) => Err({
|
|
99
|
+
let mut obj = ObjectMap::default();
|
|
100
|
+
obj.insert(Arc::from("error"), Value::String(e.into()));
|
|
101
|
+
Value::object(obj)
|
|
102
|
+
}),
|
|
103
|
+
Err(_) => Err(Value::String("Promise dropped".into())),
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
Err(Value::String("Promise already consumed".into()))
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
struct JsonTextPromise {
|
|
112
|
+
rx: Mutex<Option<tokio::sync::oneshot::Receiver<Result<String, String>>>>,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
impl TishPromise for JsonTextPromise {
|
|
116
|
+
fn block_until_settled(&self) -> std::result::Result<Value, Value> {
|
|
117
|
+
let rx = self.rx.lock().unwrap().take();
|
|
118
|
+
if let Some(rx) = rx {
|
|
119
|
+
let r = crate::http::block_on_http(rx);
|
|
120
|
+
match r {
|
|
121
|
+
Ok(Ok(s)) => match tishlang_core::json_parse(&s) {
|
|
122
|
+
Ok(v) => Ok(v),
|
|
123
|
+
Err(e) => Err({
|
|
124
|
+
let mut obj = ObjectMap::default();
|
|
125
|
+
obj.insert(Arc::from("error"), Value::String(e.into()));
|
|
126
|
+
Value::object(obj)
|
|
127
|
+
}),
|
|
128
|
+
},
|
|
129
|
+
Ok(Err(e)) => Err({
|
|
130
|
+
let mut obj = ObjectMap::default();
|
|
131
|
+
obj.insert(Arc::from("error"), Value::String(e.into()));
|
|
132
|
+
Value::object(obj)
|
|
133
|
+
}),
|
|
134
|
+
Err(_) => Err(Value::String("Promise dropped".into())),
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
Err(Value::String("Promise already consumed".into()))
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// --- Body ---
|
|
143
|
+
|
|
144
|
+
pub struct HttpBody {
|
|
145
|
+
state: Mutex<BodyState>,
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
enum BodyState {
|
|
149
|
+
Fresh(Option<reqwest::Response>),
|
|
150
|
+
ReadInProgress,
|
|
151
|
+
Gone,
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
impl HttpBody {
|
|
155
|
+
pub fn new(response: reqwest::Response) -> Self {
|
|
156
|
+
Self {
|
|
157
|
+
state: Mutex::new(BodyState::Fresh(Some(response))),
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
fn take_stream(
|
|
162
|
+
&self,
|
|
163
|
+
) -> Result<Pin<Box<dyn Stream<Item = Result<Bytes, reqwest::Error>> + Send>>, String> {
|
|
164
|
+
let mut g = self.state.lock().unwrap();
|
|
165
|
+
match &mut *g {
|
|
166
|
+
BodyState::Fresh(r) => {
|
|
167
|
+
let resp = r
|
|
168
|
+
.take()
|
|
169
|
+
.ok_or_else(|| "Response body already consumed".to_string())?;
|
|
170
|
+
*g = BodyState::ReadInProgress;
|
|
171
|
+
Ok(Box::pin(resp.bytes_stream()))
|
|
172
|
+
}
|
|
173
|
+
BodyState::ReadInProgress => {
|
|
174
|
+
Err("ReadableStream is locked; getReader() already called".into())
|
|
175
|
+
}
|
|
176
|
+
BodyState::Gone => Err("Response body already consumed".into()),
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
pub fn take_text_async(
|
|
181
|
+
&self,
|
|
182
|
+
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<String, String>> + Send + '_>>
|
|
183
|
+
{
|
|
184
|
+
let resp = {
|
|
185
|
+
let mut g = self.state.lock().unwrap();
|
|
186
|
+
match &mut *g {
|
|
187
|
+
BodyState::Fresh(r) => match r.take() {
|
|
188
|
+
Some(resp) => {
|
|
189
|
+
*g = BodyState::Gone;
|
|
190
|
+
Ok(resp)
|
|
191
|
+
}
|
|
192
|
+
None => Err("Response body already consumed".into()),
|
|
193
|
+
},
|
|
194
|
+
BodyState::ReadInProgress => {
|
|
195
|
+
Err("Cannot call text(): body is locked by ReadableStreamDefaultReader".into())
|
|
196
|
+
}
|
|
197
|
+
BodyState::Gone => Err("Response body already consumed".into()),
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
Box::pin(async move {
|
|
201
|
+
match resp {
|
|
202
|
+
Ok(r) => r.text().await.map_err(|e| e.to_string()),
|
|
203
|
+
Err(e) => Err(e),
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
fn mark_gone_after_stream(&self) {
|
|
209
|
+
let mut g = self.state.lock().unwrap();
|
|
210
|
+
*g = BodyState::Gone;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
pub struct HttpReadableStream {
|
|
215
|
+
body: Arc<HttpBody>,
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
impl TishOpaque for HttpReadableStream {
|
|
219
|
+
fn type_name(&self) -> &'static str {
|
|
220
|
+
"ReadableStream"
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
fn as_any(&self) -> &dyn std::any::Any {
|
|
224
|
+
self
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
fn get_method(&self, name: &str) -> Option<NativeFn> {
|
|
228
|
+
if name != "getReader" {
|
|
229
|
+
return None;
|
|
230
|
+
}
|
|
231
|
+
let body = Arc::clone(&self.body);
|
|
232
|
+
Some(Arc::new(move |_args: &[Value]| match body.take_stream() {
|
|
233
|
+
Ok(stream) => {
|
|
234
|
+
let inner = Arc::new(tokio::sync::Mutex::new(StreamSlot { stream }));
|
|
235
|
+
Value::Opaque(Arc::new(HttpStreamReader {
|
|
236
|
+
inner,
|
|
237
|
+
body: Arc::clone(&body),
|
|
238
|
+
}))
|
|
239
|
+
}
|
|
240
|
+
Err(e) => {
|
|
241
|
+
let mut m = ObjectMap::default();
|
|
242
|
+
m.insert(Arc::from("error"), Value::String(e.into()));
|
|
243
|
+
Value::object(m)
|
|
244
|
+
}
|
|
245
|
+
}))
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
struct StreamSlot {
|
|
250
|
+
stream: Pin<Box<dyn Stream<Item = Result<Bytes, reqwest::Error>> + Send>>,
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
pub struct HttpStreamReader {
|
|
254
|
+
inner: Arc<tokio::sync::Mutex<StreamSlot>>,
|
|
255
|
+
body: Arc<HttpBody>,
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
impl TishOpaque for HttpStreamReader {
|
|
259
|
+
fn type_name(&self) -> &'static str {
|
|
260
|
+
"ReadableStreamDefaultReader"
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
fn as_any(&self) -> &dyn std::any::Any {
|
|
264
|
+
self
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
fn get_method(&self, name: &str) -> Option<NativeFn> {
|
|
268
|
+
if name != "read" {
|
|
269
|
+
return None;
|
|
270
|
+
}
|
|
271
|
+
let inner = Arc::clone(&self.inner);
|
|
272
|
+
let body = Arc::clone(&self.body);
|
|
273
|
+
Some(Arc::new(move |_args: &[Value]| {
|
|
274
|
+
let inner = Arc::clone(&inner);
|
|
275
|
+
let body = Arc::clone(&body);
|
|
276
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
277
|
+
crate::http::RUNTIME.with(|rt| {
|
|
278
|
+
rt.spawn(async move {
|
|
279
|
+
let mut slot = inner.lock().await;
|
|
280
|
+
match slot.stream.next().await {
|
|
281
|
+
None => {
|
|
282
|
+
body.mark_gone_after_stream();
|
|
283
|
+
let _ = tx.send(Ok(ReadChunk::Done));
|
|
284
|
+
}
|
|
285
|
+
Some(Ok(b)) => {
|
|
286
|
+
let _ = tx.send(Ok(ReadChunk::Bytes(b.to_vec())));
|
|
287
|
+
}
|
|
288
|
+
Some(Err(e)) => {
|
|
289
|
+
let _ = tx.send(Err(e.to_string()));
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
Value::Promise(Arc::new(ReadChunkPromise {
|
|
295
|
+
rx: Mutex::new(Some(rx)),
|
|
296
|
+
}))
|
|
297
|
+
}))
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
fn headers_to_value(headers: &reqwest::header::HeaderMap) -> Value {
|
|
302
|
+
let mut headers_obj: ObjectMap = ObjectMap::with_capacity(headers.len());
|
|
303
|
+
for (key, value) in headers.iter() {
|
|
304
|
+
if let Ok(v) = value.to_str() {
|
|
305
|
+
headers_obj.insert(Arc::from(key.as_str()), Value::String(v.into()));
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
Value::object(headers_obj)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
pub fn response_value_from_reqwest(response: reqwest::Response) -> Value {
|
|
312
|
+
let status = response.status().as_u16() as f64;
|
|
313
|
+
let ok = response.status().is_success();
|
|
314
|
+
let headers_val = headers_to_value(response.headers());
|
|
315
|
+
let body_holder = Arc::new(HttpBody::new(response));
|
|
316
|
+
let stream = Arc::new(HttpReadableStream {
|
|
317
|
+
body: Arc::clone(&body_holder),
|
|
318
|
+
});
|
|
319
|
+
let body_stream_val = Value::Opaque(stream);
|
|
320
|
+
let bh_text = Arc::clone(&body_holder);
|
|
321
|
+
let bh_json = Arc::clone(&body_holder);
|
|
322
|
+
let text_fn: NativeFn = Arc::new(move |_args: &[Value]| {
|
|
323
|
+
let bh = Arc::clone(&bh_text);
|
|
324
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
325
|
+
crate::http::RUNTIME.with(|rt| {
|
|
326
|
+
rt.spawn(async move {
|
|
327
|
+
let r = bh.take_text_async().await;
|
|
328
|
+
let _ = tx.send(r);
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
crate::promise_io::string_result_promise(rx)
|
|
332
|
+
});
|
|
333
|
+
let json_fn: NativeFn = Arc::new(move |_args: &[Value]| {
|
|
334
|
+
let bh = Arc::clone(&bh_json);
|
|
335
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
336
|
+
crate::http::RUNTIME.with(|rt| {
|
|
337
|
+
rt.spawn(async move {
|
|
338
|
+
let r = bh.take_text_async().await;
|
|
339
|
+
let _ = tx.send(r);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
Value::Promise(Arc::new(JsonTextPromise {
|
|
343
|
+
rx: Mutex::new(Some(rx)),
|
|
344
|
+
}))
|
|
345
|
+
});
|
|
346
|
+
let mut obj: ObjectMap = ObjectMap::default();
|
|
347
|
+
obj.insert(Arc::from("status"), Value::Number(status));
|
|
348
|
+
obj.insert(Arc::from("ok"), Value::Bool(ok));
|
|
349
|
+
obj.insert(Arc::from("headers"), headers_val);
|
|
350
|
+
obj.insert(Arc::from("body"), body_stream_val);
|
|
351
|
+
obj.insert(Arc::from("text"), Value::Function(text_fn));
|
|
352
|
+
obj.insert(Arc::from("json"), Value::Function(json_fn));
|
|
353
|
+
Value::object(obj)
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async fn send_request_parts(
|
|
357
|
+
url: String,
|
|
358
|
+
method: String,
|
|
359
|
+
headers: Vec<(String, String)>,
|
|
360
|
+
body: Option<String>,
|
|
361
|
+
) -> Result<reqwest::Response, String> {
|
|
362
|
+
let client = reqwest::Client::new();
|
|
363
|
+
let mut req = match method.as_str() {
|
|
364
|
+
"GET" => client.get(&url),
|
|
365
|
+
"POST" => client.post(&url),
|
|
366
|
+
"PUT" => client.put(&url),
|
|
367
|
+
"DELETE" => client.delete(&url),
|
|
368
|
+
"PATCH" => client.patch(&url),
|
|
369
|
+
"HEAD" => client.head(&url),
|
|
370
|
+
_ => client.get(&url),
|
|
371
|
+
};
|
|
372
|
+
for (key, value) in headers {
|
|
373
|
+
req = req.header(key, value);
|
|
374
|
+
}
|
|
375
|
+
if let Some(body) = body {
|
|
376
|
+
req = req.body(body);
|
|
377
|
+
}
|
|
378
|
+
req.send().await.map_err(|e| e.to_string())
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
pub fn fetch_promise_from_args(args: Vec<Value>) -> Value {
|
|
382
|
+
let url = match args.first() {
|
|
383
|
+
Some(Value::String(s)) => s.to_string(),
|
|
384
|
+
Some(v) => v.to_display_string(),
|
|
385
|
+
None => {
|
|
386
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
387
|
+
let _ = tx.send(Err("fetch requires a URL".into()));
|
|
388
|
+
return Value::Promise(Arc::new(FetchResponsePromise {
|
|
389
|
+
rx: Mutex::new(Some(rx)),
|
|
390
|
+
}));
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
let method = extract_method(args.get(1));
|
|
394
|
+
let headers = extract_headers(args.get(1));
|
|
395
|
+
let body = extract_body(args.get(1));
|
|
396
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
397
|
+
crate::http::RUNTIME.with(|rt| {
|
|
398
|
+
rt.spawn(async move {
|
|
399
|
+
let r = send_request_parts(url, method, headers, body).await;
|
|
400
|
+
let _ = tx.send(r);
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
Value::Promise(Arc::new(FetchResponsePromise {
|
|
404
|
+
rx: Mutex::new(Some(rx)),
|
|
405
|
+
}))
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
pub fn fetch_all_promise_from_args(args: Vec<Value>) -> Value {
|
|
409
|
+
let requests = match args.first() {
|
|
410
|
+
Some(Value::Array(arr)) => arr.borrow().clone(),
|
|
411
|
+
_ => {
|
|
412
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
413
|
+
let _ = tx.send(Err("fetchAll requires an array of request objects".into()));
|
|
414
|
+
return Value::Promise(Arc::new(FetchAllResponsesPromise {
|
|
415
|
+
rx: Mutex::new(Some(rx)),
|
|
416
|
+
}));
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
let mut parts: Vec<(String, String, Vec<(String, String)>, Option<String>)> = Vec::new();
|
|
420
|
+
for req in requests {
|
|
421
|
+
let (url, opt) = match &req {
|
|
422
|
+
Value::String(s) => (s.to_string(), None),
|
|
423
|
+
Value::Object(obj) => {
|
|
424
|
+
let obj_ref = obj.borrow();
|
|
425
|
+
match obj_ref
|
|
426
|
+
.strings
|
|
427
|
+
.get("url")
|
|
428
|
+
.map(|v| v.to_display_string())
|
|
429
|
+
{
|
|
430
|
+
Some(u) => (u, Some(req.clone())),
|
|
431
|
+
None => {
|
|
432
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
433
|
+
let _ =
|
|
434
|
+
tx.send(Err("Each request object must have a 'url' property".into()));
|
|
435
|
+
return Value::Promise(Arc::new(FetchAllResponsesPromise {
|
|
436
|
+
rx: Mutex::new(Some(rx)),
|
|
437
|
+
}));
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
_ => {
|
|
442
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
443
|
+
let _ = tx.send(Err(
|
|
444
|
+
"Each request must be a string URL or request object".into()
|
|
445
|
+
));
|
|
446
|
+
return Value::Promise(Arc::new(FetchAllResponsesPromise {
|
|
447
|
+
rx: Mutex::new(Some(rx)),
|
|
448
|
+
}));
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
let method = extract_method(opt.as_ref());
|
|
452
|
+
let headers = extract_headers(opt.as_ref());
|
|
453
|
+
let body = extract_body(opt.as_ref());
|
|
454
|
+
parts.push((url, method, headers, body));
|
|
455
|
+
}
|
|
456
|
+
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
457
|
+
crate::http::RUNTIME.with(|rt| {
|
|
458
|
+
rt.spawn(async move {
|
|
459
|
+
let futs: Vec<_> = parts
|
|
460
|
+
.into_iter()
|
|
461
|
+
.map(|(url, m, h, b)| send_request_parts(url, m, h, b))
|
|
462
|
+
.collect();
|
|
463
|
+
let results = futures::future::join_all(futs).await;
|
|
464
|
+
let mapped: Vec<Result<reqwest::Response, String>> = results.into_iter().collect();
|
|
465
|
+
let _ = tx.send(Ok(mapped));
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
Value::Promise(Arc::new(FetchAllResponsesPromise {
|
|
469
|
+
rx: Mutex::new(Some(rx)),
|
|
470
|
+
}))
|
|
471
|
+
}
|