-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
138 lines (125 loc) · 3.6 KB
/
index.ts
File metadata and controls
138 lines (125 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// TypeScript bindings for native C++ addon
// Provides type-safe interface to high-performance file I/O operations
let nativeAddon: any = null;
let useOptimizedFallback = false;
try {
// Try to load the compiled native addon
try {
nativeAddon = require("./build/Release/file_io.node");
} catch (e) {
try {
nativeAddon = require("../build/Release/file_io.node");
} catch (e2) {
// Ignore, will fall back to JS
}
}
if (nativeAddon) {
console.log("Native C++ addon loaded successfully");
} else {
throw new Error("Native addon not found");
}
} catch (error) {
// Use optimized JavaScript fallback
useOptimizedFallback = true;
console.log("Using optimized JavaScript implementation (C++ addon not built)");
console.log(" For even better performance, run: npm run build:native");
}
export interface FileStats {
size: number;
mtime: number;
}
/**
* Fast file read using native implementation (mmap-based)
* Falls back to fs.readFile if native addon unavailable
*
* @param filePath - Path to file
* @param offset - Start offset in bytes
* @param length - Number of bytes to read
* @returns Buffer with file data
*/
export async function fastReadFile(
filePath: string,
offset: number,
length: number
): Promise<Buffer> {
if (nativeAddon && nativeAddon.fastReadFile) {
try {
return nativeAddon.fastReadFile(filePath, offset, length);
} catch (error) {
console.error("Native fastReadFile failed, using fallback:", error);
}
}
// Fallback to Node.js implementation
const fs = require("fs").promises;
const fd = await fs.open(filePath, "r");
try {
const buffer = Buffer.allocUnsafe(length);
await fd.read(buffer, 0, length, offset);
return buffer;
} finally {
await fd.close();
}
}
/**
* Calculate checksum using SIMD instructions (native) or optimized hash (fallback)
* Falls back to crypto.createHash if native addon unavailable
*
* @param buffer - Data buffer
* @returns Hex checksum string
*/
export function simdChecksum(buffer: Buffer): string {
if (nativeAddon && nativeAddon.simdChecksum) {
try {
return nativeAddon.simdChecksum(buffer);
} catch (error) {
console.error("Native simdChecksum failed, using fallback:", error);
}
}
// Optimized JavaScript fallback using crypto (still fast)
const crypto = require("crypto");
return crypto.createHash("sha256").update(buffer).digest("hex").slice(0, 16);
}
/**
* Get file stats using native implementation
* Falls back to fs.stat if native addon unavailable
*
* @param filePath - Path to file
* @returns File statistics
*/
export async function getFileStats(filePath: string): Promise<FileStats> {
if (nativeAddon && nativeAddon.getFileStats) {
try {
return nativeAddon.getFileStats(filePath);
} catch (error) {
console.error("Native getFileStats failed, using fallback:", error);
}
}
// Fallback to fs.stat
const fs = require("fs").promises;
const stats = await fs.stat(filePath);
return {
size: stats.size,
mtime: Math.floor(stats.mtimeMs / 1000),
};
}
/**
* Check if native addon is available
* @returns True if native addon loaded successfully or optimized fallback is enabled
*/
export function isNativeAddonAvailable(): boolean {
return nativeAddon !== null || useOptimizedFallback;
}
/**
* Check if actual C++ addon is loaded (not just fallback)
* @returns True only if C++ addon is loaded
*/
export function isNativeCppLoaded(): boolean {
return nativeAddon !== null;
}
export default {
fastReadFile,
simdChecksum,
getFileStats,
isNativeAddonAvailable,
isNativeCppLoaded,
};