@modern-js/plugin-proxy 1.2.1 → 1.2.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @modern-js/plugin-proxy
2
2
 
3
+ ## 1.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 83166714: change .npmignore
8
+ - Updated dependencies [83166714]
9
+ - Updated dependencies [c3de9882]
10
+ - Updated dependencies [33ff48af]
11
+ - @modern-js/core@1.3.2
12
+ - @modern-js/utils@1.2.2
13
+
3
14
  ## 1.2.1
4
15
 
5
16
  ### Patch Changes
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.1",
14
+ "version": "1.2.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -33,20 +33,20 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@babel/runtime": "^7",
36
- "@modern-js/utils": "^1.2.1",
36
+ "@modern-js/utils": "^1.2.2",
37
37
  "whistle": "^2.7.18"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@types/jest": "^26",
41
41
  "@types/node": "^14",
42
42
  "typescript": "^4",
43
- "@modern-js/core": "^1.3.1",
43
+ "@modern-js/core": "^1.3.2",
44
44
  "@scripts/build": "0.0.0",
45
45
  "jest": "^27",
46
46
  "@scripts/jest-config": "0.0.0"
47
47
  },
48
48
  "peerDependencies": {
49
- "@modern-js/core": "^1.3.1"
49
+ "@modern-js/core": "^1.3.2"
50
50
  },
51
51
  "sideEffects": false,
52
52
  "modernConfig": {
package/src/index.ts DELETED
@@ -1,44 +0,0 @@
1
- import {
2
- createPlugin,
3
- useAppContext,
4
- useResolvedConfigContext,
5
- } from '@modern-js/core';
6
- import { PLUGIN_SCHEMAS } from '@modern-js/utils';
7
- import { createProxyRule } from './utils/createProxyRule';
8
- import WhistleProxy from './utils/whistleProxy';
9
-
10
- let proxyServer: WhistleProxy;
11
- export default createPlugin(
12
- () => ({
13
- validateSchema() {
14
- return PLUGIN_SCHEMAS['@modern-js/plugin-proxy'];
15
- },
16
- async afterDev() {
17
- /* eslint-disable react-hooks/rules-of-hooks */
18
- const { dev } = useResolvedConfigContext();
19
- const { internalDirectory } = useAppContext();
20
- /* eslint-enable react-hooks/rules-of-hooks */
21
-
22
- if (!dev?.proxy) {
23
- return;
24
- }
25
-
26
- const rule = createProxyRule(internalDirectory, dev.proxy);
27
- proxyServer = new WhistleProxy({ port: 8899, rule });
28
- await proxyServer.start();
29
- },
30
- beforeExit() {
31
- // terminate whistle proxy
32
- proxyServer?.close();
33
- },
34
- }),
35
- { name: '@modern-js/plugin-proxy' },
36
- ) as any;
37
-
38
- export type ProxyOptions = string | Record<string, string>;
39
-
40
- declare module '@modern-js/core' {
41
- export interface DevConfig {
42
- proxy?: ProxyOptions;
43
- }
44
- }
@@ -1,50 +0,0 @@
1
- import path from 'path';
2
- import { logger, fs } from '@modern-js/utils';
3
- import type { ProxyOptions } from '..';
4
-
5
- interface ProxyRule {
6
- pattern: string;
7
- target: string;
8
- }
9
-
10
- const createWhistleProxyRule = (ruleDirectory: string, rules: ProxyRule[]) => {
11
- const dest = path.resolve(ruleDirectory, 'proxy.rule.js');
12
-
13
- let code = `/.*/ enable://intercept\n`;
14
-
15
- for (const rule of rules) {
16
- const { pattern, target } = rule;
17
- code += `${pattern} ${target}\n`;
18
- }
19
-
20
- fs.outputFileSync(
21
- dest,
22
- `exports.name = 'modernjs proxy rule';\nexports.rules = \`${code}\`;`,
23
- );
24
- return dest;
25
- };
26
-
27
- export const createProxyRule = (
28
- appDirectory: string,
29
- proxyOptions: ProxyOptions,
30
- ) => {
31
- const rules = [];
32
-
33
- if (proxyOptions && typeof proxyOptions === 'string') {
34
- return proxyOptions;
35
- }
36
-
37
- if (typeof proxyOptions === 'object') {
38
- for (const pattern of Object.keys(proxyOptions)) {
39
- const target = proxyOptions[pattern];
40
- if (!target || typeof target !== 'string') {
41
- logger.error(`dev.proxy.${pattern} value should be string type`);
42
- // eslint-disable-next-line no-process-exit
43
- process.exit(1);
44
- }
45
- rules.push({ pattern, target });
46
- }
47
- }
48
-
49
- return createWhistleProxyRule(appDirectory, rules);
50
- };
@@ -1,21 +0,0 @@
1
- import { execSync as nodeExecSync } from 'child_process';
2
-
3
- function execSync(cmd: string) {
4
- let stdout;
5
- let status = 0;
6
- try {
7
- stdout = nodeExecSync(cmd);
8
- } catch (err: any) {
9
- /* eslint-disable prefer-destructuring */
10
- stdout = err.stdout;
11
- status = err.status;
12
- /* eslint-enable prefer-destructuring */
13
- }
14
-
15
- return {
16
- stdout: stdout.toString(),
17
- status,
18
- };
19
- }
20
-
21
- export default execSync;
@@ -1,58 +0,0 @@
1
- import os from 'os';
2
- import http from 'http';
3
- import path from 'path';
4
- import { fs, logger } from '@modern-js/utils';
5
- import execSync from './execSync';
6
-
7
- const defaultCertDir = path.resolve(os.homedir(), './.whistle-proxy');
8
- export const defaultRootCA = path.resolve(defaultCertDir, './rootCA.crt');
9
-
10
- export const trustRootCA = () => {
11
- logger.info(`please type the password to trust the https certificate`);
12
- const { status } = execSync(
13
- `sudo security add-trusted-cert -d -k /Library/Keychains/System.keychain ${defaultRootCA}`,
14
- );
15
- if (status === 0) {
16
- logger.info('Root CA install, you are ready to intercept the https now');
17
- } else {
18
- logger.info('Failed to trust the root CA, please trust it manually');
19
- }
20
- };
21
-
22
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- const isRootCATrusted = () => {
24
- // current empty
25
- };
26
-
27
- export const isRootCAExists = () => {
28
- if (fs.existsSync(defaultRootCA)) {
29
- return true;
30
- }
31
- return false;
32
- };
33
-
34
- export const generateRootCA = () =>
35
- new Promise((resolve, reject) => {
36
- if (fs.existsSync(defaultRootCA)) {
37
- fs.removeSync(defaultRootCA);
38
- }
39
-
40
- fs.ensureDirSync(defaultCertDir);
41
- const stream = fs.createWriteStream(defaultRootCA);
42
-
43
- http
44
- .get('http://localhost:8899/cgi-bin/rootca', response => {
45
- response.pipe(stream);
46
- stream
47
- .on('finish', () => {
48
- resolve(defaultRootCA);
49
- })
50
- .on('error', err => {
51
- reject(err);
52
- });
53
- })
54
- .on('error', err => {
55
- fs.unlink(defaultRootCA); // Delete the file
56
- reject(err);
57
- });
58
- });
@@ -1,31 +0,0 @@
1
- import execSync from './execSync';
2
-
3
- const networkTypes = ['Ethernet', 'Thunderbolt Ethernet', 'Wi-Fi'];
4
-
5
- const getNetworkType = () => {
6
- // eslint-disable-next-line @typescript-eslint/prefer-for-of
7
- for (let i = 0; i < networkTypes.length; i++) {
8
- const type = networkTypes[i];
9
- const result = execSync(`networksetup -getwebproxy ${type}`);
10
-
11
- if (result.status === 0) {
12
- return type;
13
- }
14
- }
15
-
16
- throw new Error('Unknown network type');
17
- };
18
-
19
- export const enableGlobalProxy = (ip: string, port: string) => {
20
- const networkType = getNetworkType();
21
-
22
- // && networksetup -setproxybypassdomains ${networkType} localhost localhost
23
- execSync(`networksetup -setwebproxy ${networkType} ${ip} ${port}`);
24
- execSync(`networksetup -setsecurewebproxy ${networkType} ${ip} ${port}`);
25
- };
26
-
27
- export const disableGlobalProxy = () => {
28
- const networkType = getNetworkType();
29
- execSync(`networksetup -setwebproxystate ${networkType} off`);
30
- execSync(`networksetup -setsecurewebproxystate ${networkType} off`);
31
- };
@@ -1,67 +0,0 @@
1
- import path from 'path';
2
- import { logger } from '@modern-js/utils';
3
- import execSync from './execSync';
4
- import {
5
- isRootCAExists,
6
- generateRootCA,
7
- defaultRootCA,
8
- trustRootCA,
9
- } from './macCAManager';
10
-
11
- const { disableGlobalProxy, enableGlobalProxy } = require('./macProxyManager');
12
-
13
- interface ProxyConfig {
14
- rule: string;
15
- port: number;
16
- // mode: 'pureProxy'|'debug'|'multiEnv'
17
- }
18
-
19
- export default class WhistleProxy {
20
- private readonly rule: string;
21
-
22
- private readonly port: number;
23
-
24
- private readonly bin: string;
25
-
26
- private readonly certDir: string;
27
-
28
- constructor(config: ProxyConfig) {
29
- this.rule = config.rule;
30
- this.port = config.port;
31
- // unused
32
- // this.mode = config.mode; // pureProxy|debug|multiEnv
33
- this.bin = path.resolve(
34
- path.dirname(require.resolve('whistle')),
35
- 'bin/whistle.js',
36
- );
37
- this.certDir = path.dirname(defaultRootCA);
38
- }
39
-
40
- async installRootCA() {
41
- try {
42
- if (!isRootCAExists()) {
43
- await generateRootCA();
44
- trustRootCA();
45
- }
46
- } catch (err) {
47
- this.close();
48
- throw err;
49
- }
50
- }
51
-
52
- async start() {
53
- logger.info(`Starting the proxy server.....`);
54
- execSync(`${this.bin} start --certDir=${this.certDir} --port=${this.port}`);
55
- execSync(`${this.bin} use ${this.rule} --force`);
56
- await this.installRootCA();
57
-
58
- enableGlobalProxy('localhost', this.port);
59
- logger.info(`Proxy Server start on localhost:${this.port}\n`);
60
- }
61
-
62
- close() {
63
- execSync(`${this.bin} stop`);
64
- disableGlobalProxy();
65
- logger.info(`Proxy Server has been closed`);
66
- }
67
- }