Skip to content

Commit cda5ba6

Browse files
committed
allow providing initial data for IncomingMessage
1 parent a56a6b6 commit cda5ba6

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

example/uwebsockets_express.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const PORT = 8080;
77
// external express router
88
const users = express.Router();
99
users.get("/", (req, res) => res.json({ username: "Jake Badlands" }));
10+
users.get("/param/:id", (req, res) => res.json({ id: req.params.id }));
1011

1112
const app = expressify(uWS.App());
1213
app.use("/users", users);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uwebsockets-express",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Express API compatibility layer for uWebSockets.js",
55
"main": "./build/index.cjs",
66
"module": "./build/index.mjs",

src/Application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class Application extends EventEmitter {
3535
protected handler = async (uwsResponse: uWS.HttpResponse, uwsRequest: uWS.HttpRequest) => {
3636
const url = uwsRequest.getUrl();
3737

38-
const req = new IncomingMessage(uwsRequest, uwsResponse, [], this);
38+
const req = new IncomingMessage(uwsRequest, uwsResponse, this);
3939
const res = new ServerResponse(uwsResponse, req, this);
4040

4141
uwsResponse.onAborted(onAbort.bind(undefined, req, res));

src/IncomingMessage.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,38 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
3333
public socket = new Socket(false, true);
3434

3535
#_originalUrlParsed: URL;
36+
private parameterNames: string[] = [];
3637

3738
constructor(
3839
private req: uWS.HttpRequest,
3940
private res: uWS.HttpResponse,
40-
private parameterNames: string[],
41-
private app: Application
41+
private app: Application,
42+
initialData?: {
43+
headers?: http.IncomingHttpHeaders;
44+
url?: string;
45+
method?: string;
46+
body?: any;
47+
query?: string;
48+
remoteAddress?: ArrayBuffer;
49+
}
4250
) {
4351
super();
4452

45-
this._headers = {};
46-
this.req.forEach((key, value) => {
47-
this._headers[key] = value;
53+
this._headers = initialData?.headers || {};
4854

49-
// workaround: also consider 'referrer'
50-
if (key === "referer") {
51-
this._headers['referrer'] = value;
52-
}
53-
});
55+
if (!initialData?.headers) {
56+
this.req.forEach((key, value) => this._headers[key] = value);
57+
}
5458

55-
this.url = this.req.getUrl();
56-
this.method = this.req.getMethod().toUpperCase();
59+
this.url = initialData?.url || this.req.getUrl();
60+
this.method = (initialData?.method || this.req.getMethod()).toUpperCase();
5761

58-
this._rawquery = this.req.getQuery();
59-
this._remoteAddress = this.res.getRemoteAddressAsText();
62+
this._rawquery = initialData?.query || this.req.getQuery();
63+
this._remoteAddress = initialData?.remoteAddress || this.res.getRemoteAddressAsText();
64+
65+
if (this._headers['referer']) {
66+
this._headers['referrer'] = this._headers['referer'];
67+
}
6068

6169
if (this._rawquery) {
6270
this.url += `?${this._rawquery}`;

0 commit comments

Comments
 (0)