@ffflorian/mock-udp 1.5.5 → 1.6.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 +1 -1
- package/dist/{esm/index.js → index.js} +1 -1
- package/dist/{esm/index.test.js → index.test.js} +6 -6
- package/package.json +8 -17
- package/dist/cjs/index.js +0 -67
- package/dist/cjs/index.test.js +0 -144
- package/dist/cjs/package.json +0 -3
- package/dist/esm/index.d.ts +0 -17
- package/dist/esm/index.test.d.ts +0 -1
- package/dist/esm/package.json +0 -3
- /package/dist/{cjs/index.d.ts → index.d.ts} +0 -0
- /package/dist/{cjs/index.test.d.ts → index.test.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Mock dgram udp requests. Based on [node-mock-udp](https://github.com/mattrobenol
|
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
|
-
ℹ️ This is a
|
|
12
|
+
ℹ️ This is a pure [ESM](https://nodejs.org/api/esm.html#introduction) module.
|
|
13
13
|
|
|
14
14
|
Run `yarn add @ffflorian/mock-udp` or `npm i @ffflorian/mock-udp`.
|
|
15
15
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-magic-numbers */
|
|
2
|
+
import * as dgram from 'node:dgram';
|
|
2
3
|
import { assert, expect, describe, test, beforeEach } from 'vitest';
|
|
3
|
-
import * as dgram from 'dgram';
|
|
4
4
|
import * as mockudp from './index.js';
|
|
5
5
|
const buffer = Buffer.from('hello world');
|
|
6
6
|
describe('mock-udp.intercept', () => {
|
|
@@ -36,7 +36,7 @@ describe('mock-udp.clean', () => {
|
|
|
36
36
|
client.send(buffer, 0, buffer.length, 1000 + index, 'localhost');
|
|
37
37
|
assert.fail();
|
|
38
38
|
}
|
|
39
|
-
catch (
|
|
39
|
+
catch (_error) { }
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
42
|
});
|
|
@@ -105,7 +105,7 @@ describe('mock-udp.overriddenSocketSend', () => {
|
|
|
105
105
|
client.send(buffer, 0, buffer.length, 1000, 'localhost');
|
|
106
106
|
assert.fail();
|
|
107
107
|
}
|
|
108
|
-
catch (
|
|
108
|
+
catch (_error) {
|
|
109
109
|
done(void 0);
|
|
110
110
|
}
|
|
111
111
|
});
|
|
@@ -118,7 +118,7 @@ describe('mock-udp.overriddenSocketSend', () => {
|
|
|
118
118
|
client.send(buffer, buffer.length, buffer.length, 1000, 'localhost');
|
|
119
119
|
assert.fail();
|
|
120
120
|
}
|
|
121
|
-
catch (
|
|
121
|
+
catch (_error) { }
|
|
122
122
|
expect(scope.done()).toBe(false);
|
|
123
123
|
});
|
|
124
124
|
test('should throw an error if offset is greater than the length of the buffer', () => {
|
|
@@ -128,7 +128,7 @@ describe('mock-udp.overriddenSocketSend', () => {
|
|
|
128
128
|
client.send(buffer, buffer.length + 1, buffer.length, 1000, 'localhost');
|
|
129
129
|
assert.fail();
|
|
130
130
|
}
|
|
131
|
-
catch (
|
|
131
|
+
catch (_error) { }
|
|
132
132
|
expect(scope.done()).toBe(false);
|
|
133
133
|
});
|
|
134
134
|
test('should throw an error if the length is greater than the length of the buffer', () => {
|
|
@@ -138,7 +138,7 @@ describe('mock-udp.overriddenSocketSend', () => {
|
|
|
138
138
|
client.send(buffer, 0, buffer.length + 1, 1000, 'localhost');
|
|
139
139
|
assert.fail();
|
|
140
140
|
}
|
|
141
|
-
catch (
|
|
141
|
+
catch (_error) { }
|
|
142
142
|
expect(scope.done()).toBe(false);
|
|
143
143
|
});
|
|
144
144
|
});
|
package/package.json
CHANGED
|
@@ -2,37 +2,28 @@
|
|
|
2
2
|
"author": "Florian Imdahl <git@ffflorian.de>",
|
|
3
3
|
"description": "Mock dgram udp requests.",
|
|
4
4
|
"devDependencies": {
|
|
5
|
-
"rimraf": "
|
|
6
|
-
"typescript": "5.5.
|
|
7
|
-
"vitest": "
|
|
5
|
+
"rimraf": "6.0.1",
|
|
6
|
+
"typescript": "5.5.4",
|
|
7
|
+
"vitest": "2.0.5"
|
|
8
8
|
},
|
|
9
9
|
"engines": {
|
|
10
10
|
"node": ">= 18.0"
|
|
11
11
|
},
|
|
12
|
-
"exports":
|
|
13
|
-
".": {
|
|
14
|
-
"import": "./dist/esm/index.js",
|
|
15
|
-
"require": "./dist/cjs/index.js"
|
|
16
|
-
}
|
|
17
|
-
},
|
|
12
|
+
"exports": "./dist/index.js",
|
|
18
13
|
"files": [
|
|
19
14
|
"dist"
|
|
20
15
|
],
|
|
21
16
|
"license": "GPL-3.0",
|
|
22
|
-
"
|
|
23
|
-
"module": "dist/esm/index.js",
|
|
17
|
+
"module": "dist/index.js",
|
|
24
18
|
"name": "@ffflorian/mock-udp",
|
|
25
19
|
"repository": "https://github.com/ffflorian/node-packages/tree/main/packages/mock-udp",
|
|
26
20
|
"scripts": {
|
|
27
|
-
"build": "
|
|
28
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
29
|
-
"build:esm": "tsc -p tsconfig.json",
|
|
21
|
+
"build": "tsc -p tsconfig.json",
|
|
30
22
|
"clean": "rimraf dist",
|
|
31
23
|
"dist": "yarn clean && yarn build",
|
|
32
|
-
"generate:packagejson": "../../bin/generate-hybrid-package-json.sh",
|
|
33
24
|
"test": "vitest run"
|
|
34
25
|
},
|
|
35
26
|
"type": "module",
|
|
36
|
-
"version": "1.
|
|
37
|
-
"gitHead": "
|
|
27
|
+
"version": "1.6.1",
|
|
28
|
+
"gitHead": "aadf648655ecd0f6bea0c989542a16ff636c71a7"
|
|
38
29
|
}
|
package/dist/cjs/index.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { Socket } from 'dgram';
|
|
2
|
-
let intercepts = {};
|
|
3
|
-
const originalSocketSend = Socket.prototype.send;
|
|
4
|
-
class Scope {
|
|
5
|
-
constructor() {
|
|
6
|
-
this._done = false;
|
|
7
|
-
}
|
|
8
|
-
done() {
|
|
9
|
-
return this._done;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function overriddenSocketSend(msg, offsetOrPortOrCallback, lengthOrAddressOrCallback, portOrCallback, addressOrCallback, callbackOrUndefined) {
|
|
13
|
-
const hasLengthAndOffset = typeof portOrCallback === 'number';
|
|
14
|
-
const address = (hasLengthAndOffset ? addressOrCallback : lengthOrAddressOrCallback) || 'localhost';
|
|
15
|
-
const callback = hasLengthAndOffset ? callbackOrUndefined : portOrCallback;
|
|
16
|
-
const length = hasLengthAndOffset ? lengthOrAddressOrCallback || 0 : 0;
|
|
17
|
-
const offset = hasLengthAndOffset ? offsetOrPortOrCallback : 0;
|
|
18
|
-
const port = hasLengthAndOffset ? portOrCallback : offsetOrPortOrCallback;
|
|
19
|
-
if (offset >= msg.length) {
|
|
20
|
-
throw new Error('Offset into buffer too large.');
|
|
21
|
-
}
|
|
22
|
-
if (offset + length > msg.length) {
|
|
23
|
-
throw new Error('Offset + length beyond buffer length.');
|
|
24
|
-
}
|
|
25
|
-
const newBuffer = msg.slice(offset, offset + length);
|
|
26
|
-
const host = `${address}:${port}`;
|
|
27
|
-
if (intercepts.hasOwnProperty(host)) {
|
|
28
|
-
intercepts[host].forEach(scope => {
|
|
29
|
-
scope._done = true;
|
|
30
|
-
scope.address = address;
|
|
31
|
-
scope.buffer = newBuffer;
|
|
32
|
-
scope.length = length;
|
|
33
|
-
scope.offset = offset;
|
|
34
|
-
scope.port = port;
|
|
35
|
-
});
|
|
36
|
-
delete intercepts[host];
|
|
37
|
-
if (callback) {
|
|
38
|
-
setImmediate(() => callback(null, length));
|
|
39
|
-
}
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
throw new Error(`Request sent to unmocked path: ${host}.`);
|
|
43
|
-
}
|
|
44
|
-
overriddenSocketSend._mocked = true;
|
|
45
|
-
function add(path) {
|
|
46
|
-
if (!intercepts.hasOwnProperty(path)) {
|
|
47
|
-
intercepts[path] = [];
|
|
48
|
-
}
|
|
49
|
-
const scope = new Scope();
|
|
50
|
-
intercepts[path].push(scope);
|
|
51
|
-
return scope;
|
|
52
|
-
}
|
|
53
|
-
function cleanInterceptions() {
|
|
54
|
-
intercepts = {};
|
|
55
|
-
}
|
|
56
|
-
function interceptSocketSend() {
|
|
57
|
-
Socket.prototype.send = overriddenSocketSend;
|
|
58
|
-
}
|
|
59
|
-
function isMocked() {
|
|
60
|
-
return Socket.prototype.send.hasOwnProperty('_mocked');
|
|
61
|
-
}
|
|
62
|
-
function restoreSocketSend() {
|
|
63
|
-
Socket.prototype.send = originalSocketSend;
|
|
64
|
-
}
|
|
65
|
-
interceptSocketSend();
|
|
66
|
-
export default add;
|
|
67
|
-
export { add, restoreSocketSend as revert, interceptSocketSend as intercept, cleanInterceptions as clean, isMocked, Scope, };
|
package/dist/cjs/index.test.js
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-magic-numbers */
|
|
2
|
-
import { assert, expect, describe, test, beforeEach } from 'vitest';
|
|
3
|
-
import * as dgram from 'dgram';
|
|
4
|
-
import * as mockudp from './index.js';
|
|
5
|
-
const buffer = Buffer.from('hello world');
|
|
6
|
-
describe('mock-udp.intercept', () => {
|
|
7
|
-
test('should have punched Socket.prototype.send in the face', () => {
|
|
8
|
-
mockudp.intercept();
|
|
9
|
-
expect(mockudp.isMocked()).toBe(true);
|
|
10
|
-
mockudp.revert();
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
describe('mock-udp.revert', () => {
|
|
14
|
-
test('should revert back to the unpunched state', () => {
|
|
15
|
-
mockudp.intercept();
|
|
16
|
-
mockudp.revert();
|
|
17
|
-
expect(mockudp.isMocked()).not.toBe(true);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
describe('mock-udp.add', () => {
|
|
21
|
-
test('should return a new, unused Scope', () => {
|
|
22
|
-
const scope = mockudp.add('');
|
|
23
|
-
expect(scope.done()).toBe(false);
|
|
24
|
-
mockudp.clean();
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
describe('mock-udp.clean', () => {
|
|
28
|
-
test('should clean all interceptions', () => {
|
|
29
|
-
const range = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
30
|
-
range.forEach(index => mockudp.add(`localhost:100${index}`));
|
|
31
|
-
mockudp.intercept();
|
|
32
|
-
mockudp.clean();
|
|
33
|
-
const client = dgram.createSocket('udp4');
|
|
34
|
-
range.forEach(index => {
|
|
35
|
-
try {
|
|
36
|
-
client.send(buffer, 0, buffer.length, 1000 + index, 'localhost');
|
|
37
|
-
assert.fail();
|
|
38
|
-
}
|
|
39
|
-
catch (error) { }
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
describe('mock-udp.overriddenSocketSend', () => {
|
|
44
|
-
beforeEach(() => {
|
|
45
|
-
mockudp.intercept();
|
|
46
|
-
mockudp.clean();
|
|
47
|
-
});
|
|
48
|
-
test('should intercept a basic UDP request', () => {
|
|
49
|
-
return new Promise(done => {
|
|
50
|
-
const scope = mockudp.add('localhost:1000');
|
|
51
|
-
const client = dgram.createSocket('udp4');
|
|
52
|
-
client.send(buffer, 0, buffer.length, 1000, 'localhost', () => {
|
|
53
|
-
scope.done();
|
|
54
|
-
done(void 0);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
test('should not throw an exception with a missing callback', () => {
|
|
59
|
-
mockudp.add('localhost:1000');
|
|
60
|
-
const client = dgram.createSocket('udp4');
|
|
61
|
-
client.send(buffer, 0, buffer.length, 1000, 'localhost');
|
|
62
|
-
});
|
|
63
|
-
test('should return the correct number of bytes to the callback', () => {
|
|
64
|
-
return new Promise(done => {
|
|
65
|
-
mockudp.add('localhost:1000');
|
|
66
|
-
const client = dgram.createSocket('udp4');
|
|
67
|
-
client.send(buffer, 0, 5, 1000, 'localhost', (_, bytes) => {
|
|
68
|
-
expect(bytes).toBe(5);
|
|
69
|
-
done(void 0);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
test('should return a scope with a correct buffer', () => {
|
|
74
|
-
return new Promise(done => {
|
|
75
|
-
const scope = mockudp.add('localhost:1000');
|
|
76
|
-
const client = dgram.createSocket('udp4');
|
|
77
|
-
client.send(buffer, 1, 6, 1000, 'localhost', () => {
|
|
78
|
-
if (!scope.buffer) {
|
|
79
|
-
return assert.fail();
|
|
80
|
-
}
|
|
81
|
-
expect(scope.buffer.toString()).toBe('ello w');
|
|
82
|
-
done(void 0);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
test('should handle two UDP intercepts per request', () => {
|
|
87
|
-
return new Promise(done => {
|
|
88
|
-
const scope1 = mockudp.add('localhost:1000');
|
|
89
|
-
const scope2 = mockudp.add('localhost:1000');
|
|
90
|
-
const client = dgram.createSocket('udp4');
|
|
91
|
-
client.send(buffer, 0, buffer.length, 1000, 'localhost', () => {
|
|
92
|
-
scope1.done();
|
|
93
|
-
scope2.done();
|
|
94
|
-
done(void 0);
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
test('should throw an error when reusing an intercept', () => {
|
|
99
|
-
return new Promise(done => {
|
|
100
|
-
const scope = mockudp.add('localhost:1000');
|
|
101
|
-
const client = dgram.createSocket('udp4');
|
|
102
|
-
client.send(buffer, 0, buffer.length, 1000, 'localhost', () => {
|
|
103
|
-
scope.done();
|
|
104
|
-
try {
|
|
105
|
-
client.send(buffer, 0, buffer.length, 1000, 'localhost');
|
|
106
|
-
assert.fail();
|
|
107
|
-
}
|
|
108
|
-
catch (error) {
|
|
109
|
-
done(void 0);
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
test('should throw an error if offset is equal to the length of the buffer', () => {
|
|
115
|
-
const scope = mockudp.add('localhost:1000');
|
|
116
|
-
const client = dgram.createSocket('udp4');
|
|
117
|
-
try {
|
|
118
|
-
client.send(buffer, buffer.length, buffer.length, 1000, 'localhost');
|
|
119
|
-
assert.fail();
|
|
120
|
-
}
|
|
121
|
-
catch (error) { }
|
|
122
|
-
expect(scope.done()).toBe(false);
|
|
123
|
-
});
|
|
124
|
-
test('should throw an error if offset is greater than the length of the buffer', () => {
|
|
125
|
-
const scope = mockudp.add('localhost:1000');
|
|
126
|
-
const client = dgram.createSocket('udp4');
|
|
127
|
-
try {
|
|
128
|
-
client.send(buffer, buffer.length + 1, buffer.length, 1000, 'localhost');
|
|
129
|
-
assert.fail();
|
|
130
|
-
}
|
|
131
|
-
catch (error) { }
|
|
132
|
-
expect(scope.done()).toBe(false);
|
|
133
|
-
});
|
|
134
|
-
test('should throw an error if the length is greater than the length of the buffer', () => {
|
|
135
|
-
const scope = mockudp.add('localhost:1000');
|
|
136
|
-
const client = dgram.createSocket('udp4');
|
|
137
|
-
try {
|
|
138
|
-
client.send(buffer, 0, buffer.length + 1, 1000, 'localhost');
|
|
139
|
-
assert.fail();
|
|
140
|
-
}
|
|
141
|
-
catch (error) { }
|
|
142
|
-
expect(scope.done()).toBe(false);
|
|
143
|
-
});
|
|
144
|
-
});
|
package/dist/cjs/package.json
DELETED
package/dist/esm/index.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
declare class Scope {
|
|
2
|
-
_done: boolean;
|
|
3
|
-
address?: string;
|
|
4
|
-
buffer?: Buffer | string | Uint8Array | any[];
|
|
5
|
-
length?: number;
|
|
6
|
-
offset?: number;
|
|
7
|
-
port?: number;
|
|
8
|
-
constructor();
|
|
9
|
-
done(): boolean;
|
|
10
|
-
}
|
|
11
|
-
declare function add(path: string): Scope;
|
|
12
|
-
declare function cleanInterceptions(): void;
|
|
13
|
-
declare function interceptSocketSend(): void;
|
|
14
|
-
declare function isMocked(): boolean;
|
|
15
|
-
declare function restoreSocketSend(): void;
|
|
16
|
-
export default add;
|
|
17
|
-
export { add, restoreSocketSend as revert, interceptSocketSend as intercept, cleanInterceptions as clean, isMocked, Scope, };
|
package/dist/esm/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/esm/package.json
DELETED
|
File without changes
|
|
File without changes
|