forked from joewalnes/websocketd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c6d5c9
commit 0c483c6
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env -S qjs --module | ||
import * as std from "std"; | ||
|
||
var utf8ArrayToStr = (function() { | ||
var charCache = new Array(128); // Preallocate the cache for the common single byte chars | ||
var charFromCodePt = String.fromCodePoint || String.fromCharCode; | ||
var result = []; | ||
|
||
return function(array) { | ||
var codePt, byte1; | ||
var buffLen = array.length; | ||
|
||
result.length = 0; | ||
|
||
for (var i = 0; i < buffLen;) { | ||
byte1 = array[i++]; | ||
|
||
if (byte1 <= 0x7f) { | ||
codePt = byte1; | ||
} else if (byte1 <= 0xdf) { | ||
codePt = ((byte1 & 0x1f) << 6) | (array[i++] & 0x3f); | ||
} else if (byte1 <= 0xef) { | ||
codePt = | ||
((byte1 & 0x0f) << 12) | | ||
((array[i++] & 0x3f) << 6) | | ||
(array[i++] & 0x3f); | ||
} else if (String.fromCodePoint) { | ||
codePt = | ||
((byte1 & 0x07) << 18) | | ||
((array[i++] & 0x3f) << 12) | | ||
((array[i++] & 0x3f) << 6) | | ||
(array[i++] & 0x3f); | ||
} else { | ||
codePt = 63; // Cannot convert four byte code points, so use "?" instead | ||
i += 3; | ||
} | ||
|
||
result.push( | ||
charCache[codePt] || | ||
(charCache[codePt] = charFromCodePt(codePt)) | ||
); | ||
} | ||
|
||
return result.join(""); | ||
}; | ||
})(); | ||
|
||
function main() { | ||
let result = []; | ||
let chunk; | ||
while (std.in.eof() != true ) { | ||
chunk = std.in.getByte(); | ||
if (chunk > 0) { | ||
result.push(chunk); | ||
if( chunk == 10 ){ | ||
console.log( "RCVD: "+utf8ArrayToStr(result).replace(/\n/g,'') ) | ||
std.out.flush() | ||
result = [] | ||
} | ||
} | ||
} | ||
} | ||
|
||
main(); |