@vltpkg/git-scp-url 1.0.0-rc.23 → 1.0.0-rc.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export declare const gitScpURL: (url: string) => URL | undefined;
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ const knownProtocols = new Set([
2
+ 'http:',
3
+ 'https:',
4
+ 'git:',
5
+ 'git+ssh:',
6
+ 'git+https:',
7
+ 'ssh:',
8
+ ]);
9
+ const memo = new Map();
10
+ // git+ssh:github.com:user/repo => git+ssh://github.com:user/repo
11
+ // git@github.com:user/repo => git+ssh://user@github.com:user/repo
12
+ const correctProtocol = (arg) => {
13
+ const firstColon = arg.indexOf(':');
14
+ const proto = arg.slice(0, firstColon + 1);
15
+ const doubleSlash = arg.indexOf('//');
16
+ if (knownProtocols.has(proto)) {
17
+ if (doubleSlash === firstColon + 1) {
18
+ return arg;
19
+ }
20
+ return proto + '//' + arg.slice(firstColon + 1);
21
+ }
22
+ const firstAt = arg.indexOf('@');
23
+ if (firstAt > -1) {
24
+ if (firstAt > firstColon) {
25
+ return `git+ssh://${arg}`;
26
+ }
27
+ else {
28
+ return arg;
29
+ }
30
+ // it would've parsed just fine otherwise, but just in case */
31
+ /* c8 ignore start */
32
+ }
33
+ return proto + '//' + arg.slice(firstColon + 1);
34
+ };
35
+ /* c8 ignore stop */
36
+ // git+ssh://github.com:user/repo => git+ssh://github.com/user/repo
37
+ const correctUrl = (url) => {
38
+ const firstAt = url.indexOf('@');
39
+ const lastHash = url.lastIndexOf('#');
40
+ let firstColon = url.indexOf(':');
41
+ let lastColon = url.lastIndexOf(':', lastHash > -1 ? lastHash : Infinity);
42
+ let corrected = url;
43
+ if (lastColon > firstAt) {
44
+ // the last : comes after the first @ (or there is no @)
45
+ // like it would in:
46
+ // proto://hostname.com:user/repo
47
+ // username@hostname.com:user/repo
48
+ // :password@hostname.com:user/repo
49
+ // username:password@hostname.com:user/repo
50
+ // proto://username@hostname.com:user/repo
51
+ // proto://:password@hostname.com:user/repo
52
+ // proto://username:password@hostname.com:user/repo
53
+ // then we replace the last : with a / to create a valid path
54
+ corrected =
55
+ url.slice(0, lastColon) + '/' + url.slice(lastColon + 1);
56
+ // // and we find our new : positions
57
+ firstColon = corrected.indexOf(':');
58
+ lastColon = corrected.lastIndexOf(':');
59
+ }
60
+ if (firstColon === -1 && !url.includes('//')) {
61
+ // we have no : at all
62
+ // as it would be in:
63
+ // username@hostname.com/user/repo
64
+ // then we prepend a protocol
65
+ corrected = `git+ssh://${corrected}`;
66
+ }
67
+ return corrected;
68
+ };
69
+ export const gitScpURL = (url) => {
70
+ const memoized = memo.get(url);
71
+ if (memoized)
72
+ return memoized;
73
+ try {
74
+ const result = new URL(url);
75
+ if (result.hostname) {
76
+ memo.set(url, result);
77
+ return result;
78
+ }
79
+ }
80
+ catch { }
81
+ try {
82
+ const result = new URL(correctUrl(correctProtocol(url)));
83
+ if (result.hostname) {
84
+ memo.set(url, result);
85
+ return result;
86
+ }
87
+ }
88
+ catch { }
89
+ return undefined;
90
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/git-scp-url",
3
3
  "description": "Parse scp-style git URLs like `git+ssh://host:some/path`",
4
- "version": "1.0.0-rc.23",
4
+ "version": "1.0.0-rc.24",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",