Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use base node14 tsconfig #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SimenB
Copy link
Contributor

@SimenB SimenB commented Feb 16, 2023

Since this module only supports node 14 and up we can update its target config. E.g. stop transpiling async functions and generators.

This is the diff:

diff --git i/lib/index.js w/lib/index.js
index 11bde1c..3b99547 100644
--- i/lib/index.js
+++ w/lib/index.js
@@ -1,10 +1,13 @@
 "use strict";
-exports.__esModule = true;
+var __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+Object.defineProperty(exports, "__esModule", { value: true });
 exports.FastifySSEPlugin = void 0;
-var fastify_plugin_1 = require("fastify-plugin");
-var plugin_1 = require("./plugin");
-exports.FastifySSEPlugin = (0, fastify_plugin_1["default"])(plugin_1.plugin, {
+const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
+const plugin_1 = require("./plugin");
+exports.FastifySSEPlugin = (0, fastify_plugin_1.default)(plugin_1.plugin, {
     name: "fastify-sse-v2",
-    fastify: ">=3"
+    fastify: ">=3",
 });
-exports["default"] = exports.FastifySSEPlugin;
+exports.default = exports.FastifySSEPlugin;
diff --git i/lib/plugin.js w/lib/plugin.js
index b0bce04..6579342 100644
--- i/lib/plugin.js
+++ w/lib/plugin.js
@@ -1,79 +1,39 @@
 "use strict";
-var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
-    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
-    return new (P || (P = Promise))(function (resolve, reject) {
-        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
-        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
-        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
-        step((generator = generator.apply(thisArg, _arguments || [])).next());
-    });
-};
-var __generator = (this && this.__generator) || function (thisArg, body) {
-    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
-    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
-    function verb(n) { return function (v) { return step([n, v]); }; }
-    function step(op) {
-        if (f) throw new TypeError("Generator is already executing.");
-        while (_) try {
-            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
-            if (y = 0, t) op = [op[0] & 2, t.value];
-            switch (op[0]) {
-                case 0: case 1: t = op; break;
-                case 4: _.label++; return { value: op[1], done: false };
-                case 5: _.label++; y = op[1]; op = [0]; continue;
-                case 7: op = _.ops.pop(); _.trys.pop(); continue;
-                default:
-                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
-                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
-                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
-                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
-                    if (t[2]) _.ops.pop();
-                    _.trys.pop(); continue;
-            }
-            op = body.call(thisArg, _);
-        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
-        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
-    }
+var __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
 };
-exports.__esModule = true;
+Object.defineProperty(exports, "__esModule", { value: true });
 exports.plugin = void 0;
-var it_to_stream_1 = require("it-to-stream");
-var it_pushable_1 = require("it-pushable");
-var sse_1 = require("./sse");
-var util_1 = require("./util");
+const it_to_stream_1 = __importDefault(require("it-to-stream"));
+const it_pushable_1 = __importDefault(require("it-pushable"));
+const sse_1 = require("./sse");
+const util_1 = require("./util");
 // eslint-disable-next-line @typescript-eslint/require-await
-var plugin = function (instance, options) {
-    return __awaiter(this, void 0, void 0, function () {
-        return __generator(this, function (_a) {
-            instance.decorateReply("sse", function (source) {
-                var _this = this;
-                //if this already set, it's not first event
-                if (!this.raw.headersSent) {
-                    this.sseContext = { source: (0, it_pushable_1["default"])() };
-                    Object.entries(this.getHeaders()).forEach(function (_a) {
-                        var key = _a[0], value = _a[1];
-                        _this.raw.setHeader(key, value !== null && value !== void 0 ? value : "");
-                    });
-                    this.raw.setHeader("Content-Type", "text/event-stream");
-                    this.raw.setHeader("Connection", "keep-alive");
-                    this.raw.setHeader("Cache-Control", "no-cache,no-transform");
-                    this.raw.setHeader("x-no-compression", 1);
-                    this.raw.write((0, sse_1.serializeSSEEvent)({ retry: options.retryDelay || 3000 }));
-                    handleAsyncIterable(this, this.sseContext.source);
-                }
-                if ((0, util_1.isAsyncIterable)(source)) {
-                    return handleAsyncIterable(this, source);
-                }
-                else {
-                    this.sseContext.source.push(source);
-                    return;
-                }
+const plugin = async function (instance, options) {
+    instance.decorateReply("sse", function (source) {
+        //if this already set, it's not first event
+        if (!this.raw.headersSent) {
+            this.sseContext = { source: (0, it_pushable_1.default)() };
+            Object.entries(this.getHeaders()).forEach(([key, value]) => {
+                this.raw.setHeader(key, value ?? "");
             });
-            return [2 /*return*/];
-        });
+            this.raw.setHeader("Content-Type", "text/event-stream");
+            this.raw.setHeader("Connection", "keep-alive");
+            this.raw.setHeader("Cache-Control", "no-cache,no-transform");
+            this.raw.setHeader("x-no-compression", 1);
+            this.raw.write((0, sse_1.serializeSSEEvent)({ retry: options.retryDelay || 3000 }));
+            handleAsyncIterable(this, this.sseContext.source);
+        }
+        if ((0, util_1.isAsyncIterable)(source)) {
+            return handleAsyncIterable(this, source);
+        }
+        else {
+            this.sseContext.source.push(source);
+            return;
+        }
     });
 };
 exports.plugin = plugin;
 function handleAsyncIterable(reply, source) {
-    (0, it_to_stream_1["default"])((0, sse_1.transformAsyncIterable)(source)).pipe(reply.raw);
+    (0, it_to_stream_1.default)((0, sse_1.transformAsyncIterable)(source)).pipe(reply.raw);
 }
diff --git i/lib/sse.js w/lib/sse.js
index e9b18e8..de0cbcc 100644
--- i/lib/sse.js
+++ w/lib/sse.js
@@ -1,112 +1,26 @@
 "use strict";
-var __generator = (this && this.__generator) || function (thisArg, body) {
-    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
-    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
-    function verb(n) { return function (v) { return step([n, v]); }; }
-    function step(op) {
-        if (f) throw new TypeError("Generator is already executing.");
-        while (_) try {
-            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
-            if (y = 0, t) op = [op[0] & 2, t.value];
-            switch (op[0]) {
-                case 0: case 1: t = op; break;
-                case 4: _.label++; return { value: op[1], done: false };
-                case 5: _.label++; y = op[1]; op = [0]; continue;
-                case 7: op = _.ops.pop(); _.trys.pop(); continue;
-                default:
-                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
-                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
-                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
-                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
-                    if (t[2]) _.ops.pop();
-                    _.trys.pop(); continue;
-            }
-            op = body.call(thisArg, _);
-        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
-        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
-    }
-};
-var __asyncValues = (this && this.__asyncValues) || function (o) {
-    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
-    var m = o[Symbol.asyncIterator], i;
-    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
-    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
-    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
-};
-var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
-var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
-    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
-    var g = generator.apply(thisArg, _arguments || []), i, q = [];
-    return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
-    function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
-    function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
-    function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
-    function fulfill(value) { resume("next", value); }
-    function reject(value) { resume("throw", value); }
-    function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
-};
-exports.__esModule = true;
+Object.defineProperty(exports, "__esModule", { value: true });
 exports.serializeSSEEvent = exports.transformAsyncIterable = void 0;
-function transformAsyncIterable(source) {
-    return __asyncGenerator(this, arguments, function transformAsyncIterable_1() {
-        var source_1, source_1_1, message, e_1_1;
-        var e_1, _a;
-        return __generator(this, function (_b) {
-            switch (_b.label) {
-                case 0:
-                    _b.trys.push([0, 7, 8, 13]);
-                    source_1 = __asyncValues(source);
-                    _b.label = 1;
-                case 1: return [4 /*yield*/, __await(source_1.next())];
-                case 2:
-                    if (!(source_1_1 = _b.sent(), !source_1_1.done)) return [3 /*break*/, 6];
-                    message = source_1_1.value;
-                    return [4 /*yield*/, __await(serializeSSEEvent(message))];
-                case 3: return [4 /*yield*/, _b.sent()];
-                case 4:
-                    _b.sent();
-                    _b.label = 5;
-                case 5: return [3 /*break*/, 1];
-                case 6: return [3 /*break*/, 13];
-                case 7:
-                    e_1_1 = _b.sent();
-                    e_1 = { error: e_1_1 };
-                    return [3 /*break*/, 13];
-                case 8:
-                    _b.trys.push([8, , 11, 12]);
-                    if (!(source_1_1 && !source_1_1.done && (_a = source_1["return"]))) return [3 /*break*/, 10];
-                    return [4 /*yield*/, __await(_a.call(source_1))];
-                case 9:
-                    _b.sent();
-                    _b.label = 10;
-                case 10: return [3 /*break*/, 12];
-                case 11:
-                    if (e_1) throw e_1.error;
-                    return [7 /*endfinally*/];
-                case 12: return [7 /*endfinally*/];
-                case 13: return [4 /*yield*/, __await(serializeSSEEvent({ event: "end", data: "Stream closed" }))];
-                case 14: return [4 /*yield*/, _b.sent()];
-                case 15:
-                    _b.sent();
-                    return [2 /*return*/];
-            }
-        });
-    });
+async function* transformAsyncIterable(source) {
+    for await (const message of source) {
+        yield serializeSSEEvent(message);
+    }
+    yield serializeSSEEvent({ event: "end", data: "Stream closed" });
 }
 exports.transformAsyncIterable = transformAsyncIterable;
 function serializeSSEEvent(chunk) {
-    var payload = "";
+    let payload = "";
     if (chunk.id) {
-        payload += "id: ".concat(chunk.id, "\n");
+        payload += `id: ${chunk.id}\n`;
     }
     if (chunk.event) {
-        payload += "event: ".concat(chunk.event, "\n");
+        payload += `event: ${chunk.event}\n`;
     }
     if (chunk.data) {
-        payload += "data: ".concat(chunk.data, "\n");
+        payload += `data: ${chunk.data}\n`;
     }
     if (chunk.retry) {
-        payload += "retry: ".concat(chunk.retry, "\n");
+        payload += `retry: ${chunk.retry}\n`;
     }
     if (!payload) {
         return "";
diff --git i/lib/types.js w/lib/types.js
index 0e34578..c8ad2e5 100644
--- i/lib/types.js
+++ w/lib/types.js
@@ -1,2 +1,2 @@
 "use strict";
-exports.__esModule = true;
+Object.defineProperty(exports, "__esModule", { value: true });
diff --git i/lib/util.js w/lib/util.js
index df6ee4d..34baadd 100644
--- i/lib/util.js
+++ w/lib/util.js
@@ -1,5 +1,5 @@
 "use strict";
-exports.__esModule = true;
+Object.defineProperty(exports, "__esModule", { value: true });
 exports.isAsyncIterable = void 0;
 function isAsyncIterable(source) {
     if (source === null || source === undefined || typeof source !== "object") {

"compilerOptions": {
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the removed lines are defaults in @tsconfig/node14

@mpetrunic
Copy link
Owner

@SimenB Thanks for PR, since node14 is no longer maintained, could you update this to node16?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants