fix(tauri): set File.lastModified from stat() so batch dedup works (v1.3.3) (#27)

`fileFromNativePath` was calling `new File([text], name, { type })`
without `lastModified`. The File API defaults `lastModified` to
`Date.now()` at construction time, so reading the same path twice
produced two File objects with different timestamps — and `batchStore`
dedups on `(name, size, lastModified)`, so dropping the same file
twice through Tauri native drag-drop / native picker added both
copies to the queue with no `duplicate` toast. Web wasn't affected
because `<input type="file">` and `react-dropzone` build File objects
from the OS mtime.

Fix: call `stat()` from `@tauri-apps/plugin-fs` and pass
`mtime.getTime()` into the File constructor. Fall back to an FNV-1a
hash of the path if stat throws (capability missing, file vanished
between read and stat) so repeat drops still dedup.

Also infer `type` from the extension instead of hard-coding
`application/json`. Add `fs:allow-stat` to the default capability.

3 new unit tests for `pathHashAsMillis`. Total: 193.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 4.7 authored and GitHub committed 2026-05-13 20:33:12 +07:00
1 parent 58ef7e739a
commit 8a27c5c969
8 files changed
+87 -6

No files matched your search

+10
View File
@@ -8,6 +8,16 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and
No unreleased changes at this time. Future work tracked in `docs/ROADMAP.md` under "Beyond Phase 3".
## [1.3.3] — 2026-05-13
### Fixed
- **Tauri native drag-drop / file picker bỏ qua batch dedup**. v1.3.0–v1.3.2 desktop bundles cho phép drop cùng một file vào queue nhiều lần mà không hiển thị toast `duplicate`. Root cause: `fileFromNativePath` tạo `new File([text], name, { type })` không truyền `lastModified`, nên File API default về `Date.now()` mỗi lần gọi; key dedup của `batchStore` (`name|size|lastModified`) khác nhau giữa các lần drop cùng một file. Sửa: stat file qua `@tauri-apps/plugin-fs` để lấy mtime thật, fall back về hash FNV-1a của path nếu stat fail. Bonus: infer `type` từ extension thay vì hard-code `application/json`. Thêm permission `fs:allow-stat` vào default capability.
### Tests
- 3 unit test mới cho `pathHashAsMillis` trong `src/lib/__tests__/tauri.test.ts`. Total tests: **193**.
## [1.3.2] — 2026-05-13
### Fixed
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "jsonprism",
"version": "1.3.2",
"version": "1.3.3",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "jsonprism",
"version": "1.3.2",
"version": "1.3.3",
"license": "Apache-2.0",
"dependencies": {
"@codemirror/lang-json": "^6.0.0",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "jsonprism",
"private": true,
"version": "1.3.2",
"version": "1.3.3",
"type": "module",
"description": "Disperse JSON into many formats — JSONL, CSV, YAML, XML, TOML, RESX, Markdown, SQL — fully client-side.",
"author": "poli0981 <https://github.com/poli0981>",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "jsonprism"
version = "1.3.2"
version = "1.3.3"
description = "Disperse JSON into many formats."
authors = ["poli0981 <https://github.com/poli0981>"]
license = "Apache-2.0"
+1
View File
@@ -20,6 +20,7 @@
"fs:allow-read-text-file",
"fs:allow-write-text-file",
"fs:allow-write-file",
"fs:allow-stat",
{
"identifier": "fs:scope",
"allow": ["$HOME/**", "$DOCUMENT/**", "$DOWNLOAD/**", "$DESKTOP/**"]
+1 -1
View File
@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "JSONPrism",
"version": "1.3.2",
"version": "1.3.3",
"identifier": "com.poli0981.jsonprism",
"build": {
"beforeDevCommand": "npm run dev",
+22
View File
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest';
import { pathHashAsMillis } from '../tauri';
describe('pathHashAsMillis', () => {
it('is deterministic for the same input', () => {
const a = pathHashAsMillis('C:/Users/me/data.json');
const b = pathHashAsMillis('C:/Users/me/data.json');
expect(a).toBe(b);
});
it('produces different hashes for different paths', () => {
expect(pathHashAsMillis('/a/foo.json')).not.toBe(pathHashAsMillis('/a/bar.json'));
expect(pathHashAsMillis('/a/foo.json')).not.toBe(pathHashAsMillis('/b/foo.json'));
});
it('returns a non-negative integer (unsigned 32-bit) even for empty input', () => {
const h = pathHashAsMillis('');
expect(Number.isInteger(h)).toBe(true);
expect(h).toBeGreaterThanOrEqual(0);
expect(h).toBeLessThanOrEqual(0xffffffff);
});
});
+49 -1
View File
@@ -70,14 +70,62 @@ export async function nativeReadText(path: string): Promise<string> {
return readTextFile(path);
}
// MIME type per extension. Used so File objects created from native paths
// carry a sensible `type` instead of being hard-coded to `application/json`.
const TYPE_BY_EXT: Record<string, string> = {
json: 'application/json',
yaml: 'application/x-yaml',
yml: 'application/x-yaml',
toml: 'application/toml',
xml: 'application/xml',
csv: 'text/csv',
tsv: 'text/tab-separated-values',
resx: 'application/xml',
txt: 'text/plain',
};
/**
* FNV-1a 32-bit hash of a path, returned as an unsigned 32-bit integer.
* Used as a stable `lastModified` for File objects when `stat()` is
* unavailable — `batchStore` dedups on `(name, size, lastModified)`,
* so the value must be deterministic across repeated drops of the same path.
*/
export function pathHashAsMillis(path: string): number {
let h = 0x811c9dc5;
for (let i = 0; i < path.length; i += 1) {
h ^= path.charCodeAt(i);
h = Math.imul(h, 0x01000193);
}
return h >>> 0;
}
/**
* Convert a native file path to a browser-style File so the
* existing batch store and editor flows can consume it unchanged.
*
* `lastModified` is read from the file's real mtime via `stat()` so that
* re-dropping the same file dedups correctly (batchStore keys on
* name|size|lastModified). Falling back to `Date.now()` would break dedup
* because every drop produces a fresh timestamp.
*/
export async function fileFromNativePath(path: string): Promise<File> {
const text = await nativeReadText(path);
const name = path.replace(/^.*[\\/]/, ''); // basename
return new File([text], name, { type: 'application/json' });
const ext = (name.split('.').pop() ?? '').toLowerCase();
const type = TYPE_BY_EXT[ext] ?? 'application/octet-stream';
let lastModified: number;
try {
const { stat } = await import('@tauri-apps/plugin-fs');
const info = await stat(path);
lastModified = info.mtime ? info.mtime.getTime() : pathHashAsMillis(path);
} catch {
// Capability missing or file vanished between read + stat — fall back
// to a deterministic hash so repeat drops still dedup.
lastModified = pathHashAsMillis(path);
}
return new File([text], name, { type, lastModified });
}
/**