dfs-adapter 1.0.0 → 1.0.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.
package/lib/cos.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { Dfs, CosConfig } from "./declare";
2
+ import { VWebApplicationContext } from "vweb-core";
3
+ export default class CosDfs extends Dfs<'cos'> {
4
+ private readonly client;
5
+ constructor(options: CosConfig & {
6
+ type: 'cos';
7
+ }, context: VWebApplicationContext);
8
+ startup(): Promise<void>;
9
+ private getKey;
10
+ upload(path: string, buffer: any, options?: any): Promise<string>;
11
+ download(path: string, options?: any): Promise<Buffer>;
12
+ getAccess(path: string, options?: any): Promise<string>;
13
+ }
package/lib/cos.js ADDED
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const declare_1 = require("./declare");
7
+ const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ class CosDfs extends declare_1.Dfs {
10
+ client;
11
+ constructor(options, context) {
12
+ super(options, context);
13
+ this.client = new cos_nodejs_sdk_v5_1.default({
14
+ SecretId: options.accessKeyId,
15
+ SecretKey: options.accessKeySecret
16
+ });
17
+ }
18
+ async startup() {
19
+ return super.startup();
20
+ }
21
+ getKey(path) {
22
+ if (path.startsWith('/')) {
23
+ path = path.substring(1);
24
+ }
25
+ return path;
26
+ }
27
+ async upload(path, buffer, options = {}) {
28
+ try {
29
+ const Key = this.getKey(path);
30
+ this.timeBegin(`upload ${path}`);
31
+ let payload = { Body: buffer, ...options };
32
+ if (typeof buffer === 'string' && fs_1.default.existsSync(buffer)) {
33
+ payload = {
34
+ Body: fs_1.default.createReadStream(path),
35
+ ContentLength: fs_1.default.statSync(path).size
36
+ };
37
+ }
38
+ await this.client.putObject({
39
+ Bucket: this.options.bucket,
40
+ Region: this.options.region,
41
+ Key,
42
+ ...payload
43
+ });
44
+ return Key;
45
+ }
46
+ finally {
47
+ this.timeEnd(`upload ${path}`);
48
+ }
49
+ }
50
+ async download(path, options = {}) {
51
+ try {
52
+ const Key = this.getKey(path);
53
+ this.timeBegin(`download ${path}`);
54
+ const result = await this.client.getObject({
55
+ Bucket: this.options.bucket,
56
+ Region: this.options.region,
57
+ Key,
58
+ ...options
59
+ });
60
+ return result.Body;
61
+ }
62
+ finally {
63
+ this.timeEnd(`download ${path}`);
64
+ }
65
+ }
66
+ async getAccess(path, options = {}) {
67
+ if (this.options.access) {
68
+ return this.options.access + path;
69
+ }
70
+ const Key = this.getKey(path);
71
+ return new Promise((resolve, reject) => {
72
+ this.client.getObjectUrl({
73
+ Bucket: this.options.bucket,
74
+ Region: this.options.region,
75
+ Key,
76
+ ...options
77
+ }, (err, result) => {
78
+ if (err) {
79
+ return reject(err);
80
+ }
81
+ resolve(result.Url);
82
+ });
83
+ });
84
+ }
85
+ }
86
+ exports.default = CosDfs;
package/lib/declare.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import { VWebApplicationContext } from "vweb-core";
4
2
  export declare interface OssConfig {
5
3
  accessKeyId: string;
@@ -45,12 +43,37 @@ export declare interface FastdfsConfig {
45
43
  token: string;
46
44
  access: string;
47
45
  }
46
+ export declare interface ObsConfig {
47
+ accessKeyId: string;
48
+ accessKeySecret: string;
49
+ endpoint: string;
50
+ bucket: string;
51
+ access: string;
52
+ }
53
+ export declare interface CosConfig {
54
+ accessKeyId: string;
55
+ accessKeySecret: string;
56
+ region: string;
57
+ bucket: string;
58
+ access?: string;
59
+ }
60
+ export declare interface SftpConfig {
61
+ host: string;
62
+ port?: number;
63
+ username: string;
64
+ password: string;
65
+ saveDir?: string;
66
+ access: string;
67
+ }
48
68
  export declare type ConfigOptionsMap = {
49
69
  oss: OssConfig;
50
70
  minio: MinioConfig;
51
71
  http: HttpConfig;
52
72
  native: NativeConfig;
53
73
  fastdfs: FastdfsConfig;
74
+ obs: ObsConfig;
75
+ cos: CosConfig;
76
+ sftp: SftpConfig;
54
77
  };
55
78
  export declare type ConfigOptionsKey = keyof ConfigOptionsMap;
56
79
  type ConfigOptionsOptions<T> = T extends ConfigOptionsKey ? {
@@ -71,6 +94,6 @@ export declare class Dfs<T extends ConfigOptionsKey> {
71
94
  startup(): Promise<void>;
72
95
  upload(path: string, buffer: any, ...args: any[]): Promise<string>;
73
96
  download(path: string, ...args: any[]): Promise<Buffer>;
74
- getAccess(path: string): Promise<string>;
97
+ getAccess(path: string, ...args: any[]): Promise<string>;
75
98
  }
76
99
  export {};
package/lib/declare.js CHANGED
@@ -64,7 +64,7 @@ class Dfs {
64
64
  }
65
65
  return null;
66
66
  }
67
- async getAccess(path) {
67
+ async getAccess(path, ...args) {
68
68
  if (this.logger.isDebugEnabled()) {
69
69
  this.logger.info(`${this.constructor.name} access ${path}`);
70
70
  }
package/lib/fastdfs.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Dfs, FastdfsConfig } from "./declare";
3
2
  import { VWebApplicationContext } from "vweb-core";
4
3
  export default class HttpDfs extends Dfs<'fastdfs'> {
package/lib/http.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Dfs, HttpConfig } from "./declare";
3
2
  import { VWebApplicationContext } from "vweb-core";
4
3
  export default class HttpDfs extends Dfs<'http'> {
package/lib/mdfs.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { VWebApplicationContext } from "vweb-core";
3
2
  import { ConfigOptionsKey, Dfs, DfsConfig } from "./declare";
4
3
  export default class Mdfs extends Dfs<ConfigOptionsKey> {
package/lib/minio.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Dfs, MinioConfig } from "./declare";
3
2
  import { VWebApplicationContext } from "vweb-core";
4
3
  export default class MinioDfs extends Dfs<'minio'> {
package/lib/minio.js CHANGED
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  Object.defineProperty(exports, "__esModule", { value: true });
26
36
  const declare_1 = require("./declare");
27
37
  const minio = __importStar(require("minio"));
package/lib/native.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Dfs, NativeConfig } from "./declare";
3
2
  import { VWebApplicationContext } from "vweb-core";
4
3
  export default class NativeDfs extends Dfs<'native'> {
package/lib/native.js CHANGED
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
37
  };
package/lib/obs.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { Dfs, ObsConfig } from "./declare";
2
+ import { VWebApplicationContext } from "vweb-core";
3
+ export default class ObsDfs extends Dfs<'obs'> {
4
+ private readonly client;
5
+ constructor(options: ObsConfig & {
6
+ type: 'obs';
7
+ }, context: VWebApplicationContext);
8
+ startup(): Promise<void>;
9
+ private getKey;
10
+ upload(path: string, buffer: any, options?: any): Promise<string>;
11
+ download(path: string, options?: any): Promise<Buffer>;
12
+ getAccess(path: string, options?: any): Promise<string>;
13
+ }
package/lib/obs.js ADDED
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const declare_1 = require("./declare");
7
+ const esdk_obs_nodejs_1 = __importDefault(require("esdk-obs-nodejs"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ class ObsDfs extends declare_1.Dfs {
10
+ client;
11
+ constructor(options, context) {
12
+ super(options, context);
13
+ this.client = new esdk_obs_nodejs_1.default({
14
+ access_key_id: options.accessKeyId,
15
+ secret_access_key: options.accessKeySecret,
16
+ server: options.endpoint
17
+ });
18
+ }
19
+ async startup() {
20
+ return super.startup();
21
+ }
22
+ getKey(path) {
23
+ if (path.startsWith('/')) {
24
+ path = path.substring(1);
25
+ }
26
+ return path;
27
+ }
28
+ async upload(path, buffer, options = {}) {
29
+ try {
30
+ const Key = this.getKey(path);
31
+ this.timeBegin(`upload ${path}`);
32
+ let payload = { Bucket: this.options.bucket, Key, ...options };
33
+ if (typeof buffer === 'string' && fs_1.default.existsSync(buffer)) {
34
+ payload.SourceFile = buffer;
35
+ }
36
+ else {
37
+ payload.Body = buffer;
38
+ }
39
+ await new Promise((resolve, reject) => {
40
+ this.client.putObject(payload, (err, result) => {
41
+ if (err) {
42
+ return reject(err);
43
+ }
44
+ this.logger.debug(`文件【${Key}】上传完成, Status:${result.CommonMsg.Status} Code: ${result.CommonMsg.Code} Message: ${result.CommonMsg.Message}`);
45
+ resolve(result);
46
+ });
47
+ });
48
+ return Key;
49
+ }
50
+ finally {
51
+ this.timeEnd(`upload ${path}`);
52
+ }
53
+ }
54
+ async download(path, options = {}) {
55
+ try {
56
+ const Key = this.getKey(path);
57
+ this.timeBegin(`download ${path}`);
58
+ return await new Promise((resolve, reject) => {
59
+ this.client.getObject({
60
+ Bucket: this.options.bucket,
61
+ Key,
62
+ SaveAsStream: true,
63
+ ...options
64
+ }, (err, result) => {
65
+ if (err) {
66
+ return reject(err);
67
+ }
68
+ this.logger.debug(`文件【${Key}】下载完成, Status:${result.CommonMsg.Status} Code: ${result.CommonMsg.Code} Message: ${result.CommonMsg.Message}`);
69
+ if (result.CommonMsg.Status < 300 && result.InterfaceResult) {
70
+ console.log(result);
71
+ result.InterfaceResult.Content.on('data', resolve);
72
+ }
73
+ else {
74
+ reject(result.CommonMsg.Status);
75
+ }
76
+ });
77
+ });
78
+ }
79
+ finally {
80
+ this.timeEnd(`download ${path}`);
81
+ }
82
+ }
83
+ async getAccess(path, options = {}) {
84
+ if (this.options.access) {
85
+ return this.options.access + path;
86
+ }
87
+ const Key = this.getKey(path);
88
+ const { SignedUrl } = this.client.createSignedUrlSync({ Method: "GET", Bucket: this.options.bucket, Key, Expires: 86400, ...options });
89
+ return SignedUrl;
90
+ }
91
+ }
92
+ exports.default = ObsDfs;
package/lib/oss.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Dfs, OssConfig } from "./declare";
3
2
  import { VWebApplicationContext } from "vweb-core";
4
3
  export default class OssDfs extends Dfs<'oss'> {
package/lib/sftp.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { Dfs, SftpConfig } from "./declare";
2
+ import { VWebApplicationContext } from "vweb-core";
3
+ export default class SftpDfs extends Dfs<'sftp'> {
4
+ constructor(options: SftpConfig & {
5
+ type: 'sftp';
6
+ }, context: VWebApplicationContext);
7
+ private doSftp;
8
+ private getSavePath;
9
+ upload(remotePath: string, buffer: any): Promise<string>;
10
+ download(remotePath: string): Promise<Buffer>;
11
+ getAccess(path: string, ...args: any[]): Promise<string>;
12
+ }
package/lib/sftp.js ADDED
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const declare_1 = require("./declare");
7
+ const ssh2_sftp_client_1 = __importDefault(require("ssh2-sftp-client"));
8
+ const path_1 = __importDefault(require("path"));
9
+ class SftpDfs extends declare_1.Dfs {
10
+ constructor(options, context) {
11
+ super(options, context);
12
+ this.options.saveDir = this.options.saveDir || '/tmp';
13
+ if (this.options.saveDir.endsWith('/')) {
14
+ this.options.saveDir = this.options.saveDir.substring(0, this.options.saveDir.length - 1);
15
+ }
16
+ }
17
+ async doSftp(apply) {
18
+ const { host, port = 22, username, password } = this.options;
19
+ const client = new ssh2_sftp_client_1.default();
20
+ try {
21
+ this.timeBegin(`sftp execute`);
22
+ this.logger.info(`sftp connect ${host}:${port}`);
23
+ await client.connect({ host, port, username, password });
24
+ this.logger.info(`sftp connected do execute`);
25
+ const returnValue = await apply(client);
26
+ this.logger.info(`sftp execute done ${returnValue}`);
27
+ return returnValue;
28
+ }
29
+ catch (e) {
30
+ this.logger.error(e.messasge, e);
31
+ }
32
+ finally {
33
+ await client.end();
34
+ this.timeEnd(`sftp execute`);
35
+ }
36
+ }
37
+ getSavePath(savePath) {
38
+ if (!savePath.startsWith('/')) {
39
+ throw new Error('savePath must start with /');
40
+ }
41
+ return this.options.saveDir + savePath;
42
+ }
43
+ async upload(remotePath, buffer) {
44
+ return this.doSftp(async (client) => {
45
+ const savePath = this.getSavePath(remotePath);
46
+ const { dir } = path_1.default.parse(savePath);
47
+ if (!await client.exists(dir)) {
48
+ this.logger.info(`mkdir ${dir}`);
49
+ await client.mkdir(dir, true);
50
+ }
51
+ this.logger.info(`upload <<< ${savePath}`);
52
+ await client.put(buffer, savePath);
53
+ const link = await this.getAccess(remotePath);
54
+ this.logger.info(`upload ${remotePath} >>> ${link}`);
55
+ return link;
56
+ });
57
+ }
58
+ async download(remotePath) {
59
+ return this.doSftp(async (client) => {
60
+ this.logger.info(`download ${this.getSavePath(remotePath)}`);
61
+ const content = await client.get(this.getSavePath(remotePath));
62
+ return content;
63
+ });
64
+ }
65
+ async getAccess(path, ...args) {
66
+ const { access } = this.options;
67
+ return access ? access + path : path;
68
+ }
69
+ }
70
+ exports.default = SftpDfs;
@@ -0,0 +1 @@
1
+ {"root":["../src/cos.ts","../src/declare.ts","../src/fastdfs.ts","../src/http.ts","../src/index.ts","../src/mdfs.ts","../src/minio.ts","../src/native.ts","../src/obs.ts","../src/oss.ts","../src/sftp.ts"],"version":"5.8.3"}
package/package.json CHANGED
@@ -1,39 +1,43 @@
1
- {
2
- "name": "dfs-adapter",
3
- "version": "1.0.0",
4
- "description": "oss minio fastdfs native",
5
- "main": "./lib/index",
6
- "files": [
7
- "lib/"
8
- ],
9
- "scripts": {
10
- "test": "node bin/app.js",
11
- "build": "tsc --build tsconfig.json",
12
- "run": "npm run build && node bin/app.js"
13
- },
14
- "repository": {
15
- "type": "git",
16
- "url": "git@codeup.aliyun.com:60b0836844816b8ed2335ed6/vweb/modules/mq-adapter.git"
17
- },
18
- "devDependencies": {
19
- "@babel/cli": "^7.16.0",
20
- "@babel/core": "^7.16.0",
21
- "@babel/plugin-proposal-decorators": "^7.16.4",
22
- "@babel/plugin-syntax-dynamic-import": "^7.8.3",
23
- "@babel/plugin-transform-runtime": "^7.16.4",
24
- "@babel/preset-env": "^7.16.4",
25
- "@babel/register": "^7.16.0",
26
- "@babel/runtime": "^7.16.3",
27
- "@types/ali-oss": "^6.16.3",
28
- "@types/node": "^16.11.12",
29
- "ali-oss": "^6.18.1",
30
- "axios": "^1.6.1",
31
- "babel-plugin-transform-decorators-legacy": "^1.3.5",
32
- "form-data": "^4.0.0",
33
- "minio": "^8.0.0",
34
- "vweb-core": "^3.0.14",
35
- "vweb-mvc": "^1.2.18"
36
- },
37
- "author": "",
38
- "license": "ISC"
39
- }
1
+ {
2
+ "name": "dfs-adapter",
3
+ "version": "1.0.3",
4
+ "description": "oss minio fastdfs native",
5
+ "main": "./lib/index",
6
+ "files": [
7
+ "lib/"
8
+ ],
9
+ "scripts": {
10
+ "test": "node bin/app.js",
11
+ "build": "tsc --build tsconfig.json",
12
+ "run": "npm run build && node bin/app.js"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git@codeup.aliyun.com:60b0836844816b8ed2335ed6/vweb/modules/mq-adapter.git"
17
+ },
18
+ "devDependencies": {
19
+ "@babel/cli": "^7.16.0",
20
+ "@babel/core": "^7.16.0",
21
+ "@babel/plugin-proposal-decorators": "^7.16.4",
22
+ "@babel/plugin-syntax-dynamic-import": "^7.8.3",
23
+ "@babel/plugin-transform-runtime": "^7.16.4",
24
+ "@babel/preset-env": "^7.16.4",
25
+ "@babel/register": "^7.16.0",
26
+ "@babel/runtime": "^7.16.3",
27
+ "@types/ali-oss": "^6.16.3",
28
+ "@types/node": "^16.11.12",
29
+ "@types/ssh2-sftp-client": "^9.0.5",
30
+ "ali-oss": "^6.18.1",
31
+ "axios": "^1.6.1",
32
+ "babel-plugin-transform-decorators-legacy": "^1.3.5",
33
+ "cos-nodejs-sdk-v5": "^2.14.4",
34
+ "esdk-obs-nodejs": "^3.24.3",
35
+ "form-data": "^4.0.0",
36
+ "minio": "^8.0.0",
37
+ "ssh2-sftp-client": "^12.0.1",
38
+ "vweb-core": "^3.0.14",
39
+ "vweb-mvc": "^1.2.18"
40
+ },
41
+ "author": "",
42
+ "license": "ISC"
43
+ }