querysub 0.247.0 → 0.248.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "querysub",
3
- "version": "0.247.0",
3
+ "version": "0.248.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
@@ -97,9 +97,54 @@ export async function streamScreenOutput(config: {
97
97
  return (async () => {
98
98
  try {
99
99
  const pipeFile = `${root}${screenName}/pipe.txt`;
100
+ const tailScript = `${root}${screenName}/smart_tail.sh`;
101
+
102
+ // Create a smart tail script that handles file truncation
103
+ await fs.promises.writeFile(tailScript, `#!/bin/bash
104
+ PIPE_FILE="$1"
105
+
106
+ # Initialize position tracking
107
+ CURRENT_POS=0
108
+
109
+ # Read initial content and get file size
110
+ if [ -f "$PIPE_FILE" ]; then
111
+ CURRENT_SIZE=$(stat -c%s "$PIPE_FILE" 2>/dev/null || wc -c < "$PIPE_FILE")
112
+ # Output initial content like tail would
113
+ if [ $CURRENT_SIZE -gt 0 ]; then
114
+ cat "$PIPE_FILE"
115
+ CURRENT_POS=$CURRENT_SIZE
116
+ fi
117
+ else
118
+ CURRENT_SIZE=0
119
+ fi
120
+
121
+ # Watch for file changes
122
+ while inotifywait -q -e modify "$PIPE_FILE" 2>/dev/null; do
123
+ if [ -f "$PIPE_FILE" ]; then
124
+ NEW_SIZE=$(stat -c%s "$PIPE_FILE" 2>/dev/null || wc -c < "$PIPE_FILE")
125
+
126
+ if [ $NEW_SIZE -lt $CURRENT_SIZE ]; then
127
+ # File has shrunk (truncated), reset position to end of file
128
+ # Don't output anything, just reset our tracking
129
+ CURRENT_POS=$NEW_SIZE
130
+ CURRENT_SIZE=$NEW_SIZE
131
+ elif [ $NEW_SIZE -gt $CURRENT_SIZE ]; then
132
+ # File grew normally, read only the new part
133
+ if [ $CURRENT_POS -lt $NEW_SIZE ]; then
134
+ # Use tail with byte offset to read from current position
135
+ tail -c +$((CURRENT_POS + 1)) "$PIPE_FILE" | head -c $((NEW_SIZE - CURRENT_POS))
136
+ CURRENT_POS=$NEW_SIZE
137
+ fi
138
+ CURRENT_SIZE=$NEW_SIZE
139
+ fi
140
+ # If sizes are equal, no change to output
141
+ fi
142
+ done`);
143
+
144
+ await runPromise(`chmod +x ${tailScript}`);
100
145
 
101
- // Use tail -f to follow the pipe file
102
- childProcess = spawn(`tail`, ["-f", pipeFile, "--lines", "1000"], {
146
+ // Use our smart tail script instead of regular tail
147
+ childProcess = spawn("bash", [tailScript, pipeFile], {
103
148
  stdio: "pipe",
104
149
  });
105
150
 
@@ -312,6 +357,8 @@ while IFS= read -r line; do
312
357
  # Count total lines in file
313
358
  total_lines=$(wc -l < "${pipeFile}")
314
359
  if [ "$total_lines" -gt ${PIPE_FILE_LINE_LIMIT} ]; then
360
+ # Wait 2 seconds to give the watcher time to read pending content
361
+ sleep 2
315
362
  # Keep only the last ${PIPE_FILE_LINE_LIMIT} lines when file gets too many lines
316
363
  tail -n ${Math.floor(PIPE_FILE_LINE_LIMIT / 2)} "${pipeFile}" > "${pipeFile}.tmp" && mv "${pipeFile}.tmp" "${pipeFile}"
317
364
  fi