1 | const statement = require("./statement.js"); |
2 | const event = require("./event.js"); |
3 | const fs = require("fs"); |
4 |
|
5 | const FileHandler = require("./fileHandler.js"); |
6 |
|
7 | class Queue { |
8 | constructor(name, location, config) { |
9 | if (typeof name != "string") throw new Error(`Expected string, got ${typeof name} for name`); |
10 | if (typeof location != "string") throw new Error(`Expected string, got ${typeof location} for location`); |
11 | this.name = name; |
12 | this.timestamp = new Date(); |
13 | this.location = location; |
14 | this.filename = this.location + "/" + this.timestamp.toISOString().replace(/\:/g, "-").replace(/\./g, "-") + ".log"; |
15 | this.queue = []; |
16 | this.fixed = false; |
17 |
|
18 | this.compact = false; |
19 | this.fileStarted = false; |
20 | |
21 | if (config) { |
22 | if (config.compact) { |
23 | if (typeof config.compact != "boolean") throw new Error(`Expected boolean, got ${typeof config.compact} for config.compact`); |
24 | this.compact = config.compact; |
25 | } |
26 | } |
27 |
|
28 | let configFile = this.location + "/metadata.json"; |
29 |
|
30 | FileHandler.AppendFile(this.filename, ""); // touch the file |
31 | FileHandler.AppendFile(configFile, ""); |
32 |
|
33 | let configContent = fs.readFileSync(configFile).toString(); |
34 |
|
35 | let queueConfig = []; |
36 | if (configContent != "") queueConfig = JSON.parse(configContent); |
37 |
|
38 | queueConfig.push( |
39 | { |
40 | "time": this.timestamp, |
41 | "log": this.filename |
42 | } |
43 | ); |
44 |
|
45 | fs.writeFileSync(configFile, JSON.stringify(queueConfig)); |
46 |
|
47 | return this; |
48 | } |
49 |
|
50 | push() { |
51 | if (this.fixed) throw new Error("Cannot write to the queue after the log is fixed"); |
52 | if (arguments.length >= 3) this.queue.push(new statement(arguments[0], arguments[1], arguments[2])); |
53 | else if (arguments.length > 0) { |
54 | if (arguments[0] instanceof event) this.queue.push(arguments[0].statements); // object is an Event |
55 | else if (arguments[0] instanceof statement) this.queue.push(arguments[0]); // object is a Statement |
56 | else throw new TypeError("Expected an Event or Statement"); |
57 | } else throw new Error("Expected at least one argument"); |
58 | return this; |
59 | } |
60 |
|
61 | write() { |
62 | if (this.fixed) throw new Error("Cannot write queue after the log is fixed"); |
63 | let recursiveCompactFunction; |
64 | if (this.compact) { |
65 | recursiveCompactFunction = function(object) { |
66 | if (Array.isArray(object)) { |
67 | for (let i = 0; i < object.length; i++) { |
68 | object[i] = recursiveCompactFunction(object[i]); |
69 | } |
70 | return object; |
71 | } else { |
72 | let fixed = {}; |
73 | fixed.tt = object.timestamp; |
74 | fixed.v = object.verbosity; |
75 | fixed.t = object.tag; |
76 | fixed.m = object.message; |
77 | return fixed; |
78 | } |
79 | } |
80 | } |
81 |
|
82 | if (arguments.length > 0) { |
83 | let toWrite; |
84 | if (arguments.length >= 3) toWrite = new statement(arguments[0], arguments[1], arguments[2]); |
85 | else { |
86 | if (arguments[0] instanceof event) toWrite = arguments[0].statements; // object is an Event |
87 | else if (arguments[0] instanceof statement) toWrite = arguments[0]; // object is a Statement |
88 | else throw new TypeError("Expected an Event or Statement"); |
89 | } |
90 |
|
91 | if (this.compact) { |
92 | toWrite = recursiveCompactFunction(toWrite); |
93 | } |
94 |
|
95 | let appendString = ""; |
96 | if (this.fileStarted) appendString += "\n"; |
97 | appendString += JSON.stringify(toWrite); |
98 | if (!this.fileStarted) this.fileStarted = true; |
99 | FileHandler.AppendFile(this.filename, appendString); |
100 | } else { |
101 | let queueLength = this.queue.length; |
102 | if (queueLength > 0) { |
103 | let appendString = ""; |
104 | let queue = this; |
105 | let toWrite = this.queue; |
106 | if (this.compact) { |
107 | toWrite = recursiveCompactFunction(toWrite); |
108 | } |
109 | toWrite.forEach(function (item) { |
110 | if (queue.fileStarted) appendString += "\n"; |
111 | appendString += JSON.stringify(item); |
112 | if (!queue.fileStarted) queue.fileStarted = true; |
113 | }); |
114 | this.queue = this.queue.slice(queueLength); |
115 | FileHandler.AppendFile(this.filename, appendString); |
116 | } |
117 | } |
118 | return this; |
119 | } |
120 |
|
121 | fix() { |
122 | this.fixed = true; |
123 | let logContent = fs.readFileSync(this.filename).toString(); |
124 | logContent = "[" + logContent.replace(/\n/g, "\n,") + "]"; |
125 | fs.writeFileSync(this.filename, logContent); |
126 | } |
127 | } |
128 |
|
129 | module.exports = Queue; |
130 |
|