@tryghost/express-test 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/Request.js +78 -10
  2. package/package.json +10 -8
package/lib/Request.js CHANGED
@@ -1,8 +1,19 @@
1
- const {default: reqresnext} = require('reqresnext');
1
+ const http = require('http');
2
+ const {Writable, Readable} = require('stream');
3
+ const express = require('express');
2
4
  const {CookieAccessInfo} = require('cookiejar');
3
5
  const {parse} = require('url');
4
6
  const {isJSON, attachFile} = require('./utils');
5
- const {Readable} = require('stream');
7
+
8
+ class MockSocket extends Writable {
9
+ constructor() {
10
+ super();
11
+ this.remoteAddress = '127.0.0.1';
12
+ }
13
+ _write(chunk, encoding, callback) {
14
+ callback();
15
+ }
16
+ }
6
17
 
7
18
  class RequestOptions {
8
19
  constructor({method, url, headers, body} = {}) {
@@ -70,11 +81,8 @@ class Request {
70
81
  stream(readableStream) {
71
82
  this.reqOptions.body = undefined;
72
83
 
73
- // reqresnext doesn't support streaming, which we need for file uploads. So we need to add some new methods.
74
- // ExpressRequest inherits from http.IncomingMessage which inherits from stream.Readable
75
- // It is that stream that is eventually read by middlewares and converted as JSON, as text, as form data etc.
76
- // Currently the Express middlewares (and multer for file uploads) use the pipe method and/or the event listeners, so we don't need to override other methods
77
- // If we need other methods, we only need to map them to the readable stream.
84
+ // Override stream methods on the request to delegate to the provided readable stream.
85
+ // Express middlewares (and multer for file uploads) use pipe/event listeners to consume the body.
78
86
  this.reqOptions.methodOverrides = {
79
87
  pipe: (destination) => {
80
88
  readableStream.pipe(destination);
@@ -91,9 +99,15 @@ class Request {
91
99
  get readable() {
92
100
  return readableStream.readable;
93
101
  },
102
+ get readableEnded() {
103
+ return readableStream.readableEnded;
104
+ },
94
105
  pause: () => {
95
106
  readableStream.pause();
96
107
  },
108
+ resume: () => {
109
+ readableStream.resume();
110
+ },
97
111
  read: () => {
98
112
  return readableStream.read(...arguments);
99
113
  }
@@ -137,10 +151,64 @@ class Request {
137
151
  _getReqRes() {
138
152
  const {app, reqOptions} = this;
139
153
 
140
- const {req, res} = reqresnext({...reqOptions, app}, {app});
154
+ // Create proper Node.js req/res objects using built-in http module.
155
+ // MockSocket provides a writable stream so that res.end() properly emits 'finish'.
156
+ const socket = new MockSocket();
157
+ const hasStreamOverrides = !!this.reqOptions.methodOverrides;
158
+
159
+ // When streaming body data, the socket must appear readable so that
160
+ // body-parser's on-finished check doesn't skip the request as "already finished".
161
+ if (hasStreamOverrides) {
162
+ Object.defineProperty(socket, 'readable', {value: true});
163
+ }
164
+
165
+ const req = new http.IncomingMessage(socket);
166
+ req.method = reqOptions.method;
167
+ req.url = reqOptions.url;
168
+ req.headers = {};
169
+ for (const key of Object.keys(reqOptions.headers)) {
170
+ req.headers[key.toLowerCase()] = reqOptions.headers[key];
171
+ }
172
+ req.headers.host = req.headers.host || 'localhost';
173
+ req.body = reqOptions.body;
174
+ req.app = app;
175
+
176
+ // When body is pre-parsed (e.g. JSON object), mark it so body-parser skips parsing
177
+ if (reqOptions.body !== undefined) {
178
+ req._body = true;
179
+ }
180
+
181
+ const res = new http.ServerResponse(req);
182
+ res.assignSocket(socket);
183
+
184
+ // Apply Express prototypes so res.send(), res.json(), etc. are available
185
+ Object.setPrototypeOf(req, express.request);
186
+ Object.setPrototypeOf(res, express.response);
187
+
188
+ res.req = req;
189
+ req.res = res;
190
+ res.app = app;
191
+
192
+ // Track written body data for _buildResponse.
193
+ // Express calls res.write() then res.end() separately, so we must capture both.
194
+ let bodyChunks = [];
195
+ const originalWrite = res.write.bind(res);
196
+ res.write = function (chunk, encoding, cb) {
197
+ if (chunk !== null && chunk !== undefined) {
198
+ bodyChunks.push(Buffer.from(chunk, encoding));
199
+ }
200
+ return originalWrite(chunk, encoding, cb);
201
+ };
202
+ const originalEnd = res.end.bind(res);
203
+ res.end = function (chunk, encoding, cb) {
204
+ if (chunk !== null && chunk !== undefined) {
205
+ bodyChunks.push(Buffer.from(chunk, encoding));
206
+ }
207
+ res.body = Buffer.concat(bodyChunks);
208
+ return originalEnd(chunk, encoding, cb);
209
+ };
141
210
 
142
- if (this.reqOptions.methodOverrides) {
143
- // Copies all properties from original to copy, including getters and setters
211
+ if (hasStreamOverrides) {
144
212
  const props = Object.keys(this.reqOptions.methodOverrides);
145
213
  for (const prop of props) {
146
214
  const descriptor = Object.getOwnPropertyDescriptor(this.reqOptions.methodOverrides, prop);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/express-test",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/TryGhost/framework.git",
@@ -23,18 +23,20 @@
23
23
  "access": "public"
24
24
  },
25
25
  "devDependencies": {
26
- "c8": "10.1.3",
26
+ "c8": "11.0.0",
27
27
  "express": "5.2.1",
28
28
  "express-session": "1.19.0",
29
29
  "mocha": "11.7.5",
30
- "multer": "^2.0.0",
30
+ "multer": "2.1.0",
31
31
  "sinon": "21.0.1"
32
32
  },
33
+ "peerDependencies": {
34
+ "express": "^4.0.0 || ^5.0.0"
35
+ },
33
36
  "dependencies": {
34
- "@tryghost/jest-snapshot": "^1.2.0",
35
- "cookiejar": "^2.1.3",
36
- "form-data": "^4.0.2",
37
- "mime-types": "^3.0.1",
38
- "reqresnext": "^1.7.0"
37
+ "@tryghost/jest-snapshot": "^2.0.0",
38
+ "cookiejar": "2.1.4",
39
+ "form-data": "4.0.5",
40
+ "mime-types": "3.0.2"
39
41
  }
40
42
  }