@tsed/sse 7.79.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.
Files changed (37) hide show
  1. package/.nycrc +27 -0
  2. package/jest.config.js +16 -0
  3. package/lib/cjs/decorators/eventStream.js +12 -0
  4. package/lib/cjs/decorators/eventStream.js.map +1 -0
  5. package/lib/cjs/decorators/eventStreamCtx.js +9 -0
  6. package/lib/cjs/decorators/eventStreamCtx.js.map +1 -0
  7. package/lib/cjs/domain/EventStreamContext.js +110 -0
  8. package/lib/cjs/domain/EventStreamContext.js.map +1 -0
  9. package/lib/cjs/index.js +12 -0
  10. package/lib/cjs/index.js.map +1 -0
  11. package/lib/cjs/interceptors/EventStreamInterceptor.js +34 -0
  12. package/lib/cjs/interceptors/EventStreamInterceptor.js.map +1 -0
  13. package/lib/cjs/middlewares/EventStreamMiddleware.js +33 -0
  14. package/lib/cjs/middlewares/EventStreamMiddleware.js.map +1 -0
  15. package/lib/cjs/package.json +3 -0
  16. package/lib/esm/decorators/eventStream.js +8 -0
  17. package/lib/esm/decorators/eventStream.js.map +1 -0
  18. package/lib/esm/decorators/eventStreamCtx.js +5 -0
  19. package/lib/esm/decorators/eventStreamCtx.js.map +1 -0
  20. package/lib/esm/domain/EventStreamContext.js +106 -0
  21. package/lib/esm/domain/EventStreamContext.js.map +1 -0
  22. package/lib/esm/index.js +9 -0
  23. package/lib/esm/index.js.map +1 -0
  24. package/lib/esm/interceptors/EventStreamInterceptor.js +31 -0
  25. package/lib/esm/interceptors/EventStreamInterceptor.js.map +1 -0
  26. package/lib/esm/middlewares/EventStreamMiddleware.js +30 -0
  27. package/lib/esm/middlewares/EventStreamMiddleware.js.map +1 -0
  28. package/lib/esm/package.json +3 -0
  29. package/lib/types/decorators/eventStream.d.ts +5 -0
  30. package/lib/types/decorators/eventStreamCtx.d.ts +3 -0
  31. package/lib/types/domain/EventStreamContext.d.ts +24 -0
  32. package/lib/types/index.d.ts +8 -0
  33. package/lib/types/interceptors/EventStreamInterceptor.d.ts +6 -0
  34. package/lib/types/middlewares/EventStreamMiddleware.d.ts +4 -0
  35. package/package.json +76 -0
  36. package/readme.md +181 -0
  37. package/tsconfig.esm.json +54 -0
package/.nycrc ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "include": [
3
+ "src/**/*.ts"
4
+ ],
5
+ "exclude": [
6
+ "**/*.spec.ts",
7
+ "**/*.d.ts",
8
+ "node_modules",
9
+ "**/interfaces/**",
10
+ "**/index.ts"
11
+ ],
12
+ "extension": [
13
+ ".ts"
14
+ ],
15
+ "require": [],
16
+ "reporter": [
17
+ "text-summary",
18
+ "html",
19
+ "lcov",
20
+ "json"
21
+ ],
22
+ "check-coverage": true,
23
+ "statements": 100,
24
+ "branches": 81.56,
25
+ "functions": 97.33,
26
+ "lines": 100
27
+ }
package/jest.config.js ADDED
@@ -0,0 +1,16 @@
1
+ // For a detailed explanation regarding each configuration property, visit:
2
+ // https://jestjs.io/docs/en/configuration.html
3
+ const config = require("@tsed/jest-config");
4
+
5
+ module.exports = {
6
+ ...config,
7
+ roots: ["<rootDir>/src"],
8
+ coverageThreshold: {
9
+ global: {
10
+ statements: 0,
11
+ branches: 0,
12
+ functions: 0,
13
+ lines: 0
14
+ }
15
+ }
16
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventStream = void 0;
4
+ const core_1 = require("@tsed/core");
5
+ const platform_middlewares_1 = require("@tsed/platform-middlewares");
6
+ const schema_1 = require("@tsed/schema");
7
+ const EventStreamMiddleware_1 = require("../middlewares/EventStreamMiddleware");
8
+ function EventStream(opts = {}) {
9
+ return (0, core_1.useDecorators)((0, core_1.StoreSet)(EventStreamMiddleware_1.EventStreamMiddleware, opts), (0, platform_middlewares_1.Use)(EventStreamMiddleware_1.EventStreamMiddleware), (0, schema_1.AcceptMime)("text/event-stream"), (0, schema_1.Header)("Content-Type", "text/event-stream"));
10
+ }
11
+ exports.EventStream = EventStream;
12
+ //# sourceMappingURL=eventStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventStream.js","sourceRoot":"","sources":["../../../src/decorators/eventStream.ts"],"names":[],"mappings":";;;AAAA,qCAAmD;AACnD,qEAA+C;AAC/C,yCAAgD;AAChD,gFAA2E;AAO3E,SAAgB,WAAW,CAAC,OAAwB,EAAE;IACpD,OAAO,IAAA,oBAAa,EAClB,IAAA,eAAQ,EAAC,6CAAqB,EAAE,IAAI,CAAC,EACrC,IAAA,0BAAG,EAAC,6CAAqB,CAAC,EAC1B,IAAA,mBAAU,EAAC,mBAAmB,CAAC,EAC/B,IAAA,eAAM,EAAC,cAAc,EAAE,mBAAmB,CAAC,CAC5C,CAAC;AACJ,CAAC;AAPD,kCAOC"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventStreamCtx = void 0;
4
+ const platform_params_1 = require("@tsed/platform-params");
5
+ function EventStreamCtx() {
6
+ return (0, platform_params_1.Context)("EventStreamContext");
7
+ }
8
+ exports.EventStreamCtx = EventStreamCtx;
9
+ //# sourceMappingURL=eventStreamCtx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventStreamCtx.js","sourceRoot":"","sources":["../../../src/decorators/eventStreamCtx.ts"],"names":[],"mappings":";;;AAAA,2DAA8C;AAK9C,SAAgB,cAAc;IAC5B,OAAO,IAAA,yBAAO,EAAC,oBAAoB,CAAC,CAAC;AACvC,CAAC;AAFD,wCAEC"}
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventStreamContext = void 0;
4
+ const core_1 = require("@tsed/core");
5
+ const platform_response_filter_1 = require("@tsed/platform-response-filter");
6
+ class EventStreamContext {
7
+ $ctx;
8
+ opts;
9
+ responseFilter;
10
+ #isDone = false;
11
+ constructor({ $ctx, ...opts }) {
12
+ this.$ctx = $ctx;
13
+ this.opts = opts;
14
+ this.responseFilter = this.$ctx.injector.get(platform_response_filter_1.PlatformResponseFilter);
15
+ }
16
+ writeHead(headers = {}) {
17
+ console.log("this.$ctx", this.$ctx);
18
+ this.on("end", () => {
19
+ this.$ctx.logger.info({
20
+ event: "SSE_ENDED"
21
+ });
22
+ this.$ctx.getRes().end();
23
+ this.#isDone = true;
24
+ });
25
+ this.on("close", () => {
26
+ this.$ctx.logger.info({
27
+ event: "SSE_CLOSED_BY_CLIENT"
28
+ });
29
+ this.$ctx.getRes().end();
30
+ this.#isDone = true;
31
+ });
32
+ this.$ctx.getRes().writeHead(200, {
33
+ ...this.opts.headers,
34
+ ...headers,
35
+ "Content-Type": "text/event-stream",
36
+ "Cache-Control": "no-cache",
37
+ Connection: "keep-alive"
38
+ });
39
+ }
40
+ on(event, listener) {
41
+ this.$ctx.getRes().on(event, listener);
42
+ return this;
43
+ }
44
+ subscribe(observable) {
45
+ observable.subscribe((data) => {
46
+ this.emit(this.opts.event, data);
47
+ });
48
+ return this;
49
+ }
50
+ stream(stream) {
51
+ const listener = (data) => {
52
+ if (this.isDone()) {
53
+ stream.off("data", listener);
54
+ }
55
+ else {
56
+ this.emit(this.opts.event, data);
57
+ }
58
+ };
59
+ stream.on("data", listener);
60
+ }
61
+ emit(event, data) {
62
+ if (this.isDone()) {
63
+ return;
64
+ }
65
+ const { $ctx } = this;
66
+ if (data === undefined) {
67
+ return this;
68
+ }
69
+ if (Buffer.isBuffer(data)) {
70
+ data = data.toString("utf8");
71
+ }
72
+ if ((0, core_1.isBoolean)(data) || (0, core_1.isNumber)(data) || (0, core_1.isString)(data) || (0, core_1.isDate)(data) || data === null) {
73
+ this.write({
74
+ event: event || "",
75
+ data: JSON.stringify(data)
76
+ });
77
+ return this;
78
+ }
79
+ this.responseFilter
80
+ .serialize(data, $ctx)
81
+ .then((data) => {
82
+ return this.responseFilter.transform(data, $ctx);
83
+ })
84
+ .then((data) => {
85
+ data = JSON.stringify(data);
86
+ this.write({
87
+ event: event || "",
88
+ data: data
89
+ });
90
+ });
91
+ return this;
92
+ }
93
+ write(obj) {
94
+ const values = Object.entries(obj)
95
+ .map(([key, value]) => {
96
+ return `${key}: ${value}`;
97
+ })
98
+ .join("\n");
99
+ this.$ctx.getRes().write(`${values}\n\n`);
100
+ }
101
+ isDone() {
102
+ return this.#isDone;
103
+ }
104
+ end() {
105
+ this.#isDone = true;
106
+ this.$ctx.getRes().end();
107
+ }
108
+ }
109
+ exports.EventStreamContext = EventStreamContext;
110
+ //# sourceMappingURL=EventStreamContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventStreamContext.js","sourceRoot":"","sources":["../../../src/domain/EventStreamContext.ts"],"names":[],"mappings":";;;AACA,qCAAiE;AACjE,6EAAsE;AAStE,MAAa,kBAAkB;IACrB,IAAI,CAAkB;IACtB,IAAI,CAAkB;IACtB,cAAc,CAAyB;IAC/C,OAAO,GAAG,KAAK,CAAC;IAEhB,YAAY,EAAC,IAAI,EAAE,GAAG,IAAI,EAA4B;QACpD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAyB,iDAAsB,CAAE,CAAC;IAChG,CAAC;IAED,SAAS,CAAC,UAAkC,EAAE;QAC5C,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;YAClB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;gBACpB,KAAK,EAAE,WAAW;aACnB,CAAC,CAAC;YACH,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC;YACzB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACtB,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,EAA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,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC;aAC/B,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;YACpB,OAAO,GAAG,GAAG,KAAK,KAAK,EAAE,CAAC;QAC5B,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;QAEd,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,MAAM,MAAM,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,GAAG;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC;IAC3B,CAAC;CACF;AAxHD,gDAwHC"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ /**
5
+ * @file Automatically generated by @tsed/barrels.
6
+ */
7
+ tslib_1.__exportStar(require("./decorators/eventStream.js"), exports);
8
+ tslib_1.__exportStar(require("./decorators/eventStreamCtx.js"), exports);
9
+ tslib_1.__exportStar(require("./domain/EventStreamContext.js"), exports);
10
+ tslib_1.__exportStar(require("./interceptors/EventStreamInterceptor.js"), exports);
11
+ tslib_1.__exportStar(require("./middlewares/EventStreamMiddleware.js"), exports);
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,sEAA4C;AAC5C,yEAA+C;AAC/C,yEAA+C;AAC/C,mFAAyD;AACzD,iFAAuD"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventStreamInterceptor = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@tsed/common");
6
+ const core_1 = require("@tsed/core");
7
+ const di_1 = require("@tsed/di");
8
+ let EventStreamInterceptor = class EventStreamInterceptor {
9
+ $ctx;
10
+ async intercept(context) {
11
+ const result = await context.next();
12
+ const eventStream = this.$ctx.get("EventStreamContext");
13
+ if (eventStream) {
14
+ if ((0, core_1.isObservable)(result)) {
15
+ eventStream.subscribe(result);
16
+ return;
17
+ }
18
+ if ((0, core_1.isStream)(result)) {
19
+ eventStream.stream(result);
20
+ return;
21
+ }
22
+ }
23
+ return result;
24
+ }
25
+ };
26
+ tslib_1.__decorate([
27
+ (0, common_1.InjectContext)(),
28
+ tslib_1.__metadata("design:type", Function)
29
+ ], EventStreamInterceptor.prototype, "$ctx", void 0);
30
+ EventStreamInterceptor = tslib_1.__decorate([
31
+ (0, di_1.Injectable)()
32
+ ], EventStreamInterceptor);
33
+ exports.EventStreamInterceptor = EventStreamInterceptor;
34
+ //# sourceMappingURL=EventStreamInterceptor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventStreamInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/EventStreamInterceptor.ts"],"names":[],"mappings":";;;;AACA,yCAA2C;AAC3C,qCAAkD;AAElD,iCAAoC;AAK7B,IAAM,sBAAsB,GAA5B,MAAM,sBAAsB;IAEvB,IAAI,CAAkB;IAEhC,KAAK,CAAC,SAAS,CAAC,OAA2B;QACzC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;QACpC,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAqB,oBAAoB,CAAC,CAAC;QAE5E,IAAI,WAAW,EAAE;YACf,IAAI,IAAA,mBAAY,EAAC,MAAM,CAAC,EAAE;gBACxB,WAAW,CAAC,SAAS,CAAC,MAA6B,CAAC,CAAC;gBAErD,OAAO;aACR;YAED,IAAI,IAAA,eAAQ,EAAC,MAAM,CAAC,EAAE;gBACpB,WAAW,CAAC,MAAM,CAAC,MAA+B,CAAC,CAAC;gBAEpD,OAAO;aACR;SACF;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF,CAAA;AAvBC;IAAC,IAAA,sBAAa,GAAE;;oDACgB;AAFrB,sBAAsB;IADlC,IAAA,eAAU,GAAE;GACA,sBAAsB,CAwBlC;AAxBY,wDAAsB"}
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var EventStreamMiddleware_1;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.EventStreamMiddleware = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const common_1 = require("@tsed/common");
7
+ const platform_middlewares_1 = require("@tsed/platform-middlewares");
8
+ const EventStreamContext_1 = require("../domain/EventStreamContext");
9
+ let EventStreamMiddleware = EventStreamMiddleware_1 = class EventStreamMiddleware {
10
+ use($ctx) {
11
+ const opts = $ctx.endpoint.store.get(EventStreamMiddleware_1, {});
12
+ // Enable stream logs
13
+ $ctx.logger.flush();
14
+ $ctx.logger.maxStackSize = 0;
15
+ const eventStream = new EventStreamContext_1.EventStreamContext({
16
+ $ctx,
17
+ ...opts
18
+ });
19
+ $ctx.set("EventStreamContext", eventStream);
20
+ eventStream.writeHead();
21
+ }
22
+ };
23
+ tslib_1.__decorate([
24
+ tslib_1.__param(0, (0, common_1.Context)()),
25
+ tslib_1.__metadata("design:type", Function),
26
+ tslib_1.__metadata("design:paramtypes", [common_1.PlatformContext]),
27
+ tslib_1.__metadata("design:returntype", void 0)
28
+ ], EventStreamMiddleware.prototype, "use", null);
29
+ EventStreamMiddleware = EventStreamMiddleware_1 = tslib_1.__decorate([
30
+ (0, platform_middlewares_1.Middleware)()
31
+ ], EventStreamMiddleware);
32
+ exports.EventStreamMiddleware = EventStreamMiddleware;
33
+ //# sourceMappingURL=EventStreamMiddleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventStreamMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/EventStreamMiddleware.ts"],"names":[],"mappings":";;;;;AAAA,yCAAsD;AACtD,qEAAsD;AACtD,qEAAgE;AAGzD,IAAM,qBAAqB,6BAA3B,MAAM,qBAAqB;IAChC,GAAG,CAAY,IAAqB;QAClC,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAqB,EAAE,EAAE,CAAC,CAAC;QAEhE,qBAAqB;QACrB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACpB,IAAI,CAAC,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC;QAE7B,MAAM,WAAW,GAAG,IAAI,uCAAkB,CAAC;YACzC,IAAI;YACJ,GAAG,IAAI;SACR,CAAC,CAAC;QAEH,IAAI,CAAC,GAAG,CAAC,oBAAoB,EAAE,WAAW,CAAC,CAAC;QAE5C,WAAW,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC;CACF,CAAA;AAhBC;IAAK,mBAAA,IAAA,gBAAO,GAAE,CAAA;;6CAAO,wBAAe;;gDAenC;AAhBU,qBAAqB;IADjC,IAAA,iCAAU,GAAE;GACA,qBAAqB,CAiBjC;AAjBY,sDAAqB"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,8 @@
1
+ import { StoreSet, useDecorators } from "@tsed/core";
2
+ import { Use } from "@tsed/platform-middlewares";
3
+ import { Header, AcceptMime } from "@tsed/schema";
4
+ import { EventStreamMiddleware } from "../middlewares/EventStreamMiddleware.js";
5
+ export function EventStream(opts = {}) {
6
+ return useDecorators(StoreSet(EventStreamMiddleware, opts), Use(EventStreamMiddleware), AcceptMime("text/event-stream"), Header("Content-Type", "text/event-stream"));
7
+ }
8
+ //# sourceMappingURL=eventStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventStream.js","sourceRoot":"","sources":["../../../src/decorators/eventStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACnD,OAAO,EAAC,GAAG,EAAC,MAAM,4BAA4B,CAAC;AAC/C,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,cAAc,CAAC;AAChD,OAAO,EAAC,qBAAqB,EAAC,MAAM,sCAAsC,CAAC;AAO3E,MAAM,UAAU,WAAW,CAAC,OAAwB,EAAE;IACpD,OAAO,aAAa,CAClB,QAAQ,CAAC,qBAAqB,EAAE,IAAI,CAAC,EACrC,GAAG,CAAC,qBAAqB,CAAC,EAC1B,UAAU,CAAC,mBAAmB,CAAC,EAC/B,MAAM,CAAC,cAAc,EAAE,mBAAmB,CAAC,CAC5C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { Context } from "@tsed/platform-params";
2
+ export function EventStreamCtx() {
3
+ return Context("EventStreamContext");
4
+ }
5
+ //# sourceMappingURL=eventStreamCtx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventStreamCtx.js","sourceRoot":"","sources":["../../../src/decorators/eventStreamCtx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,uBAAuB,CAAC;AAK9C,MAAM,UAAU,cAAc;IAC5B,OAAO,OAAO,CAAC,oBAAoB,CAAC,CAAC;AACvC,CAAC"}
@@ -0,0 +1,106 @@
1
+ import { isBoolean, isDate, isNumber, isString } from "@tsed/core";
2
+ import { PlatformResponseFilter } from "@tsed/platform-response-filter";
3
+ export class EventStreamContext {
4
+ $ctx;
5
+ opts;
6
+ responseFilter;
7
+ #isDone = false;
8
+ constructor({ $ctx, ...opts }) {
9
+ this.$ctx = $ctx;
10
+ this.opts = opts;
11
+ this.responseFilter = this.$ctx.injector.get(PlatformResponseFilter);
12
+ }
13
+ writeHead(headers = {}) {
14
+ console.log("this.$ctx", this.$ctx);
15
+ this.on("end", () => {
16
+ this.$ctx.logger.info({
17
+ event: "SSE_ENDED"
18
+ });
19
+ this.$ctx.getRes().end();
20
+ this.#isDone = true;
21
+ });
22
+ this.on("close", () => {
23
+ this.$ctx.logger.info({
24
+ event: "SSE_CLOSED_BY_CLIENT"
25
+ });
26
+ this.$ctx.getRes().end();
27
+ this.#isDone = true;
28
+ });
29
+ this.$ctx.getRes().writeHead(200, {
30
+ ...this.opts.headers,
31
+ ...headers,
32
+ "Content-Type": "text/event-stream",
33
+ "Cache-Control": "no-cache",
34
+ Connection: "keep-alive"
35
+ });
36
+ }
37
+ on(event, listener) {
38
+ this.$ctx.getRes().on(event, listener);
39
+ return this;
40
+ }
41
+ subscribe(observable) {
42
+ observable.subscribe((data) => {
43
+ this.emit(this.opts.event, data);
44
+ });
45
+ return this;
46
+ }
47
+ stream(stream) {
48
+ const listener = (data) => {
49
+ if (this.isDone()) {
50
+ stream.off("data", listener);
51
+ }
52
+ else {
53
+ this.emit(this.opts.event, data);
54
+ }
55
+ };
56
+ stream.on("data", listener);
57
+ }
58
+ emit(event, data) {
59
+ if (this.isDone()) {
60
+ return;
61
+ }
62
+ const { $ctx } = this;
63
+ if (data === undefined) {
64
+ return this;
65
+ }
66
+ if (Buffer.isBuffer(data)) {
67
+ data = data.toString("utf8");
68
+ }
69
+ if (isBoolean(data) || isNumber(data) || isString(data) || isDate(data) || data === null) {
70
+ this.write({
71
+ event: event || "",
72
+ data: JSON.stringify(data)
73
+ });
74
+ return this;
75
+ }
76
+ this.responseFilter
77
+ .serialize(data, $ctx)
78
+ .then((data) => {
79
+ return this.responseFilter.transform(data, $ctx);
80
+ })
81
+ .then((data) => {
82
+ data = JSON.stringify(data);
83
+ this.write({
84
+ event: event || "",
85
+ data: data
86
+ });
87
+ });
88
+ return this;
89
+ }
90
+ write(obj) {
91
+ const values = Object.entries(obj)
92
+ .map(([key, value]) => {
93
+ return `${key}: ${value}`;
94
+ })
95
+ .join("\n");
96
+ this.$ctx.getRes().write(`${values}\n\n`);
97
+ }
98
+ isDone() {
99
+ return this.#isDone;
100
+ }
101
+ end() {
102
+ this.#isDone = true;
103
+ this.$ctx.getRes().end();
104
+ }
105
+ }
106
+ //# sourceMappingURL=EventStreamContext.js.map
@@ -0,0 +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K,CAAC,GAAG,MAAM,MAAM,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,GAAG;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC;IAC3B,CAAC;CACF"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @file Automatically generated by @tsed/barrels.
3
+ */
4
+ export * from "./decorators/eventStream.js";
5
+ export * from "./decorators/eventStreamCtx.js";
6
+ export * from "./domain/EventStreamContext.js";
7
+ export * from "./interceptors/EventStreamInterceptor.js";
8
+ export * from "./middlewares/EventStreamMiddleware.js";
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,6BAA6B,CAAC;AAC5C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,0CAA0C,CAAC;AACzD,cAAc,wCAAwC,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { InjectContext } from "@tsed/common";
3
+ import { isStream, isObservable } from "@tsed/core";
4
+ import { Injectable } from "@tsed/di";
5
+ let EventStreamInterceptor = class EventStreamInterceptor {
6
+ $ctx;
7
+ async intercept(context) {
8
+ const result = await context.next();
9
+ const eventStream = this.$ctx.get("EventStreamContext");
10
+ if (eventStream) {
11
+ if (isObservable(result)) {
12
+ eventStream.subscribe(result);
13
+ return;
14
+ }
15
+ if (isStream(result)) {
16
+ eventStream.stream(result);
17
+ return;
18
+ }
19
+ }
20
+ return result;
21
+ }
22
+ };
23
+ __decorate([
24
+ InjectContext(),
25
+ __metadata("design:type", Function)
26
+ ], EventStreamInterceptor.prototype, "$ctx", void 0);
27
+ EventStreamInterceptor = __decorate([
28
+ Injectable()
29
+ ], EventStreamInterceptor);
30
+ export { EventStreamInterceptor };
31
+ //# sourceMappingURL=EventStreamInterceptor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventStreamInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/EventStreamInterceptor.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,aAAa,EAAC,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAC,QAAQ,EAAE,YAAY,EAAC,MAAM,YAAY,CAAC;AAElD,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AAK7B,IAAM,sBAAsB,GAA5B,MAAM,sBAAsB;IAEvB,IAAI,CAAkB;IAEhC,KAAK,CAAC,SAAS,CAAC,OAA2B;QACzC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;QACpC,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAqB,oBAAoB,CAAC,CAAC;QAE5E,IAAI,WAAW,EAAE;YACf,IAAI,YAAY,CAAC,MAAM,CAAC,EAAE;gBACxB,WAAW,CAAC,SAAS,CAAC,MAA6B,CAAC,CAAC;gBAErD,OAAO;aACR;YAED,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE;gBACpB,WAAW,CAAC,MAAM,CAAC,MAA+B,CAAC,CAAC;gBAEpD,OAAO;aACR;SACF;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF,CAAA;AAvBC;IAAC,aAAa,EAAE;;oDACgB;AAFrB,sBAAsB;IADlC,UAAU,EAAE;GACA,sBAAsB,CAwBlC;SAxBY,sBAAsB"}
@@ -0,0 +1,30 @@
1
+ var EventStreamMiddleware_1;
2
+ import { __decorate, __metadata, __param } from "tslib";
3
+ import { Context, PlatformContext } from "@tsed/common";
4
+ import { Middleware } from "@tsed/platform-middlewares";
5
+ import { EventStreamContext } from "../domain/EventStreamContext.js";
6
+ let EventStreamMiddleware = EventStreamMiddleware_1 = class EventStreamMiddleware {
7
+ use($ctx) {
8
+ const opts = $ctx.endpoint.store.get(EventStreamMiddleware_1, {});
9
+ // Enable stream logs
10
+ $ctx.logger.flush();
11
+ $ctx.logger.maxStackSize = 0;
12
+ const eventStream = new EventStreamContext({
13
+ $ctx,
14
+ ...opts
15
+ });
16
+ $ctx.set("EventStreamContext", eventStream);
17
+ eventStream.writeHead();
18
+ }
19
+ };
20
+ __decorate([
21
+ __param(0, Context()),
22
+ __metadata("design:type", Function),
23
+ __metadata("design:paramtypes", [PlatformContext]),
24
+ __metadata("design:returntype", void 0)
25
+ ], EventStreamMiddleware.prototype, "use", null);
26
+ EventStreamMiddleware = EventStreamMiddleware_1 = __decorate([
27
+ Middleware()
28
+ ], EventStreamMiddleware);
29
+ export { EventStreamMiddleware };
30
+ //# sourceMappingURL=EventStreamMiddleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventStreamMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/EventStreamMiddleware.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,OAAO,EAAE,eAAe,EAAC,MAAM,cAAc,CAAC;AACtD,OAAO,EAAC,UAAU,EAAC,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAC,kBAAkB,EAAC,MAAM,8BAA8B,CAAC;AAGzD,IAAM,qBAAqB,6BAA3B,MAAM,qBAAqB;IAChC,GAAG,CAAY,IAAqB;QAClC,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,uBAAqB,EAAE,EAAE,CAAC,CAAC;QAEhE,qBAAqB;QACrB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACpB,IAAI,CAAC,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC;QAE7B,MAAM,WAAW,GAAG,IAAI,kBAAkB,CAAC;YACzC,IAAI;YACJ,GAAG,IAAI;SACR,CAAC,CAAC;QAEH,IAAI,CAAC,GAAG,CAAC,oBAAoB,EAAE,WAAW,CAAC,CAAC;QAE5C,WAAW,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC;CACF,CAAA;AAhBC;IAAK,WAAA,OAAO,EAAE,CAAA;;qCAAO,eAAe;;gDAenC;AAhBU,qBAAqB;IADjC,UAAU,EAAE;GACA,qBAAqB,CAiBjC;SAjBY,qBAAqB"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,5 @@
1
+ export interface EventStreamOpts {
2
+ headers?: Record<string, string>;
3
+ event?: string;
4
+ }
5
+ export declare function EventStream(opts?: EventStreamOpts): any;
@@ -0,0 +1,3 @@
1
+ import type { EventStreamContext } from "../domain/EventStreamContext";
2
+ export type EventStreamCtx = EventStreamContext;
3
+ export declare function EventStreamCtx(): ParameterDecorator;
@@ -0,0 +1,24 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { PlatformContext } from "@tsed/common";
4
+ import type { EventEmitter } from "events";
5
+ import type { Observable } from "rxjs";
6
+ import type { EventStreamOpts } from "../decorators/eventStream";
7
+ export type EventStreamContextOptions = {
8
+ $ctx: PlatformContext;
9
+ } & EventStreamOpts;
10
+ export declare class EventStreamContext {
11
+ #private;
12
+ private $ctx;
13
+ private opts;
14
+ private responseFilter;
15
+ constructor({ $ctx, ...opts }: EventStreamContextOptions);
16
+ writeHead(headers?: Record<string, string>): void;
17
+ on(event: string, listener: (...args: any[]) => void): this;
18
+ subscribe(observable: Observable<unknown>): this;
19
+ stream(stream: NodeJS.ReadableStream | EventEmitter): void;
20
+ emit(event: string | undefined, data: unknown): this | undefined;
21
+ write(obj: Record<string, string>): void;
22
+ isDone(): boolean;
23
+ end(): void;
24
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @file Automatically generated by @tsed/barrels.
3
+ */
4
+ export * from "./decorators/eventStream.js";
5
+ export * from "./decorators/eventStreamCtx.js";
6
+ export * from "./domain/EventStreamContext.js";
7
+ export * from "./interceptors/EventStreamInterceptor.js";
8
+ export * from "./middlewares/EventStreamMiddleware.js";
@@ -0,0 +1,6 @@
1
+ import type { PlatformContext } from "@tsed/common";
2
+ import type { InterceptorContext, InterceptorMethods } from "@tsed/di";
3
+ export declare class EventStreamInterceptor implements InterceptorMethods {
4
+ protected $ctx: PlatformContext;
5
+ intercept(context: InterceptorContext): Promise<unknown>;
6
+ }
@@ -0,0 +1,4 @@
1
+ import { PlatformContext } from "@tsed/common";
2
+ export declare class EventStreamMiddleware {
3
+ use($ctx: PlatformContext): void;
4
+ }
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@tsed/sse",
3
+ "version": "7.79.0",
4
+ "description": "Server-sent events package for Ts.ED framework",
5
+ "keywords": [
6
+ "tsed",
7
+ "server-sent events",
8
+ "sse",
9
+ "server",
10
+ "realtime"
11
+ ],
12
+ "source": "./src/index.ts",
13
+ "main": "./lib/cjs/index.js",
14
+ "module": "./lib/esm/index.js",
15
+ "typings": "./lib/types/index.d.ts",
16
+ "exports": {
17
+ "types": "./lib/types/index.d.ts",
18
+ "import": "./lib/esm/index.js",
19
+ "require": "./lib/cjs/index.js",
20
+ "default": "./lib/esm/index.js"
21
+ },
22
+ "scripts": {
23
+ "build": "yarn barrels && yarn build:ts",
24
+ "barrels": "barrels",
25
+ "start": "ts-node test/app/index.ts",
26
+ "test": "jest --runInBand --testTimeout 10000 && jest-coverage-thresholds-bumper",
27
+ "build:ts": "tsc --build tsconfig.json && tsc --build tsconfig.esm.json"
28
+ },
29
+ "dependencies": {
30
+ "tslib": "2.6.1"
31
+ },
32
+ "devDependencies": {
33
+ "@tsed/barrels": "7.79.0",
34
+ "@tsed/common": "7.79.0",
35
+ "@tsed/core": "7.79.0",
36
+ "@tsed/di": "7.79.0",
37
+ "@tsed/json-mapper": "7.79.0",
38
+ "@tsed/openspec": "7.79.0",
39
+ "@tsed/platform-middlewares": "7.79.0",
40
+ "@tsed/schema": "7.79.0",
41
+ "@tsed/socketio-testing": "7.79.0",
42
+ "@tsed/typescript": "7.79.0",
43
+ "eslint": "^8.57.0",
44
+ "express": "^4.19.1",
45
+ "jest": "^29.7.0",
46
+ "typescript": "4.9.5"
47
+ },
48
+ "peerDependencies": {
49
+ "@tsed/di": "7.79.0",
50
+ "@tsed/json-mapper": "7.79.0",
51
+ "@tsed/logger": ">=6.7.5",
52
+ "@tsed/platform-middlewares": "7.79.0",
53
+ "@tsed/schema": "7.79.0"
54
+ },
55
+ "peerDependenciesMeta": {
56
+ "@tsed/di": {
57
+ "optional": false
58
+ },
59
+ "@tsed/json-mapper": {
60
+ "optional": false
61
+ },
62
+ "@tsed/platform-middlewares": {
63
+ "optional": false
64
+ },
65
+ "@tsed/schema": {
66
+ "optional": false
67
+ }
68
+ },
69
+ "repository": "https://github.com/tsedio/tsed",
70
+ "bugs": {
71
+ "url": "https://github.com/tsedio/tsed/issues"
72
+ },
73
+ "homepage": "https://github.com/tsedio/tsed/tree/production/packages/third-parties/sse",
74
+ "author": "Romain Lenzotti",
75
+ "license": "MIT"
76
+ }
package/readme.md ADDED
@@ -0,0 +1,181 @@
1
+ <p style="text-align: center" align="center">
2
+ <a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
3
+ </p>
4
+
5
+ <div align="center">
6
+ <h1>Server-sent events</h1>
7
+
8
+ [![Build & Release](https://github.com/tsedio/tsed/workflows/Build%20&%20Release/badge.svg)](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
9
+ [![PR Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
10
+ [![npm version](https://badge.fury.io/js/%40tsed%2Fcommon.svg)](https://badge.fury.io/js/%40tsed%2Fcommon)
11
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
12
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
13
+ [![github](https://img.shields.io/static/v1?label=Github%20sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/romakita)
14
+ [![opencollective](https://img.shields.io/static/v1?label=OpenCollective%20sponsor&message=%E2%9D%A4&logo=OpenCollective&color=%23fe8e86)](https://opencollective.com/tsed)
15
+
16
+ </div>
17
+
18
+ <div align="center">
19
+ <a href="https://tsed.io/">Website</a>
20
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
21
+ <a href="https://tsed.io/getting-started/">Getting started</a>
22
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
23
+ <a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
24
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
25
+ <a href="https://twitter.com/TsED_io">Twitter</a>
26
+ </div>
27
+
28
+ <hr />
29
+
30
+ A package of Ts.ED framework. See website: https://tsed.io/#/tutorials/server-sent-events
31
+
32
+ Server-sent events let you push data to the client. It's a simple way to send data from the server to the client. The data is sent as a stream of messages, with an optional event name and id. It's a simple way to send data from the server to the client.
33
+
34
+ ## Installation
35
+
36
+ Before using the Server-sent events, we need to install the `@tsed/sse` module.
37
+
38
+ ```bash
39
+ npm install --save @tsed/sse
40
+ ```
41
+
42
+ Then add the following configuration in your Server:
43
+
44
+ ```typescript
45
+ import {Configuration} from "@tsed/common";
46
+ import "@tsed/sse"; // import sse Ts.ED module
47
+
48
+ @Configuration({
49
+ acceptMimes: ["application/json", "text/event-stream"]
50
+ })
51
+ export class Server {}
52
+ ```
53
+
54
+ ::: warning
55
+ There is a known issue with the `compression` middleware. The
56
+ `compression` middleware should be disabled to work correctly with Server-sent events.
57
+ :::
58
+
59
+ ## Features
60
+
61
+ - Support decorator usage to enable event-stream on an endpoint,
62
+ - Support Node.js stream like `EventEmmiter` to emit events from your controller to your consumer,
63
+ - Support `Observable` from `rxjs` to emit events from your controller to your consumer.
64
+ - Support `@tsed/json-mapper` to serialize your model before sending it to the client.
65
+ - Gives an API compatible with Express.js and Koa.js.
66
+
67
+ ## Enable event-stream
68
+
69
+ To enable the event-stream on an endpoint, you need to use the `@EventStream()` decorator on a method of a controller.
70
+
71
+ ```typescript
72
+ import {Controller} from "@tsed/di";
73
+ import {Get} from "@tsed/schema";
74
+ import {EventStream, EventStreamCtx} from "@tsed/sse";
75
+
76
+ @Controller("/sse")
77
+ export class MyCtrl {
78
+ @Get("/events")
79
+ @EventStream()
80
+ events(@EventStreamCtx() eventStream: EventStreamCtx) {
81
+ let intervalId: ReturnType<typeof setInterval>;
82
+
83
+ eventStream.on("close", () => {
84
+ clearInterval(intervalId);
85
+ });
86
+
87
+ eventStream.on("end", () => {
88
+ clearInterval(intervalId);
89
+ });
90
+
91
+ intervalId = setInterval(() => {
92
+ // Ts.ED support Model serialization using json-mapper here
93
+ eventStream.emit("event", new Date());
94
+ }, 1000);
95
+ }
96
+ }
97
+ ```
98
+
99
+ ### Stream events
100
+
101
+ You can use Node.js stream like `EventEmmiter` to emit events from your controller to your consumer:
102
+
103
+ ```ts
104
+ import {EventStream} from "@tsed/sse";
105
+ import {Controller} from "@tsed/di";
106
+ import {Get} from "@tsed/schema";
107
+
108
+ @Controller("/sse")
109
+ export class MyCtrl {
110
+ private eventEmitter = new EventEmitter();
111
+
112
+ $onInit() {
113
+ setInterval(() => {
114
+ this.eventEmitter.emit("message", new Date());
115
+ }, 1000);
116
+ }
117
+
118
+ @Get("/events")
119
+ @EventStream()
120
+ events() {
121
+ return this.eventEmitter;
122
+ }
123
+ }
124
+ ```
125
+
126
+ ### Observable
127
+
128
+ You can also use `Observable` from `rxjs` to emit events from your controller to your consumer:
129
+
130
+ ```ts
131
+ import {EventStream} from "@tsed/sse";
132
+ import {Controller} from "@tsed/di";
133
+ import {Get} from "@tsed/schema";
134
+
135
+ @Controller("/sse")
136
+ export class MyCtrl {
137
+ @Get("/events")
138
+ @EventStream()
139
+ events() {
140
+ const observable = new Observable((observer) => {
141
+ setInterval(() => {
142
+ observer.next(new Date());
143
+ }, 1000);
144
+ });
145
+
146
+ return observable;
147
+ }
148
+ }
149
+ ```
150
+
151
+ ## Documentation
152
+
153
+ See our documentation https://tsed.io/#/api/index
154
+
155
+ ## Contributors
156
+
157
+ Please read [contributing guidelines here](https://tsed.io/contributing.html)
158
+
159
+ <a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
160
+
161
+ ## Backers
162
+
163
+ Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
164
+
165
+ <a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
166
+
167
+ ## Sponsors
168
+
169
+ Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
170
+
171
+ ## License
172
+
173
+ The MIT License (MIT)
174
+
175
+ Copyright (c) 2016 - today Romain Lenzotti
176
+
177
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
178
+
179
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
180
+
181
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ {
2
+ "extends": "@tsed/typescript/tsconfig.node.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "module": "ES2020",
6
+ "rootDir": "src",
7
+ "outDir": "./lib/esm",
8
+ "declaration": true,
9
+ "declarationDir": "./lib/types",
10
+ "composite": true,
11
+ "noEmit": false
12
+ },
13
+ "include": ["src", "src/**/*.json"],
14
+ "exclude": [
15
+ "node_modules",
16
+ "test",
17
+ "lib",
18
+ "benchmark",
19
+ "coverage",
20
+ "spec",
21
+ "**/*.benchmark.ts",
22
+ "**/*.spec.ts",
23
+ "keys",
24
+ "jest.config.js",
25
+ "**/__mock__/**",
26
+ "webpack.config.js"
27
+ ],
28
+ "references": [
29
+ {
30
+ "path": "../../di"
31
+ },
32
+ {
33
+ "path": "../../specs/json-mapper"
34
+ },
35
+ {
36
+ "path": "../../platform/platform-middlewares"
37
+ },
38
+ {
39
+ "path": "../../specs/schema"
40
+ },
41
+ {
42
+ "path": "../../platform/common"
43
+ },
44
+ {
45
+ "path": "../../core"
46
+ },
47
+ {
48
+ "path": "../../specs/openspec"
49
+ },
50
+ {
51
+ "path": "../socketio-testing"
52
+ }
53
+ ]
54
+ }