Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/silent-shrimps-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"create-cloudflare": patch
"@cloudflare/workers-utils": patch
"wrangler": patch
---

Use the smol-toml library for parsing TOML instead of @iarna/toml
8 changes: 4 additions & 4 deletions packages/create-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"@babel/types": "^7.21.4",
"@clack/prompts": "^0.6.3",
"@cloudflare/cli": "workspace:*",
"@cloudflare/eslint-config-shared": "workspace:*",
"@cloudflare/mock-npm-registry": "workspace:*",
"@cloudflare/vite-plugin": "workspace:*",
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "catalog:default",
"@iarna/toml": "^3.0.0",
"@types/command-exists": "^1.2.0",
"@types/cross-spawn": "^6.0.2",
"@types/deepmerge": "^2.2.0",
Expand All @@ -68,6 +68,7 @@
"dns2": "^2.1.0",
"dotenv": "^16.0.0",
"esbuild": "catalog:default",
"eslint": "catalog:default",
"execa": "^7.1.1",
"exit-hook": "2.2.1",
"get-port": "^7.1.0",
Expand All @@ -79,6 +80,7 @@
"open": "^8.4.0",
"recast": "^0.22.0",
"semver": "^7.7.1",
"smol-toml": "catalog:default",
"typescript": "catalog:default",
"undici": "catalog:default",
"vite": "catalog:default",
Expand All @@ -88,9 +90,7 @@
"wrangler": "workspace:*",
"wrap-ansi": "^9.0.0",
"xdg-app-paths": "^8.3.0",
"yargs": "^17.7.2",
"@cloudflare/eslint-config-shared": "workspace:*",
"eslint": "catalog:default"
"yargs": "^17.7.2"
},
"engines": {
"node": ">=18.14.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/create-cloudflare/src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { startSection, updateStatus } from "@cloudflare/cli";
import { blue, brandColor, dim } from "@cloudflare/cli/colors";
import TOML from "@iarna/toml";
import { processArgument } from "helpers/args";
import { C3_DEFAULTS, openInBrowser } from "helpers/cli";
import { quoteShellArgs, runCommand } from "helpers/command";
import { readFile } from "helpers/files";
import { detectPackageManager } from "helpers/packageManagers";
import { poll } from "helpers/poll";
import TOML from "smol-toml";
import { isInsideGitRepo } from "./git";
import { chooseAccount, wranglerLogin } from "./wrangler/accounts";
import {
Expand Down
5 changes: 2 additions & 3 deletions packages/create-cloudflare/src/helpers/files.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs, { existsSync, statSync } from "fs";
import { join } from "path";
import TOML from "@iarna/toml";
import { parse, stringify } from "comment-json";
import type { JsonMap } from "@iarna/toml";
import TOML from "smol-toml";
import type { C3Context, PackageJson } from "types";

export const copyFile = (path: string, dest: string) => {
Expand Down Expand Up @@ -75,7 +74,7 @@ export const writeJSON = (
writeFile(path, stringify(object, null, stringifySpace));
};

export const writeToml = (path: string, object: JsonMap) => {
export const writeToml = (path: string, object: Record<string, unknown>) => {
writeFile(path, TOML.stringify(object));
};

Expand Down
2 changes: 1 addition & 1 deletion packages/create-cloudflare/src/workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { existsSync } from "fs";
import { join } from "path";
import { warn } from "@cloudflare/cli";
import { brandColor, dim } from "@cloudflare/cli/colors";
import TOML from "@iarna/toml";
import { runCommand } from "helpers/command";
import { getLatestTypesEntrypoint } from "helpers/compatDate";
import { readFile, readJSON, usesTypescript, writeFile } from "helpers/files";
import { detectPackageManager } from "helpers/packageManagers";
import { installPackages } from "helpers/packages";
import * as jsonc from "jsonc-parser";
import TOML from "smol-toml";
import {
readWranglerJson,
readWranglerToml,
Expand Down
5 changes: 2 additions & 3 deletions packages/create-cloudflare/src/wrangler/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { existsSync, mkdirSync } from "fs";
import { resolve } from "path";
import TOML from "@iarna/toml";
import { getWorkerdCompatibilityDate } from "helpers/compatDate";
import { readFile, writeFile, writeJSON } from "helpers/files";
import {
Expand All @@ -10,7 +9,7 @@ import {
readJSONWithComments,
writeJSONWithComments,
} from "helpers/json";
import type { JsonMap } from "@iarna/toml";
import TOML from "smol-toml";
import type { CommentObject } from "comment-json";
import type { C3Context } from "types";

Expand Down Expand Up @@ -82,7 +81,7 @@ export const updateWranglerConfig = async (ctx: C3Context) => {

const comment = `#:schema node_modules/wrangler/config-schema.json\n# For more details on how to configure Wrangler, refer to:\n# https://developers.cloudflare.com/workers/wrangler/configuration/\n`;

const stringified = comment + TOML.stringify(parsed as JsonMap);
const stringified = comment + TOML.stringify(parsed);

writeWranglerToml(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion packages/workers-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
"@cloudflare/workers-shared": "workspace:*",
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workflows-shared": "workspace:*",
"@iarna/toml": "^3.0.0",
"@types/node": "catalog:default",
"@vitest/ui": "catalog:default",
"cloudflare": "^5.2.0",
"concurrently": "^8.2.2",
"eslint": "catalog:default",
"find-up": "^6.3.0",
"jsonc-parser": "^3.2.0",
"smol-toml": "catalog:default",
"ts-dedent": "^2.2.0",
"tsdown": "^0.15.9",
"tsup": "8.3.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/workers-utils/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TOML from "@iarna/toml";
import TOML from "smol-toml";
import { parseJSONC, parseTOML, readFileSync } from "../parse";
import { resolveWranglerConfigPath } from "./config-helpers";
import type { Config, RawConfig } from "./config";
Expand Down Expand Up @@ -47,7 +47,7 @@ export function formatConfigSnippet(
) {
const format = configFormat(configPath);
if (format === "toml") {
return TOML.stringify(snippet as TOML.JsonMap);
return TOML.stringify(snippet);
} else {
return formatted
? JSON.stringify(snippet, null, 2)
Expand Down
4 changes: 2 additions & 2 deletions packages/workers-utils/src/config/patch-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writeFileSync } from "fs";
import TOML from "@iarna/toml";
import { applyEdits, format, modify } from "jsonc-parser";
import TOML from "smol-toml";
import { parseJSONC, parseTOML, readFileSync } from "../parse";
import type { RawConfig } from "./config";
import type { JSONPath } from "jsonc-parser";
Expand Down Expand Up @@ -46,7 +46,7 @@ export const experimental_patchConfig = (
configString = applyEdits(configString, formatEdit);

if (configPath.endsWith(".toml")) {
configString = TOML.stringify(parseJSONC(configString) as TOML.JsonMap);
configString = TOML.stringify(parseJSONC(configString));
}
writeFileSync(configPath, configString);
return configString;
Expand Down
28 changes: 8 additions & 20 deletions packages/workers-utils/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync as fsReadFileSync } from "node:fs";
import { resolve } from "node:path";
import TOML from "@iarna/toml";
import * as jsoncParser from "jsonc-parser";
import TOML, { TomlError } from "smol-toml";
import { UserError } from "./errors";
import type { TelemetryMessage } from "./errors";
import type { ParseError as JsoncParseError } from "jsonc-parser";
Expand Down Expand Up @@ -81,38 +81,26 @@ export class APIError extends ParseError {
}
}

const TOML_ERROR_NAME = "TomlError";
const TOML_ERROR_SUFFIX = " at row ";

type TomlError = Error & {
line: number;
col: number;
};

/**
* A wrapper around `TOML.parse` that throws a `ParseError`.
*/
export function parseTOML<T>(input: string, file?: string): T | never {
try {
// Normalize CRLF to LF to avoid hitting https://github.com/iarna/iarna-toml/issues/33.
const normalizedInput = input.replace(/\r\n/g, "\n");
return TOML.parse(normalizedInput) as T;
return TOML.parse(input) as T;
} catch (err) {
const { name, message, line, col } = err as TomlError;
if (name !== TOML_ERROR_NAME) {
if (!(err instanceof TomlError)) {
throw err;
}
const text = message.substring(0, message.lastIndexOf(TOML_ERROR_SUFFIX));
const lineText = input.split("\n")[line];

const location = {
lineText,
line: line + 1,
column: col - 1,
lineText: input.split("\n")[err.line - 1],
line: err.line,
column: err.column - 1,
file,
fileText: input,
};
throw new ParseError({
text,
text: err.message.substring(0, err.message.indexOf("\n")),
location,
telemetryMessage: "TOML parse error",
});
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
"@cloudflare/workers-utils": "workspace:*",
"@cloudflare/workflows-shared": "workspace:*",
"@cspotcode/source-map-support": "0.8.1",
"@iarna/toml": "^3.0.0",
"@netlify/build-info": "^10.0.9",
"@sentry/node": "^7.86.0",
"@sentry/types": "^7.86.0",
Expand Down Expand Up @@ -153,6 +152,7 @@
"semiver": "^1.1.0",
"shell-quote": "^1.8.1",
"signal-exit": "^3.0.7",
"smol-toml": "catalog:default",
"source-map": "^0.6.1",
"strip-ansi": "^7.1.0",
"supports-color": "^9.2.2",
Expand Down
Loading
Loading