@proteinjs/user-server 1.22.4 → 1.23.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.
- package/CHANGELOG.md +18 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/src/routes/executePasswordReset.js +3 -1
- package/dist/src/routes/executePasswordReset.js.map +1 -1
- package/dist/src/routes/initiatePasswordReset.d.ts.map +1 -1
- package/dist/src/routes/initiatePasswordReset.js +14 -12
- package/dist/src/routes/initiatePasswordReset.js.map +1 -1
- package/dist/src/routes/login.js +18 -14
- package/dist/src/routes/login.js.map +1 -1
- package/dist/src/throttle/MemoryThrottleWindowStore.d.ts +28 -0
- package/dist/src/throttle/MemoryThrottleWindowStore.d.ts.map +1 -0
- package/dist/src/throttle/MemoryThrottleWindowStore.js +108 -0
- package/dist/src/throttle/MemoryThrottleWindowStore.js.map +1 -0
- package/dist/src/throttle/PasswordResetThrottle.d.ts +9 -8
- package/dist/src/throttle/PasswordResetThrottle.d.ts.map +1 -1
- package/dist/src/throttle/PasswordResetThrottle.js +63 -15
- package/dist/src/throttle/PasswordResetThrottle.js.map +1 -1
- package/dist/src/throttle/SignInThrottle.d.ts +21 -15
- package/dist/src/throttle/SignInThrottle.d.ts.map +1 -1
- package/dist/src/throttle/SignInThrottle.js +94 -25
- package/dist/src/throttle/SignInThrottle.js.map +1 -1
- package/dist/src/throttle/SlidingWindow.d.ts +6 -4
- package/dist/src/throttle/SlidingWindow.d.ts.map +1 -1
- package/dist/src/throttle/SlidingWindow.js +6 -4
- package/dist/src/throttle/SlidingWindow.js.map +1 -1
- package/dist/src/throttle/StoredWindow.d.ts +59 -0
- package/dist/src/throttle/StoredWindow.d.ts.map +1 -0
- package/dist/src/throttle/StoredWindow.js +209 -0
- package/dist/src/throttle/StoredWindow.js.map +1 -0
- package/dist/src/throttle/ThrottleWindowStore.d.ts +38 -0
- package/dist/src/throttle/ThrottleWindowStore.d.ts.map +1 -0
- package/dist/src/throttle/ThrottleWindowStore.js +3 -0
- package/dist/src/throttle/ThrottleWindowStore.js.map +1 -0
- package/dist/test/MemoryThrottleWindowStore.test.d.ts +2 -0
- package/dist/test/MemoryThrottleWindowStore.test.d.ts.map +1 -0
- package/dist/test/MemoryThrottleWindowStore.test.js +210 -0
- package/dist/test/MemoryThrottleWindowStore.test.js.map +1 -0
- package/dist/test/SharedThrottleWindows.integration.test.d.ts +2 -0
- package/dist/test/SharedThrottleWindows.integration.test.d.ts.map +1 -0
- package/dist/test/SharedThrottleWindows.integration.test.js +756 -0
- package/dist/test/SharedThrottleWindows.integration.test.js.map +1 -0
- package/dist/test/UserServerTestEnvironment.d.ts.map +1 -1
- package/dist/test/UserServerTestEnvironment.js +3 -0
- package/dist/test/UserServerTestEnvironment.js.map +1 -1
- package/generated/index.ts +1 -1
- package/index.ts +4 -0
- package/package.json +5 -5
- package/src/routes/executePasswordReset.ts +1 -1
- package/src/routes/initiatePasswordReset.ts +2 -1
- package/src/routes/login.ts +2 -2
- package/src/throttle/MemoryThrottleWindowStore.ts +60 -0
- package/src/throttle/PasswordResetThrottle.ts +14 -11
- package/src/throttle/SignInThrottle.ts +27 -19
- package/src/throttle/SlidingWindow.ts +6 -4
- package/src/throttle/StoredWindow.ts +132 -0
- package/src/throttle/ThrottleWindowStore.ts +41 -0
- package/test/MemoryThrottleWindowStore.test.ts +74 -0
- package/test/SharedThrottleWindows.integration.test.ts +407 -0
- package/test/UserServerTestEnvironment.ts +3 -0
|
@@ -1,7 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.signInThrottle = exports.SignInThrottle = void 0;
|
|
4
40
|
var SlidingWindow_1 = require("./SlidingWindow");
|
|
41
|
+
var StoredWindow_1 = require("./StoredWindow");
|
|
5
42
|
/**
|
|
6
43
|
* The sign-in door's two windows (`POST /user/login`), keyed by digests (`RequestDigests`),
|
|
7
44
|
* never by an address:
|
|
@@ -18,10 +55,13 @@ var SlidingWindow_1 = require("./SlidingWindow");
|
|
|
18
55
|
* has an account, in the same time a refused password takes (the door runs the same password
|
|
19
56
|
* check and discards its verdict).
|
|
20
57
|
*
|
|
21
|
-
* The
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
58
|
+
* WHERE THE COUNTS LIVE. The account window is a `StoredWindow`: counted in the store the
|
|
59
|
+
* deployment registers (`DefaultThrottleWindowStoreFactory`), so with a shared store it is ONE
|
|
60
|
+
* count across every replica and survives a deploy; while that store fails it counts in this
|
|
61
|
+
* process's memory (the named fallback, see `StoredWindow`). The client window stays in process memory (`SlidingWindow`), per replica: a
|
|
62
|
+
* deployment that needs one per-address wall across replicas puts it in front of the servers (a
|
|
63
|
+
* rate limit at the load balancer, keyed on the client address); this window is the second line
|
|
64
|
+
* behind it, and keeping it local keeps the per-try forgiveness of a success exact and cheap.
|
|
25
65
|
*/
|
|
26
66
|
var SignInThrottle = /** @class */ (function () {
|
|
27
67
|
function SignInThrottle(options) {
|
|
@@ -30,7 +70,8 @@ var SignInThrottle = /** @class */ (function () {
|
|
|
30
70
|
limit: SignInThrottle.CLIENT_LIMIT,
|
|
31
71
|
now: options === null || options === void 0 ? void 0 : options.now,
|
|
32
72
|
});
|
|
33
|
-
this.accounts = new
|
|
73
|
+
this.accounts = new StoredWindow_1.StoredWindow({
|
|
74
|
+
name: 'sign-in-account',
|
|
34
75
|
windowMs: SignInThrottle.ACCOUNT_WINDOW_MS,
|
|
35
76
|
limit: SignInThrottle.ACCOUNT_LIMIT,
|
|
36
77
|
now: options === null || options === void 0 ? void 0 : options.now,
|
|
@@ -44,26 +85,55 @@ var SignInThrottle = /** @class */ (function () {
|
|
|
44
85
|
* the window; a success clears it (`recordSuccess`).
|
|
45
86
|
*/
|
|
46
87
|
SignInThrottle.prototype.admit = function (client, account) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
88
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
89
|
+
var _a;
|
|
90
|
+
return __generator(this, function (_b) {
|
|
91
|
+
switch (_b.label) {
|
|
92
|
+
case 0:
|
|
93
|
+
if (this.clients.hit(client)) {
|
|
94
|
+
return [2 /*return*/, 'client'];
|
|
95
|
+
}
|
|
96
|
+
_a = account !== undefined;
|
|
97
|
+
if (!_a) return [3 /*break*/, 2];
|
|
98
|
+
return [4 /*yield*/, this.accounts.hit(account)];
|
|
99
|
+
case 1:
|
|
100
|
+
_a = (_b.sent());
|
|
101
|
+
_b.label = 2;
|
|
102
|
+
case 2:
|
|
103
|
+
if (_a) {
|
|
104
|
+
return [2 /*return*/, 'account'];
|
|
105
|
+
}
|
|
106
|
+
return [2 /*return*/, undefined];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|
|
54
110
|
};
|
|
55
111
|
/**
|
|
56
|
-
* The account proved itself: its window opens again. When it was a sign-in try
|
|
57
|
-
* (rather than a reset link redeemed), that try is forgiven — a success never
|
|
58
|
-
* the device.
|
|
112
|
+
* The account proved itself: its window opens again, on every replica. When it was a sign-in try
|
|
113
|
+
* from `client` (rather than a reset link redeemed), that try is forgiven — a success never
|
|
114
|
+
* counts against the device.
|
|
59
115
|
*/
|
|
60
116
|
SignInThrottle.prototype.recordSuccess = function (account, client) {
|
|
61
|
-
this
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
117
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
118
|
+
return __generator(this, function (_a) {
|
|
119
|
+
switch (_a.label) {
|
|
120
|
+
case 0:
|
|
121
|
+
if (client !== undefined) {
|
|
122
|
+
this.clients.forgive(client);
|
|
123
|
+
}
|
|
124
|
+
return [4 /*yield*/, this.accounts.clear(account)];
|
|
125
|
+
case 1:
|
|
126
|
+
_a.sent();
|
|
127
|
+
return [2 /*return*/];
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
65
131
|
};
|
|
66
|
-
/**
|
|
132
|
+
/**
|
|
133
|
+
* What a throttled try is told — in plain words, the same for every address, known or not. The
|
|
134
|
+
* sign-in, sign-up and reset forms say the same when a load balancer's per-address limit answers
|
|
135
|
+
* 429 in front of the door (@proteinjs/user-ui `AuthApi.TOO_MANY_ATTEMPTS`).
|
|
136
|
+
*/
|
|
67
137
|
SignInThrottle.ANSWER = 'Too many attempts. Try again in a few minutes.';
|
|
68
138
|
/**
|
|
69
139
|
* Per client: 50 tries that are not successes in 10 minutes (the founder's number, 2026-09-23).
|
|
@@ -75,11 +145,10 @@ var SignInThrottle = /** @class */ (function () {
|
|
|
75
145
|
SignInThrottle.CLIENT_LIMIT = 50;
|
|
76
146
|
SignInThrottle.CLIENT_WINDOW_MS = 10 * 60 * 1000;
|
|
77
147
|
/**
|
|
78
|
-
* Per account: 10 tries in 15
|
|
79
|
-
* Someone who has forgotten a password tries a handful and asks for a reset link; ten wrong
|
|
80
|
-
* a quarter of an hour is guessing, and counting per account holds however many devices the
|
|
81
|
-
* guesses come from. "A few minutes" in the answer is honest: the
|
|
82
|
-
* within 15.
|
|
148
|
+
* Per account: 10 tries in a 15-minute window (a success clears them, so only wrong ones ever add
|
|
149
|
+
* up). Someone who has forgotten a password tries a handful and asks for a reset link; ten wrong
|
|
150
|
+
* in a quarter of an hour is guessing, and counting per account holds however many devices the
|
|
151
|
+
* guesses come from. "A few minutes" in the answer is honest: the window ends within 15.
|
|
83
152
|
*/
|
|
84
153
|
SignInThrottle.ACCOUNT_LIMIT = 10;
|
|
85
154
|
SignInThrottle.ACCOUNT_WINDOW_MS = 15 * 60 * 1000;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SignInThrottle.js","sourceRoot":"","sources":["../../../src/throttle/SignInThrottle.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SignInThrottle.js","sourceRoot":"","sources":["../../../src/throttle/SignInThrottle.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAgD;AAChD,+CAA8C;AAK9C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH;IA8BE,wBAAY,OAAgC;QAC1C,IAAI,CAAC,OAAO,GAAG,IAAI,6BAAa,CAAC;YAC/B,QAAQ,EAAE,cAAc,CAAC,gBAAgB;YACzC,KAAK,EAAE,cAAc,CAAC,YAAY;YAClC,GAAG,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,GAAG,IAAI,2BAAY,CAAC;YAC/B,IAAI,EAAE,iBAAiB;YACvB,QAAQ,EAAE,cAAc,CAAC,iBAAiB;YAC1C,KAAK,EAAE,cAAc,CAAC,aAAa;YACnC,GAAG,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG;SAClB,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACG,8BAAK,GAAX,UAAY,MAAc,EAAE,OAAgB;;;;;;wBAC1C,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;4BAC5B,sBAAO,QAAQ,EAAC;yBACjB;wBACG,KAAA,OAAO,KAAK,SAAS,CAAA;iCAArB,wBAAqB;wBAAK,qBAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAA;;wBAAjC,KAAA,CAAC,SAAgC,CAAC,CAAA;;;wBAA/D,QAAiE;4BAC/D,sBAAO,SAAS,EAAC;yBAClB;wBACD,sBAAO,SAAS,EAAC;;;;KAClB;IAED;;;;OAIG;IACG,sCAAa,GAAnB,UAAoB,OAAe,EAAE,MAAe;;;;;wBAClD,IAAI,MAAM,KAAK,SAAS,EAAE;4BACxB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;yBAC9B;wBACD,qBAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,EAAA;;wBAAlC,SAAkC,CAAC;;;;;KACpC;IAtED;;;;OAIG;IACa,qBAAM,GAAG,gDAAgD,CAAC;IAE1E;;;;;;OAMG;IACqB,2BAAY,GAAG,EAAE,CAAC;IAClB,+BAAgB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAE1D;;;;;OAKG;IACqB,4BAAa,GAAG,EAAE,CAAC;IACnB,gCAAiB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IA+C7D,qBAAC;CAAA,AAxED,IAwEC;AAxEY,wCAAc;AA0E3B,kFAAkF;AACrE,QAAA,cAAc,GAAG,IAAI,cAAc,EAAE,CAAC"}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A per-key sliding window: how many attempts a key made in the last `windowMs`, against a
|
|
3
|
-
* `limit`. The
|
|
4
|
-
* this package, and any consumer door throttling per client (lifted from the invite-request
|
|
5
|
-
* door's per-IP throttle, which it replaces).
|
|
3
|
+
* `limit`. The per-CLIENT window the throttled doors share — the sign-in and password-reset doors
|
|
4
|
+
* in this package, and any consumer door throttling per client (lifted from the invite-request
|
|
5
|
+
* door's per-IP throttle, which it replaces). The windows that must hold across replicas (per
|
|
6
|
+
* account, per reset address) are `StoredWindow`s, counted in a shared `ThrottleWindowStore`.
|
|
6
7
|
*
|
|
7
8
|
* Held in process memory only, which is what makes it cheap and what bounds it:
|
|
8
9
|
* - PER PROCESS. Each replica keeps its own windows, so behind a load balancer spreading a
|
|
9
10
|
* client over N replicas the effective ceiling is ~N× the limit; a restart forgets them.
|
|
10
|
-
* A throttle built on this is friction, not the wall
|
|
11
|
+
* A throttle built on this is friction, not the wall — the per-address wall across replicas
|
|
12
|
+
* belongs in front of the servers (a rate limit at the load balancer).
|
|
11
13
|
* - BOUNDED. At most `maxKeys` keys are tracked; at the bound the oldest-touched key is
|
|
12
14
|
* forgotten first, so a flood of fresh keys degrades the friction before it grows memory.
|
|
13
15
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SlidingWindow.d.ts","sourceRoot":"","sources":["../../../src/throttle/SlidingWindow.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"SlidingWindow.d.ts","sourceRoot":"","sources":["../../../src/throttle/SlidingWindow.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAU;IAElD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IACnC,2FAA2F;IAC3F,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA+B;gBAE5C,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;KAAE;IAO9F;;;OAGG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IASzB,uEAAuE;IACvE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAI5B,6FAA6F;IAC7F,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAMzB,+BAA+B;IAC/B,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIxB;;;OAGG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAM1B,OAAO,CAAC,QAAQ;IAKhB,yGAAyG;IACzG,OAAO,CAAC,KAAK;CAUd"}
|
|
@@ -3,14 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SlidingWindow = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* A per-key sliding window: how many attempts a key made in the last `windowMs`, against a
|
|
6
|
-
* `limit`. The
|
|
7
|
-
* this package, and any consumer door throttling per client (lifted from the invite-request
|
|
8
|
-
* door's per-IP throttle, which it replaces).
|
|
6
|
+
* `limit`. The per-CLIENT window the throttled doors share — the sign-in and password-reset doors
|
|
7
|
+
* in this package, and any consumer door throttling per client (lifted from the invite-request
|
|
8
|
+
* door's per-IP throttle, which it replaces). The windows that must hold across replicas (per
|
|
9
|
+
* account, per reset address) are `StoredWindow`s, counted in a shared `ThrottleWindowStore`.
|
|
9
10
|
*
|
|
10
11
|
* Held in process memory only, which is what makes it cheap and what bounds it:
|
|
11
12
|
* - PER PROCESS. Each replica keeps its own windows, so behind a load balancer spreading a
|
|
12
13
|
* client over N replicas the effective ceiling is ~N× the limit; a restart forgets them.
|
|
13
|
-
* A throttle built on this is friction, not the wall
|
|
14
|
+
* A throttle built on this is friction, not the wall — the per-address wall across replicas
|
|
15
|
+
* belongs in front of the servers (a rate limit at the load balancer).
|
|
14
16
|
* - BOUNDED. At most `maxKeys` keys are tracked; at the bound the oldest-touched key is
|
|
15
17
|
* forgotten first, so a flood of fresh keys degrades the friction before it grows memory.
|
|
16
18
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SlidingWindow.js","sourceRoot":"","sources":["../../../src/throttle/SlidingWindow.ts"],"names":[],"mappings":";;;AAAA
|
|
1
|
+
{"version":3,"file":"SlidingWindow.js","sourceRoot":"","sources":["../../../src/throttle/SlidingWindow.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH;IAUE,uBAAY,OAAkF;;QAH9F,2FAA2F;QAC1E,aAAQ,GAAG,IAAI,GAAG,EAAoB,CAAC;QAGtD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,MAAA,OAAO,CAAC,OAAO,mCAAI,aAAa,CAAC,gBAAgB,CAAC;QACjE,IAAI,CAAC,GAAG,GAAG,MAAA,OAAO,CAAC,GAAG,mCAAI,IAAI,CAAC,GAAG,CAAC;IACrC,CAAC;IAED;;;OAGG;IACH,2BAAG,GAAH,UAAI,GAAW;QACb,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;YACpB,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;YACpC,OAAO,IAAI,CAAC;SACb;QACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACjB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,uEAAuE;IACvE,8BAAM,GAAN,UAAO,GAAW;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC;IACjD,CAAC;IAED,6FAA6F;IAC7F,8BAAM,GAAN,UAAO,GAAW;QAChB,IAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACpC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAA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C,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IACnC,CAAC;IAvEuB,8BAAgB,GAAG,KAAM,AAAT,CAAU;IAwEpD,oBAAC;CAAA,AAzED,IAyEC;AAzEY,sCAAa"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A throttle window counted in a `ThrottleWindowStore` — the windows that must hold across server
|
|
3
|
+
* processes (the sign-in door's per-account window, the reset door's per-address window). The
|
|
4
|
+
* store is the one a consuming application registers through `DefaultThrottleWindowStoreFactory`
|
|
5
|
+
* (read on every call, so a registration made after start-up is honoured), else the library's own
|
|
6
|
+
* per-process `MemoryThrottleWindowStore`. The window's name prefixes every key, so windows sharing
|
|
7
|
+
* one store never meet.
|
|
8
|
+
*
|
|
9
|
+
* THE NAMED FALLBACK when the store fails — an error, or no answer within `STORE_DEADLINE_MS` (a
|
|
10
|
+
* client queueing commands while it reconnects never answers at all): the window COUNTS IN THIS
|
|
11
|
+
* PROCESS'S MEMORY (the `MemoryThrottleWindowStore` it already holds) until the store answers
|
|
12
|
+
* again — `hit` counts and judges there, `clear` clears there. A door that cannot reach the shared
|
|
13
|
+
* count still counts the way a single process does: each process holds its own count for the
|
|
14
|
+
* outage (at most the limit per process), behind the per-client window and any per-address limit
|
|
15
|
+
* in front of the servers — where refusing every try would lock out every person for a blip with
|
|
16
|
+
* a sentence that is untrue for them. The outage is one WARN line per window saying so, however
|
|
17
|
+
* many tries meet it, and one INFO line when the store answers again; the store then counts again
|
|
18
|
+
* from what it holds (the outage's tries were this process's own). A success clears the memory
|
|
19
|
+
* window as well as the store's, so an account that proved itself starts clean in both.
|
|
20
|
+
*
|
|
21
|
+
* THE SHADOW: every try the store answers is counted in the memory window too, so memory always
|
|
22
|
+
* holds this process's own count — judged by the store while it answers, by memory when it does
|
|
23
|
+
* not. An outage therefore continues from what this process already counted (nine wrong tries,
|
|
24
|
+
* then the store fails: the eleventh is refused), never from zero.
|
|
25
|
+
*/
|
|
26
|
+
export declare class StoredWindow {
|
|
27
|
+
/** How long a door waits on the store before counting in memory: well above a healthy store's milliseconds. */
|
|
28
|
+
private static readonly STORE_DEADLINE_MS;
|
|
29
|
+
private static readonly FACTORY;
|
|
30
|
+
private readonly name;
|
|
31
|
+
private readonly windowMs;
|
|
32
|
+
private readonly limit;
|
|
33
|
+
private readonly memory;
|
|
34
|
+
private readonly logger;
|
|
35
|
+
/** Whether the last call met a failed store — the outage is logged once, on the way in. */
|
|
36
|
+
private storeUnavailable;
|
|
37
|
+
constructor(options: {
|
|
38
|
+
name: string;
|
|
39
|
+
windowMs: number;
|
|
40
|
+
limit: number;
|
|
41
|
+
now?: () => number;
|
|
42
|
+
});
|
|
43
|
+
/**
|
|
44
|
+
* Count an attempt for `key` and answer whether it is over the window — `true` means refuse.
|
|
45
|
+
* Every attempt is counted (the window's end is fixed at its first count, so counting a refused
|
|
46
|
+
* attempt never extends it): in the store, and in this process's memory too (the shadow) — judged
|
|
47
|
+
* by the store when it answers, by memory when it fails (see the class comment).
|
|
48
|
+
*/
|
|
49
|
+
hit(key: string): Promise<boolean>;
|
|
50
|
+
/** Forget `key`'s window — in this process's memory, and in the store when it answers. */
|
|
51
|
+
clear(key: string): Promise<void>;
|
|
52
|
+
/** The registered shared store, else the library's own. */
|
|
53
|
+
private store;
|
|
54
|
+
private key;
|
|
55
|
+
private withDeadline;
|
|
56
|
+
private failed;
|
|
57
|
+
private answered;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=StoredWindow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StoredWindow.d.ts","sourceRoot":"","sources":["../../../src/throttle/StoredWindow.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,YAAY;IACvB,+GAA+G;IAC/G,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAQ;IACjD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAA8D;IAE7F,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsB;IAC7C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwC;IAC/D,2FAA2F;IAC3F,OAAO,CAAC,gBAAgB,CAAS;gBAErB,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;KAAE;IAO1F;;;;;OAKG;IACG,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAmBxC,0FAA0F;IACpF,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUvC,2DAA2D;IAC3D,OAAO,CAAC,KAAK;IAKb,OAAO,CAAC,GAAG;YAIG,YAAY;IAiB1B,OAAO,CAAC,MAAM;IAWd,OAAO,CAAC,QAAQ;CAOjB"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.StoredWindow = void 0;
|
|
40
|
+
var reflection_1 = require("@proteinjs/reflection");
|
|
41
|
+
var logger_1 = require("@proteinjs/logger");
|
|
42
|
+
var MemoryThrottleWindowStore_1 = require("./MemoryThrottleWindowStore");
|
|
43
|
+
/**
|
|
44
|
+
* A throttle window counted in a `ThrottleWindowStore` — the windows that must hold across server
|
|
45
|
+
* processes (the sign-in door's per-account window, the reset door's per-address window). The
|
|
46
|
+
* store is the one a consuming application registers through `DefaultThrottleWindowStoreFactory`
|
|
47
|
+
* (read on every call, so a registration made after start-up is honoured), else the library's own
|
|
48
|
+
* per-process `MemoryThrottleWindowStore`. The window's name prefixes every key, so windows sharing
|
|
49
|
+
* one store never meet.
|
|
50
|
+
*
|
|
51
|
+
* THE NAMED FALLBACK when the store fails — an error, or no answer within `STORE_DEADLINE_MS` (a
|
|
52
|
+
* client queueing commands while it reconnects never answers at all): the window COUNTS IN THIS
|
|
53
|
+
* PROCESS'S MEMORY (the `MemoryThrottleWindowStore` it already holds) until the store answers
|
|
54
|
+
* again — `hit` counts and judges there, `clear` clears there. A door that cannot reach the shared
|
|
55
|
+
* count still counts the way a single process does: each process holds its own count for the
|
|
56
|
+
* outage (at most the limit per process), behind the per-client window and any per-address limit
|
|
57
|
+
* in front of the servers — where refusing every try would lock out every person for a blip with
|
|
58
|
+
* a sentence that is untrue for them. The outage is one WARN line per window saying so, however
|
|
59
|
+
* many tries meet it, and one INFO line when the store answers again; the store then counts again
|
|
60
|
+
* from what it holds (the outage's tries were this process's own). A success clears the memory
|
|
61
|
+
* window as well as the store's, so an account that proved itself starts clean in both.
|
|
62
|
+
*
|
|
63
|
+
* THE SHADOW: every try the store answers is counted in the memory window too, so memory always
|
|
64
|
+
* holds this process's own count — judged by the store while it answers, by memory when it does
|
|
65
|
+
* not. An outage therefore continues from what this process already counted (nine wrong tries,
|
|
66
|
+
* then the store fails: the eleventh is refused), never from zero.
|
|
67
|
+
*/
|
|
68
|
+
var StoredWindow = /** @class */ (function () {
|
|
69
|
+
function StoredWindow(options) {
|
|
70
|
+
this.logger = new logger_1.Logger({ name: 'StoredWindow' });
|
|
71
|
+
/** Whether the last call met a failed store — the outage is logged once, on the way in. */
|
|
72
|
+
this.storeUnavailable = false;
|
|
73
|
+
this.name = options.name;
|
|
74
|
+
this.windowMs = options.windowMs;
|
|
75
|
+
this.limit = options.limit;
|
|
76
|
+
this.memory = new MemoryThrottleWindowStore_1.MemoryThrottleWindowStore({ now: options.now });
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Count an attempt for `key` and answer whether it is over the window — `true` means refuse.
|
|
80
|
+
* Every attempt is counted (the window's end is fixed at its first count, so counting a refused
|
|
81
|
+
* attempt never extends it): in the store, and in this process's memory too (the shadow) — judged
|
|
82
|
+
* by the store when it answers, by memory when it fails (see the class comment).
|
|
83
|
+
*/
|
|
84
|
+
StoredWindow.prototype.hit = function (key) {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
86
|
+
var answer, error_1;
|
|
87
|
+
var _this = this;
|
|
88
|
+
return __generator(this, function (_a) {
|
|
89
|
+
switch (_a.label) {
|
|
90
|
+
case 0:
|
|
91
|
+
_a.trys.push([0, 2, , 4]);
|
|
92
|
+
return [4 /*yield*/, this.withDeadline(function (store) { return __awaiter(_this, void 0, void 0, function () {
|
|
93
|
+
var _a;
|
|
94
|
+
return __generator(this, function (_b) {
|
|
95
|
+
switch (_b.label) {
|
|
96
|
+
case 0:
|
|
97
|
+
_a = {};
|
|
98
|
+
return [4 /*yield*/, store.increment(this.key(key), this.windowMs)];
|
|
99
|
+
case 1: return [2 /*return*/, (_a.count = _b.sent(),
|
|
100
|
+
_a.store = store,
|
|
101
|
+
_a)];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}); })];
|
|
105
|
+
case 1:
|
|
106
|
+
answer = _a.sent();
|
|
107
|
+
this.answered();
|
|
108
|
+
return [3 /*break*/, 4];
|
|
109
|
+
case 2:
|
|
110
|
+
error_1 = _a.sent();
|
|
111
|
+
this.failed(error_1);
|
|
112
|
+
return [4 /*yield*/, this.memory.increment(this.key(key), this.windowMs)];
|
|
113
|
+
case 3: return [2 /*return*/, (_a.sent()) > this.limit];
|
|
114
|
+
case 4:
|
|
115
|
+
if (!(answer.store !== this.memory)) return [3 /*break*/, 6];
|
|
116
|
+
// The shadow: this process's own count, for the outage (no shared store registered: counted once).
|
|
117
|
+
return [4 /*yield*/, this.memory.increment(this.key(key), this.windowMs)];
|
|
118
|
+
case 5:
|
|
119
|
+
// The shadow: this process's own count, for the outage (no shared store registered: counted once).
|
|
120
|
+
_a.sent();
|
|
121
|
+
_a.label = 6;
|
|
122
|
+
case 6: return [2 /*return*/, answer.count > this.limit];
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
/** Forget `key`'s window — in this process's memory, and in the store when it answers. */
|
|
128
|
+
StoredWindow.prototype.clear = function (key) {
|
|
129
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
130
|
+
var error_2;
|
|
131
|
+
var _this = this;
|
|
132
|
+
return __generator(this, function (_a) {
|
|
133
|
+
switch (_a.label) {
|
|
134
|
+
case 0: return [4 /*yield*/, this.memory.clear(this.key(key))];
|
|
135
|
+
case 1:
|
|
136
|
+
_a.sent();
|
|
137
|
+
_a.label = 2;
|
|
138
|
+
case 2:
|
|
139
|
+
_a.trys.push([2, 4, , 5]);
|
|
140
|
+
return [4 /*yield*/, this.withDeadline(function (store) { return store.clear(_this.key(key)); })];
|
|
141
|
+
case 3:
|
|
142
|
+
_a.sent();
|
|
143
|
+
this.answered();
|
|
144
|
+
return [3 /*break*/, 5];
|
|
145
|
+
case 4:
|
|
146
|
+
error_2 = _a.sent();
|
|
147
|
+
this.failed(error_2);
|
|
148
|
+
return [3 /*break*/, 5];
|
|
149
|
+
case 5: return [2 /*return*/];
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
/** The registered shared store, else the library's own. */
|
|
155
|
+
StoredWindow.prototype.store = function () {
|
|
156
|
+
var factory = reflection_1.SourceRepository.get().object(StoredWindow.FACTORY);
|
|
157
|
+
return factory ? factory.getStore() : this.memory;
|
|
158
|
+
};
|
|
159
|
+
StoredWindow.prototype.key = function (key) {
|
|
160
|
+
return "".concat(this.name, ":").concat(key);
|
|
161
|
+
};
|
|
162
|
+
StoredWindow.prototype.withDeadline = function (call) {
|
|
163
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
164
|
+
var timer, deadline;
|
|
165
|
+
return __generator(this, function (_a) {
|
|
166
|
+
switch (_a.label) {
|
|
167
|
+
case 0:
|
|
168
|
+
deadline = new Promise(function (_, reject) {
|
|
169
|
+
timer = setTimeout(function () { return reject(new Error("no answer within ".concat(StoredWindow.STORE_DEADLINE_MS, " ms"))); }, StoredWindow.STORE_DEADLINE_MS);
|
|
170
|
+
});
|
|
171
|
+
_a.label = 1;
|
|
172
|
+
case 1:
|
|
173
|
+
_a.trys.push([1, , 3, 4]);
|
|
174
|
+
return [4 /*yield*/, Promise.race([call(this.store()), deadline])];
|
|
175
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
176
|
+
case 3:
|
|
177
|
+
if (timer !== undefined) {
|
|
178
|
+
clearTimeout(timer);
|
|
179
|
+
}
|
|
180
|
+
return [7 /*endfinally*/];
|
|
181
|
+
case 4: return [2 /*return*/];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
StoredWindow.prototype.failed = function (error) {
|
|
187
|
+
if (this.storeUnavailable) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
this.storeUnavailable = true;
|
|
191
|
+
this.logger.warn({
|
|
192
|
+
message: "Throttle window store unavailable; counting in this process's memory until it answers",
|
|
193
|
+
obj: { window: this.name, errorMessage: error instanceof Error ? error.message : String(error) },
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
StoredWindow.prototype.answered = function () {
|
|
197
|
+
if (!this.storeUnavailable) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
this.storeUnavailable = false;
|
|
201
|
+
this.logger.info({ message: 'Throttle window store answering again', obj: { window: this.name } });
|
|
202
|
+
};
|
|
203
|
+
/** How long a door waits on the store before counting in memory: well above a healthy store's milliseconds. */
|
|
204
|
+
StoredWindow.STORE_DEADLINE_MS = 2000;
|
|
205
|
+
StoredWindow.FACTORY = '@proteinjs/user-server/DefaultThrottleWindowStoreFactory';
|
|
206
|
+
return StoredWindow;
|
|
207
|
+
}());
|
|
208
|
+
exports.StoredWindow = StoredWindow;
|
|
209
|
+
//# sourceMappingURL=StoredWindow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StoredWindow.js","sourceRoot":"","sources":["../../../src/throttle/StoredWindow.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAyD;AACzD,4CAA2C;AAE3C,yEAAwE;AAExE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH;IAaE,sBAAY,OAA8E;QAJzE,WAAM,GAAG,IAAI,eAAM,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC,CAAC;QAC/D,2FAA2F;QACnF,qBAAgB,GAAG,KAAK,CAAC;QAG/B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,MAAM,GAAG,IAAI,qDAAyB,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IACpE,CAAC;IAED;;;;;OAKG;IACG,0BAAG,GAAT,UAAU,GAAW;;;;;;;;wBAGR,qBAAM,IAAI,CAAC,YAAY,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MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;SACjG,CAAC,CAAC;IACL,CAAC;IAEO,+BAAQ,GAAhB;QACE,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE;YAC1B,OAAO;SACR;QACD,IAAI,CAAC,gBAAgB,GAAG,KAAK,CAAC;QAC9B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,uCAAuC,EAAE,GAAG,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACrG,CAAC;IAnGD,+GAA+G;IACvF,8BAAiB,GAAG,IAAI,AAAP,CAAQ;IACzB,oBAAO,GAAG,0DAA0D,AAA7D,CAA8D;IAkG/F,mBAAC;CAAA,AArGD,IAqGC;AArGY,oCAAY"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Loadable } from '@proteinjs/reflection';
|
|
2
|
+
/**
|
|
3
|
+
* Where a throttle counts the windows that must hold across server processes — the windows keyed
|
|
4
|
+
* on something other than the device (the sign-in door's per-account window, the reset door's
|
|
5
|
+
* per-address window; see `StoredWindow`). A window here is a TTL: it opens at a key's first count
|
|
6
|
+
* and lasts the `windowMs` that count was given; counts inside it add up; at its end the key is
|
|
7
|
+
* gone and the next count opens a fresh window.
|
|
8
|
+
*
|
|
9
|
+
* The library's own store is `MemoryThrottleWindowStore` — per process, which is right for tests
|
|
10
|
+
* and single-process servers. A deployment of several processes registers a SHARED store (one
|
|
11
|
+
* every process reads and writes) through `DefaultThrottleWindowStoreFactory`, so a window is one
|
|
12
|
+
* count across replicas and survives a restart or a deploy.
|
|
13
|
+
*
|
|
14
|
+
* Keys are digests (see `RequestDigests`) prefixed with the window's name — never an address — so
|
|
15
|
+
* nothing identifying reaches the store.
|
|
16
|
+
*/
|
|
17
|
+
export interface ThrottleWindowStore {
|
|
18
|
+
/**
|
|
19
|
+
* Count one attempt for `key` and answer how many the key holds inside its window, this one
|
|
20
|
+
* included. The first count opens the window with `windowMs` as its TTL; later counts never
|
|
21
|
+
* extend it. Atomic across every caller of the store: attempts in flight at once each get their
|
|
22
|
+
* own count.
|
|
23
|
+
*/
|
|
24
|
+
increment(key: string, windowMs: number): Promise<number>;
|
|
25
|
+
/** How many attempts `key` holds inside its window (0 when it holds none). */
|
|
26
|
+
read(key: string): Promise<number>;
|
|
27
|
+
/** Forget `key`'s window. */
|
|
28
|
+
clear(key: string): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The seam a consuming application registers its shared store through (discovered like every
|
|
32
|
+
* other `Default…Factory`). Without a registration the counted windows use the library's own
|
|
33
|
+
* per-process store.
|
|
34
|
+
*/
|
|
35
|
+
export interface DefaultThrottleWindowStoreFactory extends Loadable {
|
|
36
|
+
getStore(): ThrottleWindowStore;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=ThrottleWindowStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThrottleWindowStore.d.ts","sourceRoot":"","sources":["../../../src/throttle/ThrottleWindowStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;;;OAKG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE1D,8EAA8E;IAC9E,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnC,6BAA6B;IAC7B,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACnC;AAED;;;;GAIG;AACH,MAAM,WAAW,iCAAkC,SAAQ,QAAQ;IACjE,QAAQ,IAAI,mBAAmB,CAAC;CACjC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThrottleWindowStore.js","sourceRoot":"","sources":["../../../src/throttle/ThrottleWindowStore.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryThrottleWindowStore.test.d.ts","sourceRoot":"","sources":["../../test/MemoryThrottleWindowStore.test.ts"],"names":[],"mappings":""}
|