Skip to content

Commit f68c107

Browse files
authored
Merge pull request #109 from devsapp/fix-userAgent
fix: userAgent function
2 parents e3e4dc1 + cb3b8f0 commit f68c107

23 files changed

Lines changed: 87 additions & 108 deletions

File tree

__tests__/ut/commands/layer_test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ jest.mock('../../../src/utils', () => ({
123123
calculateCRC64: jest.fn(),
124124
getFileSize: jest.fn(),
125125
isAppCenter: jest.fn(),
126+
getUserAgent: jest.fn((userAgent, command) => {
127+
return (
128+
userAgent ||
129+
`Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch};command:${command}`
130+
);
131+
}),
126132
}));
127133

128134
describe('Layer', () => {

__tests__/ut/commands/remove/remove_test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,10 @@ describe('Remove', () => {
139139
it('should execute remove successfully with basic function config', async () => {
140140
await remove.run();
141141

142-
expect(FC).toHaveBeenCalledWith(
143-
'cn-hangzhou',
144-
undefined,
145-
expect.objectContaining({
146-
userAgent: expect.stringContaining('command:remove'),
147-
}),
148-
);
142+
expect(FC).toHaveBeenCalledWith('cn-hangzhou', undefined, {
143+
endpoint: undefined,
144+
userAgent: undefined,
145+
});
149146
expect(mockFcInstance.getFunction).toHaveBeenCalledWith('test-function');
150147
});
151148

__tests__/ut/commands/scaling/index_test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ jest.mock('../../../../src/resources/fc', () => {
2020
jest.mock('../../../../src/utils', () => ({
2121
promptForConfirmOrDetails: jest.fn(),
2222
isAppCenter: jest.fn(),
23+
getUserAgent: jest.fn((userAgent, command) => {
24+
return (
25+
userAgent ||
26+
`Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch};command:${command}`
27+
);
28+
}),
2329
}));
2430

2531
describe('Scaling', () => {

__tests__/ut/commands/session_test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ jest.mock('../../../src/logger', () => ({
1717
jest.mock('../../../src/utils', () => ({
1818
promptForConfirmOrDetails: jest.fn(),
1919
isAppCenter: jest.fn(),
20+
getUserAgent: jest.fn((userAgent, command) => {
21+
return (
22+
userAgent ||
23+
`Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch};command:${command}`
24+
);
25+
}),
2026
}));
2127

2228
describe('Session', () => {

__tests__/ut/commands/version_test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ jest.mock('../../../src/logger', () => {
3030
jest.mock('../../../src/utils', () => ({
3131
promptForConfirmOrDetails: jest.fn(),
3232
isAppCenter: jest.fn(),
33+
getUserAgent: jest.fn((userAgent, command) => {
34+
return (
35+
userAgent ||
36+
`Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch};command:${command}`
37+
);
38+
}),
3339
}));
3440

3541
describe('Version', () => {

src/subCommands/alias/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import _ from 'lodash';
44
import logger from '../../logger';
55
import FC from '../../resources/fc';
66
import commandsHelp from '../../commands-help/alias';
7-
import { isAppCenter, promptForConfirmOrDetails, tableShow } from '../../utils';
7+
import { getUserAgent, promptForConfirmOrDetails, tableShow } from '../../utils';
88

99
const commandsList = Object.keys(commandsHelp.subCommands);
1010

@@ -70,13 +70,10 @@ export default class Alias {
7070
this.versionId = versionId;
7171
this.additionalVersionWeight = additionalVersionWeight;
7272

73-
const function_ai = isAppCenter() ? 'function_ai;' : '';
73+
const userAgent = getUserAgent(inputs.userAgent, 'alias');
7474
this.fcSdk = new FC(this.region, inputs.credential, {
7575
endpoint: inputs.props.endpoint,
76-
userAgent: `${
77-
inputs.userAgent ||
78-
`${function_ai}Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch}`
79-
}command:alias`,
76+
userAgent,
8077
});
8178
}
8279

src/subCommands/build/impl/defaultBuilder.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ from 'lodash';
22
import * as path from 'path';
33

44
import { Builder } from './baseBuilder';
5-
import { runCommand, isAppCenter, isYunXiao } from '../../../utils';
5+
import { runCommand, isAppCenter, isYunXiao, getUserAgent } from '../../../utils';
66
import logger from '../../../logger';
77
import { buildPythonLocalPath } from '../../../default/image';
88
import { parseArgv } from '@serverless-devs/utils';
@@ -112,13 +112,10 @@ export class DefaultBuilder extends Builder {
112112
logger.debug(`${region}`);
113113
checkRegion(region);
114114
const credential = (await this.inputs.getCredential()) as ICredentials;
115-
const function_ai = isAppCenter() ? 'function_ai;' : '';
115+
const userAgent = getUserAgent(this.inputs.userAgent, 'build-publish-layer');
116116
const fcSdk = new FC(region, credential, {
117117
endpoint: this.getProps().endpoint,
118-
userAgent: `${
119-
this.inputs.userAgent ||
120-
`${function_ai}Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch}`
121-
}command:build-publish-layer`,
118+
userAgent,
122119
});
123120
let buildDir: string = this.getBuildDir();
124121
buildDir = path.isAbsolute(buildDir) ? buildDir : path.join(this.baseDir, buildDir);

src/subCommands/concurrency/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IInputs, IRegion, checkRegion } from '../../interface';
44
import logger from '../../logger';
55
import _ from 'lodash';
66
import FC from '../../resources/fc';
7-
import { promptForConfirmOrDetails } from '../../utils';
7+
import { getUserAgent, promptForConfirmOrDetails } from '../../utils';
88

99
const commandsList = Object.keys(commandsHelp.subCommands);
1010

@@ -53,12 +53,10 @@ export default class Concurrency {
5353
this.yes = !!yes;
5454
this.subCommand = subCommand;
5555

56+
const userAgent = getUserAgent(inputs.userAgent, 'concurrency');
5657
this.fcSdk = new FC(this.region, inputs.credential, {
5758
endpoint: inputs.props.endpoint,
58-
userAgent: `${
59-
inputs.userAgent ||
60-
`serverless-devs;Nodejs:${process.version};OS:${process.platform}-${process.arch}`
61-
}command:concurrency`,
59+
userAgent,
6260
});
6361
}
6462

src/subCommands/deploy/impl/base.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import { ICredentials } from '@serverless-devs/component-interface';
22
import { IInputs } from '../../../interface';
33
import FC from '../../../resources/fc';
4-
import { isAppCenter } from '../../../utils';
4+
import { getUserAgent } from '../../../utils';
55

66
export default abstract class Base {
77
readonly fcSdk: FC;
88
needDeploy: boolean | undefined;
99

1010
constructor(readonly inputs: IInputs, needDeploy: boolean | undefined) {
1111
this.needDeploy = needDeploy;
12-
const function_ai = isAppCenter() ? 'function_ai;' : '';
12+
const userAgent = getUserAgent(inputs.userAgent, 'deploy');
1313
this.fcSdk = new FC(inputs.props.region, inputs.credential as ICredentials, {
1414
endpoint: inputs.props.endpoint,
15-
userAgent: `${
16-
inputs.userAgent ||
17-
`${function_ai}Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch}`
18-
}command:deploy`,
15+
userAgent,
1916
});
2017
}
2118

src/subCommands/info/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import FC, { GetApiType } from '../../resources/fc';
66
import logger from '../../logger';
77
import { parseArgv } from '@serverless-devs/utils';
88
import loadComponent from '@serverless-devs/load-component';
9-
import { isAppCenter, transformCustomDomainProps } from '../../utils';
9+
import { getUserAgent, transformCustomDomainProps } from '../../utils';
1010
import { FC3_DOMAIN_COMPONENT_NAME } from '../../constant';
1111

1212
export default class Info {
@@ -38,13 +38,10 @@ export default class Info {
3838
throw new Error('functionName not specified, please specify --function-name');
3939
}
4040
this.triggersName = _.get(inputs, 'props.triggers', []).map((item) => item.triggerName);
41-
const function_ai = isAppCenter() ? 'function_ai;' : '';
41+
const userAgent = getUserAgent(inputs.userAgent, 'info');
4242
this.fcSdk = new FC(this.region, this.inputs.credential as ICredentials, {
4343
endpoint: inputs.props.endpoint,
44-
userAgent: `${
45-
inputs.userAgent ||
46-
`${function_ai}Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch}`
47-
}command:info`,
44+
userAgent,
4845
});
4946
this.getApiType = GetApiType.simple;
5047
}

0 commit comments

Comments
 (0)