1- import { IExecDataProtectorDeserializer } from '@iexec/dataprotector-deserializer' ;
21import { promises as fs } from 'fs' ;
2+ import { IExecDataProtectorDeserializer } from '@iexec/dataprotector-deserializer' ;
33import { decryptContent , downloadEncryptedContent } from './decryptContent.js' ;
44import sendTelegram from './telegramService.js' ;
55import {
@@ -9,58 +9,57 @@ import {
99 validateWorkerEnv ,
1010} from './validation.js' ;
1111
12- async function writeTaskOutput ( path , message ) {
13- try {
14- await fs . writeFile ( path , message ) ;
15- console . log ( `File successfully written at path: ${ path } ` ) ;
16- } catch {
17- console . error ( 'Failed to write Task Output' ) ;
18- process . exit ( 1 ) ;
19- }
20- }
21-
2212async function processProtectedData ( {
2313 index,
2414 IEXEC_IN ,
2515 appDeveloperSecret,
2616 requesterSecret,
2717} ) {
2818 const datasetFilename =
29- index > 0 ? process . env [ `IEXEC_DATASET_${ index } _FILENAME` ] : null ;
30-
31- let protectedData ;
19+ index > 0
20+ ? process . env [ `IEXEC_DATASET_${ index } _FILENAME` ]
21+ : process . env . IEXEC_DATASET_FILENAME ;
22+ const result = { index, protectedData : datasetFilename } ;
3223 try {
33- const deserializerConfig = datasetFilename
34- ? { protectedDataPath : `${ IEXEC_IN } /${ datasetFilename } ` }
35- : { } ;
36-
37- const deserializer = new IExecDataProtectorDeserializer ( deserializerConfig ) ;
38- protectedData = {
39- chatId : await deserializer . getValue ( 'telegram_chatId' , 'string' ) ,
40- } ;
41- } catch ( e ) {
42- throw Error ( `Failed to parse ProtectedData ${ index } : ${ e . message } ` ) ;
43- }
24+ let protectedData ;
25+ try {
26+ const deserializerConfig = datasetFilename
27+ ? { protectedDataPath : `${ IEXEC_IN } /${ datasetFilename } ` }
28+ : { } ;
29+
30+ const deserializer = new IExecDataProtectorDeserializer (
31+ deserializerConfig
32+ ) ;
33+ protectedData = {
34+ chatId : await deserializer . getValue ( 'telegram_chatId' , 'string' ) ,
35+ } ;
36+ } catch ( e ) {
37+ throw Error ( `Failed to parse ProtectedData ${ index } : ${ e . message } ` ) ;
38+ }
4439
45- validateProtectedData ( protectedData ) ;
40+ validateProtectedData ( protectedData ) ;
4641
47- const encryptedTelegramContent = await downloadEncryptedContent (
48- requesterSecret . telegramContentMultiAddr
49- ) ;
50-
51- const telegramContent = decryptContent (
52- encryptedTelegramContent ,
53- requesterSecret . telegramContentEncryptionKey
54- ) ;
42+ const encryptedTelegramContent = await downloadEncryptedContent (
43+ requesterSecret . telegramContentMultiAddr
44+ ) ;
5545
56- const response = await sendTelegram ( {
57- chatId : protectedData . chatId ,
58- message : telegramContent ,
59- botToken : appDeveloperSecret . TELEGRAM_BOT_TOKEN ,
60- senderName : requesterSecret . senderName ,
61- } ) ;
46+ const telegramContent = decryptContent (
47+ encryptedTelegramContent ,
48+ requesterSecret . telegramContentEncryptionKey
49+ ) ;
6250
63- return { index, response } ;
51+ await sendTelegram ( {
52+ chatId : protectedData . chatId ,
53+ message : telegramContent ,
54+ botToken : appDeveloperSecret . TELEGRAM_BOT_TOKEN ,
55+ senderName : requesterSecret . senderName ,
56+ } ) ;
57+ result . success = true ;
58+ } catch ( e ) {
59+ result . success = false ;
60+ result . error = e . message ;
61+ }
62+ return result ;
6463}
6564
6665async function start ( ) {
@@ -74,95 +73,71 @@ async function start() {
7473
7574 const workerEnv = validateWorkerEnv ( { IEXEC_OUT } ) ;
7675
77- let appDeveloperSecret ;
76+ let result ; // { success: boolean, error?: string , protectedData?: string, results?: { index: number, protectedData: string, success: boolean, error?: string } [] }
7877 try {
79- appDeveloperSecret = JSON . parse ( IEXEC_APP_DEVELOPER_SECRET ) ;
80- } catch {
81- throw Error ( 'Failed to parse the developer secret' ) ;
82- }
83- appDeveloperSecret = validateAppSecret ( appDeveloperSecret ) ;
84-
85- let requesterSecret ;
86- try {
87- requesterSecret = IEXEC_REQUESTER_SECRET_1
88- ? JSON . parse ( IEXEC_REQUESTER_SECRET_1 )
89- : { } ;
90- } catch {
91- throw Error ( 'Failed to parse requester secret' ) ;
92- }
93- requesterSecret = validateRequesterSecret ( requesterSecret ) ;
78+ let appDeveloperSecret ;
79+ try {
80+ appDeveloperSecret = JSON . parse ( IEXEC_APP_DEVELOPER_SECRET ) ;
81+ } catch {
82+ throw Error ( 'Failed to parse the developer secret' ) ;
83+ }
84+ appDeveloperSecret = validateAppSecret ( appDeveloperSecret ) ;
9485
95- const bulkSize = parseInt ( IEXEC_BULK_SLICE_SIZE , 10 ) || 0 ;
86+ let requesterSecret ;
87+ try {
88+ requesterSecret = JSON . parse ( IEXEC_REQUESTER_SECRET_1 ) ;
89+ } catch {
90+ throw Error ( 'Failed to parse requester secret' ) ;
91+ }
9692
97- // Process multiple protected data
98- if ( bulkSize > 0 ) {
99- const promises = [ ] ;
100- for ( let index = 1 ; index <= bulkSize ; index += 1 ) {
101- const promise = processProtectedData ( {
102- index,
93+ requesterSecret = validateRequesterSecret ( requesterSecret ) ;
94+
95+ const bulkSize = parseInt ( IEXEC_BULK_SLICE_SIZE , 10 ) || 0 ;
96+
97+ // Process multiple protected data
98+ if ( bulkSize > 0 ) {
99+ const processPromises = new Array ( bulkSize ) . fill ( null ) . map ( ( _ , index ) =>
100+ processProtectedData ( {
101+ index : index + 1 ,
102+ IEXEC_IN ,
103+ appDeveloperSecret,
104+ requesterSecret,
105+ } )
106+ ) ;
107+ const results = await Promise . all ( processPromises ) ;
108+ const successCount = results . filter ( ( r ) => r . success === true ) . length ;
109+ const errorCount = results . filter ( ( r ) => r . success !== true ) . length ;
110+ result = {
111+ success : errorCount === 0 ,
112+ error : errorCount > 0 ? 'Partial failure' : undefined ,
113+ totalCount : results . length ,
114+ successCount,
115+ errorCount,
116+ results : results . map ( ( r ) => ( {
117+ index : r . index ,
118+ protectedData : r . protectedData ,
119+ success : r . success ,
120+ error : r . error ,
121+ } ) ) ,
122+ } ;
123+ } else {
124+ const { protectedData, success, error } = await processProtectedData ( {
125+ index : 0 ,
103126 IEXEC_IN ,
104127 appDeveloperSecret,
105128 requesterSecret,
106- } ) . catch ( ( error ) => {
107- const datasetFilename = process . env [ `IEXEC_DATASET_${ index } _FILENAME` ] ;
108- return {
109- index,
110- resultFileName : datasetFilename
111- ? `${ datasetFilename } .txt`
112- : `dataset-${ index } .txt` ,
113- response : {
114- status : 500 ,
115- message : `Failed to process protected-data ${ index } . Cause: ${ error . message } ` ,
116- } ,
117- } ;
118129 } ) ;
119-
120- promises . push ( promise ) ;
130+ result = { protectedData, success, error } ;
121131 }
122-
123- const results = await Promise . all ( promises ) ;
124-
125- // Write result.json for bulk processing
126- const successCount = results . filter (
127- ( r ) => r . response . status === 200
128- ) . length ;
129- const errorCount = results . filter ( ( r ) => r . response . status !== 200 ) . length ;
130-
131- const bulkResult = {
132- message : `Bulk processing completed: ${ successCount } successful, ${ errorCount } failed` ,
133- 'total-count' : results . length ,
134- 'success-count' : successCount ,
135- 'error-count' : errorCount ,
136- results : results . map ( ( r ) => ( {
137- index : r . index ,
138- 'protected-data' :
139- process . env [ `IEXEC_DATASET_${ r . index } _FILENAME` ] ||
140- `dataset-${ r . index } ` ,
141- response : r . response ,
142- } ) ) ,
143- } ;
144-
145- await writeTaskOutput (
146- `${ workerEnv . IEXEC_OUT } /result.json` ,
147- JSON . stringify ( bulkResult , null , 2 )
148- ) ;
149- } else {
150- // Process single protected data
151- const result = await processProtectedData ( {
152- index : 0 ,
153- IEXEC_IN ,
154- appDeveloperSecret,
155- requesterSecret,
156- } ) ;
157-
158- await writeTaskOutput (
159- `${ workerEnv . IEXEC_OUT } /result.json` ,
160- JSON . stringify ( result . response , null , 2 )
161- ) ;
132+ } catch ( e ) {
133+ result = { success : false , error : e . message } ;
162134 }
163135
164- // Generate computed.json - same format for both single and bulk
165- await writeTaskOutput (
136+ await fs . writeFile (
137+ `${ workerEnv . IEXEC_OUT } /result.json` ,
138+ JSON . stringify ( result , null , 2 )
139+ ) ;
140+ await fs . writeFile (
166141 `${ workerEnv . IEXEC_OUT } /computed.json` ,
167142 JSON . stringify (
168143 {
0 commit comments