Comments (5)
The Zeebe worker payload comes over as stringified JSON, so int64 values will be numbers to the parser.
from camunda-8-js-sdk.
As the first step towards this, the Zeebe worker input and output (the job.variables
received by the handler function, and the input to the complete
function) are now parsed and stringified using the lossless-number
library.
This means that int64
number values outside the safe range of a JavaScript number sent over the wire from the Zeebe broker will cause the SDK to throw an exception when it parses the stringified JSON.
This is lossless, but not predictable for consumers.
The next phase is to allow the user to specify a LosslessDto
class as a decoder schema.
from camunda-8-js-sdk.
OK, the LosslessDto
for the worker variable decoding is implemented.
Typing of Zeebe Worker Variables
The variable payload in a Zeebe Worker task handler is available as an object job.variables
. By default, this is of type any
.
The ZBClient.createWorker()
method accepts an inputVariableDto
to control the parsing of number values and provide design-time type information. Passing an inputVariableDto
class to a Zeebe worker is optional. If a Dto class is passed to the Zeebe Worker, it is used for two purposes:
- To provide design-time type information on the
job.variables
object. - To specify the parsing of JSON number fields. These can potentially represent
int64
values that cannot be represented accurately by the JavaScriptnumber
type. With a Dto, you can specify that a specific JSON number fields be parsed losslessly to astring
orBigInt
.
With no Dto specified, there is no design-time type safety. At run-time, all JSON numbers are converted to the JavaScript number
type. If a variable field has a number value that cannot be safely represented using the JavaScript number type (a value greater than 2^53 -1) then an exception is thrown.
To provide a Dto, extend the LosslessDto
class, like so:
class MyVariableDto extends LosslessDto {
name!: string
maybeAge?: number
@Int64String
veryBigNumber!: string
@BigIntValue
veryBigInteger!: bigint
}
In this case, veryBigNumber
is an int64
value. It is transferred as a JSON number on the wire, but the parser will parse it into a string
so that no loss of precision occurs. Similarly, veryBigInteger
is a very large integer value. In this case, we direct the parser to parse this variable field as a bigint
.
You can nest Dtos like this:
class MyLargerDto extends LosslessDto {
id!: string
@ChildDto(MyVariableDto)
entry!: MyVariableDto
}
Typing of Custom Headers
The Zeebe worker receives custom headers as job.customHeaders
. The ZBClient.createWorker()
method accepts a customHeadersDto
to control the behaviour of custom header parsing of number values and provide design-time type information.
This follows the same strategy as the job variables, as previously described.
from camunda-8-js-sdk.
OK, let's make this simple.
These are the scenarios that we want to handle:
- We do not want to have JavaScript numbers in the application that claim to represent a value but are incorrect.
- We want to allow the consumer to specify how these values should be represented in the application, as
bigint
orstring
. - We want this to be consistent - in other words, we don't want a variable field that has a different type depending on its value.
- We don't want to force the consumer to make all numbers
bigint
orstring
, only the ones that could carry an unsafe int64 value.
To accomplish these:
- The parser should throw if it encounters an unsafe
int64
value that is not mapped to eitherstring
orbigint
. - We should accept a Dto that can declare that number fields are mapped to either
string
orbigint
. - Any number that is not mapped and is safe to represent as
number
will be parsed tonumber
. This means it is always an accuratenumber
, or the application throws. - See 3.
So there is one type of parser, one type of Dto.
The parser, if not supplied with a Dto, will simply parse the variables using lossless-json
, then iterate over the resulting object, converting all LosslessNumbers
to number
or throwing if they are unsafe. This requires no type information or mapping.
This will also be the last stage for the parser when supplied with a Dto. When it is supplied with a Dto, the parser will iterate over the Dto, and convert the LosslessNumber
of the parsed object to the mapped type of the Dto.
A question arises here: How should the parser behave if it encounters a value that is not a LosslessNumber
when a mapping has been specified?
Answer: it should throw an error, because the expectation expressed in the Dto does not meet reality, and as a consequence the application behaviour is not predictable from the code.
Final stage: the object should be iterated with the parser to convert any remaining (unmapped) LosslessNumbers
to number
type or throw.
A question arises here: a field may be specified in the Dto as number
but in fact it has an int64
value in it. If this is detected by the presence of an unsafe number, the parser should definitely throw.
But what about when the Dto is not complete? After the application is deployed, the payload is extended and now contains an int64
field that is not in the Dto. The application gets all the variables, and an unsafe, unmapped value shows up. How should the parser behave? If it throws, it forces the application developer to either constrain the variables requested to those in the Dto, or to extend the Dto.
Answer: it should throw if it there is an unmapped, unsafe value.
from camunda-8-js-sdk.
Next step:
Deal with Output Variable Dto and stringifying variables.
from camunda-8-js-sdk.
Related Issues (20)
- Example for usage around ancestorElementInstanceKey in modifyProcessInstance HOT 7
- Support ZEEBE_REST_ADDRESS and ZEEBE_GRPC_ADDRESS HOT 5
- Incorporate Modeler Custom Root Cert Code HOT 3
- Run integration tests on Windows runner HOT 1
- Support Custom TLS Certificate as String
- Intermittent failure in Client-ThrowError integration test HOT 3
- Test failures on Windows / Windows Support HOT 6
- 404 Error when querying object immediately HOT 6
- Zeebe gRPC client.waitForReady always fails HOT 1
- Cannot construct Camunda8 using explicit configuration HOT 3
- Intermittent failure of Tasklist test in GitHub CI on SaaS HOT 1
- Token is always expired from cache
- HTTP Basic authentication support is missing HOT 3
- Add Job Update Timeout command to the JS Client HOT 6
- Support ZEEBE_INSECURE_CONNECTION env var
- [Feature Request] Support multi-tenanted Zeebe workers HOT 5
- ZeebeGrpcClient.deployResource with an array of resources HOT 1
- Response code 415 (Unsupported Media Type) on Zeebe REST completeUserTask HOT 4
- Spike: Investigate Testing Framework for the JS SDK HOT 1
- 404 Error when calling OptimizeAPI exportReportDefinitions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from camunda-8-js-sdk.