Skip to content

Commit 75e7a5c

Browse files
committed
allow providing initial data for IncomingMessage
1 parent 8d5c892 commit 75e7a5c

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

example/uwebsockets_express.ts

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

1718
const app = expressify(uWS.App());
1819
app.use("/users", users);

src/Application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Application extends EventEmitter implements express.Application {
4141

4242
protected init() {
4343
this.uWSApp.any("/*", async (uwsResponse, uwsRequest) => {
44-
const req = new IncomingMessage(uwsRequest, uwsResponse, [], this);
44+
const req = new IncomingMessage(uwsRequest, uwsResponse, this);
4545
const res = new ServerResponse(uwsResponse, req, this);
4646

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

src/IncomingMessage.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,39 @@ export class IncomingMessage extends EventEmitter implements http.IncomingMessag
3535
public socket = new Socket(true, true);
3636

3737
#_originalUrlParsed: URL;
38+
private parameterNames: string[] = [];
3839

3940
constructor(
4041
private req: uWS.HttpRequest,
4142
private res: uWS.HttpResponse,
42-
private parameterNames: string[],
43-
private app: Application
43+
private app: Application,
44+
initialData?: {
45+
headers?: http.IncomingHttpHeaders;
46+
url?: string;
47+
method?: string;
48+
body?: any;
49+
query?: string;
50+
remoteAddress?: ArrayBuffer;
51+
}
4452
) {
4553
super();
4654

47-
this.req.forEach((key, value) => {
48-
this.headers[key] = value;
55+
this.headers = initialData?.headers || {};
4956

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

54-
this.url = this.req.getUrl();
55-
this.method = this.req.getMethod().toUpperCase();
61+
this.url = initialData?.url || this.req.getUrl();
62+
this.method = (initialData?.method || this.req.getMethod()).toUpperCase();
5663

57-
this._rawquery = this.req.getQuery();
58-
this._remoteAddress = this.res.getRemoteAddressAsText();
64+
this._rawquery = initialData?.query || this.req.getQuery();
65+
this._remoteAddress = initialData?.remoteAddress || this.res.getRemoteAddressAsText();
66+
67+
// workaround: also consider 'referrer'
68+
if (this.headers['referer']) {
69+
this.headers['referrer'] = this.headers['referer'];
70+
}
5971

6072
if (this._rawquery) {
6173
this.url += `?${this._rawquery}`;

0 commit comments

Comments
 (0)