@ship.zone/runner 2.0.0 → 3.0.0

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.
Files changed (180) hide show
  1. package/.smartconfig.json +101 -3
  2. package/changelog.md +69 -0
  3. package/dist_rust/runnerworkspace_linux_amd64_musl +0 -0
  4. package/dist_rust/runnerworkspace_linux_amd64_musl.third-party-notices.json +679 -0
  5. package/dist_rust/runnerworkspace_linux_amd64_musl.third-party-notices.txt +15719 -0
  6. package/dist_rust/runnerworkspace_linux_amd64_musl.tsrust-build.json +5 -5
  7. package/dist_rust/runnerworkspace_linux_arm64_musl +0 -0
  8. package/dist_rust/runnerworkspace_linux_arm64_musl.third-party-notices.json +679 -0
  9. package/dist_rust/runnerworkspace_linux_arm64_musl.third-party-notices.txt +15719 -0
  10. package/dist_rust/runnerworkspace_linux_arm64_musl.tsrust-build.json +5 -5
  11. package/dist_ts/00_commitinfo_data.d.ts +8 -0
  12. package/dist_ts/00_commitinfo_data.js +9 -0
  13. package/dist_ts/api-client.d.ts +46 -0
  14. package/dist_ts/api-client.js +592 -0
  15. package/dist_ts/artifact-manager.d.ts +11 -0
  16. package/dist_ts/artifact-manager.js +140 -0
  17. package/dist_ts/attempt-state.d.ts +18 -0
  18. package/dist_ts/attempt-state.js +151 -0
  19. package/dist_ts/cache-manager.d.ts +13 -0
  20. package/dist_ts/cache-manager.js +300 -0
  21. package/dist_ts/cli-entry.d.ts +2 -0
  22. package/dist_ts/cli-entry.js +4 -0
  23. package/dist_ts/cli.d.ts +14 -0
  24. package/dist_ts/cli.js +215 -0
  25. package/dist_ts/config.d.ts +8 -0
  26. package/dist_ts/config.js +445 -0
  27. package/dist_ts/errors.d.ts +41 -0
  28. package/dist_ts/errors.js +90 -0
  29. package/dist_ts/filesystem.d.ts +73 -0
  30. package/dist_ts/filesystem.js +207 -0
  31. package/dist_ts/index.d.ts +16 -0
  32. package/dist_ts/index.js +17 -0
  33. package/dist_ts/interfaces.d.ts +614 -0
  34. package/dist_ts/interfaces.js +3 -0
  35. package/dist_ts/log-sender.d.ts +26 -0
  36. package/dist_ts/log-sender.js +124 -0
  37. package/dist_ts/oci-executor.d.ts +155 -0
  38. package/dist_ts/oci-executor.js +2238 -0
  39. package/dist_ts/plugins.d.ts +19 -0
  40. package/dist_ts/plugins.js +23 -0
  41. package/dist_ts/protocol-validator.d.ts +39 -0
  42. package/dist_ts/protocol-validator.js +483 -0
  43. package/dist_ts/redactor.d.ts +78 -0
  44. package/dist_ts/redactor.js +235 -0
  45. package/dist_ts/registration-state.d.ts +16 -0
  46. package/dist_ts/registration-state.js +171 -0
  47. package/dist_ts/runner.d.ts +64 -0
  48. package/dist_ts/runner.js +882 -0
  49. package/dist_ts/source-workspace.d.ts +10 -0
  50. package/dist_ts/source-workspace.js +125 -0
  51. package/dist_ts/spec-version.d.ts +11 -0
  52. package/dist_ts/spec-version.js +41 -0
  53. package/dist_ts/staging-scavenger.d.ts +7 -0
  54. package/dist_ts/staging-scavenger.js +63 -0
  55. package/dist_ts/state-owner.d.ts +74 -0
  56. package/dist_ts/state-owner.js +289 -0
  57. package/dist_ts/transfer-archive.d.ts +78 -0
  58. package/dist_ts/transfer-archive.js +1619 -0
  59. package/dist_ts/validation.d.ts +11 -0
  60. package/dist_ts/validation.js +79 -0
  61. package/dist_ts/wire-schema.d.ts +10 -0
  62. package/dist_ts/wire-schema.js +114 -0
  63. package/native-notices/crates/bitflags-2.13.1/license-apache +201 -0
  64. package/native-notices/crates/bitflags-2.13.1/license-mit +25 -0
  65. package/native-notices/crates/cc-1.8.0/license-apache +201 -0
  66. package/native-notices/crates/cc-1.8.0/license-mit +25 -0
  67. package/native-notices/crates/cfg-if-1.0.4/license-apache +201 -0
  68. package/native-notices/crates/cfg-if-1.0.4/license-mit +25 -0
  69. package/native-notices/crates/cfg_aliases-0.2.2/license +9 -0
  70. package/native-notices/crates/cfg_aliases-0.2.2/notices.md +26 -0
  71. package/native-notices/crates/find-msvc-tools-0.1.14/license-apache +201 -0
  72. package/native-notices/crates/find-msvc-tools-0.1.14/license-mit +25 -0
  73. package/native-notices/crates/itoa-1.0.18/license-apache +176 -0
  74. package/native-notices/crates/itoa-1.0.18/license-mit +23 -0
  75. package/native-notices/crates/libc-0.2.189/license-apache +176 -0
  76. package/native-notices/crates/libc-0.2.189/license-mit +25 -0
  77. package/native-notices/crates/memchr-2.8.3/copying +3 -0
  78. package/native-notices/crates/memchr-2.8.3/license-mit +21 -0
  79. package/native-notices/crates/memchr-2.8.3/unlicense +24 -0
  80. package/native-notices/crates/nix-0.30.1/license +21 -0
  81. package/native-notices/crates/proc-macro2-1.0.107/license-apache +176 -0
  82. package/native-notices/crates/proc-macro2-1.0.107/license-mit +23 -0
  83. package/native-notices/crates/quote-1.0.47/license-apache +176 -0
  84. package/native-notices/crates/quote-1.0.47/license-mit +23 -0
  85. package/native-notices/crates/serde-1.0.229/license-apache +176 -0
  86. package/native-notices/crates/serde-1.0.229/license-mit +23 -0
  87. package/native-notices/crates/serde_core-1.0.229/license-apache +176 -0
  88. package/native-notices/crates/serde_core-1.0.229/license-mit +23 -0
  89. package/native-notices/crates/serde_derive-1.0.229/license-apache +176 -0
  90. package/native-notices/crates/serde_derive-1.0.229/license-mit +23 -0
  91. package/native-notices/crates/serde_json-1.0.151/license-apache +176 -0
  92. package/native-notices/crates/serde_json-1.0.151/license-mit +23 -0
  93. package/native-notices/crates/shlex-2.0.1/license-apache +13 -0
  94. package/native-notices/crates/shlex-2.0.1/license-mit +21 -0
  95. package/native-notices/crates/syn-3.0.3/license-apache +176 -0
  96. package/native-notices/crates/syn-3.0.3/license-mit +23 -0
  97. package/native-notices/crates/tikv-jemalloc-sys-0.7.1+5.3.1-0-g81034ce1f1373e37dc865038e1bc8eeecf559ce8/license-apache +201 -0
  98. package/native-notices/crates/tikv-jemalloc-sys-0.7.1+5.3.1-0-g81034ce1f1373e37dc865038e1bc8eeecf559ce8/license-mit +25 -0
  99. package/native-notices/crates/tikv-jemallocator-0.7.0/license-apache +201 -0
  100. package/native-notices/crates/tikv-jemallocator-0.7.0/license-mit +25 -0
  101. package/native-notices/crates/unicode-ident-1.0.24/license-apache +176 -0
  102. package/native-notices/crates/unicode-ident-1.0.24/license-mit +23 -0
  103. package/native-notices/crates/unicode-ident-1.0.24/license-unicode +39 -0
  104. package/native-notices/crates/zmij-1.0.23/license-mit +23 -0
  105. package/native-notices/manifest.json +763 -0
  106. package/native-notices/native/tikv-jemalloc-sys-0.7.1+5.3.1-0-g81034ce1f1373e37dc865038e1bc8eeecf559ce8/jemalloc-5.3.1/jemalloc-copying +27 -0
  107. package/native-notices/readme.md +46 -0
  108. package/native-notices/runtime/gcc/gpl-3.0.txt +674 -0
  109. package/native-notices/runtime/gcc/runtime-library-exception-3.1.txt +73 -0
  110. package/native-notices/runtime/libunwind-22.1.2/license.txt +311 -0
  111. package/native-notices/runtime/musl-1.2.5/copyright +193 -0
  112. package/native-notices/rust/compiler-builtins-license.txt +275 -0
  113. package/native-notices/rust/copyright-library.html +8266 -0
  114. package/native-notices/rust/crates/addr2line-0.25.1/license-apache +201 -0
  115. package/native-notices/rust/crates/addr2line-0.25.1/license-mit +25 -0
  116. package/native-notices/rust/crates/adler2-2.0.1/license-0bsd +12 -0
  117. package/native-notices/rust/crates/adler2-2.0.1/license-apache +201 -0
  118. package/native-notices/rust/crates/adler2-2.0.1/license-mit +23 -0
  119. package/native-notices/rust/crates/memchr-2.7.6/copying +3 -0
  120. package/native-notices/rust/crates/memchr-2.7.6/license-mit +21 -0
  121. package/native-notices/rust/crates/memchr-2.7.6/unlicense +24 -0
  122. package/native-notices/rust/crates/miniz_oxide-0.8.9/license +25 -0
  123. package/native-notices/rust/crates/miniz_oxide-0.8.9/license-apache.md +177 -0
  124. package/native-notices/rust/crates/miniz_oxide-0.8.9/license-mit.md +24 -0
  125. package/native-notices/rust/crates/miniz_oxide-0.8.9/license-zlib.md +14 -0
  126. package/native-notices/rust/crates/object-0.37.3/license-apache +201 -0
  127. package/native-notices/rust/crates/object-0.37.3/license-mit +25 -0
  128. package/native-notices/rust/licenses/apache-2.0.txt +73 -0
  129. package/native-notices/rust/licenses/bsd-2-clause.txt +9 -0
  130. package/native-notices/rust/licenses/cc-by-sa-4.0.txt +427 -0
  131. package/native-notices/rust/licenses/gcc-exception-3.1.txt +30 -0
  132. package/native-notices/rust/licenses/gpl-2.0-only.txt +133 -0
  133. package/native-notices/rust/licenses/gpl-3.0-or-later.txt +202 -0
  134. package/native-notices/rust/licenses/isc.txt +7 -0
  135. package/native-notices/rust/licenses/llvm-exception.txt +15 -0
  136. package/native-notices/rust/licenses/mit.txt +9 -0
  137. package/native-notices/rust/licenses/ncsa.txt +29 -0
  138. package/native-notices/rust/licenses/ofl-1.1.txt +43 -0
  139. package/native-notices/rust/licenses/unicode-3.0.txt +39 -0
  140. package/package.json +24 -21
  141. package/readme.md +91 -40
  142. package/rust/.cargo/config.toml +1 -1
  143. package/rust/Cargo.lock +43 -0
  144. package/rust/Cargo.toml +4 -1
  145. package/rust/src/lib.rs +156 -1
  146. package/rust/src/main.rs +5 -1
  147. package/rust-toolchain.toml +4 -0
  148. package/ts/00_commitinfo_data.ts +1 -1
  149. package/ts/api-client.ts +10 -7
  150. package/ts/artifact-manager.ts +9 -7
  151. package/ts/attempt-state.ts +75 -84
  152. package/ts/cache-manager.ts +22 -28
  153. package/ts/cli-entry.ts +4 -0
  154. package/ts/cli.ts +56 -45
  155. package/ts/config.ts +50 -39
  156. package/ts/errors.ts +19 -0
  157. package/ts/filesystem.ts +316 -0
  158. package/ts/index.ts +16 -15
  159. package/ts/interfaces.ts +2 -2
  160. package/ts/log-sender.ts +4 -4
  161. package/ts/oci-executor.ts +235 -383
  162. package/ts/plugins.ts +21 -3
  163. package/ts/protocol-validator.ts +5 -5
  164. package/ts/redactor.ts +2 -2
  165. package/ts/registration-state.ts +82 -90
  166. package/ts/runner.ts +30 -17
  167. package/ts/source-workspace.ts +11 -8
  168. package/ts/spec-version.ts +2 -2
  169. package/ts/staging-scavenger.ts +37 -26
  170. package/ts/state-owner.ts +346 -0
  171. package/ts/transfer-archive.ts +214 -243
  172. package/ts/validation.ts +1 -1
  173. package/ts/wire-schema.ts +3 -3
  174. package/deno.json +0 -56
  175. package/deno.lock +0 -1762
  176. package/dist_npm/mod.d.ts +0 -967
  177. package/dist_npm/mod.js +0 -10058
  178. package/mod.ts +0 -8
  179. package/musl-copyright.md +0 -81
  180. package/third-party-licenses.md +0 -95
package/ts/config.ts CHANGED
@@ -1,7 +1,29 @@
1
- import { dirname, isAbsolute, join } from '@std/path';
2
- import { ConfigurationError } from './errors.ts';
3
- import type { IOciExecutorConfig, IRunnerConfig, IRunnerCredential } from './interfaces.ts';
4
- import { isRecord, validateHttpUrl } from './validation.ts';
1
+ import * as plugins from './plugins.js';
2
+ import { ConfigurationError } from './errors.js';
3
+ import {
4
+ createNewWriteFlags,
5
+ isNotFound,
6
+ removePath,
7
+ syncDirectory,
8
+ withOpenFile,
9
+ writeAll,
10
+ } from './filesystem.js';
11
+ import type { IOciExecutorConfig, IRunnerConfig, IRunnerCredential } from './interfaces.js';
12
+ import { isRecord, validateHttpUrl } from './validation.js';
13
+
14
+ const { dirname, isAbsolute, join } = plugins.path;
15
+
16
+ /** The host architecture label in the spelling of the runner's original default (`uname -m`). */
17
+ const hostArchitectureLabel = (): string => {
18
+ switch (process.arch) {
19
+ case 'x64':
20
+ return 'x86_64';
21
+ case 'arm64':
22
+ return 'aarch64';
23
+ default:
24
+ return process.arch;
25
+ }
26
+ };
5
27
 
6
28
  const identifierPattern = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
7
29
  const timestampPattern =
@@ -31,8 +53,8 @@ const defaultOciConfig: IOciExecutorConfig = {
31
53
  export const defaultRunnerConfig = (): IRunnerConfig => ({
32
54
  controlPlaneUrl: '',
33
55
  allowInsecureLoopback: false,
34
- name: `runner-${Deno.env.get('HOSTNAME') ?? 'local'}`,
35
- labels: ['linux', Deno.build.arch, 'oci'],
56
+ name: `runner-${process.env.HOSTNAME ?? 'local'}`,
57
+ labels: ['linux', hostArchitectureLabel(), 'oci'],
36
58
  stateDirectory: './.runner-state',
37
59
  transferStagingReserveBytes: 1024 * 1024 * 1024,
38
60
  transferStagingReserveInodes: 10_000,
@@ -673,10 +695,10 @@ const applyEnvironment = (value: Record<string, unknown>): Record<string, unknow
673
695
  ['SHIP_RUNNER_STATE_DIRECTORY', 'stateDirectory'],
674
696
  ];
675
697
  for (const [environmentName, field] of mappings) {
676
- const environmentValue = Deno.env.get(environmentName);
698
+ const environmentValue = process.env[environmentName];
677
699
  if (environmentValue) result[field] = environmentValue;
678
700
  }
679
- const labels = Deno.env.get('SHIP_RUNNER_LABELS');
701
+ const labels = process.env.SHIP_RUNNER_LABELS;
680
702
  if (labels) result.labels = labels.split(',').map((entry) => entry.trim()).filter(Boolean);
681
703
  return result;
682
704
  };
@@ -684,11 +706,11 @@ const applyEnvironment = (value: Record<string, unknown>): Record<string, unknow
684
706
  export const loadRunnerConfig = async (path: string): Promise<IRunnerConfig> => {
685
707
  let input: Record<string, unknown> = {};
686
708
  try {
687
- const parsed: unknown = JSON.parse(await Deno.readTextFile(path));
709
+ const parsed: unknown = JSON.parse(await plugins.fs.readFile(path, 'utf8'));
688
710
  if (!isRecord(parsed)) throw new ConfigurationError('Runner config must be a JSON object');
689
711
  input = parsed;
690
712
  } catch (error) {
691
- if (!(error instanceof Deno.errors.NotFound)) throw error;
713
+ if (!isNotFound(error)) throw error;
692
714
  }
693
715
  return parseRunnerConfig(applyEnvironment(input));
694
716
  };
@@ -696,40 +718,29 @@ export const loadRunnerConfig = async (path: string): Promise<IRunnerConfig> =>
696
718
  export const saveRunnerConfig = async (path: string, config: IRunnerConfig): Promise<void> => {
697
719
  validateRunnerConfig(config);
698
720
  const parent = dirname(path);
699
- await Deno.mkdir(parent, { recursive: true, mode: 0o700 });
700
- const temporaryPath = join(parent, `.${crypto.randomUUID()}.runner-config.tmp`);
721
+ await plugins.fs.mkdir(parent, { recursive: true, mode: 0o700 });
722
+ const temporaryPath = join(parent, `.${plugins.crypto.randomUUID()}.runner-config.tmp`);
701
723
  const bytes = new TextEncoder().encode(`${JSON.stringify(config, null, 2)}\n`);
702
- let file: Deno.FsFile | undefined;
703
724
  try {
704
- file = await Deno.open(temporaryPath, {
705
- write: true,
706
- createNew: true,
707
- mode: 0o600,
708
- });
709
- let offset = 0;
710
- while (offset < bytes.byteLength) {
711
- const written = await file.write(bytes.subarray(offset));
712
- if (written <= 0) throw new ConfigurationError('Runner config write made no progress');
713
- offset += written;
714
- }
715
- await file.sync();
716
- file.close();
717
- file = undefined;
718
- await Deno.chmod(temporaryPath, 0o600);
719
- await Deno.rename(temporaryPath, path);
720
- await Deno.chmod(path, 0o600);
721
- const directory = await Deno.open(parent, { read: true });
722
- try {
723
- await directory.sync();
724
- } finally {
725
- directory.close();
726
- }
725
+ await withOpenFile(
726
+ temporaryPath,
727
+ createNewWriteFlags,
728
+ 0o600,
729
+ async (file) => {
730
+ await writeAll(file, bytes, 'Runner config write made no progress');
731
+ await file.sync();
732
+ },
733
+ 'Runner config write and close failed',
734
+ );
735
+ await plugins.fs.chmod(temporaryPath, 0o600);
736
+ await plugins.fs.rename(temporaryPath, path);
737
+ await plugins.fs.chmod(path, 0o600);
738
+ await syncDirectory(parent, 'Runner config directory sync and close failed');
727
739
  } catch (error) {
728
- file?.close();
729
740
  try {
730
- await Deno.remove(temporaryPath);
741
+ await removePath(temporaryPath);
731
742
  } catch (cleanupError) {
732
- if (!(cleanupError instanceof Deno.errors.NotFound)) {
743
+ if (!isNotFound(cleanupError)) {
733
744
  throw new AggregateError([error, cleanupError], 'Config write and cleanup failed');
734
745
  }
735
746
  }
package/ts/errors.ts CHANGED
@@ -77,3 +77,22 @@ export class SecretDisclosureError extends RunnerError {
77
77
  this.name = 'SecretDisclosureError';
78
78
  }
79
79
  }
80
+
81
+ /**
82
+ * Another live process holds the exclusive lock of the runner state directory. Two runners on one
83
+ * state directory are a configuration error; the second one stops at once instead of waiting.
84
+ */
85
+ export class RunnerStateOwnedElsewhereError extends ConfigurationError {
86
+ constructor(stateDirectory: string) {
87
+ super(`Runner state directory ${stateDirectory} is owned by another running runner`);
88
+ this.name = 'RunnerStateOwnedElsewhereError';
89
+ }
90
+ }
91
+
92
+ /** The helper process that held the runner state lock exited while the runner was still live. */
93
+ export class RunnerStateOwnershipLostError extends RunnerError {
94
+ constructor(message = 'Runner state ownership was lost') {
95
+ super(message);
96
+ this.name = 'RunnerStateOwnershipLostError';
97
+ }
98
+ }
@@ -0,0 +1,316 @@
1
+ import * as plugins from './plugins.js';
2
+ import { ResourceCleanupError, RunnerError } from './errors.js';
3
+
4
+ export type TFileHandle = plugins.fs.FileHandle;
5
+
6
+ /** Open flags for a new private file that must not exist yet. */
7
+ export const createNewWriteFlags = 'wx';
8
+ /** Open flags for an existing file that is rewritten from its start without being created. */
9
+ export const truncateExistingWriteFlags = (): number =>
10
+ plugins.fs.constants.O_WRONLY | plugins.fs.constants.O_TRUNC;
11
+
12
+ export interface IMakeTempOptions {
13
+ dir?: string;
14
+ prefix?: string;
15
+ suffix?: string;
16
+ }
17
+
18
+ export interface IRemoveOptions {
19
+ recursive?: boolean;
20
+ }
21
+
22
+ export const errorCode = (error: unknown): string | undefined => {
23
+ if (!(error instanceof Error)) return undefined;
24
+ const code = (error as NodeJS.ErrnoException).code;
25
+ return typeof code === 'string' ? code : undefined;
26
+ };
27
+
28
+ export const isNotFound = (error: unknown): boolean => errorCode(error) === 'ENOENT';
29
+
30
+ export const isAlreadyExists = (error: unknown): boolean => errorCode(error) === 'EEXIST';
31
+
32
+ /**
33
+ * Runs an operation on an open file handle and always closes the handle afterwards.
34
+ *
35
+ * A failed close is never hidden: it becomes a `ResourceCleanupError` that also carries the
36
+ * operation's failure, if any. Every file descriptor the runner opens goes through this helper.
37
+ */
38
+ export const withFileHandle = async <T>(
39
+ handle: TFileHandle,
40
+ operation: (handle: TFileHandle) => Promise<T>,
41
+ closeFailureMessage: string,
42
+ ): Promise<T> => {
43
+ let result: T | undefined;
44
+ let operationError: unknown;
45
+ let hasOperationError = false;
46
+ try {
47
+ result = await operation(handle);
48
+ } catch (error) {
49
+ operationError = error;
50
+ hasOperationError = true;
51
+ }
52
+ await closeAfterOperation(handle, hasOperationError, operationError, closeFailureMessage);
53
+ return result as T;
54
+ };
55
+
56
+ /**
57
+ * The async-generator form of `withFileHandle`: yields what `generate` yields and closes the
58
+ * handle when the generator completes, fails, or is ended early by its consumer.
59
+ */
60
+ export async function* withFileHandleChunks<T>(
61
+ handle: TFileHandle,
62
+ generate: (handle: TFileHandle) => AsyncGenerator<T>,
63
+ closeFailureMessage: string,
64
+ ): AsyncGenerator<T> {
65
+ let operationError: unknown;
66
+ let hasOperationError = false;
67
+ try {
68
+ yield* generate(handle);
69
+ } catch (error) {
70
+ operationError = error;
71
+ hasOperationError = true;
72
+ } finally {
73
+ await closeAfterOperation(handle, hasOperationError, operationError, closeFailureMessage);
74
+ }
75
+ }
76
+
77
+ const closeAfterOperation = async (
78
+ handle: TFileHandle,
79
+ hasOperationError: boolean,
80
+ operationError: unknown,
81
+ closeFailureMessage: string,
82
+ ): Promise<void> => {
83
+ let closeError: unknown;
84
+ let hasCloseError = false;
85
+ try {
86
+ await handle.close();
87
+ } catch (error) {
88
+ closeError = error;
89
+ hasCloseError = true;
90
+ }
91
+ if (hasCloseError) {
92
+ throw new ResourceCleanupError(closeFailureMessage, [
93
+ ...(hasOperationError ? [operationError] : []),
94
+ closeError,
95
+ ]);
96
+ }
97
+ if (hasOperationError) throw operationError;
98
+ };
99
+
100
+ /** Opens `path` and runs `operation` on the handle through `withFileHandle`. */
101
+ export const withOpenFile = async <T>(
102
+ path: string,
103
+ flags: string | number,
104
+ mode: number | undefined,
105
+ operation: (handle: TFileHandle) => Promise<T>,
106
+ closeFailureMessage: string,
107
+ ): Promise<T> =>
108
+ await withFileHandle(
109
+ await plugins.fs.open(path, flags, mode),
110
+ operation,
111
+ closeFailureMessage,
112
+ );
113
+
114
+ /** Writes every byte, failing when the kernel reports no progress. */
115
+ export const writeAll = async (
116
+ handle: TFileHandle,
117
+ bytes: Uint8Array,
118
+ noProgressMessage: string,
119
+ ): Promise<void> => {
120
+ let offset = 0;
121
+ while (offset < bytes.byteLength) {
122
+ const { bytesWritten } = await handle.write(bytes, offset, bytes.byteLength - offset);
123
+ if (bytesWritten <= 0) throw new RunnerError(noProgressMessage);
124
+ offset += bytesWritten;
125
+ }
126
+ };
127
+
128
+ /** Reads into `buffer` from the current position; returns `null` at end of file. */
129
+ export const readInto = async (
130
+ handle: TFileHandle,
131
+ buffer: Uint8Array,
132
+ ): Promise<number | null> => {
133
+ const { bytesRead } = await handle.read(buffer, 0, buffer.byteLength, null);
134
+ return bytesRead === 0 && buffer.byteLength > 0 ? null : bytesRead;
135
+ };
136
+
137
+ /** Flushes a directory entry change to stable storage by fsyncing the directory itself. */
138
+ export const syncDirectory = async (path: string, closeFailureMessage: string): Promise<void> =>
139
+ await withOpenFile(
140
+ path,
141
+ 'r',
142
+ undefined,
143
+ async (handle) => await handle.sync(),
144
+ closeFailureMessage,
145
+ );
146
+
147
+ export const lstatIfPresent = async (path: string): Promise<plugins.Stats | undefined> => {
148
+ try {
149
+ return await plugins.fs.lstat(path);
150
+ } catch (error) {
151
+ if (isNotFound(error)) return undefined;
152
+ throw error;
153
+ }
154
+ };
155
+
156
+ /** Creates a private directory with a random name; `mkdtemp` creates it with mode 0700. */
157
+ export const makeTempDir = async (options: IMakeTempOptions = {}): Promise<string> =>
158
+ await plugins.fs.mkdtemp(
159
+ `${plugins.path.resolve(options.dir ?? plugins.os.tmpdir())}${plugins.path.sep}${
160
+ options.prefix ?? ''
161
+ }`,
162
+ );
163
+
164
+ /**
165
+ * Creates an empty mode-0600 file with a random name through an exclusive create, so an existing
166
+ * path (including a planted symbolic link) is never reused.
167
+ */
168
+ export const makeTempFile = async (options: IMakeTempOptions = {}): Promise<string> => {
169
+ const path = plugins.path.join(
170
+ options.dir ?? plugins.os.tmpdir(),
171
+ `${options.prefix ?? ''}${plugins.crypto.randomBytes(16).toString('hex')}${
172
+ options.suffix ?? ''
173
+ }`,
174
+ );
175
+ await withOpenFile(
176
+ path,
177
+ createNewWriteFlags,
178
+ 0o600,
179
+ async () => undefined,
180
+ 'Temporary file creation and close failed',
181
+ );
182
+ return path;
183
+ };
184
+
185
+ /**
186
+ * Removes a file, a symbolic link or an empty directory, or a whole tree when `recursive` is set.
187
+ * A missing path is reported as `ENOENT`.
188
+ */
189
+ export const removePath = async (path: string, options: IRemoveOptions = {}): Promise<void> => {
190
+ if (options.recursive) {
191
+ await plugins.fs.rm(path, { recursive: true });
192
+ return;
193
+ }
194
+ try {
195
+ await plugins.fs.unlink(path);
196
+ } catch (error) {
197
+ if (errorCode(error) !== 'EISDIR') throw error;
198
+ await plugins.fs.rmdir(path);
199
+ }
200
+ };
201
+
202
+ /** True when both stats describe the same inode on the same device. */
203
+ export const sameFileIdentity = (first: plugins.Stats, second: plugins.Stats): boolean =>
204
+ second.dev === first.dev && second.ino === first.ino;
205
+
206
+ /**
207
+ * Reads a bounded regular file through one pinned descriptor and verifies that neither the path
208
+ * nor the descriptor changed while the exact bytes were read.
209
+ */
210
+ export const readBoundedRegularFile = async (
211
+ path: string,
212
+ maximumBytes: number,
213
+ label: string,
214
+ ): Promise<{ bytes: Uint8Array<ArrayBuffer>; stat: plugins.Stats }> => {
215
+ const pathStat = await plugins.fs.lstat(path);
216
+ if (
217
+ !pathStat.isFile() || pathStat.isSymbolicLink() || pathStat.size < 1 ||
218
+ pathStat.size > maximumBytes
219
+ ) {
220
+ throw new RunnerError(`${label} must be a bounded regular file`);
221
+ }
222
+ return await withOpenFile(
223
+ path,
224
+ 'r',
225
+ undefined,
226
+ async (handle) => {
227
+ const before = await handle.stat();
228
+ if (
229
+ !before.isFile() || before.size < 1 || before.size > maximumBytes ||
230
+ before.size !== pathStat.size || !sameFileIdentity(pathStat, before)
231
+ ) {
232
+ throw new RunnerError(`${label} changed while it was opened`);
233
+ }
234
+ const bytes = new Uint8Array(before.size);
235
+ let offset = 0;
236
+ while (offset < bytes.byteLength) {
237
+ const read = await readInto(handle, bytes.subarray(offset));
238
+ if (read === null || read <= 0) {
239
+ throw new RunnerError(`${label} changed while it was read`);
240
+ }
241
+ offset += read;
242
+ }
243
+ const overflow = await readInto(handle, new Uint8Array(1));
244
+ const after = await handle.stat();
245
+ const currentPathStat = await plugins.fs.lstat(path);
246
+ if (
247
+ overflow !== null || !after.isFile() || after.size !== before.size ||
248
+ !currentPathStat.isFile() || currentPathStat.isSymbolicLink() ||
249
+ currentPathStat.size !== before.size || !sameFileIdentity(before, after) ||
250
+ !sameFileIdentity(after, currentPathStat)
251
+ ) {
252
+ throw new RunnerError(`${label} changed while it was read`);
253
+ }
254
+ return { bytes, stat: after };
255
+ },
256
+ `${label} read and close failed`,
257
+ );
258
+ };
259
+
260
+ /** Returns the lowercase hexadecimal SHA-256 digest of `bytes`. */
261
+ export const sha256Hex = (bytes: Uint8Array): string =>
262
+ plugins.crypto.createHash('sha256').update(bytes).digest('hex');
263
+
264
+ /** Yields copies of the file content from the handle's current position until end of file. */
265
+ export async function* readFileChunks(
266
+ handle: TFileHandle,
267
+ chunkBytes: number,
268
+ signal?: AbortSignal,
269
+ ): AsyncGenerator<Uint8Array> {
270
+ const buffer = new Uint8Array(chunkBytes);
271
+ while (true) {
272
+ if (signal?.aborted) throw signal.reason;
273
+ const read = await readInto(handle, buffer);
274
+ if (read === null) return;
275
+ yield buffer.slice(0, read);
276
+ }
277
+ }
278
+
279
+ /**
280
+ * A pull-based byte stream over an async generator; canceling the stream ends the generator.
281
+ *
282
+ * The generator is started when the stream is created, so a stream canceled before its first read
283
+ * still runs the generator's cleanup instead of leaving a generator that never started.
284
+ */
285
+ export const asyncIteratorStream = (
286
+ iterator: AsyncGenerator<Uint8Array>,
287
+ ): ReadableStream<Uint8Array> => {
288
+ const forward = async (
289
+ controller: ReadableStreamDefaultController<Uint8Array>,
290
+ ): Promise<void> => {
291
+ const result = await iterator.next();
292
+ if (result.done) controller.close();
293
+ else controller.enqueue(result.value);
294
+ };
295
+ return new ReadableStream<Uint8Array>({
296
+ start: forward,
297
+ pull: forward,
298
+ async cancel() {
299
+ await iterator.return(undefined);
300
+ },
301
+ });
302
+ };
303
+
304
+ /** Opens `path` for reading as a byte stream that closes its descriptor when it ends. */
305
+ export const openReadableFile = async (
306
+ path: string,
307
+ chunkBytes: number,
308
+ closeFailureMessage: string,
309
+ ): Promise<ReadableStream<Uint8Array>> =>
310
+ asyncIteratorStream(
311
+ withFileHandleChunks(
312
+ await plugins.fs.open(path, 'r'),
313
+ (handle) => readFileChunks(handle, chunkBytes),
314
+ closeFailureMessage,
315
+ ),
316
+ );
package/ts/index.ts CHANGED
@@ -1,15 +1,16 @@
1
- export * from './api-client.ts';
2
- export * from './artifact-manager.ts';
3
- export * from './cache-manager.ts';
4
- export * from './config.ts';
5
- export * from './errors.ts';
6
- export * from './interfaces.ts';
7
- export * from './log-sender.ts';
8
- export * from './oci-executor.ts';
9
- export * from './protocol-validator.ts';
10
- export * from './redactor.ts';
11
- export * from './runner.ts';
12
- export * from './source-workspace.ts';
13
- export * from './spec-version.ts';
14
- export * from './staging-scavenger.ts';
15
- export * from './wire-schema.ts';
1
+ export * from './api-client.js';
2
+ export * from './artifact-manager.js';
3
+ export * from './cache-manager.js';
4
+ export * from './config.js';
5
+ export * from './errors.js';
6
+ export * from './interfaces.js';
7
+ export * from './log-sender.js';
8
+ export * from './oci-executor.js';
9
+ export * from './protocol-validator.js';
10
+ export * from './redactor.js';
11
+ export * from './runner.js';
12
+ export * from './source-workspace.js';
13
+ export * from './spec-version.js';
14
+ export * from './staging-scavenger.js';
15
+ export * from './state-owner.js';
16
+ export * from './wire-schema.js';
package/ts/interfaces.ts CHANGED
@@ -1,6 +1,6 @@
1
- import denoConfig from '../deno.json' with { type: 'json' };
1
+ import { commitinfo } from './00_commitinfo_data.js';
2
2
 
3
- export const runnerVersion: string = denoConfig.version;
3
+ export const runnerVersion: string = commitinfo.version;
4
4
 
5
5
  export type TExecutionProfile = 'oci' | 'oci-image' | 'vm';
6
6
  export type TIsolation = 'container' | 'microvm';
package/ts/log-sender.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { ProtocolError } from './errors.ts';
2
- import type { IJobIdentity, ILogAcknowledgement, ILogChunk, TLogStream } from './interfaces.ts';
1
+ import { ProtocolError } from './errors.js';
2
+ import type { IJobIdentity, ILogAcknowledgement, ILogChunk, TLogStream } from './interfaces.js';
3
3
  import {
4
4
  LogSequenceRedactor,
5
5
  type SecretRedactionSet,
6
6
  StreamingSecretRedactor,
7
- } from './redactor.ts';
8
- import { utf8Length } from './validation.ts';
7
+ } from './redactor.js';
8
+ import { utf8Length } from './validation.js';
9
9
 
10
10
  export interface ILogApi {
11
11
  appendLog(chunk: ILogChunk, signal?: AbortSignal): Promise<ILogAcknowledgement>;