welsonjs/lib/pipe-ipc.js

327 lines
9.4 KiB
JavaScript
Raw Normal View History

2022-09-27 08:46:27 +00:00
// pipe-ipc.js
2023-09-18 12:06:23 +00:00
// https://github.com/gnh1201/welsonjs
2022-09-27 08:46:27 +00:00
var STD = require("lib/std");
// https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/opentextfile-method
var ForReading = 1;
var ForWriting = 2;
var ForAppending = 8;
var TristateUseDefault = -2;
var TristateTrue = -1;
var TristateFalse = 0;
2023-09-18 12:06:23 +00:00
// https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/streamtypeenum
var adTypeBinary = 1;
var adTypeText = 2;
// https://learn.microsoft.com/ko-kr/sql/ado/reference/ado-api/saveoptionsenum?view=sql-server-ver16
var adSaveCreateNotExist = 1;
var adSaveCreateOverWrite = 2;
// https://learn.microsoft.com/en-us/sql/ado/reference/ado-api/connectmodeenum?view=sql-server-ver16
var adModeRead = 1;
var adModeReadWrite = 3;
var adModeRecursive = 0x400000;
var adModeShareDenyNone = 16;
var adModeShareDenyRead = 4;
var adModeShareDenyWrite = 8;
var adModeShareExclusive = 12;
var adModeUnknown = 0;
var adModeWrite = 2;
// https://learn.microsoft.com/en-us/previous-versions/exchange-server/exchange-10/ms527267(v=exchg.10)
// https://learn.microsoft.com/en-us/previous-versions/exchange-server/exchange-10/ms526296(v=exchg.10)
var CdoBIG5 = "big5";
var CdoEUC_JP = "euc-jp";
var CdoEUC_KR = "euc-kr";
var CdoGB2312 = "gb2312";
var CdoISO_2022_JP = "iso-2022-jp";
var CdoISO_2022_KR = "iso-2022-kr";
var CdoISO_8859_1 = "iso-8859-1";
var CdoISO_8859_2 = "iso-8859-2";
var CdoISO_8859_3 = "iso-8859-3";
var CdoISO_8859_4 = "iso-8859-4";
var CdoISO_8859_5 = "iso-8859-5";
var CdoISO_8859_6 = "iso-8859-6";
var CdoISO_8859_7 = "iso-8859-7";
var CdoISO_8859_8 = "iso-8859-8";
var CdoISO_8859_9 = "iso-8859-9";
var cdoKOI8_R = "koi8-r";
var cdoShift_JIS = "shift-jis";
var CdoUS_ASCII = "us-ascii";
var CdoUTF_7 = "utf-7";
var CdoUTF_8 = "utf-8";
2022-09-27 08:46:27 +00:00
function PipeIPC() {
2023-09-18 12:06:23 +00:00
this.path = "data\\.pipe_:pipename";
2023-09-18 14:25:18 +00:00
this.delimiter = "\r\n";
2022-09-27 08:46:27 +00:00
this.reader = null;
this.writer = null;
2023-09-18 12:06:23 +00:00
this.recorder = null;
this.savefile = null;
this.tmpfile = null;
this.charset = CdoUTF_8;
this.lastReadTime = -1;
this.lastWriteTime = -1;
2023-09-18 14:25:18 +00:00
this.maxSentences = 0;
2022-09-27 08:46:27 +00:00
2023-09-18 12:06:23 +00:00
this.getCurrentTime = function() {
return new Date().getTime();
};
this.setCharset = function(charset) {
this.charset = charset;
};
2023-09-18 14:25:18 +00:00
this.setDelimiter = function(delimiter) {
this.delimiter = delimiter;
};
this.setMaxSentences = function(maxSentences) {
this.maxSentences = maxSentences;
};
2023-09-18 12:06:23 +00:00
this.connect = function(pipename, callback) {
this.path = this.path.replace(":pipename", pipename);
//this.openWriter();
this.openReader();
2022-09-27 08:46:27 +00:00
if (typeof callback === "function") {
callback(this, this.reader, this.writer);
}
2023-09-18 12:06:23 +00:00
return this;
2022-09-27 08:46:27 +00:00
};
2023-09-18 12:06:23 +00:00
this.openWriter = function(iomode) {
while (this.writer == null) {
try {
this.writer = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.path, iomode, true, TristateTrue);
} catch (e) {}
}
2022-09-27 08:46:27 +00:00
};
this.closeWriter = function() {
2023-09-18 12:06:23 +00:00
if (this.writer != null) {
this.writer.Close();
this.writer = null;
}
2022-09-27 08:46:27 +00:00
};
2023-09-18 12:06:23 +00:00
this.openReader = function() {
while (this.reader == null) {
try {
this.reader = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.path, ForReading, true, TristateTrue);
} catch (e) {}
}
2022-09-27 08:46:27 +00:00
};
this.closeReader = function() {
2023-09-18 12:06:23 +00:00
if (this.reader != null) {
this.reader.Close();
this.reader = null;
}
};
this.startRecorder = function(filename) {
this.savefile = filename;
this.tmpfile = this.savefile + ".tmp";
this.openRecorder();
};
this.stopRecorder = function() {
this.savefile = null;
this.tmpfile = null;
};
this.openRecorder = function() {
while (this.recorder == null) {
try {
this.recorder = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.tmpfile, ForAppending, true, TristateTrue);
} catch (e) {}
}
};
this.closeRecorder = function() {
if (this.recorder != null) {
this.recorder.Close();
this.recorder = null;
}
2022-09-27 08:46:27 +00:00
};
2022-09-27 09:35:25 +00:00
this._write = function(message) {
2023-09-18 14:25:18 +00:00
this.writer.Write(message + this.delimiter);
2022-09-27 09:35:25 +00:00
};
2022-09-27 08:46:27 +00:00
this.write = function(message) {
2023-09-18 12:06:23 +00:00
if (this.savefile != null) {
this.record(message);
}
2022-09-27 08:46:27 +00:00
2023-09-18 12:06:23 +00:00
var isWritten = false;
2022-09-27 08:48:00 +00:00
while (!isWritten) {
2022-09-27 08:46:27 +00:00
try {
2022-09-27 10:52:56 +00:00
this.flush();
2023-09-18 12:06:23 +00:00
this.openWriter(ForAppending);
2022-09-27 09:35:25 +00:00
this._write(message);
2023-09-18 12:06:23 +00:00
this.closeWriter();
2022-09-27 08:48:00 +00:00
isWritten = true;
2023-09-18 12:06:23 +00:00
this.lastWriteTime = this.getCurrentTime();
2022-09-27 08:46:27 +00:00
} catch (e) {
2023-09-18 12:06:23 +00:00
this.closeWriter();
2022-09-27 08:48:00 +00:00
isWritten = false;
2022-09-27 08:46:27 +00:00
}
}
2023-09-18 12:06:23 +00:00
return isWritten;
};
this._record = function(message) {
this.recorder.Write(message);
};
this.record = function(message) {
2023-09-18 14:25:18 +00:00
var _this = this; // Because of the variable scope
2023-09-18 12:06:23 +00:00
var commit = function(src, dst, charset) {
2023-09-18 14:25:18 +00:00
var isCommited = false;
// define functions
var StripAdoBOM = function(adoObj) {
2023-09-18 12:06:23 +00:00
var newAdoObj = CreateObject("ADODB.Stream");
newAdoObj.Type = adTypeBinary;
newAdoObj.Mode = adModeReadWrite;
newAdoObj.Open();
adoObj.Position = 3;
adoObj.CopyTo(newAdoObj);
adoObj.Flush();
2023-09-18 14:25:18 +00:00
adoObj.Close();
2023-09-18 12:06:23 +00:00
return newAdoObj;
};
2023-09-18 14:25:18 +00:00
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;
}
}
2023-09-18 12:06:23 +00:00
// Close a temporary file
2023-09-18 14:25:18 +00:00
fso.Close();
2023-09-18 12:06:23 +00:00
};
var isRecorded = false;
while (!isRecorded) {
try {
this.openRecorder();
this._record(message);
this.closeRecorder();
commit(this.tmpfile, this.savefile, this.charset);
isRecorded = true;
} catch (e) {
//console.log(e.message);
this.closeRecorder();
isRecorded = false;
}
}
return isRecorded;
2022-09-27 08:46:27 +00:00
};
2023-09-18 12:06:23 +00:00
2022-09-27 11:09:18 +00:00
this.flush = function() {
var isFlushed = false;
while (!isFlushed) {
try {
2023-09-18 12:06:23 +00:00
this.openWriter(ForWriting);
this._write('');
this.closeWriter();
2022-09-27 11:09:18 +00:00
isFlushed = true;
} catch (e) {
isFlushed = false;
}
}
2023-09-18 12:06:23 +00:00
return isFlushed;
2022-09-27 11:09:18 +00:00
};
2022-09-27 10:28:20 +00:00
2022-09-27 09:35:25 +00:00
this._read = function() {
2022-09-27 11:09:18 +00:00
return this.reader.ReadAll();
2022-09-27 09:35:25 +00:00
};
2022-09-27 08:46:27 +00:00
this.read = function() {
var isRead = false;
2022-09-27 11:09:18 +00:00
var text = "";
2022-09-27 08:46:27 +00:00
while (!isRead) {
try {
2022-09-27 11:09:18 +00:00
text += this._read();
2022-09-27 08:46:27 +00:00
isRead = true;
2023-09-18 12:06:23 +00:00
this.lastReadTime = this.getCurrentTime();
2022-09-27 08:46:27 +00:00
} catch (e) {
this.closeReader();
2023-09-18 12:06:23 +00:00
this.openReader();
2022-09-27 08:46:27 +00:00
}
}
2022-09-27 11:09:18 +00:00
return text;
2022-09-27 09:31:45 +00:00
};
2022-09-27 08:46:27 +00:00
this.close = function() {
this.closeWriter();
this.closeReader();
2023-09-18 12:06:23 +00:00
this.closeRecorder();
2022-09-27 08:46:27 +00:00
};
}
exports.create = function() {
return new PipeIPC();
};
2023-09-18 12:06:23 +00:00
exports.connect = function(path, callback) {
return exports.create().connect(path, callback);
};
2022-09-28 18:41:21 +00:00
exports.ForReading = ForReading;
exports.ForWriting = ForWriting;
exports.ForAppending = ForAppending;
2023-09-18 14:25:18 +00:00
exports.CdoUTF_8 = CdoUTF_8;
2022-09-28 18:41:21 +00:00
2023-09-18 14:25:18 +00:00
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.5";
2022-11-25 14:11:37 +00:00
exports.AUTHOR = "abuse@catswords.net";
2022-09-27 08:46:27 +00:00
exports.global = global;
exports.require = require;