@roadwork/dapr-js-sdk
Advanced tools
Comparing version 0.1.27 to 0.2.0
@@ -1,6 +0,6 @@ | ||
import Dapr from './Dapr'; | ||
import DaprClient from './DaprClient'; | ||
import DaprServer from './DaprServer'; | ||
import { Request as Req, Response as Res } from 'restana'; | ||
import { HttpMethod } from './enum/HttpMethod.enum'; | ||
import HttpStatusCode from './enum/HttpStatusCode.enum'; | ||
export default Dapr; | ||
export { HttpMethod, HttpStatusCode, Req, Res }; | ||
export { HttpMethod, HttpStatusCode, Req, Res, DaprClient, DaprServer }; |
@@ -6,4 +6,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HttpStatusCode = exports.HttpMethod = void 0; | ||
var Dapr_1 = __importDefault(require("./Dapr")); | ||
exports.DaprServer = exports.DaprClient = exports.HttpStatusCode = exports.HttpMethod = void 0; | ||
var DaprClient_1 = __importDefault(require("./DaprClient")); | ||
exports.DaprClient = DaprClient_1.default; | ||
var DaprServer_1 = __importDefault(require("./DaprServer")); | ||
exports.DaprServer = DaprServer_1.default; | ||
var HttpMethod_enum_1 = require("./enum/HttpMethod.enum"); | ||
@@ -13,2 +16,1 @@ Object.defineProperty(exports, "HttpMethod", { enumerable: true, get: function () { return HttpMethod_enum_1.HttpMethod; } }); | ||
exports.HttpStatusCode = HttpStatusCode_enum_1.default; | ||
exports.default = Dapr_1.default; |
/// <reference types="node" /> | ||
import http from "http"; | ||
import Restana from "restana"; | ||
import WebServerImpl from "./WebServerImpl"; | ||
export interface IServerImplType extends WebServerImpl { | ||
} | ||
export interface IServerType extends Restana.Service<Restana.Protocol.HTTP> { | ||
@@ -11,11 +14,14 @@ } | ||
export default class WebServer { | ||
serverHost: string; | ||
serverPort: string; | ||
isInitialized: boolean; | ||
server: IServerType; | ||
serverAddress: string; | ||
serverImpl: IServerImplType; | ||
constructor(); | ||
getServerAddress(): Promise<string>; | ||
getServer(): Promise<IServerType>; | ||
getServerAddress(): string; | ||
getServer(): IServerType; | ||
getServerImpl(): IServerImplType; | ||
close(): Promise<void>; | ||
initialize(): Promise<void>; | ||
private initializeServer; | ||
startServer(host: string, port: string): Promise<void>; | ||
} |
@@ -44,47 +44,39 @@ "use strict"; | ||
var body_parser_1 = __importDefault(require("body-parser")); | ||
var WebServerImpl_1 = __importDefault(require("./WebServerImpl")); | ||
; | ||
; | ||
; | ||
; | ||
var WebServer = /** @class */ (function () { | ||
function WebServer() { | ||
this.serverHost = ""; | ||
this.serverPort = ""; | ||
this.isInitialized = false; | ||
this.server = restana_1.default(); | ||
this.server.use(body_parser_1.default.json()); | ||
this.serverImpl = new WebServerImpl_1.default(); | ||
this.serverAddress = ""; | ||
} | ||
WebServer.prototype.getServerAddress = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!!this.isInitialized) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.initialize()]; | ||
case 1: | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: return [2 /*return*/, this.serverAddress]; | ||
} | ||
}); | ||
}); | ||
if (!this.isInitialized) { | ||
throw new Error(JSON.stringify({ | ||
error: "HTTP_SERVER_NOT_INITIALIZED", | ||
error_message: "The HTTP server was not initialized, did you call `await HTTPServerSingleton.initialize()`?" | ||
})); | ||
} | ||
return this.serverAddress; | ||
}; | ||
WebServer.prototype.getServer = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!!this.isInitialized) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.initialize()]; | ||
case 1: | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: | ||
// throw new Error(JSON.stringify({ | ||
// code: 'WEB_SERVER_NOT_INITIALIZED', | ||
// message: "The WebServerSingleton instance was not initialized, did you run `await myClass.getInstance().initialize()`?" | ||
// })); | ||
return [2 /*return*/, this.server]; | ||
} | ||
}); | ||
}); | ||
// if (!this.isInitialized) { | ||
// throw new Error(JSON.stringify({ | ||
// error: "HTTP_SERVER_NOT_INITIALIZED", | ||
// error_message: "The HTTP server was not initialized, did you call `await HTTPServerSingleton.initialize()`?" | ||
// })); | ||
// } | ||
return this.server; | ||
}; | ||
// We allow this, since this will register the routes and handlers! | ||
WebServer.prototype.getServerImpl = function () { | ||
return this.serverImpl; | ||
}; | ||
WebServer.prototype.close = function () { | ||
@@ -94,3 +86,10 @@ return __awaiter(this, void 0, void 0, function () { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.server.close()]; | ||
case 0: | ||
if (!this.isInitialized) { | ||
throw new Error(JSON.stringify({ | ||
error: "HTTP_SERVER_NOT_INITIALIZED", | ||
error_message: "The HTTP server was not initialized, did you call `await HTTPServerSingleton.initialize()`?" | ||
})); | ||
} | ||
return [4 /*yield*/, this.server.close()]; | ||
case 1: | ||
@@ -105,13 +104,22 @@ _a.sent(); | ||
}; | ||
WebServer.prototype.initialize = function () { | ||
WebServer.prototype.startServer = function (host, port) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var randomPort, appPort, delayMs; | ||
var delayMs; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
randomPort = Math.floor(Math.random() * (20000 - 10000 + 1)) + 10000; | ||
appPort = parseInt(process.env.DAPR_INTERNAL_SERVER_PORT || "", 10) || randomPort; | ||
return [4 /*yield*/, this.initializeServer(appPort)]; | ||
case 0: | ||
// Initialize Server Listener | ||
return [4 /*yield*/, this.server.start(parseInt(port, 10))]; | ||
case 1: | ||
// Initialize Server Listener | ||
_a.sent(); | ||
console.log("[Dapr-JS] Listening on " + port); | ||
this.serverAddress = "http://127.0.0.1:" + port; | ||
// Add PubSub Routes | ||
console.log("[Dapr API][PubSub] Registering " + this.serverImpl.pubSubSubscriptionRoutes.length + " PubSub Subscriptions"); | ||
this.server.get('/dapr/subscribe', function (req, res) { | ||
res.send(_this.serverImpl.pubSubSubscriptionRoutes); | ||
console.log("[Dapr API][PubSub] Registered " + _this.serverImpl.pubSubSubscriptionRoutes.length + " PubSub Subscriptions"); | ||
}); | ||
// We need to call the Singleton to start listening on the port, else Dapr will not pick it up correctly | ||
@@ -131,18 +139,4 @@ // Dapr will probe every 50ms to see if we are listening on our port: https://github.com/dapr/dapr/blob/a43712c97ead550ca2f733e9f7e7769ecb195d8b/pkg/runtime/runtime.go#L1694 | ||
}; | ||
WebServer.prototype.initializeServer = function (port) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.server.start(port)]; | ||
case 1: | ||
_a.sent(); | ||
console.log("[Dapr-JS] Listening on " + port); | ||
this.serverAddress = "http://127.0.0.1:" + port; | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return WebServer; | ||
}()); | ||
exports.default = WebServer; |
@@ -1,2 +0,2 @@ | ||
import WebServer, { IServerType } from "./WebServer"; | ||
import WebServer, { IServerImplType, IServerType } from "./WebServer"; | ||
export default class WebServerSingleton { | ||
@@ -14,6 +14,8 @@ private static instance?; | ||
*/ | ||
static getInstance(): Promise<WebServer>; | ||
static getInstance(): WebServer; | ||
static getServer(): Promise<IServerType>; | ||
static getServerImpl(): IServerImplType; | ||
static getServerAddress(): Promise<string>; | ||
static destroy(): Promise<void>; | ||
static startServer(host: string, port: string): Promise<void>; | ||
} |
@@ -56,19 +56,7 @@ "use strict"; | ||
WebServerSingleton.getInstance = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!this.instance) { | ||
this.instance = new WebServer_1.default(); | ||
console.log("[Dapr-JS] Created WebServerSingleton"); | ||
} | ||
if (!!this.instance.isInitialized) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.instance.initialize()]; | ||
case 1: | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: return [2 /*return*/, this.instance]; | ||
} | ||
}); | ||
}); | ||
if (!this.instance) { | ||
this.instance = new WebServer_1.default(); | ||
console.log("[Dapr-JS] Created WebServerSingleton"); | ||
} | ||
return this.instance; | ||
}; | ||
@@ -88,2 +76,6 @@ WebServerSingleton.getServer = function () { | ||
}; | ||
WebServerSingleton.getServerImpl = function () { | ||
var server = this.getInstance(); | ||
return server.serverImpl; | ||
}; | ||
WebServerSingleton.getServerAddress = function () { | ||
@@ -124,4 +116,21 @@ return __awaiter(this, void 0, void 0, function () { | ||
}; | ||
WebServerSingleton.startServer = function (host, port) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var instance; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
instance = this.getInstance(); | ||
return [4 /*yield*/, instance.startServer(host, port)]; | ||
case 1: | ||
_a.sent(); | ||
this.instance = instance; | ||
console.log("[Dapr-JS][HTTP] Created Web Server Singleton"); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return WebServerSingleton; | ||
}()); | ||
exports.default = WebServerSingleton; |
@@ -1,2 +0,9 @@ | ||
import { IRequest, IResponse } from "../lib/WebServer"; | ||
export declare type TypeDaprInvokerCallback = (req: IRequest, res: IResponse) => Promise<any | void>; | ||
export interface DaprInvokerCallbackContentMetadata { | ||
contentType?: string; | ||
} | ||
export interface DaprInvokerCallbackContent { | ||
body?: string; | ||
query?: string; | ||
metadata?: DaprInvokerCallbackContentMetadata; | ||
} | ||
export declare type TypeDaprInvokerCallback = (data: DaprInvokerCallbackContent) => Promise<any | void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
; |
{ | ||
"name": "@roadwork/dapr-js-sdk", | ||
"version": "0.1.27", | ||
"version": "0.2.0", | ||
"description": "An unofficial Dapr (https://dapr.io) SDK for Node.js", | ||
@@ -9,3 +9,4 @@ "types": "http/index.d.ts", | ||
"lint": "tslint -p tsconfig.json", | ||
"build": "npm run lint && tsc --outDir ./build/" | ||
"build": "npm run lint && tsc --outDir ./build/", | ||
"start:dev": "npm run build && nodemon --ext \".ts,.js\" --watch \"./src\" --exec \"npm run build\"" | ||
}, | ||
@@ -16,3 +17,5 @@ "keywords": [], | ||
"dependencies": { | ||
"@grpc/grpc-js": "^1.3.2", | ||
"body-parser": "^1.19.0", | ||
"google-protobuf": "^3.15.8", | ||
"node-fetch": "^2.6.1", | ||
@@ -23,6 +26,10 @@ "restana": "^4.9.1" | ||
"@types/body-parser": "^1.19.0", | ||
"@types/google-protobuf": "^3.15.2", | ||
"@types/jest": "^26.0.21", | ||
"@types/node": "^15.0.3", | ||
"@types/node-fetch": "^2.5.8", | ||
"grpc_tools_node_protoc_ts": "^5.2.2", | ||
"grpc-tools": "^1.11.1", | ||
"jest": "^26.6.3", | ||
"nodemon": "^2.0.7", | ||
"prettier": "^2.2.1", | ||
@@ -29,0 +36,0 @@ "ts-jest": "^26.5.4", |
@@ -7,2 +7,23 @@ # Dapr Node.js SDK | ||
## Examples | ||
For an example of the library, see the [Examples folder](/examples) | ||
## Usage - Simple PubSub Listener | ||
To create a simply pub sub listener, we can now execute the following: | ||
```javascript | ||
import { DaprClient, DaprServer } from "@roadwork/dapr-js-sdk/grpc"; | ||
// Subscribe | ||
const server = new DaprServer(daprHost, daprPort, daprInternalServerPort); | ||
server.pubsub.subscribe("pubsub-name", "topic", async (data: any) => console.log(data)) | ||
await server.startServer(); | ||
// Publish | ||
const client = new DaprClient(daprHost, daprPort); | ||
client.pubsub.publish("pubsub-name", "topic", { hello: "world" }); | ||
``` | ||
## Usage | ||
@@ -21,3 +42,5 @@ | ||
```javascript | ||
import Dapr, { HttpMethod, HttpStatusCode, Req, Res } from "@roadwork/dapr-js-sdk/http"; | ||
import { DaprClient, DaprServer } from "@roadwork/dapr-js-sdk/http"; | ||
// OR (depending on the protocol) | ||
import { DaprClient, DaprServer } from "@roadwork/dapr-js-sdk/grpc"; | ||
@@ -37,8 +60,14 @@ // Dapr ConnectionInfo | ||
const client = new Dapr(daprHost, daprPort, daprInternalServerPort); | ||
const client = new DaprClient(daprHost, daprPort); | ||
const server = new DaprServer(daprHost, daprPort, daprInternalServerPort); | ||
// NOTE: This has to be executed AFTER all server calls are done. | ||
// This is due to Dapr requiring that routes are registered before the server is started | ||
// See the Usage - Simple PubSub Listener for more information | ||
await server.startServer(); // start the internal server | ||
// Pub / Sub | ||
// Note: /dapr/subscribe will be called on the provided "daprInternalServerPort". | ||
// if you are running an extra HTTP server, make sure to utilize a different port. Dapr will not wait till your app started, which is not required since the library takes care of Dapr related functionality internally. | ||
client.pubsub.subscribe("pubsub-name", "topic", async (data: any) => console.log(data)) | ||
server.pubsub.subscribe("pubsub-name", "topic", async (data: any) => console.log(data)) | ||
await client.pubsub.publish("pubsub-name", "topic", { hello: "world" }); | ||
@@ -70,3 +99,3 @@ | ||
await client.invoker.invoke("app-id", "method", HttpMethod.POST, { hello: "world" }); | ||
await client.invoker.listen("method", async (req: Req, res: Res) => console.log(req), { method: HttpMethod.POST }); | ||
await client.invoker.listen("method", async (data: { body: object, query: string }) => console.log(data.body), { method: HttpMethod.POST }); | ||
@@ -94,11 +123,11 @@ // Secrets | ||
## Package Maintenance | ||
## Library Development | ||
For publishing a new version, we update the version in `package.json` and we run `./publish.sh` | ||
The notes below help for developing the library locally. | ||
A custom script is utilized here since we have 2 libraries in one for HTTP and gRPC | ||
### Installation | ||
## Development | ||
Run the following commands to configure the library | ||
### Installation | ||
> ⚠ Make sure to have Dapr installed | ||
@@ -110,2 +139,16 @@ ```bash | ||
### Running the Library | ||
The command below runs the build process and will rebuild each time we change a file. This comes in handy when checking issues. | ||
```bash | ||
npm run start:dev | ||
``` | ||
### Publishing Package Package Maintenance | ||
For publishing a new version, we update the version in `package.json` and we run `./publish.sh` | ||
A custom script is utilized here since we have 2 libraries in one for HTTP and gRPC | ||
## Reference | ||
@@ -112,0 +155,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
815790
182
16741
178
5
14
6
6
+ Added@grpc/grpc-js@^1.3.2
+ Addedgoogle-protobuf@^3.15.8
+ Added@grpc/grpc-js@1.12.6(transitive)
+ Added@grpc/proto-loader@0.7.13(transitive)
+ Added@js-sdsl/ordered-map@4.4.2(transitive)
+ Added@protobufjs/aspromise@1.1.2(transitive)
+ Added@protobufjs/base64@1.1.2(transitive)
+ Added@protobufjs/codegen@2.0.4(transitive)
+ Added@protobufjs/eventemitter@1.1.0(transitive)
+ Added@protobufjs/fetch@1.1.0(transitive)
+ Added@protobufjs/float@1.0.2(transitive)
+ Added@protobufjs/inquire@1.1.0(transitive)
+ Added@protobufjs/path@1.1.2(transitive)
+ Added@protobufjs/pool@1.1.0(transitive)
+ Added@protobufjs/utf8@1.1.0(transitive)
+ Added@types/node@22.13.1(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedcliui@8.0.1(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedgoogle-protobuf@3.21.4(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedlodash.camelcase@4.3.0(transitive)
+ Addedlong@5.3.0(transitive)
+ Addedprotobufjs@7.4.0(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedwrap-ansi@7.0.0(transitive)
+ Addedy18n@5.0.8(transitive)
+ Addedyargs@17.7.2(transitive)
+ Addedyargs-parser@21.1.1(transitive)