UNPKG

2.5 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2019 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18/**
19 * Remove any invalid characters from Docker image name component
20 * `name` to make it a valid Docker image name component. If
21 * `hubOwner` is true, it ensures the name contains only alphanumeric
22 * characters.
23 *
24 * From https://docs.docker.com/engine/reference/commandline/tag/:
25 *
26 * > An image name is made up of slash-separated name components,
27 * > optionally prefixed by a registry hostname. The hostname must
28 * > comply with standard DNS rules, but may not contain
29 * > underscores. If a hostname is present, it may optionally be
30 * > followed by a port number in the format :8080. If not present,
31 * > the command uses Docker’s public registry located at
32 * > registry-1.docker.io by default. Name components may contain
33 * > lowercase letters, digits and separators. A separator is defined
34 * > as a period, one or two underscores, or one or more dashes. A
35 * > name component may not start or end with a separator.
36 * >
37 * > A tag name must be valid ASCII and may contain lowercase and
38 * > uppercase letters, digits, underscores, periods and dashes. A tag
39 * > name may not start with a period or a dash and may contain a
40 * > maximum of 128 characters.
41 *
42 * @param name Name component to clean up.
43 * @param hubOwner If `true` only allow characters valid for a Docker Hub user/org
44 * @return Valid Docker image name component.
45 */
46function cleanImageName(name, hubOwner = false) {
47 let clean = name.toLocaleLowerCase()
48 .replace(/^[^a-z0-9]+/, "").replace(/[^a-z0-9]+$/, "")
49 .replace(/[^-_/.a-z0-9]+/g, "");
50 if (hubOwner) {
51 clean = clean.replace(/[^a-z0-9]+/g, "");
52 }
53 if (clean.length > 128) {
54 clean = clean.substring(0, 128).replace(/[^a-z0-9]+$/, "");
55 }
56 return clean;
57}
58exports.cleanImageName = cleanImageName;
59//# sourceMappingURL=name.js.map
\No newline at end of file