@domain.js/main 0.2.1 → 0.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/dist/http/defines.d.ts +2 -0
- package/dist/http/socket.d.ts +7 -5
- package/dist/http/socket.js +47 -22
- package/dist/http/utils.js +4 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +6 -6
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +16 -1
- package/package.json +1 -1
package/dist/http/defines.d.ts
CHANGED
package/dist/http/socket.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { Server, Socket } from "socket.io";
|
|
2
2
|
import { DefaultEventsMap } from "socket.io/dist/typed-events";
|
|
3
|
+
import { Opt as Sign } from "../deps/signer";
|
|
3
4
|
import { Domain, Profile } from "./defines";
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
declare type Signature = Sign & {
|
|
6
|
+
signature: string;
|
|
6
7
|
};
|
|
7
|
-
declare type Client = Socket<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any> & {
|
|
8
|
+
export declare type Client = Socket<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any> & {
|
|
8
9
|
profile?: ReturnType<typeof makeProfile>;
|
|
9
|
-
|
|
10
|
+
inited?: boolean;
|
|
11
|
+
roomId?: string;
|
|
10
12
|
};
|
|
11
|
-
declare const makeProfile: (client: Client,
|
|
13
|
+
declare const makeProfile: (client: Client, type: string | undefined, auth: string | Signature, extra?: Profile["extra"]) => Profile;
|
|
12
14
|
export declare function BridgeSocket(io: Server, domain: Domain): void;
|
|
13
15
|
export {};
|
package/dist/http/socket.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.BridgeSocket = void 0;
|
|
7
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
4
8
|
const proxyIps = new Set(["127.0.0.1"]);
|
|
5
9
|
class MyError extends Error {
|
|
6
10
|
constructor(code, message, data) {
|
|
@@ -37,9 +41,8 @@ const utils = {
|
|
|
37
41
|
return realIp.split(",")[0];
|
|
38
42
|
},
|
|
39
43
|
};
|
|
40
|
-
const makeProfile = (client,
|
|
44
|
+
const makeProfile = (client, type = "user", auth, extra = {}) => {
|
|
41
45
|
const obj = {
|
|
42
|
-
token,
|
|
43
46
|
clientIp: utils.clientIp(client),
|
|
44
47
|
remoteIp: utils.remoteIp(client),
|
|
45
48
|
realIp: utils.realIp(client),
|
|
@@ -47,19 +50,31 @@ const makeProfile = (client, token, params, extra) => {
|
|
|
47
50
|
startedAt: new Date(),
|
|
48
51
|
userAgent: client.handshake.headers["user-agent"] || "Not captured",
|
|
49
52
|
requestId: client.id,
|
|
50
|
-
|
|
51
|
-
revision: params.revision,
|
|
52
|
-
/** 用户uuid 可以长期跨app */
|
|
53
|
-
uuid: params.uuid,
|
|
54
|
-
/** 额外信息,自由扩展 */
|
|
53
|
+
type,
|
|
55
54
|
extra,
|
|
56
55
|
};
|
|
56
|
+
if (extra) {
|
|
57
|
+
/** 客户端发布号 */
|
|
58
|
+
if (extra.revision)
|
|
59
|
+
obj.revision = extra.revision;
|
|
60
|
+
/** 用户uuid 可以长期跨app */
|
|
61
|
+
if (extra.uuid)
|
|
62
|
+
obj.uuid = extra.uuid;
|
|
63
|
+
}
|
|
64
|
+
if (typeof auth === "string") {
|
|
65
|
+
obj.token = auth;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
obj.sign = auth;
|
|
69
|
+
obj.sign.uri = "/socket.io";
|
|
70
|
+
obj.sign.method = "socket.init";
|
|
71
|
+
}
|
|
57
72
|
return obj;
|
|
58
73
|
};
|
|
59
74
|
function BridgeSocket(io, domain) {
|
|
60
|
-
const subscribe = domain
|
|
61
|
-
const unsubscribe = domain
|
|
62
|
-
const entrance = domain
|
|
75
|
+
const subscribe = lodash_1.default.get(domain, "message.subscribe");
|
|
76
|
+
const unsubscribe = lodash_1.default.get(domain, "message.unsubscribe");
|
|
77
|
+
const entrance = lodash_1.default.get(domain, "message.entrance");
|
|
63
78
|
if (!subscribe)
|
|
64
79
|
throw Error("要启用 socket 服务,必须要要有 message.subscribe 方法,用来处理 socket 订阅");
|
|
65
80
|
if (!unsubscribe)
|
|
@@ -67,25 +82,32 @@ function BridgeSocket(io, domain) {
|
|
|
67
82
|
if (!entrance)
|
|
68
83
|
throw Error("要启用 socket 服务,必须要要有 message.entrance 方法,用来处理 加入某个房间");
|
|
69
84
|
io.on("connection", (client) => {
|
|
85
|
+
// 定义toJSON 避免 schema 验证报错
|
|
86
|
+
Object.assign(client, {
|
|
87
|
+
toJSON() {
|
|
88
|
+
return {};
|
|
89
|
+
},
|
|
90
|
+
});
|
|
70
91
|
console.log("[%s] connection: client.id: %s", new Date(), client.id);
|
|
71
|
-
client.on("init", async (
|
|
92
|
+
client.on("init", async (type, auth, extra = {}) => {
|
|
72
93
|
console.log("[%s] socket.init: client.id: %s", new Date(), client.id);
|
|
73
|
-
if (!
|
|
74
|
-
client.emit("initError", "
|
|
94
|
+
if (!auth) {
|
|
95
|
+
client.emit("initError", "auth info lost");
|
|
75
96
|
return;
|
|
76
97
|
}
|
|
77
98
|
try {
|
|
78
|
-
Object.assign(client, { profile: makeProfile(client,
|
|
99
|
+
Object.assign(client, { profile: makeProfile(client, type, auth, extra) });
|
|
79
100
|
if (!client.profile)
|
|
80
101
|
throw new MyError("noAuth", "请先登录");
|
|
81
102
|
// 创建消息监听函数
|
|
82
|
-
if (!client.
|
|
83
|
-
client.
|
|
103
|
+
if (!client.inited)
|
|
104
|
+
client.inited = true;
|
|
84
105
|
// 向领域注册改用户的监听函数
|
|
85
|
-
const session = subscribe(client.profile, client
|
|
106
|
+
const session = await subscribe(client.profile, client);
|
|
86
107
|
client.emit("inited", session);
|
|
87
108
|
}
|
|
88
109
|
catch (e) {
|
|
110
|
+
client.inited = false;
|
|
89
111
|
if (e instanceof MyError) {
|
|
90
112
|
client.emit("internalError", e.message, e.code || "unknown");
|
|
91
113
|
return;
|
|
@@ -95,14 +117,17 @@ function BridgeSocket(io, domain) {
|
|
|
95
117
|
});
|
|
96
118
|
client.on("entrance", async (roomId) => {
|
|
97
119
|
try {
|
|
98
|
-
if (!client.profile || !client.
|
|
120
|
+
if (!client.profile || !client.inited)
|
|
99
121
|
return;
|
|
100
|
-
const res = await entrance({ roomId, ...client.profile }, client
|
|
122
|
+
const res = await entrance({ roomId, ...client.profile }, client);
|
|
101
123
|
client.profile.roomId = roomId;
|
|
102
|
-
client.
|
|
124
|
+
client.roomId = roomId;
|
|
103
125
|
client.emit("entranced", res);
|
|
104
126
|
}
|
|
105
127
|
catch (e) {
|
|
128
|
+
client.roomId = undefined;
|
|
129
|
+
if (client.profile)
|
|
130
|
+
client.profile.roomId = undefined;
|
|
106
131
|
if (e instanceof MyError) {
|
|
107
132
|
client.emit("internalError", e.message, e.code || "unknown");
|
|
108
133
|
return;
|
|
@@ -143,10 +168,10 @@ function BridgeSocket(io, domain) {
|
|
|
143
168
|
client.on("disconnect", () => {
|
|
144
169
|
if (!client.profile)
|
|
145
170
|
return;
|
|
146
|
-
if (!client.
|
|
171
|
+
if (!client.inited)
|
|
147
172
|
return;
|
|
148
173
|
// 这里要取消对领域消息的监听
|
|
149
|
-
unsubscribe(client.profile, client
|
|
174
|
+
unsubscribe(client.profile, client);
|
|
150
175
|
});
|
|
151
176
|
});
|
|
152
177
|
}
|
package/dist/http/utils.js
CHANGED
|
@@ -71,8 +71,12 @@ function Utils(cnf) {
|
|
|
71
71
|
userAgent: req.userAgent(),
|
|
72
72
|
startedAt: new Date(),
|
|
73
73
|
requestId: req.id(),
|
|
74
|
+
type: "user",
|
|
74
75
|
extra: {},
|
|
75
76
|
};
|
|
77
|
+
if (req.headers["x-auth-user-type"]) {
|
|
78
|
+
obj.type = req.headers["x-auth-user-type"].toString();
|
|
79
|
+
}
|
|
76
80
|
const token = req.headers["x-auth-token"] || req.query.access_token || req.query.accessToken;
|
|
77
81
|
// token 和签名认证只能二选一
|
|
78
82
|
if (token) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Defaults } from "./defaults";
|
|
2
2
|
import Deps = require("./deps/defines");
|
|
3
|
-
export { Main as
|
|
4
|
-
export { Errors } from "./Errors";
|
|
3
|
+
export { Main as Cfg } from "./cfg";
|
|
5
4
|
export * as DM from "./dm";
|
|
5
|
+
export { Errors } from "./Errors";
|
|
6
|
+
export { Main as Http } from "./http";
|
|
6
7
|
export * as utils from "./utils";
|
|
7
|
-
export { Main as Cfg } from "./cfg";
|
|
8
8
|
export declare const basicErrors: Readonly<Record<"notFound" | "notAllowed" | "noAuth", import("./Errors").ErrorFn>>;
|
|
9
9
|
declare type TDeps = typeof Deps;
|
|
10
10
|
declare type Merge<T> = {
|
package/dist/index.js
CHANGED
|
@@ -19,18 +19,18 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.Main = exports.basicErrors = exports.
|
|
22
|
+
exports.Main = exports.basicErrors = exports.utils = exports.Http = exports.Errors = exports.DM = exports.Cfg = void 0;
|
|
23
23
|
const defaults_1 = require("./defaults");
|
|
24
24
|
const DM = __importStar(require("./dm"));
|
|
25
25
|
const Deps = require("./deps/defines");
|
|
26
|
-
var
|
|
27
|
-
Object.defineProperty(exports, "
|
|
26
|
+
var cfg_1 = require("./cfg");
|
|
27
|
+
Object.defineProperty(exports, "Cfg", { enumerable: true, get: function () { return cfg_1.Main; } });
|
|
28
|
+
exports.DM = __importStar(require("./dm"));
|
|
28
29
|
var Errors_1 = require("./Errors");
|
|
29
30
|
Object.defineProperty(exports, "Errors", { enumerable: true, get: function () { return Errors_1.Errors; } });
|
|
30
|
-
|
|
31
|
+
var http_1 = require("./http");
|
|
32
|
+
Object.defineProperty(exports, "Http", { enumerable: true, get: function () { return http_1.Main; } });
|
|
31
33
|
exports.utils = __importStar(require("./utils"));
|
|
32
|
-
var cfg_1 = require("./cfg");
|
|
33
|
-
Object.defineProperty(exports, "Cfg", { enumerable: true, get: function () { return cfg_1.Main; } });
|
|
34
34
|
exports.basicErrors = defaults_1.defaults.errors;
|
|
35
35
|
function Main(features) {
|
|
36
36
|
const { _ } = defaults_1.defaults;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -75,4 +75,10 @@ declare type Params = {
|
|
|
75
75
|
* @returns Modified address
|
|
76
76
|
*/
|
|
77
77
|
export declare const modifiyURL: (address: string, adds?: Params | undefined, removes?: string[] | undefined) => string;
|
|
78
|
+
/**
|
|
79
|
+
* 等待,知道 test 返回 true
|
|
80
|
+
* @param test 检测函数
|
|
81
|
+
* @param intervalMS 间隔多久判断一次, 单位毫秒 默认 100
|
|
82
|
+
*/
|
|
83
|
+
export declare const waitFor: (test: () => boolean, intervalMS?: number) => Promise<void>;
|
|
78
84
|
export {};
|
package/dist/utils/index.js
CHANGED
|
@@ -18,8 +18,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
18
18
|
__setModuleDefault(result, mod);
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
21
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.modifiyURL = exports.inExpired = exports.tryCatchLog = exports.deepFreeze = exports.sleep = exports.lcfirst = exports.ucfirst = exports.nt2space = exports.randStr = exports.md5 = void 0;
|
|
25
|
+
exports.waitFor = exports.modifiyURL = exports.inExpired = exports.tryCatchLog = exports.deepFreeze = exports.sleep = exports.lcfirst = exports.ucfirst = exports.nt2space = exports.randStr = exports.md5 = void 0;
|
|
26
|
+
const async_1 = __importDefault(require("async"));
|
|
23
27
|
const crypto = __importStar(require("crypto"));
|
|
24
28
|
/** 随机字符串字典 */
|
|
25
29
|
const RAND_STR_DICT = {
|
|
@@ -147,3 +151,14 @@ const modifiyURL = (address, adds, removes) => {
|
|
|
147
151
|
return obj.toString();
|
|
148
152
|
};
|
|
149
153
|
exports.modifiyURL = modifiyURL;
|
|
154
|
+
/**
|
|
155
|
+
* 等待,知道 test 返回 true
|
|
156
|
+
* @param test 检测函数
|
|
157
|
+
* @param intervalMS 间隔多久判断一次, 单位毫秒 默认 100
|
|
158
|
+
*/
|
|
159
|
+
const waitFor = async (test, intervalMS = 100) => {
|
|
160
|
+
await async_1.default.doUntil(async () => {
|
|
161
|
+
await (0, exports.sleep)(intervalMS);
|
|
162
|
+
}, async () => test());
|
|
163
|
+
};
|
|
164
|
+
exports.waitFor = waitFor;
|