-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathMongodbStore.js
More file actions
136 lines (122 loc) · 3.54 KB
/
Copy pathMongodbStore.js
File metadata and controls
136 lines (122 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const mongoose = require('mongoose');
const Store = require('./Store.js');
async function findOrCreate({ where, defaults }) {
return this.findOneAndUpdate(
where,
{ $setOnInsert: defaults },
{ upsert: true, new: true }, // return new doc if one is upserted
).exec();
}
const abuseSchema = new mongoose.Schema({
key: {
type: String,
required: true,
index: { unique: true },
},
counter: {
type: Number,
required: true,
default: 0,
},
dateEnd: {
type: Date,
required: true,
},
}, { timestamps: true });
abuseSchema.statics.findOrCreate = findOrCreate;
const abuseHistorySchema = new mongoose.Schema({
key: {
type: String,
required: true,
},
prefix: {
type: String,
required: false,
},
interval: {
type: Number,
required: true,
},
nbMax: {
type: Number,
required: true,
},
nbHit: {
type: Number,
required: true,
default: 0,
},
userId: {
type: String,
required: false,
},
ip: {
type: String,
required: false,
},
dateEnd: {
type: Date,
required: true,
},
}, { timestamps: true });
abuseHistorySchema.index({ key: 1, dateEnd: 1 }, { unique: true });
abuseHistorySchema.statics.findOrCreate = findOrCreate;
class MongodbStore extends Store {
constructor(mongodb, options = {}) {
super();
this.collectionName = options.collectionName || 'Ratelimits';
this.collectionAbuseName = options.collectionAbuseName || `${this.collectionName}Abuses`;
this.Ratelimits = mongodb.model(this.collectionName, abuseSchema);
this.Abuse = mongodb.model(this.collectionAbuseName, abuseHistorySchema);
}
async _increment(model, where, nb = 1, field) {
return model.findOneAndUpdate(where, { $inc: { [field]: nb } });
}
// remove all if time is passed
async _removeAll() {
await this.Ratelimits.deleteMany({ dateEnd: { $lte: Date.now() } });
}
async incr(key, options, weight) {
await this._removeAll();
const data = await this.Ratelimits.findOrCreate({
where: { key },
defaults: {
key,
dateEnd: Date.now() + options.interval,
counter: 0,
},
});
await this._increment(this.Ratelimits, { key }, weight, 'counter');
return {
counter: data.counter + weight,
dateEnd: data.dateEnd,
};
}
async decrement(key, options, weight) {
await this._increment(this.Ratelimits, { key }, -weight, 'counter');
}
async saveAbuse(options) {
const ratelimit = await this.Ratelimits.findOne({
key: options.key,
}).exec();
if (ratelimit) {
const { dateEnd } = ratelimit;
// create if not exist
await this.Abuse.findOrCreate({
where: { key: options.key, dateEnd },
defaults: {
key: options.key,
prefix: options.prefixKey,
interval: options.interval,
nbMax: options.max,
nbHit: options.max,
userId: options.user_id,
ip: options.ip,
dateEnd,
},
});
await this._increment(this.Abuse, { key: options.key, dateEnd }, 1, 'nbHit');
}
}
}
module.exports = MongodbStore;