Browse Source

add more file

Paul 5 years ago
parent
commit
1c56d6c626
9 changed files with 144 additions and 1 deletions
  1. 7
    0
      dist/constant.js
  2. 1
    0
      dist/constant.js.map
  3. 47
    0
      dist/packet.js
  4. 1
    0
      dist/packet.js.map
  5. 2
    0
      dist/types/constant.d.ts
  6. 11
    0
      dist/types/packet.d.ts
  7. 2
    1
      package.json
  8. 5
    0
      src/constant.ts
  9. 68
    0
      src/packet.ts

+ 7
- 0
dist/constant.js View File

@@ -0,0 +1,7 @@
1
+"use strict";
2
+exports.__esModule = true;
3
+// 单个数据包能够传输的最大数据量
4
+exports.MAX_PAYLOAD = 1024 * 1024;
5
+// 版本
6
+exports.VERSION = '1.0';
7
+//# sourceMappingURL=constant.js.map

+ 1
- 0
dist/constant.js.map View File

@@ -0,0 +1 @@
1
+{"version":3,"file":"constant.js","sourceRoot":"","sources":["../src/constant.ts"],"names":[],"mappings":";;AAAA,kBAAkB;AACL,QAAA,WAAW,GAAG,IAAI,GAAG,IAAI,CAAC;AAEvC,KAAK;AACQ,QAAA,OAAO,GAAG,KAAK,CAAC"}

+ 47
- 0
dist/packet.js View File

@@ -0,0 +1,47 @@
1
+"use strict";
2
+exports.__esModule = true;
3
+var utils_1 = require("./utils");
4
+var Int64 = require("node-int64");
5
+var Packet = /** @class */ (function () {
6
+    function Packet() {
7
+    }
8
+    Packet.pack = function (operator, sequence, header, body) {
9
+        var header = utils_1.Utils.encrypt(header, Packet.key, Packet.key);
10
+        var body = utils_1.Utils.encrypt(body, Packet.key, Packet.key);
11
+        var headerLength = header.length;
12
+        var bodyLength = body.length;
13
+        var buf = new ArrayBuffer(20 + headerLength + bodyLength);
14
+        var dataView = new DataView(buf);
15
+        var nsBuf = new Int64(sequence).toBuffer();
16
+        dataView.setUint32(0, operator);
17
+        dataView.setUint32(12, headerLength);
18
+        dataView.setUint32(16, bodyLength);
19
+        var bufView = new Uint8Array(buf);
20
+        for (var i = 0; i < 8; i++) {
21
+            bufView[4 + i] = nsBuf[i];
22
+        }
23
+        for (var i_1 = 0; i_1 < headerLength; i_1++) {
24
+            bufView[20 + i_1] = header.charCodeAt(i_1);
25
+        }
26
+        for (var i_2 = 0; i_2 < bodyLength; i_2++) {
27
+            bufView[20 + headerLength + i_2] = body.charCodeAt(i_2);
28
+        }
29
+        return buf;
30
+    };
31
+    Packet.unPack = function (data) {
32
+        var dataView = new DataView(data);
33
+        Packet.operator = dataView.getUint32(0, false);
34
+        Packet.sequence = new Int64(new Uint8Array(dataView.buffer.slice(4, 12))).toNumber();
35
+        Packet.headerLength = dataView.getUint32(12, false);
36
+        Packet.bodyLength = dataView.getUint32(16, false);
37
+        var header = utils_1.Utils.ab2str(dataView.buffer.slice(20, 20 + Packet.headerLength));
38
+        var body = utils_1.Utils.ab2str(dataView.buffer.slice(20 + Packet.headerLength));
39
+        Packet.header = utils_1.Utils.decrypt(header, Packet.key, Packet.key);
40
+        Packet.body = utils_1.Utils.decrypt(body, Packet.key, Packet.key);
41
+        return Packet;
42
+    };
43
+    Packet.key = 'b8ca9aa66def05ff3f24919274bb4a66';
44
+    return Packet;
45
+}());
46
+exports.Packet = Packet;
47
+//# sourceMappingURL=packet.js.map

+ 1
- 0
dist/packet.js.map View File

@@ -0,0 +1 @@
1
+{"version":3,"file":"packet.js","sourceRoot":"","sources":["../src/packet.ts"],"names":[],"mappings":";;AAAA,iCAAgC;AAChC,kCAAoC;AAEpC;IAAA;IAgEA,CAAC;IAvDgB,WAAI,GAAnB,UACE,QAAgB,EAChB,QAAgB,EAChB,MAAc,EACd,IAAY;QAEZ,IAAM,MAAM,GAAG,aAAK,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;QAC7D,IAAM,IAAI,GAAG,aAAK,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;QAEzD,IAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC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AQ,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;QAC/C,MAAM,CAAC,QAAQ,GAAG,IAAI,KAAK,CACzB,IAAI,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAC7C,CAAC,QAAQ,EAAE,CAAC;QACb,MAAM,CAAC,YAAY,GAAG,QAAQ,CAAC,SAAS,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QACpD,MAAM,CAAC,UAAU,GAAG,QAAQ,CAAC,SAAS,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QAElD,IAAM,MAAM,GAAG,aAAK,CAAC,MAAM,CACzB,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,GAAG,MAAM,CAAC,YAAY,CAAC,CACpD,CAAC;QACF,IAAM,IAAI,GAAG,aAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC;QAE3E,MAAM,CAAC,MAAM,GAAG,aAAK,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;QAC9D,MAAM,CAAC,IAAI,GAAG,aAAK,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;QAE1D,OAAO,MAAM,CAAC;IAChB,CAAC;IA9Dc,UAAG,GAAG,kCAAkC,CAAC;IA+D1D,aAAC;CAAA,AAhED,IAgEC;AAhEY,wBAAM"}

+ 2
- 0
dist/types/constant.d.ts View File

@@ -0,0 +1,2 @@
1
+export declare const MAX_PAYLOAD: number;
2
+export declare const VERSION = "1.0";

+ 11
- 0
dist/types/packet.d.ts View File

@@ -0,0 +1,11 @@
1
+export declare class Packet {
2
+    private static key;
3
+    private static operator;
4
+    private static sequence;
5
+    private static headerLength;
6
+    private static bodyLength;
7
+    private static header;
8
+    private static body;
9
+    private static pack;
10
+    private static unPack;
11
+}

+ 2
- 1
package.json View File

@@ -9,7 +9,8 @@
9 9
   "author": "",
10 10
   "license": "ISC",
11 11
   "dependencies": {
12
-    "crypto-js": "^3.1.9-1"
12
+    "crypto-js": "^3.1.9-1",
13
+    "node-int64": "^0.4.0"
13 14
   },
14 15
   "devDependencies": {
15 16
     "@types/jest": "^24.0.11",

+ 5
- 0
src/constant.ts View File

@@ -0,0 +1,5 @@
1
+// 单个数据包能够传输的最大数据量
2
+export const MAX_PAYLOAD = 1024 * 1024;
3
+
4
+// 版本
5
+export const VERSION = '1.0';

+ 68
- 0
src/packet.ts View File

@@ -0,0 +1,68 @@
1
+import { Utils } from './utils';
2
+import * as Int64 from 'node-int64';
3
+
4
+export class Packet {
5
+  private static key = 'b8ca9aa66def05ff3f24919274bb4a66';
6
+  private static operator: number;
7
+  private static sequence: number;
8
+  private static headerLength: number;
9
+  private static bodyLength: number;
10
+  private static header: string;
11
+  private static body: string;
12
+
13
+  private static pack(
14
+    operator: number,
15
+    sequence: number,
16
+    header: string,
17
+    body: string,
18
+  ): ArrayBuffer {
19
+    const header = Utils.encrypt(header, Packet.key, Packet.key);
20
+    const body = Utils.encrypt(body, Packet.key, Packet.key);
21
+
22
+    const headerLength = header.length;
23
+    const bodyLength = body.length;
24
+
25
+    const buf = new ArrayBuffer(20 + headerLength + bodyLength);
26
+    const dataView = new DataView(buf);
27
+    const nsBuf = new Int64(sequence).toBuffer();
28
+
29
+    dataView.setUint32(0, operator);
30
+    dataView.setUint32(12, headerLength);
31
+    dataView.setUint32(16, bodyLength);
32
+
33
+    let bufView = new Uint8Array(buf);
34
+    for (var i = 0; i < 8; i++) {
35
+      bufView[4 + i] = nsBuf[i];
36
+    }
37
+    for (let i = 0; i < headerLength; i++) {
38
+      bufView[20 + i] = header.charCodeAt(i);
39
+    }
40
+
41
+    for (let i = 0; i < bodyLength; i++) {
42
+      bufView[20 + headerLength + i] = body.charCodeAt(i);
43
+    }
44
+
45
+    return buf;
46
+  }
47
+
48
+  private static unPack(data: ArrayBuffer): Packet {
49
+    const dataView = new DataView(data);
50
+
51
+    Packet.operator = dataView.getUint32(0, false);
52
+    Packet.sequence = new Int64(
53
+      new Uint8Array(dataView.buffer.slice(4, 12)),
54
+    ).toNumber();
55
+    Packet.headerLength = dataView.getUint32(12, false);
56
+    Packet.bodyLength = dataView.getUint32(16, false);
57
+
58
+    const header = Utils.ab2str(
59
+      dataView.buffer.slice(20, 20 + Packet.headerLength),
60
+    );
61
+    const body = Utils.ab2str(dataView.buffer.slice(20 + Packet.headerLength));
62
+
63
+    Packet.header = Utils.decrypt(header, Packet.key, Packet.key);
64
+    Packet.body = Utils.decrypt(body, Packet.key, Packet.key);
65
+
66
+    return Packet;
67
+  }
68
+}