Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export function createStartArgentRemoteSessionBuildFunction(
deviceRunSessionId,
toolsUrl: `http://127.0.0.1:${toolServerPort}`,
toolsAuthToken: toolServerToken,
logger,
});

const publicToolsUrl = await startNgrokTunnelAsync({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bunyan } from '@expo/logger';
import fetch from 'node-fetch';
import { Readable } from 'node:stream';

import { CustomBuildContext } from '../../../customBuildContext';
import { uploadDeviceRunSessionArtifactAsync } from '../deviceRunSessionArtifacts';
Expand All @@ -10,6 +11,12 @@ jest.mock('node-fetch');

const { Response } = jest.requireActual('node-fetch') as typeof import('node-fetch');

async function readStreamAsync(stream: NodeJS.ReadableStream): Promise<void> {
for await (const chunk of stream as Readable) {
void chunk;
}
}

function createLoggerMock(): bunyan {
return {
info: jest.fn(),
Expand All @@ -33,7 +40,6 @@ describe(listArgentArtifactsAsync, () => {
id: 'artifact-id',
filename: 'report.json',
mimeType: 'application/json',
size: 12,
isDirectory: false,
},
],
Expand All @@ -51,7 +57,6 @@ describe(listArgentArtifactsAsync, () => {
id: 'artifact-id',
filename: 'report.json',
mimeType: 'application/json',
size: 12,
isDirectory: false,
},
]);
Expand All @@ -69,22 +74,25 @@ describe(uploadArgentArtifactAsync, () => {

it('downloads an Argent artifact and uploads it as a device run session artifact', async () => {
const data = Buffer.from('artifact-data');
const reportedSize = 1024;
const ctx = {
logger: createLoggerMock(),
} as unknown as CustomBuildContext;
const logger = createLoggerMock();
const ctx = {} as unknown as CustomBuildContext;

jest.mocked(fetch).mockResolvedValueOnce(new Response(data));
jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
});

await uploadArgentArtifactAsync(ctx, {
deviceRunSessionId: 'drs-id',
toolsUrl: 'http://127.0.0.1:1234',
toolsAuthToken: 'tools-token',
logger,
artifact: {
id: 'artifact-id',
filename: 'report.json',
mimeType: 'application/json',
size: reportedSize,
},
});

Expand All @@ -96,7 +104,7 @@ describe(uploadArgentArtifactAsync, () => {
artifactId: 'artifact-id',
name: 'report.json (artifact-id)',
filename: 'report.json',
size: reportedSize,
size: data.length,
stream: expect.anything(),
});
});
Expand Down
58 changes: 39 additions & 19 deletions packages/build-tools/src/steps/utils/argentArtifacts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { SystemError } from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import { createReadStream, createWriteStream } from 'node:fs';
import { mkdtemp, rm, stat } from 'node:fs/promises';
import fetch from 'node-fetch';
import os from 'node:os';
import path from 'node:path';
import { pipeline } from 'node:stream/promises';
import { z } from 'zod';

import { CustomBuildContext } from '../../customBuildContext';
Expand All @@ -14,7 +20,6 @@ const ArgentArtifactSchema = z.object({
id: z.string(),
filename: z.string(),
mimeType: z.string(),
size: z.number(),
isDirectory: z.boolean().optional(),
});
const ArgentArtifactsListResponseSchema = z.object({
Expand All @@ -29,13 +34,14 @@ export async function pollArgentArtifactsForUploadAsync(
deviceRunSessionId,
toolsUrl,
toolsAuthToken,
logger,
}: {
deviceRunSessionId: string;
toolsUrl: string;
toolsAuthToken?: string;
logger: bunyan;
}
): Promise<never> {
const { logger } = ctx;
logger.info('Started polling Argent tool-server for artifacts.');
const seenArtifactIds = new Set<string>();
let listArtifactsErrorCount = 0;
Expand All @@ -54,6 +60,7 @@ export async function pollArgentArtifactsForUploadAsync(
toolsUrl,
toolsAuthToken,
artifact,
logger,
}).catch(err => {
const error = err instanceof Error ? err : new Error(String(err));
Sentry.capture('Could not upload Argent remote session artifact', error);
Expand Down Expand Up @@ -104,39 +111,52 @@ export async function uploadArgentArtifactAsync(
toolsUrl,
toolsAuthToken,
artifact,
logger,
}: {
deviceRunSessionId: string;
toolsUrl: string;
toolsAuthToken?: string;
artifact: ArgentArtifact;
logger: bunyan;
}
): Promise<void> {
const filename = artifact.isDirectory ? `${artifact.filename}.tar.gz` : artifact.filename;
ctx.logger.info(`Uploading artifact ${filename} (${formatBytes(artifact.size)}).`);
const stream = await createArgentArtifactDownloadStreamAsync({
artifact,
toolsUrl,
toolsAuthToken,
});
await uploadDeviceRunSessionArtifactAsync(ctx, {
deviceRunSessionId,
artifactId: artifact.id,
name: `${filename} (${artifact.id})`,
filename,
size: artifact.size,
stream,
});
logger.info(`Downloading artifact ${filename}.`);
const temporaryDirectory = await mkdtemp(path.join(os.tmpdir(), 'argent-artifact-'));
try {
const temporaryArtifactPath = path.join(temporaryDirectory, path.basename(filename));
await downloadArgentArtifactToFileAsync({
artifact,
toolsUrl,
toolsAuthToken,
destinationPath: temporaryArtifactPath,
});
const { size } = await stat(temporaryArtifactPath);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth adding a comment explaining why we do this even though there is non-null size field in Argent artifact Zod schema. Or removing it from the zod schema as it's unused.

logger.info(`Uploading artifact ${filename} (${formatBytes(size)}).`);
await uploadDeviceRunSessionArtifactAsync(ctx, {
deviceRunSessionId,
artifactId: artifact.id,
name: `${filename} (${artifact.id})`,
filename,
size,
stream: createReadStream(temporaryArtifactPath),
});
} finally {
await rm(temporaryDirectory, { recursive: true, force: true });
}
}

async function createArgentArtifactDownloadStreamAsync({
async function downloadArgentArtifactToFileAsync({
artifact,
toolsUrl,
toolsAuthToken,
destinationPath,
}: {
artifact: ArgentArtifact;
toolsUrl: string;
toolsAuthToken?: string;
}): Promise<NodeJS.ReadableStream> {
destinationPath: string;
}): Promise<void> {
const response = await fetch(new URL(`/artifacts/${artifact.id}`, toolsUrl).toString(), {
headers: toolsAuthToken ? { Authorization: `Bearer ${toolsAuthToken}` } : {},
});
Expand All @@ -150,5 +170,5 @@ async function createArgentArtifactDownloadStreamAsync({
`Argent artifact ${artifact.id} response did not include a readable body.`
);
}
return response.body;
await pipeline(response.body, createWriteStream(destinationPath));
}
Loading