@oipage/cli 0.3.0-alpha.4 → 0.3.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.
Files changed (74) hide show
  1. package/.github/FUNDING.yml +11 -11
  2. package/AUTHORS.txt +6 -6
  3. package/CHANGELOG +47 -47
  4. package/LICENSE +20 -20
  5. package/README.md +132 -132
  6. package/bin/WebSocket/decodeWsFrame.js +43 -43
  7. package/bin/WebSocket/encodeWsFrame.js +28 -28
  8. package/bin/WebSocket/headersToJSON.js +26 -26
  9. package/bin/WebSocket/index.js +106 -106
  10. package/bin/data/mineTypes.json +104 -104
  11. package/bin/disk.js +42 -42
  12. package/bin/help.js +55 -55
  13. package/bin/intercept.js +15 -15
  14. package/bin/network.js +21 -21
  15. package/bin/notebook/index.js +8 -8
  16. package/bin/proxy.js +82 -82
  17. package/bin/run +146 -146
  18. package/bin/serve.js +318 -318
  19. package/bin/template/404.html +184 -184
  20. package/bin/tools/deleteEmptyFolder.js +33 -33
  21. package/bin/tools/format.js +75 -75
  22. package/bin/tools/isBinary.js +83 -83
  23. package/bin/tools/network.js +42 -42
  24. package/bin/tools/openBrowser.js +34 -34
  25. package/bin/tools/resolve404.js +98 -98
  26. package/bin/tools/resolveImport.js +96 -96
  27. package/bin/website-htmls/components/ui-select-file/index.html +8 -8
  28. package/bin/website-htmls/components/ui-select-file/index.js +42 -42
  29. package/bin/website-htmls/components/ui-select-file/index.scss +44 -44
  30. package/bin/website-htmls/dialogs/imageSize/index.html +54 -54
  31. package/bin/website-htmls/dialogs/imageSize/index.js +53 -53
  32. package/bin/website-htmls/dialogs/imageSize/index.scss +140 -140
  33. package/bin/website-htmls/dialogs/index.js +52 -52
  34. package/bin/website-htmls/images/addFile.svg +1 -1
  35. package/bin/website-htmls/images/img-to-pdf.svg +1 -1
  36. package/bin/website-htmls/index.html +17 -17
  37. package/bin/website-htmls/main.js +13 -13
  38. package/bin/website-htmls/pages/App/index.html +6 -6
  39. package/bin/website-htmls/pages/App/index.js +22 -22
  40. package/bin/website-htmls/pages/App/index.scss +43 -43
  41. package/bin/website-htmls/pages/appStore/index.html +7 -7
  42. package/bin/website-htmls/pages/appStore/index.js +19 -19
  43. package/bin/website-htmls/pages/appStore/index.scss +54 -54
  44. package/bin/website-htmls/pages/chart/index.html +8 -8
  45. package/bin/website-htmls/pages/chart/index.js +72 -72
  46. package/bin/website-htmls/pages/chart/index.scss +71 -71
  47. package/bin/website-htmls/pages/home/index.html +19 -19
  48. package/bin/website-htmls/pages/home/index.js +19 -19
  49. package/bin/website-htmls/pages/home/index.scss +87 -87
  50. package/bin/website-htmls/pages/image-editor/index.html +27 -27
  51. package/bin/website-htmls/pages/image-editor/index.js +106 -106
  52. package/bin/website-htmls/pages/image-editor/index.scss +69 -69
  53. package/bin/website-htmls/pages/img-to-pdf/index.html +3 -3
  54. package/bin/website-htmls/pages/img-to-pdf/index.js +44 -44
  55. package/bin/website-htmls/pages/img-to-pdf/index.scss +9 -9
  56. package/bin/website-htmls/pages/recorder-screen/index.html +15 -15
  57. package/bin/website-htmls/pages/recorder-screen/index.js +227 -227
  58. package/bin/website-htmls/pages/recorder-screen/index.scss +52 -52
  59. package/bin/website-htmls/router.config.js +29 -29
  60. package/bin/website-htmls/styles/common.css +44 -44
  61. package/bin/website-htmls/styles/normalize.css +94 -94
  62. package/bin/website-plugins/intercept/chart.js +33 -33
  63. package/bin/website-plugins/intercept/getPackage.js +17 -17
  64. package/bin/website-plugins/intercept/head.js +8 -8
  65. package/bin/website-plugins/intercept/index.js +8 -8
  66. package/bin/website-plugins/intercept/oipage-vislite-intercept.js +55 -55
  67. package/bin/website-plugins/intercept/oipage-zipaper-intercept.js +36 -36
  68. package/bin/website-plugins/loader/index.js +11 -11
  69. package/bin/website-plugins/loader/oipage-html-loader.js +7 -7
  70. package/bin/website-plugins/loader/oipage-scss-loader.js +149 -149
  71. package/docs/config.md +106 -106
  72. package/package.json +42 -42
  73. package/src/index.js +29 -29
  74. package/types/serve.d.ts +83 -83
@@ -1,12 +1,12 @@
1
- # These are supported funding model platforms
2
-
3
- github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4
- patreon: # Replace with a single Patreon username
5
- open_collective: # Replace with a single Open Collective username
6
- ko_fi: # Replace with a single Ko-fi username
7
- tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
- community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
- liberapay: # Replace with a single Liberapay username
10
- issuehunt: # Replace with a single IssueHunt username
11
- otechie: # Replace with a single Otechie username
1
+ # These are supported funding model platforms
2
+
3
+ github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4
+ patreon: # Replace with a single Patreon username
5
+ open_collective: # Replace with a single Open Collective username
6
+ ko_fi: # Replace with a single Ko-fi username
7
+ tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
+ community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
+ liberapay: # Replace with a single Liberapay username
10
+ issuehunt: # Replace with a single IssueHunt username
11
+ otechie: # Replace with a single Otechie username
12
12
  custom: ['https://zxl20070701.github.io/notebook/home.html']
package/AUTHORS.txt CHANGED
@@ -1,6 +1,6 @@
1
- Authors ordered by first contribution.
2
-
3
- zxl20070701 <1904314465@qq.com> <https://github.com/zxl20070701>
4
-
5
- -----------------------------------------------------
6
- We thank all of them for their contributions!
1
+ Authors ordered by first contribution.
2
+
3
+ zxl20070701 <1904314465@qq.com> <https://github.com/zxl20070701>
4
+
5
+ -----------------------------------------------------
6
+ We thank all of them for their contributions!
package/CHANGELOG CHANGED
@@ -1,47 +1,47 @@
1
- v0.1.0:
2
- date:2026-03-30
3
- changes:
4
- - 初始化版本
5
- (承接原OIPagev1.x的命令行相关功能: https://github.com/oi-contrib/OIPage/tree/v1.x)
6
- v0.2.0:
7
- date:2026-04-05
8
- changes:
9
- - 修复bug
10
- 1、修复部分服务器日志用户设置name未生效问题
11
- - 优化改造
12
- 1、暴露更多有用的插件
13
- * ZipaperIntercept
14
- * VISLiteIntercept
15
- * ScssLoader
16
- * HtmlLoader
17
- v0.2.1:
18
- date:2026-04-13
19
- changes:
20
- - 修复bug
21
- 1、修复npm安装包当前命令行非根目录时,无法正确查找命令行目录下的node_modules错误
22
- v0.3.0:
23
- date:
24
- changes:
25
- - 修复bug
26
- 1、修复服务器若干问题
27
- * 路径穿越漏洞(Path Traversal)
28
- 比如: printf 'GET /../../../../../Users/admin/.codex/auth.json HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nConnection: close\r\n\r\n' | nc 127.0.0.1 8080
29
- * 404 页面 XSS 漏洞
30
- * 畸形URL导致客户端持续挂起等待问题
31
- * 加固阻止 Nmap 等扫描器的服务探测
32
- * 文件扩展名提取可能抛出 TypeError 问题
33
- * 路径匹配添加更多兼容,防止意外错误
34
- * 修复对文件夹判断未考虑符号链接指向的目录问题导致import语句解析出错问题
35
- * 兼容 "@oipage/browser.js" 类似的包
36
- 2、修复错误读取导致服务器奔溃问题
37
- 3、对于二进制文件,采用流式传输
38
- - 优化改造
39
- 1、优化命令行相关文档优化,提高可读性
40
- 2、优化服务器若干问题
41
- * proxy中decodeURIComponent捕获错误,方便调试
42
- * 打印访问IP,方便后续追踪攻击
43
- - 新增功能
44
- 1、oipage-cli notebook
45
- 快速笔记
46
- 2、devServer
47
- * 支持gzip
1
+ v0.1.0:
2
+ date:2026-03-30
3
+ changes:
4
+ - 初始化版本
5
+ (承接原OIPagev1.x的命令行相关功能: https://github.com/oi-contrib/OIPage/tree/v1.x)
6
+ v0.2.0:
7
+ date:2026-04-05
8
+ changes:
9
+ - 修复bug
10
+ 1、修复部分服务器日志用户设置name未生效问题
11
+ - 优化改造
12
+ 1、暴露更多有用的插件
13
+ * ZipaperIntercept
14
+ * VISLiteIntercept
15
+ * ScssLoader
16
+ * HtmlLoader
17
+ v0.2.1:
18
+ date:2026-04-13
19
+ changes:
20
+ - 修复bug
21
+ 1、修复npm安装包当前命令行非根目录时,无法正确查找命令行目录下的node_modules错误
22
+ v0.3.0:
23
+ date:2026-10-25
24
+ changes:
25
+ - 修复bug
26
+ 1、修复服务器若干问题
27
+ * 路径穿越漏洞(Path Traversal)
28
+ 比如: printf 'GET /../../../../../Users/admin/.codex/auth.json HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nConnection: close\r\n\r\n' | nc 127.0.0.1 8080
29
+ * 404 页面 XSS 漏洞
30
+ * 畸形URL导致客户端持续挂起等待问题
31
+ * 加固阻止 Nmap 等扫描器的服务探测
32
+ * 文件扩展名提取可能抛出 TypeError 问题
33
+ * 路径匹配添加更多兼容,防止意外错误
34
+ * 修复对文件夹判断未考虑符号链接指向的目录问题导致import语句解析出错问题
35
+ * 兼容 "@oipage/browser.js" 类似的包
36
+ 2、修复错误读取导致服务器奔溃问题
37
+ 3、对于二进制文件,采用流式传输
38
+ - 优化改造
39
+ 1、优化命令行相关文档优化,提高可读性
40
+ 2、优化服务器若干问题
41
+ * proxy中decodeURIComponent捕获错误,方便调试
42
+ * 打印访问IP,方便后续追踪攻击
43
+ - 新增功能
44
+ 1、oipage-cli notebook
45
+ 快速笔记
46
+ 2、devServer
47
+ * 支持gzip
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) zxl20070701 走一步,再走一步
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
1
+ MIT License
2
+
3
+ Copyright (c) zxl20070701 走一步,再走一步
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
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,132 +1,132 @@
1
- # [@oipage/cli](https://github.com/oi-contrib/oipage-cli)
2
- 开发快速助手,一个包含常用功能的命令行工具,旨在提升开发效率
3
-
4
- <p>
5
- <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@oipage/cli&interval=7">
6
- <img src="https://img.shields.io/npm/dm/@oipage/cli.svg" alt="downloads">
7
- </a>
8
- <a href="https://www.npmjs.com/package/@oipage/cli">
9
- <img src="https://img.shields.io/npm/v/@oipage/cli.svg" alt="npm">
10
- </a>
11
- <a href="https://github.com/oi-contrib/oipage-cli/issues">
12
- <img src="https://img.shields.io/github/issues/oi-contrib/oipage-cli" alt="issue">
13
- </a>
14
- <a href="https://github.com/oi-contrib/oipage-cli" target='_blank'>
15
- <img alt="GitHub repo stars" src="https://img.shields.io/github/stars/oi-contrib/oipage-cli?style=social">
16
- </a>
17
- <a href="https://github.com/oi-contrib/oipage-cli">
18
- <img src="https://img.shields.io/github/forks/oi-contrib/oipage-cli" alt="forks">
19
- </a>
20
- <a href="https://gitee.com/oi-contrib/oipage-cli" target='_blank'>
21
- <img alt="Gitee repo stars" src="https://gitee.com/oi-contrib/oipage-cli/badge/star.svg">
22
- </a>
23
- <a href="https://gitee.com/oi-contrib/oipage-cli">
24
- <img src="https://gitee.com/oi-contrib/oipage-cli/badge/fork.svg" alt="forks">
25
- </a>
26
- </p>
27
-
28
- <img src="https://nodei.co/npm/@oipage/cli.png?downloads=true&amp;downloadRank=true&amp;stars=true" alt="NPM">
29
-
30
- ## 如何使用?
31
-
32
- 作为一个命令行,你可以全局安装:
33
-
34
- ```shell
35
- npm install -g @oipage/cli
36
- ```
37
-
38
- 也可以作为项目开发中的一个功能加强,在 package.json 中配置命令使用,那么就在项目中执行安装命令:
39
-
40
- ```shell
41
- npm install @oipage/cli --save
42
- ```
43
-
44
- 安装后,就可以使用了,先打印帮助信息查看一下:
45
-
46
- ```shell
47
- oipage-cli
48
- ```
49
-
50
- 比如会出现类似下列内容:
51
-
52
- ```
53
- @oipage/cli@v0.1.0
54
-
55
- 可以使用的命令如下:
56
-
57
- 【1】oipage-cli serve 开发服务器
58
- --port|-p 端口号
59
- --baseUrl 服务器根目录
60
- ......
61
- ```
62
-
63
- 根据提示我们知道,比如第一个命令,其提供了一个快速访问本地资源服务器的功能,执行:
64
-
65
- ```shell
66
- oipage-cli serve -p 8080
67
- ```
68
-
69
- 启动成功后,直接访问: http://localhost:8080/ 即可。
70
-
71
- ### oipage.config.js
72
-
73
- 通过配置文件 `oipage.config.js` 可以对服务器进行更精细化的配置:
74
-
75
- ```shell
76
- oipage-cli serve --config ./oipage.config.js
77
- ```
78
-
79
- 其中 `oipage.config.js` 格式如下:
80
-
81
- ```js
82
- module.exports = {
83
- devServer: {
84
- port: 20000,
85
- baseUrl: "./"
86
- },
87
- module: {
88
- rules: [{ // 配置对文件的自定义处理
89
- test: /\.html$/, // 匹配文件,如果多个匹配到,只会选用第一个
90
- use(source) { return source }
91
- }]
92
- }
93
- }
94
- ```
95
-
96
- 上述一级配置项(devServer、module等)皆可选,按需配置即可,具体可以查看[ 可配置项 ](./docs/config.md)查看详情。
97
-
98
- ### 作为API使用
99
-
100
- 当然,也可以使用API的方式,比如:
101
-
102
- ```js
103
- const { serve } = require("@oipage/cli");
104
-
105
- serve({
106
- devServer: {
107
- port: 20000,
108
- baseUrl: "./"
109
- }
110
- });
111
- ```
112
-
113
- ### 可用插件
114
-
115
- > 0.2.0 新增
116
-
117
- ```js
118
- const {
119
- ZipaperIntercept, // Zipaper框架引入
120
- VISLiteIntercept, // VISLite库引入
121
- ScssLoader, // scss文件解析
122
- HtmlLoader, // html解析成json对象
123
- } = require("@oipage/cli");
124
- ```
125
-
126
- 已有功能或更多功能我们将根据实际情况逐步完善和维护,当然,你可以通过 [issue](https://github.com/oi-contrib/oipage-cli/issues) 给我们留言,告诉我们你的改进意见。
127
-
128
- ## 版权
129
-
130
- MIT License
131
-
132
- Copyright (c) [zxl20070701](https://zxl20070701.github.io/notebook/home.html) 走一步,再走一步
1
+ # [@oipage/cli](https://github.com/oi-contrib/oipage-cli)
2
+ 开发快速助手,一个包含常用功能的命令行工具,旨在提升开发效率
3
+
4
+ <p>
5
+ <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@oipage/cli&interval=7">
6
+ <img src="https://img.shields.io/npm/dm/@oipage/cli.svg" alt="downloads">
7
+ </a>
8
+ <a href="https://www.npmjs.com/package/@oipage/cli">
9
+ <img src="https://img.shields.io/npm/v/@oipage/cli.svg" alt="npm">
10
+ </a>
11
+ <a href="https://github.com/oi-contrib/oipage-cli/issues">
12
+ <img src="https://img.shields.io/github/issues/oi-contrib/oipage-cli" alt="issue">
13
+ </a>
14
+ <a href="https://github.com/oi-contrib/oipage-cli" target='_blank'>
15
+ <img alt="GitHub repo stars" src="https://img.shields.io/github/stars/oi-contrib/oipage-cli?style=social">
16
+ </a>
17
+ <a href="https://github.com/oi-contrib/oipage-cli">
18
+ <img src="https://img.shields.io/github/forks/oi-contrib/oipage-cli" alt="forks">
19
+ </a>
20
+ <a href="https://gitee.com/oi-contrib/oipage-cli" target='_blank'>
21
+ <img alt="Gitee repo stars" src="https://gitee.com/oi-contrib/oipage-cli/badge/star.svg">
22
+ </a>
23
+ <a href="https://gitee.com/oi-contrib/oipage-cli">
24
+ <img src="https://gitee.com/oi-contrib/oipage-cli/badge/fork.svg" alt="forks">
25
+ </a>
26
+ </p>
27
+
28
+ <img src="https://nodei.co/npm/@oipage/cli.png?downloads=true&amp;downloadRank=true&amp;stars=true" alt="NPM">
29
+
30
+ ## 如何使用?
31
+
32
+ 作为一个命令行,你可以全局安装:
33
+
34
+ ```shell
35
+ npm install -g @oipage/cli
36
+ ```
37
+
38
+ 也可以作为项目开发中的一个功能加强,在 package.json 中配置命令使用,那么就在项目中执行安装命令:
39
+
40
+ ```shell
41
+ npm install @oipage/cli --save
42
+ ```
43
+
44
+ 安装后,就可以使用了,先打印帮助信息查看一下:
45
+
46
+ ```shell
47
+ oipage-cli
48
+ ```
49
+
50
+ 比如会出现类似下列内容:
51
+
52
+ ```
53
+ @oipage/cli@v0.1.0
54
+
55
+ 可以使用的命令如下:
56
+
57
+ 【1】oipage-cli serve 开发服务器
58
+ --port|-p 端口号
59
+ --baseUrl 服务器根目录
60
+ ......
61
+ ```
62
+
63
+ 根据提示我们知道,比如第一个命令,其提供了一个快速访问本地资源服务器的功能,执行:
64
+
65
+ ```shell
66
+ oipage-cli serve -p 8080
67
+ ```
68
+
69
+ 启动成功后,直接访问: http://localhost:8080/ 即可。
70
+
71
+ ### oipage.config.js
72
+
73
+ 通过配置文件 `oipage.config.js` 可以对服务器进行更精细化的配置:
74
+
75
+ ```shell
76
+ oipage-cli serve --config ./oipage.config.js
77
+ ```
78
+
79
+ 其中 `oipage.config.js` 格式如下:
80
+
81
+ ```js
82
+ module.exports = {
83
+ devServer: {
84
+ port: 20000,
85
+ baseUrl: "./"
86
+ },
87
+ module: {
88
+ rules: [{ // 配置对文件的自定义处理
89
+ test: /\.html$/, // 匹配文件,如果多个匹配到,只会选用第一个
90
+ use(source) { return source }
91
+ }]
92
+ }
93
+ }
94
+ ```
95
+
96
+ 上述一级配置项(devServer、module等)皆可选,按需配置即可,具体可以查看[ 可配置项 ](./docs/config.md)查看详情。
97
+
98
+ ### 作为API使用
99
+
100
+ 当然,也可以使用API的方式,比如:
101
+
102
+ ```js
103
+ const { serve } = require("@oipage/cli");
104
+
105
+ serve({
106
+ devServer: {
107
+ port: 20000,
108
+ baseUrl: "./"
109
+ }
110
+ });
111
+ ```
112
+
113
+ ### 可用插件
114
+
115
+ > 0.2.0 新增
116
+
117
+ ```js
118
+ const {
119
+ ZipaperIntercept, // Zipaper框架引入
120
+ VISLiteIntercept, // VISLite库引入
121
+ ScssLoader, // scss文件解析
122
+ HtmlLoader, // html解析成json对象
123
+ } = require("@oipage/cli");
124
+ ```
125
+
126
+ 已有功能或更多功能我们将根据实际情况逐步完善和维护,当然,你可以通过 [issue](https://github.com/oi-contrib/oipage-cli/issues) 给我们留言,告诉我们你的改进意见。
127
+
128
+ ## 版权
129
+
130
+ MIT License
131
+
132
+ Copyright (c) [zxl20070701](https://zxl20070701.github.io/notebook/home.html) 走一步,再走一步
@@ -1,43 +1,43 @@
1
-
2
- // 处理收到的数据
3
- module.exports = function decodeWsFrame(data) {
4
- let start = 0;
5
- let frame = {
6
- isFinal: (data[start] & 0x80) === 0x80,
7
- opcode: data[start++] & 0xF,
8
- masked: (data[start] & 0x80) === 0x80,
9
- payloadLen: data[start++] & 0x7F,
10
- maskingKey: '',
11
- payloadData: null
12
- };
13
-
14
- if (frame.payloadLen === 126) {
15
- frame.payloadLen = (data[start++] << 8) + data[start++];
16
- } else if (frame.payloadLen === 127) {
17
- frame.payloadLen = 0;
18
- for (let i = 7; i >= 0; --i) {
19
- frame.payloadLen += (data[start++] << (i * 8));
20
- }
21
- }
22
-
23
- if (frame.payloadLen) {
24
- if (frame.masked) {
25
- const maskingKey = [
26
- data[start++],
27
- data[start++],
28
- data[start++],
29
- data[start++]
30
- ];
31
-
32
- frame.maskingKey = maskingKey;
33
-
34
- frame.payloadData = data
35
- .slice(start, start + frame.payloadLen)
36
- .map((byte, idx) => byte ^ maskingKey[idx % 4]);
37
- } else {
38
- frame.payloadData = data.slice(start, start + frame.payloadLen);
39
- }
40
- }
41
-
42
- return frame;
43
- };
1
+
2
+ // 处理收到的数据
3
+ module.exports = function decodeWsFrame(data) {
4
+ let start = 0;
5
+ let frame = {
6
+ isFinal: (data[start] & 0x80) === 0x80,
7
+ opcode: data[start++] & 0xF,
8
+ masked: (data[start] & 0x80) === 0x80,
9
+ payloadLen: data[start++] & 0x7F,
10
+ maskingKey: '',
11
+ payloadData: null
12
+ };
13
+
14
+ if (frame.payloadLen === 126) {
15
+ frame.payloadLen = (data[start++] << 8) + data[start++];
16
+ } else if (frame.payloadLen === 127) {
17
+ frame.payloadLen = 0;
18
+ for (let i = 7; i >= 0; --i) {
19
+ frame.payloadLen += (data[start++] << (i * 8));
20
+ }
21
+ }
22
+
23
+ if (frame.payloadLen) {
24
+ if (frame.masked) {
25
+ const maskingKey = [
26
+ data[start++],
27
+ data[start++],
28
+ data[start++],
29
+ data[start++]
30
+ ];
31
+
32
+ frame.maskingKey = maskingKey;
33
+
34
+ frame.payloadData = data
35
+ .slice(start, start + frame.payloadLen)
36
+ .map((byte, idx) => byte ^ maskingKey[idx % 4]);
37
+ } else {
38
+ frame.payloadData = data.slice(start, start + frame.payloadLen);
39
+ }
40
+ }
41
+
42
+ return frame;
43
+ };
@@ -1,28 +1,28 @@
1
-
2
- // 处理发出的数据
3
- module.exports = function encodeWsFrame(data) {
4
- const isFinal = data.isFinal !== undefined ? data.isFinal : true,
5
- opcode = data.opcode !== undefined ? data.opcode : 1,
6
- payloadData = data.payloadData ? Buffer.from(data.payloadData) : null,
7
- payloadLen = payloadData ? payloadData.length : 0;
8
-
9
- let frame = [];
10
-
11
- if (isFinal) frame.push((1 << 7) + opcode);
12
- else frame.push(opcode);
13
-
14
- if (payloadLen < 126) {
15
- frame.push(payloadLen);
16
- } else if (payloadLen < 65536) {
17
- frame.push(126, payloadLen >> 8, payloadLen & 0xFF);
18
- } else {
19
- frame.push(127);
20
- for (let i = 7; i >= 0; --i) {
21
- frame.push((payloadLen & (0xFF << (i * 8))) >> (i * 8));
22
- }
23
- }
24
-
25
- frame = payloadData ? Buffer.concat([Buffer.from(frame), payloadData]) : Buffer.from(frame);
26
-
27
- return frame;
28
- };
1
+
2
+ // 处理发出的数据
3
+ module.exports = function encodeWsFrame(data) {
4
+ const isFinal = data.isFinal !== undefined ? data.isFinal : true,
5
+ opcode = data.opcode !== undefined ? data.opcode : 1,
6
+ payloadData = data.payloadData ? Buffer.from(data.payloadData) : null,
7
+ payloadLen = payloadData ? payloadData.length : 0;
8
+
9
+ let frame = [];
10
+
11
+ if (isFinal) frame.push((1 << 7) + opcode);
12
+ else frame.push(opcode);
13
+
14
+ if (payloadLen < 126) {
15
+ frame.push(payloadLen);
16
+ } else if (payloadLen < 65536) {
17
+ frame.push(126, payloadLen >> 8, payloadLen & 0xFF);
18
+ } else {
19
+ frame.push(127);
20
+ for (let i = 7; i >= 0; --i) {
21
+ frame.push((payloadLen & (0xFF << (i * 8))) >> (i * 8));
22
+ }
23
+ }
24
+
25
+ frame = payloadData ? Buffer.concat([Buffer.from(frame), payloadData]) : Buffer.from(frame);
26
+
27
+ return frame;
28
+ };
@@ -1,26 +1,26 @@
1
- module.exports = function (headersStr) {
2
-
3
- let headersArray = headersStr.split(/\r{0,1}\n/);
4
- let headersJSON = {};
5
-
6
- // 第一行的内容:GET / HTTP/1.1
7
- let firstLine = headersArray.shift().split(' ');
8
- headersJSON.method = firstLine[0];
9
- headersJSON.url = firstLine[1];
10
- headersJSON.protocol = firstLine[2];
11
-
12
- for (let item of headersArray) {
13
- let temp = item.split(':');
14
-
15
- if (temp.length > 1) {
16
-
17
- let key = temp.shift();
18
- let value = temp.join(':');
19
-
20
- headersJSON[key] = value.trim();
21
-
22
- }
23
- }
24
-
25
- return headersJSON;
26
- };
1
+ module.exports = function (headersStr) {
2
+
3
+ let headersArray = headersStr.split(/\r{0,1}\n/);
4
+ let headersJSON = {};
5
+
6
+ // 第一行的内容:GET / HTTP/1.1
7
+ let firstLine = headersArray.shift().split(' ');
8
+ headersJSON.method = firstLine[0];
9
+ headersJSON.url = firstLine[1];
10
+ headersJSON.protocol = firstLine[2];
11
+
12
+ for (let item of headersArray) {
13
+ let temp = item.split(':');
14
+
15
+ if (temp.length > 1) {
16
+
17
+ let key = temp.shift();
18
+ let value = temp.join(':');
19
+
20
+ headersJSON[key] = value.trim();
21
+
22
+ }
23
+ }
24
+
25
+ return headersJSON;
26
+ };