This commit is contained in:
2026-03-03 15:23:00 +00:00
parent 5e3726de39
commit 8e223bfbec
3689 changed files with 955330 additions and 1011 deletions

View File

@@ -0,0 +1,104 @@
const methods = require('methods');
const deprecate = require('depd')('express');
const app = {};
const trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';
app.init = function init() {
this.cache = {};
this.settings = {};
this.engines = {};
this.defaultConfiguration();
};
app.defaultConfiguration = function defaultConfiguration() {
this.enable('x-powered-by');
this.set('etag', 'weak');
const env = process.env.NODE_ENV || 'development';
this.set('env', env);
this.set('query parser', 'extended');
this.set('subdomain offset', 2);
this.set('trust proxy', false);
Object.defineProperty(this.settings, trustProxyDefaultSymbol, {
configurable: true,
value: true
});
this.locals = Object.create(null);
this.mountpath = '/';
this.locals.settings = this.settings;
this.set('jsonp callback name', 'callback');
if (env === 'production') {
this.enable('view cache');
}
Object.defineProperty(this, 'router', {
get() {
throw new Error(
"'app.router' is deprecated!\nPlease see the 3.x to 4.x migration guide for details on how to update your app."
);
}
});
};
app.lazyrouter = () => {};
app.handle = () => {};
app.route = () => {};
app.render = () => {};
app.listen = () => {};
app.use = function use() {
return this;
};
app.engine = function engine() {
return this;
};
app.param = function param() {
return this;
};
app.set = function set(setting, val) {
if (arguments.length === 1) {
return this.settings[setting];
}
this.settings[setting] = val;
return this;
};
app.path = function path() {
return '';
};
app.enabled = function enabled(setting) {
return !!this.set(setting);
};
app.disabled = function disabled(setting) {
return !this.set(setting);
};
app.enable = function enable(setting) {
return this.set(setting, true);
};
app.disable = function disable(setting) {
return this.set(setting, false);
};
methods.forEach((method) => {
app[method] = function httpMethod() {
return this;
};
});
app.all = function all() {
return this;
};
app.del = deprecate.function(app.delete, 'app.del: Use app.delete instead');
module.exports = app;

View File

@@ -0,0 +1,35 @@
const { EventEmitter } = require('events');
const mixin = require('merge-descriptors');
const application = require('./mock-application');
const request = require('./mock-request');
const response = require('../mockResponse');
const expressResponse = {
createResponse: response.createResponse
};
function createApplication() {
const app = function () {};
mixin(app, EventEmitter.prototype, false);
mixin(app, application, false);
app.request = {
__proto__: request,
app
};
app.response = {
__proto__: expressResponse.createResponse(),
app
};
app.init();
return app;
}
module.exports = createApplication;
module.exports.application = application;
module.exports.request = request;
module.exports.response = expressResponse;

View File

@@ -0,0 +1,170 @@
const accepts = require('accepts');
const typeis = require('type-is');
const parseRange = require('range-parser');
const parse = require('parseurl');
const { isIP } = require('net');
const fresh = require('fresh');
const http = require('http');
const defineGetter = require('./utils/define-getter');
const req = {
__proto__: http.IncomingMessage.prototype
};
req.header = function header(name) {
const headerName = name.toLowerCase();
switch (headerName) {
case 'referer':
case 'referrer':
return this.headers.referrer || this.headers.referer;
default:
return this.headers[headerName];
}
};
req.get = req.header;
req.accepts = function acceptTypes(...args) {
const accept = accepts(this);
return accept.types(...args);
};
req.acceptsEncodings = function acceptsEncodings(...args) {
const accept = accepts(this);
return accept.encodings(...args);
};
req.acceptsEncoding = req.acceptsEncodings;
req.acceptsCharsets = function acceptsCharsets(...args) {
const accept = accepts(this);
return accept.charsets(...args);
};
req.acceptsCharset = req.acceptsCharsets;
req.acceptsLanguages = function acceptsLanguages(...args) {
const accept = accepts(this);
return accept.languages(...args);
};
req.acceptsLanguage = req.acceptsLanguages;
req.range = function getRange(size) {
const range = this.get('Range');
if (!range) {
return undefined;
}
return parseRange(size, range);
};
req.param = function param(name, defaultValue) {
const params = this.params || {};
const body = this.body || {};
const query = this.query || {};
if (params[name] !== null && {}.hasOwnProperty.call(params, name)) {
return params[name];
}
if (body[name] !== null) {
return body[name];
}
if (query[name] !== null) {
return query[name];
}
return defaultValue;
};
req.is = function is(...args) {
let types = args;
if (Array.isArray(args[0])) {
types = args[0];
}
return typeis(this, types);
};
defineGetter(req, 'protocol', function protocol() {
let { proto } = this.options;
proto = this.get('X-Forwarded-Proto') || proto;
return proto.split(/\s*,\s*/)[0];
});
defineGetter(req, 'secure', function secure() {
return this.protocol === 'https';
});
defineGetter(req, 'ip', function ip() {
return this.options.ip || '127.0.0.1';
});
defineGetter(req, 'ips', function ips() {
return [this.ip];
});
defineGetter(req, 'subdomains', function subdomains() {
const { hostname } = this;
if (!hostname) {
return [];
}
const offset = this.app.get('subdomain offset');
const domains = !isIP(hostname) ? hostname.split('.').reverse() : [hostname];
return domains.slice(offset);
});
defineGetter(req, 'path', function path() {
return parse(this).pathname;
});
defineGetter(req, 'hostname', function hostname() {
let host = this.get('X-Forwarded-Host');
if (!host) {
host = this.get('Host');
}
if (!host) {
return undefined;
}
const offset = host[0] === '[' ? host.indexOf(']') + 1 : 0;
const index = host.indexOf(':', offset);
return index < 0 ? host.substring(0, index) : host;
});
defineGetter(req, 'host', function host() {
return this.hostname;
});
defineGetter(req, 'fresh', function isFresh() {
const { method } = this;
const { statusCode } = this.res;
if (method !== 'GET' && method !== 'HEAD') {
return false;
}
if ((statusCode >= 200 && statusCode < 300) || statusCode === 304) {
return fresh(this.headers, this.res._headers || {});
}
return false;
});
defineGetter(req, 'stale', function stale() {
return !this.fresh;
});
defineGetter(req, 'xhr', function xhr() {
const val = this.get('X-Requested-With') || '';
return val.toLowerCase() === 'xmlhttprequest';
});
module.exports = req;

View File

@@ -0,0 +1,9 @@
function defineGetter(obj, name, getter) {
Object.defineProperty(obj, name, {
configurable: true,
enumerable: true,
get: getter
});
}
module.exports = defineGetter;