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
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 @@
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 @@ -156,14 +156,49 @@
if (c.expectEnsureSchemaGQL) {
expect(ensureSyncStub).to.have.been.calledWith("dataconnect/schema/schema.gql");
}
expect(askWriteProjectFileStub.args.map((a) => a[0])).to.deep.equal(c.expectedFiles);

Check warning on line 159 in src/init/features/dataconnect/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe return of an `any` typed value
expect(provisionCSQLStub.called).to.equal(c.expectCSQLProvisioning);
});
}
});

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 {

Check warning on line 201 in src/init/features/dataconnect/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
return new Config(data, {});
}
function mockRequiredInfo(info: Partial<init.RequiredInfo> = {}): init.RequiredInfo {
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 @@ -70,7 +70,7 @@
const defaultSchema = { path: "schema.gql", content: SCHEMA_TEMPLATE };

// doSetup is split into 2 phases - ask questions and then actuate files and API calls based on those answers.
export async function doSetup(setup: Setup, config: Config): Promise<void> {

Check warning on line 73 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const isBillingEnabled = setup.projectId ? await checkBillingEnabled(setup.projectId) : false;
if (setup.projectId) {
isBillingEnabled ? await ensureApis(setup.projectId) : await ensureSparkApis(setup.projectId);
Expand All @@ -78,8 +78,8 @@
const info = await askQuestions(setup, isBillingEnabled);
// Most users will want to perist data between emulator runs, so set this to a reasonable default.

const dir: string = config.get("dataconnect.source", "dataconnect");

Check warning on line 81 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const dataDir = config.get("emulators.dataconnect.dataDir", `${dir}/.dataconnect/pgliteData`);

Check warning on line 82 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
config.set("emulators.dataconnect.dataDir", dataDir);
await actuate(setup, config, info);

Expand Down Expand Up @@ -141,7 +141,9 @@
}))
);
} 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 All @@ -151,7 +153,7 @@

// actuate writes product specific files and makes product specifc API calls.
// It does not handle writing firebase.json and .firebaserc
export async function actuate(setup: Setup, config: Config, info: RequiredInfo) {

Check warning on line 156 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function

Check warning on line 156 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
await writeFiles(config, info);

if (setup.projectId && info.shouldProvisionCSQL) {
Expand All @@ -160,15 +162,15 @@
locationId: info.locationId,
instanceId: info.cloudSqlInstanceId,
databaseId: info.cloudSqlDatabase,
configYamlPath: join(config.get("dataconnect.source"), "dataconnect.yaml"),

Check warning on line 165 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `string`
enableGoogleMlIntegration: false,
waitForCreation: false,
});
}
}

async function writeFiles(config: Config, info: RequiredInfo) {

Check warning on line 172 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
const dir: string = config.get("dataconnect.source") || "dataconnect";

Check warning on line 173 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const subbedDataconnectYaml = subDataconnectYamlValues({
...info,
connectorDirs: info.connectors.map((c) => c.path),
Expand Down Expand Up @@ -447,3 +449,20 @@
];
}
}

/**
* 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";
}
Loading