@astratra/shipping 1.1.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.fr.md CHANGED
@@ -4,7 +4,7 @@ Les étiquettes d'expédition sans les pièges : un contrat de transporteur, un
4
4
  adaptateur Sendcloud (étiquettes, points relais, suivi), le webhook de suivi
5
5
  signé et protégé contre les rejeux, et un mode test sans frais.
6
6
 
7
- Né du code de Boutique Roro, pour toutes les boutiques : le paquet ne connaît
7
+ Pour toutes les boutiques : le paquet ne connaît
8
8
  ni commandes, ni tables, ni prix. Il parle au transporteur ; la boutique décide.
9
9
 
10
10
  ```bash
package/README.md CHANGED
@@ -4,7 +4,7 @@ Shipping labels without the traps: a carrier contract, a Sendcloud adapter
4
4
  (labels, service points, tracking), the signed tracking webhook with replay
5
5
  protection, and a free test mode.
6
6
 
7
- Extracted from Boutique Roro, for every shop: the package knows no orders, no
7
+ For every shop: the package knows no orders, no
8
8
  tables and no prices. It talks to the carrier; the shop decides.
9
9
 
10
10
  [Version française](README.fr.md)
@@ -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,9 +1,9 @@
1
1
  {
2
2
  "name": "@astratra/shipping",
3
- "version": "1.1.0",
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
- "node": ">=20"
6
+ "node": ">=22"
7
7
  },
8
8
  "main": "src/index.js",
9
9
  "types": "src/index.d.ts",
@@ -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') {