11import { Injectable , NotFoundException , Logger } from '@nestjs/common' ;
22import { S3Client , GetObjectCommand } from '@aws-sdk/client-s3' ;
3- import { Readable , PassThrough } from 'stream' ;
4- import archiver from 'archiver' ;
5- import { generateWindowsScript } from './scripts/windows' ;
6- import {
7- getPackageFilename ,
8- getReadmeContent ,
9- getScriptFilename ,
10- } from './scripts/common' ;
3+ import { Readable } from 'stream' ;
114
125@Injectable ( )
136export class DeviceAgentService {
@@ -73,56 +66,12 @@ export class DeviceAgentService {
7366 }
7467 }
7568
76- async downloadWindowsAgent (
77- organizationId : string ,
78- employeeId : string ,
79- ) : Promise < { stream : Readable ; filename : string ; contentType : string } > {
69+ async downloadWindowsAgent ( ) : Promise < { stream : Readable ; filename : string ; contentType : string } > {
8070 try {
81- this . logger . log (
82- `Creating Windows agent zip for org ${ organizationId } , employee ${ employeeId } ` ,
83- ) ;
84-
85- // Hardcoded device marker paths used by the setup scripts
86- const fleetDevicePathWindows = 'C:\\ProgramData\\CompAI\\Fleet' ;
87-
88- // Generate the Windows setup script
89- const script = generateWindowsScript ( {
90- orgId : organizationId ,
91- employeeId : employeeId ,
92- fleetDevicePath : fleetDevicePathWindows ,
93- } ) ;
94-
95- // Create a passthrough stream for the response
96- const passThrough = new PassThrough ( ) ;
97- const archive = archiver ( 'zip' , { zlib : { level : 9 } } ) ;
98-
99- // Pipe archive to passthrough
100- archive . pipe ( passThrough ) ;
101-
102- // Error handling for the archive
103- archive . on ( 'error' , ( err ) => {
104- this . logger . error ( 'Archive error:' , err ) ;
105- passThrough . destroy ( err ) ;
106- } ) ;
107-
108- archive . on ( 'warning' , ( warn ) => {
109- this . logger . warn ( 'Archive warning:' , warn ) ;
110- } ) ;
111-
112- // Add script file
113- const scriptFilename = getScriptFilename ( 'windows' ) ;
114- archive . append ( script , { name : scriptFilename , mode : 0o755 } ) ;
115-
116- // Add README
117- const readmeContent = getReadmeContent ( 'windows' ) ;
118- archive . append ( readmeContent , { name : 'README.txt' } ) ;
119-
120- // Get MSI package from S3 and stream it into the zip
121- const windowsPackageFilename = 'fleet-osquery.msi' ;
71+ const windowsPackageFilename = 'Comp AI Agent 1.0.0.exe' ;
12272 const packageKey = `windows/${ windowsPackageFilename } ` ;
123- const packageFilename = getPackageFilename ( 'windows' ) ;
12473
125- this . logger . log ( `Downloading Windows MSI from S3: ${ packageKey } ` ) ;
74+ this . logger . log ( `Downloading Windows agent from S3: ${ packageKey } ` ) ;
12675
12776 const getObjectCommand = new GetObjectCommand ( {
12877 Bucket : this . fleetBucketName ,
@@ -131,31 +80,27 @@ export class DeviceAgentService {
13180
13281 const s3Response = await this . s3Client . send ( getObjectCommand ) ;
13382
134- if ( s3Response . Body ) {
135- const s3Stream = s3Response . Body as Readable ;
136- s3Stream . on ( 'error' , ( err ) => {
137- this . logger . error ( 'S3 stream error:' , err ) ;
138- passThrough . destroy ( err ) ;
139- } ) ;
140- archive . append ( s3Stream , { name : packageFilename , store : true } ) ;
141- } else {
142- this . logger . warn (
143- 'Windows MSI file not found in S3, creating zip without MSI' ,
144- ) ;
83+ if ( ! s3Response . Body ) {
84+ throw new NotFoundException ( 'Windows agent executable file not found in S3' ) ;
14585 }
14686
147- // Finalize the archive
148- archive . finalize ( ) ;
87+ // Use S3 stream directly as Node.js Readable
88+ const s3Stream = s3Response . Body as Readable ;
14989
150- this . logger . log ( 'Successfully created Windows agent zip' ) ;
90+ this . logger . log (
91+ `Successfully retrieved Windows agent: ${ windowsPackageFilename } ` ,
92+ ) ;
15193
15294 return {
153- stream : passThrough ,
154- filename : `compai-device-agent-windows.zip` ,
155- contentType : 'application/zip ' ,
95+ stream : s3Stream ,
96+ filename : windowsPackageFilename ,
97+ contentType : 'application/octet-stream ' ,
15698 } ;
15799 } catch ( error ) {
158- this . logger . error ( 'Failed to create Windows agent zip:' , error ) ;
100+ if ( error instanceof NotFoundException ) {
101+ throw error ;
102+ }
103+ this . logger . error ( 'Failed to download Windows agent from S3:' , error ) ;
159104 throw error ;
160105 }
161106 }
0 commit comments