Skip to content

Commit

Permalink
normalize channel names on hosting:channel:create and remove hashes (#…
Browse files Browse the repository at this point in the history
…2784)

* normalize channel names on :create and remove hashes

* update comments
  • Loading branch information
bkendall committed Nov 5, 2020
1 parent bc882e2 commit d719419
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/commands/hosting-channel-create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bold, yellow } from "cli-color";

import { Channel, createChannel, addAuthDomain } from "../hosting/api";
import { Channel, createChannel, addAuthDomain, normalizeName } from "../hosting/api";
import { Command } from "../command";
import { DEFAULT_DURATION, calculateChannelExpireTTL } from "../hosting/expireUtils";
import { FirebaseError } from "../error";
Expand Down Expand Up @@ -56,6 +56,8 @@ export default new Command("hosting:channel:create [channelId]")
throw new FirebaseError(`"channelId" must not be empty`);
}

channelId = normalizeName(channelId);

let channel: Channel;
try {
channel = await createChannel(projectId, site, channelId, expireTTL);
Expand Down
6 changes: 3 additions & 3 deletions src/hosting/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ export interface Version {

/**
* normalizeName normalizes a name given to it. Most useful for normalizing
* user provided names. This removes any `/`, ':', or '_' characters and
* user provided names. This removes any `/`, ':', '_', or '#' characters and
* replaces them with dashes (`-`).
* @param s the name to normalize.
* @return the normalized name.
*/
export function normalizeName(s: string): string {
// Using a regex replaces *all* bad characters.
return s.replace(/[/:_]/g, "-");
// Using a regex replaces *all* specified characters at once.
return s.replace(/[/:_#]/g, "-");
}

const apiClient = new Client({
Expand Down
1 change: 1 addition & 0 deletions src/test/hosting/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe("normalizeName", () => {
{ in: "happyBranch", out: "happyBranch" },
{ in: "happy:branch", out: "happy-branch" },
{ in: "happy_branch", out: "happy-branch" },
{ in: "happy#branch", out: "happy-branch" },
];

for (const t of tests) {
Expand Down

0 comments on commit d719419

Please sign in to comment.