@silenteer/natsu-port-server
Advanced tools
Comparing version 0.0.38 to 0.0.39
@@ -1,2 +0,2 @@ | ||
declare type Config = { | ||
type Config = { | ||
logLevels: Array<'all' | 'none' | 'error' | 'info' | 'log'>; | ||
@@ -3,0 +3,0 @@ natsURI: string; |
@@ -5,4 +5,4 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const yup = (0, tslib_1.__importStar)(require("yup")); | ||
const dotenv_1 = (0, tslib_1.__importDefault)(require("dotenv")); | ||
const yup = tslib_1.__importStar(require("yup")); | ||
const dotenv_1 = tslib_1.__importDefault(require("dotenv")); | ||
dotenv_1.default.config(); | ||
@@ -9,0 +9,0 @@ const schema = yup.object({ |
@@ -5,9 +5,12 @@ /// <reference types="node" /> | ||
import type { NatsPortResponse, NatsPortErrorResponse } from '@silenteer/natsu-type'; | ||
export declare type CustomFastifyRequest = FastifyRequest<any> & { | ||
export type CustomFastifyRequest = FastifyRequest<any> & { | ||
parts: () => any; | ||
file?: Buffer; | ||
}; | ||
export declare type OnBeforeSendNatsRequest = (request: CustomFastifyRequest) => Promise<void>; | ||
export declare type OnAfterSendNatsRequest = (request: CustomFastifyRequest, response: NatsPortResponse<unknown> | NatsPortErrorResponse) => Promise<void>; | ||
export type OnBeforeSendNatsRequest = (request: CustomFastifyRequest) => Promise<void>; | ||
export type OnAfterSendNatsRequest = (request: CustomFastifyRequest, response: NatsPortResponse<unknown> | NatsPortErrorResponse) => Promise<void>; | ||
declare function start(options?: { | ||
onRequest?: (request: CustomFastifyRequest) => Promise<void>; | ||
onResponseSuccess?: (request: CustomFastifyRequest, response: NatsPortResponse<unknown> | NatsPortErrorResponse) => Promise<void>; | ||
onResponseError?: (request: CustomFastifyRequest, error: Error) => Promise<void>; | ||
onBeforeSendNatsRequest?: OnBeforeSendNatsRequest; | ||
@@ -14,0 +17,0 @@ onAfterSendNatsRequest?: OnAfterSendNatsRequest; |
@@ -5,12 +5,12 @@ "use strict"; | ||
const crypto_1 = require("crypto"); | ||
const yup = (0, tslib_1.__importStar)(require("yup")); | ||
const yup = tslib_1.__importStar(require("yup")); | ||
const nats_1 = require("nats"); | ||
const fastify_1 = (0, tslib_1.__importDefault)(require("fastify")); | ||
const fastify_cors_1 = (0, tslib_1.__importDefault)(require("fastify-cors")); | ||
const multipart_1 = (0, tslib_1.__importDefault)(require("@fastify/multipart")); | ||
const fastify_websocket_1 = (0, tslib_1.__importDefault)(require("fastify-websocket")); | ||
const fastify_1 = tslib_1.__importDefault(require("fastify")); | ||
const fastify_cors_1 = tslib_1.__importDefault(require("fastify-cors")); | ||
const multipart_1 = tslib_1.__importDefault(require("@fastify/multipart")); | ||
const fastify_websocket_1 = tslib_1.__importDefault(require("fastify-websocket")); | ||
require("colors"); | ||
const configuration_1 = (0, tslib_1.__importDefault)(require("./configuration")); | ||
const logger_1 = (0, tslib_1.__importDefault)(require("./logger")); | ||
const service_nats_1 = (0, tslib_1.__importDefault)(require("./service-nats")); | ||
const configuration_1 = tslib_1.__importDefault(require("./configuration")); | ||
const logger_1 = tslib_1.__importDefault(require("./logger")); | ||
const service_nats_1 = tslib_1.__importDefault(require("./service-nats")); | ||
const httpRequestSchema = yup.object({ | ||
@@ -42,5 +42,8 @@ subject: yup.string().trim().required(), | ||
.register(fastify_websocket_1.default) | ||
.post(configuration_1.default.httpPath, (request, reply) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
.post(configuration_1.default.httpPath, (request, reply) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
var _a, _b; | ||
const subject = request.headers['nats-subject']; | ||
if (options === null || options === void 0 ? void 0 : options.onRequest) { | ||
yield options.onRequest(request); | ||
} | ||
try { | ||
@@ -88,2 +91,5 @@ reply.header('nats-subject', subject); | ||
} | ||
if (options === null || options === void 0 ? void 0 : options.onResponseSuccess) { | ||
yield options.onResponseSuccess(request, response); | ||
} | ||
reply.send(response); | ||
@@ -93,2 +99,5 @@ } | ||
logger_1.default.error(subject, error); | ||
if (options === null || options === void 0 ? void 0 : options.onResponseError) { | ||
yield options.onResponseError(request, error); | ||
} | ||
if (error.code) { | ||
@@ -107,3 +116,3 @@ reply.send(error); | ||
}); | ||
connection.socket.on('message', (message) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
connection.socket.on('message', (message) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let wsRequest; | ||
@@ -197,3 +206,3 @@ try { | ||
var _a, _b; | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let result; | ||
@@ -227,3 +236,3 @@ const subject = request.headers['nats-subject']; | ||
var _a, _b; | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const { httpRequest, natsAuthResponse } = params; | ||
@@ -260,3 +269,3 @@ const subject = httpRequest.headers['nats-subject']; | ||
function sendNatsAuthRequest(request) { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let natsResponse; | ||
@@ -283,3 +292,3 @@ for (const subject of configuration_1.default.natsAuthSubjects) { | ||
var _a; | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const { httpRequest, natsAuthResponse } = params; | ||
@@ -286,0 +295,0 @@ const natsRequest = { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const configuration_1 = (0, tslib_1.__importDefault)(require("./configuration")); | ||
const configuration_1 = tslib_1.__importDefault(require("./configuration")); | ||
let logLevels; | ||
@@ -6,0 +6,0 @@ if (configuration_1.default.logLevels.includes('none')) { |
@@ -5,3 +5,3 @@ "use strict"; | ||
const nats_1 = require("nats"); | ||
const configuration_1 = (0, tslib_1.__importDefault)(require("./configuration")); | ||
const configuration_1 = tslib_1.__importDefault(require("./configuration")); | ||
class Queue { | ||
@@ -33,3 +33,3 @@ constructor(_onProcess) { | ||
function getConnection() { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (!natsConnection) { | ||
@@ -49,3 +49,3 @@ natsConnection = yield (0, nats_1.connect)({ | ||
function request(params) { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const { subject, data, options } = params; | ||
@@ -58,3 +58,3 @@ const connection = yield getConnection(); | ||
var _a, _b, _c; | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const { connectionId, subject, namespace, onHandle } = params; | ||
@@ -79,16 +79,29 @@ const _subject = namespace ? `${subject}.${namespace}` : subject; | ||
const codec = (0, nats_1.JSONCodec)(); | ||
(() => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
var e_1, _d; | ||
var _e, _f; | ||
(() => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
var _d, e_1, _e, _f; | ||
var _g, _h; | ||
try { | ||
for (var _g = (0, tslib_1.__asyncValues)(subscriptions[_subject].subscription), _h; _h = yield _g.next(), !_h.done;) { | ||
const message = _h.value; | ||
for (var _j = true, _k = tslib_1.__asyncValues(subscriptions[_subject].subscription), _l; _l = yield _k.next(), _d = _l.done, !_d;) { | ||
_f = _l.value; | ||
_j = false; | ||
try { | ||
const data = message.data ? codec.decode(message.data) : undefined; | ||
if (data) { | ||
subscriptions[_subject].connections.forEach(({ onHandle }) => { | ||
const message = _f; | ||
try { | ||
const data = message.data ? codec.decode(message.data) : undefined; | ||
if (data) { | ||
subscriptions[_subject].connections.forEach(({ onHandle }) => { | ||
onHandle({ | ||
subject, | ||
code: data.code, | ||
body: data.body, | ||
}); | ||
}); | ||
} | ||
} | ||
catch (error) { | ||
console.error(error); | ||
(_h = (_g = subscriptions[_subject]) === null || _g === void 0 ? void 0 : _g.connections) === null || _h === void 0 ? void 0 : _h.forEach(({ onHandle }) => { | ||
onHandle({ | ||
subject, | ||
code: data.code, | ||
body: data.body, | ||
code: 500, | ||
}); | ||
@@ -98,10 +111,4 @@ }); | ||
} | ||
catch (error) { | ||
console.error(error); | ||
(_f = (_e = subscriptions[_subject]) === null || _e === void 0 ? void 0 : _e.connections) === null || _f === void 0 ? void 0 : _f.forEach(({ onHandle }) => { | ||
onHandle({ | ||
subject, | ||
code: 500, | ||
}); | ||
}); | ||
finally { | ||
_j = true; | ||
} | ||
@@ -113,3 +120,3 @@ } | ||
try { | ||
if (_h && !_h.done && (_d = _g.return)) yield _d.call(_g); | ||
if (!_j && !_d && (_e = _k.return)) yield _e.call(_k); | ||
} | ||
@@ -122,3 +129,3 @@ finally { if (e_1) throw e_1.error; } | ||
function unsubscribe(params) { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const { connectionId, subject, namespace } = params; | ||
@@ -125,0 +132,0 @@ const _subject = namespace ? `${subject}.${namespace}` : subject; |
{ | ||
"name": "@silenteer/natsu-port-server", | ||
"version": "0.0.38", | ||
"version": "0.0.39", | ||
"license": "MIT", | ||
@@ -41,4 +41,4 @@ "private": false, | ||
"tslib": "2.3.1", | ||
"typescript": "4.4.3" | ||
"typescript": "4.9.5" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51873
791