@astratra/shipping 1.2.0 → 1.2.1
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/README.md +4 -1
- package/package.json +5 -5
- package/src/webhook.js +11 -1
package/README.md
CHANGED
|
@@ -119,7 +119,10 @@ Webhooks can arrive out of order: `timestamp` says when the status changed,
|
|
|
119
119
|
converted), so an older status never overwrites a newer one.
|
|
120
120
|
With an event log that can prune (`createPostgresEventLog`), handled events
|
|
121
121
|
older than 90 days are deleted at most once a day, after the answer
|
|
122
|
-
(`pruneOlderThanMs`, `false` to do it yourself; `pruneEveryMs`; `settled()`).
|
|
122
|
+
(`pruneOlderThanMs`, `false` to do it yourself; `pruneEveryMs`; `settled()`). An event dated
|
|
123
|
+
before what the log remembers is acknowledged without being handled: a signature has
|
|
124
|
+
no expiry, so a captured old event would otherwise replay once the log forgot it. If
|
|
125
|
+
you prune yourself, refuse events older than your own retention.
|
|
123
126
|
Wrong signature, parsed or unreadable body: 400 (no endless retries). Another
|
|
124
127
|
event type: 200, ignored. The same body again: 200 `duplicate`. An error in your
|
|
125
128
|
handlers: 500 and the event is released, so Sendcloud's resend is handled.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astratra/shipping",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Shipping labels without the traps: a carrier contract, a Sendcloud adapter (labels, service points, tracking), signed tracking webhooks with replay protection, and a free test mode.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22"
|
|
@@ -11,9 +11,6 @@
|
|
|
11
11
|
"files": [
|
|
12
12
|
"src"
|
|
13
13
|
],
|
|
14
|
-
"scripts": {
|
|
15
|
-
"test": "jest"
|
|
16
|
-
},
|
|
17
14
|
"jest": {
|
|
18
15
|
"testMatch": [
|
|
19
16
|
"**/__tests__/**/*.test.js"
|
|
@@ -39,5 +36,8 @@
|
|
|
39
36
|
},
|
|
40
37
|
"devDependencies": {
|
|
41
38
|
"jest": "30.4.2"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"test": "jest"
|
|
42
42
|
}
|
|
43
|
-
}
|
|
43
|
+
}
|
package/src/webhook.js
CHANGED
|
@@ -30,7 +30,8 @@ const DEFAULT_PRUNE_EVERY_MS = 24 * 3600_000;
|
|
|
30
30
|
* @param {object} [options.logger]
|
|
31
31
|
* @param {number|false} [options.pruneOlderThanMs] when the event log has prune(): handled events older
|
|
32
32
|
* than this are deleted, at most once per `pruneEveryMs`, after a webhook is answered. Default 90 days;
|
|
33
|
-
* false disables it (prune it yourself).
|
|
33
|
+
* false disables it (prune it yourself). An event dated before what the log remembers is acknowledged without
|
|
34
|
+
* being handled: once forgotten, a replayed old event could not be told from a new one.
|
|
34
35
|
* @param {number} [options.pruneEveryMs] default 24 h
|
|
35
36
|
* @param {Function} [options.now] for tests
|
|
36
37
|
*/
|
|
@@ -46,6 +47,8 @@ function createCarrierWebhook(options = {}) {
|
|
|
46
47
|
const pruneEveryMs = options.pruneEveryMs || DEFAULT_PRUNE_EVERY_MS;
|
|
47
48
|
const now = options.now || Date.now;
|
|
48
49
|
const logger = options.logger || {};
|
|
50
|
+
/* Ce que le journal retient : la durée de sa purge, quand c'est ce webhook qui la fait. */
|
|
51
|
+
const forgetsAfterMs = pruneOlderThanMs && eventLog && typeof eventLog.prune === 'function' ? pruneOlderThanMs : null;
|
|
49
52
|
let lastPrune = null;
|
|
50
53
|
let pruning = null;
|
|
51
54
|
|
|
@@ -88,6 +91,13 @@ function createCarrierWebhook(options = {}) {
|
|
|
88
91
|
},
|
|
89
92
|
events: {
|
|
90
93
|
async parcel_status_changed(event, tools) {
|
|
94
|
+
/* Le journal oublie ce qui est plus vieux que sa purge, et la signature d'un événement n'a pas de date limite :
|
|
95
|
+
un événement daté d'avant ce que le journal retient ne peut plus être distingué d'un renvoi. Il est accusé
|
|
96
|
+
sans être traité (un vieux corps signé, capté une fois, se rejouerait sinon à volonté). */
|
|
97
|
+
if (forgetsAfterMs && Number.isFinite(event.parcel.timestamp) && now() - event.parcel.timestamp > forgetsAfterMs) {
|
|
98
|
+
if (logger.warn) logger.warn('[shipping] event older than the event log remembers: acknowledged, not handled');
|
|
99
|
+
return tools.duplicate('stale');
|
|
100
|
+
}
|
|
91
101
|
if (typeof options.onEvent === 'function') await options.onEvent(event.parcel, tools);
|
|
92
102
|
const act = byStage[event.parcel.stage];
|
|
93
103
|
if (typeof act === 'function') {
|