Skip to content

Commit

Permalink
Support the dataBinary action in the openedFile event
Browse files Browse the repository at this point in the history
  • Loading branch information
shalithasuranga committed Sep 19, 2023
1 parent 5791908 commit 6d76072
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/api/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sendMessage } from '../ws/websocket';
import { base64ToBytesArray } from '../helpers';

export interface DirectoryEntry {
entry: string;
Expand Down Expand Up @@ -68,13 +69,7 @@ export function readBinaryFile(path: string, options?: FileReaderOptions): Promi
return new Promise((resolve: any, reject: any) => {
sendMessage('filesystem.readBinaryFile', { path, ...options })
.then((base64Data: string) => {
let binaryData: string = window.atob(base64Data);
let len: number = binaryData.length;
let bytes: Uint8Array = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryData.charCodeAt(i);
}
resolve(bytes.buffer);
resolve(base64ToBytesArray(base64Data));
})
.catch((error: any) => {
reject(error);
Expand Down
11 changes: 11 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function base64ToBytesArray(data: string): ArrayBuffer {
let binaryData: string = window.atob(data);
let len: number = binaryData.length;
let bytes: Uint8Array = new Uint8Array(len);

for (let i = 0; i < len; i++) {
bytes[i] = binaryData.charCodeAt(i);
}

return bytes.buffer;
}
4 changes: 4 additions & 0 deletions src/ws/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as extensions from '../api/extensions';
import * as events from '../browser/events';
import { base64ToBytesArray } from '../helpers';

let ws;
let nativeCalls = {};
Expand Down Expand Up @@ -103,6 +104,9 @@ function registerSocketEvents() {
}
else if(message.event) {
// Event from process
if(message.event == 'openedFile' && message?.data?.action == 'dataBinary') {
message.data.data = base64ToBytesArray(message.data.data);
}
events.dispatch(message.event, message.data);
}
});
Expand Down

0 comments on commit 6d76072

Please sign in to comment.