Skip to content

Commit 6af1d00

Browse files
authored
Merge pull request #81 from dicej/remove-case-conversion-workarounds
remove workarounds for `quickjs-wasm-rs` case conversion
2 parents 7e83d1d + 09e0c80 commit 6af1d00

File tree

4 files changed

+42
-62
lines changed

4 files changed

+42
-62
lines changed

Cargo.lock

Lines changed: 18 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/spin-js-engine/src/js_sdk/modules/spinSdk.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ interface BaseHttpRequest {
99
method: string
1010
uri: string
1111
body?: ArrayBuffer
12-
}
13-
14-
interface SpinHttpRequest extends BaseHttpRequest {
15-
headers: Array<[string, string]>
12+
headers: Record<string, string>
1613
}
1714

1815
interface HttpRequest extends BaseHttpRequest {
19-
headers: Record<string, string>
2016
json:() => object
2117
text: () => string
2218
}
@@ -33,7 +29,7 @@ interface SpinSDK {
3329
config: SpinConfig
3430
/** @internal */
3531
http: {
36-
send: (arg0: SpinHttpRequest) => HttpResponse
32+
send: (arg0: BaseHttpRequest) => HttpResponse
3733
}
3834
redis: {
3935
get: (address: string, key: string) => ArrayBuffer
@@ -48,8 +44,9 @@ interface SpinSDK {
4844
}
4945

5046
interface FetchOptions {
51-
method: string
52-
headers: object
47+
method?: string
48+
headers?: Record<string, string>
49+
body?: ArrayBuffer
5350
}
5451

5552
interface FetchHeaders {
@@ -68,15 +65,11 @@ interface FetchResult {
6865

6966
/** @internal */
7067
function fetch(uri: string, options?: FetchOptions) {
71-
let reqHeaders: Array<[string, string]> = []
72-
if (options && options.headers) {
73-
reqHeaders = Object.entries(options.headers)
74-
}
7568
const { status, headers, body } = spinSdk.http.send({
7669
method: (options && options.method) || "GET",
7770
uri,
78-
...(options || {}),
79-
headers: reqHeaders
71+
headers: (options && options.headers) || {},
72+
body: options && options.body,
8073
})
8174
return Promise.resolve({
8275
status,
@@ -97,7 +90,7 @@ function fetch(uri: string, options?: FetchOptions) {
9790
declare global {
9891
const spinSdk: SpinSDK
9992
function fetch(uri: string, options?: object) : Promise<FetchResult>
100-
93+
10194
}
10295

10396
/** @internal */

crates/spin-js-engine/src/lib.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,7 @@ struct HttpRequest {
3737
method: String,
3838
uri: String,
3939
#[serde(default)]
40-
headers: Vec<(String, String)>,
41-
body: Option<ByteBuf>,
42-
}
43-
44-
// Seperate Structure to add headers without having to use serde
45-
// serde causes issues with headers as it sanitzes keys leading to headers being invalid
46-
// (eg) content-type becomes contentType.
47-
#[derive(Serialize, Deserialize, Debug)]
48-
struct JSHttpRequest {
49-
method: String,
50-
uri: String,
40+
headers: HashMap<String, String>,
5141
body: Option<ByteBuf>,
5242
}
5343

@@ -366,7 +356,7 @@ fn redis_sadd(context: &Context, _this: &Value, args: &[Value]) -> Result<Value>
366356

367357
fn redis_smembers(context: &Context, _this: &Value, args: &[Value]) -> Result<Value> {
368358
match args {
369-
[address, key, ] => {
359+
[address, key] => {
370360
let address = &deserialize_helper(address)?;
371361
let key = &deserialize_helper(key)?;
372362
let value = redis::smembers(address, key)
@@ -513,15 +503,19 @@ fn handle(request: Request) -> Result<Response> {
513503

514504
global.set_property("process", process)?;
515505

516-
let headers = context.object_value()?;
517-
for (key, value) in request.headers().iter() {
518-
let header_value = context.value_from_str(str::from_utf8(value.as_bytes())?)?;
519-
headers.set_property(key.as_str().to_owned(), header_value)?;
520-
}
521-
522-
let request = JSHttpRequest {
506+
let request = HttpRequest {
523507
method: request.method().as_str().to_owned(),
524508
uri: request.uri().to_string(),
509+
headers: request
510+
.headers()
511+
.iter()
512+
.map(|(k, v)| {
513+
Ok((
514+
k.as_str().to_owned(),
515+
str::from_utf8(v.as_bytes())?.to_owned(),
516+
))
517+
})
518+
.collect::<Result<_>>()?,
525519
body: request
526520
.into_body()
527521
.map(|bytes| ByteBuf::from(bytes.deref())),
@@ -530,7 +524,7 @@ fn handle(request: Request) -> Result<Response> {
530524
request.serialize(&mut serializer)?;
531525
let request_value = serializer.value;
532526
let body = request.body;
533-
request_value.set_property("headers", headers)?;
527+
534528
request_value.set_property(
535529
"text",
536530
context.wrap_callback({
@@ -578,7 +572,7 @@ fn handle(request: Request) -> Result<Response> {
578572
}
579573
}
580574

581-
Ok(builder.body(response.body.map(|buffer| buffer.into_vec().into()))?)
575+
Ok(builder.body(response.body.map(|buffer| buffer.to_vec().into()))?)
582576
}
583577

584578
fn deserialize_helper(value: &Value) -> Result<String> {

types/lib/modules/spinSdk.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ interface BaseHttpRequest {
55
method: string;
66
uri: string;
77
body?: ArrayBuffer;
8+
headers: Record<string, string>;
89
}
910
interface HttpRequest extends BaseHttpRequest {
10-
headers: Record<string, string>;
1111
json: () => object;
1212
text: () => string;
1313
}

0 commit comments

Comments
 (0)