Skip to content

Commit

Permalink
fix(util): deserialize bytes as Uint8Array instead of Buffer
Browse files Browse the repository at this point in the history
Buffer is Node.js-specific

ArrayBuffer and it's views is available in web environments as well
  • Loading branch information
mkazlauskas committed Sep 14, 2023
1 parent 9331d01 commit 78460d3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 150 deletions.
8 changes: 4 additions & 4 deletions packages/cardano-services-client/test/HttpProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import path from 'path';

const packageJson = require(path.join(__dirname, '..', 'package.json'));

type ComplexArg2 = { map: Map<string, Buffer> };
type ComplexResponse = Map<bigint, Buffer>[];
type ComplexArg2 = { map: Map<string, Uint8Array> };
type ComplexResponse = Map<bigint, Uint8Array>[];
interface TestProvider extends Provider {
noArgsEmptyReturn(): Promise<void>;
complexArgsAndReturn({ arg1, arg2 }: { arg1: bigint; arg2: ComplexArg2 }): Promise<ComplexResponse>;
Expand Down Expand Up @@ -95,8 +95,8 @@ describe('createHttpProvider', () => {
describe('method with complex args and return', () => {
it('serializes args and deserializes response using core serializableObject', async () => {
const arg1 = 123n;
const arg2: ComplexArg2 = { map: new Map([['key', Buffer.from('abc')]]) };
const expectedResponse: ComplexResponse = [new Map([[1234n, Buffer.from('response data')]])];
const arg2: ComplexArg2 = { map: new Map([['key', new Uint8Array(Buffer.from('abc'))]]) };
const expectedResponse: ComplexResponse = [new Map([[1234n, new Uint8Array(Buffer.from('response data'))]])];
const provider = createTxSubmitProviderClient();
closeServer = await createStubHttpProviderServer(port, stubProviderPaths.complexArgsAndReturn, (req, res) => {
expect(fromSerializableObject(req.body)).toEqual({ arg1, arg2 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,15 @@ Array [
"witness": Object {
"bootstrap": Array [],
"datums": Array [
Object {
"data": Array [],
"type": "Buffer",
},
Uint8Array [],
],
"redeemers": Array [
Object {
"data": Object {
"data": Array [
77,
53,
122,
],
"type": "Buffer",
},
"data": Uint8Array [
77,
53,
122,
],
"executionUnits": Object {
"memory": 8959327235984229000n,
"steps": 4473903472119932000n,
Expand All @@ -112,21 +106,15 @@ Array [
"cbor": "9f2341e14478c0fcf5ff",
"items": Array [
-4n,
Object {
"data": Array [
225,
],
"type": "Buffer",
},
Object {
"data": Array [
120,
192,
252,
245,
],
"type": "Buffer",
},
Uint8Array [
225,
],
Uint8Array [
120,
192,
252,
245,
],
],
},
},
Expand All @@ -137,14 +125,11 @@ Array [
"fields": Object {
"cbor": "9f438002ab0104ff",
"items": Array [
Object {
"data": Array [
128,
2,
171,
],
"type": "Buffer",
},
Uint8Array [
128,
2,
171,
],
1n,
4n,
],
Expand All @@ -153,15 +138,12 @@ Array [
Object {
"cbor": "a1448858788900",
"data": Map {
Object {
"data": Array [
136,
88,
120,
137,
],
"type": "Buffer",
} => 0n,
Uint8Array [
136,
88,
120,
137,
] => 0n,
},
},
],
Expand All @@ -175,10 +157,7 @@ Array [
"cbor": "a0",
"data": Map {},
},
Object {
"data": Array [],
"type": "Buffer",
},
Uint8Array [],
],
},
],
Expand Down
144 changes: 48 additions & 96 deletions packages/ogmios/test/ogmiosToCore/__snapshots__/block.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,41 +138,29 @@ Object {
"data": Object {
"cbor": "a34160029f20a3419c204386a48244e21bd936202004a0ffa3a4435aed90040242b07a210523409f414d429b3f0440ffa09f034001ffa222234041b4d8668218da9f2341440302413eff4204e1a1d866821901939f23ffa0",
"data": Map {
Object {
"data": Array [
96,
],
"type": "Buffer",
} => 2n,
Uint8Array [
96,
] => 2n,
Object {
"cbor": "9f20a3419c204386a48244e21bd936202004a0ff",
"items": Array [
-1n,
Object {
"cbor": "a3419c204386a48244e21bd9362020",
"data": Map {
Object {
"data": Array [
156,
],
"type": "Buffer",
} => -1n,
Object {
"data": Array [
134,
164,
130,
],
"type": "Buffer",
} => Object {
"data": Array [
226,
27,
217,
54,
],
"type": "Buffer",
},
Uint8Array [
156,
] => -1n,
Uint8Array [
134,
164,
130,
] => Uint8Array [
226,
27,
217,
54,
],
-1n => -1n,
},
},
Expand All @@ -188,48 +176,30 @@ Object {
Object {
"cbor": "a4435aed90040242b07a21052340",
"data": Map {
Object {
"data": Array [
90,
237,
144,
],
"type": "Buffer",
} => 4n,
2n => Object {
"data": Array [
176,
122,
],
"type": "Buffer",
},
Uint8Array [
90,
237,
144,
] => 4n,
2n => Uint8Array [
176,
122,
],
-2n => 5n,
-4n => Object {
"data": Array [],
"type": "Buffer",
},
-4n => Uint8Array [],
},
} => Object {
"cbor": "9f414d429b3f0440ff",
"items": Array [
Object {
"data": Array [
77,
],
"type": "Buffer",
},
Object {
"data": Array [
155,
63,
],
"type": "Buffer",
},
Uint8Array [
77,
],
Uint8Array [
155,
63,
],
4n,
Object {
"data": Array [],
"type": "Buffer",
},
Uint8Array [],
],
},
Object {
Expand All @@ -239,26 +209,17 @@ Object {
"cbor": "9f034001ff",
"items": Array [
3n,
Object {
"data": Array [],
"type": "Buffer",
},
Uint8Array [],
1n,
],
},
Object {
"cbor": "a222234041b4",
"data": Map {
-3n => -4n,
Object {
"data": Array [],
"type": "Buffer",
} => Object {
"data": Array [
180,
],
"type": "Buffer",
},
Uint8Array [] => Uint8Array [
180,
],
},
} => Object {
"cbor": "d8668218da9f2341440302413eff",
Expand All @@ -267,32 +228,23 @@ Object {
"cbor": "9f2341440302413eff",
"items": Array [
-4n,
Object {
"data": Array [
68,
],
"type": "Buffer",
},
Uint8Array [
68,
],
3n,
2n,
Object {
"data": Array [
62,
],
"type": "Buffer",
},
Uint8Array [
62,
],
],
},
},
},
},
Object {
"data": Array [
4,
225,
],
"type": "Buffer",
} => Object {
Uint8Array [
4,
225,
] => Object {
"cbor": "a1d866821901939f23ffa0",
"data": Map {
Object {
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/serializableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const fromSerializableObjectUnknown = (obj: unknown, options: FromSerializableOb
case 'bigint':
return BigInt(docAsAny.value);
case 'Buffer':
return Buffer.from(docAsAny.value, 'hex');
return new Uint8Array(Buffer.from(docAsAny.value, 'hex'));
case 'Date':
return new Date(docAsAny.value);
case 'Set':
Expand Down
2 changes: 1 addition & 1 deletion packages/util/test/serializableObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('serializableObject', () => {
it('supports types that are used in SDK, but not natively supported in JSON', () => {
const obj = {
bigint: 123n,
buffer: Buffer.from('data'),
buffer: new Uint8Array(Buffer.from('data')),
date: new Date(),
error: new Error('error obj'),
map: new Map([['key', 1n]]),
Expand Down

0 comments on commit 78460d3

Please sign in to comment.