Update pipe-ipc.js

This commit is contained in:
Namhyeon Go 2023-09-19 15:54:31 +09:00 committed by GitHub
parent 4a3d044f02
commit 3baaf8dd2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,6 +53,15 @@ var CdoUS_ASCII = "us-ascii";
var CdoUTF_7 = "utf-7";
var CdoUTF_8 = "utf-8";
function createUUIDv4() {
sleep(1);
var randomize = Math.random;
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = randomize() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function PipeIPC() {
this.path = "data\\.pipe_:pipename";
this.delimiter = "\r\n";
@ -69,6 +78,7 @@ function PipeIPC() {
this.getCurrentTime = function() {
return new Date().getTime();
};
this.setCharset = function(charset) {
this.charset = charset;
@ -122,10 +132,10 @@ function PipeIPC() {
}
};
this.startRecorder = function(filename) {
this.startRecorder = function(filename, iomode) {
this.savefile = filename;
this.tmpfile = this.savefile + ".tmp";
this.openRecorder();
this.openRecorder(iomode);
};
this.stopRecorder = function() {
@ -133,10 +143,10 @@ function PipeIPC() {
this.tmpfile = null;
};
this.openRecorder = function() {
this.openRecorder = function(iomode) {
while (this.recorder == null) {
try {
this.recorder = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.tmpfile, ForAppending, true, TristateTrue);
this.recorder = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.tmpfile, iomode, true, TristateTrue);
} catch (e) {}
}
};
@ -180,76 +190,13 @@ function PipeIPC() {
};
this.record = function(message) {
var _this = this; // Because of the variable scope
var commit = function(src, dst, charset) {
var isCommited = false;
// define functions
var StripAdoBOM = function(adoObj) {
var newAdoObj = CreateObject("ADODB.Stream");
newAdoObj.Type = adTypeBinary;
newAdoObj.Mode = adModeReadWrite;
newAdoObj.Open();
adoObj.Position = 3;
adoObj.CopyTo(newAdoObj);
adoObj.Flush();
adoObj.Close();
return newAdoObj;
};
while (!isCommited) {
try {
// Open a temporary file
var fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForReading, true, TristateTrue);
var text = fso.ReadAll();
var sentences = text.split(_this.delimiter);
var newSentences = [], str = '';
// if enabled "maxSentences" feature
if (_this.maxSentences > 0) {
while (sentences.length > 0 && newSentences.length < _this.maxSentences) {
newSentences.push(sentences.pop());
}
str = newSentences.reverse().join(_this.delimiter);
} else {
str = text;
}
// Convert UTF-16 BOM to UTF-8
var ado = CreateObject("ADODB.Stream");
ado.Type = adTypeText;
ado.Charset = charset;
ado.Open();
ado.WriteText(str);
ado = StripAdoBOM(ado);
ado.SaveToFile(dst, adSaveCreateOverWrite);
ado.Close();
/*
// Write a new temporary file
var _fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForWriting, true, TristateTrue);
_fso.Write(str);
_fso.Close();
*/
// Set a result
isCommited = true;
} catch (e) {
isCommited = false;
}
}
// Close a temporary file
fso.Close();
};
var isRecorded = false;
while (!isRecorded) {
try {
this.openRecorder();
this._record(message);
this.closeRecorder();
commit(this.tmpfile, this.savefile, this.charset);
this.commit(this.savefile);
isRecorded = true;
} catch (e) {
//console.log(e.message);
@ -261,6 +208,73 @@ function PipeIPC() {
return isRecorded;
};
this.commit = function(dst) {
var src = this.tmpfile;
var charset = this.charset;
var isCommited = false;
// define functions
var StripAdoBOM = function(adoObj) {
var newAdoObj = CreateObject("ADODB.Stream");
newAdoObj.Type = adTypeBinary;
newAdoObj.Mode = adModeReadWrite;
newAdoObj.Open();
adoObj.Position = 3;
adoObj.CopyTo(newAdoObj);
adoObj.Flush();
adoObj.Close();
return newAdoObj;
};
while (!isCommited) {
try {
// Open a temporary file
var fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForReading, true, TristateTrue);
var text = fso.ReadAll();
var sentences = text.split(this.delimiter);
var newSentences = [], str = '';
// if enabled "maxSentences" feature
if (this.maxSentences > 0) {
while (sentences.length > 0 && newSentences.length < this.maxSentences) {
newSentences.push(sentences.pop());
}
str = newSentences.reverse().join(this.delimiter);
} else {
str = text;
}
// Convert UTF-16 BOM to UTF-8
var ado = CreateObject("ADODB.Stream");
ado.Type = adTypeText;
ado.Charset = charset;
ado.Open();
ado.WriteText(str);
ado = StripAdoBOM(ado);
ado.SaveToFile(dst, adSaveCreateOverWrite);
ado.Close();
// Write a new temporary file
/*
var _fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForWriting, true, TristateTrue);
_fso.Write(str);
_fso.Close();
*/
// Close the temporary file
fso.Close();
// Set a result
isCommited = true;
} catch (e) {
isCommited = false;
}
}
return isCommited;
};
this.flush = function() {
var isFlushed = false;
@ -305,6 +319,43 @@ function PipeIPC() {
this.closeReader();
this.closeRecorder();
};
this.destroy = function() {
this.close();
CreateObject("Scripting.FileSystemObject").DeleteFile(this.path);
};
this.readTextFromFile = function(filename) {
var text = '';
var isLoaded = false;
var isFileExists = CreateObject("Scripting.FileSystemObject").FileExists(filename);
if (isFileExists) {
//console.info("File", filename, "exists");
while (!isLoaded) {
try {
var ado = CreateObject("ADODB.Stream");
ado.CharSet = this.charset;
ado.Open();
ado.LoadFromFile(filename);
text += ado.ReadText();
ado.Close();
isLoaded = true;
} catch (e) {
isLoaded = false;
}
}
} else {
console.warn("File", filename, "not exists");
}
return text;
};
this.loadFromFile = function(filename) {
return this.readTextFromFile(filename);
};
}
exports.create = function() {
@ -312,15 +363,26 @@ exports.create = function() {
};
exports.connect = function(path, callback) {
return exports.create().connect(path, callback);
var pipe = exports.create();
return pipe.connect(path, callback);
};
exports.createUUIDv4 = createUUIDv4;
exports.ForReading = ForReading;
exports.ForWriting = ForWriting;
exports.ForAppending = ForAppending;
exports.CdoUTF_8 = CdoUTF_8;
exports.CdoUS_ASCII = CdoUS_ASCII;
exports.CdoEUC_KR = CdoEUC_KR;
exports.CdoEUC_JP = CdoEUC_JP;
exports.adTypeBinary = adTypeBinary;
exports.adTypeText = adTypeText;
exports.adSaveCreateNotExist = adSaveCreateNotExist;
exports.adSaveCreateOverWrite = adSaveCreateOverWrite;
exports.adModeReadWrite = adModeReadWrite;
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.5";
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.6";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = require;