Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 6630b69

Browse files
authored
0.20.3 (#70)
0.20.3
2 parents bea0b2e + c1d372e commit 6630b69

File tree

9 files changed

+143
-32
lines changed

9 files changed

+143
-32
lines changed

README.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,35 @@ Retry is provided by [promise-retry](https://www.npmjs.com/package/promise-retry
9393

9494
Additionally, the gRPC Client will contiually reconnect when in a failed state.
9595

96+
### TLS
97+
98+
Enable a secure connection by setting `useTLS: true`:
99+
100+
```typescript
101+
const zbc = new ZB.ZBClient(tlsProxiedGatewayAddress, {
102+
useTLS: true,
103+
})
104+
```
105+
96106
### OAuth
97107

98-
In case you need to connect to a secured endpoint with OAuth (such as Camunda Cloud), you can pass in OAuth credentials. This will enable TLS, and handle the OAuth flow to get / renew a JWT:
108+
In case you need to connect to a secured endpoint with OAuth (such as Camunda Cloud), you can pass in OAuth credentials. This will enable TLS (unless you explicitly disable it with `useTLS: false`), and handle the OAuth flow to get / renew a JWT:
99109

100110
```typescript
101111
const zbc = new ZB.ZBClient("103ca930-6da6-4df7-aa97-941eb1f85040.zeebe.camunda.io:443", {
102-
auth: {
112+
oAuth: {
103113
url: "https://login.cloud.camunda.io/oauth/token",
104114
audience: "103ca930-6da6-4df7-aa97-941eb1f85040.zeebe.camunda.io",
105115
clientId: "yStuGvJ6a1RQhy8DQpeXJ80yEpar3pXh",
106116
clientSecret:
107117
"WZahIGHjyj0-oQ7DZ_aH2wwNuZt5O8Sq0ZJTz0OaxfO7D6jaDBZxM_Q-BHRsiGO_",
108-
cache: true
118+
cacheOnDisk: true
109119
}
110120
}
111121
```
112122
123+
The `cacheOnDisk` option will cache the token on disk, which can be useful in development if you are restarting the service frequently.
124+
113125
### Create a Task Worker
114126
115127
```javascript

interfaces/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../dist/lib/interfaces'

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zeebe-node",
3-
"version": "v0.20.2",
3+
"version": "v0.20.3",
44
"description": "A Node.js client library for the Zeebe Microservices Orchestration Engine.",
55
"keywords": [
66
"zeebe",

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { BpmnParser } from './lib/BpmnParser'
2+
export * from './lib/interfaces'
23
export * from './zb/ZBClient'
34
export * from './zb/ZBWorker'

src/lib/GRPCClient.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class GRPCClient extends EventEmitter {
6363
packageName,
6464
protoPath,
6565
service,
66+
useTLS,
6667
}: {
6768
host: string
6869
loglevel: Loglevel
@@ -71,6 +72,7 @@ export class GRPCClient extends EventEmitter {
7172
packageName: string
7273
protoPath: string
7374
service: string
75+
useTLS: boolean
7476
}) {
7577
super()
7678
this.oAuth = oAuth
@@ -95,7 +97,7 @@ export class GRPCClient extends EventEmitter {
9597

9698
const proto = loadPackageDefinition(this.packageDefinition)[packageName]
9799
const listMethods = this.packageDefinition[`${packageName}.${service}`]
98-
const channelCredentials = oAuth
100+
const channelCredentials = useTLS
99101
? credentials.createSsl()
100102
: credentials.createInsecure()
101103
this.client = new proto[service](host, channelCredentials, {

src/lib/OAuthProvider.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ export interface OAuthProviderConfig {
1717
clientId: string
1818
clientSecret: string
1919
/** Cache token in memory and on filesystem? */
20-
cache: boolean
20+
cacheOnDisk: boolean
2121
}
22+
2223
export class OAuthProvider {
2324
private static cachedTokenFile = (clientId: string) =>
2425
`./.oauth-token-${clientId}.json`
@@ -27,7 +28,7 @@ export class OAuthProvider {
2728
public clientId: string
2829
public clientSecret: string
2930
public useFileCache: boolean
30-
public tokenCache = new Map()
31+
public tokenCache = {}
3132

3233
constructor({
3334
/** OAuth Endpoint URL */
@@ -37,24 +38,24 @@ export class OAuthProvider {
3738
clientId,
3839
clientSecret,
3940
/** Cache token in memory and on filesystem? */
40-
cache,
41+
cacheOnDisk,
4142
}: {
4243
url: string
4344
audience: string
4445
clientId: string
4546
clientSecret: string
46-
cache: boolean
47+
cacheOnDisk: boolean
4748
}) {
4849
this.url = url
4950
this.audience = audience
5051
this.clientId = clientId
5152
this.clientSecret = clientSecret
52-
this.useFileCache = cache
53+
this.useFileCache = cacheOnDisk
5354
}
5455

5556
public async getToken(): Promise<string> {
56-
if (this.tokenCache.has(this.clientId)) {
57-
return this.tokenCache.get(this.clientId).access_token
57+
if (this.tokenCache[this.clientId]) {
58+
return this.tokenCache[this.clientId].access_token
5859
}
5960
if (this.useFileCache) {
6061
const cachedToken = this.fromFileCache(this.clientId)
@@ -79,6 +80,7 @@ export class OAuthProvider {
7980
if (this.useFileCache) {
8081
this.toFileCache(token)
8182
}
83+
this.tokenCache[this.clientId] = token
8284
this.startExpiryTimer(token)
8385

8486
return token.access_token
@@ -112,13 +114,16 @@ export class OAuthProvider {
112114

113115
private toFileCache(token: Token) {
114116
const d = new Date()
117+
const file = OAuthProvider.cachedTokenFile(this.clientId)
115118
fs.writeFile(
116-
OAuthProvider.cachedTokenFile(this.clientId),
119+
file,
117120
JSON.stringify({
118121
...token,
119122
expiry: d.setSeconds(d.getSeconds() + token.expires_in),
120123
}),
121124
e => {
125+
// tslint:disable-next-line
126+
console.log('Error writing OAuth token to file' + file)
122127
// tslint:disable-next-line
123128
console.error(e)
124129
}
@@ -135,9 +140,9 @@ export class OAuthProvider {
135140
const current = d.setSeconds(d.getSeconds())
136141
const validityPeriod = token.expiry - current * 1000
137142
if (validityPeriod <= 0) {
138-
this.tokenCache.delete(this.clientId)
143+
delete this.tokenCache[this.clientId]
139144
return
140145
}
141-
setTimeout(() => this.tokenCache.delete(this.clientId), validityPeriod)
146+
setTimeout(() => delete this.tokenCache[this.clientId], validityPeriod)
142147
}
143148
}

src/lib/interfaces.ts

+91-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ export type Loglevel = 'INFO' | 'DEBUG' | 'NONE' | 'ERROR'
1010

1111
export interface CompleteFn<WorkerOutputVariables> {
1212
(updatedVariables?: Partial<WorkerOutputVariables>): boolean
13+
/**
14+
* Complete the job with a success, optionally passing in a state update to merge
15+
* with the workflow variables on the broker.
16+
*/
1317
success: (updatedVariables?: Partial<WorkerOutputVariables>) => boolean
18+
/**
19+
* Fail the job with an informative message as to the cause. Optionally pass in a
20+
* value remaining retries. If no value is passed for retries then the current retry
21+
* count is decremented. Pass in `0`for retries to raise an incident in Operate.
22+
*/
1423
failure: (errorMessage: string, retries?: number) => void
1524
}
1625

@@ -25,14 +34,6 @@ export interface OperationOptionsNoRetry {
2534
version?: number
2635
}
2736

28-
export function hasRetry(options): options is OperationOptionsWithRetry {
29-
return options.retry === true
30-
}
31-
32-
export function noRetry(options): options is OperationOptionsNoRetry {
33-
return options.retry === false
34-
}
35-
3637
export type OperationOptions =
3738
| OperationOptionsWithRetry
3839
| OperationOptionsNoRetry
@@ -85,10 +86,28 @@ export interface ActivateJobsResponse {
8586
* Request object to send the broker to request jobs for the worker.
8687
*/
8788
export interface ActivateJobsRequest {
89+
/**
90+
* The job type, as defined in the BPMN process (e.g. <zeebe:taskDefinition
91+
* type="payment-service" />)
92+
*/
8893
type: string
94+
/** The name of the worker activating the jobs, mostly used for logging purposes */
8995
worker: string
96+
/**
97+
* The duration the broker allows for jobs activated by this call to complete
98+
* before timing them out releasing them for retry on the broker.
99+
* The broker checks time outs every 30 seconds, so the broker timeout is guaranteed in at-most timeout + 29s
100+
* be guaranteed.
101+
*/
90102
timeout: number
103+
/**
104+
* The maximum jobs to activate by this request
105+
*/
91106
maxJobsToActivate: number
107+
/**
108+
* A list of variables to fetch as the job variables; if empty, all visible variables at
109+
* the time of activation for the scope of the job will be returned
110+
*/
92111
fetchVariable?: string[]
93112
/**
94113
* The request will be completed when atleast one job is activated or after the requestTimeout.
@@ -100,44 +119,84 @@ export interface ActivateJobsRequest {
100119
}
101120

102121
export interface ActivatedJob {
122+
/** The key, a unique identifier for the job */
103123
readonly key: string
124+
/**
125+
* The job type, as defined in the BPMN process (e.g. <zeebe:taskDefinition
126+
* type="payment-service" />)
127+
*/
104128
readonly type: string
129+
/** The job's workflow instance key */
105130
readonly workflowInstanceKey: string
131+
/** The bpmn process ID of the job workflow definition */
106132
readonly bpmnProcessId: string
133+
/** The version of the job workflow definition */
107134
readonly workflowDefinitionVersion: number
135+
/** The key of the job workflow definition */
108136
readonly workflowKey: string
137+
/** The associated task element ID */
109138
readonly elementId: string
139+
/**
140+
* The unique key identifying the associated task, unique within the scope of the
141+
* workflow instance
142+
*/
110143
readonly elementInstanceKey: string
111144
/**
112-
* JSON object as a string
145+
* A set of custom headers defined during modelling
113146
*/
114147
readonly customHeaders: string
148+
/** The name of the worker that activated this job */
115149
readonly worker: string
150+
/* The amount of retries left to this job (should always be positive) */
116151
readonly retries: number
117152
/**
118-
* epoch milliseconds
153+
* When the job will timeout on the broker if it is not completed by this worker.
154+
* In epoch milliseconds
119155
*/
120156
readonly deadline: string
121157
/**
122-
* JSON object as a string
158+
* All visible variables in the task scope, computed at activation time, constrained by any
159+
* fetchVariables value in the ActivateJobRequest.
123160
*/
124161
readonly variables: string
125162
}
126163

127164
export interface Job<Variables = KeyedObject, CustomHeaders = KeyedObject> {
165+
/** The key, a unique identifier for the job */
128166
readonly key: string
167+
/**
168+
* The job type, as defined in the BPMN process (e.g. <zeebe:taskDefinition
169+
* type="payment-service" />)
170+
*/
129171
readonly type: string
172+
/** The job's workflow instance key */
130173
readonly workflowInstanceKey: string
174+
/** The bpmn process ID of the job workflow definition */
131175
readonly bpmnProcessId: string
176+
/** The version of the job workflow defini` tion */
132177
readonly workflowDefinitionVersion: number
178+
/** The key of the job workflow definition */
133179
readonly workflowKey: string
180+
/** The associated task element ID */
134181
readonly elementId: string
182+
/**
183+
* The unique key identifying the associated task, unique within the scope of the
184+
* workflow instance
185+
*/
135186
readonly elementInstanceKey: string
187+
/**
188+
* A set of custom headers defined during modelling
189+
*/
136190
readonly customHeaders: CustomHeaders
191+
/** The name of the worker that activated this job */
137192
readonly worker: string
193+
/* The amount of retries left to this job (should always be positive) */
138194
readonly retries: number
139195
// epoch milliseconds
140196
readonly deadline: string
197+
/**
198+
* All visible variables in the task scope, computed at activation time.
199+
*/
141200
readonly variables: Variables
142201
}
143202

@@ -147,7 +206,8 @@ export interface ZBWorkerOptions {
147206
*/
148207
maxJobsToActivate?: number
149208
/**
150-
* Max ms to allow before time out of a task given to this worker. Default: 1000ms.
209+
* Max ms to allow before time out of a task given to this worker. Default: 30000ms.
210+
* The broker checks deadline timeouts every 30 seconds, so an
151211
*/
152212
timeout?: number
153213
/**
@@ -179,9 +239,26 @@ export interface CreateWorkflowInstanceRequest<Variables = KeyedObject> {
179239
}
180240

181241
export interface CreateWorkflowInstanceResponse {
242+
/**
243+
* The unique key identifying the workflow definition (e.g. returned from a workflow
244+
* in the DeployWorkflowResponse message)
245+
*/
182246
readonly workflowKey: string
247+
/**
248+
* The BPMN process ID of the workflow definition
249+
*/
183250
readonly bpmnProcessId: string
251+
/**
252+
* The version of the process; set to -1 to use the latest version
253+
*/
184254
readonly version: number
255+
/**
256+
* Stringified JSON document that will instantiate the variables for the root variable scope of the
257+
* workflow instance; it must be a JSON object, as variables will be mapped in a
258+
* key-value fashion. e.g. { "a": 1, "b": 2 } will create two variables, named "a" and
259+
* "b" respectively, with their associated values. [{ "a": 1, "b": 2 }] would not be a\
260+
* valid argument, as the root of the JSON document is an array and not an object.
261+
*/
185262
readonly workflowInstanceKey: string
186263
}
187264

@@ -319,7 +396,8 @@ export interface ZBClientOptions {
319396
retry?: boolean
320397
maxRetries?: number
321398
maxRetryTimeout?: number
322-
auth?: OAuthProviderConfig
399+
oAuth?: OAuthProviderConfig
400+
useTLS?: boolean
323401
longPoll?: number
324402
}
325403

0 commit comments

Comments
 (0)