@hot-updater/react-native 1.0.0-rc.27 → 1.0.0-rc.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/lib/commonjs/fetchJSON.js +12 -26
  2. package/lib/commonjs/fetchJSON.js.map +1 -1
  3. package/lib/commonjs/fetchUpdateResponse.js +54 -0
  4. package/lib/commonjs/fetchUpdateResponse.js.map +1 -0
  5. package/lib/commonjs/httpClient.js +9 -3
  6. package/lib/commonjs/httpClient.js.map +1 -1
  7. package/lib/commonjs/index.js +6 -2
  8. package/lib/commonjs/index.js.map +1 -1
  9. package/lib/commonjs/releaseCatalogCache.js +17 -27
  10. package/lib/commonjs/releaseCatalogCache.js.map +1 -1
  11. package/lib/commonjs/sdkVersion.js +1 -1
  12. package/lib/module/clientPlugin.js.map +1 -1
  13. package/lib/module/fetchJSON.js +12 -26
  14. package/lib/module/fetchJSON.js.map +1 -1
  15. package/lib/module/fetchUpdateResponse.js +49 -0
  16. package/lib/module/fetchUpdateResponse.js.map +1 -0
  17. package/lib/module/httpClient.js +9 -3
  18. package/lib/module/httpClient.js.map +1 -1
  19. package/lib/module/index.js +6 -2
  20. package/lib/module/index.js.map +1 -1
  21. package/lib/module/releaseCatalogCache.js +17 -27
  22. package/lib/module/releaseCatalogCache.js.map +1 -1
  23. package/lib/module/sdkVersion.js +1 -1
  24. package/lib/typescript/commonjs/clientPlugin.d.ts +1 -1
  25. package/lib/typescript/commonjs/clientPlugin.d.ts.map +1 -1
  26. package/lib/typescript/commonjs/fetchJSON.d.ts +2 -5
  27. package/lib/typescript/commonjs/fetchJSON.d.ts.map +1 -1
  28. package/lib/typescript/commonjs/fetchUpdateResponse.d.ts +14 -0
  29. package/lib/typescript/commonjs/fetchUpdateResponse.d.ts.map +1 -0
  30. package/lib/typescript/commonjs/httpClient.d.ts +2 -2
  31. package/lib/typescript/commonjs/httpClient.d.ts.map +1 -1
  32. package/lib/typescript/commonjs/index.d.ts +1 -1
  33. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  34. package/lib/typescript/commonjs/releaseCatalogCache.d.ts +2 -0
  35. package/lib/typescript/commonjs/releaseCatalogCache.d.ts.map +1 -1
  36. package/lib/typescript/commonjs/sdkVersion.d.ts +1 -1
  37. package/lib/typescript/module/clientPlugin.d.ts +1 -1
  38. package/lib/typescript/module/clientPlugin.d.ts.map +1 -1
  39. package/lib/typescript/module/fetchJSON.d.ts +2 -5
  40. package/lib/typescript/module/fetchJSON.d.ts.map +1 -1
  41. package/lib/typescript/module/fetchUpdateResponse.d.ts +14 -0
  42. package/lib/typescript/module/fetchUpdateResponse.d.ts.map +1 -0
  43. package/lib/typescript/module/httpClient.d.ts +2 -2
  44. package/lib/typescript/module/httpClient.d.ts.map +1 -1
  45. package/lib/typescript/module/index.d.ts +1 -1
  46. package/lib/typescript/module/index.d.ts.map +1 -1
  47. package/lib/typescript/module/releaseCatalogCache.d.ts +2 -0
  48. package/lib/typescript/module/releaseCatalogCache.d.ts.map +1 -1
  49. package/lib/typescript/module/sdkVersion.d.ts +1 -1
  50. package/package.json +3 -3
  51. package/src/clientPlugin.ts +1 -0
  52. package/src/fetchJSON.ts +11 -31
  53. package/src/fetchUpdateResponse.ts +54 -0
  54. package/src/httpClient.ts +10 -2
  55. package/src/index.ts +7 -2
  56. package/src/releaseCatalogCache.ts +23 -38
  57. package/src/sdkVersion.ts +1 -1
package/src/index.ts CHANGED
@@ -57,6 +57,7 @@ export {
57
57
  type UpdateError,
58
58
  type UpdateErrorReason,
59
59
  type UpdateErrorStage,
60
+ type UpdateHttpResponse,
60
61
  type UpdateStrategy,
61
62
  } from "./clientPlugin";
62
63
  // The built-in Insights client plugin, for HotUpdater.init({ plugins }).
@@ -181,7 +182,9 @@ function createHotUpdaterClient() {
181
182
  const { baseURL, plugins: _plugins, ...rest } = autoOptions;
182
183
  return {
183
184
  ...rest,
184
- client: createHttpClient(baseURL),
185
+ client: createHttpClient(baseURL, (response) => {
186
+ emitAfterAppReady("onHttpResponse", () => response);
187
+ }),
185
188
  };
186
189
  }
187
190
 
@@ -200,7 +203,9 @@ function createHotUpdaterClient() {
200
203
  const { baseURL, plugins: _plugins, ...baseURLRest } = rest;
201
204
  return {
202
205
  ...baseURLRest,
203
- client: createHttpClient(baseURL),
206
+ client: createHttpClient(baseURL, (response) => {
207
+ emitAfterAppReady("onHttpResponse", () => response);
208
+ }),
204
209
  };
205
210
  }
206
211
 
@@ -6,6 +6,7 @@ import {
6
6
  type ReleaseCatalog,
7
7
  } from "@hot-updater/protocol";
8
8
 
9
+ import { fetchUpdateResponse, type UpdateRequest } from "./fetchUpdateResponse";
9
10
  import { InvalidUpdateResponseError, UpdateHttpError } from "./updateError";
10
11
 
11
12
  const CACHE_FORMAT_VERSION = "1";
@@ -50,6 +51,7 @@ type FetchReleaseCatalogInput = {
50
51
  readonly requestHeaders?: Record<string, string>;
51
52
  readonly requestTimeout?: number;
52
53
  readonly url: string;
54
+ readonly onResponse?: UpdateRequest["onResponse"];
53
55
  };
54
56
 
55
57
  const isValidETag = (value: string | null): value is string =>
@@ -113,36 +115,24 @@ export const createReleaseCatalogCachePartition = (
113
115
  version: 2,
114
116
  });
115
117
 
116
- const fetchCatalogResponse = async (
118
+ const isEmptyCatalogResponse = (response: Response) =>
119
+ response.status === 404 &&
120
+ response.headers.get("x-hot-updater-catalog")?.trim().toLowerCase() ===
121
+ "none";
122
+
123
+ const fetchCatalogResponse = (
117
124
  input: FetchReleaseCatalogInput,
118
125
  etag?: string,
119
- ): Promise<Response> => {
120
- const controller = new AbortController();
121
- const timeoutId = setTimeout(
122
- () => controller.abort(),
123
- input.requestTimeout ?? 5000,
124
- );
125
-
126
- try {
127
- const headers = new Headers(input.requestHeaders);
128
- headers.set("Accept", "application/json");
129
- if (etag === undefined) {
130
- headers.delete("If-None-Match");
131
- } else {
132
- headers.set("If-None-Match", etag);
133
- }
134
- return await fetch(input.url, {
135
- headers,
136
- signal: controller.signal,
137
- });
138
- } catch (error: unknown) {
139
- if (error instanceof Error && error.name === "AbortError") {
140
- throw new Error("Request timed out");
141
- }
142
- throw error;
143
- } finally {
144
- clearTimeout(timeoutId);
145
- }
126
+ ) => {
127
+ const headers = new Headers(input.requestHeaders);
128
+ headers.set("Accept", "application/json");
129
+ if (etag === undefined) headers.delete("If-None-Match");
130
+ else headers.set("If-None-Match", etag);
131
+ return fetchUpdateResponse({
132
+ ...input,
133
+ resource: "catalog",
134
+ requestHeaders: Object.fromEntries(headers.entries()),
135
+ });
146
136
  };
147
137
 
148
138
  const readValidatedCache = async (
@@ -160,25 +150,20 @@ const readValidatedCache = async (
160
150
  };
161
151
 
162
152
  const consumeSuccessfulResponse = async (
163
- response: Response,
153
+ { response, body }: Awaited<ReturnType<typeof fetchCatalogResponse>>,
164
154
  input: FetchReleaseCatalogInput,
165
155
  partition: string,
166
156
  ): Promise<ReleaseCatalog | null> => {
167
157
  // The server marks the 404 of a scope that has no catalog yet: no update,
168
158
  // not a failure. Any other 404 is a wrong baseURL or route.
169
- if (
170
- response.status === 404 &&
171
- response.headers.get("x-hot-updater-catalog")?.trim().toLowerCase() ===
172
- "none"
173
- ) {
159
+ if (isEmptyCatalogResponse(response)) {
174
160
  return null;
175
161
  }
176
162
  if (response.status !== 200) {
177
163
  throw new UpdateHttpError(response.status, response.statusText);
178
164
  }
179
165
 
180
- const body = await response.text();
181
- const catalog = parseReleaseCatalog(body, input.expectedScope);
166
+ const catalog = parseReleaseCatalog(body!, input.expectedScope);
182
167
  if (catalog === null) {
183
168
  await removeNativeReleaseCatalogCache(partition);
184
169
  throw new InvalidUpdateResponseError("Received an invalid Release catalog");
@@ -186,7 +171,7 @@ const consumeSuccessfulResponse = async (
186
171
 
187
172
  const etag = response.headers.get("etag");
188
173
  if (isValidETag(etag)) {
189
- const serialized = serializeCache(etag, body);
174
+ const serialized = serializeCache(etag, body!);
190
175
  if (getUtf8ByteLength(serialized) > MAX_RELEASE_CATALOG_CACHE_ENTRY_BYTES) {
191
176
  await removeNativeReleaseCatalogCache(partition);
192
177
  return catalog;
@@ -216,7 +201,7 @@ export const fetchReleaseCatalogWithCache = async (
216
201
  const cached = await readValidatedCache(partition, input.expectedScope);
217
202
  const response = await fetchCatalogResponse(input, cached?.etag);
218
203
 
219
- if (response.status === 304) {
204
+ if (response.response.status === 304) {
220
205
  if (cached !== null) return cached.catalog;
221
206
 
222
207
  const repairResponse = await fetchCatalogResponse(input);
package/src/sdkVersion.ts CHANGED
@@ -1 +1 @@
1
- export const HOT_UPDATER_SDK_VERSION = "1.0.0-rc.27";
1
+ export const HOT_UPDATER_SDK_VERSION = "1.0.0-rc.29";