kopher

A simple Gopher client for KaiOS
git clone git://git.luxferre.top/kopher.git
Log | Files | Refs | README | LICENSE

transport.js (1155B)


      1 // KaiOS 2.x specific part to connect and retrieve raw data from Gopher servers
      2 // Requires "tcp-socket" permission in the manifest
      3 // and the "privileged" or "certified" app permission level
      4 // resource format: [type, selector, host, port]
      5 // type is autotrimmed, so you don't have to manually strip the description
      6 // input is optional and only considered for resource type 7
      7 
      8 function gopherRequest(resource, input, successCb, errorCb) {
      9   var xsock = navigator.mozTCPSocket.open(resource[2], (resource[3]||70) | 0, {binaryType: 'arraybuffer'}),
     10       type = resource[0][0], databuf = []
     11   xsock.ondata = function(e) {
     12     var i = 0, db = new Uint8Array(e.data), l = db.length
     13     for(;i<l;i++)
     14       databuf.push(db[i])
     15   }
     16   xsock.onclose = function() {
     17     var res = new Uint8Array(databuf)
     18     successCb(res, type)
     19     databuf = []
     20   }
     21   xsock.onerror = errorCb
     22   xsock.onopen = function() {
     23     var selpath = resource[1]
     24     if(type == '7' && input)
     25       selpath += '\t' + input
     26     var reqbuf = (new TextEncoder).encode(selpath + '\r\n').buffer
     27     xsock.send(reqbuf) // send CRLF-terminated selector path and optionally the search string
     28   }
     29 }