Skip to content

Commit

Permalink
Improvements in CDN data fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuri committed Jul 11, 2023
1 parent 0f870b9 commit 8fccf34
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
14 changes: 10 additions & 4 deletions src/ppom-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Object.defineProperty(globalThis, 'performance', {
value: () => undefined,
});

(AbortSignal.prototype as any).timeout = () => undefined;

// eslint-disable-next-line jsdoc/require-jsdoc
async function flushPromises() {
// Wait for promises running in the non-async timer callback to complete.
Expand Down Expand Up @@ -301,7 +303,9 @@ describe('PPOMController', () => {

await expect(async () => {
await ppomController.updatePPOM({ updateForAllChains: false });
}).rejects.toThrow('Failed to fetch version info');
}).rejects.toThrow(
'Failed to fetch file with url: https://ppom_cdn_base_url/ppom_version.json',
);
});

it('should throw error if fetch for blob return 500', async () => {
Expand All @@ -314,7 +318,7 @@ describe('PPOMController', () => {
await expect(async () => {
await ppomController.updatePPOM({ updateForAllChains: false });
}).rejects.toThrow(
'Failed to fetch file with url https://ppom_cdn_base_url/blob',
'Failed to fetch file with url: https://ppom_cdn_base_url/blob',
);
});

Expand Down Expand Up @@ -365,7 +369,9 @@ describe('PPOMController', () => {

await expect(async () => {
await ppomController.updatePPOM();
}).rejects.toThrow('Failed to fetch version info');
}).rejects.toThrow(
'Failed to fetch file with url: https://ppom_cdn_base_url/ppom_version.json',
);
});

it('should not throw error if fetch for blob return 500', async () => {
Expand All @@ -379,7 +385,7 @@ describe('PPOMController', () => {
await ppomController.updatePPOM();
jest.runOnlyPendingTimers();
}).not.toThrow(
'Failed to fetch file with url https://ppom_cdn_base_url/blob',
'Failed to fetch file with url: https://ppom_cdn_base_url/blob',
);
await flushPromises();
});
Expand Down
66 changes: 37 additions & 29 deletions src/ppom-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const stateMetaData = {
};

const PPOM_VERSION_FILE_NAME = 'ppom_version.json';
const URL_PREFIX = 'https://';
const controllerName = 'PPOMController';

export type UsePPOM = {
Expand Down Expand Up @@ -409,7 +410,7 @@ export class PPOMController extends BaseControllerV2<
*/
async #updateVersionInfo() {
const versionInfo = await this.#fetchVersionInfo(
`${this.#cdnBaseUrl}/${PPOM_VERSION_FILE_NAME}`,
`${URL_PREFIX}${this.#cdnBaseUrl}/${PPOM_VERSION_FILE_NAME}`,
);
if (versionInfo) {
this.update((draftState) => {
Expand Down Expand Up @@ -448,7 +449,9 @@ export class PPOMController extends BaseControllerV2<
if (this.#checkFilePresentInStorage(storageMetadata, fileVersionInfo)) {
return;
}
const fileUrl = `${this.#cdnBaseUrl}/${fileVersionInfo.filePath}`;
const fileUrl = `${URL_PREFIX}${this.#cdnBaseUrl}/${
fileVersionInfo.filePath
}`;
const fileData = await this.#fetchBlob(fileUrl);

await this.#storage.writeFile({
Expand Down Expand Up @@ -626,44 +629,49 @@ export class PPOMController extends BaseControllerV2<
}

/*
* Fetch the version info from the PPOM cdn.
* fetchFile - Generic method to fetch file from CDN.
*/
async #fetchVersionInfo(
async #fetchFile(
url: string,
): Promise<PPOMVersionResponse | undefined> {
options: Record<string, unknown> = {},
): Promise<any> {
const response = await safelyExecute(
async () => fetch(url, { cache: 'no-cache' }),
async () =>
fetch(url, {
cache: 'no-cache',
redirect: 'error',
signal: (AbortSignal as any).timeout(10000),
...options,
}),
true,
);
switch (response?.status) {
case 200: {
return response.json();
}

default: {
throw new Error(`Failed to fetch version info url: ${url}`);
}
if (response?.status !== 200) {
throw new Error(`Failed to fetch file with url: ${url}`);
}
return response;
}

/*
* Fetch the blob from the PPOM cdn.
* Fetch the version info from the PPOM cdn.
*/
async #fetchBlob(fileUrl: string): Promise<ArrayBuffer> {
const response = await safelyExecute(
async () => fetch(fileUrl, { cache: 'no-cache' }),
true,
);

switch (response?.status) {
case 200: {
return await response.arrayBuffer();
}
async #fetchVersionInfo(
url: string,
): Promise<PPOMVersionResponse | undefined> {
const response = await this.#fetchFile(url, {
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Content-Type': 'application/json',
},
});
return response.json();
}

default: {
throw new Error(`Failed to fetch file with url ${fileUrl}`);
}
}
/*
* Fetch the blob from the PPOM cdn.
*/
async #fetchBlob(url: string): Promise<ArrayBuffer> {
const response = await this.#fetchFile(url);
return await response.arrayBuffer();
}

/*
Expand Down
2 changes: 1 addition & 1 deletion test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const buildPPOMController = (args?: any) => {
});
},
},
cdnBaseUrl: 'https://ppom_cdn_base_url',
cdnBaseUrl: 'ppom_cdn_base_url',
...args,
});
return ppomController;
Expand Down

0 comments on commit 8fccf34

Please sign in to comment.