Skip to content

Commit

Permalink
Suggest DNS friendly service ids (#8118)
Browse files Browse the repository at this point in the history
  • Loading branch information
joehan authored Jan 15, 2025
1 parent be02559 commit a16eb2e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Updated `cross-env` and `cross-spawn` dependencies to avoid vulnerable versions. (#7979)
- Fixed an issue with the Data Connect emulator where `dataDir` and `--export` were relative to the current directory insead of `firebase.json`.
- `init dataconnect` now suggests DNS compatible service IDs.
37 changes: 36 additions & 1 deletion src/init/features/dataconnect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("init dataconnect", () => {
expectEnsureSchemaGQL: false,
},
{
desc: "exiting project should use existing directory",
desc: "existing project should use existing directory",
requiredInfo: mockRequiredInfo(),
config: mockConfig({ dataconnect: { source: "not-dataconnect" } }),
expectedSource: "not-dataconnect",
Expand Down Expand Up @@ -161,6 +161,41 @@ describe("init dataconnect", () => {
});
}
});

describe("toDNSCompatibleId", () => {
const cases: { description: string; input: string; expected: string }[] = [
{
description: "Should noop compatible strings",
input: "this-is-compatible",
expected: "this-is-compatible",
},
{
description: "Should lower case",
input: "This-Is-Compatible",
expected: "this-is-compatible",
},
{
description: "Should strip special characters",
input: "this-is-compatible?~!@#$%^&*()_+=",
expected: "this-is-compatible",
},
{
description: "Should strip trailing and leading -",
input: "---this-is-compatible---",
expected: "this-is-compatible",
},
{
description: "Should cut to 63 characters",
input: "a".repeat(1000),
expected: "a".repeat(63),
},
];
for (const c of cases) {
it(c.description, () => {
expect(init.toDNSCompatibleId(c.input)).to.equal(c.expected);
});
}
});
});

function mockConfig(data: Record<string, any> = {}): Config {
Expand Down
21 changes: 20 additions & 1 deletion src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ async function askQuestions(setup: Setup, isBillingEnabled: boolean): Promise<Re
}))
);
} else {
info.serviceId = info.serviceId || basename(process.cwd());
// Ensure that the suggested name is DNS compatible
const defaultServiceId = toDNSCompatibleId(basename(process.cwd()));
info.serviceId = info.serviceId || defaultServiceId;
info.cloudSqlInstanceId = info.cloudSqlInstanceId || `${info.serviceId || "app"}-fdc`;
info.locationId = info.locationId || `us-central1`;
info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;
Expand Down Expand Up @@ -447,3 +449,20 @@ async function locationChoices(setup: Setup) {
];
}
}

/**
* Converts any string to a DNS friendly service ID.
*/
export function toDNSCompatibleId(id: string): string {
let defaultServiceId = basename(id)
.toLowerCase()
.replaceAll(/[^a-z0-9-]/g, "")
.slice(0, 63);
while (defaultServiceId.endsWith("-") && defaultServiceId.length) {
defaultServiceId = defaultServiceId.slice(0, defaultServiceId.length - 1);
}
while (defaultServiceId.startsWith("-") && defaultServiceId.length) {
defaultServiceId = defaultServiceId.slice(1, defaultServiceId.length);
}
return defaultServiceId || "app";
}

0 comments on commit a16eb2e

Please sign in to comment.