Skip to content

Commit f869a69

Browse files
committed
fix: allow Uint16|8Array for binary data
Previously we only considered `Uint32Array` binary data. This was an oversight. This fixes that issue. Fixes: cloudevents#491 Signed-off-by: Lance Ball <[email protected]>
1 parent d9ee0e0 commit f869a69

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/event/cloudevent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ See: https://github.com/cloudevents/spec/blob/v1.0/spec.md#type-system`);
133133

134134
set data(value: T | undefined) {
135135
if (isBinary(value)) {
136-
this.data_base64 = asBase64(value);
136+
this.data_base64 = asBase64(value as unknown as Buffer);
137137
}
138138
this.#_data = value;
139139
}

src/event/validation.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ export const isDefined = (v: unknown): boolean => v !== null && typeof v !== "un
3636
export const isBoolean = (v: unknown): boolean => typeof v === "boolean";
3737
export const isInteger = (v: unknown): boolean => Number.isInteger(v as number);
3838
export const isDate = (v: unknown): v is Date => v instanceof Date;
39-
export const isBinary = (v: unknown): v is Uint32Array => v instanceof Uint32Array;
39+
export const isBinary = (v: unknown): boolean =>
40+
v instanceof Uint32Array ||
41+
v instanceof Uint16Array ||
42+
v instanceof Uint8Array;
4043

4144
export const isStringOrThrow = (v: unknown, t: Error): boolean =>
4245
isString(v)
@@ -73,7 +76,7 @@ export const isBase64 = (value: unknown): boolean =>
7376

7477
export const isBuffer = (value: unknown): boolean => value instanceof Buffer;
7578

76-
export const asBuffer = (value: string | Buffer | Uint32Array): Buffer =>
79+
export const asBuffer = (value: string | Buffer | Uint32Array | Uint16Array | Uint8Array): Buffer =>
7780
isBinary(value)
7881
? Buffer.from((value as unknown) as string)
7982
: isBuffer(value)
@@ -82,7 +85,8 @@ export const asBuffer = (value: string | Buffer | Uint32Array): Buffer =>
8285
throw new TypeError("is not buffer or a valid binary");
8386
})();
8487

85-
export const asBase64 = (value: string | Buffer | Uint32Array): string => asBuffer(value).toString("base64");
88+
export const asBase64 =
89+
(value: string | Buffer | Uint32Array | Uint16Array | Uint8Array): string => asBuffer(value).toString("base64");
8690

8791
export const clone = (o: Record<string, unknown>): Record<string, unknown> => JSON.parse(JSON.stringify(o));
8892

test/integration/spec_1_tests.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,25 @@ describe("CloudEvents Spec v1.0", () => {
182182
const ce = cloudevent.cloneWith({ datacontenttype: "text/plain", data: dataBinary });
183183
expect(ce.data_base64).to.equal(expected);
184184
});
185+
186+
it("should be ok when type is 'Uint16Array' for 'Binary'", () => {
187+
const dataString = ")(*~^my data for ce#@#$%";
188+
189+
const dataBinary = Uint16Array.from(dataString, (c) => c.codePointAt(0) as number);
190+
const expected = asBase64(dataBinary);
191+
192+
const ce = cloudevent.cloneWith({ datacontenttype: "text/plain", data: dataBinary });
193+
expect(ce.data_base64).to.equal(expected);
194+
});
195+
196+
it("should be ok when type is 'Uint8Array' for 'Binary'", () => {
197+
const dataString = ")(*~^my data for ce#@#$%";
198+
199+
const dataBinary = Uint8Array.from(dataString, (c) => c.codePointAt(0) as number);
200+
const expected = asBase64(dataBinary);
201+
202+
const ce = cloudevent.cloneWith({ datacontenttype: "text/plain", data: dataBinary });
203+
expect(ce.data_base64).to.equal(expected);
204+
});
185205
});
186206
});

0 commit comments

Comments
 (0)