@bash0816/claude-code 2.1.272 → 2.1.276-1
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.
- package/README.md +4 -1
- package/config/claude-native-audited-versions.json +142 -1
- package/config/claude-termux-release-manifest.json +3 -3
- package/lib/bunfs-esm-loader.mjs +156 -0
- package/lib/bunfs-esm-loader.test.js +885 -0
- package/lib/bunfs-extract.js +10 -1
- package/lib/bunfs-extract.test.js +36 -3
- package/lib/hooks-standalone-pattern.js +7 -0
- package/lib/hooks-standalone-pattern.test.js +115 -0
- package/lib/termux-run-claude-native.sh +36 -0
- package/lib/termux-run-claude-native.test.js +160 -0
- package/package.json +1 -1
package/lib/bunfs-extract.js
CHANGED
|
@@ -9,6 +9,13 @@ const MODULE_TABLE_ENTRY_SIZE = 52;
|
|
|
9
9
|
const SCAN_CHUNK_SIZE = 1024 * 1024;
|
|
10
10
|
const NAPI_LOADER = 10;
|
|
11
11
|
|
|
12
|
+
class ModuleGraphNotFoundError extends Error {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = 'ModuleGraphNotFoundError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
12
19
|
function readRange(fd, offset, length) {
|
|
13
20
|
const buf = Buffer.alloc(length);
|
|
14
21
|
const bytesRead = readSync(fd, buf, 0, length, offset);
|
|
@@ -26,7 +33,7 @@ function findTrailerOffset(fd, fileSize) {
|
|
|
26
33
|
const idx = buf.lastIndexOf(TRAILER);
|
|
27
34
|
if (idx >= 0) return start + idx;
|
|
28
35
|
}
|
|
29
|
-
throw new
|
|
36
|
+
throw new ModuleGraphNotFoundError('bunfs-extract: StandaloneModuleGraph trailer not found');
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
function isSafeUint(value) {
|
|
@@ -197,6 +204,8 @@ function readEntryContentPrefix(fd, entryModule, maxLength = 256) {
|
|
|
197
204
|
}
|
|
198
205
|
|
|
199
206
|
module.exports = {
|
|
207
|
+
TRAILER,
|
|
208
|
+
ModuleGraphNotFoundError,
|
|
200
209
|
discoverModuleGraph,
|
|
201
210
|
extractToProcessOwnedDir,
|
|
202
211
|
cleanupStaleOwnedDirs,
|
|
@@ -8,14 +8,14 @@ const path = require('node:path');
|
|
|
8
8
|
|
|
9
9
|
const {
|
|
10
10
|
discoverModuleGraph,
|
|
11
|
+
ModuleGraphNotFoundError,
|
|
12
|
+
TRAILER,
|
|
11
13
|
extractToProcessOwnedDir,
|
|
12
14
|
cleanupStaleOwnedDirs,
|
|
13
15
|
prepareProcessOwnedDir,
|
|
14
16
|
readEntryContentPrefix,
|
|
15
17
|
} = require('./bunfs-extract.js');
|
|
16
18
|
|
|
17
|
-
const TRAILER = '\n---- Bun! ----\n';
|
|
18
|
-
|
|
19
19
|
// StandaloneModuleGraphの最小合成バイナリを構築する。
|
|
20
20
|
// レイアウト: [preamble padding][module contents][module table][Offsets(32byte)][trailer]
|
|
21
21
|
function buildSyntheticBinary({ modules, entryPointId, corruptTrailer = false, preamblePadding = 64 }) {
|
|
@@ -58,7 +58,7 @@ function buildSyntheticBinary({ modules, entryPointId, corruptTrailer = false, p
|
|
|
58
58
|
offsetsBuf.writeUInt32LE(modulesLength, 12);
|
|
59
59
|
offsetsBuf.writeUInt32LE(entryPointId, 16);
|
|
60
60
|
|
|
61
|
-
const trailerBuf = Buffer.from(
|
|
61
|
+
const trailerBuf = corruptTrailer ? Buffer.from('\n---- NOT BUN ----\n', 'utf8') : TRAILER;
|
|
62
62
|
|
|
63
63
|
return Buffer.concat([
|
|
64
64
|
Buffer.alloc(preamblePadding),
|
|
@@ -114,6 +114,39 @@ test('discoverModuleGraph rejects a binary with a corrupted trailer', () => {
|
|
|
114
114
|
}
|
|
115
115
|
});
|
|
116
116
|
|
|
117
|
+
test('discoverModuleGraph throws ModuleGraphNotFoundError when the trailer is absent', () => {
|
|
118
|
+
const file = writeTempBinary(Buffer.from('not a bun standalone module graph'));
|
|
119
|
+
try {
|
|
120
|
+
assert.throws(() => discoverModuleGraph(file), (error) => {
|
|
121
|
+
assert.ok(error instanceof ModuleGraphNotFoundError);
|
|
122
|
+
assert.match(error.message, /trailer not found/);
|
|
123
|
+
return true;
|
|
124
|
+
});
|
|
125
|
+
} finally {
|
|
126
|
+
fs.rmSync(file, { force: true });
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('discoverModuleGraph throws plain Error for a malformed graph with a trailer', () => {
|
|
131
|
+
const buf = buildSyntheticBinary({
|
|
132
|
+
modules: [{ name: '/$bunfs/root/cli', content: 'x' }],
|
|
133
|
+
entryPointId: 0,
|
|
134
|
+
});
|
|
135
|
+
const offsetsStart = buf.length - TRAILER.length - 32;
|
|
136
|
+
buf.writeUInt32LE(1, offsetsStart + 12);
|
|
137
|
+
const file = writeTempBinary(buf);
|
|
138
|
+
try {
|
|
139
|
+
assert.throws(() => discoverModuleGraph(file), (error) => {
|
|
140
|
+
assert.ok(error instanceof Error);
|
|
141
|
+
assert.equal(error instanceof ModuleGraphNotFoundError, false);
|
|
142
|
+
assert.match(error.message, /module table length is not a multiple/);
|
|
143
|
+
return true;
|
|
144
|
+
});
|
|
145
|
+
} finally {
|
|
146
|
+
fs.rmSync(file, { force: true });
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
117
150
|
test('discoverModuleGraph rejects entry_point_id out of range', () => {
|
|
118
151
|
const buf = buildSyntheticBinary({
|
|
119
152
|
modules: [{ name: '/$bunfs/root/cli', content: 'x' }],
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// qle 形: X=(a,b,c)=>FN()?AR(b,c(),a):{module:b,folder:a}
|
|
3
|
+
const QLE_PATTERN_SOURCE = '(\\b[\\w$]+=\\(([\\w$]+),([\\w$]+),([\\w$]+)\\)=>)[\\w$]+\\(\\)\\?([\\w$]+)\\(\\3,\\4\\(\\),\\2\\):\\{module:\\3,folder:\\2\\}';
|
|
4
|
+
const QLE_REPLACEMENT = '$1(!0)?$5($3,$4(),$2):{module:$3,folder:$2}';
|
|
5
|
+
function countQleMatches(source) { return (source.match(new RegExp(QLE_PATTERN_SOURCE, 'g')) || []).length; }
|
|
6
|
+
function applyQlePatch(source) { return source.replace(new RegExp(QLE_PATTERN_SOURCE, 'g'), QLE_REPLACEMENT); }
|
|
7
|
+
module.exports = { QLE_PATTERN_SOURCE, QLE_REPLACEMENT, countQleMatches, applyQlePatch };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('assert/strict');
|
|
3
|
+
const { countQleMatches, applyQlePatch } = require('./hooks-standalone-pattern.js');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
test('countQleMatches finds pattern once in simple example', () => {
|
|
8
|
+
const source = 'var qle=(e,o,r)=>uu()?ar(o,r(),e):{module:o,folder:e};';
|
|
9
|
+
const count = countQleMatches(source);
|
|
10
|
+
assert.equal(count, 1);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('applyQlePatch replaces uu() with (!0)', () => {
|
|
14
|
+
const source = 'var qle=(e,o,r)=>uu()?ar(o,r(),e):{module:o,folder:e};';
|
|
15
|
+
const expected = 'var qle=(e,o,r)=>(!0)?ar(o,r(),e):{module:o,folder:e};';
|
|
16
|
+
const result = applyQlePatch(source);
|
|
17
|
+
assert.equal(result, expected);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('applyQlePatch is idempotent', () => {
|
|
21
|
+
const source = 'var qle=(e,o,r)=>uu()?ar(o,r(),e):{module:o,folder:e};';
|
|
22
|
+
const patched = applyQlePatch(source);
|
|
23
|
+
const repatch = applyQlePatch(patched);
|
|
24
|
+
assert.equal(repatch, patched);
|
|
25
|
+
assert.equal(countQleMatches(patched), 0);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('countQleMatches finds pattern with different minify names', () => {
|
|
29
|
+
const source = 'var vCe=(a,b,c)=>H1()?zz(b,c(),a):{module:b,folder:a};';
|
|
30
|
+
const count = countQleMatches(source);
|
|
31
|
+
assert.equal(count, 1);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('applyQlePatch works with different minify names', () => {
|
|
35
|
+
const source = 'var vCe=(a,b,c)=>H1()?zz(b,c(),a):{module:b,folder:a};';
|
|
36
|
+
const expected = 'var vCe=(a,b,c)=>(!0)?zz(b,c(),a):{module:b,folder:a};';
|
|
37
|
+
const result = applyQlePatch(source);
|
|
38
|
+
assert.equal(result, expected);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('countQleMatches returns 0 for mismatched pattern (wrong field order)', () => {
|
|
42
|
+
const source = 'var qle=(e,o,r)=>uu()?ar(o,r(),e):{module:e,folder:o};'; // swapped
|
|
43
|
+
const count = countQleMatches(source);
|
|
44
|
+
assert.equal(count, 0);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('countQleMatches returns 0 for import.meta.dirname only', () => {
|
|
48
|
+
const source = 'import.meta.dirname';
|
|
49
|
+
const count = countQleMatches(source);
|
|
50
|
+
assert.equal(count, 0);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('countQleMatches returns 0 for unrelated arrow function', () => {
|
|
54
|
+
const source = 'const f = (a, b, c) => someFunc() ? a : b;';
|
|
55
|
+
const count = countQleMatches(source);
|
|
56
|
+
assert.equal(count, 0);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('countQleMatches finds multiple patterns', () => {
|
|
60
|
+
const source = 'var qle=(e,o,r)=>uu()?ar(o,r(),e):{module:o,folder:e}; var qle2=(a,b,c)=>uu()?ar(b,c(),a):{module:b,folder:a};';
|
|
61
|
+
const count = countQleMatches(source);
|
|
62
|
+
assert.equal(count, 2);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('real chunk from 2.1.278 (if exists)', () => {
|
|
66
|
+
const versions = [
|
|
67
|
+
path.join(process.env.HOME || '.', '.claude-termux-native-package', 'versions', '2.1.278', 'launcher-workdir'),
|
|
68
|
+
];
|
|
69
|
+
const testRun = versions.some((vdir) => {
|
|
70
|
+
const chunks = [];
|
|
71
|
+
if (fs.existsSync(vdir)) {
|
|
72
|
+
const esmDir = fs.readdirSync(vdir).find((d) => d.startsWith('esm.'));
|
|
73
|
+
if (esmDir) {
|
|
74
|
+
const chunkDir = path.join(vdir, esmDir);
|
|
75
|
+
const chunkFile = fs.readdirSync(chunkDir).find((f) => f.match(/^chunk-7nckpqfv\.js$/));
|
|
76
|
+
if (chunkFile) {
|
|
77
|
+
const source = fs.readFileSync(path.join(chunkDir, chunkFile), 'utf8');
|
|
78
|
+
const count = countQleMatches(source);
|
|
79
|
+
assert.equal(count, 1, `chunk-7nckpqfv.js should have exactly 1 match, found ${count}`);
|
|
80
|
+
chunks.push({ file: chunkFile, count });
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
});
|
|
87
|
+
if (!testRun) {
|
|
88
|
+
test.skip();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('real chunk from 2.1.276 (if exists)', () => {
|
|
93
|
+
const versions = [
|
|
94
|
+
path.join(process.env.HOME || '.', '.claude-termux-native-package', 'versions', '2.1.276', 'launcher-workdir'),
|
|
95
|
+
];
|
|
96
|
+
const testRun = versions.some((vdir) => {
|
|
97
|
+
if (fs.existsSync(vdir)) {
|
|
98
|
+
const esmDir = fs.readdirSync(vdir).find((d) => d.startsWith('esm.'));
|
|
99
|
+
if (esmDir) {
|
|
100
|
+
const chunkDir = path.join(vdir, esmDir);
|
|
101
|
+
const chunkFile = fs.readdirSync(chunkDir).find((f) => f.match(/^chunk-0sa9p840\.js$/));
|
|
102
|
+
if (chunkFile) {
|
|
103
|
+
const source = fs.readFileSync(path.join(chunkDir, chunkFile), 'utf8');
|
|
104
|
+
const count = countQleMatches(source);
|
|
105
|
+
assert.equal(count, 1, `chunk-0sa9p840.js should have exactly 1 match, found ${count}`);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
});
|
|
112
|
+
if (!testRun) {
|
|
113
|
+
test.skip();
|
|
114
|
+
}
|
|
115
|
+
});
|
|
@@ -305,6 +305,12 @@ function stableHash(value, seed) {
|
|
|
305
305
|
return hash >>> 0;
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
+
stableHash.xxHash64 = function bunHashXxHash64(value, seed) {
|
|
309
|
+
const lo = stableHash(value, seed);
|
|
310
|
+
const hi = stableHash(value, lo ^ 0x85ebca6b);
|
|
311
|
+
return (BigInt(hi) << 32n) | BigInt(lo);
|
|
312
|
+
};
|
|
313
|
+
|
|
308
314
|
function replaceRequired(source, pattern, replacement, label, expectedCount) {
|
|
309
315
|
const text = String(source);
|
|
310
316
|
const matches = text.match(pattern);
|
|
@@ -712,6 +718,17 @@ async function esmChunkedMain() {
|
|
|
712
718
|
// 読み込み失敗時は空配列のまま(fail-closed、既存の同期require経路にフォールバック)
|
|
713
719
|
}
|
|
714
720
|
|
|
721
|
+
let hooksMetadata = { status: 'read-failed', patches: [] };
|
|
722
|
+
try {
|
|
723
|
+
const auditedVersionsForHooks = require(path.join(process.env.CLAUDE_TERMUX_PACKAGE_DIR, 'config', 'claude-native-audited-versions.json'));
|
|
724
|
+
const hooksEntry = auditedVersionsForHooks.versions?.[process.env.CURRENT_CLAUDE_VERSION];
|
|
725
|
+
if (!hooksEntry) hooksMetadata = { status: 'entry-missing', patches: [] };
|
|
726
|
+
else if (Array.isArray(hooksEntry.hooks_standalone_patches)) hooksMetadata = { status: 'ok', patches: hooksEntry.hooks_standalone_patches };
|
|
727
|
+
else hooksMetadata = { status: 'field-absent', patches: [] };
|
|
728
|
+
} catch {
|
|
729
|
+
// 既定の read-failed のまま(fail-visible: ローダーが警告する)
|
|
730
|
+
}
|
|
731
|
+
|
|
715
732
|
const loaderMod = require(path.join(libDir, 'bunfs-esm-loader.mjs'));
|
|
716
733
|
loaderMod.initialize({
|
|
717
734
|
processOwnedDir: ownedDir,
|
|
@@ -720,6 +737,7 @@ async function esmChunkedMain() {
|
|
|
720
737
|
vmGuardPath: path.join(libDir, 'bunfs-vm-guard.mjs'),
|
|
721
738
|
wsStubPath: path.join(libDir, 'bunfs-ws-stub.mjs'),
|
|
722
739
|
cycleHoists,
|
|
740
|
+
hooksMetadata,
|
|
723
741
|
reExtract: (sb, od) => extractToProcessOwnedDir(sb, od),
|
|
724
742
|
});
|
|
725
743
|
loaderMod.installFsBunfsInterception();
|
|
@@ -1349,6 +1367,12 @@ function stableHash(value, seed) {
|
|
|
1349
1367
|
return hash >>> 0;
|
|
1350
1368
|
}
|
|
1351
1369
|
|
|
1370
|
+
stableHash.xxHash64 = function bunHashXxHash64(value, seed) {
|
|
1371
|
+
const lo = stableHash(value, seed);
|
|
1372
|
+
const hi = stableHash(value, lo ^ 0x85ebca6b);
|
|
1373
|
+
return (BigInt(hi) << 32n) | BigInt(lo);
|
|
1374
|
+
};
|
|
1375
|
+
|
|
1352
1376
|
function replaceRequired(source, pattern, replacement, label, expectedCount) {
|
|
1353
1377
|
const text = String(source);
|
|
1354
1378
|
const matches = text.match(pattern);
|
|
@@ -1756,6 +1780,17 @@ async function esmChunkedMain() {
|
|
|
1756
1780
|
// 読み込み失敗時は空配列のまま(fail-closed、既存の同期require経路にフォールバック)
|
|
1757
1781
|
}
|
|
1758
1782
|
|
|
1783
|
+
let hooksMetadata = { status: 'read-failed', patches: [] };
|
|
1784
|
+
try {
|
|
1785
|
+
const auditedVersionsForHooks = require(path.join(process.env.CLAUDE_TERMUX_PACKAGE_DIR, 'config', 'claude-native-audited-versions.json'));
|
|
1786
|
+
const hooksEntry = auditedVersionsForHooks.versions?.[process.env.CURRENT_CLAUDE_VERSION];
|
|
1787
|
+
if (!hooksEntry) hooksMetadata = { status: 'entry-missing', patches: [] };
|
|
1788
|
+
else if (Array.isArray(hooksEntry.hooks_standalone_patches)) hooksMetadata = { status: 'ok', patches: hooksEntry.hooks_standalone_patches };
|
|
1789
|
+
else hooksMetadata = { status: 'field-absent', patches: [] };
|
|
1790
|
+
} catch {
|
|
1791
|
+
// 既定の read-failed のまま(fail-visible: ローダーが警告する)
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1759
1794
|
const loaderMod = require(path.join(libDir, 'bunfs-esm-loader.mjs'));
|
|
1760
1795
|
loaderMod.initialize({
|
|
1761
1796
|
processOwnedDir: ownedDir,
|
|
@@ -1764,6 +1799,7 @@ async function esmChunkedMain() {
|
|
|
1764
1799
|
vmGuardPath: path.join(libDir, 'bunfs-vm-guard.mjs'),
|
|
1765
1800
|
wsStubPath: path.join(libDir, 'bunfs-ws-stub.mjs'),
|
|
1766
1801
|
cycleHoists,
|
|
1802
|
+
hooksMetadata,
|
|
1767
1803
|
reExtract: (sb, od) => extractToProcessOwnedDir(sb, od),
|
|
1768
1804
|
});
|
|
1769
1805
|
loaderMod.installFsBunfsInterception();
|
|
@@ -2163,3 +2163,163 @@ test('CellSegmenter: fresh instance pool initialization (NB-2)', () => {
|
|
|
2163
2163
|
assert.ok(count2 > 0, 'should successfully segment after reset');
|
|
2164
2164
|
assert.equal(d2.graphemes.length, count2, 'grapheme count should match segment result');
|
|
2165
2165
|
});
|
|
2166
|
+
|
|
2167
|
+
test('stableHash.xxHash64 implementation (G2 v3 implementation)', () => {
|
|
2168
|
+
const api = loadHelperApi();
|
|
2169
|
+
const { stableHash } = api;
|
|
2170
|
+
|
|
2171
|
+
// Test 1: Base64 representation of xxHash64 result is valid (1-13 chars in base36)
|
|
2172
|
+
const result1 = stableHash.xxHash64('x').toString(36);
|
|
2173
|
+
assert.match(result1, /^[0-9a-z]{1,13}$/, `xxHash64('x').toString(36) should match [0-9a-z]{1,13}, got: ${result1}`);
|
|
2174
|
+
|
|
2175
|
+
// Test 2: seedless mode with fixed literal values - hi !== lo
|
|
2176
|
+
const fixedLiterals = ['a', 'hello', 'thinking:1562789', 'thinking:1779192', '', 'x'.repeat(100)];
|
|
2177
|
+
for (const val of fixedLiterals) {
|
|
2178
|
+
const h = stableHash.xxHash64(val);
|
|
2179
|
+
const hi = h >> 32n;
|
|
2180
|
+
const lo = h & 0xffffffffn;
|
|
2181
|
+
assert.ok(hi !== lo, `xxHash64('${val.slice(0, 20)}${val.length > 20 ? '...' : ''}') should have hi !== lo (got hi=${hi}, lo=${lo})`);
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
// Test 3: seed specified mode - hi !== lo
|
|
2185
|
+
const seededPairs = [
|
|
2186
|
+
['x', 42],
|
|
2187
|
+
['hello', 1234],
|
|
2188
|
+
['test', 9999],
|
|
2189
|
+
];
|
|
2190
|
+
for (const [val, seed] of seededPairs) {
|
|
2191
|
+
const h = stableHash.xxHash64(val, seed);
|
|
2192
|
+
const hi = h >> 32n;
|
|
2193
|
+
const lo = h & 0xffffffffn;
|
|
2194
|
+
assert.ok(hi !== lo, `xxHash64('${val}', ${seed}) should have hi !== lo (got hi=${hi}, lo=${lo})`);
|
|
2195
|
+
}
|
|
2196
|
+
|
|
2197
|
+
// Test 4: Deterministic (repeated calls return same result)
|
|
2198
|
+
const val = 'determinism-test';
|
|
2199
|
+
const r1 = stableHash.xxHash64(val);
|
|
2200
|
+
const r2 = stableHash.xxHash64(val);
|
|
2201
|
+
const r3 = stableHash.xxHash64(val);
|
|
2202
|
+
assert.equal(r1, r2, `xxHash64 should be deterministic (1st vs 2nd call)`);
|
|
2203
|
+
assert.equal(r2, r3, `xxHash64 should be deterministic (2nd vs 3rd call)`);
|
|
2204
|
+
|
|
2205
|
+
// Test 5: Return type is bigint
|
|
2206
|
+
const result5 = stableHash.xxHash64('x');
|
|
2207
|
+
assert.equal(typeof result5, 'bigint', `xxHash64 should return bigint, got: ${typeof result5}`);
|
|
2208
|
+
|
|
2209
|
+
// Test 6: Verify .xxHash64 is accessible in ESM shim context
|
|
2210
|
+
// Use the same logic as the "実行モード網羅" section of the design doc
|
|
2211
|
+
function extractEsmShimSource(block) {
|
|
2212
|
+
const sm = '\n globalThis.Bun = {';
|
|
2213
|
+
const em = '\n globalThis.__claudeBun = globalThis.Bun;';
|
|
2214
|
+
const s = block.indexOf(sm);
|
|
2215
|
+
const e = block.indexOf(em, s);
|
|
2216
|
+
return block.slice(s + 1, e + em.length);
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
// Verify for both _helper and _bootstrap heredocs
|
|
2220
|
+
for (const markerKey of ['cat <<\'NODE\' > "$_helper"', 'cat <<\'NODE\' > "$_bootstrap"']) {
|
|
2221
|
+
const helperBlock = extractBlock(markerKey, '\n export ENABLE_CLAUDEAI_MCP_SERVERS=');
|
|
2222
|
+
const stableHashSource = extractFunction(
|
|
2223
|
+
helperBlock,
|
|
2224
|
+
'function stableHash(value, seed) {',
|
|
2225
|
+
'\n\nfunction replaceRequired(source, pattern, replacement, label, expectedCount) {',
|
|
2226
|
+
);
|
|
2227
|
+
const context = vm.createContext({
|
|
2228
|
+
stringWidth: () => 0, wrapAnsi: v => v, stripANSI: v => v,
|
|
2229
|
+
sliceAnsi: s => s, sleepSync: () => {}, CellSegmenter: class {},
|
|
2230
|
+
process: { versions: {} }, Buffer, require,
|
|
2231
|
+
});
|
|
2232
|
+
context.module = { exports: {} };
|
|
2233
|
+
vm.runInContext(
|
|
2234
|
+
`${stableHashSource}\n${extractEsmShimSource(helperBlock)}\nmodule.exports = { Bun: globalThis.Bun, stableHash };`,
|
|
2235
|
+
context,
|
|
2236
|
+
);
|
|
2237
|
+
const { Bun, stableHash: contextStableHash } = context.module.exports;
|
|
2238
|
+
assert.equal(Bun.hash, contextStableHash, `Bun.hash should be identical to stableHash in ${markerKey}`);
|
|
2239
|
+
assert.equal(typeof Bun.hash.xxHash64, 'function', `Bun.hash.xxHash64 should be a function in ${markerKey}`);
|
|
2240
|
+
const testResult = Bun.hash.xxHash64('x').toString(36);
|
|
2241
|
+
assert.match(testResult, /^[0-9a-z]{1,13}$/, `Bun.hash.xxHash64('x').toString(36) should match pattern in ${markerKey}, got: ${testResult}`);
|
|
2242
|
+
}
|
|
2243
|
+
});
|
|
2244
|
+
|
|
2245
|
+
test('hooksMetadata construction: both helper and bootstrap esmChunkedMain contain same code', () => {
|
|
2246
|
+
const fullScript = fs.readFileSync(scriptPath, 'utf8');
|
|
2247
|
+
|
|
2248
|
+
// Extract hooksMetadata construction snippets from both esmChunkedMain blocks
|
|
2249
|
+
// Pattern: "let hooksMetadata = { status: 'read-failed'..." through "} catch { ... }"
|
|
2250
|
+
const pattern = /let hooksMetadata = \{ status: 'read-failed', patches: \[\] \};[\s\S]*?} catch \{[\s\S]*?\}/g;
|
|
2251
|
+
const matches = fullScript.match(pattern);
|
|
2252
|
+
assert.equal(matches.length, 2, 'should find hooksMetadata code in both helper and bootstrap esmChunkedMain');
|
|
2253
|
+
|
|
2254
|
+
// Verify both are identical
|
|
2255
|
+
assert.equal(matches[0], matches[1], 'hooksMetadata construction should be identical in helper and bootstrap');
|
|
2256
|
+
|
|
2257
|
+
// Extract the first snippet for detailed verification
|
|
2258
|
+
const snippet = matches[0];
|
|
2259
|
+
assert.ok(snippet.includes('const auditedVersionsForHooks = require(path.join(process.env.CLAUDE_TERMUX_PACKAGE_DIR'), 'should construct auditedVersionsForHooks');
|
|
2260
|
+
assert.ok(snippet.includes('const hooksEntry = auditedVersionsForHooks.versions?.[process.env.CURRENT_CLAUDE_VERSION]'), 'should read hooksEntry');
|
|
2261
|
+
assert.ok(snippet.includes("status: 'entry-missing'"), 'should set entry-missing status');
|
|
2262
|
+
assert.ok(snippet.includes("status: 'ok'"), 'should set ok status');
|
|
2263
|
+
assert.ok(snippet.includes("status: 'field-absent'"), 'should set field-absent status');
|
|
2264
|
+
});
|
|
2265
|
+
|
|
2266
|
+
test('hooksMetadata snippet execution: 4 status cases work correctly', () => {
|
|
2267
|
+
const fullScript = fs.readFileSync(scriptPath, 'utf8');
|
|
2268
|
+
const pattern = /let hooksMetadata = \{ status: 'read-failed', patches: \[\] \};[\s\S]*?} catch \{[\s\S]*?\}/;
|
|
2269
|
+
const match = fullScript.match(pattern);
|
|
2270
|
+
assert.ok(match, 'should find hooksMetadata snippet');
|
|
2271
|
+
|
|
2272
|
+
const snippet = match[0];
|
|
2273
|
+
|
|
2274
|
+
// Case (a): require throws exception → 'read-failed'
|
|
2275
|
+
const testFnA = new Function('require', 'path', 'process', snippet + '; return hooksMetadata;');
|
|
2276
|
+
const resultA = testFnA(
|
|
2277
|
+
() => { throw new Error('not found'); },
|
|
2278
|
+
{ join: () => 'dummy' },
|
|
2279
|
+
{ env: { CLAUDE_TERMUX_PACKAGE_DIR: 'dummy', CURRENT_CLAUDE_VERSION: '1.0.0' } }
|
|
2280
|
+
);
|
|
2281
|
+
assert.equal(resultA.status, 'read-failed');
|
|
2282
|
+
assert.deepEqual(resultA.patches, []);
|
|
2283
|
+
|
|
2284
|
+
// Case (b): hooksEntry is undefined → 'entry-missing'
|
|
2285
|
+
const testFnB = new Function('require', 'path', 'process', snippet + '; return hooksMetadata;');
|
|
2286
|
+
const resultB = testFnB(
|
|
2287
|
+
(p) => ({ versions: {} }), // no entry for version
|
|
2288
|
+
{ join: () => 'dummy' },
|
|
2289
|
+
{ env: { CLAUDE_TERMUX_PACKAGE_DIR: 'dummy', CURRENT_CLAUDE_VERSION: 'no.such.version' } }
|
|
2290
|
+
);
|
|
2291
|
+
assert.equal(resultB.status, 'entry-missing');
|
|
2292
|
+
|
|
2293
|
+
// Case (c): hooks_standalone_patches is not an array → 'field-absent'
|
|
2294
|
+
const testFnC = new Function('require', 'path', 'process', snippet + '; return hooksMetadata;');
|
|
2295
|
+
const resultC = testFnC(
|
|
2296
|
+
(p) => ({ versions: { '1.0.0': { hooks_standalone_patches: 'not_array' } } }),
|
|
2297
|
+
{ join: () => 'dummy' },
|
|
2298
|
+
{ env: { CLAUDE_TERMUX_PACKAGE_DIR: 'dummy', CURRENT_CLAUDE_VERSION: '1.0.0' } }
|
|
2299
|
+
);
|
|
2300
|
+
assert.equal(resultC.status, 'field-absent');
|
|
2301
|
+
|
|
2302
|
+
// Case (d): valid array → 'ok' with patches
|
|
2303
|
+
const testFnD = new Function('require', 'path', 'process', snippet + '; return hooksMetadata;');
|
|
2304
|
+
const patches = [{ file: 'test.js', expectedOccurrences: 1 }];
|
|
2305
|
+
const resultD = testFnD(
|
|
2306
|
+
(p) => ({ versions: { '1.0.0': { hooks_standalone_patches: patches } } }),
|
|
2307
|
+
{ join: () => 'dummy' },
|
|
2308
|
+
{ env: { CLAUDE_TERMUX_PACKAGE_DIR: 'dummy', CURRENT_CLAUDE_VERSION: '1.0.0' } }
|
|
2309
|
+
);
|
|
2310
|
+
assert.equal(resultD.status, 'ok');
|
|
2311
|
+
assert.deepEqual(resultD.patches, patches);
|
|
2312
|
+
});
|
|
2313
|
+
|
|
2314
|
+
test('hooksMetadata passed to loaderMod.initialize in both esmChunkedMain', () => {
|
|
2315
|
+
const fullScript = fs.readFileSync(scriptPath, 'utf8');
|
|
2316
|
+
|
|
2317
|
+
// Both helper and bootstrap should have loaderMod.initialize({ ... hooksMetadata, ... })
|
|
2318
|
+
const helperBlock = extractBlock('cat <<\'NODE\' > "$_helper"', '\n export ENABLE_CLAUDEAI_MCP_SERVERS=');
|
|
2319
|
+
assert.match(helperBlock, /loaderMod\.initialize\(\{[\s\S]*?hooksMetadata,[\s\S]*?\}/, 'helper: hooksMetadata should be passed to initialize');
|
|
2320
|
+
|
|
2321
|
+
// Count occurrences: should see hooksMetadata in initialize arguments
|
|
2322
|
+
const initCalls = fullScript.match(/loaderMod\.initialize\(\{/g) || [];
|
|
2323
|
+
const hooksMetadataInInit = fullScript.match(/hooksMetadata,/g) || [];
|
|
2324
|
+
assert.ok(hooksMetadataInInit.length >= 2, 'hooksMetadata should appear in at least 2 initialize calls (helper + bootstrap)');
|
|
2325
|
+
});
|