MCPcopy Create free account
hub / github.com/avoidwork/tenso / init

Method init

src/tenso.js:161–238  ·  view source on GitHub ↗

* Initializes the Tenso server with middleware, routes, and configuration * @returns {Tenso} The Tenso instance for method chaining

()

Source from the content-addressed store, hash-verified

159 * @returns {Tenso} The Tenso instance for method chaining
160 */
161 init () {
162 const authorization = Object.keys(this.auth).filter(i => this.auth?.[i]?.enabled === true).length > INT_0 || this.rate.enabled || this.security.csrf;
163
164 // Matching MaxListeners for signals
165 this.setMaxListeners(this.maxListeners);
166 process.setMaxListeners(this.maxListeners);
167
168 this.decorate = this.decorate.bind(this);
169 this.route = this.route.bind(this);
170 this.render = this.render.bind(this);
171 this.signals();
172 this.addListener(CONNECT, this.connect.bind(this));
173 this.onSend = (req, res, body = EMPTY, status = INT_200, headers) => {
174 this.headers(req, res);
175 res.statusCode = status;
176
177 if (status !== INT_204 && status !== INT_304 && (body === null || typeof body.on !== FUNCTION)) {
178 for (const fn of [serialize, hypermedia, this.final, this.render]) {
179 body = fn(req, res, body);
180 }
181 }
182
183 return [body, status, headers];
184 };
185
186 // Prometheus metrics
187 if (this.prometheus.enabled) {
188 const {middleware, register} = prometheus(this.prometheus.metrics);
189
190 this.log(`type=init, message"${MSG_PROMETHEUS_ENABLED}"`);
191 this.always(middleware).ignore(middleware);
192
193 // Registering a route for metrics endpoint
194 this.get(METRICS_PATH, (req, res) => {
195 res.header(HEADER_CONTENT_TYPE, register.contentType);
196 register.metrics().then(result => res.end(result)).catch(err => {
197 res.statusCode = INT_500;
198 res.end(`Error collecting metrics: ${err.message}`);
199 });
200 });
201 }
202
203 // Early exit after prometheus metrics (for GETs only)
204 this.always(exit).ignore(exit);
205
206 // Payload handling
207 this.always(payload).ignore(payload);
208 this.always(parse).ignore(parse);
209
210 // Setting 'always' routes before authorization runs
211 for (const [key, value] of Object.entries(this.initRoutes.always ?? {})) {
212 if (typeof value === FUNCTION) {
213 this.always(key, value).ignore(value);
214 }
215 }
216
217 delete this.initRoutes.always;
218

Callers 1

tensoFunction · 0.95

Calls 7

signalsMethod · 0.95
headersMethod · 0.95
prometheusFunction · 0.90
authFunction · 0.90
fnFunction · 0.85
logMethod · 0.80
headerMethod · 0.80

Tested by

no test coverage detected