@domain.js/main 0.5.2 → 0.6.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.
@@ -33,6 +33,8 @@ export interface Profile {
33
33
  isSocket?: boolean;
34
34
  /** socket 的时候加入的房间 */
35
35
  roomId?: string;
36
+ /** 是否需要返回 stream */
37
+ needStream?: boolean;
36
38
  }
37
39
  export interface HttpCodes {
38
40
  [propName: string]: number;
@@ -90,6 +90,16 @@ function Router(deps) {
90
90
  if (!method || !lodash_1.default.isFunction(method)) {
91
91
  throw Error(`Missing domain method: ${methodPath}`);
92
92
  }
93
+ const send = (res, results, isEventStream = false) => {
94
+ if ("pipe" in results) {
95
+ if (isEventStream)
96
+ res.setHeader("Content-Type", "text/event-stream");
97
+ results.pipe(res);
98
+ }
99
+ else {
100
+ res.send(code, results);
101
+ }
102
+ };
93
103
  server[verb](route, async (req, res, next) => {
94
104
  const profile = makeProfile(req, methodPath, makeProfileHook);
95
105
  if (resource)
@@ -129,7 +139,7 @@ function Router(deps) {
129
139
  }
130
140
  }
131
141
  else {
132
- res.send(code, code !== 204 && results);
142
+ send(res, code !== 204 && results, req.header("response-event-stream") === "yes");
133
143
  }
134
144
  }
135
145
  catch (e) {
@@ -15,6 +15,6 @@ export declare type Client = Socket<DefaultEventsMap, DefaultEventsMap, DefaultE
15
15
  quit?: Function;
16
16
  entranceOpts?: any[];
17
17
  };
18
- declare const makeProfile: (client: Client, type: string | undefined, auth: string | Signature, extra?: Profile["extra"]) => Profile;
18
+ declare const makeProfile: (client: Client, type: string | undefined, auth: string | Signature | undefined, extra?: Profile["extra"]) => Profile;
19
19
  export declare function BridgeSocket(io: Server, domain: Domain): void;
20
20
  export {};
@@ -58,13 +58,15 @@ const makeProfile = (client, type = "user", auth, extra = {}) => {
58
58
  if (extra.uuid)
59
59
  obj.uuid = extra.uuid;
60
60
  }
61
- if (typeof auth === "string") {
62
- obj.token = auth;
63
- }
64
- else {
65
- obj.sign = auth;
66
- obj.sign.uri = "/socket.io";
67
- obj.sign.method = "socket.init";
61
+ if (auth) {
62
+ if (typeof auth === "string") {
63
+ obj.token = auth;
64
+ }
65
+ else {
66
+ obj.sign = auth;
67
+ obj.sign.uri = "/socket.io";
68
+ obj.sign.method = "socket.init";
69
+ }
68
70
  }
69
71
  return obj;
70
72
  };
@@ -88,10 +90,6 @@ function BridgeSocket(io, domain) {
88
90
  console.log("[%s] connection: client.id: %s", new Date(), client.id);
89
91
  client.on("init", async (type, auth, extra = {}) => {
90
92
  console.log("[%s] socket.init: client.id: %s", new Date(), client.id);
91
- if (!auth) {
92
- client.emit("initError", "auth info lost");
93
- return;
94
- }
95
93
  try {
96
94
  Object.assign(client, { profile: makeProfile(client, type, auth, extra) });
97
95
  if (!client.profile)
@@ -74,6 +74,7 @@ function Utils(cnf) {
74
74
  requestId: req.id(),
75
75
  method,
76
76
  type: "user",
77
+ needStream: req.headers["response-event-stream"] === "yes",
77
78
  extra: {},
78
79
  };
79
80
  if (req.headers["x-auth-user-type"]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {