mqtt-crypto-client 0.1.0 → 0.2.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/README.md +43 -5
- package/dist/index.iife.js +9 -0
- package/dist/index.iife.js.map +7 -0
- package/package.json +5 -2
- package/src/browser.js +8 -0
- package/src/crypto-browser.js +295 -0
- package/src/crypto-js.d.ts +4 -0
- package/src/events-browser.js +114 -0
- package/src/process-browser.js +23 -0
- package/types/index.d.ts +5 -4
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
- 仓库:<https://github.com/weijia/mqtt-crypto-client>
|
|
9
9
|
- 包名:`mqtt-crypto-client`
|
|
10
10
|
- 运行时:Node.js **18+**(CI 发布使用 Node 24)
|
|
11
|
-
-
|
|
11
|
+
- 依赖:Node 端仅 [`mqtt`](https://www.npmjs.com/package/mqtt),加解密使用内置 `node:crypto`;浏览器 IIFE 内置 crypto-js(仅构建时 devDependency)
|
|
12
12
|
|
|
13
13
|
## 安装
|
|
14
14
|
|
|
@@ -26,7 +26,45 @@ import { createClient } from 'mqtt-crypto-client';
|
|
|
26
26
|
const { createClient } = require('mqtt-crypto-client');
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
> ESM 源码直接作为 `import` 入口发布;CJS 由 `esbuild`
|
|
29
|
+
> ESM 源码直接作为 `import` 入口发布;CJS 与浏览器 IIFE 由 `esbuild` 构建。发布前 `prepack`/`prepublishOnly` 会自动构建。
|
|
30
|
+
|
|
31
|
+
## 浏览器 `<script>` 直引(IIFE)
|
|
32
|
+
|
|
33
|
+
不经过 bundler,直接用 `<script>` 引入。构建产物暴露全局 `MqttCryptoClient`,包含 `createClient`、加解密工具、信封/topic 工具等全部公开导出。
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<script src="https://unpkg.com/mqtt-crypto-client@0.2.1/dist/index.iife.js"></script>
|
|
37
|
+
<script>
|
|
38
|
+
const { createClient, encryptPayload, decryptPayload, buildTopic } = MqttCryptoClient;
|
|
39
|
+
|
|
40
|
+
const ciphertext = encryptPayload('中文 hello 😀', 'your-secret');
|
|
41
|
+
console.log(decryptPayload(ciphertext, 'your-secret')); // 中文 hello 😀
|
|
42
|
+
|
|
43
|
+
const client = createClient({
|
|
44
|
+
broker: 'wss://broker.emqx.io:8084/mqtt', // 浏览器只能用 ws/wss
|
|
45
|
+
topic: buildTopic('secure', 'stock-condition-order'),
|
|
46
|
+
secret: 'your-secret',
|
|
47
|
+
});
|
|
48
|
+
client.on('connect', () => console.log('connected'));
|
|
49
|
+
client.on('message', ({ command, decrypted }) => console.log(decrypted, command));
|
|
50
|
+
// await client.connect();
|
|
51
|
+
</script>
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
CDN 地址模板(`unpkg` / `jsdelivr` 字段均指向 IIFE):
|
|
55
|
+
|
|
56
|
+
- `https://unpkg.com/mqtt-crypto-client@0.2.1/dist/index.iife.js`
|
|
57
|
+
- `https://cdn.jsdelivr.net/npm/mqtt-crypto-client@0.2.1/dist/index.iife.js`
|
|
58
|
+
- 省略版本号会取 latest:`https://unpkg.com/mqtt-crypto-client`、`https://cdn.jsdelivr.net/npm/mqtt-crypto-client`
|
|
59
|
+
|
|
60
|
+
说明:
|
|
61
|
+
|
|
62
|
+
- 浏览器端使用 **crypto-js** 做同步 AES,格式与 Node 端、原 CryptoJS 实现逐字节一致(`Salted__` + 8 字节 salt + AES-256-CBC + `EVP_BytesToKey(MD5,1)` + PKCS#7 + base64);Node 端仍用 `node:crypto`,行为不变。
|
|
63
|
+
- IIFE 构建期会把 `process.env.*`、`global` 静态替换掉;无法静态消除的裸 `process` 由内联最小 shim 兜底(不污染 `window.process`)。构建后脚本会扫描 `process.env`/`global.` 残留,并在无 `process`/`Buffer` 的 vm 沙箱中加载验证,失败即构建失败。
|
|
64
|
+
- 浏览器下 `encryptPayload` 的 `salt` 参数接受 `Uint8Array` 或 crypto-js `WordArray`;`deriveKeyAndIv` 返回 `Uint8Array`。
|
|
65
|
+
- IIFE 内含 mqtt 浏览器构建与 crypto-js,体积较大;Node 用户不需要该产物。
|
|
66
|
+
- 该加密格式没有 MAC/AEAD,不能防篡改,详见下方安全说明。
|
|
67
|
+
- 本地演示:先 `npm run build`,再打开 [`examples/script-tag.html`](./examples/script-tag.html)。
|
|
30
68
|
|
|
31
69
|
## 快速开始
|
|
32
70
|
|
|
@@ -59,7 +97,7 @@ await client.sendCommand('list'); // => Promise<msgId>
|
|
|
59
97
|
// await client.disconnect();
|
|
60
98
|
```
|
|
61
99
|
|
|
62
|
-
完整示例见 [`examples/basic.js`](./examples/basic.js)
|
|
100
|
+
完整示例见 [`examples/basic.js`](./examples/basic.js)、[`examples/request-response.js`](./examples/request-response.js) 与浏览器直引示例 [`examples/script-tag.html`](./examples/script-tag.html)。
|
|
63
101
|
|
|
64
102
|
## API
|
|
65
103
|
|
|
@@ -201,9 +239,9 @@ CryptoJS -> node: OK
|
|
|
201
239
|
|
|
202
240
|
```bash
|
|
203
241
|
npm install
|
|
204
|
-
npm test # node:test,
|
|
242
|
+
npm test # node:test,45 个用例(含浏览器/Node 互通与 IIFE 冒烟)
|
|
205
243
|
npm run typecheck # tsc checkJs
|
|
206
|
-
npm run build #
|
|
244
|
+
npm run build # dist/index.cjs + 浏览器 IIFE dist/index.iife.js
|
|
207
245
|
npm pack --dry-run
|
|
208
246
|
```
|
|
209
247
|
|