Skip to content

Commit c22d85b

Browse files
authored
download asset corrupted app cache scenario
1 parent 441338b commit c22d85b

File tree

7 files changed

+34
-65
lines changed

7 files changed

+34
-65
lines changed

bootstrap/scripts/verify-dlls.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ async function checkManifests(pattern) {
6767

6868
await cleanupManifest("bootstrap-prod-manifest.json");
6969

70-
await checkManifests("bootstrap*-manifest.json");
70+
await checkManifests("bootstrap*-manifest.json");

jest/setupTests.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import matchers from "@testing-library/jest-dom/matchers";
2+
import "whatwg-fetch";
23

34
global.IS_REACT_ACT_ENVIRONMENT = true;
45

@@ -21,19 +22,4 @@ jest.mock("@linaria/core", () => ({
2122
css: jest.fn(() => ""),
2223
}));
2324

24-
if (!globalThis.fetch) {
25-
globalThis.fetch = jest.fn();
26-
globalThis.Request = jest.fn();
27-
globalThis.Response = jest.fn();
28-
}
29-
30-
if (!globalThis.AbortController) {
31-
globalThis.AbortController = jest.fn();
32-
}
33-
34-
if (!globalThis.TextEncoder || !globalThis.TextDecoder) {
35-
globalThis.TextEncoder = jest.fn();
36-
globalThis.TextDecoder = jest.fn();
37-
}
38-
3925
globalThis.DEFAULT_LOCALE = "en-US";

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lastui/rocker",
3-
"version": "0.17.6",
3+
"version": "0.18.0",
44
"license": "Apache-2.0",
55
"author": "[email protected]",
66
"homepage": "https://github.com/lastui/rocker#readme",
@@ -134,6 +134,7 @@
134134
"weak-napi": "2.0.2",
135135
"webpack": "5.89.0",
136136
"webpack-dev-server": "4.15.1",
137+
"whatwg-fetch": "3.6.19",
137138
"yargs": "17.7.2"
138139
},
139140
"engines": {

platform/scripts/verify-dlls.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ async function checkManifests(pattern) {
6767

6868
await cleanupManifest("platform-prod-manifest.json");
6969

70-
await checkManifests("platform*-manifest.json");
70+
await checkManifests("platform*-manifest.json");

platform/src/kernel/registry/__tests__/assets.test.js

+12-42
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ describe("assets registry", () => {
2323

2424
describe("downloadAsset", () => {
2525
it("happy path", async () => {
26-
global.fetch.mockImplementationOnce(async () => ({
27-
ok: true,
28-
status: 200,
29-
text: async () => "data",
30-
headers: {
31-
get() {},
32-
},
33-
}));
26+
global.fetch.mockImplementationOnce(async () => new Response("data", { status: 200, statusText: "OK" }));
3427

3528
const result = await downloadAsset("/path/data.json");
3629

@@ -49,10 +42,7 @@ describe("assets registry", () => {
4942
});
5043

5144
it("error path", async () => {
52-
global.fetch.mockImplementationOnce(async () => ({
53-
ok: false,
54-
status: 404,
55-
}));
45+
global.fetch.mockImplementationOnce(async () => new Response(null, { status: 404, statusText: "Not Found" }));
5646

5747
await expect(downloadAsset("/path/data.json")).rejects.toThrow("404");
5848
});
@@ -118,16 +108,10 @@ describe("assets registry", () => {
118108
});
119109

120110
it("compiles program", async () => {
121-
global.fetch.mockImplementationOnce(async () => ({
122-
ok: true,
123-
status: 200,
124-
text: async () => `!function(){
125-
window.__SANDBOX_SCOPE__.component = () => 'main'
126-
}();`,
127-
headers: {
128-
get() {},
129-
},
130-
}));
111+
global.fetch.mockImplementationOnce(
112+
async () =>
113+
new Response(`!function(){ window.__SANDBOX_SCOPE__.component = () => 'main' }();`, { status: 200, statusText: "OK" }),
114+
);
131115

132116
const result = await downloadProgram("my-feature", {
133117
url: "/service/program.js",
@@ -141,16 +125,9 @@ describe("assets registry", () => {
141125
spy.mockImplementation(() => {});
142126
spy.mockClear();
143127

144-
global.fetch.mockImplementationOnce(async () => ({
145-
ok: true,
146-
status: 200,
147-
text: async () => `
148-
window.__SANDBOX_SCOPE__.component = () => 'main';
149-
`,
150-
headers: {
151-
get() {},
152-
},
153-
}));
128+
global.fetch.mockImplementationOnce(
129+
async () => new Response(`window.__SANDBOX_SCOPE__.component = () => 'main';`, { status: 200, statusText: "OK" }),
130+
);
154131

155132
const result = await downloadProgram("my-feature", {
156133
url: "/service/program.js",
@@ -166,16 +143,9 @@ describe("assets registry", () => {
166143
spy.mockImplementation(() => {});
167144
spy.mockClear();
168145

169-
global.fetch.mockImplementationOnce(async () => ({
170-
ok: true,
171-
status: 200,
172-
text: async () => `!function(){
173-
throw new Error('ouch');
174-
}();`,
175-
headers: {
176-
get() {},
177-
},
178-
}));
146+
global.fetch.mockImplementationOnce(
147+
async () => new Response(`!function(){ throw new Error('ouch'); }();`, { status: 200, statusText: "OK" }),
148+
);
179149

180150
const result = await downloadProgram("my-feature", {
181151
url: "/service/program.js",

platform/src/kernel/registry/assets.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,13 @@ function downloadAsset(resource, parentController) {
127127
/* istanbul ignore next */
128128
if (response.status === 304) {
129129
if (currentEtag) {
130-
const assetEntry = await resources.match(`${resource}_${currentEtag}`);
131-
if (assetEntry) {
132-
return assetEntry.clone();
130+
try {
131+
const assetEntry = await resources.match(`${resource}_${currentEtag}`);
132+
if (assetEntry) {
133+
return assetEntry.clone();
134+
}
135+
} catch (error) {
136+
/* silence the cache error */
133137
}
134138
resources.delete(`${resource}_${currentEtag}`);
135139
}
@@ -150,12 +154,14 @@ function downloadAsset(resource, parentController) {
150154
resources.delete(`${resource}_${currentEtag}`);
151155
}
152156
const latestEtag = response.headers.get("Etag");
157+
const blob = await response.blob();
158+
const cleaned = new Response(blob, { status: 200, statusText: "OK" });
153159
/* istanbul ignore next */
154160
if (latestEtag) {
155-
resources.put(`${resource}_${latestEtag}`, response.clone());
161+
resources.put(`${resource}_${latestEtag}`, cleaned.clone());
156162
etags.put(resource, new Response(latestEtag, { status: 200, statusText: "OK" }));
157163
}
158-
return response;
164+
return cleaned;
159165
}
160166

161167
const request = Promise.race([aborter, fetcher()]).catch((error) => {

0 commit comments

Comments
 (0)