@lzwme/sserver 0.0.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/.eslintrc.js ADDED
@@ -0,0 +1,70 @@
1
+ // @ts-check
2
+ const path = require('path');
3
+ const rootDir = path.resolve(__dirname);
4
+
5
+ /** @type {import('eslint').Linter.Config} */
6
+ module.exports = {
7
+ extends: [
8
+ 'eslint:recommended',
9
+ // https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/docs/getting-started/linting/README.md
10
+ 'plugin:@typescript-eslint/recommended',
11
+ // 'plugin:@typescript-eslint/recommended-requiring-type-checking',
12
+ 'plugin:prettier/recommended',
13
+ 'plugin:jest/recommended',
14
+ 'plugin:unicorn/recommended',
15
+ ],
16
+ env: {
17
+ browser: true,
18
+ es2021: true,
19
+ node: true,
20
+ },
21
+ globals: {
22
+ SharedArrayBuffer: 'readonly',
23
+ },
24
+ parser: '@typescript-eslint/parser',
25
+ parserOptions: {
26
+ tsconfigRootDir: rootDir,
27
+ project: path.resolve(rootDir, './tsconfig.eslint.json'),
28
+ projectFolderIgnoreList: ['**/node_modules/**', '**/dist/**', '**/dist-admin/**'],
29
+ warnOnUnsupportedTypeScriptVersion: false,
30
+ // createDefaultProgram: true,
31
+ ecmaFeatures: {
32
+ jsx: true,
33
+ },
34
+ ecmaVersion: 2020,
35
+ sourceType: 'module',
36
+ },
37
+ plugins: ['@typescript-eslint'],
38
+ ignorePatterns: [
39
+ '**/node_modules/**',
40
+ 'dist/**',
41
+ 'build/**',
42
+ 'mock/**',
43
+ '**/*.js',
44
+ '**/*.d.ts',
45
+ ],
46
+ rules: {
47
+ 'prettier/prettier': 'warn',
48
+ // 关闭 eslint 的 indent,使用 prettier 格式化格式
49
+ indent: ['off', 2],
50
+ '@typescript-eslint/explicit-module-boundary-types': 'off',
51
+ '@typescript-eslint/no-unused-vars': ['warn', { varsIgnorePattern: '^_', argsIgnorePattern: '^_', ignoreRestSiblings: true }],
52
+ '@typescript-eslint/ban-ts-comment': 'off',
53
+ 'unicorn/prefer-module': 'off',
54
+ 'unicorn/prefer-node-protocol': 'off',
55
+ 'unicorn/no-nested-ternary': 'off',
56
+ 'unicorn/no-process-exit': 'off',
57
+ 'unicorn/consistent-destructuring': 'off',
58
+ 'jest/no-export': 'off',
59
+ 'unicorn/filename-case': 'off',
60
+ 'unicorn/no-null': 'off',
61
+ 'unicorn/prevent-abbreviations': 'off',
62
+ 'unicorn/import-style': ['error', {
63
+ styles: {
64
+ path: {
65
+ named: true,
66
+ }
67
+ }
68
+ }]
69
+ },
70
+ };
package/.gitattributes ADDED
@@ -0,0 +1,3 @@
1
+ * text=auto eol=lf
2
+ *.ts text eol=lf
3
+ *.bat text eol=crlf
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ flh --commitlint
package/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
+
5
+ ### 0.0.1 (2022-07-01)
6
+
7
+
8
+ ### Features
9
+
10
+ * 新增代理转发支持;使用 ts 重构 ([9d6a960](https://github.com/lzwme/static-server/commit/9d6a960c0a311f1f345f5e9c7b544161920fbae2))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 任侠
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.MD ADDED
@@ -0,0 +1,125 @@
1
+ [![@lzwme/sserver](https://nodei.co/npm/@lzwme/sserver.png)][npm-url]
2
+
3
+ # @lzwme/sserver
4
+
5
+ [![NPM version][npm-badge]][npm-url]
6
+ [![node version][node-badge]][node-url]
7
+ [![npm download][download-badge]][download-url]
8
+ [![GitHub issues][issues-badge]][issues-url]
9
+ [![GitHub forks][forks-badge]][forks-url]
10
+ [![GitHub stars][stars-badge]][stars-url]
11
+
12
+ 一个基于 nodejs 的简易静态服务器,方便随时开发调试使用。
13
+
14
+ ## 安装
15
+
16
+ 安装:
17
+
18
+ ```bash
19
+ # 全局安装
20
+ npm i -g @lzwme/sserver
21
+ # 测试
22
+ sserver -h
23
+
24
+ # 项目中安装
25
+ npm i -D @lzwme/sserver
26
+ yarn add -D @lzwme/sserver
27
+ ```
28
+
29
+ 也可以不安装,直接使用 `npx` 执行:
30
+
31
+ ```bash
32
+ npx @lzwme/sserver -d ./dist
33
+ ```
34
+
35
+ ## 使用
36
+
37
+ 示例:
38
+
39
+ ```bash
40
+ # 已当前目录下的 dist 作为静态目录
41
+ ssserver -d ./dist
42
+ ```
43
+
44
+ ### 帮助信息
45
+
46
+ ```bash
47
+ sserver -h
48
+
49
+ Usage: sserver|ss [options]
50
+
51
+ 一个基于 nodejs 的简易静态服务器,方便随时调试使用 [version@1.0.0]
52
+
53
+ Options:
54
+ -v, --version output the version number
55
+ -d, --dir [rootDir] 静态服务器的根目录路径
56
+ -p, --port 端口号 (default: 8890)
57
+ -o, --open 启动后是否打开静态服务器首页
58
+ --debug 开启调试模式。 (default: false)
59
+ -h, --help display help for command
60
+ ```
61
+
62
+ ### 配置代理转发
63
+
64
+ 支持通过配置文件 `ss.config.js` 指定代理转发信息。示例:
65
+
66
+ ```js
67
+ // ss.config.js
68
+
69
+ /** @type {import('@lzwme/sserver').SSConfig} */
70
+ module.exports = {
71
+ port: 8890,
72
+ baseDir: './dist',
73
+ // 代理转发配置
74
+ proxyConfig: [
75
+ // 代理企业微信机器人接口示例
76
+ {
77
+ api: '/proxy/cgi-bin/webhook/send',
78
+ config: {
79
+ target: 'https://qyapi.weixin.qq.com',
80
+ changeOrigin: true,
81
+ pathRewrite: {
82
+ '^/proxy': '',
83
+ },
84
+ },
85
+ },
86
+ ],
87
+ }
88
+ ```
89
+
90
+ ## 开发
91
+
92
+ 本地二次开发:
93
+
94
+ ```bash
95
+ git clone https://github.com/lzwme/static-server
96
+ pnpm install
97
+ npm link
98
+ pnpm dev
99
+ ```
100
+
101
+ 或者 fork 本项目进行代码贡献。
102
+
103
+ **欢迎贡献想法与代码。**
104
+
105
+ ## License
106
+
107
+ `@lzwme/sserver` is released under the MIT license.
108
+
109
+ 该插件由[志文工作室](https://lzw.me)开发和维护。
110
+
111
+
112
+ [stars-badge]: https://img.shields.io/github/stars/lzwme/static-server.svg
113
+ [stars-url]: https://github.com/lzwme/static-server/stargazers
114
+ [forks-badge]: https://img.shields.io/github/forks/lzwme/static-server.svg
115
+ [forks-url]: https://github.com/lzwme/static-server/network
116
+ [issues-badge]: https://img.shields.io/github/issues/lzwme/static-server.svg
117
+ [issues-url]: https://github.com/lzwme/static-server/issues
118
+ [npm-badge]: https://img.shields.io/npm/v/@lzwme/sserver.svg?style=flat-square
119
+ [npm-url]: https://npmjs.org/package/@lzwme/sserver
120
+ [node-badge]: https://img.shields.io/badge/node.js-%3E=_10.0.0-green.svg?style=flat-square
121
+ [node-url]: https://nodejs.org/download/
122
+ [download-badge]: https://img.shields.io/npm/dm/@lzwme/sserver.svg?style=flat-square
123
+ [download-url]: https://npmjs.org/package/@lzwme/sserver
124
+ [bundlephobia-url]: https://bundlephobia.com/result?p=@lzwme/sserver@latest
125
+ [bundlephobia-badge]: https://badgen.net/bundlephobia/minzip/@lzwme/sserver@latest
package/bin/sserver.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('./dist/cli');
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const commander_1 = require("commander");
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const static_server_1 = require("../src/static-server");
9
+ const pkg = require('../package.json');
10
+ commander_1.program
11
+ .aliases(['ss'])
12
+ .version(pkg.version, '-v, --version')
13
+ .description(chalk_1.default.yellow(pkg.description) + ` [version@${chalk_1.default.cyanBright(pkg.version)}]`)
14
+ // .option('-c, --config-path [filepath]', `配置文件 ${chalk.yellow(config.configPath)} 的路径`)
15
+ .option('-d, --base-dir [baseDir]', '静态服务器的根目录路径')
16
+ .option('-p, --port [port]', '端口号')
17
+ .option('-o, --open', '启动后是否打开静态服务器首页')
18
+ .option('--debug', `开启调试模式。`, false)
19
+ .action(opts => {
20
+ if (opts.debug)
21
+ console.log(opts);
22
+ (0, static_server_1.initServer)(opts);
23
+ });
24
+ commander_1.program.parse(process.argv);
@@ -0,0 +1,15 @@
1
+ import { type Options } from 'http-proxy-middleware';
2
+ export interface SSConfig {
3
+ /** 根目录 */
4
+ baseDir?: string;
5
+ /** 启动后是否打开首页 */
6
+ open?: boolean;
7
+ /** 监听端口号 */
8
+ port?: number;
9
+ /** 代理转发配置 */
10
+ proxyConfig?: {
11
+ api: string;
12
+ config: Options;
13
+ }[];
14
+ }
15
+ export declare function getConfig(useCache?: boolean, cfg?: SSConfig): SSConfig;
package/dist/config.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getConfig = void 0;
4
+ const path_1 = require("path");
5
+ const child_process_1 = require("child_process");
6
+ const ssConfig = {
7
+ baseDir: '.',
8
+ open: true,
9
+ port: 8890,
10
+ proxyConfig: [],
11
+ };
12
+ function getConfig(useCache = true, cfg) {
13
+ if (ssConfig.port && useCache)
14
+ return ssConfig;
15
+ const cfgFile = (0, path_1.resolve)(process.cwd(), './ss.config.js');
16
+ if ((0, child_process_1.execSync)(cfgFile))
17
+ Object.assign(ssConfig, require(cfgFile));
18
+ if (cfg)
19
+ Object.assign(ssConfig, cfg);
20
+ return ssConfig;
21
+ }
22
+ exports.getConfig = getConfig;
@@ -0,0 +1 @@
1
+ export * from './static-server';
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./static-server"), exports);
18
+ const static_server_1 = require("./static-server");
19
+ if (module === require.main)
20
+ (0, static_server_1.initServer)();
@@ -0,0 +1,2 @@
1
+ import { type Express } from 'express';
2
+ export declare function proxyByExpress(app: Express): void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.proxyByExpress = void 0;
4
+ const console_log_colors_1 = require("console-log-colors");
5
+ const http_proxy_middleware_1 = require("http-proxy-middleware");
6
+ const config_1 = require("../config");
7
+ function proxyByExpress(app) {
8
+ const { proxyConfig } = (0, config_1.getConfig)();
9
+ if (!proxyConfig)
10
+ return;
11
+ proxyConfig.forEach(({ api, config }) => {
12
+ const onProxyReq = config.onProxyReq;
13
+ config.onProxyReq = (pReq, req, res, options) => {
14
+ const now = new Date();
15
+ console.log(now.toTimeString().slice(0, 8) + '.' + String(now.getMilliseconds()).padStart(3, '0'), console_log_colors_1.color.gray('[proxyReq]'), console_log_colors_1.color.cyan(req.originalUrl), '=>', console_log_colors_1.color.cyanBright(`${pReq.protocol}${pReq.host}${pReq.path}`));
16
+ if (onProxyReq)
17
+ onProxyReq(pReq, req, res, options);
18
+ };
19
+ app.use(api, (0, http_proxy_middleware_1.createProxyMiddleware)(config));
20
+ console.log(`Init proxy for ${console_log_colors_1.color.cyanBright(api)}`);
21
+ });
22
+ }
23
+ exports.proxyByExpress = proxyByExpress;
@@ -0,0 +1,7 @@
1
+ /// <reference types="node" />
2
+ import net from 'net';
3
+ export declare function socketProxy(config?: {
4
+ localPort: number;
5
+ remotePort: number;
6
+ remoteHost: string;
7
+ }): net.Server;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.socketProxy = void 0;
7
+ const net_1 = __importDefault(require("net"));
8
+ const console_log_colors_1 = require("console-log-colors");
9
+ function socketProxy(config = {
10
+ // 外网访问的端口
11
+ localPort: 3717,
12
+ // 代理转发的端口
13
+ remotePort: 27017,
14
+ //代理转发的 IP 地址
15
+ remoteHost: '127.0.0.1',
16
+ }) {
17
+ const server = net_1.default
18
+ .createServer(socket => {
19
+ console_log_colors_1.log.green('proxy for address', socket.address());
20
+ let to = net_1.default.createConnection({
21
+ host: config.remoteHost,
22
+ port: config.remotePort,
23
+ });
24
+ socket.pipe(to);
25
+ to.pipe(socket);
26
+ })
27
+ .listen(config.localPort);
28
+ return server;
29
+ }
30
+ exports.socketProxy = socketProxy;
@@ -0,0 +1,3 @@
1
+ import { type Express } from 'express';
2
+ import { SSConfig } from './config';
3
+ export declare function initServer(options?: SSConfig): Express;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ /* eslint-disable no-console */
3
+ /*
4
+ * @Author: lzw
5
+ * @Date: 2021-05-28 16:24:40
6
+ * @LastEditors: lzw
7
+ * @LastEditTime: 2022-07-01 17:43:51
8
+ * @Description:
9
+ */
10
+ var __importDefault = (this && this.__importDefault) || function (mod) {
11
+ return (mod && mod.__esModule) ? mod : { "default": mod };
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.initServer = void 0;
15
+ const console_log_colors_1 = require("console-log-colors");
16
+ const express_1 = __importDefault(require("express"));
17
+ const child_process_1 = require("child_process");
18
+ const config_1 = require("./config");
19
+ const express_proxy_1 = require("./proxy/express-proxy");
20
+ function initServer(options) {
21
+ options = (0, config_1.getConfig)(true, options);
22
+ const app = (0, express_1.default)();
23
+ const { baseDir = '.', port = 8890 } = options;
24
+ // @see https://www.expressjs.com.cn/4x/api.html#express.static
25
+ app.use(express_1.default.static(baseDir));
26
+ (0, express_proxy_1.proxyByExpress)(app);
27
+ app.listen(port, () => {
28
+ console.log(console_log_colors_1.color.green(`Static server Listening Port:`), port);
29
+ console.log(console_log_colors_1.color.green(`Static DIR:`), console_log_colors_1.color.cyanBright(baseDir));
30
+ });
31
+ if (options.open !== false) {
32
+ const openPageCmd = `${process.platform === 'win32' ? `start` : `open`} http://localhost:${port}`;
33
+ (0, child_process_1.execSync)(openPageCmd);
34
+ }
35
+ return app;
36
+ }
37
+ exports.initServer = initServer;
38
+ ;
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@lzwme/sserver",
3
+ "version": "0.0.1",
4
+ "description": "一个基于 nodejs 的简易静态服务器,方便随时开发调试使用",
5
+ "main": "dist/index.js",
6
+ "type": "commonjs",
7
+ "bin": {
8
+ "sserver": "bin/sserver.js",
9
+ "ss": "bin/sserver.js"
10
+ },
11
+ "repository": "http://github.com/lzwme/static-server.git",
12
+ "author": {
13
+ "name": "renxia",
14
+ "email": "lzwy0820@qq.com",
15
+ "url": "https://lzw.me"
16
+ },
17
+ "license": "MIT",
18
+ "private": false,
19
+ "engines": {
20
+ "node": ">=14"
21
+ },
22
+ "packageManager": "pnpm@7.2.0",
23
+ "keywords": [
24
+ "server",
25
+ "web-server",
26
+ "static-server",
27
+ "proxy-server"
28
+ ],
29
+ "publishConfig": {
30
+ "access": "public",
31
+ "registry": "https://registry.npmjs.com"
32
+ },
33
+ "scripts": {
34
+ "prepare": "husky install",
35
+ "ss": "node bin/sserver.js",
36
+ "start": "pnpm run stop && pm2 start dist/index.js --name sserver",
37
+ "stop": "pm2 stop sserver",
38
+ "dev": "ts-node -T src/index.ts",
39
+ "build": "npm run clean && tsc",
40
+ "clean": "flh rm -f dist",
41
+ "version": "standard-version"
42
+ },
43
+ "dependencies": {
44
+ "commander": "^9.3.0",
45
+ "console-log-colors": "^0.2.3",
46
+ "express": "^4.18.1",
47
+ "http-proxy-middleware": "^2.0.6"
48
+ },
49
+ "devDependencies": {
50
+ "@lzwme/fed-lint-helper": "^1.9.1",
51
+ "@types/express": "^4.17.13",
52
+ "@types/node": "^18.0.0",
53
+ "husky": "^8.0.1",
54
+ "standard-version": "^9.5.0",
55
+ "ts-node": "^10.8.1",
56
+ "typescript": "^4.7.4"
57
+ }
58
+ }
@@ -0,0 +1,18 @@
1
+ /** @type {import('@lzwme/sserver').SSConfig} */
2
+ module.exports = {
3
+ port: 8890,
4
+ baseDir: '.',
5
+ proxyConfig: [
6
+ // 代理企业微信机器人接口示例
7
+ {
8
+ api: '/proxy/cgi-bin/webhook/send',
9
+ config: {
10
+ target: 'https://qyapi.weixin.qq.com',
11
+ changeOrigin: true,
12
+ pathRewrite: {
13
+ '^/proxy': '',
14
+ },
15
+ },
16
+ },
17
+ ],
18
+ }