cueme 0.1.7 → 0.1.9

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.
Files changed (2) hide show
  1. package/package.json +5 -3
  2. package/src/io.js +14 -34
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cueme",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Cue command protocol adapter (stdin/stdout JSON)",
5
5
  "license": "Apache-2.0",
6
6
  "files": [
@@ -16,12 +16,14 @@
16
16
  },
17
17
  "type": "commonjs",
18
18
  "engines": {
19
- "node": ">=18"
19
+ "node": ">=20"
20
20
  },
21
21
  "scripts": {
22
22
  "prepare": "node -c src/cli.js && node -c src/handler.js"
23
23
  },
24
24
  "dependencies": {
25
- "better-sqlite3": "^11.7.0"
25
+ "better-sqlite3": "^12.6.0",
26
+ "chardet": "^2.1.1",
27
+ "iconv-lite": "^0.7.2"
26
28
  }
27
29
  }
package/src/io.js CHANGED
@@ -1,3 +1,6 @@
1
+ const chardet = require('chardet');
2
+ const iconv = require('iconv-lite');
3
+
1
4
  function readAllStdin() {
2
5
  if (process.stdin.isTTY) return Promise.resolve('');
3
6
  return new Promise((resolve, reject) => {
@@ -9,7 +12,7 @@ function readAllStdin() {
9
12
  const buf = Buffer.concat(chunks);
10
13
  if (!buf || buf.length === 0) return resolve('');
11
14
 
12
- // BOM detection
15
+ // BOM detection (fast path for common cases)
13
16
  if (buf.length >= 3 && buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) {
14
17
  return resolve(buf.slice(3).toString('utf8'));
15
18
  }
@@ -26,42 +29,19 @@ function readAllStdin() {
26
29
  return resolve(swapped.toString('utf16le'));
27
30
  }
28
31
 
29
- // Heuristic: PowerShell piping to native exe often uses UTF-16LE without BOM.
30
- const sampleLen = Math.min(buf.length, 256);
31
-
32
- // For small samples, use odd/even byte distribution (UTF-16LE ASCII often has 0x00 in odd indices).
33
- if (sampleLen <= 64) {
34
- let oddTotal = 0;
35
- let evenTotal = 0;
36
- let oddZeros = 0;
37
- let evenZeros = 0;
38
- for (let i = 0; i < sampleLen; i += 1) {
39
- if (i % 2 === 0) {
40
- evenTotal += 1;
41
- if (buf[i] === 0x00) evenZeros += 1;
42
- } else {
43
- oddTotal += 1;
44
- if (buf[i] === 0x00) oddZeros += 1;
45
- }
46
- }
47
-
48
- const oddZeroRatio = oddTotal > 0 ? oddZeros / oddTotal : 0;
49
- const evenZeroRatio = evenTotal > 0 ? evenZeros / evenTotal : 0;
50
-
51
- if (oddZeroRatio > 0.6 && evenZeroRatio < 0.2) {
52
- return resolve(buf.toString('utf16le'));
53
- }
54
- } else {
55
- let zeros = 0;
56
- for (let i = 0; i < sampleLen; i += 1) {
57
- if (buf[i] === 0x00) zeros += 1;
58
- }
59
- const zeroRatio = zeros / sampleLen;
60
- if (zeroRatio > 0.2) {
61
- return resolve(buf.toString('utf16le'));
32
+ // Use chardet for automatic encoding detection
33
+ const detected = chardet.detect(buf);
34
+
35
+ if (detected && detected !== 'UTF-8') {
36
+ try {
37
+ return resolve(iconv.decode(buf, detected));
38
+ } catch {
39
+ // Fallback to UTF-8 if decode fails
40
+ return resolve(buf.toString('utf8'));
62
41
  }
63
42
  }
64
43
 
44
+ // Default to UTF-8
65
45
  return resolve(buf.toString('utf8'));
66
46
  });
67
47
  process.stdin.on('error', reject);