Skip to content

Commit

Permalink
fix: support for binary data
Browse files Browse the repository at this point in the history
  • Loading branch information
infodusha committed Sep 26, 2023
1 parent 2732534 commit bceae90
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
27 changes: 26 additions & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Forest } from "./test-data/generated/forest_pb";
import { Universe } from "./test-data/generated/universe_pb";
import { Spices } from "./test-data/generated/spices_pb";
import { Newspaper } from "./test-data/generated/newspaper_pb";
import { File } from "./test-data/generated/file_pb";

describe('fromProtobufObject', () => {
it('Should work with easy structure', () => {
Expand Down Expand Up @@ -245,4 +246,28 @@ describe('Map rule', () => {
const newspaper = fromProtobufObject(Newspaper, obj);
expect(newspaper.toObject()).toEqual(obj);
});
});
});

describe('Binary data', () => {
it('Should work with Uint8Array', () => {
const obj = {
name: 'index.pdf',
data: new Uint8Array([1, 3, 4]),
} satisfies File.AsObject;
const expected = new File();
expected.setName(obj.name);
expected.setData(obj.data);
const file = fromProtobufObject(File, obj);
expect(file).toEqual(expected);
});


it('Should work with string', () => {
const obj = {
name: 'index.pdf',
data: 'teeeeestttt',
} satisfies File.AsObject;
const file = fromProtobufObject(File, obj);
expect(file.toObject()).toEqual(obj);
});
})
6 changes: 6 additions & 0 deletions __tests__/test-data/file.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

message File {
string name = 1;
bytes data = 2;
}
1 change: 1 addition & 0 deletions __tests__/test-data/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ gen ./forest.proto
gen ./universe.proto
gen ./spices.proto
gen ./newspaper.proto
gen ./file.proto
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"eslint": "^8.32.0",
"jest": "^29.3.1",
"protoc-gen-grpc-web": "^1.4.2",
"protoc-gen-js": "^3.21.2",
"ts-jest": "^29.0.5",
"typescript": "^4.9.4"
},
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export function fromProtobufObject<T extends Message>(MessageType: MessageConstr
}

function getResult<T extends Message>(instance: T, prop: string, value: unknown): unknown {
if (value instanceof Uint8Array) {
return value;
}
if (Array.isArray(value)) {
if (value.length === 0 || !isArrayOfObjects(value, prop)) {
return value;
Expand Down Expand Up @@ -130,7 +133,7 @@ function validateType<T extends Message>(instance: T, prop: string, value: unkno
const getter = getMethod(prop, PREFIX.GET);
const instanceValue = callMethod(instance, getter);
const expectedType = instanceValue !== undefined ? typeof instanceValue : 'object';
const actualType = typeof value;
const actualType = value instanceof Uint8Array ? 'string' : typeof value;
if (Array.isArray(instanceValue) && !Array.isArray(value)) {
throw new Error(`Invalid type for '${prop}' (expected array, got '${actualType}')`);
}
Expand Down

0 comments on commit bceae90

Please sign in to comment.