koatty_core
Advanced tools
Comparing version 1.2.19 to 1.2.21
@@ -9,2 +9,3 @@ /** | ||
import { Application } from "koatty_container"; | ||
import { KoattyContext } from "./Context"; | ||
/** | ||
@@ -110,3 +111,3 @@ * InitOptions | ||
options: InitOptions; | ||
context: any; | ||
context: KoattyContext; | ||
server: KoattyServer; | ||
@@ -174,6 +175,6 @@ router: KoattyRouter; | ||
* @param {string} [protocol] | ||
* @returns {*} {*} | ||
* @returns {KoattyContext} {*} | ||
* @memberof Koatty | ||
*/ | ||
createContext(req: any, res: any, protocol?: string): any; | ||
createContext(req: any, res: any, protocol?: string): KoattyContext; | ||
/** | ||
@@ -180,0 +181,0 @@ * listening and start server |
@@ -12,2 +12,3 @@ "use strict"; | ||
const koa_1 = tslib_1.__importDefault(require("koa")); | ||
const http_1 = require("http"); | ||
const Helper = tslib_1.__importStar(require("koatty_lib")); | ||
@@ -17,3 +18,2 @@ const koatty_logger_1 = require("koatty_logger"); | ||
const Context_1 = require("./Context"); | ||
const GrpcContext_1 = require("./GrpcContext"); | ||
/** | ||
@@ -151,13 +151,24 @@ * Application | ||
* @param {string} [protocol] | ||
* @returns {*} {*} | ||
* @returns {KoattyContext} {*} | ||
* @memberof Koatty | ||
*/ | ||
createContext(req, res, protocol) { | ||
if (protocol === "grpc") { | ||
this.context = GrpcContext_1.CreateGrpcContext(req, res); | ||
let context, resp; | ||
switch (protocol) { | ||
case "ws": | ||
case "wss": | ||
resp = new http_1.ServerResponse(req); | ||
context = super.createContext(req, resp); | ||
this.context = Context_1.CreateWsContext(Context_1.CreateContext(context), res); | ||
break; | ||
case "grpc": | ||
resp = new http_1.ServerResponse(req); | ||
context = super.createContext(req, resp); | ||
this.context = Context_1.CreateGrpcContext(Context_1.CreateContext(context), res); | ||
break; | ||
default: | ||
context = super.createContext(req, res); | ||
this.context = Context_1.CreateContext(context); | ||
break; | ||
} | ||
else { | ||
const context = super.createContext(req, res); | ||
this.context = Context_1.CreateContext(context); | ||
} | ||
return this.context; | ||
@@ -164,0 +175,0 @@ } |
@@ -0,3 +1,14 @@ | ||
/// <reference types="node" /> | ||
import Koa from "koa"; | ||
import { Context } from "koatty_container"; | ||
import { GrpcStatusCode, HttpStatusCode } from "koatty_exception"; | ||
import { sendUnaryData, ServerDuplexStream, ServerReadableStream, ServerUnaryCall, ServerUnaryCallImpl, ServerWritableStream } from "@grpc/grpc-js/build/src/server-call"; | ||
import { Metadata } from "@grpc/grpc-js"; | ||
export declare type IRpcServerUnaryCall<RequestType, ResponseType> = ServerUnaryCall<RequestType, ResponseType>; | ||
export declare type IRpcServerReadableStream<RequestType, ResponseType> = ServerReadableStream<RequestType, ResponseType>; | ||
export declare type IRpcServerWriteableStream<RequestType, ResponseType> = ServerWritableStream<RequestType, ResponseType>; | ||
export declare type IRpcServerDuplexStream<RequestType, ResponseType> = ServerDuplexStream<RequestType, ResponseType>; | ||
export declare type IRpcServerCall<RequestType, ResponseType> = IRpcServerUnaryCall<RequestType, ResponseType> | IRpcServerReadableStream<RequestType, ResponseType> | IRpcServerWriteableStream<RequestType, ResponseType> | IRpcServerDuplexStream<RequestType, ResponseType>; | ||
export declare type IRpcServerUnaryCallImpl<RequestType, ResponseType> = ServerUnaryCallImpl<RequestType, ResponseType>; | ||
export declare type IRpcServerCallback<ResponseType> = sendUnaryData<ResponseType>; | ||
/** | ||
@@ -22,3 +33,13 @@ * AppContext | ||
state: any; | ||
status: HttpStatusCode | GrpcStatusCode; | ||
cancelled?: boolean; | ||
metadata: Metadata; | ||
/** | ||
* websocket instance | ||
* | ||
* @type {*} | ||
* @memberof KoattyContext | ||
*/ | ||
websocket?: any; | ||
/** | ||
* Request body parser | ||
@@ -28,3 +49,3 @@ * | ||
*/ | ||
bodyParser: () => Promise<Object>; | ||
bodyParser?: () => Promise<Object>; | ||
/** | ||
@@ -35,3 +56,3 @@ * QueryString parser | ||
*/ | ||
queryParser: () => Object; | ||
queryParser?: () => Object; | ||
/** | ||
@@ -52,3 +73,3 @@ * Replace ctx.throw | ||
getMetaData: (key: string) => unknown; | ||
setMetaData: (key: string, value: unknown) => Map<string, unknown>; | ||
setMetaData: (key: string, value: string | Buffer) => any; | ||
} | ||
@@ -66,2 +87,20 @@ /** | ||
export declare function CreateContext(ctx: Koa.Context): KoattyContext; | ||
/** | ||
* Create KoattyGrpcContext | ||
* | ||
* @export | ||
* @param {IRpcServerCall<any, any>} call | ||
* @param {IRpcServerCallback<any>} [callback] | ||
* @returns {*} {KoattyGrpcContext} | ||
*/ | ||
export declare function CreateGrpcContext(ctx: KoattyContext, call: IRpcServerUnaryCallImpl<any, any>): KoattyContext; | ||
/** | ||
* Create KoattyWsContext | ||
* | ||
* @export | ||
* @param {KoattyContext} ctx | ||
* @param {*} data | ||
* @returns {*} {KoattyContext} | ||
*/ | ||
export declare function CreateWsContext(ctx: KoattyContext, data: any): KoattyContext; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CreateContext = void 0; | ||
exports.CreateWsContext = exports.CreateGrpcContext = exports.CreateContext = void 0; | ||
const koatty_exception_1 = require("koatty_exception"); | ||
/** | ||
* Create KoattyContext | ||
* | ||
* @param {Koa.Context} ctx | ||
* @returns {*} {KoattyContext} | ||
*/ | ||
function CreateContext(ctx) { | ||
function initBaseContext(ctx) { | ||
const context = Object.create(ctx); | ||
@@ -26,17 +20,65 @@ context.throw = function (statusOrMessage, codeOrMessage = 1, status) { | ||
}; | ||
Reflect.defineProperty(context, '_caches', { | ||
value: {}, | ||
writable: true, | ||
configurable: false, | ||
enumerable: false, | ||
}); | ||
context.getMetaData = function (key) { | ||
return context._caches[key]; | ||
const value = context.metadata.get(key); | ||
if (value.length === 1) { | ||
return value[0]; | ||
} | ||
return value; | ||
}; | ||
context.setMetaData = function (key, value) { | ||
context._caches[key] = value; | ||
context.metadata.set(key, value); | ||
}; | ||
return context; | ||
} | ||
/** | ||
* Create KoattyContext | ||
* | ||
* @param {Koa.Context} ctx | ||
* @returns {*} {KoattyContext} | ||
*/ | ||
function CreateContext(ctx) { | ||
return initBaseContext(ctx); | ||
} | ||
exports.CreateContext = CreateContext; | ||
/** | ||
* Create KoattyGrpcContext | ||
* | ||
* @export | ||
* @param {IRpcServerCall<any, any>} call | ||
* @param {IRpcServerCallback<any>} [callback] | ||
* @returns {*} {KoattyGrpcContext} | ||
*/ | ||
function CreateGrpcContext(ctx, call) { | ||
ctx.throw = function (statusOrMessage, codeOrMessage = 1, status) { | ||
if (typeof statusOrMessage !== "string") { | ||
if (koatty_exception_1.GrpcStatusCodeMap.has(statusOrMessage)) { | ||
status = statusOrMessage; | ||
statusOrMessage = koatty_exception_1.GrpcStatusCodeMap.get(statusOrMessage); | ||
} | ||
} | ||
if (typeof codeOrMessage === "string") { | ||
statusOrMessage = codeOrMessage; | ||
codeOrMessage = 2; | ||
} | ||
throw new koatty_exception_1.Exception(statusOrMessage, codeOrMessage, status); | ||
}; | ||
ctx.body = call.request; | ||
ctx.setMetaData("_body", call.request); | ||
return ctx; | ||
} | ||
exports.CreateGrpcContext = CreateGrpcContext; | ||
/** | ||
* Create KoattyWsContext | ||
* | ||
* @export | ||
* @param {KoattyContext} ctx | ||
* @param {*} data | ||
* @returns {*} {KoattyContext} | ||
*/ | ||
function CreateWsContext(ctx, data) { | ||
ctx.body = data; | ||
ctx.setMetaData("_body", data); | ||
return ctx; | ||
} | ||
exports.CreateWsContext = CreateWsContext; | ||
//# sourceMappingURL=Context.js.map |
export * from "./Application"; | ||
export * from "./Context"; | ||
export * from "./GrpcContext"; |
@@ -9,7 +9,6 @@ "use strict"; | ||
* @Date: 2021-07-09 18:49:56 | ||
* @LastEditTime: 2021-11-16 20:56:07 | ||
* @LastEditTime: 2021-11-17 23:50:56 | ||
*/ | ||
tslib_1.__exportStar(require("./Application"), exports); | ||
tslib_1.__exportStar(require("./Context"), exports); | ||
tslib_1.__exportStar(require("./GrpcContext"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "koatty_core", | ||
"version": "1.2.19", | ||
"version": "1.2.21", | ||
"description": "Koatty routing component, adapt to http1/2, websocket, gRPC.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
2096053
21
836
1