@domain.js/main 0.5.3 → 0.6.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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const stream_1 = require("stream");
4
+ const http_1 = require("../http");
5
+ (async () => {
6
+ const services = {
7
+ "home.index": {
8
+ profile: {},
9
+ params: {},
10
+ method: async (profile, params) => {
11
+ return { profile, params };
12
+ },
13
+ },
14
+ stream: {
15
+ profile: {},
16
+ params: {},
17
+ method: async (profile, params) => {
18
+ class TimeStream extends stream_1.Readable {
19
+ constructor(options = {}) {
20
+ super(options);
21
+ this.count = 0;
22
+ this.timer = setInterval(() => {
23
+ this.push(new Date().toISOString() + "\n");
24
+ this.count++;
25
+ if (this.count === 5) {
26
+ clearInterval(this.timer);
27
+ this.push(null);
28
+ }
29
+ }, 1000);
30
+ }
31
+ _read() { }
32
+ }
33
+ const timeStream = new TimeStream();
34
+ timeStream.pipe(process.stdout);
35
+ return timeStream;
36
+ },
37
+ },
38
+ };
39
+ const routers = (r) => {
40
+ r.get("/", "home.index");
41
+ r.get("/stream", "stream");
42
+ };
43
+ const Start = (0, http_1.Main)({
44
+ port: 3009,
45
+ }, {
46
+ domain: services,
47
+ httpCodes: {},
48
+ routers,
49
+ });
50
+ const server = Start();
51
+ server.server.setTimeout(1000 * 60 * 5);
52
+ })();
@@ -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,7 +90,27 @@ function Router(deps) {
90
90
  if (!method || !lodash_1.default.isFunction(method)) {
91
91
  throw Error(`Missing domain method: ${methodPath}`);
92
92
  }
93
- server[verb](route, async (req, res, next) => {
93
+ const send = async (res, results, isEventStream = false) => {
94
+ if ("pipe" in results) {
95
+ if (isEventStream) {
96
+ res.setHeader("Content-Type", "text/event-stream");
97
+ await new Promise((resolve) => {
98
+ results.on("data", (chunk) => {
99
+ res.write(chunk);
100
+ });
101
+ results.on("end", resolve);
102
+ });
103
+ res.end();
104
+ }
105
+ else {
106
+ results.pipe(res);
107
+ }
108
+ }
109
+ else {
110
+ res.send(code, results);
111
+ }
112
+ };
113
+ server[verb](route, async (req, res) => {
94
114
  const profile = makeProfile(req, methodPath, makeProfileHook);
95
115
  if (resource)
96
116
  profile.resource = resource;
@@ -129,15 +149,13 @@ function Router(deps) {
129
149
  }
130
150
  }
131
151
  else {
132
- res.send(code, code !== 204 && results);
152
+ await send(res, code !== 204 && results, req.header("response-event-stream") === "yes");
133
153
  }
134
154
  }
135
155
  catch (e) {
136
156
  res.header("X-ConsumedTime", Date.now() - profile.startedAt.valueOf());
137
- next(error2httpError(e));
138
- return;
157
+ throw error2httpError(e);
139
158
  }
140
- next();
141
159
  });
142
160
  }
143
161
  function RouterVerbFn(verb) {
@@ -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.3",
3
+ "version": "0.6.1",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -76,7 +76,7 @@
76
76
  "lru-cache": "^6.0.0",
77
77
  "moment": "^2.29.1",
78
78
  "mysql2": "^2.3.3",
79
- "restify": "^8.6.0",
79
+ "restify": "^11.1.0",
80
80
  "restify-errors": "^8.0.2",
81
81
  "sequelize": "^6.16.1",
82
82
  "socket.io": "^4.4.1",