@phystack/socket.io-proxy 4.3.40-dev
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/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +115 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
- package/src/index.ts +108 -0
- package/tsconfig.json +45 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAM,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAwC9C,eAAO,MAAM,oBAAoB,GAAU,KAAK,MAAM,EAAE,UAAS,GAAQ,KAAG,OAAO,CAAC,MAAM,CAiEzF,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getSocketIOWithProxy = void 0;
|
|
37
|
+
const socket_io_client_1 = require("socket.io-client");
|
|
38
|
+
const proxyStr = process.env.HTTPS_PROXY ||
|
|
39
|
+
process.env.https_proxy ||
|
|
40
|
+
process.env.HTTP_PROXY ||
|
|
41
|
+
process.env.http_proxy;
|
|
42
|
+
const shouldSkipProxy = (url) => {
|
|
43
|
+
if (typeof window !== 'undefined') {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
const noProxyStr = typeof window === 'undefined' ? (process.env.NO_PROXY || process.env.no_proxy) : undefined;
|
|
47
|
+
if (!noProxyStr)
|
|
48
|
+
return false;
|
|
49
|
+
const noProxyList = noProxyStr
|
|
50
|
+
.split(',')
|
|
51
|
+
.map(np => np.trim().toLowerCase())
|
|
52
|
+
.filter(Boolean);
|
|
53
|
+
const hostname = new URL(url).hostname.toLowerCase();
|
|
54
|
+
return noProxyList.some(entry => {
|
|
55
|
+
if (entry === '*') {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return hostname.endsWith(entry);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const getSocketIOWithProxy = async (url, options = {}) => {
|
|
62
|
+
return new Promise(async (resolve) => {
|
|
63
|
+
try {
|
|
64
|
+
const deviceId = options?.auth?.deviceId;
|
|
65
|
+
const extraHeaders = options.extraHeaders || {};
|
|
66
|
+
if (deviceId) {
|
|
67
|
+
extraHeaders.Cookie = `deviceId=${deviceId}`;
|
|
68
|
+
}
|
|
69
|
+
const transportOptions = {
|
|
70
|
+
transports: ['websocket']
|
|
71
|
+
};
|
|
72
|
+
const updatedOptions = {
|
|
73
|
+
...options,
|
|
74
|
+
extraHeaders,
|
|
75
|
+
...transportOptions
|
|
76
|
+
};
|
|
77
|
+
if (!proxyStr || shouldSkipProxy(url)) {
|
|
78
|
+
resolve((0, socket_io_client_1.io)(url, updatedOptions));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (typeof window === 'undefined') {
|
|
82
|
+
const HttpsProxyAgent = await Promise.resolve().then(() => __importStar(require('https-proxy-agent')));
|
|
83
|
+
const agent = new HttpsProxyAgent.HttpsProxyAgent(proxyStr);
|
|
84
|
+
resolve((0, socket_io_client_1.io)(url, {
|
|
85
|
+
transportOptions: {
|
|
86
|
+
websocket: { agent },
|
|
87
|
+
},
|
|
88
|
+
reconnection: true,
|
|
89
|
+
reconnectionDelayMax: 10000,
|
|
90
|
+
reconnectionAttempts: Infinity,
|
|
91
|
+
extraHeaders,
|
|
92
|
+
...transportOptions,
|
|
93
|
+
...options,
|
|
94
|
+
}));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
console.warn(`Invalid proxy URL: ${proxyStr}. Creating Socket.IO instance without proxy.`);
|
|
100
|
+
}
|
|
101
|
+
const deviceId = options?.auth?.deviceId;
|
|
102
|
+
const extraHeaders = options.extraHeaders || {};
|
|
103
|
+
if (deviceId) {
|
|
104
|
+
extraHeaders.Cookie = `deviceId=${deviceId}`;
|
|
105
|
+
}
|
|
106
|
+
resolve((0, socket_io_client_1.io)(url, {
|
|
107
|
+
...options,
|
|
108
|
+
extraHeaders,
|
|
109
|
+
transports: ['websocket']
|
|
110
|
+
}));
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
exports.getSocketIOWithProxy = getSocketIOWithProxy;
|
|
114
|
+
exports.default = exports.getSocketIOWithProxy;
|
|
115
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uDAA8C;AAE9C,MAAM,QAAQ,GACZ,OAAO,CAAC,GAAG,CAAC,WAAW;IACvB,OAAO,CAAC,GAAG,CAAC,WAAW;IACvB,OAAO,CAAC,GAAG,CAAC,UAAU;IACtB,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;AAKzB,MAAM,eAAe,GAAG,CAAC,GAAW,EAAW,EAAE;IAE/C,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC9G,IAAI,CAAC,UAAU;QAAE,OAAO,KAAK,CAAC;IAG9B,MAAM,WAAW,GAAG,UAAU;SAC3B,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;SAClC,MAAM,CAAC,OAAO,CAAC,CAAC;IAEnB,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;IAIrD,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;QAE9B,IAAI,KAAK,KAAK,G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;oBAC9B,YAAY;oBACZ,GAAG,gBAAgB;oBACnB,GAAG,OAAO;iBACX,CAAC,CAAC,CAAC;gBACJ,OAAO;YACT,CAAC;QAEH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,IAAI,CAAC,sBAAsB,QAAQ,8CAA8C,CAAC,CAAC;QAC7F,CAAC;QAGD,MAAM,QAAQ,GAAG,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC;QACzC,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,EAAE,CAAC;QAChD,IAAI,QAAQ,EAAE,CAAC;YACb,YAAY,CAAC,MAAM,GAAG,YAAY,QAAQ,EAAE,CAAC;QAC/C,CAAC;QAID,OAAO,CAAC,IAAA,qBAAE,EAAC,GAAG,EAAE;YACd,GAAG,OAAO;YACV,YAAY;YACZ,UAAU,EAAE,CAAC,WAAW,CAAC;SAC1B,CAAC,CAAC,CAAC;IACN,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAjEW,QAAA,oBAAoB,wBAiE/B;AAEF,kBAAe,4BAAoB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@phystack/socket.io-proxy",
|
|
3
|
+
"version": "4.3.40-dev",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"private": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "rimraf dist && tsc",
|
|
12
|
+
"format": "prettier --write \"src/**/*.{ts,js,json,md}\"",
|
|
13
|
+
"format:check": "prettier --check \"src/**/*.{ts,js,json,md}\""
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"axios": "^1.7.9",
|
|
17
|
+
"bufferutil": "^4.0.9",
|
|
18
|
+
"socket.io-client": "^4.7.5",
|
|
19
|
+
"utf-8-validate": "^6.0.5"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"prettier": "^3.4.2",
|
|
23
|
+
"rimraf": "^5.0.7",
|
|
24
|
+
"typescript": "^5.4.5"
|
|
25
|
+
},
|
|
26
|
+
"optionalDependencies": {
|
|
27
|
+
"http-proxy-agent": "^7.0.2",
|
|
28
|
+
"https-proxy-agent": "^7.0.6"
|
|
29
|
+
},
|
|
30
|
+
"test": "react-scripts test",
|
|
31
|
+
"test:coverage": "npm run test -- --coverage --watchAll=false",
|
|
32
|
+
"gitHead": "53c505b3ecad47ebd4f38819c07b0ec1227214a6"
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { io, Socket } from 'socket.io-client';
|
|
2
|
+
|
|
3
|
+
const proxyStr =
|
|
4
|
+
process.env.HTTPS_PROXY ||
|
|
5
|
+
process.env.https_proxy ||
|
|
6
|
+
process.env.HTTP_PROXY ||
|
|
7
|
+
process.env.http_proxy;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Checks whether we should skip using a proxy based on the NO_PROXY or no_proxy environment variable.
|
|
11
|
+
*/
|
|
12
|
+
const shouldSkipProxy = (url: string): boolean => {
|
|
13
|
+
// In browser, always skip proxy
|
|
14
|
+
if (typeof window !== 'undefined') {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const noProxyStr = typeof window === 'undefined' ? (process.env.NO_PROXY || process.env.no_proxy) : undefined;
|
|
19
|
+
if (!noProxyStr) return false;
|
|
20
|
+
|
|
21
|
+
// Convert the comma-separated list of no_proxy entries into an array
|
|
22
|
+
const noProxyList = noProxyStr
|
|
23
|
+
.split(',')
|
|
24
|
+
.map(np => np.trim().toLowerCase())
|
|
25
|
+
.filter(Boolean);
|
|
26
|
+
|
|
27
|
+
const hostname = new URL(url).hostname.toLowerCase();
|
|
28
|
+
|
|
29
|
+
// If the hostname is found in the list (allowing for suffix matches like '.example.com'),
|
|
30
|
+
// we consider that we should not use a proxy
|
|
31
|
+
return noProxyList.some(entry => {
|
|
32
|
+
// If entry is '*', then skip proxy for all
|
|
33
|
+
if (entry === '*') {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
// If no wildcard, require that the host ends with the entry
|
|
37
|
+
return hostname.endsWith(entry);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const getSocketIOWithProxy = async (url: string, options: any = {}): Promise<Socket> => {
|
|
42
|
+
return new Promise(async (resolve) => {
|
|
43
|
+
try {
|
|
44
|
+
// Extract deviceId from options if present
|
|
45
|
+
const deviceId = options?.auth?.deviceId;
|
|
46
|
+
|
|
47
|
+
// Create extraHeaders with cookie if deviceId is available
|
|
48
|
+
const extraHeaders = options.extraHeaders || {};
|
|
49
|
+
if (deviceId) {
|
|
50
|
+
extraHeaders.Cookie = `deviceId=${deviceId}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Force WebSocket transport only - prevent polling fallback
|
|
54
|
+
const transportOptions = {
|
|
55
|
+
transports: ['websocket']
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Update options with extraHeaders and transport settings
|
|
59
|
+
const updatedOptions = {
|
|
60
|
+
...options,
|
|
61
|
+
extraHeaders,
|
|
62
|
+
...transportOptions
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
if (!proxyStr || shouldSkipProxy(url)) {
|
|
66
|
+
resolve(io(url, updatedOptions));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (typeof window === 'undefined') {
|
|
71
|
+
const HttpsProxyAgent = await import('https-proxy-agent');
|
|
72
|
+
const agent = new HttpsProxyAgent.HttpsProxyAgent(proxyStr);
|
|
73
|
+
resolve(io(url, {
|
|
74
|
+
transportOptions: {
|
|
75
|
+
websocket: { agent },
|
|
76
|
+
},
|
|
77
|
+
reconnection: true,
|
|
78
|
+
reconnectionDelayMax: 10000,
|
|
79
|
+
reconnectionAttempts: Infinity,
|
|
80
|
+
extraHeaders,
|
|
81
|
+
...transportOptions,
|
|
82
|
+
...options,
|
|
83
|
+
}));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.warn(`Invalid proxy URL: ${proxyStr}. Creating Socket.IO instance without proxy.`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Ensure we still have the cookie in the fallback case
|
|
92
|
+
const deviceId = options?.auth?.deviceId;
|
|
93
|
+
const extraHeaders = options.extraHeaders || {};
|
|
94
|
+
if (deviceId) {
|
|
95
|
+
extraHeaders.Cookie = `deviceId=${deviceId}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Fallback to creating a Socket.IO instance without proxy if an error occurs
|
|
99
|
+
// Still force WebSocket transport only
|
|
100
|
+
resolve(io(url, {
|
|
101
|
+
...options,
|
|
102
|
+
extraHeaders,
|
|
103
|
+
transports: ['websocket']
|
|
104
|
+
}));
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export default getSocketIOWithProxy;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "commonjs",
|
|
4
|
+
"target": "es2022",
|
|
5
|
+
"outDir": "dist",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"baseUrl": "src",
|
|
8
|
+
"removeComments": true,
|
|
9
|
+
"moduleResolution": "node",
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"alwaysStrict": true,
|
|
14
|
+
"allowJs": true,
|
|
15
|
+
"noEmitOnError": true,
|
|
16
|
+
"noFallthroughCasesInSwitch": true,
|
|
17
|
+
"noImplicitAny": true,
|
|
18
|
+
"noImplicitReturns": false,
|
|
19
|
+
"noImplicitThis": true,
|
|
20
|
+
"noUnusedLocals": true,
|
|
21
|
+
"noUnusedParameters": true,
|
|
22
|
+
"strictBindCallApply": true,
|
|
23
|
+
"strictNullChecks": true,
|
|
24
|
+
"allowSyntheticDefaultImports": true,
|
|
25
|
+
"resolveJsonModule": true,
|
|
26
|
+
"esModuleInterop": true,
|
|
27
|
+
"declarationDir": "dist",
|
|
28
|
+
"declarationMap": true,
|
|
29
|
+
"declaration": true,
|
|
30
|
+
"paths": {
|
|
31
|
+
"@/*": ["*"]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"include": [
|
|
35
|
+
"./src/**/*"
|
|
36
|
+
, "src/__tests__" ],
|
|
37
|
+
"exclude": [
|
|
38
|
+
"node_modules/**/*",
|
|
39
|
+
".serverless/**/*",
|
|
40
|
+
".webpack/**/*",
|
|
41
|
+
"_warmup/**/*",
|
|
42
|
+
"dist/**/*",
|
|
43
|
+
".vscode/**/*",
|
|
44
|
+
],
|
|
45
|
+
}
|