Update pipe-ipc.js

This commit is contained in:
Namhyeon Go 2023-09-18 23:25:18 +09:00 committed by GitHub
parent 145be2a894
commit 4a3d044f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -54,9 +54,8 @@ var CdoUTF_7 = "utf-7";
var CdoUTF_8 = "utf-8";
function PipeIPC() {
var EOL = "\r\n";
this.path = "data\\.pipe_:pipename";
this.delimiter = "\r\n";
this.reader = null;
this.writer = null;
this.recorder = null;
@ -65,6 +64,7 @@ function PipeIPC() {
this.charset = CdoUTF_8;
this.lastReadTime = -1;
this.lastWriteTime = -1;
this.maxSentences = 0;
this.getCurrentTime = function() {
return new Date().getTime();
@ -74,6 +74,14 @@ function PipeIPC() {
this.charset = charset;
};
this.setDelimiter = function(delimiter) {
this.delimiter = delimiter;
};
this.setMaxSentences = function(maxSentences) {
this.maxSentences = maxSentences;
};
this.connect = function(pipename, callback) {
this.path = this.path.replace(":pipename", pipename);
//this.openWriter();
@ -141,7 +149,7 @@ function PipeIPC() {
};
this._write = function(message) {
this.writer.Write(message + EOL);
this.writer.Write(message + this.delimiter);
};
this.write = function(message) {
@ -172,11 +180,12 @@ function PipeIPC() {
};
this.record = function(message) {
var _this = this; // Because of the variable scope
var commit = function(src, dst, charset) {
// Open a temporary file
var fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForReading, true, TristateTrue);
var str = fso.ReadAll();
var StripBOM = function(adoObj) {
var isCommited = false;
// define functions
var StripAdoBOM = function(adoObj) {
var newAdoObj = CreateObject("ADODB.Stream");
newAdoObj.Type = adTypeBinary;
newAdoObj.Mode = adModeReadWrite;
@ -184,20 +193,52 @@ function PipeIPC() {
adoObj.Position = 3;
adoObj.CopyTo(newAdoObj);
adoObj.Flush();
//adoObj.Close();
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);
StripBOM(ado).SaveToFile(dst, adSaveCreateOverWrite);
//ado.SaveToFile(savefile, adSaveCreateOverWrite);
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();
};
@ -264,7 +305,6 @@ function PipeIPC() {
this.closeReader();
this.closeRecorder();
};
}
exports.create = function() {
@ -278,8 +318,9 @@ exports.connect = function(path, callback) {
exports.ForReading = ForReading;
exports.ForWriting = ForWriting;
exports.ForAppending = ForAppending;
exports.CdoUTF_8 = CdoUTF_8;
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.4";
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.5";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = require;