@midwayjs/oss 4.0.0-beta.9 → 4.0.0

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/dist/manager.d.ts CHANGED
@@ -1,10 +1,15 @@
1
- import { ServiceFactory } from '@midwayjs/core';
1
+ import { ServiceFactory, MidwayTraceService } from '@midwayjs/core';
2
2
  import * as OSS from 'ali-oss';
3
3
  import type { OSSServiceFactoryReturnType, OSSServiceFactoryCreateClientConfigType } from './interface';
4
4
  export declare class OSSServiceFactory<T extends OSSServiceFactoryReturnType = OSSServiceFactoryReturnType> extends ServiceFactory<T> {
5
5
  ossConfig: OSSServiceFactoryCreateClientConfigType;
6
+ protected traceService: MidwayTraceService;
7
+ protected traceMetaResolver: any;
8
+ protected traceEnabled: any;
9
+ protected traceInjector: any;
6
10
  init(): Promise<void>;
7
11
  createClient(config: OSSServiceFactoryCreateClientConfigType): Promise<T>;
12
+ protected bindTraceContext(client: any): void;
8
13
  getName(): string;
9
14
  }
10
15
  export declare class OSSService implements OSS {
package/dist/manager.js CHANGED
@@ -19,6 +19,10 @@ function checkBucketConfig(config) {
19
19
  }
20
20
  let OSSServiceFactory = class OSSServiceFactory extends core_1.ServiceFactory {
21
21
  ossConfig;
22
+ traceService;
23
+ traceMetaResolver;
24
+ traceEnabled;
25
+ traceInjector;
22
26
  async init() {
23
27
  await this.initClients(this.ossConfig, {
24
28
  concurrent: true,
@@ -30,16 +34,58 @@ let OSSServiceFactory = class OSSServiceFactory extends core_1.ServiceFactory {
30
34
  }
31
35
  if (config.clusters) {
32
36
  config.clusters.forEach(checkBucketConfig);
33
- // @ts-expect-error because this code can return the correct type, but TS still reports an error
34
- return new OSS.ClusterClient(config);
37
+ const client = new OSS.ClusterClient(config);
38
+ this.bindTraceContext(client);
39
+ return client;
35
40
  }
36
41
  if (config.sts === true) {
37
- // @ts-expect-error because this code can return the correct type, but TS still reports an error
38
- return new OSS.STS(config);
42
+ const client = new OSS.STS(config);
43
+ this.bindTraceContext(client);
44
+ return client;
39
45
  }
40
46
  checkBucketConfig(config);
41
- // @ts-expect-error because this code can return the correct type, but TS still reports an error
42
- return new OSS(config);
47
+ const client = new OSS(config);
48
+ this.bindTraceContext(client);
49
+ return client;
50
+ }
51
+ bindTraceContext(client) {
52
+ if (!client || !this.traceService) {
53
+ return;
54
+ }
55
+ const rawRequest = client.request?.bind(client);
56
+ if (!rawRequest) {
57
+ return;
58
+ }
59
+ client.request = async (...args) => {
60
+ const requestMethod = args?.[0]?.method || 'request';
61
+ const rawCarrier = typeof this.traceInjector === 'function'
62
+ ? this.traceInjector({
63
+ request: args?.[0],
64
+ custom: {
65
+ requestMethod: String(requestMethod),
66
+ },
67
+ })
68
+ : {};
69
+ const carrier = rawCarrier && typeof rawCarrier === 'object' ? rawCarrier : {};
70
+ return await this.traceService.runWithExitSpan(`oss.${String(requestMethod).toLowerCase()}`, {
71
+ enable: this.traceEnabled !== false,
72
+ carrier,
73
+ attributes: {
74
+ 'midway.protocol': 'oss',
75
+ 'midway.oss.method': String(requestMethod),
76
+ },
77
+ meta: this.traceMetaResolver,
78
+ metaArgs: {
79
+ carrier,
80
+ request: args?.[0],
81
+ custom: {
82
+ requestMethod: String(requestMethod),
83
+ },
84
+ },
85
+ }, async () => {
86
+ return await rawRequest(...args);
87
+ });
88
+ };
43
89
  }
44
90
  getName() {
45
91
  return 'oss';
@@ -50,6 +96,22 @@ __decorate([
50
96
  (0, core_1.Config)('oss'),
51
97
  __metadata("design:type", Object)
52
98
  ], OSSServiceFactory.prototype, "ossConfig", void 0);
99
+ __decorate([
100
+ (0, core_1.Inject)(),
101
+ __metadata("design:type", core_1.MidwayTraceService)
102
+ ], OSSServiceFactory.prototype, "traceService", void 0);
103
+ __decorate([
104
+ (0, core_1.Config)('oss.tracing.meta'),
105
+ __metadata("design:type", Object)
106
+ ], OSSServiceFactory.prototype, "traceMetaResolver", void 0);
107
+ __decorate([
108
+ (0, core_1.Config)('oss.tracing.enable'),
109
+ __metadata("design:type", Object)
110
+ ], OSSServiceFactory.prototype, "traceEnabled", void 0);
111
+ __decorate([
112
+ (0, core_1.Config)('oss.tracing.injector'),
113
+ __metadata("design:type", Object)
114
+ ], OSSServiceFactory.prototype, "traceInjector", void 0);
53
115
  __decorate([
54
116
  (0, core_1.Init)(),
55
117
  __metadata("design:type", Function),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/oss",
3
3
  "description": "midway oss component",
4
- "version": "4.0.0-beta.9",
4
+ "version": "4.0.0",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -10,12 +10,12 @@
10
10
  "index.d.ts"
11
11
  ],
12
12
  "devDependencies": {
13
- "@midwayjs/core": "^4.0.0-beta.9",
14
- "@midwayjs/mock": "^4.0.0-beta.9",
13
+ "@midwayjs/core": "^4.0.0",
14
+ "@midwayjs/mock": "^4.0.0",
15
15
  "dotenv": "16.4.7"
16
16
  },
17
17
  "dependencies": {
18
- "@types/ali-oss": "6.16.13",
18
+ "@types/ali-oss": "6.23.3",
19
19
  "ali-oss": "6.23.0"
20
20
  },
21
21
  "keywords": [
@@ -38,5 +38,5 @@
38
38
  "type": "git",
39
39
  "url": "https://github.com/midwayjs/midway.git"
40
40
  },
41
- "gitHead": "771e83974ef9f3d78c296e4ee0c8c68db44f6b31"
41
+ "gitHead": "014f32c23ebc1d5ac21777c76be2fd373ce992d8"
42
42
  }