[ci] format

This commit is contained in:
matthewp 2022-07-22 19:24:58 +00:00 committed by fredkbot
parent 4ca6a0933d
commit 4392083cca
4 changed files with 13 additions and 14 deletions

View file

@ -14,7 +14,7 @@ function createRequestFromNodeRequest(req: IncomingMessage, body?: string): Requ
let request = new Request(url, { let request = new Request(url, {
method: req.method || 'GET', method: req.method || 'GET',
headers: new Headers(entries), headers: new Headers(entries),
body body,
}); });
if (req.socket?.remoteAddress) { if (req.socket?.remoteAddress) {
Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress); Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress);
@ -27,23 +27,23 @@ export class NodeApp extends App {
return super.match(req instanceof Request ? req : createRequestFromNodeRequest(req)); return super.match(req instanceof Request ? req : createRequestFromNodeRequest(req));
} }
render(req: IncomingMessage | Request) { render(req: IncomingMessage | Request) {
if('on' in req) { if ('on' in req) {
let body: string | undefined = undefined; let body: string | undefined = undefined;
let reqBodyComplete = new Promise((resolve, reject) => { let reqBodyComplete = new Promise((resolve, reject) => {
req.on('data', d => { req.on('data', (d) => {
if(body === undefined) { if (body === undefined) {
body = ''; body = '';
} }
if(d instanceof Buffer) { if (d instanceof Buffer) {
body += d.toString('utf-8'); body += d.toString('utf-8');
} else if(typeof d === 'string') { } else if (typeof d === 'string') {
body += d; body += d;
} }
}); });
req.on('end', () => { req.on('end', () => {
resolve(body); resolve(body);
}); });
req.on('error', err => { req.on('error', (err) => {
reject(err); reject(err);
}); });
}); });

View file

@ -29,8 +29,8 @@ export function createExports(manifest: SSRManifest) {
} else if (next) { } else if (next) {
return next(); return next();
} }
} catch(err: unknown) { } catch (err: unknown) {
if(!res.headersSent) { if (!res.headersSent) {
res.writeHead(500, `Server error`); res.writeHead(500, `Server error`);
res.end(); res.end();
} }

View file

@ -2,7 +2,6 @@ import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse, toPromise } from './test-utils.js'; import { loadFixture, createRequestAndResponse, toPromise } from './test-utils.js';
import { expect } from 'chai'; import { expect } from 'chai';
describe('API routes', () => { describe('API routes', () => {
/** @type {import('./test-utils').Fixture} */ /** @type {import('./test-utils').Fixture} */
let fixture; let fixture;
@ -23,13 +22,13 @@ describe('API routes', () => {
let { req, res, done } = createRequestAndResponse({ let { req, res, done } = createRequestAndResponse({
method: 'POST', method: 'POST',
url: '/recipes' url: '/recipes',
}); });
handler(req, res); handler(req, res);
req.send(JSON.stringify({ id: 2 })); req.send(JSON.stringify({ id: 2 }));
let [ buffer ] = await done; let [buffer] = await done;
let json = JSON.parse(buffer.toString('utf-8')); let json = JSON.parse(buffer.toString('utf-8'));
expect(json.length).to.equal(1); expect(json.length).to.equal(1);
expect(json[0].name).to.equal('Broccoli Soup'); expect(json[0].name).to.equal('Broccoli Soup');

View file

@ -23,7 +23,7 @@ export function createRequestAndResponse(reqOptions) {
let res = httpMocks.createResponse({ let res = httpMocks.createResponse({
eventEmitter: EventEmitter, eventEmitter: EventEmitter,
req req,
}); });
let done = toPromise(res); let done = toPromise(res);
@ -32,7 +32,7 @@ export function createRequestAndResponse(reqOptions) {
} }
export function toPromise(res) { export function toPromise(res) {
return new Promise(resolve => { return new Promise((resolve) => {
res.on('end', () => { res.on('end', () => {
let chunks = res._getChunks(); let chunks = res._getChunks();
resolve(chunks); resolve(chunks);