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