初始化提交

This commit is contained in:
王立帮
2024-07-19 10:16:00 +08:00
parent 4c7b571f20
commit 4a2d56dcc4
7084 changed files with 741212 additions and 63 deletions

View File

@@ -0,0 +1,132 @@
importScripts('../../../web-modules/workerpool.min.js');
importScripts('../../../web-modules/browserfs.min.js');
let fs = BrowserFS.fs;
const createPromise = function(func, ...args) {
return new Promise((resolve, reject) => {
func(...args, function() {
resolve([...arguments]);
});
});
}
const addFileSystemHandler = function(filesystem) {
return new Promise((resolve, reject) => {
BrowserFS.configure({
fs: "FileSystemAccess",
options: { handle: filesystem }
}, function (error) {
if (error) {
reject(error);
return;
}
fs = BrowserFS.fs;
resolve('/' + filesystem.name);
});
});
}
const rename = function(oldPath, newPath) {
return createPromise(fs.rename, oldPath, newPath);
}
const stat = function(p) {
return createPromise(fs.stat, p);
}
const open = function(p, flag, mode) {
return createPromise(fs.open, p, flag, mode);
}
const unlink = function(p) {
return createPromise(fs.unlink, p);
}
const rmdir = function(p) {
return createPromise(fs.rmdir, p);
}
const mkdir = function(p, mode) {
return createPromise(fs.mkdir, p, mode);
}
const readdir = function(p) {
return createPromise(fs.readdir, p);
}
const exists = function(p) {
return createPromise(fs.exists, p);
}
const realpath = function(p) {
return createPromise(fs.realpath, p);
}
const truncate = function(p, len) {
return createPromise(fs.truncate, p, len);
}
const readFile = function(fname, encoding, flag) {
return createPromise(fs.readFile, fname, encoding);
}
const writeFile = function(fname, data, encoding, flag, mode) {
return createPromise(fs.writeFile, fname, data, encoding, flag, mode);
}
const appendFile = function(fname, data, encoding, flag, mode) {
return createPromise(fs.appendFile, fname, data, encoding, flag, mode);
}
const chmod = function(p, mode) {
return createPromise(fs.chmod, p, mode);
}
const chown = function(p, new_uid, new_gid) {
return createPromise(fs.chown, p, new_uid, new_gid);
}
const utimes = function(p, atime, mtime) {
return createPromise(fs.utimes, p, atime, mtime);
}
const link = function(srcpath, dstpath) {
return createPromise(fs.link, srcpath, dstpath);
}
const symlink = function(srcpath, dstpath, type) {
return createPromise(fs.symlink, srcpath, dstpath, type);
}
const readlink = function(p) {
return createPromise(fs.readlink, p);
}
const syncClose = function(method, fd) {
return fs.syncClose(method, fd);
}
workerpool.worker({
addFileSystemHandler,
rename,
stat,
open,
unlink,
rmdir,
mkdir,
readdir,
exists,
realpath,
truncate,
readFile,
writeFile,
appendFile,
chmod,
chown,
utimes,
link,
symlink,
readlink,
syncClose
});

View File

@@ -0,0 +1,123 @@
// importScripts('../../web-modules/workerpool.min.js');
const workerpool = require('../../web-modules/workerpool.min.js');
const encoder = new TextEncoder();
const decoder = new TextDecoder('utf-8');
class SerialWorker {
constructor(serial) {
this.serial = serial;
this.receiveBuffer = [];
this.receiveStr = '';
this.bufferLength = 0;
const test = setInterval(() => {
const message = generateRandomString(5);
this.onData(encoder.encode(message));
}, 1000);
setTimeout(() => {
clearInterval(test);
}, 120 * 1000);
}
onOpen() {
}
onData(data) {
/* UTF-8编码方式
* ------------------------------------------------------------
* |1字节 0xxxxxxx |
* |2字节 110xxxxx 10xxxxxx |
* |3字节 1110xxxx 10xxxxxx 10xxxxxx |
* |4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
* |5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
* |6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx|
* ------------------------------------------------------------
*/
for (let i in data) {
if ((data[i] & 0x80) === 0x00) {
// 1字节
this.receiveBuffer = [];
this.bufferLength = 0;
this.receiveStr += String.fromCharCode(data[i]);
} else if ((data[i] & 0xc0) === 0x80) {
// 2字节以上的中间字节10xxxxxx
// 如果没有起始头,则丢弃这个字节
if (!this.receiveBuffer.length) {
return;
}
// 如果不是2字节及以上的起始头则丢弃这个字节
if (this.bufferLength < 2) {
return;
}
this.receiveBuffer.push(data[i]);
if (this.bufferLength === this.receiveBuffer.length) {
this.receiveStr += decoder.decode(new Uint8Array(this.receiveBuffer));
this.receiveBuffer = [];
}
} else {
// 2字节以上的起始头
if (this.receiveBuffer) {
this.receiveBuffer = [];
}
this.bufferLength = this.#getBufferLength(data[i]);
this.receiveBuffer.push(data[i]);
}
}
}
onError() {
}
onClose() {
}
#getBufferLength(data) {
let len = 2;
if ((data & 0xFC) === 0xFC) {
len = 6;
} else if ((data & 0xF8) === 0xF8) {
len = 5;
} else if ((data & 0xF0) === 0xF0) {
len = 4;
} else if ((data & 0xE0) === 0xE0) {
len = 3;
} else if ((data & 0xC0) === 0xC0) {
len = 2;
}
return len;
}
}
const createSerialWork = function(serial) {
return new Promise((resolve, reject) => {
console.log(serial)
const serialWork = new SerialWorker(serial);
const test = setInterval(() => {
const data = serialWork.receiveStr;
serialWork.receiveStr = '';
workerpool.workerEmit({
status: 'data',
data: data
});
}, 5000);
setTimeout(() => {
workerpool.workerEmit({
status: 'close'
});
resolve();
}, 120 * 1000);
});
}
function generateRandomString() {
return '1234';
}
workerpool.worker({
createSerialWork,
generateRandomString
});