@deployagram/msw 0.0.0-stage → 0.1.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 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,40 @@
1
- # Temporary Holding Version
1
+ # @deployagram/msw
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Records HTTP calls that [MSW](https://mswjs.io) intercepts, and sends them to the Deployagram
4
+ Collector. It is the alternative to the Deployagram HTTP Proxy for tests that stub HTTP with MSW:
5
+ the calls never leave the test process, so there is no Proxy to route them through.
6
+
7
+ ```ts
8
+ import { setupServer } from 'msw/node';
9
+ import { recordHttp } from '@deployagram/msw';
10
+ import { DiagramLoggerClient } from '@deployagram/logger-client';
11
+
12
+ const server = setupServer(/* your handlers */);
13
+ server.listen({ onUnhandledFrame: 'error' });
14
+
15
+ const recorder = recordHttp(server, {
16
+ logger: new DiagramLoggerClient({ baseUrl: 'http://localhost:1152' }),
17
+ caller: 'EmailClient',
18
+ callees: [{ name: 'Emailer', urlPrefix: 'http://localhost:9001' }],
19
+ });
20
+
21
+ // After each test, before marking the test's end:
22
+ await recorder.flush();
23
+ ```
24
+
25
+ Each request and response becomes one log entry in the same format the Proxy writes, so the
26
+ Collector and Deployagram Cloud read them in the same way.
27
+
28
+ ## Differences from the Proxy
29
+
30
+ - The entries are timestamped when the event happens, not by the Collector.
31
+ - Repeated header values are joined with commas, not kept as separate values.
32
+ - A failure to reach the Collector is reported with a warning, and does not fail the test.
33
+
34
+ ## Testing
35
+
36
+ ```bash
37
+ npm test
38
+ ```
39
+
40
+ The tests use MSW and a logger that keeps entries in memory. They need no Collector.
@@ -0,0 +1,29 @@
1
+ import type { DiagramLogger } from '@deployagram/api';
2
+ import type { SetupServer } from 'msw/node';
3
+ /** An application that HTTP calls may go to, and the name the diagrams show for it. */
4
+ export interface Callee {
5
+ name: string;
6
+ /** Calls to URLs starting with this are recorded as calls to `name`, for example "http://localhost:9001". */
7
+ urlPrefix: string;
8
+ }
9
+ export interface RecordHttpOptions {
10
+ logger: DiagramLogger;
11
+ /** The name of the application making the calls, for example "EmailClient". */
12
+ caller: string;
13
+ callees: Callee[];
14
+ }
15
+ export interface HttpRecorder {
16
+ /** Resolves once every call recorded so far has been sent to the Collector. */
17
+ flush(): Promise<void>;
18
+ /** Stops recording. */
19
+ stop(): void;
20
+ }
21
+ /**
22
+ * Records the HTTP calls that MSW intercepts, and sends each request and response to the Collector
23
+ * in the same format the Deployagram HTTP Proxy uses. Calls to hosts that are not in `callees` are
24
+ * not recorded.
25
+ *
26
+ * Use it in place of the Proxy when tests stub HTTP with MSW: the calls never leave the test process,
27
+ * so there is no Proxy to route them through.
28
+ */
29
+ export declare function recordHttp(server: SetupServer, options: RecordHttpOptions): HttpRecorder;
package/dist/index.js ADDED
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Records the HTTP calls that MSW intercepts, and sends each request and response to the Collector
3
+ * in the same format the Deployagram HTTP Proxy uses. Calls to hosts that are not in `callees` are
4
+ * not recorded.
5
+ *
6
+ * Use it in place of the Proxy when tests stub HTTP with MSW: the calls never leave the test process,
7
+ * so there is no Proxy to route them through.
8
+ */
9
+ export function recordHttp(server, options) {
10
+ const { logger, caller, callees } = options;
11
+ let active = true;
12
+ // Entries are recorded one after another, in the order their events fired, so a request is always
13
+ // recorded before its response, as the Proxy does.
14
+ let queue = Promise.resolve();
15
+ const track = (work) => {
16
+ queue = queue
17
+ .then(work)
18
+ .catch((error) => {
19
+ const reason = error instanceof Error ? error.message : String(error);
20
+ console.warn(`Deployagram: could not record an HTTP call: ${reason}`);
21
+ });
22
+ };
23
+ const send = (log) => logger.log(log);
24
+ const calleeFor = (url) => callees.find((callee) => url.startsWith(callee.urlPrefix));
25
+ const recordRequest = async (request, callee) => {
26
+ const body = request.body === null ? undefined : await request.clone().text();
27
+ const content = { Headers: headersOf(request.headers) };
28
+ if (body !== undefined && body !== '')
29
+ content.Body = body;
30
+ content.Endpoint = endpointOf(request.url, callee);
31
+ await send({
32
+ timestamp: new Date().toISOString(),
33
+ protocol: 'HTTP',
34
+ source: caller,
35
+ target: callee.name,
36
+ message: `${request.method} Request from ${caller} to ${callee.name}`,
37
+ body: JSON.stringify(content),
38
+ });
39
+ };
40
+ server.events.on('request:start', ({ request }) => {
41
+ const callee = calleeFor(request.url);
42
+ if (!active || !callee)
43
+ return;
44
+ track(() => recordRequest(request, callee));
45
+ });
46
+ const recordResponse = async (request, response, callee) => {
47
+ const body = await response.clone().text();
48
+ // As the Proxy does, an empty response has a null Body rather than no Body.
49
+ const content = {
50
+ Headers: headersOf(response.headers),
51
+ Body: body === '' ? null : body,
52
+ ResponseCode: response.status,
53
+ };
54
+ await send({
55
+ timestamp: new Date().toISOString(),
56
+ protocol: 'HTTP',
57
+ source: callee.name,
58
+ target: caller,
59
+ message: `${request.method} Response from ${callee.name} to ${caller}`,
60
+ body: JSON.stringify(content),
61
+ });
62
+ };
63
+ const onResponse = ({ request, response }) => {
64
+ const callee = calleeFor(request.url);
65
+ if (!active || !callee)
66
+ return;
67
+ track(() => recordResponse(request, response, callee));
68
+ };
69
+ server.events.on('response:mocked', onResponse);
70
+ server.events.on('response:bypass', onResponse);
71
+ return {
72
+ async flush() {
73
+ let current;
74
+ do {
75
+ current = queue;
76
+ await current;
77
+ } while (current !== queue);
78
+ },
79
+ stop() {
80
+ active = false;
81
+ },
82
+ };
83
+ }
84
+ /** Headers as the Proxy writes them: each name maps to a list of values. */
85
+ function headersOf(headers) {
86
+ const result = {};
87
+ headers.forEach((value, name) => {
88
+ result[name] = [value];
89
+ });
90
+ return result;
91
+ }
92
+ /** The path within the callee, without its leading slash, as the Proxy writes it: "Emailer/email". */
93
+ function endpointOf(url, callee) {
94
+ const parsed = new URL(url);
95
+ const prefixPath = new URL(callee.urlPrefix).pathname.replace(/\/$/, '');
96
+ const path = parsed.pathname.slice(prefixPath.length).replace(/^\//, '');
97
+ return `${path}${parsed.search}`;
98
+ }
99
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAwBA;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAC,MAAmB,EAAE,OAA0B;IACxE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAC5C,IAAI,MAAM,GAAG,IAAI,CAAC;IAElB,kGAAkG;IAClG,mDAAmD;IACnD,IAAI,KAAK,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;IAC7C,MAAM,KAAK,GAAG,CAAC,IAAyB,EAAQ,EAAE;QAChD,KAAK,GAAG,KAAK;aACV,IAAI,CAAC,IAAI,CAAC;aACV,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;YACxB,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACtE,OAAO,CAAC,IAAI,CAAC,+CAA+C,MAAM,EAAE,CAAC,CAAC;QACxE,CAAC,CAAC,CAAC;IACP,CAAC,CAAC;IAEF,MAAM,IAAI,GAAG,CAAC,GAAQ,EAAiB,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAE1D,MAAM,SAAS,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,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;IAEhD,OAAO;QACL,KAAK,CAAC,KAAK;YACT,IAAI,OAAsB,CAAC;YAC3B,GAAG,CAAC;gBACF,OAAO,GAAG,KAAK,CAAC;gBAChB,MAAM,OAAO,CAAC;YAChB,CAAC,QAAQ,OAAO,KAAK,KAAK,EAAE;QAC9B,CAAC;QACD,IAAI;YACF,MAAM,GAAG,KAAK,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,4EAA4E;AAC5E,SAAS,SAAS,CAAC,OAAgB;IACjC,MAAM,MAAM,GAA6B,EAAE,CAAC;IAC5C,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QAC9B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,sGAAsG;AACtG,SAAS,UAAU,CAAC,GAAW,EAAE,MAAc;IAC7C,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;IAC5B,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACzE,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACzE,OAAO,GAAG,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;AACnC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,43 @@
1
1
  {
2
2
  "name": "@deployagram/msw",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Records HTTP calls intercepted by MSW (Mock Service Worker) and sends them to the Deployagram Collector, in the same format as the Deployagram HTTP Proxy. Use it instead of the Proxy when tests stub HTTP with MSW.",
5
+ "type": "module",
6
+ "license": "SEE LICENSE IN LICENSE",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "LICENSE",
18
+ "README.md"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "engines": {
24
+ "node": ">=22"
25
+ },
26
+ "scripts": {
27
+ "build": "tsc -p tsconfig.json",
28
+ "typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit",
29
+ "test": "vitest run"
30
+ },
31
+ "dependencies": {
32
+ "@deployagram/api": "^0.1.0"
33
+ },
34
+ "peerDependencies": {
35
+ "msw": "^3.0.4"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^22.20.5",
39
+ "msw": "^3.0.4",
40
+ "typescript": "^7.0.2",
41
+ "vitest": "^5.1.0"
42
+ }
43
+ }