Skip to content

Commit 30aedcf

Browse files
committed
Encode buffers into base64 before serializing & sending from client to server
1 parent 6d9a3a5 commit 30aedcf

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

js/src/comm.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { encode } from "base64-arraybuffer";
12
import { Throttler } from "./utils";
23

34
// This class is a striped down version of Comm from @jupyter-widgets/base
@@ -29,11 +30,17 @@ export class ShinyComm {
2930
metadata?: any,
3031
buffers?: ArrayBuffer[] | ArrayBufferView[]
3132
): string {
33+
34+
// Encode buffers as base64 before stringifying the message
35+
const buffers_64 = (buffers || []).map((b: ArrayBuffer | ArrayBufferView) => {
36+
const buffer = b instanceof ArrayBuffer ? b : b.buffer;
37+
return encode(buffer);
38+
});
39+
3240
const msg = {
3341
content: {comm_id: this.comm_id, data: data},
3442
metadata: metadata,
35-
// TODO: need to _encode_ any buffers into base64 (JSON.stringify just drops them)
36-
buffers: buffers || [],
43+
buffers: buffers_64,
3744
// this doesn't seem relevant to the widget?
3845
header: {}
3946
};

shinywidgets/static/output.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)