Files
orca/src/main/runtime/file-watcher-worker.ts
T
NeilandOrca 46646d7ff1 chore(lint): upgrade oxlint to 1.71 + enable 7 new rules (autofixed backlog) (#6841)
* chore(lint): upgrade oxlint to 1.71 and enable 7 new rules

Upgrade oxlint 1.67.0 -> 1.71.0 (1.72 was blocked by the repo's 3-day
minimum-release-age supply-chain guard; nothing here needs it). The
bump is a no-op on the existing config.

Enable 3 error rules (backlog autofixed to zero in this commit) and
4 warn rules (surface signal without gating CI):

error (autofixed, behavior-preserving):
- unicorn/prefer-node-protocol        (~1531 sites: bare builtin -> node:)
- typescript/no-import-type-side-effects (~36: all-inline-type -> import type)
- unicorn/no-array-reverse            (19: copy-then-reverse -> toReversed)

warn (real signal, current fires are test-only/correct):
- unicorn/no-array-fill-with-reference-type  (aliasing footgun guard)
- typescript/no-unsafe-function-type         (bans bare Function type)
- unicorn/prefer-array-flat-map              (map().flat() -> flatMap())
- unicorn/prefer-regexp-test                 (.match() in bool ctx -> .test())

mobile/.oxlintrc.json extends root, so it inherits all 7; the autofix
ran from root and covered mobile/ too.

Verification (all green): oxlint 0 errors (root+mobile+aux configs),
oxfmt clean, typecheck (node+cli+web), vitest 22795 passed / 0 failed,
builds (electron-vite + web + cli) succeed. node: rewrites confirmed to
skip embedded SSH/CLI string payloads (AST-only); all toReversed sites
verified to operate on fresh copies or write-once locals.

* chore(lint): bump mobile oxlint to 1.71 so inherited rules parse

mobile/ is a standalone pnpm project pinning its own oxlint@1.67, which
lacks unicorn/no-array-fill-with-reference-type (needs >=1.70). Since
mobile/.oxlintrc.json extends the root config, mobile CI's 'cd mobile &&
oxlint' failed to parse the new rule. Bump mobile to match root (1.71).

Verified in mobile/: oxlint 0 errors, oxfmt --check clean, tsc --noEmit
pass, vitest 978 passed / 0 failed.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-06-29 22:38:29 -07:00

141 lines
4.7 KiB
TypeScript

// Why: on Linux/Windows @parcel/watcher uses a brute-force backend that
// recursively walks the whole tree on a libuv threadpool thread before
// subscribe() resolves. On a huge tree backed by slow storage (a home dir on
// NFS opened as a worktree) that crawl can run for minutes. Running it here, in
// a dedicated worker thread, keeps it off the main/`serve` process's libuv pool
// so it can never starve static-asset serving, RPC crypto, or other clients
// (issue #5308). The worker owns the subscribe, the per-event stat fanout, and
// the event batching; the main thread only relays results.
import { stat } from 'node:fs/promises'
import { parentPort, workerData } from 'node:worker_threads'
import type * as ParcelWatcher from '@parcel/watcher'
import type { FsChangeEvent } from '../../shared/types'
const RUNTIME_FILE_WATCH_EVENT_STAT_LIMIT = 200
const RUNTIME_FILE_WATCH_STAT_CONCURRENCY = 8
type FileWatcherWorkerData = {
rootPath: string
ignore: string[]
}
// Messages the worker sends back to the host.
export type FileWatcherWorkerMessage =
| { type: 'ready' }
| { type: 'events'; events: FsChangeEvent[] }
| { type: 'error'; message: string }
// Messages the host sends to the worker.
export type FileWatcherHostMessage = { type: 'unsubscribe' }
const data = workerData as FileWatcherWorkerData
if (!parentPort) {
throw new Error('File watcher worker must run with a parent port.')
}
const port = parentPort
/** Report a watcher failure to the host and ask the renderer to refresh from
* scratch (the overflow event), so a mid-stream error never leaves the
* explorer silently stale. */
function reportWatchError(err: unknown): void {
port.postMessage({
type: 'error',
message: err instanceof Error ? err.message : String(err)
} satisfies FileWatcherWorkerMessage)
port.postMessage({
type: 'events',
events: [{ kind: 'overflow', absolutePath: data.rootPath }]
} satisfies FileWatcherWorkerMessage)
}
/** Run an async mapper over items with a bounded number in flight at once, so a
* large batch can't occupy every libuv threadpool thread in this worker. */
async function mapWithConcurrency<T, R>(
items: readonly T[],
limit: number,
mapper: (item: T) => Promise<R>
): Promise<R[]> {
const results = Array.from<R>({ length: items.length })
let cursor = 0
const worker = async (): Promise<void> => {
while (cursor < items.length) {
const index = cursor++
results[index] = await mapper(items[index])
}
}
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker))
return results
}
async function main(): Promise<void> {
let watcher: typeof ParcelWatcher
try {
watcher = await import('@parcel/watcher')
} catch (err) {
port.postMessage({
type: 'error',
message: err instanceof Error ? err.message : String(err)
} satisfies FileWatcherWorkerMessage)
return
}
const subscription = await watcher.subscribe(
data.rootPath,
(err, events) => {
if (err) {
reportWatchError(err)
return
}
// Why: large watcher batches usually mean a generated directory or branch
// switch. Avoid stat fanout and ask the renderer to refresh.
if (events.length > RUNTIME_FILE_WATCH_EVENT_STAT_LIMIT) {
port.postMessage({
type: 'events',
events: [{ kind: 'overflow', absolutePath: data.rootPath }]
} satisfies FileWatcherWorkerMessage)
return
}
void mapWithConcurrency(
events,
RUNTIME_FILE_WATCH_STAT_CONCURRENCY,
async (event): Promise<FsChangeEvent> => {
let isDirectory = false
try {
isDirectory = (await stat(event.path)).isDirectory()
} catch {
isDirectory = false
}
return { kind: event.type, absolutePath: event.path, isDirectory }
}
)
.then((mapped) => {
port.postMessage({ type: 'events', events: mapped } satisfies FileWatcherWorkerMessage)
})
// Why: without this, a throwing postMessage / stat becomes an unhandled
// rejection that crashes the worker silently. Surface it instead.
.catch((err: unknown) => reportWatchError(err))
},
{ ignore: data.ignore }
)
// The crawl finished and the subscription is live.
port.postMessage({ type: 'ready' } satisfies FileWatcherWorkerMessage)
port.on('message', (message: FileWatcherHostMessage) => {
if (message.type === 'unsubscribe') {
void subscription.unsubscribe().finally(() => {
port.close()
})
}
})
}
void main().catch((err: unknown) => {
port.postMessage({
type: 'error',
message: err instanceof Error ? err.message : String(err)
} satisfies FileWatcherWorkerMessage)
})