Skip to content

Commit 5c99e89

Browse files
Ensure payload converters keep Uint8Array type equality (#1142) (#1143)
Closes #1142 <!--- For ALL Contributors 👇 --> ## What was changed <!-- Describe what has changed in this PR --> Modify BinaryPayloadConverter and ProtobufBinaryPayloadConverter so that they wrap Uint8Array with an instance of Uint8Array from the workflow context. ## Why? <!-- Tell your future self why have you made these changes --> Preserve instanceof semantics by payload converters ## Checklist <!--- add/delete as needed ---> 1. Closes #1142 3. How was this tested: <!--- Please describe how you tested your changes/how we can test them --> Modified integration test args-and-return to check instanceof of an injected Uint8Array
1 parent aec754c commit 5c99e89

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

packages/common/src/converter/payload-converter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,12 @@ export class BinaryPayloadConverter implements PayloadConverterWithEncoding {
209209
}
210210

211211
public fromPayload<T>(content: Payload): T {
212-
return content.data as any;
212+
return (
213+
// Wrap with Uint8Array from this context to ensure `instanceof` works
214+
(
215+
content.data ? new Uint8Array(content.data.buffer, content.data.byteOffset, content.data.length) : content.data
216+
) as any
217+
);
213218
}
214219
}
215220

packages/common/src/converter/protobuf-payload-converters.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ export class ProtobufBinaryPayloadConverter extends ProtobufPayloadConverter {
9797

9898
public fromPayload<T>(content: Payload): T {
9999
const { messageType, data } = this.validatePayload(content);
100-
return messageType.decode(data) as unknown as T;
100+
// Wrap with Uint8Array from this context to ensure `instanceof` works
101+
const localData = data ? new Uint8Array(data.buffer, data.byteOffset, data.length) : data;
102+
return messageType.decode(localData) as unknown as T;
101103
}
102104
}
103105

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { decode } from '@temporalio/common/lib/encoding';
2+
import { ApplicationFailure } from '@temporalio/workflow';
23

3-
export async function argsAndReturn(greeting: string, _skip: undefined, arr: ArrayBuffer): Promise<string> {
4-
const name = decode(new Uint8Array(arr));
4+
export async function argsAndReturn(greeting: string, _skip: undefined, arr: Uint8Array): Promise<string> {
5+
if (!(arr instanceof Uint8Array)) {
6+
throw ApplicationFailure.nonRetryable('Uint8Array not wrapped');
7+
}
8+
const name = decode(arr);
59
return `${greeting}, ${name}`;
610
}

0 commit comments

Comments
 (0)