Skip to content

Commit 52e768f

Browse files
committed
fix(core/interaction-output): return an ArrayBuffer in the arrayBuffer method
1 parent bd027fb commit 52e768f

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

packages/core/src/content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Content {
2525
this.body = body;
2626
}
2727

28-
toBuffer(): Promise<Buffer> {
28+
toBuffer(): Promise<Buffer<ArrayBuffer>> {
2929
return ProtocolHelpers.readStreamFully(this.body);
3030
}
3131
}

packages/core/src/interaction-output.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ addFormats(ajv);
3737
export class InteractionOutput implements WoT.InteractionOutput {
3838
#content: Content;
3939
#value: unknown;
40+
#valueBuffer?: Buffer<ArrayBuffer>;
4041
#buffer?: ArrayBuffer;
4142
#stream?: ReadableStream;
4243
dataUsed: boolean;
@@ -73,11 +74,16 @@ export class InteractionOutput implements WoT.InteractionOutput {
7374
throw new Error("Can't read the stream once it has been already used");
7475
}
7576

76-
const data = await this.#content.toBuffer();
77+
const data = this.#valueBuffer ?? (await this.#content.toBuffer());
7778
this.dataUsed = true;
78-
// this.#buffer = new Uint8Array(data).buffer
79-
this.#buffer = data as unknown as ArrayBuffer;
79+
const isPooled = data.byteLength < 4096 && data.buffer.byteLength > data.byteLength;
8080

81+
if (isPooled) {
82+
this.#buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
83+
} else {
84+
this.#buffer = data.buffer;
85+
}
86+
//this.#buffer = data as unknown as ArrayBuffer
8187
return this.#buffer;
8288
}
8389

@@ -121,8 +127,7 @@ export class InteractionOutput implements WoT.InteractionOutput {
121127
// read fully the stream
122128
const bytes = await this.#content.toBuffer();
123129
this.dataUsed = true;
124-
// this.#buffer = new Uint8Array(bytes).buffer;
125-
this.#buffer = bytes as unknown as ArrayBuffer;
130+
this.#valueBuffer = bytes;
126131

127132
const json = ContentSerdes.get().contentToValue({ type: this.#content.type, body: bytes }, this.schema);
128133

packages/core/src/protocol-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ export default class ProtocolHelpers {
233233
return result;
234234
}
235235

236-
static readStreamFully(stream: NodeJS.ReadableStream): Promise<Buffer> {
237-
return new Promise<Buffer>((resolve, reject) => {
236+
static readStreamFully(stream: NodeJS.ReadableStream): Promise<Buffer<ArrayBuffer>> {
237+
return new Promise((resolve, reject) => {
238238
if (stream != null) {
239239
const chunks: Array<unknown> = [];
240240
stream.on("data", (data) => chunks.push(data));

packages/core/test/interaction-output-test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class InteractionOutputTests {
3434

3535
const out = new InteractionOutput(content, {});
3636
const result = await out.arrayBuffer();
37-
expect(result).be.deep.equals(Buffer.from([1, 2, 3]));
37+
38+
expect(result).instanceOf(ArrayBuffer);
39+
expect(Buffer.from(result)).be.deep.equals(Buffer.from([1, 2, 3]));
3840
}
3941

4042
@test async "should be readable with Streams"() {

0 commit comments

Comments
 (0)