@deployagram/nock 0.0.0-stage → 1.0.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/LICENSE +14 -0
- package/README.md +37 -2
- package/dist/index.d.ts +48 -0
- package/dist/index.js +102 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
DEPLOYAGRAM PROPRIETARY LICENSE
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Deployagram, Cookiebit and Andy Muir.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted to use this software solely for its intended purpose.
|
|
6
|
+
|
|
7
|
+
RESTRICTIONS:
|
|
8
|
+
- No modification of the software is permitted
|
|
9
|
+
- No reverse engineering, decompilation, or disassembly
|
|
10
|
+
- No redistribution without explicit written permission
|
|
11
|
+
- Source code remains proprietary and confidential
|
|
12
|
+
- Usage is limited to the compiled npm package (the distributed tarball) artifact only
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
|
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @deployagram/nock
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Records the HTTP calls that a [Nock](https://github.com/nock/nock) scope intercepts, and sends each request
|
|
4
|
+
and response to the Deployagram Collector, in the same format as the Deployagram HTTP Proxy. Use it instead of
|
|
5
|
+
the Proxy when a test stubs HTTP with Nock.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import nock from 'nock';
|
|
11
|
+
import { recordNock } from '@deployagram/nock';
|
|
12
|
+
import { DiagramLoggerClient } from '@deployagram/logger-client';
|
|
13
|
+
|
|
14
|
+
const logger = new DiagramLoggerClient({ baseUrl: collectorUrl });
|
|
15
|
+
const scope = nock('http://localhost:9050').post('/Emailer/email').reply(204);
|
|
16
|
+
const recorder = recordNock(scope, { logger, caller: 'DontForget', callee: 'Emailer' });
|
|
17
|
+
|
|
18
|
+
// ... the test runs ...
|
|
19
|
+
|
|
20
|
+
await recorder.flush(); // wait until every call has reached the Collector
|
|
21
|
+
recorder.stop();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Differences from the Proxy
|
|
25
|
+
|
|
26
|
+
- Nock reports a request before it answers. The response is recorded from the stub's status and reply
|
|
27
|
+
body, not from what the application reads.
|
|
28
|
+
- A stub's body is recorded as it was written. A stub with no body records `Body: null`.
|
|
29
|
+
- A failure to reach the Collector is warned about, and does not fail the call.
|
|
30
|
+
- The recorder works on one scope. Record each scope that the test uses.
|
|
31
|
+
|
|
32
|
+
## Testing
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm test
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The tests use real Nock and `fetch`, with a logger that keeps entries in memory. They need no Collector.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { DiagramLogger } from '@deployagram/api';
|
|
2
|
+
/**
|
|
3
|
+
* The parts of a Nock scope the recorder uses. Nock's Scope has an `on` method that reports each
|
|
4
|
+
* request it intercepts, so the recorder needs no Nock import and the package stays free of it.
|
|
5
|
+
*/
|
|
6
|
+
export interface NockScope {
|
|
7
|
+
on(event: 'request', listener: (request: NockRequest, interceptor: NockInterceptor, body: string) => void): unknown;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The request Nock reports, as the recorder reads it. Nock 15 reports a fetch `Request`, with a full `url` and
|
|
11
|
+
* `Headers`. Nock 14 and 13 report an `http` request, with a `path` and a plain object of headers, and the scope's
|
|
12
|
+
* base URL is on the interceptor. The recorder reads either.
|
|
13
|
+
*/
|
|
14
|
+
export interface NockRequest {
|
|
15
|
+
method: string;
|
|
16
|
+
url?: string;
|
|
17
|
+
path?: string;
|
|
18
|
+
headers: Headers | Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
/** The stub that answered the request: its status, and the reply's body and headers when it has them. */
|
|
21
|
+
export interface NockInterceptor {
|
|
22
|
+
statusCode?: number;
|
|
23
|
+
body?: unknown;
|
|
24
|
+
headers?: Record<string, unknown>;
|
|
25
|
+
/** The scope's base URL, for example "http://localhost:9050". Nock 14 and 13 report requests by path. */
|
|
26
|
+
basePath?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface RecordNockOptions {
|
|
29
|
+
logger: DiagramLogger;
|
|
30
|
+
/** The name of the application making the calls, for example "DontForget". */
|
|
31
|
+
caller: string;
|
|
32
|
+
/** The name of the application the scope stands for, shown on the diagram, for example "Emailer". */
|
|
33
|
+
callee: string;
|
|
34
|
+
}
|
|
35
|
+
export interface NockRecorder {
|
|
36
|
+
/** Resolves once every call recorded so far has been sent to the Collector. */
|
|
37
|
+
flush(): Promise<void>;
|
|
38
|
+
/** Stops recording. */
|
|
39
|
+
stop(): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Records the calls a Nock scope intercepts, and sends each request and response to the Collector in the
|
|
43
|
+
* same format the Deployagram HTTP Proxy uses.
|
|
44
|
+
*
|
|
45
|
+
* Nock reports a request before it answers, so the response is recorded from the stub's status and reply
|
|
46
|
+
* body, not from what the application reads. A stub's body and headers are recorded as they were written.
|
|
47
|
+
*/
|
|
48
|
+
export declare function recordNock(scope: NockScope, options: RecordNockOptions): NockRecorder;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.recordNock = recordNock;
|
|
4
|
+
/**
|
|
5
|
+
* Records the calls a Nock scope intercepts, and sends each request and response to the Collector in the
|
|
6
|
+
* same format the Deployagram HTTP Proxy uses.
|
|
7
|
+
*
|
|
8
|
+
* Nock reports a request before it answers, so the response is recorded from the stub's status and reply
|
|
9
|
+
* body, not from what the application reads. A stub's body and headers are recorded as they were written.
|
|
10
|
+
*/
|
|
11
|
+
function recordNock(scope, options) {
|
|
12
|
+
let active = true;
|
|
13
|
+
let queue = Promise.resolve();
|
|
14
|
+
const track = (work) => {
|
|
15
|
+
queue = queue.then(work).catch((error) => warn(error));
|
|
16
|
+
};
|
|
17
|
+
scope.on('request', (request, interceptor, body) => {
|
|
18
|
+
if (!active)
|
|
19
|
+
return;
|
|
20
|
+
track(() => recordCall(request, interceptor, body, options));
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
async flush() {
|
|
24
|
+
let seen;
|
|
25
|
+
do {
|
|
26
|
+
seen = queue;
|
|
27
|
+
await seen;
|
|
28
|
+
} while (seen !== queue);
|
|
29
|
+
},
|
|
30
|
+
stop() {
|
|
31
|
+
active = false;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
async function recordCall(request, interceptor, body, options) {
|
|
36
|
+
const { caller, callee } = options;
|
|
37
|
+
const url = new URL(absoluteUrlOf(request, interceptor));
|
|
38
|
+
const requestContent = { Headers: headersOf(request.headers) };
|
|
39
|
+
if (body)
|
|
40
|
+
requestContent.Body = body;
|
|
41
|
+
requestContent.Endpoint = `${url.pathname.replace(/^\//, '')}${url.search}`;
|
|
42
|
+
await send(options, {
|
|
43
|
+
protocol: 'HTTP',
|
|
44
|
+
source: caller,
|
|
45
|
+
target: callee,
|
|
46
|
+
message: `${request.method} Request from ${caller} to ${callee}`,
|
|
47
|
+
body: JSON.stringify(requestContent),
|
|
48
|
+
});
|
|
49
|
+
const responseContent = {
|
|
50
|
+
Headers: recordedHeaders(interceptor.headers),
|
|
51
|
+
Body: bodyText(interceptor.body),
|
|
52
|
+
ResponseCode: interceptor.statusCode ?? 200,
|
|
53
|
+
};
|
|
54
|
+
await send(options, {
|
|
55
|
+
protocol: 'HTTP',
|
|
56
|
+
source: callee,
|
|
57
|
+
target: caller,
|
|
58
|
+
message: `${request.method} Response from ${callee} to ${caller}`,
|
|
59
|
+
body: JSON.stringify(responseContent),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
async function send(options, log) {
|
|
63
|
+
await options.logger.log({ timestamp: new Date().toISOString(), ...log });
|
|
64
|
+
}
|
|
65
|
+
/** The request's full URL: as Nock 15 reports it, or the scope's base URL with the request's path. */
|
|
66
|
+
function absoluteUrlOf(request, interceptor) {
|
|
67
|
+
if (request.url !== undefined)
|
|
68
|
+
return request.url;
|
|
69
|
+
return `${interceptor.basePath ?? ''}${request.path ?? '/'}`;
|
|
70
|
+
}
|
|
71
|
+
/** Headers as the Proxy writes them: each name maps to a list of values. */
|
|
72
|
+
function headersOf(headers) {
|
|
73
|
+
const result = {};
|
|
74
|
+
if (headers instanceof Headers) {
|
|
75
|
+
headers.forEach((value, name) => {
|
|
76
|
+
result[name] = [value];
|
|
77
|
+
});
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
81
|
+
result[name.toLowerCase()] = Array.isArray(value) ? value.map(String) : [String(value)];
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
function recordedHeaders(headers) {
|
|
86
|
+
const result = {};
|
|
87
|
+
for (const [name, value] of Object.entries(headers ?? {})) {
|
|
88
|
+
result[name] = Array.isArray(value) ? value.map(String) : [String(value)];
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
/** The reply body as text, or null when the stub has none, as the Proxy writes an empty body. */
|
|
93
|
+
function bodyText(body) {
|
|
94
|
+
if (body === undefined || body === null || body === '')
|
|
95
|
+
return null;
|
|
96
|
+
return typeof body === 'string' ? body : JSON.stringify(body);
|
|
97
|
+
}
|
|
98
|
+
function warn(error) {
|
|
99
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
100
|
+
console.warn(`Deployagram: could not record a Nock call: ${reason}`);
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|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ChB,CAAC;AAED,iGAAiG;AACjG,SAAS,QAAQ,CAAC,IAAa;IAC7B,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE;QAAE,OAAO,IAAI,CAAC;IACpE,OAAO,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;AAChE,CAAC;AAED,SAAS,IAAI,CAAC,KAAc;IAC1B,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACtE,OAAO,CAAC,IAAI,CAAC,8CAA8C,MAAM,EAAE,CAAC,CAAC;AACvE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deployagram/nock",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Records the HTTP calls that a Nock scope intercepts, and sends each request and response to the Deployagram Collector, in the same format as the Deployagram HTTP Proxy. Use it instead of the Proxy when tests stub HTTP with Nock.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.json",
|
|
27
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit",
|
|
28
|
+
"test": "vitest run"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@deployagram/api": "^1.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^22.20.5",
|
|
35
|
+
"nock": "^15.0.6",
|
|
36
|
+
"typescript": "^7.0.2",
|
|
37
|
+
"vitest": "^5.1.0"
|
|
38
|
+
}
|
|
39
|
+
}
|