-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileWorker.js
More file actions
109 lines (96 loc) · 2.62 KB
/
fileWorker.js
File metadata and controls
109 lines (96 loc) · 2.62 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
// Web Worker for file processing and token counting
// This runs in a separate thread to avoid blocking the UI
// Token estimation (3.5 chars per token for code)
function estimateTokens(text) {
return Math.ceil(text.length / 3.5);
}
// Text-based file extensions
const textFileExtensions = [
'txt', 'js', 'jsx', 'ts', 'tsx', 'html', 'css', 'scss', 'sass',
'less', 'json', 'xml', 'md', 'markdown', 'py', 'java', 'c', 'cpp',
'h', 'hpp', 'cs', 'php', 'rb', 'swift', 'kt', 'go', 'rs', 'sql',
'yaml', 'yml', 'toml', 'ini', 'cfg', 'conf', 'sh', 'bash', 'env',
'gitignore', 'dockerfile', 'vue', 'svelte', 'astro', 'graphql',
'prisma', 'csv', 'tsv'
];
function isTextFile(filename) {
const extension = filename.split('.').pop().toLowerCase();
return textFileExtensions.includes(extension) || filename.toLowerCase().endsWith('gitignore');
}
// Process file content and count tokens
function processFileContent(content) {
const lines = content.split('\n').length;
const tokens = estimateTokens(content);
const chars = content.length;
return { lines, tokens, chars };
}
// Parse gitignore content
function parseGitignore(content) {
if (!content) return [];
return content
.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#'))
.map(pattern => {
if (pattern.startsWith('/')) {
pattern = pattern.slice(1);
}
if (pattern.endsWith('/')) {
pattern = pattern.slice(0, -1);
}
return pattern;
});
}
// Message handler
self.onmessage = async function(e) {
const { type, data } = e.data;
try {
switch (type) {
case 'ESTIMATE_TOKENS':
const tokens = estimateTokens(data.text);
self.postMessage({ type: 'TOKENS_ESTIMATED', result: tokens });
break;
case 'PROCESS_FILE':
const result = processFileContent(data.content);
self.postMessage({
type: 'FILE_PROCESSED',
result: result,
fileId: data.fileId
});
break;
case 'PARSE_GITIGNORE':
const patterns = parseGitignore(data.content);
self.postMessage({
type: 'GITIGNORE_PARSED',
result: patterns
});
break;
case 'BATCH_PROCESS':
const batchResults = [];
for (const item of data.items) {
if (isTextFile(item.name)) {
const processed = processFileContent(item.content);
batchResults.push({
path: item.path,
...processed
});
}
}
self.postMessage({
type: 'BATCH_PROCESSED',
result: batchResults
});
break;
default:
self.postMessage({
type: 'ERROR',
error: `Unknown message type: ${type}`
});
}
} catch (error) {
self.postMessage({
type: 'ERROR',
error: error.message
});
}
};