Skip to content

Commit 2337424

Browse files
committed
Merge branch 'master' into shubham1172/customizable-logging
2 parents 7775787 + c003770 commit 2337424

File tree

26 files changed

+587
-484
lines changed

26 files changed

+587
-484
lines changed

README.md

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,70 @@
1-
[![Discord](https://img.shields.io/discord/778680217417809931)]()
2-
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/dapr/js-sdk/blob/master/LICENSE)
3-
[![FOSSA Status](https://app.fossa.com/api/projects/custom%2B162%2Fgithub.com%2Fdapr%2Fcomponents-contrib.svg?type=shield)](https://app.fossa.com/projects/custom%2B162%2Fgithub.com%2Fdapr%2Fcomponents-contrib?ref=badge_shield)
1+
<p align="center">
2+
<a href="https://dapr.io">
3+
<img src="https://dapr.io/images/dapr.svg" height="128">
4+
<h1 align="center">Dapr</h1>
5+
</a>
6+
</p>
7+
8+
<p align="center">
9+
<a aria-label="NPM version" href="https://www.npmjs.com/package/@dapr/dapr">
10+
<img alt="" src="https://img.shields.io/npm/v/@dapr/dapr?style=for-the-badge&labelColor=000000">
11+
</a>
12+
<a aria-label="License" href="https://github.com/dapr/js-sdk/blob/master/LICENSE">
13+
<img alt="" src="https://img.shields.io/badge/License-Apache_2.0-blue.svg?style=for-the-badge&labelColor=000000">
14+
</a>
15+
<a aria-label="Join the community on Discord" href="https://discord.com/invite/ptHhX6jc34">
16+
<img alt="" src="https://img.shields.io/badge/Join%20the%20community-blueviolet.svg?style=for-the-badge&logo=Discord&labelColor=000000&logoWidth=20&logoColor=FFFFFF">
17+
</a>
18+
</p>
19+
20+
## Getting Started
21+
22+
Instantly get started by installing the Dapr JS SDK and reading the [getting started documentation](https://docs.dapr.io/developing-applications/sdks/js) or [follow one of the quickstarts](https://github.com/dapr/quickstarts)
423

5-
# Dapr Node.js SDKs
6-
7-
The official [Dapr](https://dapr.io) Node.js SDK that allows interfacing with the Dapr sidecar for easy application building.
8-
9-
## Introduction
10-
11-
The Dapr JS SDK will allow you to interface with the Dapr process that abstracts several commonly used functionalities such as Service-to-Service invocation, State Management, PubSub, and more.
12-
13-
![](./documentation/assets/dapr-architecture.png)
14-
15-
Looking at the illustration above, we can see there will always always be another process (the Dapr Sidecar) running that your application will interface with. This process can either be started manually (e.g. through Dapr CLI) or injected as a container (e.g. through Kubernetes Sidecar injection in your pod).
16-
17-
For your application to interface with this, 2 components should be taken into account:
18-
* **DaprServer:** Dapr Sidecar -> our Application - When we Subscribe to a Topic, Create an Actor, ... we receive an event from the Dapr process that abstracted its implementation. Our application should thus listen to this (which this SDK helps you with).
19-
* **DaprClient:** Your Application -> Dapr Sidecar - When we want to Publish an Event, Execute a Binding, ... we will talk with the Dapr process that calls its implementation for us so that we don't have to write it ourself!
20-
21-
## Simple Example
24+
```
25+
npm install --save @dapr/dapr
26+
```
2227

23-
> [Full version](./examples/http/pubsub)
28+
> ⚠️ the [`dapr-client`](https://www.npmjs.com/package/dapr-client) package has been deprecated. Please see https://github.com/dapr/js-sdk/issues/259 for more information.
2429
25-
As a simple example, consider the use case: "Creating a PubSub where we can publish a message on a Topic and also receive messages back from this topic". Normally for this use case we would have to look at the Broker that we want to utilize and implement their specificities. While as with Dapr we can use a simple SDK and configure the "component" that we want to interface with.
30+
## Documentation
2631

27-
> 🤩 This also means that if we want to switch from one broker to another (e.g. [Azure Service Bus](https://docs.dapr.io/reference/components-reference/supported-pubsub/setup-azure-servicebus/) to [RabbitMQ](https://docs.dapr.io/reference/components-reference/supported-pubsub/setup-rabbitmq/)) we just have to change the component implementation!
32+
Visit [https://docs.dapr.io/developing-applications/sdks/js/](https://docs.dapr.io/developing-applications/sdks/js/) to view the full documentation.
2833

29-
**component.yaml**
34+
## Who is using Dapr?
3035

31-
```yaml
32-
apiVersion: dapr.io/v1alpha1
33-
kind: Component
34-
metadata:
35-
name: my-pubsub-component
36-
namespace: default
37-
spec:
38-
type: pubsub.rabbitmq
39-
version: v1
40-
metadata:
41-
- name: host
42-
value: "amqp://localhost:5672"
43-
```
36+
Dapr is used by the world's leading companies.
4437

45-
**example.ts**
38+
<div align="center">
39+
<img src="https://dapr.io/images/bosch.png" width="100px">
40+
<img src="https://dapr.io/images/zeiss.png" width="100px">
41+
<img src="https://dapr.io/images/alibaba.png" width="100px">
42+
<img src="https://dapr.io/images/ignition-group.png" width="100px">
43+
<img src="https://dapr.io/images/roadwork.png" width="100px">
44+
<img src="https://dapr.io/images/autonavi.png" width="100px">
45+
<img src="https://dapr.io/images/legentic.png" width="100px">
46+
<img src="https://dapr.io/images/man-group.png" width="100px">
47+
</div>
4648

47-
```javascript
48-
import { DaprClient, DaprServer } from "@dapr/dapr";
49+
View the main site [https://dapr.io/](https://dapr.io/) to learn more.
4950

50-
const daprHost = "127.0.0.1"; // Dapr Sidecar Host
51-
const daprPort = "50000"; // Dapr Sidecar Port
52-
const serverHost = "127.0.0.1"; // App Host of this Example Server
53-
const serverPort = "50001"; // App Port of this Example Server
51+
## Community
5452

55-
// Create a Server (will subscribe) and Client (will publish)
56-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
57-
const client = new DaprClient(daprHost, daprPort);
53+
For the JS SDK we are utilizing [GitHub Communities](https://github.com/dapr/js-sdk/discussions) to track announcements, articles, and more!
5854

59-
// Initialize the server to subscribe (listen)
60-
await server.pubsub.subscribe("my-pubsub-component", "my-topic", async (data: any) => console.log(`Received: ${JSON.stringify(data)}`));
61-
await server.start();
55+
The General Dapr community can be found on [Discord](https://discord.com/invite/ptHhX6jc34), where you can ask questions, propose features, and share your thoughts.
6256

63-
// Send a message
64-
await client.pubsub.publish("my-pubsub-component", "my-topic", { hello: "world" });
65-
```
57+
## Contributing
6658

67-
To start this we of course have to start the Dapr process with it. With the command below we can utilize the CLI to quickly test this:
59+
Please see our [Contributing Overview](https://docs.dapr.io/contributing/js-contributing/).
6860

69-
```bash
70-
dapr run --app-id example-http-pubsub --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./components npm run start
71-
```
72-
73-
Which will result in the message:
61+
### Good First Issues
7462

75-
```bash
76-
Received: {"hello":"world"}
77-
```
63+
We have a list of [good first issues](https://github.com/dapr/js-sdk/labels/good%20first%20issue) that contain bugs which have a relatively limited scope. This is a great place to get started, gain experience, and get familiar with our contribution process.
7864

79-
## Information & Links
65+
## Authors
8066

81-
* [Reference](./documentation/reference.md) containing code snippets of how to use the different methods.
82-
* [Examples](./documentation/examples.md) containing examples you can use to build your application.
83-
* [Development](./documentation/development.md) containing pointers for getting started on how to contribute to the SDK.
67+
- [Xavier Geerinck](https://www.linkedin.com/in/xaviergeerinck/) ([@XavierGeerinck](https://twitter.com/XavierGeerinck)) – [M18X](https://xaviergeerinck.com/) & [Proximus](https://proximus.com)
68+
- [Shubham Sharma](https://www.linkedin.com/in/shubham1172/) ([@shubham1172](https://twitter.com/shubham1172)) – [Microsoft](https://microsoft.com)
69+
- [Amulya Varote](https://www.linkedin.com/in/amulya-varote-96954287/) - [Microsoft](https://microsoft.com)
70+
- [Tanvi Gour](https://www.linkedin.com/in/tanvigour/) - [Microsoft](https://microsoft.com)

daprdocs/content/en/js-sdk-docs/js-client/_index.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,7 @@ const daprAppId = "example-config";
347347

348348
async function start() {
349349

350-
const client = new DaprClient(
351-
daprHost,
352-
process.env.DAPR_HTTP_PORT
353-
);
350+
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT);
354351

355352
const config = await client.configuration.get('config-store', ['key1', 'key2']);
356353
console.log(config);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
type: docs
3+
title: "JavaScript Examples"
4+
linkTitle: "Examples"
5+
weight: 500
6+
description: Get started with the Dapr Javascript SDK through some of our examples!
7+
---
8+
9+
## Quickstarts
10+
11+
* [State Management](https://github.com/dapr/quickstarts/tree/master/state_management/javascript/sdk): Learn the concept of state management with Dapr
12+
* [Pub Sub](https://github.com/dapr/quickstarts/tree/master/pub_sub/javascript/sdk): Create your own Publish / Subscribe system
13+
* [Secrets Management](https://github.com/dapr/quickstarts/tree/master/secrets_management/javascript/sdk)
14+
* [Service Invocation](https://github.com/dapr/quickstarts/tree/master/service_invocation/javascript/http)
15+
16+
## Articles
17+
18+
> Want your article added? [Let us know!](https://github.com/dapr/js-sdk/discussions/categories/articles) so we can add it below
19+
20+
* [xaviergeerinck.com - Create an Azure IoT Hub Stream Processor with Dapr](https://xaviergeerinck.com/post/2022/05/19/dapr-stream-processor)
21+
22+
* [xaviergeerinck.com - Integrate Dapr with Nest.JS and the Dapr KS SDK](https://xaviergeerinck.com/post/2022/03/29/dapr-with-nestjs)
23+
24+
25+
* [xaviergeerinck.com - Parking Garage Sensor implementation using Dapr Actors](https://xaviergeerinck.com/post/2021/10/09/parking-garage)
26+
27+
* [xaviergeerinck.com - Running Dapr on Azure IoT Edge](https://xaviergeerinck.com/post/2021/04/23/iot-edge)
28+
29+
* [xaviergeerinck.com - Creating an Email Microservice with Typescript and Dapr](https://xaviergeerinck.com/post/2021/03/25/creating-email-microservice)
30+
31+
* [xaviergeerinck.com - Creating a User Login/Register Microservice](https://xaviergeerinck.com/post/2020/04/10/creating-account-microservice)

documentation/development.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ npm run test:unit:main
4444
npm run test:unit:actors
4545

4646
# Start gRPC tests
47-
npm run test:e2e:grpc:main
47+
npm run test:e2e:grpc
4848

4949
# Start HTTP tests
50-
npm run test:e2e:http:main
51-
npm run test:e2e:http:actors
50+
npm run test:e2e:http
5251
```
5352

5453
## Publishing

examples/configuration/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const daprHost = "127.0.0.1";
1717
const daprPortDefault = "3500";
1818

1919
async function start() {
20-
2120
const client = new DaprClient(
2221
daprHost,
2322
process.env.DAPR_HTTP_PORT ?? daprPortDefault

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
"scripts": {
77
"test": "npm run test:unit:all && npm run test:e2e:all",
88
"test:load": "jest --runInBand --detectOpenHandles",
9-
"test:load:http": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components npm run test:load 'test/load'",
9+
"test:load:http": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components -- npm run test:load 'test/load'",
1010
"test:e2e": "jest --runInBand --detectOpenHandles",
11-
"test:e2e:all": "npm run test:e2e:grpc:main && npm run test:e2e:http:main && npm run test:e2e:http:actors",
12-
"test:e2e:grpc:main": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol grpc --app-port 50001 --dapr-grpc-port 50000 --components-path ./test/components npm run test:e2e 'test/e2e/main.grpc.test.ts'",
13-
"test:e2e:http:main": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components npm run test:e2e 'test/e2e/main.http.test.ts'",
14-
"test:e2e:http:actors": "dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components npm run test:e2e 'test/e2e/actors.http.test.ts'",
11+
"test:e2e:all": "npm run test:e2e:grpc && npm run test:e2e:http && npm run test:e2e:http:actors",
12+
"test:e2e:grpc": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol grpc --app-port 50001 --dapr-grpc-port 50000 --components-path ./test/components -- jest --runInBand --detectOpenHandles --testMatch [ '**/test/e2e/grpc/(client|server).test.ts' ]",
13+
"test:e2e:http": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components -- jest --runInBand --detectOpenHandles --testMatch [ '**/test/e2e/http/(client|server).test.ts' ]",
14+
"test:e2e:http:actors": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components -- jest --runInBand --detectOpenHandles --testMatch [ '**/test/e2e/http/actors.test.ts' ]",
1515
"test:unit": "jest --runInBand --detectOpenHandles",
1616
"test:unit:all": "npm run test:unit:main && npm run test:unit:actors && npm run test:unit:utils",
1717
"test:unit:main": "NODE_ENV=test npm run test:unit 'test/unit/main/.*\\.test\\.ts'",

scripts/test-e2e.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515

1616
# Start gRPC tests
1717
echo "Running gRPC tests"
18-
npm run test:e2e:grpc:main
18+
npm run test:e2e:grpc
1919

2020
# Start HTTP tests
2121
echo "Running HTTP tests"
22-
npm run test:e2e:http:main
23-
npm run test:e2e:http:actors
22+
npm run test:e2e:http

scripts/test-init.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/implementation/Client/DaprClient.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14+
import * as NodeJSUtils from "../../utils/NodeJS.util";
15+
1416
import IClientBinding from '../../interfaces/Client/IClientBinding';
1517
import IClientPubSub from '../../interfaces/Client/IClientPubSub';
1618
import IClientState from '../../interfaces/Client/IClientState';
@@ -136,6 +138,45 @@ export default class DaprClient {
136138
await this.daprClient.stop();
137139
}
138140

141+
async awaitSidecarStarted(): Promise<void> {
142+
// 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
143+
// if we are using actors we will change this to 4s to let the placement tables update
144+
let isHealthy = false;
145+
let isHealthyRetryCount = 0;
146+
const isHealthyMaxRetryCount = 60; // 1s startup delay and we try max for 60s
147+
148+
console.log(`[Dapr-JS][Client] Awaiting Sidecar to be Started`);
149+
while (!isHealthy) {
150+
console.log(`[Dapr-JS][Client] Waiting till Dapr Sidecar Started (#${isHealthyRetryCount})`);
151+
await NodeJSUtils.sleep(Settings.getDaprSidecarPollingDelayMs());
152+
153+
// Implement API call manually as we need to enable calling without initialization
154+
// everything routes through the `execute` method
155+
// to check health, we just ping the /metadata endpoint and see if we get a response
156+
isHealthy = await this.health.isHealthy();
157+
158+
// Finally, Handle the retry logic
159+
isHealthyRetryCount++;
160+
161+
if (isHealthyRetryCount > isHealthyMaxRetryCount) {
162+
throw new Error("DAPR_SIDECAR_COULD_NOT_BE_STARTED");
163+
}
164+
}
165+
}
166+
167+
/**
168+
* Ensure the client is started, this takes care of:
169+
* 1. Making sure the sidecar is started
170+
* 2. Making sure the connection is established (e.g. in gRPC)
171+
* 3. Making sure the client is ready to be used
172+
*/
173+
async start(): Promise<void> {
174+
await this.awaitSidecarStarted();
175+
await this.daprClient.start();
176+
await this.daprClient.setIsInitialized(true);
177+
console.log(`[Dapr-JS][Client] Sidecar Started`);
178+
}
179+
139180
getDaprClient(): IClient {
140181
return this.daprClient;
141182
}

0 commit comments

Comments
 (0)