Update pipe-ipc.js

This commit is contained in:
Namhyeon Go 2023-09-20 19:07:18 +09:00 committed by GitHub
parent 44a9a32775
commit 59e76de85d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,6 +53,19 @@ var CdoUS_ASCII = "us-ascii";
var CdoUTF_7 = "utf-7";
var CdoUTF_8 = "utf-8";
var CRC32Table = (function() {
var c;
var crcTable = [];
for (var n =0; n < 256; n++) {
c = n;
for(var k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTable;
})();
function createUUIDv4() {
var randomize = Math.random;
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
@ -61,6 +74,17 @@ function createUUIDv4() {
});
}
function CRC32(str) {
var crcTable = CRC32Table;
var crc = 0 ^ (-1);
for (var i = 0; i < str.length; i++ ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
}
return ((crc ^ (-1)) >>> 0).toString(16).padStart(8, '0');
}
function createFSO() {
return CreateObject("Scripting.FileSystemObject");
}
@ -161,11 +185,17 @@ function PipeIPC() {
this.tmpfile = this.savefile + ".tmp";
// read a text from save file
var isExistsSaveFile = createFSO().FileExists(this.savefile);
var isExistsTmpFile = createFSO().FileExists(this.tmpfile);
if (!isExistsTmpFile) {
var handler = createFSO().OpenTextFile(this.tmpfile, ForWriting, true, TristateTrue);
handler.Write(this.readTextFromFile(this.savefile));
handler.Close();
while (isExistsSaveFile && !isExistsTmpFile) {
try {
var fso = createFSO().OpenTextFile(this.tmpfile, ForWriting, true, TristateTrue);
fso.Write(this.readTextFromFile(this.savefile));
fso.Close();
isExistsTmpFile = createFSO().FileExists(this.tmpfile);
} catch (e) {
isExistsTmpFile = false;
}
}
// with iomode (default: ForAppending)
@ -280,11 +310,13 @@ function PipeIPC() {
ado.Close();
// Write a new temporary file
/*
if (this.maxSentences > 0) {
var handler = createFSO().OpenTextFile(src, ForWriting, true, TristateTrue);
handler.Write(str);
handler.Close();
}
*/
// Close the temporary file
fso.Close();
@ -393,6 +425,7 @@ exports.connect = function(path, callback) {
};
exports.createUUIDv4 = createUUIDv4;
exports.CRC32 = CRC32;
exports.ForReading = ForReading;
exports.ForWriting = ForWriting;
@ -407,7 +440,7 @@ exports.adSaveCreateNotExist = adSaveCreateNotExist;
exports.adSaveCreateOverWrite = adSaveCreateOverWrite;
exports.adModeReadWrite = adModeReadWrite;
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.10";
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.11";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = require;