@idm-plugin/aliclient2 0.1.0 → 0.1.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.
@@ -3,15 +3,22 @@
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
+ pvc = "pvc"
11
+ }
6
12
  /**
7
13
  * 阿里云OSS客户端, 限NODE环境中使用
8
14
  */
9
15
  export declare class AliOssClient {
10
16
  private client;
11
17
  private readonly region;
18
+ private readonly domain;
12
19
  /**
13
20
  * 创建阿里云OSS客户端
14
- * @param region enum [tk, hz]
21
+ * @param region enum [tk, hz, sh]
15
22
  * @param accessKeyId
16
23
  * @param accessKeySecret
17
24
  * @param internal boolean true 内网访问
package/dist/index.js CHANGED
@@ -1,54 +1,81 @@
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 = (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";
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__ */ ((i) => (i.tk = "tk", i.hz = "hz", i.sh = "sh", i.pvc = "pvc", i))($ || {});
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
+ 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
41
+ }
42
+ };
43
+ function v(i, n) {
44
+ return i.domain ? i.domain : `https://${i.bucket}.${i.region}${n ? "-internal" : ""}.aliyuncs.com`;
45
+ }
46
+ class C {
14
47
  /**
15
48
  * 创建阿里云OSS客户端
16
- * @param region enum [tk, hz]
49
+ * @param region enum [tk, hz, sh]
17
50
  * @param accessKeyId
18
51
  * @param accessKeySecret
19
52
  * @param internal boolean true 内网访问
20
53
  * @param cname boolean 是否使用自定义域名访问
21
54
  */
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({
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({
35
62
  secure: !0,
36
- region: "oss-cn-hangzhou",
37
- accessKeyId: i,
63
+ region: c.region,
64
+ accessKeyId: o,
38
65
  accessKeySecret: e,
39
- bucket: "idm-hz",
66
+ bucket: c.bucket,
40
67
  internal: r,
41
- cname: !0,
42
- endpoint: "https://ossimg.ormwx.com"
68
+ cname: l,
69
+ endpoint: l ? c.endpoint : void 0
43
70
  });
44
71
  }
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, "/");
49
- 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 };
72
+ async putObjects(n, o, e = {}) {
73
+ const r = o.map(async (t) => {
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 };
52
79
  });
53
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), []));
54
81
  }
@@ -57,15 +84,15 @@ class I {
57
84
  * @param filePath
58
85
  * @param options
59
86
  */
60
- async deleteObject(c, i = {}) {
87
+ async deleteObject(n, o = {}) {
61
88
  var r;
62
- let e = `${c}`.replaceAll(/\/\//g, "/");
89
+ let e = `${n}`.replaceAll(/\/\//g, "/");
63
90
  e = e.startsWith("/") ? e.substring(1) : e;
64
91
  try {
65
92
  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;
93
+ return s == null || s.info("[%s] delete oss object success: %s", o.requestId, e), t;
67
94
  } catch (t) {
68
- return s == null || s.error("[%s] delete oss object failed: %s - %j", i.requestId, e, t), null;
95
+ return s == null || s.error("[%s] delete oss object failed: %s - %j", o.requestId, e, t), null;
69
96
  }
70
97
  }
71
98
  /**
@@ -74,35 +101,35 @@ class I {
74
101
  * @param names
75
102
  * @param options
76
103
  */
77
- async deleteObjects(c, i, e = {}) {
104
+ async deleteObjects(n, o, e = {}) {
78
105
  var h, m;
79
- let r = `${c}`.replaceAll(/\/\//g, "/");
106
+ let r = `${n}`.replaceAll(/\/\//g, "/");
80
107
  r = r.startsWith("/") ? r.substring(1) : r, r && !r.endsWith("/") && (r += "/");
81
108
  let t = [];
82
- if (Array.isArray(i) && i.length > 0)
83
- t = i.map((n) => `${r}${n}`.replaceAll(/\/\//g, "/").replace(/^\//, ""));
109
+ if (Array.isArray(o) && o.length > 0)
110
+ t = o.map((a) => `${r}${a}`.replaceAll(/\/\//g, "/").replace(/^\//, ""));
84
111
  else {
85
- let n;
112
+ let a;
86
113
  try {
87
114
  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), [];
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) || [];
116
+ t.push(...p.map((b) => b.name)), a = u == null ? void 0 : u.nextMarker;
117
+ } while (a);
118
+ } catch (u) {
119
+ return s == null || s.error("[%s] list oss objects failed: %s - %j", e.requestId, r, u), [];
93
120
  }
94
121
  }
95
122
  if (t.length === 0)
96
123
  return s == null || s.info("[%s] no objects to delete for prefix: %s", e.requestId, r), [];
97
- const a = 1e3, u = [];
124
+ const c = 1e3, l = [];
98
125
  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);
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);
102
129
  }
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), [];
130
+ return s == null || s.info("[%s] delete oss objects success, count: %d", e.requestId, t.length), l;
131
+ } catch (a) {
132
+ return s == null || s.error("[%s] delete oss objects failed: %s - %j", e.requestId, r, a), [];
106
133
  }
107
134
  }
108
135
  /**
@@ -110,11 +137,12 @@ class I {
110
137
  * @param url 待签名的url
111
138
  * @param expires 过期时间,单位秒
112
139
  */
113
- async signature(c, i = 60) {
140
+ async signature(n, o = 60) {
114
141
  var e;
115
- return (e = this.client) == null ? void 0 : e.signatureUrl(c, { expires: i });
142
+ return (e = this.client) == null ? void 0 : e.signatureUrl(n, { expires: o });
116
143
  }
117
144
  }
118
145
  export {
119
- I as AliOssClient
146
+ C as AliOssClient,
147
+ $ as OSSRegion
120
148
  };
@@ -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 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"})});
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.2",
5
5
  "description": "orm plugin for ali used in backend",
6
6
  "type": "module",
7
7
  "keywords": [