@@ -6,6 +6,21 @@ const { exec } = require('child_process');
66
77const IS_MACOS = os . platform ( ) === 'darwin' ;
88
9+ /**
10+ * Converts a buffer to an `ArrayBuffer`.
11+ *
12+ * @param {Buffer } buf The buffer to convert
13+ * @return {ArrayBuffer } Converted buffer
14+ * @public
15+ */
16+ function toArrayBuffer ( buf ) {
17+ if ( buf . length === buf . buffer . byteLength ) {
18+ return buf . buffer ;
19+ }
20+
21+ return buf . buffer . slice ( buf . byteOffset , buf . byteOffset + buf . length ) ;
22+ }
23+
924function getWindowsDrives ( callback ) {
1025 exec ( 'wmic logicaldisk get name' , ( error , stdout ) => {
1126 if ( error ) {
@@ -89,7 +104,8 @@ const WS_COMMAND = {
89104 CONTROL_SOCKET_ANNOUNCE : "controlSock" ,
90105 GET_WINDOWS_DRIVES : "getWinDrives" ,
91106 READ_DIR : "readDir" ,
92- STAT : "stat"
107+ STAT : "stat" ,
108+ READ_BIN_FILE : "readBinFile"
93109} ;
94110
95111const LARGE_DATA_THRESHOLD = 2 * 1024 * 1024 ; // 2MB
@@ -196,6 +212,14 @@ function _stat(ws, metadata) {
196212 } ) . catch ( ( err ) => _reportError ( ws , metadata , err , `Failed to get stat for ${ fullPath } ` ) ) ;
197213}
198214
215+ function _readBinaryFile ( ws , metadata ) {
216+ const fullPath = metadata . data . path ;
217+ fs . readFile ( fullPath )
218+ . then ( data => {
219+ _sendResponse ( ws , metadata , { } , toArrayBuffer ( data ) ) ;
220+ } ) . catch ( ( err ) => _reportError ( ws , metadata , err , `Failed to read file at path ${ fullPath } ` ) ) ;
221+ }
222+
199223function processWSCommand ( ws , metadata , dataBuffer ) {
200224 try {
201225 switch ( metadata . commandCode ) {
@@ -214,6 +238,9 @@ function processWSCommand(ws, metadata, dataBuffer) {
214238 case WS_COMMAND . STAT :
215239 _stat ( ws , metadata ) ;
216240 return ;
241+ case WS_COMMAND . READ_BIN_FILE :
242+ _readBinaryFile ( ws , metadata ) ;
243+ return ;
217244 case WS_COMMAND . LARGE_DATA_SOCKET_ANNOUNCE :
218245 console . log ( "Large Data Transfer Socket established, socket Group: " , metadata . socketGroupID ) ;
219246 ws . isLargeData = true ;
0 commit comments