@ricsam/r5d-worker 0.0.3 → 0.0.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.
package/dist/cjs/main.cjs CHANGED
@@ -37,6 +37,7 @@ const MAX_SYNC_DIFF_BYTES = 5 * 1024 * 1024;
37
37
  const activeProcesses = /* @__PURE__ */ new Map();
38
38
  const activePtys = /* @__PURE__ */ new Map();
39
39
  const branchLocks = /* @__PURE__ */ new Map();
40
+ let containerRegistryCredential = null;
40
41
  function defaultConfigPath() {
41
42
  return import_node_path.default.join(import_node_os.default.homedir(), ".config", "r5d", "r5dctl", "config.json");
42
43
  }
@@ -447,6 +448,68 @@ function configureVisibleGitHubAuth(branchPath, remoteUrl, authHeader) {
447
448
  }
448
449
  runGit(["config", `${gitExtraHeaderConfigKey(gitExtraHeaderUrlForRemote(remoteUrl))}`, authHeader], { cwd: branchPath });
449
450
  }
451
+ function dockerConfigPath() {
452
+ return import_node_path.default.join(import_node_os.default.homedir(), ".docker", "config.json");
453
+ }
454
+ function containersAuthPath() {
455
+ return import_node_path.default.join(import_node_os.default.homedir(), ".config", "containers", "auth.json");
456
+ }
457
+ function readJsonFile(filePath) {
458
+ if (!import_node_fs.default.existsSync(filePath)) {
459
+ return {};
460
+ }
461
+ try {
462
+ const parsed = JSON.parse(import_node_fs.default.readFileSync(filePath, "utf8"));
463
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
464
+ } catch {
465
+ return {};
466
+ }
467
+ }
468
+ function writeRegistryAuthFile(filePath, credential) {
469
+ const config = readJsonFile(filePath);
470
+ const existingAuths = config.auths && typeof config.auths === "object" && !Array.isArray(config.auths) ? config.auths : {};
471
+ const auths = existingAuths;
472
+ auths[credential.registry] = {
473
+ username: credential.username,
474
+ password: credential.password,
475
+ auth: Buffer.from(`${credential.username}:${credential.password}`).toString("base64")
476
+ };
477
+ config.auths = auths;
478
+ import_node_fs.default.mkdirSync(import_node_path.default.dirname(filePath), { recursive: true });
479
+ import_node_fs.default.writeFileSync(filePath, `${JSON.stringify(config, null, 2)}
480
+ `, { mode: 384 });
481
+ import_node_fs.default.chmodSync(filePath, 384);
482
+ }
483
+ function configureContainerRegistryAuth(credential) {
484
+ containerRegistryCredential = credential;
485
+ if (!credential) {
486
+ return;
487
+ }
488
+ writeRegistryAuthFile(dockerConfigPath(), credential);
489
+ writeRegistryAuthFile(containersAuthPath(), credential);
490
+ if (credential.missingScopes.length > 0) {
491
+ process.stderr.write(
492
+ `[r5d-worker] GitHub token is missing GHCR scope(s): ${credential.missingScopes.join(", ")}. Sign in with GitHub again if GHCR push/pull fails.
493
+ `
494
+ );
495
+ } else {
496
+ process.stdout.write(`[r5d-worker] configured GHCR auth for ${credential.username}
497
+ `);
498
+ }
499
+ }
500
+ function containerRegistryEnv() {
501
+ if (!containerRegistryCredential) {
502
+ return {};
503
+ }
504
+ return {
505
+ R5D_GHCR_REGISTRY: containerRegistryCredential.registry,
506
+ R5D_GHCR_NAMESPACE: containerRegistryCredential.username,
507
+ R5D_GHCR_AUTH_FILE: containersAuthPath(),
508
+ REGISTRY_AUTH_FILE: containersAuthPath(),
509
+ GHCR_REGISTRY: containerRegistryCredential.registry,
510
+ GHCR_NAMESPACE: containerRegistryCredential.username
511
+ };
512
+ }
450
513
  function checkoutVisibleBranch(input) {
451
514
  const branchExists = tryGit(["show-ref", "--verify", "--quiet", `refs/heads/${input.branchName}`], { cwd: input.branchPath });
452
515
  const remoteBranchExists = tryGit(["show-ref", "--verify", "--quiet", `refs/remotes/origin/${input.branchName}`], {
@@ -888,6 +951,7 @@ async function executeCommand(input) {
888
951
  stderr: "pipe",
889
952
  env: {
890
953
  ...process.env,
954
+ ...containerRegistryEnv(),
891
955
  ...input.message.env ?? {}
892
956
  }
893
957
  });
@@ -1134,6 +1198,7 @@ function openPty(input) {
1134
1198
  cwd: branchPath.branchPath,
1135
1199
  env: {
1136
1200
  ...process.env,
1201
+ ...containerRegistryEnv(),
1137
1202
  ...input.message.env ?? {},
1138
1203
  BUILD_IT_NOW_PROJECT_ID: input.projectId,
1139
1204
  BUILD_IT_NOW_BRANCH_NAME: input.message.branchName
@@ -1274,6 +1339,7 @@ async function startWorker(options) {
1274
1339
  return;
1275
1340
  }
1276
1341
  if (message.type === "project_manifest") {
1342
+ configureContainerRegistryAuth(message.githubContainerRegistry);
1277
1343
  manifestByProjectId.clear();
1278
1344
  for (const project of message.projects) {
1279
1345
  manifestByProjectId.set(project.projectId, project);
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/r5d-worker",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "type": "commonjs"
5
5
  }
package/dist/mjs/main.mjs CHANGED
@@ -14,6 +14,7 @@ const MAX_SYNC_DIFF_BYTES = 5 * 1024 * 1024;
14
14
  const activeProcesses = /* @__PURE__ */ new Map();
15
15
  const activePtys = /* @__PURE__ */ new Map();
16
16
  const branchLocks = /* @__PURE__ */ new Map();
17
+ let containerRegistryCredential = null;
17
18
  function defaultConfigPath() {
18
19
  return path.join(os.homedir(), ".config", "r5d", "r5dctl", "config.json");
19
20
  }
@@ -424,6 +425,68 @@ function configureVisibleGitHubAuth(branchPath, remoteUrl, authHeader) {
424
425
  }
425
426
  runGit(["config", `${gitExtraHeaderConfigKey(gitExtraHeaderUrlForRemote(remoteUrl))}`, authHeader], { cwd: branchPath });
426
427
  }
428
+ function dockerConfigPath() {
429
+ return path.join(os.homedir(), ".docker", "config.json");
430
+ }
431
+ function containersAuthPath() {
432
+ return path.join(os.homedir(), ".config", "containers", "auth.json");
433
+ }
434
+ function readJsonFile(filePath) {
435
+ if (!fs.existsSync(filePath)) {
436
+ return {};
437
+ }
438
+ try {
439
+ const parsed = JSON.parse(fs.readFileSync(filePath, "utf8"));
440
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
441
+ } catch {
442
+ return {};
443
+ }
444
+ }
445
+ function writeRegistryAuthFile(filePath, credential) {
446
+ const config = readJsonFile(filePath);
447
+ const existingAuths = config.auths && typeof config.auths === "object" && !Array.isArray(config.auths) ? config.auths : {};
448
+ const auths = existingAuths;
449
+ auths[credential.registry] = {
450
+ username: credential.username,
451
+ password: credential.password,
452
+ auth: Buffer.from(`${credential.username}:${credential.password}`).toString("base64")
453
+ };
454
+ config.auths = auths;
455
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
456
+ fs.writeFileSync(filePath, `${JSON.stringify(config, null, 2)}
457
+ `, { mode: 384 });
458
+ fs.chmodSync(filePath, 384);
459
+ }
460
+ function configureContainerRegistryAuth(credential) {
461
+ containerRegistryCredential = credential;
462
+ if (!credential) {
463
+ return;
464
+ }
465
+ writeRegistryAuthFile(dockerConfigPath(), credential);
466
+ writeRegistryAuthFile(containersAuthPath(), credential);
467
+ if (credential.missingScopes.length > 0) {
468
+ process.stderr.write(
469
+ `[r5d-worker] GitHub token is missing GHCR scope(s): ${credential.missingScopes.join(", ")}. Sign in with GitHub again if GHCR push/pull fails.
470
+ `
471
+ );
472
+ } else {
473
+ process.stdout.write(`[r5d-worker] configured GHCR auth for ${credential.username}
474
+ `);
475
+ }
476
+ }
477
+ function containerRegistryEnv() {
478
+ if (!containerRegistryCredential) {
479
+ return {};
480
+ }
481
+ return {
482
+ R5D_GHCR_REGISTRY: containerRegistryCredential.registry,
483
+ R5D_GHCR_NAMESPACE: containerRegistryCredential.username,
484
+ R5D_GHCR_AUTH_FILE: containersAuthPath(),
485
+ REGISTRY_AUTH_FILE: containersAuthPath(),
486
+ GHCR_REGISTRY: containerRegistryCredential.registry,
487
+ GHCR_NAMESPACE: containerRegistryCredential.username
488
+ };
489
+ }
427
490
  function checkoutVisibleBranch(input) {
428
491
  const branchExists = tryGit(["show-ref", "--verify", "--quiet", `refs/heads/${input.branchName}`], { cwd: input.branchPath });
429
492
  const remoteBranchExists = tryGit(["show-ref", "--verify", "--quiet", `refs/remotes/origin/${input.branchName}`], {
@@ -865,6 +928,7 @@ async function executeCommand(input) {
865
928
  stderr: "pipe",
866
929
  env: {
867
930
  ...process.env,
931
+ ...containerRegistryEnv(),
868
932
  ...input.message.env ?? {}
869
933
  }
870
934
  });
@@ -1111,6 +1175,7 @@ function openPty(input) {
1111
1175
  cwd: branchPath.branchPath,
1112
1176
  env: {
1113
1177
  ...process.env,
1178
+ ...containerRegistryEnv(),
1114
1179
  ...input.message.env ?? {},
1115
1180
  BUILD_IT_NOW_PROJECT_ID: input.projectId,
1116
1181
  BUILD_IT_NOW_BRANCH_NAME: input.message.branchName
@@ -1251,6 +1316,7 @@ async function startWorker(options) {
1251
1316
  return;
1252
1317
  }
1253
1318
  if (message.type === "project_manifest") {
1319
+ configureContainerRegistryAuth(message.githubContainerRegistry);
1254
1320
  manifestByProjectId.clear();
1255
1321
  for (const project of message.projects) {
1256
1322
  manifestByProjectId.set(project.projectId, project);
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/r5d-worker",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "type": "module"
5
5
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ricsam/r5d-worker",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "main": "./dist/cjs/main.cjs",
6
6
  "module": "./dist/mjs/main.mjs",