querysub 0.247.0 → 0.249.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.249.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,64 @@ 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
+ LAST_MTIME=""
109
+
110
+ # Read initial content and get file size
111
+ if [ -f "$PIPE_FILE" ]; then
112
+ CURRENT_SIZE=$(stat -c%s "$PIPE_FILE" 2>/dev/null || wc -c < "$PIPE_FILE")
113
+ LAST_MTIME=$(stat -c%Y "$PIPE_FILE" 2>/dev/null || stat -f%m "$PIPE_FILE" 2>/dev/null || echo "0")
114
+ # Output initial content like tail would
115
+ if [ $CURRENT_SIZE -gt 0 ]; then
116
+ cat "$PIPE_FILE"
117
+ CURRENT_POS=$CURRENT_SIZE
118
+ fi
119
+ else
120
+ CURRENT_SIZE=0
121
+ fi
122
+
123
+ # Poll for file changes every 250ms
124
+ while true; do
125
+ sleep 0.25
126
+
127
+ if [ -f "$PIPE_FILE" ]; then
128
+ NEW_MTIME=$(stat -c%Y "$PIPE_FILE" 2>/dev/null || stat -f%m "$PIPE_FILE" 2>/dev/null || echo "0")
129
+
130
+ # Check if file was modified
131
+ if [ "$NEW_MTIME" != "$LAST_MTIME" ]; then
132
+ NEW_SIZE=$(stat -c%s "$PIPE_FILE" 2>/dev/null || wc -c < "$PIPE_FILE")
133
+
134
+ if [ $NEW_SIZE -lt $CURRENT_SIZE ]; then
135
+ # File has shrunk (truncated), reset position to end of file
136
+ # Don't output anything, just reset our tracking
137
+ CURRENT_POS=$NEW_SIZE
138
+ CURRENT_SIZE=$NEW_SIZE
139
+ elif [ $NEW_SIZE -gt $CURRENT_SIZE ]; then
140
+ # File grew normally, read only the new part
141
+ if [ $CURRENT_POS -lt $NEW_SIZE ]; then
142
+ # Use tail with byte offset to read from current position
143
+ tail -c +$((CURRENT_POS + 1)) "$PIPE_FILE" | head -c $((NEW_SIZE - CURRENT_POS))
144
+ CURRENT_POS=$NEW_SIZE
145
+ fi
146
+ CURRENT_SIZE=$NEW_SIZE
147
+ fi
148
+
149
+ LAST_MTIME="$NEW_MTIME"
150
+ fi
151
+ fi
152
+ done`);
153
+
154
+ await runPromise(`chmod +x ${tailScript}`);
100
155
 
101
- // Use tail -f to follow the pipe file
102
- childProcess = spawn(`tail`, ["-f", pipeFile, "--lines", "1000"], {
156
+ // Use our smart tail script instead of regular tail
157
+ childProcess = spawn("bash", [tailScript, pipeFile], {
103
158
  stdio: "pipe",
104
159
  });
105
160
 
@@ -312,6 +367,8 @@ while IFS= read -r line; do
312
367
  # Count total lines in file
313
368
  total_lines=$(wc -l < "${pipeFile}")
314
369
  if [ "$total_lines" -gt ${PIPE_FILE_LINE_LIMIT} ]; then
370
+ # Wait 2 seconds to give the watcher time to read pending content
371
+ sleep 2
315
372
  # Keep only the last ${PIPE_FILE_LINE_LIMIT} lines when file gets too many lines
316
373
  tail -n ${Math.floor(PIPE_FILE_LINE_LIMIT / 2)} "${pipeFile}" > "${pipeFile}.tmp" && mv "${pipeFile}.tmp" "${pipeFile}"
317
374
  fi