Skip to content

Commit b66fb86

Browse files
committed
Make Netlify FOC worker self-configuring
1 parent 8107c65 commit b66fb86

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

packages/cli/src/index.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,24 +2507,42 @@ export async function runFocUploadFromJob(job) {
25072507
25082508
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'tokenhost-netlify-upload-'));
25092509
const tmpFile = path.join(tmpDir, job.fileName || 'upload.bin');
2510+
const runtimeHome = path.join(os.tmpdir(), 'tokenhost-netlify-home');
2511+
const xdgConfigHome = path.join(runtimeHome, '.config');
2512+
const npmCacheDir = path.join(runtimeHome, '.npm');
2513+
const normalizedPrivateKey = privateKey.startsWith('0x') ? privateKey : \`0x\${privateKey}\`;
25102514
25112515
try {
2516+
await fs.mkdir(runtimeHome, { recursive: true });
2517+
await fs.mkdir(xdgConfigHome, { recursive: true });
2518+
await fs.mkdir(npmCacheDir, { recursive: true });
25122519
await fs.writeFile(tmpFile, Buffer.from(String(job.bodyBase64 || ''), 'base64'));
25132520
const command = String(process.env.TH_UPLOAD_FOC_COMMAND ?? DEFAULT_FOC_COMMAND).trim() || DEFAULT_FOC_COMMAND;
25142521
const chainId = parsePositiveInt(process.env.TH_UPLOAD_FOC_CHAIN, DEFAULT_CHAIN_ID);
25152522
const copies = parsePositiveInt(process.env.TH_UPLOAD_FOC_COPIES, DEFAULT_COPIES);
25162523
const withCDN = String(process.env.TH_UPLOAD_FOC_WITH_CDN ?? '').trim().toLowerCase();
25172524
const debug = String(process.env.TH_UPLOAD_FOC_DEBUG ?? '').trim().toLowerCase();
2525+
const sharedEnv = {
2526+
...process.env,
2527+
HOME: runtimeHome,
2528+
XDG_CONFIG_HOME: xdgConfigHome,
2529+
NPM_CONFIG_CACHE: npmCacheDir,
2530+
npm_config_cache: npmCacheDir,
2531+
NPM_CONFIG_UPDATE_NOTIFIER: 'false',
2532+
PRIVATE_KEY: privateKey,
2533+
TH_UPLOAD_FOC_PRIVATE_KEY: privateKey
2534+
};
2535+
const focConfigDir = path.join(xdgConfigHome, 'foc-cli-nodejs');
2536+
const focConfigPath = path.join(focConfigDir, 'config.json');
2537+
await fs.mkdir(focConfigDir, { recursive: true });
2538+
await fs.writeFile(focConfigPath, JSON.stringify({ privateKey: normalizedPrivateKey }) + '\\n', 'utf8');
2539+
25182540
const shellCommand =
25192541
\`\${command} upload \${shellQuote(tmpFile)} --format json --chain \${chainId} --copies \${copies}\` +
25202542
((withCDN === '1' || withCDN === 'true' || withCDN === 'yes' || withCDN === 'on') ? ' --withCDN true' : '') +
25212543
((debug === '1' || debug === 'true' || debug === 'yes' || debug === 'on') ? ' --debug true --verbose' : '');
25222544
2523-
const result = await runShellCommand(shellCommand, {
2524-
...process.env,
2525-
PRIVATE_KEY: privateKey,
2526-
TH_UPLOAD_FOC_PRIVATE_KEY: privateKey
2527-
});
2545+
const result = await runShellCommand(shellCommand, sharedEnv);
25282546
25292547
if (result.code !== 0) {
25302548
throw new Error(String(result.stderr || result.stdout || \`foc-cli failed with status \${result.code}\`).trim());

test/fixtures/fake-foc-cli.mjs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
#!/usr/bin/env node
22

3-
const payload = {
4-
ok: true,
5-
result: {
6-
pieceCid: 'bafkqaaaafakecidfornetlifyuploadtest',
7-
size: 321,
8-
copyResults: [
9-
{
10-
url: 'https://calibration.example.invalid/piece/bafkqaaaafakecidfornetlifyuploadtest'
11-
}
12-
]
13-
}
14-
};
3+
const args = process.argv.slice(2);
154

16-
process.stdout.write(JSON.stringify(payload));
5+
if (args[0] === 'wallet' && args[1] === 'init') {
6+
process.stdout.write(JSON.stringify({ ok: true, initialized: true }));
7+
process.exit(0);
8+
}
9+
10+
if (args[0] === 'upload') {
11+
const payload = {
12+
ok: true,
13+
result: {
14+
pieceCid: 'bafkqaaaafakecidfornetlifyuploadtest',
15+
size: 321,
16+
copyResults: [
17+
{
18+
url: 'https://calibration.example.invalid/piece/bafkqaaaafakecidfornetlifyuploadtest'
19+
}
20+
]
21+
}
22+
};
23+
24+
process.stdout.write(JSON.stringify(payload));
25+
process.exit(0);
26+
}
27+
28+
process.stderr.write(`Unexpected fake foc-cli args: ${args.join(' ')}\n`);
29+
process.exit(1);

0 commit comments

Comments
 (0)