@kevisual/cnb 0.0.74 → 0.0.75

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/cnb",
3
- "version": "0.0.74",
3
+ "version": "0.0.75",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "basename": "/root/cnb",
@@ -8,22 +8,27 @@ type DownloadFn = (url: string, opts?: { headers?: any, hash?: string, token?: s
8
8
  */
9
9
  export const downloadByCNBApi: DownloadFn = async (url, opts) => {
10
10
  const hash = opts?.hash;
11
- const headers = opts?.token ? { Authorization: `${opts.token}` } : {};
11
+ const headers = {
12
+ Accept: "application/vnd.cnb.api+json",
13
+ // Authorization: `${CNB_API_KEY}`
14
+ ...(opts?.token ? { Authorization: `${opts.token}` } : {}),
15
+ ...opts?.headers
16
+ }
12
17
  const res = await fetch(url, {
13
- headers: {
14
- Accept: "application/vnd.cnb.api+json",
15
- ...headers,
16
- ...opts?.headers
17
- }
18
+ headers: headers
18
19
  });
19
20
  type CNBResponse = {
20
- type?: 'blob' | 'file' | 'dir';
21
+ type?: 'blob' | 'file' | 'dir' | 'lfs';
21
22
  size?: number;
22
23
  path: string;
23
24
  name: string;
24
25
  sha?: string;
25
26
  encoding?: string;
26
27
  content?: string;
28
+
29
+ lfs_oid?: string;
30
+ lfs_size?: number;
31
+ lfs_download_url?: string;
27
32
  }
28
33
  let resJson: CNBResponse;
29
34
  try {
@@ -32,13 +37,40 @@ export const downloadByCNBApi: DownloadFn = async (url, opts) => {
32
37
  console.error(`下载失败 ${url}: ${res.statusText}`, e);
33
38
  return { code: res.status, buffer: null };
34
39
  }
35
- if (res.ok && resJson.type === 'blob' && resJson.encoding === 'base64' && resJson.content) {
36
- if (hash && resJson.sha === hash) {
37
- // 文件未修改,跳过下载
38
- return { code: 304, buffer: null };
40
+ if (res.ok) {
41
+ if (resJson.size === 0) {
42
+ return { code: 200, buffer: Buffer.from('') };
39
43
  }
40
- const buffer = Buffer.from(resJson.content, 'base64');
41
- return { code: 200, buffer };
44
+ if (resJson.type === 'blob' && resJson.encoding === 'base64') {
45
+ if (resJson.content) {
46
+ if (hash && resJson.sha === hash) {
47
+ // 文件未修改,跳过下载
48
+ return { code: 304, buffer: null };
49
+ }
50
+ const buffer = Buffer.from(resJson.content, 'base64');
51
+ return { code: 200, buffer };
52
+ }
53
+ }
54
+ if (resJson.type === 'lfs') {
55
+ const lfs_download_url = resJson.lfs_download_url;
56
+ if (lfs_download_url) {
57
+ const lfsRes = await fetch(lfs_download_url, {
58
+ headers: headers
59
+ });
60
+ if (lfsRes.ok) {
61
+ const etag = lfsRes.headers.get('ETag');
62
+ if (hash && etag === hash) {
63
+ // 文件未修改,跳过下载
64
+ return { code: 304, buffer: null };
65
+ }
66
+ const arrayBuffer = await lfsRes.arrayBuffer();
67
+ return { code: 200, buffer: Buffer.from(arrayBuffer) };
68
+ }
69
+ }
70
+ }
71
+
72
+ console.error(`下载失败 ${url}: 内容为空`, resJson);
73
+ return { code: 200, buffer: null };
42
74
  } else {
43
75
  console.error(`下载失败 ${url}: ${res.statusText}`, resJson);
44
76
  return { code: res.status, buffer: null };