-
-
Notifications
You must be signed in to change notification settings - Fork 938
/
index.ts
183 lines (161 loc) · 6.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/// <reference path="../../../__typings__/index.d.ts"/>
import {
depPathToFilename,
isAbsolute,
parse,
refToAbsolute,
refToRelative,
relative,
resolve,
tryGetPackageId,
} from '@pnpm/dependency-path'
test('isAbsolute()', () => {
expect(isAbsolute('/foo/1.0.0')).toBeFalsy()
expect(isAbsolute('registry.npmjs.org/foo/1.0.0')).toBeTruthy()
})
test('parse()', () => {
/* eslint-disable @typescript-eslint/no-explicit-any */
expect(() => parse(undefined as any)).toThrow(/got `undefined`/)
expect(() => parse({} as any)).toThrow(/got `object`/)
expect(() => parse(1 as any)).toThrow(/got `number`/)
/* eslint-enable @typescript-eslint/no-explicit-any */
expect(parse('/foo/1.0.0')).toStrictEqual({
host: undefined,
isAbsolute: false,
name: 'foo',
peersSuffix: undefined,
version: '1.0.0',
})
expect(parse('/@foo/bar/1.0.0')).toStrictEqual({
host: undefined,
isAbsolute: false,
name: '@foo/bar',
peersSuffix: undefined,
version: '1.0.0',
})
expect(parse('registry.npmjs.org/foo/1.0.0')).toStrictEqual({
host: 'registry.npmjs.org',
isAbsolute: true,
name: 'foo',
peersSuffix: undefined,
version: '1.0.0',
})
expect(parse('registry.npmjs.org/@foo/bar/1.0.0')).toStrictEqual({
host: 'registry.npmjs.org',
isAbsolute: true,
name: '@foo/bar',
peersSuffix: undefined,
version: '1.0.0',
})
expect(parse('github.com/kevva/is-positive')).toStrictEqual({
host: 'github.com',
isAbsolute: true,
})
expect(parse('example.com/foo/1.0.0')).toStrictEqual({
host: 'example.com',
isAbsolute: true,
name: 'foo',
peersSuffix: undefined,
version: '1.0.0',
})
expect(parse('example.com/foo/1.0.0_bar@2.0.0')).toStrictEqual({
host: 'example.com',
isAbsolute: true,
name: 'foo',
peersSuffix: 'bar@2.0.0',
version: '1.0.0',
})
expect(parse('/foo/1.0.0_@types+babel__core@7.1.14')).toStrictEqual({
host: undefined,
isAbsolute: false,
name: 'foo',
peersSuffix: '@types+babel__core@7.1.14',
version: '1.0.0',
})
expect(parse('example.com/foo/1.0.0(bar@2.0.0)')).toStrictEqual({
host: 'example.com',
isAbsolute: true,
name: 'foo',
peersSuffix: '(bar@2.0.0)',
version: '1.0.0',
})
expect(parse('/foo/1.0.0(@types/babel__core@7.1.14)(foo@1.0.0)')).toStrictEqual({
host: undefined,
isAbsolute: false,
name: 'foo',
peersSuffix: '(@types/babel__core@7.1.14)(foo@1.0.0)',
version: '1.0.0',
})
expect(parse('/@(-.-)/foo/1.0.0(@types/babel__core@7.1.14)(foo@1.0.0)')).toStrictEqual({
host: undefined,
isAbsolute: false,
name: '@(-.-)/foo',
peersSuffix: '(@types/babel__core@7.1.14)(foo@1.0.0)',
version: '1.0.0',
})
expect(() => parse('/foo/bar')).toThrow(/\/foo\/bar is an invalid relative dependency path/)
expect(parse('file:project(foo@1.0.0)')).toStrictEqual({
host: 'file:project(foo@1.0.0)',
isAbsolute: true,
})
})
test('refToAbsolute()', () => {
const registries = {
'@foo': 'http://foo.com/',
default: 'https://registry.npmjs.org/',
}
expect(refToAbsolute('1.0.0', 'foo', registries)).toEqual('registry.npmjs.org/foo/1.0.0')
expect(refToAbsolute('1.0.0', '@foo/foo', registries)).toEqual('foo.com/@foo/foo/1.0.0')
expect(refToAbsolute('registry.npmjs.org/foo/1.0.0', 'foo', registries)).toEqual('registry.npmjs.org/foo/1.0.0')
expect(refToAbsolute('/foo/1.0.0', 'foo', registries)).toEqual('registry.npmjs.org/foo/1.0.0')
expect(refToAbsolute('/@foo/foo/1.0.0', '@foo/foo', registries)).toEqual('foo.com/@foo/foo/1.0.0')
expect(refToAbsolute('/@foo/foo@1.0.0(@foo/bar@1.0.0)', '@foo/foo', registries)).toEqual('foo.com/@foo/foo@1.0.0(@foo/bar@1.0.0)')
expect(refToAbsolute('/@foo/foo@1.0.0(@foo/bar@1.0.0)(@foo/qar@1.0.0)', '@foo/foo', registries)).toEqual('foo.com/@foo/foo@1.0.0(@foo/bar@1.0.0)(@foo/qar@1.0.0)')
// linked dependencies don't have an absolute path
expect(refToAbsolute('link:../foo', 'foo', registries)).toBeNull()
})
test('refToRelative()', () => {
expect(refToRelative('/@most/multicast/1.3.0/most@1.7.3', '@most/multicast')).toEqual('/@most/multicast/1.3.0/most@1.7.3')
expect(refToRelative('/@most/multicast/1.3.0/most@1.7.3(@foo/bar@1.0.0)', '@most/multicast')).toEqual('/@most/multicast/1.3.0/most@1.7.3(@foo/bar@1.0.0)')
expect(refToRelative('/@most/multicast/1.3.0/most@1.7.3(@foo/bar@1.0.0)(@foo/qar@1.0.0)', '@most/multicast')).toEqual('/@most/multicast/1.3.0/most@1.7.3(@foo/bar@1.0.0)(@foo/qar@1.0.0)')
// linked dependencies don't have a relative path
expect(refToRelative('link:../foo', 'foo')).toBeNull()
expect(refToRelative('file:../tarball.tgz', 'foo')).toEqual('file:../tarball.tgz')
expect(refToRelative('1.3.0(@foo/bar@1.0.0)', '@qar/bar')).toEqual('/@qar/bar/1.3.0(@foo/bar@1.0.0)')
expect(refToRelative('1.3.0(@foo/bar@1.0.0)(@foo/qar@1.0.0)', '@qar/bar')).toEqual('/@qar/bar/1.3.0(@foo/bar@1.0.0)(@foo/qar@1.0.0)')
})
test('relative()', () => {
const registries = {
'@foo': 'http://localhost:4873/',
default: 'https://registry.npmjs.org/',
}
expect(relative(registries, 'foo', 'registry.npmjs.org/foo/1.0.0')).toEqual('/foo/1.0.0')
expect(relative(registries, '@foo/foo', 'localhost+4873/@foo/foo/1.0.0')).toEqual('/@foo/foo/1.0.0')
expect(relative(registries, 'foo', 'registry.npmjs.org/foo/1.0.0/PeLdniYiO858gXNY39o5wISKyw')).toEqual('/foo/1.0.0/PeLdniYiO858gXNY39o5wISKyw')
})
test('resolve()', () => {
const registries = {
'@bar': 'https://bar.com/',
default: 'https://foo.com/',
}
expect(resolve(registries, '/foo/1.0.0')).toEqual('foo.com/foo/1.0.0')
expect(resolve(registries, '/@bar/bar/1.0.0')).toEqual('bar.com/@bar/bar/1.0.0')
expect(resolve(registries, '/@qar/qar/1.0.0')).toEqual('foo.com/@qar/qar/1.0.0')
expect(resolve(registries, 'qar.com/foo/1.0.0')).toEqual('qar.com/foo/1.0.0')
})
test('depPathToFilename()', () => {
expect(depPathToFilename('/foo/1.0.0')).toBe('foo@1.0.0')
expect(depPathToFilename('/@foo/bar/1.0.0')).toBe('@foo+bar@1.0.0')
expect(depPathToFilename('github.com/something/foo/0000?v=1')).toBe('github.com+something+foo@0000+v=1')
expect(depPathToFilename('\\//:*?"<>|')).toBe('++@+++++++')
expect(depPathToFilename('/foo/1.0.0(react@16.0.0)(react-dom@16.0.0)')).toBe('foo@1.0.0_react@16.0.0_react-dom@16.0.0')
const filename = depPathToFilename('file:test/foo-1.0.0.tgz_foo@2.0.0')
expect(filename).toBe('file+test+foo-1.0.0.tgz_foo@2.0.0')
expect(filename).not.toContain(':')
expect(depPathToFilename('abcd/'.repeat(200))).toBe('abcd+abcd+abcd+abcd+abcd+abcd+abcd+abcd+abcd+abcd+_e5jega7r3xmarw3h6f277a3any')
expect(depPathToFilename('/JSONSteam/1.0.0')).toBe('JSONSteam@1.0.0_jmswpk4sf667aelr6wp2xd3p54')
})
test('tryGetPackageId', () => {
expect(tryGetPackageId({ default: 'https://registry.npmjs.org/' }, '/foo/1.0.0_@types+babel__core@7.1.14')).toEqual('registry.npmjs.org/foo/1.0.0')
expect(tryGetPackageId({ default: 'https://registry.npmjs.org/' }, '/foo/1.0.0(@types/babel__core@7.1.14)')).toEqual('registry.npmjs.org/foo/1.0.0')
})