@idm-plugin/aliclient2 0.1.0 → 0.1.1

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.
@@ -3,15 +3,21 @@
3
3
  /// <reference types="node" />
4
4
  import OSS from 'ali-oss';
5
5
  import { ReadStream } from 'fs';
6
+ export declare enum OSSRegion {
7
+ tk = "tk",
8
+ hz = "hz",
9
+ sh = "sh"
10
+ }
6
11
  /**
7
12
  * 阿里云OSS客户端, 限NODE环境中使用
8
13
  */
9
14
  export declare class AliOssClient {
10
15
  private client;
11
16
  private readonly region;
17
+ private readonly domain;
12
18
  /**
13
19
  * 创建阿里云OSS客户端
14
- * @param region enum [tk, hz]
20
+ * @param region enum [tk, hz, sh]
15
21
  * @param accessKeyId
16
22
  * @param accessKeySecret
17
23
  * @param internal boolean true 内网访问
package/dist/index.js CHANGED
@@ -1,71 +1,91 @@
1
1
  var j = Object.defineProperty;
2
- var w = (l, c, i) => c in l ? j(l, c, { enumerable: !0, configurable: !0, writable: !0, value: i }) : l[c] = i;
3
- var p = (l, c, i) => (w(l, typeof c != "symbol" ? c + "" : c, i), i);
4
- import x from "@log4js-node/log4js-api";
5
- import f from "ali-oss";
6
- import { ReadStream as k } from "fs";
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";
5
+ import y from "ali-oss";
6
+ import { ReadStream as x } from "fs";
7
7
  let s;
8
8
  try {
9
- s = x.getLogger("ali-client");
9
+ s = k.getLogger("ali-client");
10
10
  } catch {
11
11
  } finally {
12
12
  }
13
- class I {
13
+ var $ = /* @__PURE__ */ ((c) => (c.tk = "tk", c.hz = "hz", c.sh = "sh", c))($ || {});
14
+ const f = {
15
+ tk: {
16
+ region: "oss-ap-northeast-1",
17
+ bucket: "idm-wx",
18
+ domain: "https://osstk.ormwx.com",
19
+ endpoint: "https://osstk.ormwx.com"
20
+ },
21
+ hz: {
22
+ region: "oss-cn-hangzhou",
23
+ bucket: "idm-hz",
24
+ domain: "https://ossimg.ormwx.com",
25
+ endpoint: "https://ossimg.ormwx.com",
26
+ forceCname: !0
27
+ },
28
+ sh: {
29
+ region: "oss-cn-shanghai",
30
+ bucket: "orm-sh",
31
+ domain: "https://osssh.ormwx.com",
32
+ endpoint: "https://osssh.ormwx.com",
33
+ forceCname: !0
34
+ }
35
+ };
36
+ function I(c, n) {
37
+ return c.domain ? c.domain : `https://${c.bucket}.${c.region}${n ? "-internal" : ""}.aliyuncs.com`;
38
+ }
39
+ class O {
14
40
  /**
15
41
  * 创建阿里云OSS客户端
16
- * @param region enum [tk, hz]
42
+ * @param region enum [tk, hz, sh]
17
43
  * @param accessKeyId
18
44
  * @param accessKeySecret
19
45
  * @param internal boolean true 内网访问
20
46
  * @param cname boolean 是否使用自定义域名访问
21
47
  */
22
- constructor(c, i, e, r, t = !0) {
23
- p(this, "client");
24
- p(this, "region");
25
- this.region = c, c === "tk" ? this.client = new f({
26
- secure: !0,
27
- region: "oss-ap-northeast-1",
28
- accessKeyId: i,
29
- accessKeySecret: e,
30
- bucket: "idm-wx",
31
- internal: r,
32
- cname: t,
33
- endpoint: t ? "https://osstk.ormwx.com" : void 0
34
- }) : this.client = new f({
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({
35
55
  secure: !0,
36
- region: "oss-cn-hangzhou",
56
+ region: a.region,
37
57
  accessKeyId: i,
38
- accessKeySecret: e,
39
- bucket: "idm-hz",
58
+ accessKeySecret: t,
59
+ bucket: a.bucket,
40
60
  internal: r,
41
- cname: !0,
42
- endpoint: "https://ossimg.ormwx.com"
61
+ cname: u,
62
+ endpoint: u ? a.endpoint : void 0
43
63
  });
44
64
  }
45
- async putObjects(c, i, e = {}) {
46
- const r = i.map(async (t) => {
47
- var n, o;
48
- let a = `${c}/${t.name}`.replaceAll(/\/\//g, "/");
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, "/");
49
69
  a = a.startsWith("/") ? a.substring(1) : a;
50
- const u = this.region === "tk" ? `https://osstk.ormwx.com/${a}` : `https://ossimg.ormwx.com/${a}`, h = this.region === "tk" ? `https://osstk.ormwx.com/${a}` : `https://ossimg.ormwx.com/${a}`, m = e.headers || {};
51
- return t.stream instanceof k ? await ((n = this.client) == null ? void 0 : n.putStream(a, t.stream)) : await ((o = this.client) == null ? void 0 : o.put(a, t.stream, { headers: m })), { url: u, name: t.name, aliSign: !0, external: h };
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 };
52
72
  });
53
- 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), []));
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), []));
54
74
  }
55
75
  /**
56
76
  * 删除对象
57
77
  * @param filePath
58
78
  * @param options
59
79
  */
60
- async deleteObject(c, i = {}) {
80
+ async deleteObject(n, i = {}) {
61
81
  var r;
62
- let e = `${c}`.replaceAll(/\/\//g, "/");
63
- e = e.startsWith("/") ? e.substring(1) : e;
82
+ let t = `${n}`.replaceAll(/\/\//g, "/");
83
+ t = t.startsWith("/") ? t.substring(1) : t;
64
84
  try {
65
- const t = await ((r = this.client) == null ? void 0 : r.delete(e));
66
- return s == null || s.info("[%s] delete oss object success: %s", i.requestId, e), t;
67
- } catch (t) {
68
- return s == null || s.error("[%s] delete oss object failed: %s - %j", i.requestId, e, t), null;
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;
69
89
  }
70
90
  }
71
91
  /**
@@ -74,35 +94,35 @@ class I {
74
94
  * @param names
75
95
  * @param options
76
96
  */
77
- async deleteObjects(c, i, e = {}) {
97
+ async deleteObjects(n, i, t = {}) {
78
98
  var h, m;
79
- let r = `${c}`.replaceAll(/\/\//g, "/");
99
+ let r = `${n}`.replaceAll(/\/\//g, "/");
80
100
  r = r.startsWith("/") ? r.substring(1) : r, r && !r.endsWith("/") && (r += "/");
81
- let t = [];
101
+ let e = [];
82
102
  if (Array.isArray(i) && i.length > 0)
83
- t = i.map((n) => `${r}${n}`.replaceAll(/\/\//g, "/").replace(/^\//, ""));
103
+ e = i.map((o) => `${r}${o}`.replaceAll(/\/\//g, "/").replace(/^\//, ""));
84
104
  else {
85
- let n;
105
+ let o;
86
106
  try {
87
107
  do {
88
- const o = await ((h = this.client) == null ? void 0 : h.list({ prefix: r, marker: n, "max-keys": 1e3 })), d = (o == null ? void 0 : o.objects) || [];
89
- t.push(...d.map((b) => b.name)), n = o == null ? void 0 : o.nextMarker;
90
- } while (n);
91
- } catch (o) {
92
- return s == null || s.error("[%s] list oss objects failed: %s - %j", e.requestId, r, o), [];
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), [];
93
113
  }
94
114
  }
95
- if (t.length === 0)
96
- return s == null || s.info("[%s] no objects to delete for prefix: %s", e.requestId, r), [];
115
+ if (e.length === 0)
116
+ return s == null || s.info("[%s] no objects to delete for prefix: %s", t.requestId, r), [];
97
117
  const a = 1e3, u = [];
98
118
  try {
99
- for (let n = 0; n < t.length; n += a) {
100
- const o = t.slice(n, n + a), d = await ((m = this.client) == null ? void 0 : m.deleteMulti(o, { quiet: !0 }));
101
- u.push(d);
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);
102
122
  }
103
- return s == null || s.info("[%s] delete oss objects success, count: %d", e.requestId, t.length), u;
104
- } catch (n) {
105
- return s == null || s.error("[%s] delete oss objects failed: %s - %j", e.requestId, r, n), [];
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), [];
106
126
  }
107
127
  }
108
128
  /**
@@ -110,11 +130,12 @@ class I {
110
130
  * @param url 待签名的url
111
131
  * @param expires 过期时间,单位秒
112
132
  */
113
- async signature(c, i = 60) {
114
- var e;
115
- return (e = this.client) == null ? void 0 : e.signatureUrl(c, { expires: i });
133
+ async signature(n, i = 60) {
134
+ var t;
135
+ return (t = this.client) == null ? void 0 : t.signatureUrl(n, { expires: i });
116
136
  }
117
137
  }
118
138
  export {
119
- I as AliOssClient
139
+ O as AliOssClient,
140
+ $ as OSSRegion
120
141
  };
@@ -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,d,b){"use strict";var q=Object.defineProperty;var k=(r,c,d)=>c in r?q(r,c,{enumerable:!0,configurable:!0,writable:!0,value:d}):r[c]=d;var j=(r,c,d)=>(k(r,typeof c!="symbol"?c+"":c,d),d);let s;try{s=c.getLogger("ali-client")}catch{}finally{}class w{constructor(u,o,t,i,e=!0){j(this,"client");j(this,"region");this.region=u,u==="tk"?this.client=new d({secure:!0,region:"oss-ap-northeast-1",accessKeyId:o,accessKeySecret:t,bucket:"idm-wx",internal:i,cname:e,endpoint:e?"https://osstk.ormwx.com":void 0}):this.client=new d({secure:!0,region:"oss-cn-hangzhou",accessKeyId:o,accessKeySecret:t,bucket:"idm-hz",internal:i,cname:!0,endpoint:"https://ossimg.ormwx.com"})}async putObjects(u,o,t={}){const i=o.map(async e=>{var n,a;let l=`${u}/${e.name}`.replaceAll(/\/\//g,"/");l=l.startsWith("/")?l.substring(1):l;const h=this.region==="tk"?`https://osstk.ormwx.com/${l}`:`https://ossimg.ormwx.com/${l}`,f=this.region==="tk"?`https://osstk.ormwx.com/${l}`:`https://ossimg.ormwx.com/${l}`,m=t.headers||{};return e.stream instanceof b.ReadStream?await((n=this.client)==null?void 0:n.putStream(l,e.stream)):await((a=this.client)==null?void 0:a.put(l,e.stream,{headers:m})),{url:h,name:e.name,aliSign:!0,external:f}});return await Promise.all(i).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),[]))}async deleteObject(u,o={}){var i;let t=`${u}`.replaceAll(/\/\//g,"/");t=t.startsWith("/")?t.substring(1):t;try{const e=await((i=this.client)==null?void 0:i.delete(t));return s==null||s.info("[%s] delete oss object success: %s",o.requestId,t),e}catch(e){return s==null||s.error("[%s] delete oss object failed: %s - %j",o.requestId,t,e),null}}async deleteObjects(u,o,t={}){var f,m;let i=`${u}`.replaceAll(/\/\//g,"/");i=i.startsWith("/")?i.substring(1):i,i&&!i.endsWith("/")&&(i+="/");let e=[];if(Array.isArray(o)&&o.length>0)e=o.map(n=>`${i}${n}`.replaceAll(/\/\//g,"/").replace(/^\//,""));else{let n;try{do{const a=await((f=this.client)==null?void 0:f.list({prefix:i,marker:n,"max-keys":1e3})),p=(a==null?void 0:a.objects)||[];e.push(...p.map(x=>x.name)),n=a==null?void 0:a.nextMarker}while(n)}catch(a){return s==null||s.error("[%s] list oss objects failed: %s - %j",t.requestId,i,a),[]}}if(e.length===0)return s==null||s.info("[%s] no objects to delete for prefix: %s",t.requestId,i),[];const l=1e3,h=[];try{for(let n=0;n<e.length;n+=l){const a=e.slice(n,n+l),p=await((m=this.client)==null?void 0:m.deleteMulti(a,{quiet:!0}));h.push(p)}return s==null||s.info("[%s] delete oss objects success, count: %d",t.requestId,e.length),h}catch(n){return s==null||s.error("[%s] delete oss objects failed: %s - %j",t.requestId,i,n),[]}}async signature(u,o=60){var t;return(t=this.client)==null?void 0:t.signatureUrl(u,{expires:o})}}r.AliOssClient=w,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@idm-plugin/aliclient2",
3
3
  "private": false,
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "description": "orm plugin for ali used in backend",
6
6
  "type": "module",
7
7
  "keywords": [