@@ -14,7 +14,7 @@ The Dapr Client allows you to communicate with the Dapr Sidecar and get access t
1414
1515- [ Dapr CLI] ({{< ref install-dapr-cli.md >}}) installed
1616- Initialized [ Dapr environment] ({{< ref install-dapr-selfhost.md >}})
17- - [ Latest LTS version of Node or greater] ( https://nodejs.org/en/ )
17+ - [ Latest LTS version of Node.js or greater] ( https://nodejs.org/en/ )
1818
1919## Installing and importing Dapr's JS SDK
2020
@@ -451,6 +451,96 @@ start().catch((e) => {
451451});
452452```
453453
454+ ### Cryptography API
455+
456+ > Support for the cryptography API is only available on the gRPC client in the JavaScript SDK.
457+
458+ ``` typescript
459+ import { createReadStream , createWriteStream } from " node:fs" ;
460+ import { readFile , writeFile } from " node:fs/promises" ;
461+ import { pipeline } from " node:stream/promises" ;
462+
463+ import { DaprClient , CommunicationProtocolEnum } from " @dapr/dapr" ;
464+
465+ const daprHost = " 127.0.0.1" ;
466+ const daprPort = " 50050" ; // Dapr Sidecar Port of this example server
467+
468+ async function start() {
469+ const client = new DaprClient ({
470+ daprHost ,
471+ daprPort ,
472+ communicationProtocol: CommunicationProtocolEnum .GRPC ,
473+ });
474+
475+ // Encrypt and decrypt a message using streams
476+ await encryptDecryptStream (client );
477+
478+ // Encrypt and decrypt a message from a buffer
479+ await encryptDecryptBuffer (client );
480+ }
481+
482+ async function encryptDecryptStream(client : DaprClient ) {
483+ // First, encrypt the message
484+ console .log (" == Encrypting message using streams" );
485+ console .log (" Encrypting plaintext.txt to ciphertext.out" );
486+
487+ await pipeline (
488+ createReadStream (" plaintext.txt" ),
489+ await client .crypto .encrypt ({
490+ componentName: " crypto-local" ,
491+ keyName: " symmetric256" ,
492+ keyWrapAlgorithm: " A256KW" ,
493+ }),
494+ createWriteStream (" ciphertext.out" ),
495+ );
496+
497+ // Decrypt the message
498+ console .log (" == Decrypting message using streams" );
499+ console .log (" Encrypting ciphertext.out to plaintext.out" );
500+ await pipeline (
501+ createReadStream (" ciphertext.out" ),
502+ await client .crypto .decrypt ({
503+ componentName: " crypto-local" ,
504+ }),
505+ createWriteStream (" plaintext.out" ),
506+ );
507+ }
508+
509+ async function encryptDecryptBuffer(client : DaprClient ) {
510+ // Read "plaintext.txt" so we have some content
511+ const plaintext = await readFile (" plaintext.txt" );
512+
513+ // First, encrypt the message
514+ console .log (" == Encrypting message using buffers" );
515+
516+ const ciphertext = await client .crypto .encrypt (plaintext , {
517+ componentName: " crypto-local" ,
518+ keyName: " my-rsa-key" ,
519+ keyWrapAlgorithm: " RSA" ,
520+ });
521+
522+ await writeFile (" test.out" , ciphertext );
523+
524+ // Decrypt the message
525+ console .log (" == Decrypting message using buffers" );
526+ const decrypted = await client .crypto .decrypt (ciphertext , {
527+ componentName: " crypto-local" ,
528+ });
529+
530+ // The contents should be equal
531+ if (plaintext .compare (decrypted ) !== 0 ) {
532+ throw new Error (" Decrypted message does not match original message" );
533+ }
534+ }
535+
536+ start ().catch ((e ) => {
537+ console .error (e );
538+ process .exit (1 );
539+ });
540+ ```
541+
542+ > For a full guide on cryptography visit [ How-To: Cryptography] ({{< ref howto-cryptography.md >}}).
543+
454544### Distributed Lock API
455545
456546#### Try Lock and Unlock APIs
@@ -500,6 +590,56 @@ start().catch((e) => {
500590
501591> For a full guide on distributed locks visit [ How-To: Use Distributed Locks] ({{< ref howto-use-distributed-lock.md >}}).
502592
593+ ### Workflow API
594+
595+ #### Workflow management
596+
597+ ``` typescript
598+ import { DaprClient } from " @dapr/dapr" ;
599+
600+ async function start() {
601+ const client = new DaprClient ();
602+
603+ // Start a new workflow instance
604+ const instanceId = await client .workflow .start (" OrderProcessingWorkflow" , {
605+ Name: " Paperclips" ,
606+ TotalCost: 99.95 ,
607+ Quantity: 4 ,
608+ });
609+ console .log (` Started workflow instance ${instanceId } ` );
610+
611+ // Get a workflow instance
612+ const workflow = await client .workflow .get (instanceId );
613+ console .log (
614+ ` Workflow ${workflow .workflowName }, created at ${workflow .createdAt .toUTCString ()}, has status ${
615+ workflow .runtimeStatus
616+ } ` ,
617+ );
618+ console .log (` Additional properties: ${JSON .stringify (workflow .properties )} ` );
619+
620+ // Pause a workflow instance
621+ await client .workflow .pause (instanceId );
622+ console .log (` Paused workflow instance ${instanceId } ` );
623+
624+ // Resume a workflow instance
625+ await client .workflow .resume (instanceId );
626+ console .log (` Resumed workflow instance ${instanceId } ` );
627+
628+ // Terminate a workflow instance
629+ await client .workflow .terminate (instanceId );
630+ console .log (` Terminated workflow instance ${instanceId } ` );
631+
632+ // Purge a workflow instance
633+ await client .workflow .purge (instanceId );
634+ console .log (` Purged workflow instance ${instanceId } ` );
635+ }
636+
637+ start ().catch ((e ) => {
638+ console .error (e );
639+ process .exit (1 );
640+ });
641+ ```
642+
503643## Related links
504644
505645- [ JavaScript SDK examples] ( https://github.com/dapr/js-sdk/tree/master/examples )
0 commit comments