Skip to content

Commit ddaca25

Browse files
committed
Allow for async injected values
1 parent 3bd7909 commit ddaca25

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

packages/core/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export type BundlerReport = {
7676
version: string;
7777
};
7878

79-
export type ToInjectItem = { type: 'file' | 'code'; value: string; fallback?: ToInjectItem };
79+
type InjectedValue = string | (() => Promise<string>);
80+
export type ToInjectItem = { type: 'file' | 'code'; value: InjectedValue; fallback?: ToInjectItem };
8081

8182
export type GetLogger = (name: string) => Logger;
8283
export type Logger = {

packages/plugins/injection/src/helpers.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ import { DISTANT_FILE_RX } from './constants';
1111

1212
const MAX_TIMEOUT_IN_MS = 5000;
1313

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+
1422
export const processDistantFile = async (
1523
item: ToInjectItem,
1624
timeout: number = MAX_TIMEOUT_IN_MS,
1725
): Promise<string> => {
1826
let timeoutId: ReturnType<typeof setTimeout> | undefined;
1927
return Promise.race([
20-
doRequest<string>({ url: item.value }).finally(() => {
28+
doRequest<string>({ url: await getInjectedValue(item) }).finally(() => {
2129
if (timeout) {
2230
clearTimeout(timeoutId);
2331
}
@@ -31,20 +39,24 @@ export const processDistantFile = async (
3139
};
3240

3341
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));
3543
return readFile(absolutePath, { encoding: 'utf-8' });
3644
};
3745

3846
export const processRawCode = async (item: ToInjectItem): Promise<string> => {
3947
// TODO: Confirm the code actually executes without errors.
48+
if (typeof item.value === 'function') {
49+
return item.value();
50+
}
4051
return item.value;
4152
};
4253

4354
export const processItem = async (item: ToInjectItem, log: Logger): Promise<string> => {
4455
let result: string;
56+
const value = await getInjectedValue(item);
4557
try {
4658
if (item.type === 'file') {
47-
if (item.value.match(DISTANT_FILE_RX)) {
59+
if (value.match(DISTANT_FILE_RX)) {
4860
result = await processDistantFile(item);
4961
} else {
5062
result = await processLocalFile(item);
@@ -55,7 +67,7 @@ export const processItem = async (item: ToInjectItem, log: Logger): Promise<stri
5567
throw new Error(`Invalid item type "${item.type}", only accepts "code" or "file".`);
5668
}
5769
} catch (error: any) {
58-
const itemId = `${item.type} - ${truncateString(item.value)}`;
70+
const itemId = `${item.type} - ${truncateString(value)}`;
5971
if (item.fallback) {
6072
// In case of any error, we'll fallback to next item in queue.
6173
log.warn(`Fallback for "${itemId}": ${error.toString()}`);

packages/tests/src/plugins/injection/helpers.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
processItem,
99
processLocalFile,
1010
processDistantFile,
11+
getInjectedValue,
1112
} from '@dd/internal-injection-plugin/helpers';
1213
import { mockLogger } from '@dd/tests/_jest/helpers/mocks';
1314
import { vol } from 'memfs';
@@ -39,13 +40,13 @@ const nonExistingDistantFile: ToInjectItem = {
3940
describe('Injection Plugin Helpers', () => {
4041
let nockScope: nock.Scope;
4142

42-
beforeEach(() => {
43+
beforeEach(async () => {
4344
nockScope = nock('https://example.com')
4445
.get('/distant-file.js')
4546
.reply(200, distantFileContent);
4647
// Emulate some fixtures.
4748
vol.fromJSON({
48-
[existingFile.value]: localFileContent,
49+
[await getInjectedValue(existingFile)]: localFileContent,
4950
});
5051
});
5152

0 commit comments

Comments
 (0)