diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 40e02e4..a919632 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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', () => { @@ -245,4 +246,28 @@ describe('Map rule', () => { const newspaper = fromProtobufObject(Newspaper, obj); expect(newspaper.toObject()).toEqual(obj); }); -}); \ No newline at end of file +}); + +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); + }); +}) \ No newline at end of file diff --git a/__tests__/test-data/file.proto b/__tests__/test-data/file.proto new file mode 100644 index 0000000..af44d40 --- /dev/null +++ b/__tests__/test-data/file.proto @@ -0,0 +1,6 @@ +syntax = "proto3"; + +message File { + string name = 1; + bytes data = 2; +} diff --git a/__tests__/test-data/generate.sh b/__tests__/test-data/generate.sh index 6400833..3e15053 100644 --- a/__tests__/test-data/generate.sh +++ b/__tests__/test-data/generate.sh @@ -17,3 +17,4 @@ gen ./forest.proto gen ./universe.proto gen ./spices.proto gen ./newspaper.proto +gen ./file.proto diff --git a/package-lock.json b/package-lock.json index 88f8ea7..1187cf6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,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" }, @@ -1630,6 +1631,15 @@ "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, + "node_modules/adm-zip": { + "version": "0.5.10", + "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.10.tgz", + "integrity": "sha512-x0HvcHqVJNTPk/Bw8JbLWlWoo6Wwnsug0fnYYro1HBrjxZ3G7/AZk7Ahv8JwDe1uIcz8eBqvu86FuF1POiG7vQ==", + "dev": true, + "engines": { + "node": ">=6.0" + } + }, "node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -5019,6 +5029,20 @@ "protoc-gen-grpc-web": "cli.js" } }, + "node_modules/protoc-gen-js": { + "version": "3.21.2", + "resolved": "https://registry.npmjs.org/protoc-gen-js/-/protoc-gen-js-3.21.2.tgz", + "integrity": "sha512-nSpiXulygg0vUv05uFeATuZSbgMQMeoef0BhB5266Y6HmsqVtIrbSkK/Z2Yk0KLE+BirRNjsTKDUJxg3OPO9pQ==", + "dev": true, + "hasInstallScript": true, + "dependencies": { + "adm-zip": "^0.5.10", + "download": "^8.0.0" + }, + "bin": { + "protoc-gen-js": "cli.js" + } + }, "node_modules/pump": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", diff --git a/package.json b/package.json index fc04190..df3cb4a 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/index.ts b/src/index.ts index 49b390c..95ddf89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,6 +39,9 @@ export function fromProtobufObject(MessageType: MessageConstr } function getResult(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; @@ -130,7 +133,7 @@ function validateType(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}')`); }