New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

wrangler

Package Overview
Dependencies
Maintainers
4
Versions
4318
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wrangler - npm Package Compare versions

Comparing version

to
0.0.0-f590943

src/__tests__/helpers/clipboardy-mock.js

6

package.json
{
"name": "wrangler",
"version": "0.0.0-f528351",
"version": "0.0.0-f590943",
"author": "wrangler@cloudflare.com",

@@ -101,3 +101,3 @@ "description": "Command-line interface for all things Cloudflare Workers",

"start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
"test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true",
"test": "jest --silent=false --verbose=true",
"test-watch": "npm run test -- --runInBand --testTimeout=50000 --watch"

@@ -116,3 +116,3 @@ },

"moduleNameMapper": {
"clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
"clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js"
},

@@ -119,0 +119,0 @@ "transform": {

@@ -0,39 +1,47 @@

import { toUrlPath } from "../../src/paths";
import { compareRoutes } from "./filepath-routing";
describe("compareRoutes()", () => {
const url = toUrlPath;
test("routes / last", () => {
expect(compareRoutes("/", "/foo")).toBeGreaterThanOrEqual(1);
expect(compareRoutes("/", "/:foo")).toBeGreaterThanOrEqual(1);
expect(compareRoutes("/", "/:foo*")).toBeGreaterThanOrEqual(1);
expect(compareRoutes([url("/")], [url("/foo")])).toBeGreaterThanOrEqual(1);
expect(compareRoutes([url("/")], [url("/:foo")])).toBeGreaterThanOrEqual(1);
expect(compareRoutes([url("/")], [url("/:foo*")])).toBeGreaterThanOrEqual(
1
);
});
test("routes with fewer segments come after those with more segments", () => {
expect(compareRoutes("/foo", "/foo/bar")).toBeGreaterThanOrEqual(1);
expect(compareRoutes("/foo", "/foo/bar/cat")).toBeGreaterThanOrEqual(1);
expect(
compareRoutes([url("/foo")], [url("/foo/bar")])
).toBeGreaterThanOrEqual(1);
expect(
compareRoutes([url("/foo")], [url("/foo/bar/cat")])
).toBeGreaterThanOrEqual(1);
});
test("routes with wildcard segments come after those without", () => {
expect(compareRoutes("/:foo*", "/foo")).toBe(1);
expect(compareRoutes("/:foo*", "/:foo")).toBe(1);
expect(compareRoutes([url("/:foo*")], [url("/foo")])).toBe(1);
expect(compareRoutes([url("/:foo*")], [url("/:foo")])).toBe(1);
});
test("routes with dynamic segments come after those without", () => {
expect(compareRoutes("/:foo", "/foo")).toBe(1);
expect(compareRoutes([url("/:foo")], [url("/foo")])).toBe(1);
});
test("routes with dynamic segments occuring earlier come after those with dynamic segments in later positions", () => {
expect(compareRoutes("/foo/:id/bar", "/foo/bar/:id")).toBe(1);
test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => {
expect(compareRoutes([url("/foo/:id/bar")], [url("/foo/bar/:id")])).toBe(1);
});
test("routes with no HTTP method come after those specifying a method", () => {
expect(compareRoutes("/foo", "GET /foo")).toBe(1);
expect(compareRoutes([url("/foo")], [url("/foo"), "GET"])).toBe(1);
});
test("two equal routes are sorted according to their original position in the list", () => {
expect(compareRoutes("GET /foo", "GET /foo")).toBe(0);
expect(compareRoutes([url("/foo"), "GET"], [url("/foo"), "GET"])).toBe(0);
});
test("it returns -1 if the first argument should appear first in the list", () => {
expect(compareRoutes("GET /foo", "/foo")).toBe(-1);
expect(compareRoutes([url("/foo"), "GET"], [url("/foo")])).toBe(-1);
});
});

@@ -6,2 +6,4 @@ import fs from "node:fs/promises";

import { transform } from "esbuild";
import { toUrlPath } from "../../src/paths";
import type { UrlPath } from "../../src/paths";
import type { Config, RouteConfig } from "./routes";

@@ -12,5 +14,8 @@ import type { ExportNamedDeclaration, Identifier } from "estree";

baseDir: string;
baseURL: string;
baseURL: UrlPath;
};
type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "HEAD";
type RouteKey = [UrlPath, Method] | [UrlPath, undefined] | [UrlPath];
export async function generateConfigFromFileTree({

@@ -20,10 +25,10 @@ baseDir,

}: Arguments) {
let routeEntries: [string, RouteConfig][] = [];
let routeEntries: [RouteKey, RouteConfig][] = [];
if (!baseURL.startsWith("/")) {
baseURL = `/${baseURL}`;
baseURL = `/${baseURL}` as UrlPath;
}
if (baseURL.endsWith("/")) {
baseURL = baseURL.slice(0, -1);
baseURL = baseURL.slice(0, -1) as UrlPath;
}

@@ -114,8 +119,10 @@

if (method) {
routePath = `${method.toUpperCase()} ${routePath}`;
}
const routeUrlPath = toUrlPath(routePath);
const routeKey: RouteKey = [
routeUrlPath,
method ? (method.toUpperCase() as Method) : undefined,
];
routeEntries.push([
routePath,
routeKey,
{

@@ -153,3 +160,3 @@ [isMiddlewareFile ? "middleware" : "module"]: [

routeEntries.sort(([pathA], [pathB]) => compareRoutes(pathA, pathB));
routeEntries.sort(([a], [b]) => compareRoutes(a, b));

@@ -162,16 +169,12 @@ return { routes: Object.fromEntries(routeEntries) } as Config;

// less specific routes appearing first in the route list.
export function compareRoutes(a: string, b: string) {
function parseRoutePath(routePath: string): [string | null, string[]] {
const parts = routePath.split(" ", 2);
// split() will guarantee at least one element.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const segmentedPath = parts.pop()!;
const method = parts.pop() ?? null;
const segments = segmentedPath.slice(1).split("/").filter(Boolean);
return [method, segments];
export function compareRoutes(
[routePathA, methodA]: RouteKey,
[routePathB, methodB]: RouteKey
) {
function parseRoutePath(routePath: UrlPath): string[] {
return routePath.slice(1).split("/").filter(Boolean);
}
const [methodA, segmentsA] = parseRoutePath(a);
const [methodB, segmentsB] = parseRoutePath(b);
const segmentsA = parseRoutePath(routePathA);
const segmentsB = parseRoutePath(routePathB);

@@ -202,4 +205,4 @@ // sort routes with fewer segments after those with more segments

// all else equal, just sort them lexicographically
return a.localeCompare(b);
// all else equal, just sort the paths lexicographically
return routePathA.localeCompare(routePathB);
}

@@ -206,0 +209,0 @@

import fs from "node:fs/promises";
import path from "node:path";
import { toUrlPath } from "../../src/paths";
import { isValidIdentifier, normalizeIdentifier } from "./identifiers";
import type { UrlPath } from "../../src/paths";

@@ -22,3 +24,3 @@ export const HTTP_METHODS = [

export type RoutesCollection = Array<{
routePath: string;
routePath: UrlPath;
methods: HTTPMethod[];

@@ -35,3 +37,3 @@ modules: string[];

export type RoutesConfig = {
[route: string]: RouteConfig;
[route: UrlPath]: RouteConfig;
};

@@ -127,3 +129,3 @@

routes.push({
routePath,
routePath: toUrlPath(routePath),
methods: _methods.split("|").filter(isHTTPMethod),

@@ -130,0 +132,0 @@ middlewares: parseModuleIdentifiers(props.middleware),

import * as fs from "node:fs";
import * as fsp from "node:fs/promises";
import * as TOML from "@iarna/toml";
import { mockConsoleMethods } from "./mock-console";
import { mockConfirm } from "./mock-dialogs";
import { runInTempDir } from "./run-in-tmp";
import { runWrangler } from "./run-wrangler";
import { version as wranglerVersion } from "../../package.json";
import { npm } from "../npm-installer";
import { mockConsoleMethods } from "./helpers/mock-console";
import { mockConfirm } from "./helpers/mock-dialogs";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";

@@ -12,2 +14,7 @@ describe("wrangler", () => {

beforeEach(() => {
jest.spyOn(npm, "addDevDeps").mockResolvedValue();
jest.spyOn(npm, "install").mockResolvedValue();
});
const std = mockConsoleMethods();

@@ -49,10 +56,7 @@

it("should display an error", async () => {
let err: Error | undefined;
try {
await runWrangler("invalid-command");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(`Unknown command: invalid-command.`);
}
await expect(
runWrangler("invalid-command")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unknown command: invalid-command."`
);

@@ -156,3 +160,3 @@ expect(std.out).toMatchInlineSnapshot(`""`);

expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
expect(packageJson.version).toEqual("0.0.1");
expect(packageJson.version).toEqual("0.0.0");
expect(packageJson.devDependencies).toEqual({

@@ -162,2 +166,3 @@ wrangler: expect.any(String),

expect(fs.existsSync("./tsconfig.json")).toBe(false);
expect(npm.install).toHaveBeenCalled();
});

@@ -215,5 +220,5 @@

expect(packageJson.version).toEqual("1.0.0");
expect(packageJson.devDependencies).toEqual({
wrangler: expect.any(String),
});
expect(npm.addDevDeps).toHaveBeenCalledWith(
`wrangler@${wranglerVersion}`
);
});

@@ -276,9 +281,6 @@

]);
const packageJson = JSON.parse(
fs.readFileSync("./package.json", "utf-8")
expect(npm.addDevDeps).toHaveBeenCalledWith(
"@cloudflare/workers-types",
"typescript"
);
expect(packageJson.devDependencies).toEqual({
"@cloudflare/workers-types": expect.any(String),
wrangler: expect.any(String),
});
});

@@ -343,8 +345,3 @@

expect(tsconfigJson.compilerOptions).toEqual({});
const packageJson = JSON.parse(
fs.readFileSync("./package.json", "utf-8")
);
expect(packageJson.devDependencies).toEqual({
"@cloudflare/workers-types": expect.any(String),
});
expect(npm.addDevDeps).toHaveBeenCalledWith("@cloudflare/workers-types");
});

@@ -385,48 +382,33 @@

it("should error if `--type` is used", async () => {
let err: undefined | Error;
try {
await runWrangler("init --type");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(`The --type option is no longer supported.`);
}
await expect(
runWrangler("init --type")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The --type option is no longer supported."`
);
});
it("should error if `--type javascript` is used", async () => {
let err: undefined | Error;
try {
await runWrangler("init --type javascript");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(`The --type option is no longer supported.`);
}
await expect(
runWrangler("init --type javascript")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The --type option is no longer supported."`
);
});
it("should error if `--type rust` is used", async () => {
let err: undefined | Error;
try {
await runWrangler("init --type rust");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(`The --type option is no longer supported.`);
}
await expect(
runWrangler("init --type rust")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The --type option is no longer supported."`
);
});
it("should error if `--type webpack` is used", async () => {
let err: undefined | Error;
try {
await runWrangler("init --type webpack");
} catch (e) {
err = e;
} finally {
expect(err?.message).toBe(
`The --type option is no longer supported.
If you wish to use webpack then you will need to create a custom build.`
);
}
await expect(runWrangler("init --type webpack")).rejects
.toThrowErrorMatchingInlineSnapshot(`
"The --type option is no longer supported.
If you wish to use webpack then you will need to create a custom build."
`);
});
});
});
import fetchMock from "jest-fetch-mock";
import { fetchInternal } from "../cfetch/internal";
import { fetchInternal, fetchKVGetValue } from "../cfetch/internal";
import { confirm, prompt } from "../dialogs";
import { mockFetchInternal } from "./mock-cfetch";
import { mockFetchInternal, mockFetchKVGetValue } from "./helpers/mock-cfetch";

@@ -19,2 +19,3 @@ jest.mock("undici", () => {

(fetchInternal as jest.Mock).mockImplementation(mockFetchInternal);
(fetchKVGetValue as jest.Mock).mockImplementation(mockFetchKVGetValue);

@@ -21,0 +22,0 @@ jest.mock("../dialogs");

import { writeFileSync } from "node:fs";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import {
setMockResponse,
setMockRawResponse,
unsetAllMocks,
} from "./mock-cfetch";
import { mockConsoleMethods } from "./mock-console";
import { mockKeyListRequest } from "./mock-kv";
import { runInTempDir } from "./run-in-tmp";
import { runWrangler } from "./run-wrangler";
unsetMockFetchKVGetValues,
setMockFetchKVGetValue,
} from "./helpers/mock-cfetch";
import { mockConsoleMethods } from "./helpers/mock-console";
import { mockKeyListRequest } from "./helpers/mock-kv";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import type { KVNamespaceInfo } from "../kv";
describe("wrangler", () => {
mockAccountId();
mockApiToken();
runInTempDir();

@@ -37,8 +41,7 @@ const std = mockConsoleMethods();

it("should error if no namespace is given", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:namespace create");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:namespace create")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Not enough non-option arguments: got 0, need at least 1"`
);
expect(std.out).toMatchInlineSnapshot(`""`);

@@ -65,14 +68,10 @@ expect(std.err).toMatchInlineSnapshot(`

`);
expect(error).toMatchInlineSnapshot(
`[Error: Not enough non-option arguments: got 0, need at least 1]`
);
});
it("should error if the namespace to create contains spaces", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:namespace create abc def ghi");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:namespace create abc def ghi")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unexpected additional positional arguments \\"def ghi\\"."`
);
expect(std.out).toMatchInlineSnapshot(`""`);

@@ -99,14 +98,10 @@ expect(std.err).toMatchInlineSnapshot(`

`);
expect(error).toMatchInlineSnapshot(
`[Error: Unexpected additional positional arguments "def ghi".]`
);
});
it("should error if the namespace to create is not valid", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:namespace create abc-def");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:namespace create abc-def")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."`
);

@@ -134,5 +129,2 @@ expect(std.out).toMatchInlineSnapshot(`""`);

`);
expect(error).toMatchInlineSnapshot(
`[Error: The namespace binding name "abc-def" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.]`
);
});

@@ -280,12 +272,7 @@

writeWranglerConfig();
let error: Error | undefined;
try {
await runWrangler("kv:namespace delete --binding otherBinding");
} catch (e) {
error = e;
}
expect(error).toMatchInlineSnapshot(`
[Error: Not able to delete namespace.
A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]
`);
await expect(runWrangler("kv:namespace delete --binding otherBinding"))
.rejects.toThrowErrorMatchingInlineSnapshot(`
"Not able to delete namespace.
A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
`);
expect(std.err).toMatchInlineSnapshot(`

@@ -430,11 +417,5 @@ "wrangler kv:namespace delete

);
let error: Error | undefined;
try {
await runWrangler(
"kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
);
} catch (e) {
error = e;
}
await runWrangler(
"kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
);
expect(requests.count).toEqual(1);

@@ -445,3 +426,2 @@ expect(std.out).toMatchInlineSnapshot(

expect(std.err).toMatchInlineSnapshot(`""`);
expect(error).toMatchInlineSnapshot(`undefined`);
});

@@ -484,8 +464,7 @@

it("should error if no key is provided", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:key put");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key put")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Not enough non-option arguments: got 0, need at least 1"`
);

@@ -519,14 +498,10 @@ expect(std.out).toMatchInlineSnapshot(`""`);

`);
expect(error).toMatchInlineSnapshot(
`[Error: Not enough non-option arguments: got 0, need at least 1]`
);
});
it("should error if no binding nor namespace is provided", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:key put foo bar");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key put foo bar")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Exactly one of the arguments binding and namespace-id is required"`
);

@@ -560,14 +535,10 @@ expect(std.out).toMatchInlineSnapshot(`""`);

`);
expect(error).toMatchInlineSnapshot(
`[Error: Exactly one of the arguments binding and namespace-id is required]`
);
});
it("should error if both binding and namespace is provided", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:key put foo bar --binding x --namespace-id y");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key put foo bar --binding x --namespace-id y")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Arguments binding and namespace-id are mutually exclusive"`
);

@@ -601,14 +572,10 @@ expect(std.out).toMatchInlineSnapshot(`""`);

`);
expect(error).toMatchInlineSnapshot(
`[Error: Arguments binding and namespace-id are mutually exclusive]`
);
});
it("should error if no value nor path is provided", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:key put key --namespace-id 12345");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key put key --namespace-id 12345")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Exactly one of the arguments value and path is required"`
);

@@ -642,16 +609,10 @@ expect(std.out).toMatchInlineSnapshot(`""`);

`);
expect(error).toMatchInlineSnapshot(
`[Error: Exactly one of the arguments value and path is required]`
);
});
it("should error if both value and path is provided", async () => {
let error: Error | undefined;
try {
await runWrangler(
"kv:key put key value --path xyz --namespace-id 12345"
);
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key put key value --path xyz --namespace-id 12345")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Arguments value and path are mutually exclusive"`
);

@@ -685,5 +646,2 @@ expect(std.out).toMatchInlineSnapshot(`""`);

`);
expect(error).toMatchInlineSnapshot(
`[Error: Arguments value and path are mutually exclusive]`
);
});

@@ -693,8 +651,7 @@

writeWranglerConfig();
let error: Error | undefined;
try {
await runWrangler("kv:key put key value --binding otherBinding");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key put key value --binding otherBinding")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
);

@@ -708,5 +665,2 @@ expect(std.out).toMatchInlineSnapshot(`""`);

`);
expect(error).toMatchInlineSnapshot(
`[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
);
});

@@ -721,8 +675,7 @@

);
let error: Error | undefined;
try {
await runWrangler("kv:key put my-key my-value --binding someBinding");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key put my-key my-value --binding someBinding")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace."`
);
expect(std.out).toMatchInlineSnapshot(`""`);

@@ -735,5 +688,2 @@ expect(std.err).toMatchInlineSnapshot(`

`);
expect(error).toMatchInlineSnapshot(
`[Error: someBinding has both a namespace ID and a preview ID. Specify "--preview" or "--preview false" to avoid writing data to the wrong namespace.]`
);
expect(requests.count).toEqual(0);

@@ -917,10 +867,6 @@ });

writeWranglerConfig();
let error: Error | undefined;
try {
await runWrangler("kv:key list --binding otherBinding");
} catch (e) {
error = e;
}
expect(error).toMatchInlineSnapshot(
`[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
await expect(
runWrangler("kv:key list --binding otherBinding")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
);

@@ -938,23 +884,9 @@ expect(std.err).toMatchInlineSnapshot(`

describe("get", () => {
function mockKeyGetRequest(
expectedNamespaceId: string,
expectedKey: string,
expectedValue: string
) {
const requests = { count: 0 };
setMockRawResponse(
"/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
([_url, accountId, namespaceId, key]) => {
requests.count++;
expect(accountId).toEqual("some-account-id");
expect(namespaceId).toEqual(expectedNamespaceId);
expect(key).toEqual(expectedKey);
return expectedValue;
}
);
return requests;
}
afterEach(() => {
unsetMockFetchKVGetValues();
});
it("should get a key in a given namespace specified by namespace-id", async () => {
const requests = mockKeyGetRequest(
setMockFetchKVGetValue(
"some-account-id",
"some-namespace-id",

@@ -967,3 +899,2 @@ "my-key",

expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});

@@ -973,3 +904,8 @@

writeWranglerConfig();
const requests = mockKeyGetRequest("bound-id", "my-key", "my-value");
setMockFetchKVGetValue(
"some-account-id",
"bound-id",
"my-key",
"my-value"
);
await runWrangler(

@@ -980,3 +916,2 @@ "kv:key get my-key --binding someBinding --preview false"

expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});

@@ -986,3 +921,4 @@

writeWranglerConfig();
const requests = mockKeyGetRequest(
setMockFetchKVGetValue(
"some-account-id",
"preview-bound-id",

@@ -995,3 +931,2 @@ "my-key",

expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});

@@ -1001,3 +936,4 @@

writeWranglerConfig();
const requests = mockKeyGetRequest(
setMockFetchKVGetValue(
"some-account-id",
"env-bound-id",

@@ -1012,12 +948,10 @@ "my-key",

expect(std.err).toMatchInlineSnapshot(`""`);
expect(requests.count).toEqual(1);
});
it("should error if no key is provided", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:key get");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key get")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Not enough non-option arguments: got 0, need at least 1"`
);
expect(std.out).toMatchInlineSnapshot(`""`);

@@ -1046,14 +980,10 @@ expect(std.err).toMatchInlineSnapshot(`

`);
expect(error).toMatchInlineSnapshot(
`[Error: Not enough non-option arguments: got 0, need at least 1]`
);
});
it("should error if no binding nor namespace is provided", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:key get foo");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key get foo")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Exactly one of the arguments binding and namespace-id is required"`
);
expect(std.out).toMatchInlineSnapshot(`""`);

@@ -1082,14 +1012,10 @@ expect(std.err).toMatchInlineSnapshot(`

`);
expect(error).toMatchInlineSnapshot(
`[Error: Exactly one of the arguments binding and namespace-id is required]`
);
});
it("should error if both binding and namespace is provided", async () => {
let error: Error | undefined;
try {
await runWrangler("kv:key get foo --binding x --namespace-id y");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key get foo --binding x --namespace-id y")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Arguments binding and namespace-id are mutually exclusive"`
);

@@ -1119,5 +1045,2 @@ expect(std.out).toMatchInlineSnapshot(`""`);

`);
expect(error).toMatchInlineSnapshot(
`[Error: Arguments binding and namespace-id are mutually exclusive]`
);
});

@@ -1127,8 +1050,7 @@

writeWranglerConfig();
let error: Error | undefined;
try {
await runWrangler("kv:key get key --binding otherBinding");
} catch (e) {
error = e;
}
await expect(
runWrangler("kv:key get key --binding otherBinding")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
);
expect(std.out).toMatchInlineSnapshot(`""`);

@@ -1141,5 +1063,2 @@ expect(std.err).toMatchInlineSnapshot(`

`);
expect(error).toMatchInlineSnapshot(
`[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
);
});

@@ -1196,8 +1115,7 @@ });

writeWranglerConfig();
let error: Error | undefined;
try {
await runWrangler(`kv:key delete --binding otherBinding someKey`);
} catch (e) {
error = e;
}
await expect(
runWrangler(`kv:key delete --binding otherBinding someKey`)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
);

@@ -1210,5 +1128,2 @@ expect(std.err).toMatchInlineSnapshot(`

`);
expect(error).toMatchInlineSnapshot(
`[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
);
});

@@ -1215,0 +1130,0 @@

@@ -6,10 +6,7 @@ import { existsSync } from "node:fs";

import { initialise } from "../user";
import { mockConsoleMethods } from "./mock-console";
import { writeUserConfig } from "./mock-user";
import { runInTempDir } from "./run-in-tmp";
import { runWrangler } from "./run-wrangler";
import { mockConsoleMethods } from "./helpers/mock-console";
import { writeUserConfig } from "./helpers/mock-user";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
const ORIGINAL_CF_API_TOKEN = process.env.CF_API_TOKEN;
const ORIGINAL_CF_ACCOUNT_ID = process.env.CF_ACCOUNT_ID;
describe("wrangler", () => {

@@ -19,13 +16,2 @@ runInTempDir({ homedir: "./home" });

beforeEach(() => {
delete process.env.CF_API_TOKEN;
delete process.env.CF_ACCOUNT_ID;
});
afterEach(() => {
// Reset any changes to the environment variables
process.env.CF_API_TOKEN = ORIGINAL_CF_API_TOKEN;
process.env.CF_ACCOUNT_ID = ORIGINAL_CF_ACCOUNT_ID;
});
describe("logout", () => {

@@ -32,0 +18,0 @@ it("should exit with a message stating the user is not logged in", async () => {

import * as fs from "node:fs";
import * as path from "node:path";
import * as TOML from "@iarna/toml";
import { setMockResponse, unsetAllMocks } from "./mock-cfetch";
import { mockConsoleMethods } from "./mock-console";
import { mockKeyListRequest } from "./mock-kv";
import { runInTempDir } from "./run-in-tmp";
import { runWrangler } from "./run-wrangler";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
import { mockConsoleMethods } from "./helpers/mock-console";
import { mockKeyListRequest } from "./helpers/mock-kv";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import type { WorkerMetadata } from "../api/form_data";

@@ -15,2 +16,4 @@ import type { Config } from "../config";

describe("publish", () => {
mockAccountId();
mockApiToken();
runInTempDir();

@@ -32,3 +35,3 @@ const std = mockConsoleMethods();

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"Uploaded

@@ -53,3 +56,3 @@ test-name

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"Uploaded

@@ -75,3 +78,3 @@ test-name

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"Uploaded

@@ -96,3 +99,3 @@ test-name

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"Uploaded

@@ -120,3 +123,3 @@ test-name

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"Uploaded

@@ -151,3 +154,3 @@ test-name

await runWrangler("publish index.js");
expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"Uploaded

@@ -173,3 +176,3 @@ test-name

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"Uploaded

@@ -198,3 +201,3 @@ test-name

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"Uploaded

@@ -222,9 +225,9 @@ test-name

mockSubDomainRequest();
let error: Error | undefined;
try {
await runWrangler("publish ./index.js");
} catch (e) {
error = e;
}
await expect(
runWrangler("publish ./index.js")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A [site] definition requires a \`bucket\` field with a path to the site's public directory."`
);
expect(std.out).toMatchInlineSnapshot(`""`);

@@ -242,5 +245,2 @@ expect(std.err).toMatchInlineSnapshot(`

`);
expect(error).toMatchInlineSnapshot(
`[AssertionError: A [site] definition requires a \`bucket\` field with a path to the site's public directory.]`
);
});

@@ -274,3 +274,3 @@

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -302,10 +302,9 @@ uploading as assets/file-1.2ca234f380.txt...

mockSubDomainRequest();
let error: Error | undefined;
try {
await runWrangler("publish");
} catch (e) {
error = e;
}
await expect(
runWrangler("publish")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field."`
);
expect(stripTimings(std.out)).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`

@@ -317,5 +316,2 @@ "Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.

`);
expect(error).toMatchInlineSnapshot(
`[Error: Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.]`
);
});

@@ -349,3 +345,3 @@ });

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -402,3 +398,3 @@ uploading as assets/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -417,3 +413,3 @@ uploading as assets/file-1.2ca234f380.txt...

`);
expect(stripTimings(std.err)).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

@@ -458,3 +454,3 @@

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -473,3 +469,3 @@ uploading as assets/file-1.2ca234f380.txt...

`);
expect(stripTimings(std.err)).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

@@ -511,3 +507,3 @@

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -558,3 +554,3 @@ uploading as assets/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -604,3 +600,3 @@ skipping - already uploaded

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -648,3 +644,3 @@ uploading as assets/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -693,3 +689,3 @@ uploading as assets/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -738,3 +734,3 @@ uploading as assets/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -783,3 +779,3 @@ uploading as assets/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -828,3 +824,3 @@ uploading as assets/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/file-1.txt...

@@ -875,3 +871,3 @@ uploading as assets/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/directory-1/file-1.txt...

@@ -926,3 +922,3 @@ uploading as assets/directory-1/file-1.2ca234f380.txt...

expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"reading assets/.well-known/file-2.txt...

@@ -972,8 +968,8 @@ uploading as assets/.well-known/file-2.5938485188.txt...

let error: Error | undefined;
try {
await runWrangler("publish");
} catch (e) {
error = e;
}
await expect(
runWrangler("publish")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"File assets/too-large-file.txt is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits"`
);
expect(std.out).toMatchInlineSnapshot(`

@@ -989,5 +985,2 @@ "reading assets/large-file.txt...

`);
expect(error).toMatchInlineSnapshot(
`[Error: File assets/too-large-file.txt is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits]`
);
});

@@ -1017,8 +1010,7 @@

let error: Error | undefined;
try {
await runWrangler("publish");
} catch (e) {
error = e;
}
await expect(
runWrangler("publish")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The asset path key \\"assets/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/file.3da0d0cd12.txt\\" exceeds the maximum key size limit of 512. See https://developers.cloudflare.com/workers/platform/limits#kv-limits\\","`
);

@@ -1034,5 +1026,2 @@ expect(std.out).toMatchInlineSnapshot(

`);
expect(error).toMatchInlineSnapshot(
`[Error: The asset path key "assets/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/file.3da0d0cd12.txt" exceeds the maximum key size limit of 512. See https://developers.cloudflare.com/workers/platform/limits#kv-limits",]`
);
});

@@ -1045,3 +1034,3 @@ });

build: {
command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
},

@@ -1056,5 +1045,5 @@ });

await runWrangler("publish index.js");
expect(stripTimings(std.out)).toMatchInlineSnapshot(`
expect(std.out).toMatchInlineSnapshot(`
"running:
echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
Uploaded

@@ -1072,2 +1061,33 @@ test-name

});
if (process.platform !== "win32") {
it("should run a custom build of multiple steps combined by && before publishing", async () => {
writeWranglerToml({
build: {
command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
},
});
mockUploadWorkerRequest({
expectedEntry: "return new Response(123)",
});
mockSubDomainRequest();
await runWrangler("publish index.js");
expect(std.out).toMatchInlineSnapshot(`
"running:
echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
Uploaded
test-name
(TIMINGS)
Deployed
test-name
(TIMINGS)
test-name.test-sub-domain.workers.dev"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});
}
});

@@ -1239,6 +1259,1 @@ });

}
/** Strip timing data out of the stdout, since this is not always deterministic. */
function stripTimings(stdout: string): string {
return stdout.replace(/\(\d+\.\d+ sec\)/g, "(TIMINGS)");
}

@@ -1,6 +0,7 @@

import { setMockResponse, unsetAllMocks } from "./mock-cfetch";
import { mockConsoleMethods } from "./mock-console";
import { mockConfirm, mockPrompt } from "./mock-dialogs";
import { runInTempDir } from "./run-in-tmp";
import { runWrangler } from "./run-wrangler";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
import { mockConsoleMethods } from "./helpers/mock-console";
import { mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";

@@ -10,2 +11,5 @@ describe("wrangler secret", () => {

runInTempDir();
mockAccountId();
mockApiToken();
afterEach(() => {

@@ -12,0 +16,0 @@ unsetAllMocks();

@@ -21,12 +21,3 @@ import { URLSearchParams } from "node:url";

/**
* Make a fetch request for a raw JSON value.
*/
export async function fetchRaw<ResponseType>(
resource: string,
init: RequestInit = {},
queryParams?: URLSearchParams
): Promise<ResponseType> {
return fetchInternal<ResponseType>(resource, init, queryParams);
}
export { fetchKVGetValue } from "./internal";

@@ -33,0 +24,0 @@ /**

@@ -34,3 +34,2 @@ import { fetch } from "undici";

});
const jsonText = await response.text();

@@ -74,1 +73,31 @@ try {

}
/**
* The implementation for fetching a kv value from the cloudflare API.
* We special-case this one call, because it's the only API call that
* doesn't return json. We inline the implementation and try not to share
* any code with the other calls. We should push back on any new APIs that
* try to introduce non-"standard" response structures.
*/
export async function fetchKVGetValue(
accountId: string,
namespaceId: string,
key: string
): Promise<string> {
await requireLoggedIn();
const apiToken = requireApiToken();
const headers = { Authorization: `Bearer ${apiToken}` };
const resource = `${CF_API_BASE_URL}/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`;
const response = await fetch(resource, {
method: "GET",
headers,
});
if (response.ok) {
return await response.text();
} else {
throw new Error(
`Failed to fetch ${resource} - ${response.status}: ${response.statusText});`
);
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet