@@ -11,13 +11,21 @@ import { DISTANT_FILE_RX } from './constants';
11
11
12
12
const MAX_TIMEOUT_IN_MS = 5000 ;
13
13
14
+ export const getInjectedValue = async ( item : ToInjectItem ) : Promise < string > => {
15
+ if ( typeof item . value === 'function' ) {
16
+ return item . value ( ) ;
17
+ }
18
+
19
+ return item . value ;
20
+ } ;
21
+
14
22
export const processDistantFile = async (
15
23
item : ToInjectItem ,
16
24
timeout : number = MAX_TIMEOUT_IN_MS ,
17
25
) : Promise < string > => {
18
26
let timeoutId : ReturnType < typeof setTimeout > | undefined ;
19
27
return Promise . race ( [
20
- doRequest < string > ( { url : item . value } ) . finally ( ( ) => {
28
+ doRequest < string > ( { url : await getInjectedValue ( item ) } ) . finally ( ( ) => {
21
29
if ( timeout ) {
22
30
clearTimeout ( timeoutId ) ;
23
31
}
@@ -31,20 +39,24 @@ export const processDistantFile = async (
31
39
} ;
32
40
33
41
export const processLocalFile = async ( item : ToInjectItem ) : Promise < string > => {
34
- const absolutePath = getAbsolutePath ( process . cwd ( ) , item . value ) ;
42
+ const absolutePath = getAbsolutePath ( process . cwd ( ) , await getInjectedValue ( item ) ) ;
35
43
return readFile ( absolutePath , { encoding : 'utf-8' } ) ;
36
44
} ;
37
45
38
46
export const processRawCode = async ( item : ToInjectItem ) : Promise < string > => {
39
47
// TODO: Confirm the code actually executes without errors.
48
+ if ( typeof item . value === 'function' ) {
49
+ return item . value ( ) ;
50
+ }
40
51
return item . value ;
41
52
} ;
42
53
43
54
export const processItem = async ( item : ToInjectItem , log : Logger ) : Promise < string > => {
44
55
let result : string ;
56
+ const value = await getInjectedValue ( item ) ;
45
57
try {
46
58
if ( item . type === 'file' ) {
47
- if ( item . value . match ( DISTANT_FILE_RX ) ) {
59
+ if ( value . match ( DISTANT_FILE_RX ) ) {
48
60
result = await processDistantFile ( item ) ;
49
61
} else {
50
62
result = await processLocalFile ( item ) ;
@@ -55,7 +67,7 @@ export const processItem = async (item: ToInjectItem, log: Logger): Promise<stri
55
67
throw new Error ( `Invalid item type "${ item . type } ", only accepts "code" or "file".` ) ;
56
68
}
57
69
} catch ( error : any ) {
58
- const itemId = `${ item . type } - ${ truncateString ( item . value ) } ` ;
70
+ const itemId = `${ item . type } - ${ truncateString ( value ) } ` ;
59
71
if ( item . fallback ) {
60
72
// In case of any error, we'll fallback to next item in queue.
61
73
log . warn ( `Fallback for "${ itemId } ": ${ error . toString ( ) } ` ) ;
0 commit comments