@niccrow/optimusctx 1.2.4

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.
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const { spawnSync } = require('node:child_process');
5
+
6
+ const { resolveRuntimeTarget, runtimeBinaryPath } = require('../lib/install');
7
+
8
+ const WINDOWS_BINARY_NAME = 'optimusctx.exe';
9
+
10
+ function main() {
11
+ const target = resolveRuntimeTarget();
12
+ const binaryPath = runtimeBinaryPath(target);
13
+ const result = spawnSync(binaryPath, process.argv.slice(2), { stdio: 'inherit' });
14
+
15
+ if (result.error) {
16
+ if (result.error.code === 'ENOENT') {
17
+ console.error(
18
+ `OptimusCtx binary not found at ${binaryPath}. Reinstall the package so node ./lib/install.js can download the tagged runtime into the package-local runtime/ directory, including ${WINDOWS_BINARY_NAME} on Windows.`
19
+ );
20
+ } else {
21
+ console.error(result.error.message);
22
+ }
23
+ process.exit(1);
24
+ }
25
+
26
+ process.exit(result.status ?? 0);
27
+ }
28
+
29
+ main();
package/lib/install.js ADDED
@@ -0,0 +1,197 @@
1
+ 'use strict';
2
+
3
+ const crypto = require('node:crypto');
4
+ const fs = require('node:fs');
5
+ const https = require('node:https');
6
+ const os = require('node:os');
7
+ const path = require('node:path');
8
+ const { pipeline } = require('node:stream/promises');
9
+ const { execFileSync } = require('node:child_process');
10
+
11
+ const { resolvePlatform } = require('./platform');
12
+
13
+ const PACKAGE_ROOT = path.join(__dirname, '..');
14
+ const PACKAGE_JSON_PATH = path.join(PACKAGE_ROOT, 'package.json');
15
+ const RUNTIME_ROOT = path.join(PACKAGE_ROOT, 'runtime');
16
+ const PACKAGE_RUNTIME_DIRECTORY = 'runtime/';
17
+ const ARCHIVE_NAME_TEMPLATE = 'optimusctx_${versionNoV}_${goos}_${goarch}';
18
+
19
+ function readPackageMetadata() {
20
+ return JSON.parse(fs.readFileSync(PACKAGE_JSON_PATH, 'utf8'));
21
+ }
22
+
23
+ function runtimeBinaryPath(target) {
24
+ return path.join(RUNTIME_ROOT, target.runtimeDir, target.binaryName);
25
+ }
26
+
27
+ function resolveRuntimeTarget(platform = process.platform, arch = process.arch) {
28
+ const target = resolvePlatform(platform, arch);
29
+ const packageMetadata = readPackageMetadata();
30
+ const platformMetadata = packageMetadata.optimusctx.platforms[`${target.goos}-${target.goarch}`];
31
+
32
+ if (!platformMetadata) {
33
+ throw new Error(`No release metadata found for ${target.goos}-${target.goarch}.`);
34
+ }
35
+
36
+ return {
37
+ ...target,
38
+ archiveFileName: platformMetadata.archive,
39
+ archiveURL: platformMetadata.archiveUrl,
40
+ archiveFormat: platformMetadata.archiveFormat,
41
+ checksumFileName: packageMetadata.optimusctx.checksumManifest.file,
42
+ checksumURL: packageMetadata.optimusctx.checksumManifest.url,
43
+ };
44
+ }
45
+
46
+ function fetch(url) {
47
+ return new Promise((resolve, reject) => {
48
+ const request = https.get(url, (response) => {
49
+ const statusCode = response.statusCode || 0;
50
+
51
+ if ([301, 302, 303, 307, 308].includes(statusCode)) {
52
+ response.resume();
53
+ if (!response.headers.location) {
54
+ reject(new Error(`Redirect from ${url} did not include a location header.`));
55
+ return;
56
+ }
57
+ resolve(fetch(response.headers.location));
58
+ return;
59
+ }
60
+
61
+ if (statusCode < 200 || statusCode >= 300) {
62
+ response.resume();
63
+ reject(new Error(`Download failed for ${url}: HTTP ${statusCode}`));
64
+ return;
65
+ }
66
+
67
+ resolve(response);
68
+ });
69
+
70
+ request.on('error', reject);
71
+ });
72
+ }
73
+
74
+ async function downloadToFile(url, destinationPath) {
75
+ const response = await fetch(url);
76
+ await fs.promises.mkdir(path.dirname(destinationPath), { recursive: true });
77
+ await pipeline(response, fs.createWriteStream(destinationPath));
78
+ return destinationPath;
79
+ }
80
+
81
+ async function downloadText(url) {
82
+ const response = await fetch(url);
83
+ const chunks = [];
84
+ for await (const chunk of response) {
85
+ chunks.push(Buffer.from(chunk));
86
+ }
87
+ return Buffer.concat(chunks).toString('utf8');
88
+ }
89
+
90
+ function parseChecksumManifest(content) {
91
+ const checksums = new Map();
92
+ for (const line of content.split('\n')) {
93
+ const trimmed = line.trim();
94
+ if (!trimmed) {
95
+ continue;
96
+ }
97
+ const [sha256, fileName] = trimmed.split(/\s+/, 2);
98
+ if (!sha256 || !fileName) {
99
+ throw new Error(`Invalid checksum manifest line: ${line}`);
100
+ }
101
+ checksums.set(fileName, sha256);
102
+ }
103
+ return checksums;
104
+ }
105
+
106
+ async function sha256ForFile(filePath) {
107
+ const hash = crypto.createHash('sha256');
108
+ for await (const chunk of fs.createReadStream(filePath)) {
109
+ hash.update(chunk);
110
+ }
111
+ return hash.digest('hex');
112
+ }
113
+
114
+ function verifyChecksum(archivePath, expectedSHA256) {
115
+ return sha256ForFile(archivePath).then((actualSHA256) => {
116
+ if (actualSHA256 !== expectedSHA256) {
117
+ throw new Error(`Checksum verification failed for ${archivePath}: expected ${expectedSHA256}, got ${actualSHA256}`);
118
+ }
119
+ });
120
+ }
121
+
122
+ function extractArchive(archivePath, destinationPath, archiveFormat) {
123
+ fs.mkdirSync(destinationPath, { recursive: true });
124
+
125
+ if (archiveFormat === 'zip') {
126
+ execFileSync('powershell', [
127
+ '-NoProfile',
128
+ '-Command',
129
+ `Expand-Archive -LiteralPath '${archivePath.replace(/'/g, "''")}' -DestinationPath '${destinationPath.replace(/'/g, "''")}' -Force`,
130
+ ], { stdio: 'inherit' });
131
+ return;
132
+ }
133
+
134
+ execFileSync('tar', ['-xzf', archivePath, '-C', destinationPath], { stdio: 'inherit' });
135
+ }
136
+
137
+ async function install(platform = process.platform, arch = process.arch) {
138
+ const target = resolveRuntimeTarget(platform, arch);
139
+ const binaryPath = runtimeBinaryPath(target);
140
+
141
+ if (fs.existsSync(binaryPath)) {
142
+ return binaryPath;
143
+ }
144
+
145
+ const scratchRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'optimusctx-npm-'));
146
+ const archivePath = path.join(scratchRoot, target.archiveFileName);
147
+
148
+ try {
149
+ const checksumManifest = await downloadText(target.checksumURL);
150
+ const checksums = parseChecksumManifest(checksumManifest);
151
+ const expectedSHA256 = checksums.get(target.archiveFileName);
152
+ if (!expectedSHA256) {
153
+ throw new Error(
154
+ `Checksum manifest ${target.checksumFileName} is missing ${target.archiveFileName}; expected the ${ARCHIVE_NAME_TEMPLATE} release naming contract.`
155
+ );
156
+ }
157
+
158
+ await downloadToFile(target.archiveURL, archivePath);
159
+ await verifyChecksum(archivePath, expectedSHA256);
160
+
161
+ const destinationPath = path.join(RUNTIME_ROOT, target.runtimeDir);
162
+ await fs.promises.rm(destinationPath, { recursive: true, force: true });
163
+ extractArchive(archivePath, destinationPath, target.archiveFormat);
164
+
165
+ if (!fs.existsSync(binaryPath)) {
166
+ throw new Error(`Archive extracted successfully but ${binaryPath} was not created under ${PACKAGE_RUNTIME_DIRECTORY}.`);
167
+ }
168
+ if (target.goos !== 'windows') {
169
+ await fs.promises.chmod(binaryPath, 0o755);
170
+ }
171
+
172
+ return binaryPath;
173
+ } finally {
174
+ await fs.promises.rm(scratchRoot, { recursive: true, force: true });
175
+ }
176
+ }
177
+
178
+ async function main() {
179
+ try {
180
+ await install();
181
+ } catch (error) {
182
+ console.error(error.message);
183
+ process.exit(1);
184
+ }
185
+ }
186
+
187
+ module.exports = {
188
+ install,
189
+ parseChecksumManifest,
190
+ readPackageMetadata,
191
+ resolveRuntimeTarget,
192
+ runtimeBinaryPath,
193
+ };
194
+
195
+ if (require.main === module) {
196
+ void main();
197
+ }
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const OS_MAP = Object.freeze({
4
+ darwin: 'darwin',
5
+ linux: 'linux',
6
+ win32: 'windows',
7
+ });
8
+
9
+ const ARCH_MAP = Object.freeze({
10
+ x64: 'amd64',
11
+ arm64: 'arm64',
12
+ });
13
+
14
+ function runtimeBinaryName(goos) {
15
+ return goos === 'windows' ? 'optimusctx.exe' : 'optimusctx';
16
+ }
17
+
18
+ function resolvePlatform(platform = process.platform, arch = process.arch) {
19
+ const goos = OS_MAP[platform];
20
+ const goarch = ARCH_MAP[arch];
21
+
22
+ if (!goos || !goarch) {
23
+ throw new Error(`Unsupported platform ${platform}/${arch}; supported targets are darwin|linux|windows on amd64|arm64.`);
24
+ }
25
+
26
+ return {
27
+ goos,
28
+ goarch,
29
+ runtimeDir: `${goos}-${goarch}`,
30
+ binaryName: runtimeBinaryName(goos),
31
+ };
32
+ }
33
+
34
+ module.exports = {
35
+ ARCH_MAP,
36
+ OS_MAP,
37
+ resolvePlatform,
38
+ runtimeBinaryName,
39
+ };
package/package.json ADDED
@@ -0,0 +1,98 @@
1
+ {
2
+ "name": "@niccrow/optimusctx",
3
+ "version": "1.2.4",
4
+ "description": "Local-first runtime that builds and maintains persistent repository context for coding agents.",
5
+ "license": "MIT",
6
+ "homepage": "https://github.com/NicoMoralesDev/optimusctx",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/NicoMoralesDev/optimusctx.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/NicoMoralesDev/optimusctx/issues"
13
+ },
14
+ "engines": {
15
+ "node": ">=18"
16
+ },
17
+ "bin": {
18
+ "optimusctx": "bin/optimusctx.js"
19
+ },
20
+ "scripts": {
21
+ "postinstall": "node ./lib/install.js"
22
+ },
23
+ "files": [
24
+ "bin",
25
+ "lib"
26
+ ],
27
+ "optimusctx": {
28
+ "command": "optimusctx",
29
+ "launcher": "./bin/optimusctx.js",
30
+ "projectName": "optimusctx",
31
+ "repository": {
32
+ "owner": "NicoMoralesDev",
33
+ "name": "optimusctx"
34
+ },
35
+ "releaseTag": "v1.2.4",
36
+ "version": "1.2.4",
37
+ "checksumManifest": {
38
+ "file": "optimusctx_1.2.4_checksums.txt",
39
+ "url": "https://github.com/NicoMoralesDev/optimusctx/releases/download/v1.2.4/optimusctx_1.2.4_checksums.txt"
40
+ },
41
+ "platforms": {
42
+ "darwin-amd64": {
43
+ "os": "darwin",
44
+ "arch": "amd64",
45
+ "archive": "optimusctx_1.2.4_darwin_amd64.tar.gz",
46
+ "archiveUrl": "https://github.com/NicoMoralesDev/optimusctx/releases/download/v1.2.4/optimusctx_1.2.4_darwin_amd64.tar.gz",
47
+ "archiveFormat": "tar.gz",
48
+ "binary": "optimusctx",
49
+ "runtimeDirectory": "darwin-amd64"
50
+ },
51
+ "darwin-arm64": {
52
+ "os": "darwin",
53
+ "arch": "arm64",
54
+ "archive": "optimusctx_1.2.4_darwin_arm64.tar.gz",
55
+ "archiveUrl": "https://github.com/NicoMoralesDev/optimusctx/releases/download/v1.2.4/optimusctx_1.2.4_darwin_arm64.tar.gz",
56
+ "archiveFormat": "tar.gz",
57
+ "binary": "optimusctx",
58
+ "runtimeDirectory": "darwin-arm64"
59
+ },
60
+ "linux-amd64": {
61
+ "os": "linux",
62
+ "arch": "amd64",
63
+ "archive": "optimusctx_1.2.4_linux_amd64.tar.gz",
64
+ "archiveUrl": "https://github.com/NicoMoralesDev/optimusctx/releases/download/v1.2.4/optimusctx_1.2.4_linux_amd64.tar.gz",
65
+ "archiveFormat": "tar.gz",
66
+ "binary": "optimusctx",
67
+ "runtimeDirectory": "linux-amd64"
68
+ },
69
+ "linux-arm64": {
70
+ "os": "linux",
71
+ "arch": "arm64",
72
+ "archive": "optimusctx_1.2.4_linux_arm64.tar.gz",
73
+ "archiveUrl": "https://github.com/NicoMoralesDev/optimusctx/releases/download/v1.2.4/optimusctx_1.2.4_linux_arm64.tar.gz",
74
+ "archiveFormat": "tar.gz",
75
+ "binary": "optimusctx",
76
+ "runtimeDirectory": "linux-arm64"
77
+ },
78
+ "windows-amd64": {
79
+ "os": "windows",
80
+ "arch": "amd64",
81
+ "archive": "optimusctx_1.2.4_windows_amd64.zip",
82
+ "archiveUrl": "https://github.com/NicoMoralesDev/optimusctx/releases/download/v1.2.4/optimusctx_1.2.4_windows_amd64.zip",
83
+ "archiveFormat": "zip",
84
+ "binary": "optimusctx.exe",
85
+ "runtimeDirectory": "windows-amd64"
86
+ },
87
+ "windows-arm64": {
88
+ "os": "windows",
89
+ "arch": "arm64",
90
+ "archive": "optimusctx_1.2.4_windows_arm64.zip",
91
+ "archiveUrl": "https://github.com/NicoMoralesDev/optimusctx/releases/download/v1.2.4/optimusctx_1.2.4_windows_arm64.zip",
92
+ "archiveFormat": "zip",
93
+ "binary": "optimusctx.exe",
94
+ "runtimeDirectory": "windows-arm64"
95
+ }
96
+ }
97
+ }
98
+ }