Skip to content

Commit e345fff

Browse files
committed
pr feedback
1 parent f2b0b40 commit e345fff

File tree

8 files changed

+38
-71
lines changed

8 files changed

+38
-71
lines changed

packages/wrangler/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
"selfsigned": "^2.0.1",
8787
"source-map": "^0.6.1",
8888
"unenv": "npm:[email protected]",
89-
"which-pm-runs": "^1.1.0",
9089
"workerd": "1.20241106.1",
9190
"xxhash-wasm": "^1.0.1"
9291
},

packages/wrangler/src/__tests__/metrics.test.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe("metrics", () => {
6161
vi.mocked(getOS).mockReturnValue("foo:bar");
6262
vi.mocked(getWranglerVersion).mockReturnValue("1.2.3");
6363
vi.mocked(getOSVersion).mockReturnValue("mock os version");
64-
vi.mocked(getNodeVersion).mockReturnValue("1.1.1");
64+
vi.mocked(getNodeVersion).mockReturnValue(1);
6565
vi.mocked(getPlatform).mockReturnValue("mock platform");
6666
vi.mocked(getConfigFileType).mockReturnValue("toml");
6767
vi.useFakeTimers({
@@ -83,19 +83,7 @@ describe("metrics", () => {
8383
describe("sendEvent()", () => {
8484
it("should send a request to the default URL", async () => {
8585
const requests = mockMetricRequest();
86-
// {
87-
// event: "some-event",
88-
// properties: {
89-
// category: "Workers",
90-
// wranglerVersion: "1.2.3",
91-
// os: "foo:bar",
92-
// a: 1,
93-
// b: 2,
94-
// },
95-
// },
96-
// {
97-
// "Sparrow-Source-Key": "MOCK_KEY",
98-
// }
86+
9987
const dispatcher = await getMetricsDispatcher({
10088
sendMetrics: true,
10189
});
@@ -192,7 +180,7 @@ describe("metrics", () => {
192180
wranglerVersion: "1.2.3",
193181
osPlatform: "mock platform",
194182
osVersion: "mock os version",
195-
nodeVersion: "1.1.1",
183+
nodeVersion: 1,
196184
isFirstUsage: false,
197185
configFileType: "toml",
198186
isCI: false,
@@ -293,7 +281,7 @@ describe("metrics", () => {
293281
msw.use(
294282
http.post<Record<string, never>, { params: string | undefined }>(
295283
`*/1/indexes/developers-cloudflare2/query`,
296-
async ({}) => {
284+
async () => {
297285
vi.advanceTimersByTime(6000);
298286
return HttpResponse.error();
299287
},

packages/wrangler/src/config/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type {
2929
RawEnvironment,
3030
} from "./environment";
3131

32-
function configFormat(
32+
export function configFormat(
3333
configPath: string | undefined
3434
): "jsonc" | "toml" | "none" {
3535
if (configPath?.endsWith("toml")) {
@@ -91,9 +91,8 @@ export function readConfig(
9191
requirePagesConfig?: boolean,
9292
hideWarnings: boolean = false
9393
): Config {
94-
const { rawConfig, configPath: updateConfigPath } = parseConfig(
94+
const { rawConfig, configPath: updateConfigPath } = readRawConfig(
9595
configPath,
96-
args,
9796
requirePagesConfig
9897
);
9998

@@ -153,10 +152,8 @@ export function readConfig(
153152
return config;
154153
}
155154

156-
export const parseConfig = (
155+
export const readRawConfig = (
157156
configPath: string | undefined,
158-
// Include command specific args as well as the wrangler global flags
159-
args: ReadConfigCommandArgs,
160157
requirePagesConfig?: boolean
161158
) => {
162159
let rawConfig: RawConfig = {};

packages/wrangler/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
configFileName,
1313
formatConfigSnippet,
1414
loadDotEnv,
15-
parseConfig,
15+
readRawConfig,
1616
} from "./config";
1717
import { demandSingleValue } from "./core";
1818
import { CommandRegistry } from "./core/CommandRegistry";
@@ -1064,7 +1064,7 @@ export async function main(argv: string[]): Promise<void> {
10641064
// key to fetch) or flags
10651065

10661066
try {
1067-
const { rawConfig, configPath } = parseConfig(args.config, args, false);
1067+
const { rawConfig, configPath } = readRawConfig(args.config, false);
10681068
dispatcher = getMetricsDispatcher({
10691069
sendMetrics: rawConfig.send_metrics,
10701070
configPath,

packages/wrangler/src/metrics/helpers.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import os from "node:os";
2-
import whichPmRuns from "which-pm-runs";
32
import { version as wranglerVersion } from "../../package.json";
3+
import { configFormat } from "../config";
4+
import {
5+
getPackageManager as _getPackageManager,
6+
getPackageManagerName,
7+
} from "../package-manager";
48

59
export function getWranglerVersion() {
610
return wranglerVersion;
711
}
812

9-
export function getPackageManager() {
10-
return whichPmRuns()?.name;
13+
export async function getPackageManager() {
14+
const packageManager = await _getPackageManager(process.cwd());
15+
return getPackageManagerName(packageManager);
1116
}
1217

1318
// used by "new" metrics
@@ -36,22 +41,10 @@ export function getOSVersion() {
3641
}
3742

3843
export function getNodeVersion() {
39-
return process.version;
44+
const nodeVersion = process.versions.node;
45+
return parseInt(nodeVersion.split(".")[0]);
4046
}
4147

4248
export function getConfigFileType(configPath: string | undefined) {
43-
if (configPath === undefined) {
44-
return "none";
45-
}
46-
if (configPath.endsWith(".toml")) {
47-
return "toml";
48-
}
49-
if (configPath.endsWith(".json")) {
50-
return "json";
51-
}
52-
if (configPath.endsWith(".jsonc")) {
53-
return "jsonc";
54-
}
55-
/** shouldn't ever get here */
56-
return "invalid";
49+
return configFormat(configPath);
5750
}

packages/wrangler/src/metrics/metrics-dispatcher.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import chalk from "chalk";
22
import { fetch } from "undici";
3-
import _isInteractive from "../is-interactive";
3+
import isInteractive from "../is-interactive";
44
import { logger } from "../logger";
55
import { CI } from "./../is-ci";
66
import {
@@ -28,16 +28,9 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
2828
const SPARROW_SOURCE_KEY = process.env.SPARROW_SOURCE_KEY ?? "";
2929
const requests: Array<Promise<void>> = [];
3030
const wranglerVersion = getWranglerVersion();
31-
const osPlatform = getPlatform();
32-
const osVersion = getOSVersion();
33-
const nodeVersion = getNodeVersion();
34-
const packageManager = getPackageManager();
35-
const isFirstUsage = readMetricsConfig().permission === undefined;
36-
const isCI = CI.isCI();
37-
const isInteractive = _isInteractive();
3831
const amplitude_session_id = Date.now();
39-
const configFileType = getConfigFileType(options.configPath);
4032
let amplitude_event_id = 0;
33+
4134
/** We redact strings in arg values, unless they are named here */
4235
const allowList = {
4336
// applies to all commands
@@ -95,20 +88,20 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
9588
amplitude_session_id,
9689
amplitude_event_id: amplitude_event_id++,
9790
wranglerVersion,
98-
osPlatform,
99-
osVersion,
100-
nodeVersion,
101-
packageManager,
102-
isFirstUsage,
103-
configFileType,
104-
isCI,
105-
isInteractive,
91+
osPlatform: getPlatform(),
92+
osVersion: getOSVersion(),
93+
nodeVersion: getNodeVersion(),
94+
packageManager: await getPackageManager(),
95+
isFirstUsage: readMetricsConfig().permission === undefined,
96+
configFileType: getConfigFileType(options.configPath),
97+
isCI: CI.isCI(),
98+
isInteractive: isInteractive(),
10699
argsUsed,
107100
argsCombination,
108101
};
109-
// we redact all args unless they are in the allowList
110-
const allowedKeys = getAllowedKeys(allowList, properties.command ?? "");
111-
properties.args = redactArgValues(properties.args ?? {}, allowedKeys);
102+
// get the args where we don't want to redact their values
103+
const allowedArgs = getAllowedArgs(allowList, properties.command ?? "");
104+
properties.args = redactArgValues(properties.args ?? {}, allowedArgs);
112105
await dispatch({
113106
name,
114107
properties: {
@@ -154,8 +147,8 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
154147

155148
logger.debug(`Metrics dispatcher: Posting data ${JSON.stringify(body)}`);
156149

157-
// Do not await this fetch call.
158-
// Just fire-and-forget, otherwise we might slow down the rest of Wrangler.
150+
// Don't await fetch but make sure requests are resolved (with a timeout)
151+
// before exiting Wrangler
159152
const request = fetch(`${SPARROW_URL}/api/v1/event`, {
160153
method: "POST",
161154
headers: {
@@ -235,7 +228,7 @@ const sanitiseUserInput = (argsWithValues: Record<string, unknown>) => {
235228
return result;
236229
};
237230

238-
const getAllowedKeys = (
231+
const getAllowedArgs = (
239232
allowList: Record<string, string[]> & { "*": string[] },
240233
key: string
241234
) => {

packages/wrangler/src/metrics/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export type CommonEventProperties = {
1414
*/
1515
packageManager: string | undefined;
1616
/**
17-
* The version of node that the Wrangler client is running on.
17+
* The major version of node that the Wrangler client is running on.
1818
*/
19-
nodeVersion: string;
19+
nodeVersion: number;
2020
/**
2121
* Whether this is the first time the user has used the wrangler client.
2222
*/

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)