Skip to content

Commit 87b258f

Browse files
authored
feat: make header input more flexible (#42)
1 parent 66ab114 commit 87b258f

File tree

15 files changed

+289
-1155
lines changed

15 files changed

+289
-1155
lines changed

__test__/headers.spec.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,14 @@ test('includes iterator methods', (t) => {
8181
['content-type', 'application/json', headers]
8282
])
8383
})
84+
85+
test('construct from object', (t) => {
86+
const headers = new Headers({
87+
'Content-Type': 'application/json',
88+
'Accept': ['application/json', 'text/html']
89+
})
90+
t.assert(headers.has('Content-Type'))
91+
t.is(headers.get('Content-Type'), 'application/json')
92+
t.assert(headers.has('Accept'))
93+
t.deepEqual(headers.getAll('Accept'), ['application/json', 'text/html'])
94+
})

__test__/request.spec.mjs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ test('full construction', (t) => {
2222
url: 'http://example.com/test.php',
2323
body: Buffer.from('Hello, from Node.js!'),
2424
headers: {
25-
'Content-Type': ['application/json'],
26-
'X-Custom-Header': ['CustomValue']
25+
'Content-Type': 'application/json',
26+
'Accept': ['application/json', 'text/html'],
27+
'X-Custom-Header': 'CustomValue'
2728
}
2829
})
2930

@@ -32,7 +33,33 @@ test('full construction', (t) => {
3233
t.assert(req.body instanceof Buffer)
3334
t.is(req.body.toString('utf8'), 'Hello, from Node.js!')
3435
t.assert(req.headers instanceof Headers)
35-
t.is(req.headers.size, 2)
36+
t.is(req.headers.size, 3)
3637
t.is(req.headers.get('Content-Type'), 'application/json')
38+
t.deepEqual(req.headers.getAll('Accept'), ['application/json', 'text/html'])
39+
t.is(req.headers.get('X-Custom-Header'), 'CustomValue')
40+
})
41+
42+
test('construction with headers instance', (t) => {
43+
const headers = new Headers({
44+
'Content-Type': 'application/json',
45+
'Accept': ['application/json', 'text/html'],
46+
'X-Custom-Header': 'CustomValue'
47+
})
48+
49+
const req = new Request({
50+
method: 'POST',
51+
url: 'http://example.com/test.php',
52+
body: Buffer.from('Hello, from Node.js!'),
53+
headers
54+
})
55+
56+
t.is(req.method, 'POST')
57+
t.is(req.url, 'http://example.com/test.php')
58+
t.assert(req.body instanceof Buffer)
59+
t.is(req.body.toString('utf8'), 'Hello, from Node.js!')
60+
t.assert(req.headers instanceof Headers)
61+
t.is(req.headers.size, 3)
62+
t.is(req.headers.get('Content-Type'), 'application/json')
63+
t.deepEqual(req.headers.getAll('Accept'), ['application/json', 'text/html'])
3764
t.is(req.headers.get('X-Custom-Header'), 'CustomValue')
3865
})

__test__/response.spec.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ test('Full Response construction', (t) => {
2424
const res = new Response({
2525
status: 200,
2626
headers: {
27-
'Content-Type': ['application/json'],
28-
'X-Custom-Header': ['CustomValue']
27+
'Content-Type': 'application/json',
28+
'Accept': ['application/json', 'text/plain'],
29+
'X-Custom-Header': 'CustomValue'
2930
},
3031
body: Buffer.from(json),
3132
log: Buffer.from('Hello, from error_log!'),
@@ -35,6 +36,7 @@ test('Full Response construction', (t) => {
3536
t.is(res.status, 200)
3637
t.assert(res.headers instanceof Headers)
3738
t.deepEqual(res.headers.get('Content-Type'), 'application/json')
39+
t.deepEqual(res.headers.getAll('Accept'), ['application/json', 'text/plain'])
3840
t.assert(res.body instanceof Buffer)
3941
t.deepEqual(res.body.toString(), json)
4042
t.assert(res.log instanceof Buffer)

crates/lang_handler/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ path = "src/lib.rs"
1010

1111
[features]
1212
default = []
13-
c = []
13+
napi = ["dep:napi", "dep:napi-build"]
1414

1515
[build-dependencies]
16-
cbindgen = "0.28.0"
16+
napi-build = { version = "2.0.1", optional = true }
1717

1818
[dependencies]
1919
bytes = "1.10.1"
20+
napi = { version = "2.16.17", optional = true }
2021
regex = "1.11.1"
2122
url = "2.5.4"

crates/lang_handler/build.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
1-
extern crate cbindgen;
2-
3-
use std::env;
4-
use std::path::PathBuf;
1+
#[cfg(feature = "napi")]
2+
extern crate napi_build;
53

64
fn main() {
7-
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
8-
9-
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap())
10-
.join("../../..")
11-
.canonicalize()
12-
.unwrap();
13-
14-
let header_path = out_dir.join("lang_handler.h");
15-
16-
cbindgen::Builder::new()
17-
.with_crate(crate_dir)
18-
.with_include_guard("LANG_HANDLER_H")
19-
.with_language(cbindgen::Language::C)
20-
.generate()
21-
.expect("Unable to generate bindings")
22-
.write_to_file(header_path);
5+
#[cfg(feature = "napi")]
6+
napi_build::setup();
237
}

0 commit comments

Comments
 (0)