formacao
This commit is contained in:
+76
-24
@@ -36,7 +36,7 @@ function ipKeyGenerator(ip, ipv6Subnet = 56) {
|
||||
if (address.is4()) return address.to4().correctForm();
|
||||
if (ipv6Subnet) {
|
||||
const subnet = new import_ip_address.Address6(`${ip}/${ipv6Subnet}`);
|
||||
return `${subnet.startAddress().correctForm()}/${ipv6Subnet}`;
|
||||
return subnet.networkForm();
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
@@ -201,6 +201,16 @@ var MemoryStore = class {
|
||||
// source/rate-limit.ts
|
||||
var import_node_net3 = require("node:net");
|
||||
|
||||
// source/console-logger.ts
|
||||
var ConsoleLogger = {
|
||||
warn(...args) {
|
||||
console.warn(...args.reverse());
|
||||
},
|
||||
error(...args) {
|
||||
console.error(...args.reverse());
|
||||
}
|
||||
};
|
||||
|
||||
// source/headers.ts
|
||||
var import_node_buffer = require("node:buffer");
|
||||
var import_node_crypto = require("node:crypto");
|
||||
@@ -548,7 +558,8 @@ var validations = {
|
||||
validate: true,
|
||||
headers: true,
|
||||
max: true,
|
||||
passOnStoreError: true
|
||||
passOnStoreError: true,
|
||||
logger: true
|
||||
};
|
||||
const validOptions = Object.keys(optionsMap).concat(
|
||||
"draft_polli_ratelimit_headers",
|
||||
@@ -661,7 +672,15 @@ var validations = {
|
||||
}
|
||||
}
|
||||
};
|
||||
var getValidations = (_enabled) => {
|
||||
function validateLogger(logger) {
|
||||
if (typeof logger !== "object" || typeof logger.error !== "function" || typeof logger.warn !== "function") {
|
||||
throw new TypeError(
|
||||
"Provided logger does not implement the Logger interface"
|
||||
);
|
||||
}
|
||||
}
|
||||
var getValidations = (_enabled, logger) => {
|
||||
validateLogger(logger);
|
||||
let enabled;
|
||||
if (typeof _enabled === "boolean") {
|
||||
enabled = {
|
||||
@@ -687,8 +706,8 @@ var getValidations = (_enabled) => {
|
||||
args
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof ChangeWarning) console.warn(error);
|
||||
else console.error(error);
|
||||
if (error instanceof ChangeWarning) logger.warn(error);
|
||||
else logger.error(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -741,7 +760,11 @@ var getOptionsFromConfig = (config) => {
|
||||
};
|
||||
var parseOptions = (passedOptions) => {
|
||||
const notUndefinedOptions = omitUndefinedProperties(passedOptions);
|
||||
const validations2 = getValidations(notUndefinedOptions?.validate ?? true);
|
||||
const logger = passedOptions.logger ?? ConsoleLogger;
|
||||
const validations2 = getValidations(
|
||||
notUndefinedOptions?.validate ?? true,
|
||||
logger
|
||||
);
|
||||
validations2.validationsConfig();
|
||||
validations2.knownOptions(passedOptions);
|
||||
validations2.draftPolliHeaders(
|
||||
@@ -816,7 +839,8 @@ var parseOptions = (passedOptions) => {
|
||||
notUndefinedOptions.store ?? new MemoryStore(validations2)
|
||||
),
|
||||
// Print an error to the console if a few known misconfigurations are detected.
|
||||
validations: validations2
|
||||
validations: validations2,
|
||||
logger
|
||||
};
|
||||
if (typeof config.store.increment !== "function" || typeof config.store.decrement !== "function" || typeof config.store.resetKey !== "function" || config.store.resetAll !== void 0 && typeof config.store.resetAll !== "function" || config.store.init !== void 0 && typeof config.store.init !== "function") {
|
||||
throw new TypeError(
|
||||
@@ -837,9 +861,29 @@ var rateLimit = (passedOptions) => {
|
||||
const options = getOptionsFromConfig(config);
|
||||
config.validations.creationStack(config.store);
|
||||
config.validations.unsharedStore(config.store);
|
||||
if (typeof config.store.init === "function") config.store.init(options);
|
||||
if (typeof config.store.init === "function") {
|
||||
try {
|
||||
const storeInit = config.store.init(options);
|
||||
if (storeInit instanceof Promise) {
|
||||
storeInit.catch(
|
||||
(error) => config.logger.error(
|
||||
error,
|
||||
"express-rate-limit: async error during store initialization."
|
||||
)
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
config.logger.error(
|
||||
error,
|
||||
"express-rate-limit: error during store initialization."
|
||||
);
|
||||
}
|
||||
}
|
||||
const middleware = handleAsyncErrors(
|
||||
async (request, response, next) => {
|
||||
const closePromise = config.skipFailedRequests && new Promise((resolve) => response.once("close", resolve));
|
||||
const finishPromise = (config.skipFailedRequests || config.skipSuccessfulRequests) && new Promise((resolve) => response.once("finish", resolve));
|
||||
const errorPromise = config.skipFailedRequests && new Promise((resolve) => response.once("error", resolve));
|
||||
const skip = await config.skip(request, response);
|
||||
if (skip) {
|
||||
next();
|
||||
@@ -855,9 +899,9 @@ var rateLimit = (passedOptions) => {
|
||||
resetTime = incrementResult.resetTime;
|
||||
} catch (error) {
|
||||
if (config.passOnStoreError) {
|
||||
console.error(
|
||||
"express-rate-limit: error from store, allowing request without rate-limiting.",
|
||||
error
|
||||
config.logger.error(
|
||||
error,
|
||||
"express-rate-limit: error from store, allowing request without rate-limiting."
|
||||
);
|
||||
next();
|
||||
return;
|
||||
@@ -918,22 +962,30 @@ var rateLimit = (passedOptions) => {
|
||||
}
|
||||
};
|
||||
if (config.skipFailedRequests) {
|
||||
response.on("finish", async () => {
|
||||
if (!await config.requestWasSuccessful(request, response))
|
||||
if (finishPromise) {
|
||||
void finishPromise.then(async () => {
|
||||
if (!await config.requestWasSuccessful(request, response))
|
||||
await decrementKey();
|
||||
});
|
||||
}
|
||||
if (closePromise) {
|
||||
void closePromise.then(async () => {
|
||||
if (!response.writableEnded) await decrementKey();
|
||||
});
|
||||
}
|
||||
if (errorPromise) {
|
||||
void errorPromise.then(async () => {
|
||||
await decrementKey();
|
||||
});
|
||||
response.on("close", async () => {
|
||||
if (!response.writableEnded) await decrementKey();
|
||||
});
|
||||
response.on("error", async () => {
|
||||
await decrementKey();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
if (config.skipSuccessfulRequests) {
|
||||
response.on("finish", async () => {
|
||||
if (await config.requestWasSuccessful(request, response))
|
||||
await decrementKey();
|
||||
});
|
||||
if (finishPromise) {
|
||||
void finishPromise.then(async () => {
|
||||
if (await config.requestWasSuccessful(request, response))
|
||||
await decrementKey();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
config.validations.disable();
|
||||
|
||||
Reference in New Issue
Block a user