@tryghost/webhook-mock-receiver 2.3.16 → 2.3.18

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.
@@ -1,10 +1,14 @@
1
1
  const { AssertionError } = require('assert');
2
2
  const { URL } = require('url');
3
- const nock = require('nock');
3
+ const nockModule = require('nock');
4
+ const nock = nockModule.default || nockModule;
4
5
  const pWaitFor = require('p-wait-for').default;
5
6
  class WebhookMockReceiver {
6
7
  constructor({ snapshotManager }) {
7
8
  this.snapshotManager = snapshotManager;
9
+ // Bumped by mock() and reset() so reply callbacks from a superseded
10
+ // mock can't record into the current one
11
+ this._mockGeneration = 0;
8
12
  this.recordRequest = this.recordRequest.bind(this);
9
13
  }
10
14
 
@@ -15,7 +19,9 @@ class WebhookMockReceiver {
15
19
 
16
20
  async receivedRequest() {
17
21
  // @NOTE: figure out a better waiting mechanism here, don't allow it to hang forever
18
- await pWaitFor(() => this._receiver.isDone());
22
+ // Nock marks the interceptor done when the request matches, before the
23
+ // async reply callback records it, so wait for both
24
+ await pWaitFor(() => this._receiver.isDone() && this.headers !== undefined);
19
25
  }
20
26
 
21
27
  /**
@@ -25,21 +31,50 @@ class WebhookMockReceiver {
25
31
  */
26
32
  mock(url) {
27
33
  const parsedURL = new URL(url);
28
- const recordRequest = this.recordRequest;
34
+ const mockGeneration = ++this._mockGeneration;
35
+ const recordRequest = (...args) => {
36
+ if (mockGeneration === this._mockGeneration) {
37
+ this.recordRequest(...args);
38
+ }
39
+ };
40
+ this.body = undefined;
41
+ this.headers = undefined;
29
42
 
30
43
  this._receiver = nock(parsedURL.origin)
31
- .post(parsedURL.pathname, function (body) {
32
- recordRequest(body, this);
33
- // let the nock continue with the response
34
- return true;
35
- })
36
- .reply(200, { status: 'OK' });
44
+ .post(parsedURL.pathname)
45
+ .reply(
46
+ 200,
47
+ async function (...args) {
48
+ if (args.length === 1) {
49
+ // Nock 15 passes a Fetch Request; older versions pass URI and body.
50
+ const request = args[0];
51
+ const text = await request.text();
52
+ let body = text;
53
+ if (
54
+ text &&
55
+ request.headers.get('content-type')?.includes('application/json')
56
+ ) {
57
+ try {
58
+ body = JSON.parse(text);
59
+ } catch {
60
+ // Preserve non-JSON payloads, as Nock's body matcher does.
61
+ }
62
+ }
63
+ recordRequest(body, { headers: Object.fromEntries(request.headers) });
64
+ } else {
65
+ recordRequest(args[1], this.req);
66
+ }
67
+ return { status: 'OK' };
68
+ },
69
+ { 'Content-Length': '15' },
70
+ );
37
71
 
38
72
  return this;
39
73
  }
40
74
 
41
75
  reset() {
42
76
  nock.cleanAll();
77
+ this._mockGeneration += 1;
43
78
  this._receiver = undefined;
44
79
  this.body = undefined;
45
80
  this.headers = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/webhook-mock-receiver",
3
- "version": "2.3.16",
3
+ "version": "2.3.18",
4
4
  "license": "MIT",
5
5
  "author": "Ghost Foundation",
6
6
  "repository": {
@@ -21,18 +21,18 @@
21
21
  },
22
22
  "devDependencies": {
23
23
  "got": "16.0.0",
24
- "nock": "14.0.17",
24
+ "nock": "15.0.1",
25
25
  "sinon": "22.1.0"
26
26
  },
27
27
  "peerDependencies": {
28
- "nock": "^13.0.0 || ^14.0.0"
28
+ "nock": "^13.0.0 || ^14.0.0 || ^15.0.0"
29
29
  },
30
30
  "engines": {
31
31
  "node": "^22.12.0 || >=24.0.0"
32
32
  },
33
33
  "scripts": {
34
34
  "dev": "echo \"Implement me!\"",
35
- "test": "NODE_ENV=testing vitest run --coverage",
35
+ "test": "NODE_ENV=testing vitest run --config ../../vitest.config.ts --root . --coverage",
36
36
  "lint": "oxlint -c ../../.oxlintrc.json .",
37
37
  "posttest": "pnpm run lint"
38
38
  }