Skip to content

Commit 78460d3

Browse files
committed
fix(util): deserialize bytes as Uint8Array instead of Buffer
Buffer is Node.js-specific ArrayBuffer and it's views is available in web environments as well
1 parent 9331d01 commit 78460d3

File tree

5 files changed

+81
-150
lines changed

5 files changed

+81
-150
lines changed

packages/cardano-services-client/test/HttpProvider.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import path from 'path';
1111

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

14-
type ComplexArg2 = { map: Map<string, Buffer> };
15-
type ComplexResponse = Map<bigint, Buffer>[];
14+
type ComplexArg2 = { map: Map<string, Uint8Array> };
15+
type ComplexResponse = Map<bigint, Uint8Array>[];
1616
interface TestProvider extends Provider {
1717
noArgsEmptyReturn(): Promise<void>;
1818
complexArgsAndReturn({ arg1, arg2 }: { arg1: bigint; arg2: ComplexArg2 }): Promise<ComplexResponse>;
@@ -95,8 +95,8 @@ describe('createHttpProvider', () => {
9595
describe('method with complex args and return', () => {
9696
it('serializes args and deserializes response using core serializableObject', async () => {
9797
const arg1 = 123n;
98-
const arg2: ComplexArg2 = { map: new Map([['key', Buffer.from('abc')]]) };
99-
const expectedResponse: ComplexResponse = [new Map([[1234n, Buffer.from('response data')]])];
98+
const arg2: ComplexArg2 = { map: new Map([['key', new Uint8Array(Buffer.from('abc'))]]) };
99+
const expectedResponse: ComplexResponse = [new Map([[1234n, new Uint8Array(Buffer.from('response data'))]])];
100100
const provider = createTxSubmitProviderClient();
101101
closeServer = await createStubHttpProviderServer(port, stubProviderPaths.complexArgsAndReturn, (req, res) => {
102102
expect(fromSerializableObject(req.body)).toEqual({ arg1, arg2 });

packages/ogmios/test/CardanoNode/__snapshots__/ObservableOgmiosCardanoNode.test.ts.snap

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,15 @@ Array [
7373
"witness": Object {
7474
"bootstrap": Array [],
7575
"datums": Array [
76-
Object {
77-
"data": Array [],
78-
"type": "Buffer",
79-
},
76+
Uint8Array [],
8077
],
8178
"redeemers": Array [
8279
Object {
83-
"data": Object {
84-
"data": Array [
85-
77,
86-
53,
87-
122,
88-
],
89-
"type": "Buffer",
90-
},
80+
"data": Uint8Array [
81+
77,
82+
53,
83+
122,
84+
],
9185
"executionUnits": Object {
9286
"memory": 8959327235984229000n,
9387
"steps": 4473903472119932000n,
@@ -112,21 +106,15 @@ Array [
112106
"cbor": "9f2341e14478c0fcf5ff",
113107
"items": Array [
114108
-4n,
115-
Object {
116-
"data": Array [
117-
225,
118-
],
119-
"type": "Buffer",
120-
},
121-
Object {
122-
"data": Array [
123-
120,
124-
192,
125-
252,
126-
245,
127-
],
128-
"type": "Buffer",
129-
},
109+
Uint8Array [
110+
225,
111+
],
112+
Uint8Array [
113+
120,
114+
192,
115+
252,
116+
245,
117+
],
130118
],
131119
},
132120
},
@@ -137,14 +125,11 @@ Array [
137125
"fields": Object {
138126
"cbor": "9f438002ab0104ff",
139127
"items": Array [
140-
Object {
141-
"data": Array [
142-
128,
143-
2,
144-
171,
145-
],
146-
"type": "Buffer",
147-
},
128+
Uint8Array [
129+
128,
130+
2,
131+
171,
132+
],
148133
1n,
149134
4n,
150135
],
@@ -153,15 +138,12 @@ Array [
153138
Object {
154139
"cbor": "a1448858788900",
155140
"data": Map {
156-
Object {
157-
"data": Array [
158-
136,
159-
88,
160-
120,
161-
137,
162-
],
163-
"type": "Buffer",
164-
} => 0n,
141+
Uint8Array [
142+
136,
143+
88,
144+
120,
145+
137,
146+
] => 0n,
165147
},
166148
},
167149
],
@@ -175,10 +157,7 @@ Array [
175157
"cbor": "a0",
176158
"data": Map {},
177159
},
178-
Object {
179-
"data": Array [],
180-
"type": "Buffer",
181-
},
160+
Uint8Array [],
182161
],
183162
},
184163
],

packages/ogmios/test/ogmiosToCore/__snapshots__/block.test.ts.snap

Lines changed: 48 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -138,41 +138,29 @@ Object {
138138
"data": Object {
139139
"cbor": "a34160029f20a3419c204386a48244e21bd936202004a0ffa3a4435aed90040242b07a210523409f414d429b3f0440ffa09f034001ffa222234041b4d8668218da9f2341440302413eff4204e1a1d866821901939f23ffa0",
140140
"data": Map {
141-
Object {
142-
"data": Array [
143-
96,
144-
],
145-
"type": "Buffer",
146-
} => 2n,
141+
Uint8Array [
142+
96,
143+
] => 2n,
147144
Object {
148145
"cbor": "9f20a3419c204386a48244e21bd936202004a0ff",
149146
"items": Array [
150147
-1n,
151148
Object {
152149
"cbor": "a3419c204386a48244e21bd9362020",
153150
"data": Map {
154-
Object {
155-
"data": Array [
156-
156,
157-
],
158-
"type": "Buffer",
159-
} => -1n,
160-
Object {
161-
"data": Array [
162-
134,
163-
164,
164-
130,
165-
],
166-
"type": "Buffer",
167-
} => Object {
168-
"data": Array [
169-
226,
170-
27,
171-
217,
172-
54,
173-
],
174-
"type": "Buffer",
175-
},
151+
Uint8Array [
152+
156,
153+
] => -1n,
154+
Uint8Array [
155+
134,
156+
164,
157+
130,
158+
] => Uint8Array [
159+
226,
160+
27,
161+
217,
162+
54,
163+
],
176164
-1n => -1n,
177165
},
178166
},
@@ -188,48 +176,30 @@ Object {
188176
Object {
189177
"cbor": "a4435aed90040242b07a21052340",
190178
"data": Map {
191-
Object {
192-
"data": Array [
193-
90,
194-
237,
195-
144,
196-
],
197-
"type": "Buffer",
198-
} => 4n,
199-
2n => Object {
200-
"data": Array [
201-
176,
202-
122,
203-
],
204-
"type": "Buffer",
205-
},
179+
Uint8Array [
180+
90,
181+
237,
182+
144,
183+
] => 4n,
184+
2n => Uint8Array [
185+
176,
186+
122,
187+
],
206188
-2n => 5n,
207-
-4n => Object {
208-
"data": Array [],
209-
"type": "Buffer",
210-
},
189+
-4n => Uint8Array [],
211190
},
212191
} => Object {
213192
"cbor": "9f414d429b3f0440ff",
214193
"items": Array [
215-
Object {
216-
"data": Array [
217-
77,
218-
],
219-
"type": "Buffer",
220-
},
221-
Object {
222-
"data": Array [
223-
155,
224-
63,
225-
],
226-
"type": "Buffer",
227-
},
194+
Uint8Array [
195+
77,
196+
],
197+
Uint8Array [
198+
155,
199+
63,
200+
],
228201
4n,
229-
Object {
230-
"data": Array [],
231-
"type": "Buffer",
232-
},
202+
Uint8Array [],
233203
],
234204
},
235205
Object {
@@ -239,26 +209,17 @@ Object {
239209
"cbor": "9f034001ff",
240210
"items": Array [
241211
3n,
242-
Object {
243-
"data": Array [],
244-
"type": "Buffer",
245-
},
212+
Uint8Array [],
246213
1n,
247214
],
248215
},
249216
Object {
250217
"cbor": "a222234041b4",
251218
"data": Map {
252219
-3n => -4n,
253-
Object {
254-
"data": Array [],
255-
"type": "Buffer",
256-
} => Object {
257-
"data": Array [
258-
180,
259-
],
260-
"type": "Buffer",
261-
},
220+
Uint8Array [] => Uint8Array [
221+
180,
222+
],
262223
},
263224
} => Object {
264225
"cbor": "d8668218da9f2341440302413eff",
@@ -267,32 +228,23 @@ Object {
267228
"cbor": "9f2341440302413eff",
268229
"items": Array [
269230
-4n,
270-
Object {
271-
"data": Array [
272-
68,
273-
],
274-
"type": "Buffer",
275-
},
231+
Uint8Array [
232+
68,
233+
],
276234
3n,
277235
2n,
278-
Object {
279-
"data": Array [
280-
62,
281-
],
282-
"type": "Buffer",
283-
},
236+
Uint8Array [
237+
62,
238+
],
284239
],
285240
},
286241
},
287242
},
288243
},
289-
Object {
290-
"data": Array [
291-
4,
292-
225,
293-
],
294-
"type": "Buffer",
295-
} => Object {
244+
Uint8Array [
245+
4,
246+
225,
247+
] => Object {
296248
"cbor": "a1d866821901939f23ffa0",
297249
"data": Map {
298250
Object {

packages/util/src/serializableObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const fromSerializableObjectUnknown = (obj: unknown, options: FromSerializableOb
104104
case 'bigint':
105105
return BigInt(docAsAny.value);
106106
case 'Buffer':
107-
return Buffer.from(docAsAny.value, 'hex');
107+
return new Uint8Array(Buffer.from(docAsAny.value, 'hex'));
108108
case 'Date':
109109
return new Date(docAsAny.value);
110110
case 'Set':

packages/util/test/serializableObject.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('serializableObject', () => {
3232
it('supports types that are used in SDK, but not natively supported in JSON', () => {
3333
const obj = {
3434
bigint: 123n,
35-
buffer: Buffer.from('data'),
35+
buffer: new Uint8Array(Buffer.from('data')),
3636
date: new Date(),
3737
error: new Error('error obj'),
3838
map: new Map([['key', 1n]]),

0 commit comments

Comments
 (0)