Wow, load was slow because websocket sends were slow, because TextEcoder was slow. Do it in C.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3796 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-01-27 01:15:54 +00:00
parent 7b32067b07
commit 9fd4be0e4a
6 changed files with 23 additions and 3358 deletions

View File

@ -121,7 +121,7 @@ function authHandler(request, response) {
response.end(); response.end();
} else { } else {
File.readFile("core/auth.html").then(function(data) { File.readFile("core/auth.html").then(function(data) {
var html = new TextDecoder("UTF-8").decode(data); var html = utf8Decode(data);
var contents = ""; var contents = "";
if (entry) { if (entry) {

View File

@ -1,8 +1,5 @@
"use strict"; "use strict";
require("encoding-indexes");
require("encoding");
var auth = require("auth"); var auth = require("auth");
var app = require("app"); var app = require("app");

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -200,7 +200,7 @@ function handleWebSocketRequest(request, response, client) {
opCode = 0x2; opCode = 0x2;
} }
if (opCode == 0x1 && (typeof message == "string" || message instanceof String)) { if (opCode == 0x1 && (typeof message == "string" || message instanceof String)) {
message = new TextEncoder("UTF-8").encode(message); message = utf8Encode(message);
} }
var fin = true; var fin = true;
var packet = [(fin ? (1 << 7) : 0) | (opCode & 0xf)]; var packet = [(fin ? (1 << 7) : 0) | (opCode & 0xf)];
@ -291,7 +291,7 @@ function handleWebSocketRequest(request, response, client) {
if (fin) { if (fin) {
if (response.onMessage) { if (response.onMessage) {
response.onMessage({ response.onMessage({
data: frameOpCode == 0x1 ? new TextDecoder("UTF-8").decode(frame) : frame, data: frameOpCode == 0x1 ? utf8Decode(frame) : frame,
opCode: frameOpCode, opCode: frameOpCode,
}); });
} }
@ -376,7 +376,7 @@ function handleConnection(client) {
function handleLine(line, length) { function handleLine(line, length) {
if (bodyToRead == -1) { if (bodyToRead == -1) {
line = new TextDecoder("ASCII").decode(line); line = utf8Decode(line);
if (!request) { if (!request) {
request = line.split(' '); request = line.split(' ');
return true; return true;
@ -406,7 +406,7 @@ function handleConnection(client) {
} }
} }
} else { } else {
line = new TextDecoder("UTF-8").decode(line); line = utf8Decode(line);
body += line; body += line;
bodyToRead -= length; bodyToRead -= length;
if (bodyToRead <= 0) { if (bodyToRead <= 0) {
@ -489,8 +489,8 @@ if (tildefriends.https_port) {
tls.keyStat.mtime != stat[1].mtime || tls.keyStat.mtime != stat[1].mtime ||
tls.keyStat.size != stat[1].size) { tls.keyStat.size != stat[1].size) {
print("Reloading " + kCertificatePath + " and " + kPrivateKeyPath); print("Reloading " + kCertificatePath + " and " + kPrivateKeyPath);
var privateKey = new TextDecoder("ASCII").decode(await File.readFile(kPrivateKeyPath)); var privateKey = utf8Decode(await File.readFile(kPrivateKeyPath));
var certificate = new TextDecoder("ASCII").decode(await File.readFile(kCertificatePath)); var certificate = utf8Decode(await File.readFile(kCertificatePath));
tls.context = new TlsContext(); tls.context = new TlsContext();
tls.context.setPrivateKey(privateKey); tls.context.setPrivateKey(privateKey);

View File

@ -9,6 +9,21 @@
#include <string.h> #include <string.h>
static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
size_t length = 0;
const char* value = JS_ToCStringLen(context, &length, argv[0]);
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (const uint8_t*)value, length);
JSValue global = JS_GetGlobalObject(context);
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
JS_FreeValue(context, global);
JSValue typedArray = JS_CallConstructor(context, constructor, 1, &arrayBuffer);
JS_FreeValue(context, constructor);
JS_FreeValue(context, arrayBuffer);
JS_FreeCString(context, value);
return typedArray;
}
static JSValue _util_utf8_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) static JSValue _util_utf8_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{ {
JSValue result = JS_NULL; JSValue result = JS_NULL;
@ -184,6 +199,7 @@ void tf_util_register(JSContext* context)
{ {
JSValue global = JS_GetGlobalObject(context); JSValue global = JS_GetGlobalObject(context);
JS_SetPropertyStr(context, global, "utf8Decode", JS_NewCFunction(context, _util_utf8_decode, "utf8Decode", 1)); JS_SetPropertyStr(context, global, "utf8Decode", JS_NewCFunction(context, _util_utf8_decode, "utf8Decode", 1));
JS_SetPropertyStr(context, global, "utf8Encode", JS_NewCFunction(context, _util_utf8_encode, "utf8Encode", 1));
JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1)); JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1));
JS_SetPropertyStr(context, global, "setTimeout", JS_NewCFunction(context, _util_setTimeout, "setTimeout", 2)); JS_SetPropertyStr(context, global, "setTimeout", JS_NewCFunction(context, _util_setTimeout, "setTimeout", 2));
JS_FreeValue(context, global); JS_FreeValue(context, global);