Repository navigation
Expand file tree
/
Copy pathartifact-download.ts
More file actions
193 lines (187 loc) · 7.21 KB
/
Copy pathartifact-download.ts
File metadata and controls
193 lines (187 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import fs from 'node:fs';
// Type-only: a runtime `node:http` import would load it eagerly for every CLI command. The actual
// HTTP stack loads through `loadNodeHttpRequester` only when a remote daemon returns an artifact.
import type http from 'node:http';
import path from 'node:path';
import { pipeline } from 'node:stream/promises';
import { AppError } from '@agent-device/kernel/errors';
import { loadNodeHttpRequester } from '@agent-device/host-kit/transport';
import type { DaemonRequestMeta } from '@agent-device/kernel/contracts';
import {
buildDaemonHttpAuthHeaders,
buildDaemonHttpLeaseHeaders,
buildDaemonHttpTenantHeaders,
} from '@agent-device/contracts/daemon-http';
const REMOTE_ARTIFACT_DOWNLOAD_TIMEOUT_MS = 90_000;
export type RemoteArtifactDownload = {
artifactUrl: URL;
token: string;
artifactId: string;
destinationPath: string;
requestScope: DaemonRequestMeta | undefined;
timeoutMs?: number;
/** Whether `destinationPath` is a caller-owned root rather than one file to create. */
isDirectory?: boolean;
};
type DownloadDestination = {
prepare(): Promise<void>;
cleanupOnError(): Promise<void>;
materialize(res: http.IncomingMessage, signal: AbortSignal): Promise<string>;
};
function buildDownloadDestination(params: RemoteArtifactDownload): DownloadDestination {
const { destinationPath } = params;
if (params.isDirectory) {
return {
prepare: async () => {
await fs.promises.mkdir(destinationPath, { recursive: true });
},
// The root belongs to the caller and may contain earlier runs. Directory materialization
// owns and cleans its hidden staging directory, so the root itself is never removed.
cleanupOnError: async () => {},
materialize: (res, signal) => materializeDirectoryArtifact(res, destinationPath, signal),
};
}
return {
prepare: async () => {
await fs.promises.mkdir(path.dirname(destinationPath), { recursive: true });
},
cleanupOnError: () => fs.promises.rm(destinationPath, { force: true }),
materialize: async (res, signal) => {
await pipeline(res, fs.createWriteStream(destinationPath), { signal });
return destinationPath;
},
};
}
/** Downloads an artifact and returns the exact file or directory published on the client. */
export async function downloadRemoteArtifactFromUrl(
params: RemoteArtifactDownload,
): Promise<string> {
const transport = await loadNodeHttpRequester(params.artifactUrl.protocol);
const destination = buildDownloadDestination(params);
await destination.prepare();
return await new Promise<string>((resolve, reject) => {
let settled = false;
let materializationStarted = false;
let timeoutError: AppError | undefined;
const operation = new AbortController();
const timeoutMs = params.timeoutMs ?? REMOTE_ARTIFACT_DOWNLOAD_TIMEOUT_MS;
const settle = (result?: string, error?: Error) => {
if (settled) return;
settled = true;
clearTimeout(timeoutHandle);
if (error) {
void destination.cleanupOnError().finally(() => reject(error));
return;
}
resolve(result ?? params.destinationPath);
};
const request = transport.request(
{
protocol: params.artifactUrl.protocol,
host: params.artifactUrl.hostname,
port: params.artifactUrl.port,
method: 'GET',
path: params.artifactUrl.pathname + params.artifactUrl.search,
headers: {
...buildDaemonHttpAuthHeaders(params.token),
...buildDaemonHttpTenantHeaders(params.requestScope?.tenantId),
...buildDaemonHttpLeaseHeaders(params.requestScope?.leaseId),
},
},
(res) => {
if ((res.statusCode ?? 500) >= 400) {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
settle(
undefined,
new AppError('COMMAND_FAILED', 'Failed to download remote artifact', {
artifactId: params.artifactId,
statusCode: res.statusCode,
requestId: params.requestScope?.requestId,
body,
}),
);
});
return;
}
materializationStarted = true;
void destination.materialize(res, operation.signal).then(
(materializedPath) => settle(materializedPath),
(error: unknown) =>
settle(
undefined,
timeoutError ?? (error instanceof Error ? error : new Error(String(error))),
),
);
},
);
const timeoutHandle = setTimeout(() => {
timeoutError = new AppError('COMMAND_FAILED', 'Remote artifact download timed out', {
artifactId: params.artifactId,
requestId: params.requestScope?.requestId,
timeoutMs,
});
operation.abort(timeoutError);
request.destroy(timeoutError);
// The abort-aware materializer cleans its staging area. Do not reject before it finishes,
// or extraction could keep writing after the caller observes the timeout.
if (!materializationStarted) settle(undefined, timeoutError);
}, timeoutMs);
request.on('error', (error) => {
if (materializationStarted) return;
if (error instanceof AppError) {
settle(undefined, timeoutError ?? error);
return;
}
settle(
undefined,
new AppError(
'COMMAND_FAILED',
'Failed to download remote artifact',
{
artifactId: params.artifactId,
requestId: params.requestScope?.requestId,
timeoutMs,
},
error instanceof Error ? error : undefined,
),
);
});
request.end();
});
}
/** Safely extracts one directory and atomically publishes it under the caller-owned root. */
async function materializeDirectoryArtifact(
res: http.IncomingMessage,
destinationRoot: string,
signal: AbortSignal,
): Promise<string> {
// Staging beside the final entry guarantees one filesystem, so there is no EXDEV copy fallback
// that could expose a partially copied suite.
const tempDir = await fs.promises.mkdtemp(path.join(destinationRoot, '.agent-device-download-'));
const archivePath = path.join(tempDir, 'artifact.tar.gz');
const stagingRoot = path.join(tempDir, 'extracted');
try {
await pipeline(res, fs.createWriteStream(archivePath), { signal });
const { extractArchiveSafely } = await import('@agent-device/host-kit/archive');
await extractArchiveSafely({ archivePath, outputRoot: stagingRoot, type: 'tgz', signal });
signal.throwIfAborted();
const [entryName, ...extraEntries] = await fs.promises.readdir(stagingRoot);
if (!entryName || extraEntries.length > 0) {
throw new AppError(
'COMMAND_FAILED',
`Downloaded directory artifact has an unexpected shape: expected exactly one top-level entry, found ${extraEntries.length + (entryName ? 1 : 0)}`,
);
}
signal.throwIfAborted();
const materializedPath = path.join(destinationRoot, entryName);
await fs.promises.rename(path.join(stagingRoot, entryName), materializedPath);
return materializedPath;
} finally {
await fs.promises.rm(tempDir, { recursive: true, force: true });
}
}