@netlify/nock-udp 1.0.0 → 3.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/README.md +40 -42
- package/lib/main.d.ts +21 -0
- package/lib/main.js +68 -0
- package/package.json +18 -49
- package/src/main.js +0 -92
package/README.md
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
[](https://github.com/netlify/nock-udp/actions)
|
|
2
|
-
[](https://www.npmjs.com/package/@netlify/nock-udp)
|
|
3
|
-
|
|
4
1
|
# Nock UDP
|
|
5
2
|
|
|
6
|
-
Mock outgoing UDP requests. Useful for testing purposes mostly. Based on
|
|
3
|
+
Mock outgoing UDP requests. Useful for testing purposes mostly. Based on
|
|
4
|
+
[node-mock-udp](https://github.com/mattrobenolt/node-mock-udp).
|
|
7
5
|
|
|
8
6
|
## Install
|
|
9
7
|
|
|
@@ -14,61 +12,61 @@ npm install @netlify/nock-udp
|
|
|
14
12
|
## Usage
|
|
15
13
|
|
|
16
14
|
```js
|
|
17
|
-
|
|
15
|
+
const { Buffer } = require('buffer')
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
const { intercept, cleanAll } = require('@netlify/nock-udp')
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
const buffer = Buffer.from('test')
|
|
20
|
+
const host = 'localhost'
|
|
21
|
+
const port = '1234'
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
// const opts = {
|
|
24
|
+
// persist: false, // Persist the interception for more than one message, defaults to false
|
|
25
|
+
// startIntercept: true // Start overriding Socket.prototype.send, defaults to true
|
|
26
|
+
// allowUnknown: false // If `startIntercept = true` allow messages to addresses which aren't intercepted, defaults to false
|
|
27
|
+
// }
|
|
28
|
+
const opts = {}
|
|
29
|
+
const scope = intercept(`${host}:${port}`, opts)
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
console.log(scope.used)
|
|
32
|
+
// false
|
|
35
33
|
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
const client = createSocket('udp4')
|
|
35
|
+
client.send(buffer, 0, buffer.length, port, host)
|
|
38
36
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
console.log(scope.used)
|
|
38
|
+
// true
|
|
39
|
+
console.log(scope.buffers[0].toString())
|
|
40
|
+
// test
|
|
43
41
|
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
// stop intercepting this address
|
|
43
|
+
scope.clean()
|
|
46
44
|
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
// clean all the interceptions and restore the original Socket.prototype.send
|
|
46
|
+
cleanAll()
|
|
49
47
|
```
|
|
50
48
|
|
|
51
49
|
### Other utility methods
|
|
52
50
|
|
|
53
51
|
```js
|
|
54
|
-
|
|
52
|
+
const { Buffer } = require('buffer')
|
|
55
53
|
|
|
56
|
-
|
|
54
|
+
const { restoreSocketSend, interceptSocketSend, isMocked } = require('@netlify/nock-udp')
|
|
57
55
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
// check if Socket.prototype.send is currently overridden
|
|
57
|
+
console.log(isMocked())
|
|
58
|
+
// false
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
// override Socket.prototype.send with our custom method and start using the interceptors (used by `intercept` if `startIntercept = true`)
|
|
61
|
+
// optionally allow for requests to addresses which are not intercepted
|
|
62
|
+
interceptSocketSend({ allowUnknown: false })
|
|
63
|
+
console.log(isMocked())
|
|
64
|
+
// true
|
|
67
65
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
// restore the original Socket.prototype.send while keeping the current interceptors state
|
|
67
|
+
restoreSocketSend()
|
|
68
|
+
console.log(isMocked())
|
|
69
|
+
// false
|
|
72
70
|
```
|
|
73
71
|
|
|
74
72
|
## Contributors
|
package/lib/main.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function isMocked(): boolean;
|
|
2
|
+
export function intercept(address: any, { persist, startIntercept, allowUnknown }?: {
|
|
3
|
+
persist?: boolean;
|
|
4
|
+
startIntercept?: boolean;
|
|
5
|
+
allowUnknown?: boolean;
|
|
6
|
+
}): {
|
|
7
|
+
used: boolean;
|
|
8
|
+
persist: boolean;
|
|
9
|
+
buffers: any[];
|
|
10
|
+
offset: number;
|
|
11
|
+
length: number;
|
|
12
|
+
address: any;
|
|
13
|
+
clean: () => void;
|
|
14
|
+
};
|
|
15
|
+
export function cleanAll({ stopIntercept }?: {
|
|
16
|
+
stopIntercept?: boolean;
|
|
17
|
+
}): void;
|
|
18
|
+
export function restoreSocketSend(): void;
|
|
19
|
+
export function interceptSocketSend({ allowUnknown }?: {
|
|
20
|
+
allowUnknown: any;
|
|
21
|
+
}): void;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Socket } from 'dgram';
|
|
2
|
+
const originalSocketSend = Socket.prototype.send;
|
|
3
|
+
let intercepts = {};
|
|
4
|
+
const createScope = function (address, { persist = false } = {}) {
|
|
5
|
+
return {
|
|
6
|
+
used: false,
|
|
7
|
+
persist,
|
|
8
|
+
buffers: [],
|
|
9
|
+
offset: 0,
|
|
10
|
+
length: 0,
|
|
11
|
+
address,
|
|
12
|
+
clean: () => {
|
|
13
|
+
delete intercepts[address];
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
const validateIncomingMsg = function (buffer, offset, length, address) {
|
|
18
|
+
if (offset >= buffer.length)
|
|
19
|
+
throw new Error('Offset into buffer too large');
|
|
20
|
+
if (offset + length > buffer.length)
|
|
21
|
+
throw new Error('Offset + length beyond buffer length');
|
|
22
|
+
if (!intercepts[address])
|
|
23
|
+
throw new Error(`Request sent to unmocked path: ${address}`);
|
|
24
|
+
};
|
|
25
|
+
const getMockSocketSend = function ({ allowUnknown = false } = {}) {
|
|
26
|
+
const mockSocketSend = function (buffer, offset, length, port, host, callback) {
|
|
27
|
+
const address = `${host}:${port}`;
|
|
28
|
+
if (allowUnknown && !intercepts[address]) {
|
|
29
|
+
// We allow extraneous connections, fallback to original use
|
|
30
|
+
originalSocketSend.call(this, buffer, offset, length, port, host, callback);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
validateIncomingMsg(buffer, offset, length, address);
|
|
34
|
+
const newBuffer = buffer.slice(offset, offset + length);
|
|
35
|
+
const scope = intercepts[address];
|
|
36
|
+
scope.used = true;
|
|
37
|
+
scope.buffers.push(newBuffer);
|
|
38
|
+
scope.offset = offset;
|
|
39
|
+
scope.length = length;
|
|
40
|
+
if (!scope.persist)
|
|
41
|
+
delete intercepts[address];
|
|
42
|
+
if (callback)
|
|
43
|
+
return callback(null, length);
|
|
44
|
+
};
|
|
45
|
+
mockSocketSend.mocked = true;
|
|
46
|
+
return mockSocketSend;
|
|
47
|
+
};
|
|
48
|
+
export const isMocked = function () {
|
|
49
|
+
return Boolean(Socket.prototype.send.mocked);
|
|
50
|
+
};
|
|
51
|
+
export const intercept = (address, { persist = false, startIntercept = true, allowUnknown = false } = {}) => {
|
|
52
|
+
const scope = createScope(address, { persist });
|
|
53
|
+
intercepts[address] = scope;
|
|
54
|
+
if (!isMocked() && startIntercept)
|
|
55
|
+
interceptSocketSend({ allowUnknown });
|
|
56
|
+
return scope;
|
|
57
|
+
};
|
|
58
|
+
export const cleanAll = function ({ stopIntercept = true } = {}) {
|
|
59
|
+
intercepts = {};
|
|
60
|
+
if (isMocked() && stopIntercept)
|
|
61
|
+
restoreSocketSend();
|
|
62
|
+
};
|
|
63
|
+
export const restoreSocketSend = function () {
|
|
64
|
+
Socket.prototype.send = originalSocketSend;
|
|
65
|
+
};
|
|
66
|
+
export const interceptSocketSend = function ({ allowUnknown } = {}) {
|
|
67
|
+
Socket.prototype.send = getMockSocketSend({ allowUnknown });
|
|
68
|
+
};
|
package/package.json
CHANGED
|
@@ -1,65 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@netlify/nock-udp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Here to mock your UDP packets",
|
|
5
|
-
"
|
|
5
|
+
"exports": "./lib/main.js",
|
|
6
|
+
"main": "./lib/main.js",
|
|
7
|
+
"types": "./lib/main.d.ts",
|
|
8
|
+
"type": "module",
|
|
6
9
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
"!src/**/*.test.js"
|
|
10
|
+
"lib/**/*"
|
|
9
11
|
],
|
|
10
12
|
"scripts": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"format:check-fix:lint": "run-e format:check:lint format:fix:lint",
|
|
16
|
-
"format:check:lint": "cross-env-shell eslint $npm_package_config_eslint",
|
|
17
|
-
"format:fix:lint": "cross-env-shell eslint --fix $npm_package_config_eslint",
|
|
18
|
-
"format:check-fix:prettier": "run-e format:check:prettier format:fix:prettier",
|
|
19
|
-
"format:check:prettier": "cross-env-shell prettier --check $npm_package_config_prettier",
|
|
20
|
-
"format:fix:prettier": "cross-env-shell prettier --write $npm_package_config_prettier",
|
|
21
|
-
"test:dev": "run-s test:dev:*",
|
|
22
|
-
"test:ci": "run-s test:ci:*",
|
|
23
|
-
"test:dev:ava": "ava",
|
|
24
|
-
"test:ci:ava": "nyc -r lcovonly -r text -r json ava"
|
|
25
|
-
},
|
|
26
|
-
"config": {
|
|
27
|
-
"eslint": "--ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{src,scripts,.github}/**/*.{js,md,html}\" \"*.{js,md,html}\" \".*.{js,md,html}\"",
|
|
28
|
-
"prettier": "--ignore-path .gitignore --loglevel=warn \"{src,scripts,.github}/**/*.{js,md,yml,json,html}\" \"*.{js,yml,json,html}\" \".*.{js,yml,json,html}\" \"!**/package-lock.json\" \"!package-lock.json\""
|
|
29
|
-
},
|
|
30
|
-
"ava": {
|
|
31
|
-
"files": [
|
|
32
|
-
"test/**/*.js",
|
|
33
|
-
"!test/helpers"
|
|
34
|
-
],
|
|
35
|
-
"verbose": true
|
|
36
|
-
},
|
|
37
|
-
"husky": {
|
|
38
|
-
"hooks": {
|
|
39
|
-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
|
|
40
|
-
"pre-push": "npm run format"
|
|
41
|
-
}
|
|
13
|
+
"prebuild": "rm -rf lib",
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"test": "ava",
|
|
16
|
+
"test:ci": "c8 -r lcovonly -r text -r json ava"
|
|
42
17
|
},
|
|
43
18
|
"keywords": [],
|
|
44
19
|
"license": "MIT",
|
|
45
|
-
"repository": "netlify/nock-udp",
|
|
46
20
|
"bugs": {
|
|
47
|
-
"url": "https://github.com/netlify/
|
|
21
|
+
"url": "https://github.com/netlify/build/issues"
|
|
48
22
|
},
|
|
49
23
|
"author": "Netlify Inc.",
|
|
50
|
-
"directories": {
|
|
51
|
-
"test": "test"
|
|
52
|
-
},
|
|
53
|
-
"dependencies": {},
|
|
54
24
|
"devDependencies": {
|
|
55
|
-
"@
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"husky": "^4.3.8",
|
|
60
|
-
"nyc": "^15.0.0"
|
|
25
|
+
"@types/node": "^14.18.31",
|
|
26
|
+
"ava": "^4.0.0",
|
|
27
|
+
"c8": "^7.11.0",
|
|
28
|
+
"typescript": "^4.8.4"
|
|
61
29
|
},
|
|
62
30
|
"engines": {
|
|
63
|
-
"node": ">=
|
|
64
|
-
}
|
|
31
|
+
"node": "^14.16.0 || >=16.0.0"
|
|
32
|
+
},
|
|
33
|
+
"gitHead": "c4502f1c112e7f33d5e6fa053bfbe7dabdfe0160"
|
|
65
34
|
}
|
package/src/main.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Socket } = require('dgram')
|
|
4
|
-
|
|
5
|
-
const originalSocketSend = Socket.prototype.send
|
|
6
|
-
|
|
7
|
-
let intercepts = {}
|
|
8
|
-
|
|
9
|
-
const createScope = function (address, { persist = false } = {}) {
|
|
10
|
-
return {
|
|
11
|
-
used: false,
|
|
12
|
-
persist,
|
|
13
|
-
buffers: [],
|
|
14
|
-
offset: 0,
|
|
15
|
-
length: 0,
|
|
16
|
-
address,
|
|
17
|
-
clean: () => {
|
|
18
|
-
delete intercepts[address]
|
|
19
|
-
},
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const validateIncomingMsg = function (buffer, offset, length, address) {
|
|
24
|
-
if (offset >= buffer.length) throw new Error('Offset into buffer too large')
|
|
25
|
-
|
|
26
|
-
if (offset + length > buffer.length) throw new Error('Offset + length beyond buffer length')
|
|
27
|
-
|
|
28
|
-
if (!intercepts[address]) throw new Error(`Request sent to unmocked path: ${address}`)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const getMockSocketSend = function ({ allowUnknown = false } = {}) {
|
|
32
|
-
// eslint-disable-next-line max-params
|
|
33
|
-
const mockSocketSend = function (buffer, offset, length, port, host, callback) {
|
|
34
|
-
const address = `${host}:${port}`
|
|
35
|
-
if (allowUnknown && !intercepts[address]) {
|
|
36
|
-
// We allow extraneous connections, fallback to original use
|
|
37
|
-
// eslint-disable-next-line no-invalid-this
|
|
38
|
-
originalSocketSend.call(this, buffer, offset, length, port, host, callback)
|
|
39
|
-
return
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
validateIncomingMsg(buffer, offset, length, address)
|
|
43
|
-
|
|
44
|
-
const newBuffer = buffer.slice(offset, offset + length)
|
|
45
|
-
|
|
46
|
-
const scope = intercepts[address]
|
|
47
|
-
scope.used = true
|
|
48
|
-
scope.buffers.push(newBuffer)
|
|
49
|
-
scope.offset = offset
|
|
50
|
-
scope.length = length
|
|
51
|
-
|
|
52
|
-
if (!scope.persist) delete intercepts[address]
|
|
53
|
-
|
|
54
|
-
if (callback) return callback(null, length)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
mockSocketSend.mocked = true
|
|
58
|
-
|
|
59
|
-
return mockSocketSend
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const isMocked = function () {
|
|
63
|
-
return Boolean(Socket.prototype.send.mocked)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const intercept = function (address, { persist = false, startIntercept = true, allowUnknown = false } = {}) {
|
|
67
|
-
const scope = createScope(address, { persist })
|
|
68
|
-
intercepts[address] = scope
|
|
69
|
-
if (!isMocked() && startIntercept) interceptSocketSend({ allowUnknown })
|
|
70
|
-
return scope
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const cleanAll = function ({ stopIntercept = true } = {}) {
|
|
74
|
-
intercepts = {}
|
|
75
|
-
if (isMocked() && stopIntercept) restoreSocketSend()
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const restoreSocketSend = function () {
|
|
79
|
-
Socket.prototype.send = originalSocketSend
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const interceptSocketSend = function ({ allowUnknown } = {}) {
|
|
83
|
-
Socket.prototype.send = getMockSocketSend({ allowUnknown })
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
module.exports = {
|
|
87
|
-
intercept,
|
|
88
|
-
cleanAll,
|
|
89
|
-
restoreSocketSend,
|
|
90
|
-
interceptSocketSend,
|
|
91
|
-
isMocked,
|
|
92
|
-
}
|