datool 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -169,10 +169,14 @@ Tails a local file and optionally honors `history`.
169
169
 
170
170
  ```ts
171
171
  sources.file({
172
+ defaultHistory: 5,
172
173
  path: "./app.log",
173
174
  })
174
175
  ```
175
176
 
177
+ `defaultHistory` lets the file source emit existing lines on startup when the URL
178
+ does not include a `history` query param.
179
+
176
180
  ### `sources.command(...)`
177
181
 
178
182
  Spawns a local process and streams stdout lines.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datool",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "type": "module",
5
5
  "description": "Local-only config-driven log viewer with SSE streaming and a generic table UI.",
6
6
  "bin": {
@@ -6,6 +6,7 @@ import type { DatoolSource } from "../../shared/types"
6
6
  type Resolver<T> = T | ((context: { query: URLSearchParams }) => T)
7
7
 
8
8
  export type FileSourceOptions = {
9
+ defaultHistory?: number
9
10
  historyParam?: string
10
11
  path: Resolver<string>
11
12
  pollIntervalMs?: number
@@ -21,20 +22,28 @@ function resolveValue<T>(value: Resolver<T>, query: URLSearchParams) {
21
22
  return value
22
23
  }
23
24
 
24
- function toHistoryLineCount(query: URLSearchParams, historyParam: string) {
25
+ function normalizeHistoryLineCount(value: number) {
26
+ if (!Number.isFinite(value) || value <= 0) {
27
+ return 0
28
+ }
29
+
30
+ return Math.floor(value)
31
+ }
32
+
33
+ function resolveHistoryLineCount(
34
+ query: URLSearchParams,
35
+ historyParam: string,
36
+ defaultHistory: number
37
+ ) {
25
38
  const rawValue = query.get(historyParam)
26
39
 
27
40
  if (!rawValue) {
28
- return 0
41
+ return normalizeHistoryLineCount(defaultHistory)
29
42
  }
30
43
 
31
44
  const parsedValue = Number.parseInt(rawValue, 10)
32
45
 
33
- if (!Number.isFinite(parsedValue) || parsedValue <= 0) {
34
- return 0
35
- }
36
-
37
- return parsedValue
46
+ return normalizeHistoryLineCount(parsedValue)
38
47
  }
39
48
 
40
49
  function normalizeLines(content: string) {
@@ -76,10 +85,15 @@ async function readAppendedText(filePath: string, start: number, end: number) {
76
85
  export function fileSource(options: FileSourceOptions): DatoolSource {
77
86
  return {
78
87
  async open(context) {
88
+ const defaultHistory = options.defaultHistory ?? 0
79
89
  const historyParam = options.historyParam ?? "history"
80
90
  const pollIntervalMs = options.pollIntervalMs ?? 250
81
91
  const filePath = resolveValue(options.path, context.query)
82
- const historyLineCount = toHistoryLineCount(context.query, historyParam)
92
+ const historyLineCount = resolveHistoryLineCount(
93
+ context.query,
94
+ historyParam,
95
+ defaultHistory
96
+ )
83
97
  let stat = await fs.stat(filePath)
84
98
  let position = stat.size
85
99
  let remainder = ""