shared-credentials-concurrent-process.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { createSharedZCodeCredentialStore } from "../../src/auth/shared-credentials.js";
  2. process.send?.({ type: "ready" });
  3. process.once(
  4. "message",
  5. async (message: {
  6. filePath?: unknown;
  7. keysPerProcess?: unknown;
  8. operation?: unknown;
  9. processIndex?: unknown;
  10. type?: unknown;
  11. }) => {
  12. if (
  13. message.type !== "start" ||
  14. typeof message.filePath !== "string" ||
  15. (message.operation !== "read-corrupt" &&
  16. (typeof message.keysPerProcess !== "number" || typeof message.processIndex !== "number"))
  17. ) {
  18. process.send?.({ error: "invalid worker input", type: "error" });
  19. process.disconnect?.();
  20. return;
  21. }
  22. try {
  23. const store = createSharedZCodeCredentialStore({
  24. env: { ZCODE_CREDENTIAL_SECRET: "test-secret" },
  25. filePath: message.filePath,
  26. });
  27. if (message.operation === "read-corrupt") {
  28. await store.load("corrupt-probe").then(
  29. () => {
  30. throw new Error("corrupt credential read unexpectedly succeeded");
  31. },
  32. () => undefined,
  33. );
  34. process.send?.({ type: "done" }, () => process.disconnect?.());
  35. return;
  36. }
  37. for (let keyIndex = 0; keyIndex < message.keysPerProcess; keyIndex += 1) {
  38. await store.save(
  39. `worker-${message.processIndex}:key-${keyIndex}`,
  40. `value-${message.processIndex}-${keyIndex}`,
  41. );
  42. }
  43. process.send?.({ type: "done" }, () => process.disconnect?.());
  44. } catch (error) {
  45. process.send?.(
  46. {
  47. error: error instanceof Error ? (error.stack ?? error.message) : String(error),
  48. type: "error",
  49. },
  50. () => process.disconnect?.(),
  51. );
  52. }
  53. },
  54. );