wrangler 0.0.0-e2a26a0 → 0.0.0-e3cab74
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/package.json +16 -7
- package/pages/functions/filepath-routing.test.ts +112 -36
- package/pages/functions/filepath-routing.ts +66 -119
- package/pages/functions/routes.ts +8 -17
- package/pages/functions/template-worker.ts +3 -9
- package/src/__tests__/configuration.test.ts +2185 -0
- package/src/__tests__/dev.test.tsx +48 -6
- package/src/__tests__/guess-worker-format.test.ts +66 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +6 -6
- package/src/__tests__/helpers/mock-bin.ts +4 -1
- package/src/__tests__/helpers/mock-cfetch.ts +2 -2
- package/src/__tests__/helpers/mock-console.ts +11 -5
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-wrangler.ts +5 -3
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/index.test.ts +343 -20
- package/src/__tests__/jest.setup.ts +21 -1
- package/src/__tests__/kv.test.ts +82 -80
- package/src/__tests__/package-manager.test.ts +2 -2
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +1428 -244
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/secret.test.ts +176 -60
- package/src/__tests__/sentry.test.ts +146 -0
- package/src/__tests__/tail.test.ts +564 -0
- package/src/api/form_data.ts +106 -23
- package/src/api/preview.ts +32 -13
- package/src/api/worker.ts +49 -18
- package/src/bundle.ts +133 -0
- package/src/cfetch/index.ts +8 -6
- package/src/cfetch/internal.ts +49 -17
- package/src/cli.ts +10 -0
- package/src/config/README.md +107 -0
- package/src/config/config.ts +265 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +305 -0
- package/src/config/index.ts +52 -0
- package/src/config/validation-helpers.ts +370 -0
- package/src/config/validation.ts +1017 -0
- package/src/dev.tsx +338 -211
- package/src/entry.ts +57 -0
- package/src/environment-variables.ts +33 -0
- package/src/guess-worker-format.ts +68 -0
- package/src/index.tsx +860 -634
- package/src/inspect.ts +16 -3
- package/src/kv.tsx +20 -29
- package/src/module-collection.ts +141 -34
- package/src/open-in-browser.ts +13 -0
- package/src/package-manager.ts +21 -15
- package/src/pages.tsx +9 -10
- package/src/parse.ts +183 -0
- package/src/proxy.ts +11 -5
- package/src/publish.ts +272 -314
- package/src/r2.ts +50 -0
- package/src/reporting.ts +115 -0
- package/src/sites.tsx +16 -15
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +62 -0
- package/src/user.tsx +74 -44
- package/templates/new-worker.js +1 -1
- package/templates/new-worker.ts +1 -1
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +51308 -36440
- package/wrangler-dist/cli.js.map +3 -3
- package/src/config.ts +0 -641
- package/src/tail.tsx +0 -73
package/src/r2.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { fetchResult } from "./cfetch";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Information about a bucket, returned from `listR2Buckets()`.
|
|
5
|
+
*/
|
|
6
|
+
export interface R2BucketInfo {
|
|
7
|
+
name: string;
|
|
8
|
+
creation_date: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Fetch a list of all the buckets under the given `accountId`.
|
|
13
|
+
*/
|
|
14
|
+
export async function listR2Buckets(
|
|
15
|
+
accountId: string
|
|
16
|
+
): Promise<R2BucketInfo[]> {
|
|
17
|
+
const results = await fetchResult<{
|
|
18
|
+
buckets: R2BucketInfo[];
|
|
19
|
+
}>(`/accounts/${accountId}/r2/buckets`);
|
|
20
|
+
return results.buckets;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Create a bucket with the given `bucketName` within the account given by `accountId`.
|
|
25
|
+
*
|
|
26
|
+
* A 400 is returned if the account already owns a bucket with this name.
|
|
27
|
+
* A bucket must be explicitly deleted to be replaced.
|
|
28
|
+
*/
|
|
29
|
+
export async function createR2Bucket(
|
|
30
|
+
accountId: string,
|
|
31
|
+
bucketName: string
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
return await fetchResult<void>(
|
|
34
|
+
`/accounts/${accountId}/r2/buckets/${bucketName}`,
|
|
35
|
+
{ method: "PUT" }
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Delete a bucket with the given name
|
|
41
|
+
*/
|
|
42
|
+
export async function deleteR2Bucket(
|
|
43
|
+
accountId: string,
|
|
44
|
+
bucketName: string
|
|
45
|
+
): Promise<void> {
|
|
46
|
+
return await fetchResult<void>(
|
|
47
|
+
`/accounts/${accountId}/r2/buckets/${bucketName}`,
|
|
48
|
+
{ method: "DELETE" }
|
|
49
|
+
);
|
|
50
|
+
}
|
package/src/reporting.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { appendFile, readFile } from "fs/promises";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "path/posix";
|
|
5
|
+
import TOML from "@iarna/toml";
|
|
6
|
+
import { RewriteFrames } from "@sentry/integrations";
|
|
7
|
+
import {
|
|
8
|
+
captureException,
|
|
9
|
+
getCurrentHub,
|
|
10
|
+
startTransaction,
|
|
11
|
+
init,
|
|
12
|
+
Integrations,
|
|
13
|
+
setContext,
|
|
14
|
+
} from "@sentry/node";
|
|
15
|
+
import { execaSync } from "execa";
|
|
16
|
+
import prompts from "prompts";
|
|
17
|
+
import * as pkj from "../package.json";
|
|
18
|
+
|
|
19
|
+
export function initReporting() {
|
|
20
|
+
init({
|
|
21
|
+
release: `${pkj.name}@${pkj.version}`,
|
|
22
|
+
initialScope: {
|
|
23
|
+
tags: { [pkj.name]: pkj.version },
|
|
24
|
+
},
|
|
25
|
+
dsn: "https://5089b76bf8a64a9c949bf5c2b5e8003c@o51786.ingest.sentry.io/6190959",
|
|
26
|
+
tracesSampleRate: 1.0,
|
|
27
|
+
integrations: [
|
|
28
|
+
new RewriteFrames({
|
|
29
|
+
root: "",
|
|
30
|
+
prefix: "/",
|
|
31
|
+
}),
|
|
32
|
+
new Integrations.Http({ tracing: true }),
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
setContext("System Information", {
|
|
37
|
+
OS: process.platform,
|
|
38
|
+
node: process.version,
|
|
39
|
+
npm: execaSync("npm", ["--version"]).stdout,
|
|
40
|
+
wrangler: pkj.version,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function appendReportingDecision(userInput: "true" | "false") {
|
|
45
|
+
const homePath = path.join(os.homedir(), ".wrangler/config/");
|
|
46
|
+
fs.mkdirSync(homePath, { recursive: true });
|
|
47
|
+
await appendFile(
|
|
48
|
+
path.join(homePath, "reporting.toml"),
|
|
49
|
+
`error_tracking_opt = ${userInput} # Sentry \nerror_tracking_opt_date = ${new Date().toISOString()} # Sentry Date Decision \n`,
|
|
50
|
+
{ encoding: "utf-8" }
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function exceptionTransaction(error: Error, origin = "") {
|
|
55
|
+
const transaction = startTransaction({
|
|
56
|
+
op: origin,
|
|
57
|
+
name: error.name,
|
|
58
|
+
});
|
|
59
|
+
captureException(error);
|
|
60
|
+
transaction.finish();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function reportError(err: Error, origin = "") {
|
|
64
|
+
if (!process.stdout.isTTY) return await appendReportingDecision("false");
|
|
65
|
+
|
|
66
|
+
const errorTrackingOpt = await reportingPermission();
|
|
67
|
+
|
|
68
|
+
if (errorTrackingOpt === undefined) {
|
|
69
|
+
const userInput = await prompts.prompt({
|
|
70
|
+
type: "select",
|
|
71
|
+
name: "sentryDecision",
|
|
72
|
+
message: "Would you like to submit a report when an error occurs?",
|
|
73
|
+
choices: [
|
|
74
|
+
{ title: "Always", value: true },
|
|
75
|
+
{ title: "Yes", value: "once" },
|
|
76
|
+
{ title: "No", value: false },
|
|
77
|
+
],
|
|
78
|
+
initial: 2,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (userInput.sentryDecision === "once") {
|
|
82
|
+
exceptionTransaction(err, origin);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
userInput.sentryDecision
|
|
87
|
+
? await appendReportingDecision("true")
|
|
88
|
+
: await appendReportingDecision("false");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!errorTrackingOpt) {
|
|
92
|
+
const sentryClient = getCurrentHub().getClient();
|
|
93
|
+
if (sentryClient !== undefined) sentryClient.getOptions().enabled = false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
exceptionTransaction(err, origin);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function reportingPermission() {
|
|
100
|
+
if (
|
|
101
|
+
!fs.existsSync(path.join(os.homedir(), ".wrangler/config/reporting.toml"))
|
|
102
|
+
)
|
|
103
|
+
return undefined;
|
|
104
|
+
|
|
105
|
+
const reportingTOML = TOML.parse(
|
|
106
|
+
await readFile(path.join(os.homedir(), ".wrangler/config/reporting.toml"), {
|
|
107
|
+
encoding: "utf-8",
|
|
108
|
+
})
|
|
109
|
+
);
|
|
110
|
+
const { error_tracking_opt } = reportingTOML as {
|
|
111
|
+
error_tracking_opt: string | undefined;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
return error_tracking_opt;
|
|
115
|
+
}
|
package/src/sites.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
import ignore from "ignore";
|
|
4
|
-
import
|
|
4
|
+
import xxhash from "xxhash-wasm";
|
|
5
5
|
import {
|
|
6
6
|
createNamespace,
|
|
7
7
|
listNamespaceKeys,
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
putBulkKeyValue,
|
|
10
10
|
} from "./kv";
|
|
11
11
|
import type { Config } from "./config";
|
|
12
|
+
import type { XXHashAPI } from "xxhash-wasm";
|
|
12
13
|
|
|
13
14
|
/** Paths to always ignore. */
|
|
14
15
|
const ALWAYS_IGNORE = ["node_modules"];
|
|
@@ -46,11 +47,8 @@ async function* getFilesInFolder(dirPath: string): AsyncIterable<string> {
|
|
|
46
47
|
* the most important thing here is to detect changes of a single file to invalidate the cache and
|
|
47
48
|
* it's impossible to serve two different files with the same name
|
|
48
49
|
*/
|
|
49
|
-
function hashFileContent(content: string): string {
|
|
50
|
-
|
|
51
|
-
hasher.update(Buffer.from(content));
|
|
52
|
-
const hash = hasher.digest();
|
|
53
|
-
return hash.toString("hex").substring(0, 10);
|
|
50
|
+
function hashFileContent(hasher: XXHashAPI, content: string): string {
|
|
51
|
+
return hasher.h64ToString(content).substring(0, 10);
|
|
54
52
|
}
|
|
55
53
|
|
|
56
54
|
/**
|
|
@@ -59,11 +57,15 @@ function hashFileContent(content: string): string {
|
|
|
59
57
|
* The key will change if the file path or content of the asset changes.
|
|
60
58
|
* The algorithm used here matches that of Wrangler 1.
|
|
61
59
|
*/
|
|
62
|
-
function hashAsset(
|
|
60
|
+
function hashAsset(
|
|
61
|
+
hasher: XXHashAPI,
|
|
62
|
+
filePath: string,
|
|
63
|
+
content: string
|
|
64
|
+
): string {
|
|
63
65
|
const extName = path.extname(filePath) || "";
|
|
64
66
|
const baseName = path.basename(filePath, extName);
|
|
65
67
|
const directory = path.dirname(filePath);
|
|
66
|
-
const hash = hashFileContent(content);
|
|
68
|
+
const hash = hashFileContent(hasher, content);
|
|
67
69
|
return urlSafe(path.join(directory, `${baseName}.${hash}${extName}`));
|
|
68
70
|
}
|
|
69
71
|
|
|
@@ -97,7 +99,6 @@ async function createKVNamespaceIfNotAlreadyExisting(
|
|
|
97
99
|
* @param scriptName the name of the worker whose assets we are uploading.
|
|
98
100
|
* @param siteAssets an objects describing what assets to upload, or undefined if there are no assets to upload.
|
|
99
101
|
* @param preview if true then upload to a "preview" KV namespace.
|
|
100
|
-
* @param _env (not implemented).
|
|
101
102
|
* @returns a promise for an object mapping the relative paths of the assets to the key of that
|
|
102
103
|
* asset in the KV namespace.
|
|
103
104
|
*/
|
|
@@ -105,8 +106,7 @@ export async function syncAssets(
|
|
|
105
106
|
accountId: string,
|
|
106
107
|
scriptName: string,
|
|
107
108
|
siteAssets: AssetPaths | undefined,
|
|
108
|
-
preview: boolean
|
|
109
|
-
env: string | undefined
|
|
109
|
+
preview: boolean
|
|
110
110
|
): Promise<{
|
|
111
111
|
manifest: { [filePath: string]: string } | undefined;
|
|
112
112
|
namespace: string | undefined;
|
|
@@ -115,7 +115,7 @@ export async function syncAssets(
|
|
|
115
115
|
return { manifest: undefined, namespace: undefined };
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
const title = `__${scriptName}
|
|
118
|
+
const title = `__${scriptName}-workers_sites_assets${
|
|
119
119
|
preview ? "_preview" : ""
|
|
120
120
|
}`;
|
|
121
121
|
const { id: namespace } = await createKVNamespaceIfNotAlreadyExisting(
|
|
@@ -127,7 +127,7 @@ export async function syncAssets(
|
|
|
127
127
|
const result = await listNamespaceKeys(accountId, namespace);
|
|
128
128
|
const keys = new Set(result.map((x) => x.name));
|
|
129
129
|
|
|
130
|
-
const manifest = {};
|
|
130
|
+
const manifest: Record<string, string> = {};
|
|
131
131
|
const upload: {
|
|
132
132
|
key: string;
|
|
133
133
|
value: string;
|
|
@@ -136,6 +136,8 @@ export async function syncAssets(
|
|
|
136
136
|
|
|
137
137
|
const include = createPatternMatcher(siteAssets.includePatterns, false);
|
|
138
138
|
const exclude = createPatternMatcher(siteAssets.excludePatterns, true);
|
|
139
|
+
const hasher = await xxhash();
|
|
140
|
+
|
|
139
141
|
// TODO: this can be more efficient by parallelising
|
|
140
142
|
for await (const file of getFilesInFolder(siteAssets.baseDirectory)) {
|
|
141
143
|
if (!include(file)) {
|
|
@@ -149,7 +151,7 @@ export async function syncAssets(
|
|
|
149
151
|
console.log(`reading ${file}...`);
|
|
150
152
|
const content = await readFile(file, "base64");
|
|
151
153
|
|
|
152
|
-
const assetKey = hashAsset(file, content);
|
|
154
|
+
const assetKey = await hashAsset(hasher, file, content);
|
|
153
155
|
validateAssetKey(assetKey);
|
|
154
156
|
|
|
155
157
|
// now put each of the files into kv
|
|
@@ -222,7 +224,6 @@ export interface AssetPaths {
|
|
|
222
224
|
* Uses the args (passed from the command line) if available,
|
|
223
225
|
* falling back to those defined in the config.
|
|
224
226
|
*
|
|
225
|
-
* // TODO: Support for environments
|
|
226
227
|
*/
|
|
227
228
|
export function getAssetPaths(
|
|
228
229
|
config: Config,
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When tailing logs from a worker, oftentimes you don't want to see _every
|
|
3
|
+
* single event_. That's where filters come in. We can send a set of filters
|
|
4
|
+
* to the tail worker, and it will pre-filter any logs for us so that we
|
|
5
|
+
* only recieve the ones we care about.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* These are the filters we accept in the CLI. They
|
|
10
|
+
* were copied directly from wrangler 1 in order to
|
|
11
|
+
* maintain compatability, so they aren't actually the exact
|
|
12
|
+
* filters we need to send up to the tail worker. They generally map 1:1,
|
|
13
|
+
* but often require some transformation or
|
|
14
|
+
* renaming to match what it expects.
|
|
15
|
+
*
|
|
16
|
+
* For a full description of each filter, either check the
|
|
17
|
+
* CLI description or see the documentation for `ApiFilter`.
|
|
18
|
+
*/
|
|
19
|
+
export type TailCLIFilters = {
|
|
20
|
+
status?: ("ok" | "error" | "canceled")[];
|
|
21
|
+
header?: string;
|
|
22
|
+
method?: string[];
|
|
23
|
+
search?: string;
|
|
24
|
+
samplingRate?: number;
|
|
25
|
+
clientIp?: string[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* These are the filters we send to the tail worker. We
|
|
30
|
+
* actually send a list of filters (an array of objects),
|
|
31
|
+
* so rather than having a single TailAPIFilters type,
|
|
32
|
+
* each kind of filter gets its own type and we define
|
|
33
|
+
* TailAPIFilter to be the union of those types.
|
|
34
|
+
*/
|
|
35
|
+
export type TailAPIFilter =
|
|
36
|
+
| SamplingRateFilter
|
|
37
|
+
| OutcomeFilter
|
|
38
|
+
| MethodFilter
|
|
39
|
+
| HeaderFilter
|
|
40
|
+
| ClientIPFilter
|
|
41
|
+
| QueryFilter;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Filters logs based on a given sampling rate.
|
|
45
|
+
* For example, a `sampling_rate` of 0.25 will let one-quarter of the
|
|
46
|
+
* logs through.
|
|
47
|
+
*/
|
|
48
|
+
type SamplingRateFilter = {
|
|
49
|
+
sampling_rate: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Filters logs based on the outcome of the worker's event handler.
|
|
54
|
+
*/
|
|
55
|
+
type OutcomeFilter = {
|
|
56
|
+
outcome: Outcome[];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* There are five possible outcomes we can get, three of which
|
|
61
|
+
* (exception, exceededCpu, and unknown) are considered errors
|
|
62
|
+
*/
|
|
63
|
+
export type Outcome =
|
|
64
|
+
| "ok"
|
|
65
|
+
| "canceled"
|
|
66
|
+
| "exception"
|
|
67
|
+
| "exceededCpu"
|
|
68
|
+
| "unknown";
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Filters logs based on the HTTP method used for the request
|
|
72
|
+
* that triggered the worker.
|
|
73
|
+
*/
|
|
74
|
+
type MethodFilter = {
|
|
75
|
+
method: string[];
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Filters logs based on an HTTP header on the request that
|
|
80
|
+
* triggered the worker.
|
|
81
|
+
*/
|
|
82
|
+
type HeaderFilter = {
|
|
83
|
+
header: {
|
|
84
|
+
/**
|
|
85
|
+
* Filters on the header "key", e.g. "X-CLOUDFLARE-HEADER"
|
|
86
|
+
* or "X-CUSTOM-HEADER"
|
|
87
|
+
*/
|
|
88
|
+
key: string;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Filters on the header "value", e.g. if this is set to
|
|
92
|
+
* "filter-for-me" and the "key" is "X-SHOULD-LOG", only
|
|
93
|
+
* events triggered by requests with the header
|
|
94
|
+
* "X-SHOULD-LOG:filter-for-me" will be logged.
|
|
95
|
+
*/
|
|
96
|
+
query?: string;
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Filters on the IP address the request came from that triggered
|
|
102
|
+
* the worker. A value of "self" will be replaced with the IP
|
|
103
|
+
* address that is running `wrangler tail`
|
|
104
|
+
*/
|
|
105
|
+
type ClientIPFilter = {
|
|
106
|
+
client_ip: string[];
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Filters logs by a query string. This means only logs that
|
|
111
|
+
* contain the given string will be sent to wrangler, and any
|
|
112
|
+
* that don't will be discarded by the tail worker.
|
|
113
|
+
*/
|
|
114
|
+
type QueryFilter = {
|
|
115
|
+
query: string;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The full message we send to the tail worker includes our
|
|
120
|
+
* filters and a debug flag.
|
|
121
|
+
*/
|
|
122
|
+
export type TailFilterMessage = {
|
|
123
|
+
filters: TailAPIFilter[];
|
|
124
|
+
debug: boolean;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Translate the flags passed in via a CLI invokation of wrangler
|
|
129
|
+
* into a message that we can send to the tail worker.
|
|
130
|
+
*
|
|
131
|
+
* @param cliFilters An object containing all the filters passed in from the CLI
|
|
132
|
+
* @param debug Whether or not we should be in debug mode
|
|
133
|
+
* @returns A filter message ready to be sent to the tail worker
|
|
134
|
+
*/
|
|
135
|
+
export function translateCLICommandToFilterMessage(
|
|
136
|
+
cliFilters: TailCLIFilters,
|
|
137
|
+
debug: boolean
|
|
138
|
+
): TailFilterMessage {
|
|
139
|
+
const apiFilters: TailAPIFilter[] = [];
|
|
140
|
+
|
|
141
|
+
if (cliFilters.samplingRate) {
|
|
142
|
+
apiFilters.push(parseSamplingRate(cliFilters.samplingRate));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (cliFilters.status) {
|
|
146
|
+
apiFilters.push(parseOutcome(cliFilters.status));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (cliFilters.method) {
|
|
150
|
+
apiFilters.push(parseMethod(cliFilters.method));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (cliFilters.header) {
|
|
154
|
+
apiFilters.push(parseHeader(cliFilters.header));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (cliFilters.clientIp) {
|
|
158
|
+
apiFilters.push(parseIP(cliFilters.clientIp));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (cliFilters.search) {
|
|
162
|
+
apiFilters.push(parseQuery(cliFilters.search));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
filters: apiFilters,
|
|
167
|
+
debug,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Parse the sampling rate passed in via command line
|
|
173
|
+
*
|
|
174
|
+
* @param sampling_rate the sampling rate passed in via CLI
|
|
175
|
+
* @throws an Error if the rate doesn't make sense
|
|
176
|
+
* @returns a SamplingRateFilter for use with the API
|
|
177
|
+
*/
|
|
178
|
+
function parseSamplingRate(sampling_rate: number): SamplingRateFilter {
|
|
179
|
+
if (sampling_rate <= 0 || sampling_rate >= 1) {
|
|
180
|
+
throw new Error(
|
|
181
|
+
"A sampling rate must be between 0 and 1 in order to have any effect.\nFor example, a sampling rate of 0.25 means 25% of events will be logged."
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return { sampling_rate };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Translate from CLI "status"es to API "outcome"s, including
|
|
190
|
+
* broadening "error" into "exception", "exceededCpu", and "unknown".
|
|
191
|
+
*
|
|
192
|
+
* @param statuses statuses passed in via CLI
|
|
193
|
+
* @returns an OutcomeFilter for use with the API
|
|
194
|
+
*/
|
|
195
|
+
function parseOutcome(
|
|
196
|
+
statuses: ("ok" | "error" | "canceled")[]
|
|
197
|
+
): OutcomeFilter {
|
|
198
|
+
const outcomes = new Set<Outcome>();
|
|
199
|
+
|
|
200
|
+
for (const status of statuses) {
|
|
201
|
+
switch (status) {
|
|
202
|
+
case "ok":
|
|
203
|
+
outcomes.add("ok");
|
|
204
|
+
break;
|
|
205
|
+
|
|
206
|
+
case "canceled":
|
|
207
|
+
outcomes.add("canceled");
|
|
208
|
+
break;
|
|
209
|
+
|
|
210
|
+
case "error":
|
|
211
|
+
outcomes.add("exception");
|
|
212
|
+
outcomes.add("exceededCpu");
|
|
213
|
+
outcomes.add("unknown");
|
|
214
|
+
break;
|
|
215
|
+
|
|
216
|
+
default:
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
outcome: Array.from(outcomes),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* We just send silly methods through to the API anyway, since they don't
|
|
228
|
+
* cause any harm.
|
|
229
|
+
*
|
|
230
|
+
* @param method an array of HTTP request methods passed in via CLI
|
|
231
|
+
* @returns a MethodFilter for use with the API
|
|
232
|
+
*/
|
|
233
|
+
function parseMethod(method: string[]): MethodFilter {
|
|
234
|
+
return { method };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Header filters can contain either just a key ("X-HEADER-KEY") or both
|
|
239
|
+
* a key and a value ("X-HEADER-KEY:some-value"). This function parses
|
|
240
|
+
* a given string according to that pattern.
|
|
241
|
+
*
|
|
242
|
+
* @param header a header string, "X-HEADER-KEY" or "X-HEADER-KEY:some-value"
|
|
243
|
+
* @returns a HeaderFilter for use with the API
|
|
244
|
+
*/
|
|
245
|
+
function parseHeader(header: string): HeaderFilter {
|
|
246
|
+
const [headerKey, headerQuery] = header.split(":", 2);
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
header: {
|
|
250
|
+
key: headerKey.trim(),
|
|
251
|
+
query: headerQuery?.trim(),
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* A list of IPs can be passed in to filter for messages that come from
|
|
258
|
+
* a worker triggered by a request originating from one of those IPs.
|
|
259
|
+
* You can also pass in the string "self" to filter for the IP of the
|
|
260
|
+
* machine running `wrangler tail`.
|
|
261
|
+
*
|
|
262
|
+
* @param client_ip an array of IP addresses to filter
|
|
263
|
+
* @returns a ClientIPFilter for use with the API
|
|
264
|
+
*/
|
|
265
|
+
function parseIP(client_ip: string[]): ClientIPFilter {
|
|
266
|
+
return { client_ip };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Users can filter for logs that contain a "search" or a "query string".
|
|
271
|
+
* For example, if `--search findme` is passed to then we will only
|
|
272
|
+
* receive logs that contain the string "findme".
|
|
273
|
+
*
|
|
274
|
+
* @param query a query string to search for
|
|
275
|
+
* @returns a QueryFilter for use with the API
|
|
276
|
+
*/
|
|
277
|
+
function parseQuery(query: string): QueryFilter {
|
|
278
|
+
return { query };
|
|
279
|
+
}
|