@idm-plugin/aliclient2 0.1.1 → 0.1.3

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.
@@ -6,7 +6,8 @@ import { ReadStream } from 'fs';
6
6
  export declare enum OSSRegion {
7
7
  tk = "tk",
8
8
  hz = "hz",
9
- sh = "sh"
9
+ sh = "sh",
10
+ pvc = "pvc"
10
11
  }
11
12
  /**
12
13
  * 阿里云OSS客户端, 限NODE环境中使用
@@ -33,6 +34,13 @@ export declare class AliOssClient {
33
34
  aliSign: boolean;
34
35
  external: string;
35
36
  }[] | never[]>;
37
+ /**
38
+ * 检查对象是否存在
39
+ * @param filePath 对象路径
40
+ * @param options
41
+ * @returns boolean 存在返回 true, 不存在返回 false; 鉴权/网络等异常会向上抛出
42
+ */
43
+ existsObject(filePath: string, options?: any): Promise<boolean>;
36
44
  /**
37
45
  * 删除对象
38
46
  * @param filePath
package/dist/index.js CHANGED
@@ -1,16 +1,16 @@
1
1
  var j = Object.defineProperty;
2
- var w = (c, n, i) => n in c ? j(c, n, { enumerable: !0, configurable: !0, writable: !0, value: i }) : c[n] = i;
3
- var d = (c, n, i) => (w(c, typeof n != "symbol" ? n + "" : n, i), i);
4
- import k from "@log4js-node/log4js-api";
2
+ var w = (i, c, n) => c in i ? j(i, c, { enumerable: !0, configurable: !0, writable: !0, value: n }) : i[c] = n;
3
+ var m = (i, c, n) => (w(i, typeof c != "symbol" ? c + "" : c, n), n);
4
+ import x from "@log4js-node/log4js-api";
5
5
  import y from "ali-oss";
6
- import { ReadStream as x } from "fs";
6
+ import { ReadStream as k } from "fs";
7
7
  let s;
8
8
  try {
9
- s = k.getLogger("ali-client");
9
+ s = x.getLogger("ali-client");
10
10
  } catch {
11
11
  } finally {
12
12
  }
13
- var $ = /* @__PURE__ */ ((c) => (c.tk = "tk", c.hz = "hz", c.sh = "sh", c))($ || {});
13
+ var $ = /* @__PURE__ */ ((i) => (i.tk = "tk", i.hz = "hz", i.sh = "sh", i.pvc = "pvc", i))($ || {});
14
14
  const f = {
15
15
  tk: {
16
16
  region: "oss-ap-northeast-1",
@@ -31,12 +31,19 @@ const f = {
31
31
  domain: "https://osssh.ormwx.com",
32
32
  endpoint: "https://osssh.ormwx.com",
33
33
  forceCname: !0
34
+ },
35
+ pvc: {
36
+ region: "oss-cn-hangzhou",
37
+ bucket: "orm-privacy",
38
+ domain: "https://osspvc.ormwx.com",
39
+ endpoint: "https://osspvc.ormwx.com",
40
+ forceCname: !0
34
41
  }
35
42
  };
36
- function I(c, n) {
37
- return c.domain ? c.domain : `https://${c.bucket}.${c.region}${n ? "-internal" : ""}.aliyuncs.com`;
43
+ function I(i, c) {
44
+ return i.domain ? i.domain : `https://${i.bucket}.${i.region}${c ? "-internal" : ""}.aliyuncs.com`;
38
45
  }
39
- class O {
46
+ class C {
40
47
  /**
41
48
  * 创建阿里云OSS客户端
42
49
  * @param region enum [tk, hz, sh]
@@ -45,47 +52,63 @@ class O {
45
52
  * @param internal boolean true 内网访问
46
53
  * @param cname boolean 是否使用自定义域名访问
47
54
  */
48
- constructor(n, i, t, r, e = !0) {
49
- d(this, "client");
50
- d(this, "region");
51
- d(this, "domain");
52
- this.region = n;
53
- const a = f[this.region] || f.hz, u = !!a.endpoint && (a.forceCname ?? e);
54
- this.domain = I(a, r), this.client = new y({
55
+ constructor(c, n, e, r, t = !0) {
56
+ m(this, "client");
57
+ m(this, "region");
58
+ m(this, "domain");
59
+ this.region = c;
60
+ const o = f[this.region] || f.hz, h = !!o.endpoint && (o.forceCname ?? t);
61
+ this.domain = I(o, r), this.client = new y({
55
62
  secure: !0,
56
- region: a.region,
57
- accessKeyId: i,
58
- accessKeySecret: t,
59
- bucket: a.bucket,
63
+ region: o.region,
64
+ accessKeyId: n,
65
+ accessKeySecret: e,
66
+ bucket: o.bucket,
60
67
  internal: r,
61
- cname: u,
62
- endpoint: u ? a.endpoint : void 0
68
+ cname: h,
69
+ endpoint: h ? o.endpoint : void 0
63
70
  });
64
71
  }
65
- async putObjects(n, i, t = {}) {
66
- const r = i.map(async (e) => {
67
- var o, l;
68
- let a = `${n}/${e.name}`.replaceAll(/\/\//g, "/");
69
- a = a.startsWith("/") ? a.substring(1) : a;
70
- const u = `${this.domain}/${a}`, h = `${this.domain}/${a}`, m = t.headers || {};
71
- return e.stream instanceof x ? await ((o = this.client) == null ? void 0 : o.putStream(a, e.stream)) : await ((l = this.client) == null ? void 0 : l.put(a, e.stream, { headers: m })), { url: u, name: e.name, aliSign: !0, external: h };
72
+ async putObjects(c, n, e = {}) {
73
+ const r = n.map(async (t) => {
74
+ var a, u;
75
+ let o = `${c}/${t.name}`.replaceAll(/\/\//g, "/");
76
+ o = o.startsWith("/") ? o.substring(1) : o;
77
+ const h = `${this.domain}/${o}`, l = `${this.domain}/${o}`, d = e.headers || {};
78
+ return t.stream instanceof k ? await ((a = this.client) == null ? void 0 : a.putStream(o, t.stream)) : await ((u = this.client) == null ? void 0 : u.put(o, t.stream, { headers: d })), { url: h, name: t.name, aliSign: !0, external: l };
72
79
  });
73
- return await Promise.all(r).then((e) => (s.info("[%s] upload to oss success, url: %j", t.requestId, e), e)).catch((e) => (s.error("[%s] upload to oss failed: %j-%s", t.requestId, e), []));
80
+ return await Promise.all(r).then((t) => (s.info("[%s] upload to oss success, url: %j", e.requestId, t), t)).catch((t) => (s.error("[%s] upload to oss failed: %j-%s", e.requestId, t), []));
81
+ }
82
+ /**
83
+ * 检查对象是否存在
84
+ * @param filePath 对象路径
85
+ * @param options
86
+ * @returns boolean 存在返回 true, 不存在返回 false; 鉴权/网络等异常会向上抛出
87
+ */
88
+ async existsObject(c, n = {}) {
89
+ var r;
90
+ let e = `${c}`.replaceAll(/\/\//g, "/");
91
+ e = e.startsWith("/") ? e.substring(1) : e;
92
+ try {
93
+ return await ((r = this.client) == null ? void 0 : r.head(e)), s == null || s.info("[%s] oss object exists: %s", n.requestId, e), !0;
94
+ } catch (t) {
95
+ return (t == null ? void 0 : t.status) === 404 || (t == null ? void 0 : t.code) === "NoSuchKey" || (t == null ? void 0 : t.code) === "NotFound" ? (s == null || s.info("[%s] oss object not exists: %s", n.requestId, e), !1) : (s == null || s.error("[%s] check oss object failed: %s - %j", n.requestId, e, t), !1);
96
+ }
74
97
  }
75
98
  /**
76
99
  * 删除对象
77
100
  * @param filePath
78
101
  * @param options
79
102
  */
80
- async deleteObject(n, i = {}) {
103
+ async deleteObject(c, n = {}) {
81
104
  var r;
82
- let t = `${n}`.replaceAll(/\/\//g, "/");
83
- t = t.startsWith("/") ? t.substring(1) : t;
105
+ let e = `${c}`.replaceAll(/\/\//g, "/");
106
+ e = e.startsWith("/") ? e.substring(1) : e;
84
107
  try {
85
- const e = await ((r = this.client) == null ? void 0 : r.delete(t));
86
- return s == null || s.info("[%s] delete oss object success: %s", i.requestId, t), e;
87
- } catch (e) {
88
- return s == null || s.error("[%s] delete oss object failed: %s - %j", i.requestId, t, e), null;
108
+ const t = await ((r = this.client) == null ? void 0 : r.delete(e));
109
+ return s == null || s.info("[%s] delete oss object success: %s", n.requestId, e), t;
110
+ } catch (t) {
111
+ return s == null || s.error("[%s] delete oss object failed: %s - %j", n.requestId, e, t), null;
89
112
  }
90
113
  }
91
114
  /**
@@ -94,35 +117,35 @@ class O {
94
117
  * @param names
95
118
  * @param options
96
119
  */
97
- async deleteObjects(n, i, t = {}) {
98
- var h, m;
99
- let r = `${n}`.replaceAll(/\/\//g, "/");
120
+ async deleteObjects(c, n, e = {}) {
121
+ var l, d;
122
+ let r = `${c}`.replaceAll(/\/\//g, "/");
100
123
  r = r.startsWith("/") ? r.substring(1) : r, r && !r.endsWith("/") && (r += "/");
101
- let e = [];
102
- if (Array.isArray(i) && i.length > 0)
103
- e = i.map((o) => `${r}${o}`.replaceAll(/\/\//g, "/").replace(/^\//, ""));
124
+ let t = [];
125
+ if (Array.isArray(n) && n.length > 0)
126
+ t = n.map((a) => `${r}${a}`.replaceAll(/\/\//g, "/").replace(/^\//, ""));
104
127
  else {
105
- let o;
128
+ let a;
106
129
  try {
107
130
  do {
108
- const l = await ((h = this.client) == null ? void 0 : h.list({ prefix: r, marker: o, "max-keys": 1e3 })), p = (l == null ? void 0 : l.objects) || [];
109
- e.push(...p.map((b) => b.name)), o = l == null ? void 0 : l.nextMarker;
110
- } while (o);
111
- } catch (l) {
112
- return s == null || s.error("[%s] list oss objects failed: %s - %j", t.requestId, r, l), [];
131
+ const u = await ((l = this.client) == null ? void 0 : l.list({ prefix: r, marker: a, "max-keys": 1e3 })), p = (u == null ? void 0 : u.objects) || [];
132
+ t.push(...p.map((b) => b.name)), a = u == null ? void 0 : u.nextMarker;
133
+ } while (a);
134
+ } catch (u) {
135
+ return s == null || s.error("[%s] list oss objects failed: %s - %j", e.requestId, r, u), [];
113
136
  }
114
137
  }
115
- if (e.length === 0)
116
- return s == null || s.info("[%s] no objects to delete for prefix: %s", t.requestId, r), [];
117
- const a = 1e3, u = [];
138
+ if (t.length === 0)
139
+ return s == null || s.info("[%s] no objects to delete for prefix: %s", e.requestId, r), [];
140
+ const o = 1e3, h = [];
118
141
  try {
119
- for (let o = 0; o < e.length; o += a) {
120
- const l = e.slice(o, o + a), p = await ((m = this.client) == null ? void 0 : m.deleteMulti(l, { quiet: !0 }));
121
- u.push(p);
142
+ for (let a = 0; a < t.length; a += o) {
143
+ const u = t.slice(a, a + o), p = await ((d = this.client) == null ? void 0 : d.deleteMulti(u, { quiet: !0 }));
144
+ h.push(p);
122
145
  }
123
- return s == null || s.info("[%s] delete oss objects success, count: %d", t.requestId, e.length), u;
124
- } catch (o) {
125
- return s == null || s.error("[%s] delete oss objects failed: %s - %j", t.requestId, r, o), [];
146
+ return s == null || s.info("[%s] delete oss objects success, count: %d", e.requestId, t.length), h;
147
+ } catch (a) {
148
+ return s == null || s.error("[%s] delete oss objects failed: %s - %j", e.requestId, r, a), [];
126
149
  }
127
150
  }
128
151
  /**
@@ -130,12 +153,12 @@ class O {
130
153
  * @param url 待签名的url
131
154
  * @param expires 过期时间,单位秒
132
155
  */
133
- async signature(n, i = 60) {
134
- var t;
135
- return (t = this.client) == null ? void 0 : t.signatureUrl(n, { expires: i });
156
+ async signature(c, n = 60) {
157
+ var e;
158
+ return (e = this.client) == null ? void 0 : e.signatureUrl(c, { expires: n });
136
159
  }
137
160
  }
138
161
  export {
139
- O as AliOssClient,
162
+ C as AliOssClient,
140
163
  $ as OSSRegion
141
164
  };
@@ -1 +1 @@
1
- (function(r,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("@log4js-node/log4js-api"),require("ali-oss"),require("fs")):typeof define=="function"&&define.amd?define(["exports","@log4js-node/log4js-api","ali-oss","fs"],c):(r=typeof globalThis<"u"?globalThis:r||self,c(r["idm-plugin-aliclient"]={},r["@log4js-node/log4js-api"],r.OSS,r.fs))})(this,function(r,c,h,k){"use strict";var g=Object.defineProperty;var I=(r,c,h)=>c in r?g(r,c,{enumerable:!0,configurable:!0,writable:!0,value:h}):r[c]=h;var j=(r,c,h)=>(I(r,typeof c!="symbol"?c+"":c,h),h);let t;try{t=c.getLogger("ali-client")}catch{}finally{}var y=(u=>(u.tk="tk",u.hz="hz",u.sh="sh",u))(y||{});const w={tk:{region:"oss-ap-northeast-1",bucket:"idm-wx",domain:"https://osstk.ormwx.com",endpoint:"https://osstk.ormwx.com"},hz:{region:"oss-cn-hangzhou",bucket:"idm-hz",domain:"https://ossimg.ormwx.com",endpoint:"https://ossimg.ormwx.com",forceCname:!0},sh:{region:"oss-cn-shanghai",bucket:"orm-sh",domain:"https://osssh.ormwx.com",endpoint:"https://osssh.ormwx.com",forceCname:!0}};function x(u,d){return u.domain?u.domain:`https://${u.bucket}.${u.region}${d?"-internal":""}.aliyuncs.com`}class ${constructor(d,l,s,i,e=!0){j(this,"client");j(this,"region");j(this,"domain");this.region=d;const n=w[this.region]||w.hz,m=!!n.endpoint&&(n.forceCname??e);this.domain=x(n,i),this.client=new h({secure:!0,region:n.region,accessKeyId:l,accessKeySecret:s,bucket:n.bucket,internal:i,cname:m,endpoint:m?n.endpoint:void 0})}async putObjects(d,l,s={}){const i=l.map(async e=>{var o,a;let n=`${d}/${e.name}`.replaceAll(/\/\//g,"/");n=n.startsWith("/")?n.substring(1):n;const m=`${this.domain}/${n}`,f=`${this.domain}/${n}`,p=s.headers||{};return e.stream instanceof k.ReadStream?await((o=this.client)==null?void 0:o.putStream(n,e.stream)):await((a=this.client)==null?void 0:a.put(n,e.stream,{headers:p})),{url:m,name:e.name,aliSign:!0,external:f}});return await Promise.all(i).then(e=>(t.info("[%s] upload to oss success, url: %j",s.requestId,e),e)).catch(e=>(t.error("[%s] upload to oss failed: %j-%s",s.requestId,e),[]))}async deleteObject(d,l={}){var i;let s=`${d}`.replaceAll(/\/\//g,"/");s=s.startsWith("/")?s.substring(1):s;try{const e=await((i=this.client)==null?void 0:i.delete(s));return t==null||t.info("[%s] delete oss object success: %s",l.requestId,s),e}catch(e){return t==null||t.error("[%s] delete oss object failed: %s - %j",l.requestId,s,e),null}}async deleteObjects(d,l,s={}){var f,p;let i=`${d}`.replaceAll(/\/\//g,"/");i=i.startsWith("/")?i.substring(1):i,i&&!i.endsWith("/")&&(i+="/");let e=[];if(Array.isArray(l)&&l.length>0)e=l.map(o=>`${i}${o}`.replaceAll(/\/\//g,"/").replace(/^\//,""));else{let o;try{do{const a=await((f=this.client)==null?void 0:f.list({prefix:i,marker:o,"max-keys":1e3})),b=(a==null?void 0:a.objects)||[];e.push(...b.map(q=>q.name)),o=a==null?void 0:a.nextMarker}while(o)}catch(a){return t==null||t.error("[%s] list oss objects failed: %s - %j",s.requestId,i,a),[]}}if(e.length===0)return t==null||t.info("[%s] no objects to delete for prefix: %s",s.requestId,i),[];const n=1e3,m=[];try{for(let o=0;o<e.length;o+=n){const a=e.slice(o,o+n),b=await((p=this.client)==null?void 0:p.deleteMulti(a,{quiet:!0}));m.push(b)}return t==null||t.info("[%s] delete oss objects success, count: %d",s.requestId,e.length),m}catch(o){return t==null||t.error("[%s] delete oss objects failed: %s - %j",s.requestId,i,o),[]}}async signature(d,l=60){var s;return(s=this.client)==null?void 0:s.signatureUrl(d,{expires:l})}}r.AliOssClient=$,r.OSSRegion=y,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@log4js-node/log4js-api"),require("ali-oss"),require("fs")):typeof define=="function"&&define.amd?define(["exports","@log4js-node/log4js-api","ali-oss","fs"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r["idm-plugin-aliclient"]={},r["@log4js-node/log4js-api"],r.OSS,r.fs))})(this,function(r,a,h,x){"use strict";var I=Object.defineProperty;var v=(r,a,h)=>a in r?I(r,a,{enumerable:!0,configurable:!0,writable:!0,value:h}):r[a]=h;var j=(r,a,h)=>(v(r,typeof a!="symbol"?a+"":a,h),h);let t;try{t=a.getLogger("ali-client")}catch{}finally{}var y=(d=>(d.tk="tk",d.hz="hz",d.sh="sh",d.pvc="pvc",d))(y||{});const w={tk:{region:"oss-ap-northeast-1",bucket:"idm-wx",domain:"https://osstk.ormwx.com",endpoint:"https://osstk.ormwx.com"},hz:{region:"oss-cn-hangzhou",bucket:"idm-hz",domain:"https://ossimg.ormwx.com",endpoint:"https://ossimg.ormwx.com",forceCname:!0},sh:{region:"oss-cn-shanghai",bucket:"orm-sh",domain:"https://osssh.ormwx.com",endpoint:"https://osssh.ormwx.com",forceCname:!0},pvc:{region:"oss-cn-hangzhou",bucket:"orm-privacy",domain:"https://osspvc.ormwx.com",endpoint:"https://osspvc.ormwx.com",forceCname:!0}};function k(d,u){return d.domain?d.domain:`https://${d.bucket}.${d.region}${u?"-internal":""}.aliyuncs.com`}class q{constructor(u,c,s,i,e=!0){j(this,"client");j(this,"region");j(this,"domain");this.region=u;const n=w[this.region]||w.hz,m=!!n.endpoint&&(n.forceCname??e);this.domain=k(n,i),this.client=new h({secure:!0,region:n.region,accessKeyId:c,accessKeySecret:s,bucket:n.bucket,internal:i,cname:m,endpoint:m?n.endpoint:void 0})}async putObjects(u,c,s={}){const i=c.map(async e=>{var o,l;let n=`${u}/${e.name}`.replaceAll(/\/\//g,"/");n=n.startsWith("/")?n.substring(1):n;const m=`${this.domain}/${n}`,f=`${this.domain}/${n}`,p=s.headers||{};return e.stream instanceof x.ReadStream?await((o=this.client)==null?void 0:o.putStream(n,e.stream)):await((l=this.client)==null?void 0:l.put(n,e.stream,{headers:p})),{url:m,name:e.name,aliSign:!0,external:f}});return await Promise.all(i).then(e=>(t.info("[%s] upload to oss success, url: %j",s.requestId,e),e)).catch(e=>(t.error("[%s] upload to oss failed: %j-%s",s.requestId,e),[]))}async existsObject(u,c={}){var i;let s=`${u}`.replaceAll(/\/\//g,"/");s=s.startsWith("/")?s.substring(1):s;try{return await((i=this.client)==null?void 0:i.head(s)),t==null||t.info("[%s] oss object exists: %s",c.requestId,s),!0}catch(e){return(e==null?void 0:e.status)===404||(e==null?void 0:e.code)==="NoSuchKey"||(e==null?void 0:e.code)==="NotFound"?(t==null||t.info("[%s] oss object not exists: %s",c.requestId,s),!1):(t==null||t.error("[%s] check oss object failed: %s - %j",c.requestId,s,e),!1)}}async deleteObject(u,c={}){var i;let s=`${u}`.replaceAll(/\/\//g,"/");s=s.startsWith("/")?s.substring(1):s;try{const e=await((i=this.client)==null?void 0:i.delete(s));return t==null||t.info("[%s] delete oss object success: %s",c.requestId,s),e}catch(e){return t==null||t.error("[%s] delete oss object failed: %s - %j",c.requestId,s,e),null}}async deleteObjects(u,c,s={}){var f,p;let i=`${u}`.replaceAll(/\/\//g,"/");i=i.startsWith("/")?i.substring(1):i,i&&!i.endsWith("/")&&(i+="/");let e=[];if(Array.isArray(c)&&c.length>0)e=c.map(o=>`${i}${o}`.replaceAll(/\/\//g,"/").replace(/^\//,""));else{let o;try{do{const l=await((f=this.client)==null?void 0:f.list({prefix:i,marker:o,"max-keys":1e3})),b=(l==null?void 0:l.objects)||[];e.push(...b.map($=>$.name)),o=l==null?void 0:l.nextMarker}while(o)}catch(l){return t==null||t.error("[%s] list oss objects failed: %s - %j",s.requestId,i,l),[]}}if(e.length===0)return t==null||t.info("[%s] no objects to delete for prefix: %s",s.requestId,i),[];const n=1e3,m=[];try{for(let o=0;o<e.length;o+=n){const l=e.slice(o,o+n),b=await((p=this.client)==null?void 0:p.deleteMulti(l,{quiet:!0}));m.push(b)}return t==null||t.info("[%s] delete oss objects success, count: %d",s.requestId,e.length),m}catch(o){return t==null||t.error("[%s] delete oss objects failed: %s - %j",s.requestId,i,o),[]}}async signature(u,c=60){var s;return(s=this.client)==null?void 0:s.signatureUrl(u,{expires:c})}}r.AliOssClient=q,r.OSSRegion=y,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@idm-plugin/aliclient2",
3
3
  "private": false,
4
- "version": "0.1.1",
4
+ "version": "0.1.3",
5
5
  "description": "orm plugin for ali used in backend",
6
6
  "type": "module",
7
7
  "keywords": [