Skip to content

Commit b9f1d7c

Browse files
authored
Merge pull request #255 from nyannyacha/fix-oak-13
fix(sb_core): adjusting http handler to be compatible with `[email protected]`
2 parents ced19dc + cb3301e commit b9f1d7c

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

crates/sb_core/js/http.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ async function serve(args1, args2) {
7878
opts['handler'] = args1;
7979
} else if (typeof args1 === 'object' && typeof args2 === 'function') {
8080
opts['handler'] = args2;
81-
} else {
82-
if (typeof handler !== 'function') {
83-
throw new TypeError('A handler function must be provided.');
81+
} else if (typeof args1 === 'object') {
82+
if (typeof args1['handler'] === 'function') {
83+
opts['handler'] = args1['handler'];
84+
}
85+
if (typeof args1['onListen'] === 'function') {
86+
opts['onListen'] = args1['onListen'];
8487
}
88+
} else {
89+
throw new TypeError('A handler function must be provided.');
8590
}
8691

8792
let serve;
@@ -90,7 +95,14 @@ async function serve(args1, args2) {
9095
serve = serveHttp(conn);
9196
for await (const e of serve) {
9297
try {
93-
const res = await opts['handler'](e.request);
98+
const res = await opts['handler'](e.request, {
99+
remoteAddr: {
100+
port: opts.port,
101+
hostname: opts.hostname,
102+
transport: opts.transport
103+
}
104+
});
105+
94106
e.respondWith(res);
95107
} catch (error) {
96108
console.error(error);
@@ -100,6 +112,11 @@ async function serve(args1, args2) {
100112
};
101113

102114
const finished = (async () => {
115+
opts['onListen']?.({
116+
hostname: opts.hostname,
117+
port: opts.port
118+
});
119+
103120
for await (const conn of listener) {
104121
handleHttp(conn);
105122
}

0 commit comments

Comments
 (0)