@autonomys/auto-files 1.5.0 → 1.5.2

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.
package/dist/api.d.ts CHANGED
@@ -13,7 +13,13 @@ interface FetchedFile {
13
13
  * @returns An object containing methods to interact with the API
14
14
  */
15
15
  export declare const createAutoFilesApi: (baseUrl: string, apiSecret: string) => {
16
- getFile: (cid: string, { retriesPerFetch, onProgress, }?: {
16
+ getFile: (cid: string, { retriesPerFetch, onProgress, ignoreCache, }?: {
17
+ retriesPerFetch?: number;
18
+ onProgress?: (progress: number) => void;
19
+ ignoreCache?: boolean;
20
+ }) => Promise<Readable>;
21
+ isFileCached: (cid: string) => Promise<any>;
22
+ getChunkedFile: (cid: string, { retriesPerFetch, onProgress, }?: {
17
23
  retriesPerFetch?: number;
18
24
  onProgress?: (progress: number) => void;
19
25
  }) => Promise<FetchedFile>;
package/dist/api.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAC7E,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AAGjC,UAAU,WAAW;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,kBAAkB,GAAG,SAAS,CAAA;IAC3C,UAAU,EAAE,iBAAiB,GAAG,SAAS,CAAA;IACzC,IAAI,EAAE,QAAQ,CAAA;CACf;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,SAAS,MAAM,EAAE,WAAW,MAAM;mBA+C5D,MAAM,qCAIR;QAAE,eAAe,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;KAAE,KACvE,OAAO,CAAC,WAAW,CAAC;CAoCxB,CAAA"}
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAC7E,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AAEjC,UAAU,WAAW;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,kBAAkB,GAAG,SAAS,CAAA;IAC3C,UAAU,EAAE,iBAAiB,GAAG,SAAS,CAAA;IACzC,IAAI,EAAE,QAAQ,CAAA;CACf;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,SAAS,MAAM,EAAE,WAAW,MAAM;mBAmI5D,MAAM,kDAKR;QACD,eAAe,CAAC,EAAE,MAAM,CAAA;QACxB,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;QACvC,WAAW,CAAC,EAAE,OAAO,CAAA;KACtB;wBA5C8B,MAAM;0BAlDhC,MAAM,qCAIR;QAAE,eAAe,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;KAAE,KACvE,OAAO,CAAC,WAAW,CAAC;CAuGxB,CAAA"}
package/dist/api.js CHANGED
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.createAutoFilesApi = void 0;
13
+ const asynchronous_1 = require("@autonomys/asynchronous");
13
14
  const stream_1 = require("stream");
14
15
  const utils_1 = require("./utils");
15
16
  /**
@@ -39,13 +40,12 @@ const createAutoFilesApi = (baseUrl, apiSecret) => {
39
40
  const getChunk = (cid, chunk) => __awaiter(void 0, void 0, void 0, function* () {
40
41
  const response = yield authFetch(`${baseUrl}/files/${cid}/partial?chunk=${chunk}`);
41
42
  if (!response.ok) {
42
- throw new Error('Error fetching chunk');
43
+ throw new Error(`Error fetching chunk: ${response.status} ${response.statusText}`);
43
44
  }
44
45
  if (response.status === 204) {
45
46
  return null;
46
47
  }
47
48
  const buffer = yield response.arrayBuffer();
48
- console.log('Chunk download finished:', buffer.byteLength);
49
49
  return buffer;
50
50
  });
51
51
  /**
@@ -57,11 +57,16 @@ const createAutoFilesApi = (baseUrl, apiSecret) => {
57
57
  * @returns A Promise that resolves to a FetchedFile object containing the file data and metadata
58
58
  * @throws Error if the file metadata fetch fails
59
59
  */
60
- const getFile = (cid_1, ...args_1) => __awaiter(void 0, [cid_1, ...args_1], void 0, function* (cid, { retriesPerFetch = 3, onProgress, } = {}) {
60
+ const getChunkedFile = (cid_1, ...args_1) => __awaiter(void 0, [cid_1, ...args_1], void 0, function* (cid, { retriesPerFetch = 3, onProgress, } = {}) {
61
61
  var _a, _b, _c;
62
- const response = yield (0, utils_1.withRetries)(() => authFetch(`${baseUrl}/files/${cid}/metadata`), retriesPerFetch);
62
+ const response = yield (0, utils_1.withRetries)(() => authFetch(`${baseUrl}/files/${cid}/metadata`), {
63
+ retries: retriesPerFetch,
64
+ onRetry: (error, pendingRetries) => {
65
+ console.error(`Error fetching file header, pending retries: ${pendingRetries}`, error);
66
+ },
67
+ });
63
68
  if (!response.ok) {
64
- throw new Error('Error fetching file header');
69
+ throw new Error(`Error fetching file header: ${response.status} ${response.statusText}`);
65
70
  }
66
71
  const metadata = yield response.json();
67
72
  // Parse size from string to bigint
@@ -76,7 +81,9 @@ const createAutoFilesApi = (baseUrl, apiSecret) => {
76
81
  read() {
77
82
  return __awaiter(this, void 0, void 0, function* () {
78
83
  var _a;
79
- const chunk = yield (0, utils_1.withRetries)(() => getChunk(cid, i++), retriesPerFetch);
84
+ const chunk = yield (0, utils_1.withRetries)(() => getChunk(cid, i++), {
85
+ retries: retriesPerFetch,
86
+ });
80
87
  this.push(chunk ? Buffer.from(chunk) : null);
81
88
  totalDownloaded += BigInt((_a = chunk === null || chunk === void 0 ? void 0 : chunk.byteLength) !== null && _a !== void 0 ? _a : 0);
82
89
  onProgress === null || onProgress === void 0 ? void 0 : onProgress(Number((BigInt(precision) * totalDownloaded) / length) / precision);
@@ -88,6 +95,48 @@ const createAutoFilesApi = (baseUrl, apiSecret) => {
88
95
  encryption,
89
96
  };
90
97
  });
91
- return { getFile };
98
+ /**
99
+ * Checks if a file is cached on the gateway
100
+ * @param cid - The content identifier of the file
101
+ * @returns A Promise that resolves to true if the file is cached, false otherwise
102
+ * @throws Error if the file status check fails
103
+ */
104
+ const isFileCached = (cid) => __awaiter(void 0, void 0, void 0, function* () {
105
+ const response = yield authFetch(`${baseUrl}/files/${cid}/status`);
106
+ if (!response.ok) {
107
+ throw new Error(`Error checking file status: ${response.status} ${response.statusText}`);
108
+ }
109
+ const status = yield response.json();
110
+ return status.isCached;
111
+ });
112
+ const getFileFromCache = (cid_1, ...args_1) => __awaiter(void 0, [cid_1, ...args_1], void 0, function* (cid, options = {}) {
113
+ var _a;
114
+ const raw = (_a = options.raw) !== null && _a !== void 0 ? _a : false;
115
+ const response = yield authFetch(`${baseUrl}/files/${cid}?raw=${raw}`);
116
+ if (!response.ok) {
117
+ throw new Error(`Error fetching file from cache: ${response.status} ${response.statusText}`);
118
+ }
119
+ if (!response.body) {
120
+ throw new Error('No body in response');
121
+ }
122
+ return (0, asynchronous_1.httpBodyToStream)(response.body);
123
+ });
124
+ /**
125
+ * Fetches a complete file from the API with support for progress tracking and retries
126
+ * @param cid - The content identifier of the file to fetch
127
+ * @returns A Promise that resolves to a FetchedFile object containing the file data and metadata
128
+ * @throws Error if the file metadata fetch fails
129
+ */
130
+ const getFile = (cid_1, ...args_1) => __awaiter(void 0, [cid_1, ...args_1], void 0, function* (cid, { retriesPerFetch = 3, onProgress, ignoreCache = false, } = {}) {
131
+ if (!ignoreCache && (yield isFileCached(cid))) {
132
+ return getFileFromCache(cid, { raw: true });
133
+ }
134
+ const file = yield getChunkedFile(cid, {
135
+ retriesPerFetch,
136
+ onProgress,
137
+ });
138
+ return file.data;
139
+ });
140
+ return { getFile, isFileCached, getChunkedFile };
92
141
  };
93
142
  exports.createAutoFilesApi = createAutoFilesApi;
package/dist/utils.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- export declare const withRetries: <T>(fn: () => Promise<T>, retries: number, delay?: number) => Promise<T>;
1
+ export declare const withRetries: <T>(fn: () => Promise<T>, { retries, delay, onRetry, }?: {
2
+ retries?: number;
3
+ delay?: number;
4
+ onRetry?: (error: Error, pendingRetries: number) => void;
5
+ }) => Promise<T>;
2
6
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,GAAI,CAAC,EAAE,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,MAAM,EAAE,QAAO,MAAa,eAiBzF,CAAA"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,GAAI,CAAC,EAC3B,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,+BAIG;IACD,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,KAAK,IAAI,CAAA;CACpD,eAmBP,CAAA"}
package/dist/utils.js CHANGED
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.withRetries = void 0;
13
- const withRetries = (fn, retries, delay = 1000) => {
13
+ const withRetries = (fn, { retries = 3, delay = 1000, onRetry, } = {}) => {
14
14
  return new Promise((resolve, reject) => {
15
15
  const attempt = () => __awaiter(void 0, void 0, void 0, function* () {
16
16
  try {
@@ -19,6 +19,7 @@ const withRetries = (fn, retries, delay = 1000) => {
19
19
  }
20
20
  catch (error) {
21
21
  if (retries > 0) {
22
+ onRetry === null || onRetry === void 0 ? void 0 : onRetry(error, retries);
22
23
  yield new Promise((resolve) => setTimeout(resolve, delay));
23
24
  attempt();
24
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autonomys/auto-files",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "packageManager": "yarn@4.7.0",
5
5
  "scripts": {
6
6
  "build": "tsc"
@@ -21,7 +21,7 @@
21
21
  }
22
22
  },
23
23
  "dependencies": {
24
- "@autonomys/auto-drive": "^1.5.0"
24
+ "@autonomys/auto-drive": "^1.5.2"
25
25
  },
26
- "gitHead": "27ce75d189f2a9b0f3459cf19cc05dc07ff16100"
26
+ "gitHead": "caa444d14e4f81e9aa408098fe0192dc2131953a"
27
27
  }