This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpm.cc
286 lines (256 loc) · 8.56 KB
/
npm.cc
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "filetransfer.hh"
#include "cache.hh"
#include "globals.hh"
#include "store-api.hh"
#include "types.hh"
#include "url-parts.hh"
#include "fetchers.hh"
#include "fetch-settings.hh"
#include <iostream>
#include <optional>
#include <nlohmann/json.hpp>
#include <fstream>
namespace nix::fetchers {
struct DownloadUrl
{
std::string url;
Headers headers;
};
const static std::string npmVersionRegexS =
"[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9._]+(\\+[a-zA-Z0-9._])?)?";
std::regex npmVersionRegex( npmVersionRegexS, std::regex::ECMAScript );
const static std::string npmEscapedIdentScopedRegexS =
"(@|%40)[^@/\%]+\%2[fF][^@/\%]+";
std::regex npmEscapedIdentScopedRegex(
npmEscapedIdentScopedRegexS, std::regex::ECMAScript
);
const static std::string npmUnescapedScopeRegexS = "@[^@/\%]+";
std::regex npmUnescapedScopeRegex(
npmUnescapedScopeRegexS, std::regex::ECMAScript
);
const static std::string npmUnescapedBnameRegexS = "[^@/\%]+";
std::regex npmUnescapedBnameRegex(
npmUnescapedBnameRegexS, std::regex::ECMAScript
);
struct NpmArchiveInputScheme : InputScheme
{
const std::string type() const { return "npm"; }
std::optional<Input> inputFromURL( const ParsedURL & url ) override
{
if ( url.scheme != type() ) return {};
if ( url.authority && ( url.authority != "registry.npmjs.org" ) )
{
throw BadURL( " '%s', only 'registry.npmjs.org' is supported",
url.url );
}
auto path = tokenizeString<std::vector<std::string>>( url.path, "/" );
std::string ident;
std::string version;
auto size = path.size();
/**
* @foo/bar/1.0.0
* baz/1.0.0
* %40foo%2Fbar/1.0.0
* @foo%2fbar/1.0.0
*/
if ( size == 3 )
{
if ( std::regex_match( path[0], npmUnescapedScopeRegex ) &&
std::regex_match( path[1], npmUnescapedBnameRegex ) &&
std::regex_match( path[2], npmVersionRegex ) )
{
ident = path[0] + "/" + path[1];
version = path[3];
}
else
{
throw BadURL(
"in URL '%s', '%s' is not a valid <SCOPE>[@/]<VERSION> URI",
url.url, url.path
);
}
}
else if ( size == 2 )
{
auto spat =
tokenizeString<std::vector<std::string>>( path[1], "@" );
/* baz/1.0.0, %40foo%2Fbar/1.0.0, @foo%2fbar/1.0.0 */
if ( std::regex_match( path[0], npmUnescapedBnameRegex ) &&
std::regex_match( path[1], npmVersionRegex ) )
{
ident = path[0];
version = path[1];
}
else if ( std::regex_match( path[0], npmEscapedIdentScopedRegex ) &&
std::regex_match( path[1], npmVersionRegex ) )
{
ident = std::regex_replace( path[0], std::regex( "%40" ), "@" );
ident = std::regex_replace(
ident, std::regex( "\%2[fF]" ), "/"
);
}
else if ( std::regex_match( path[0], npmUnescapedScopeRegex ) &&
std::regex_match( spat[0], npmUnescapedBnameRegex ) &&
std::regex_match( spat[1], npmVersionRegex ) )
{
ident = path[0] + "/" + spat[0];
version = spat[1];
}
else
{
throw BadURL(
"in URL '%s', '%s' is not a valid <SCOPE>[@/]<VERSION> URI",
url.url, url.path
);
}
}
else if ( size == 1 )
{
auto spat =
tokenizeString<std::vector<std::string>>( path[0], "@" );
if ( std::regex_match( spat[0], npmUnescapedBnameRegex ) &&
std::regex_match( spat[1], npmVersionRegex ) )
{
ident = spat[0];
version = spat[1];
}
else
{
throw BadURL(
"in URL '%s', '%s' is not a valid <SCOPE>[@/]<VERSION> URI",
url.url, url.path
);
}
}
else
{
throw BadURL( "URL '%s' is invalid", url.url );
}
Input input;
input.attrs.insert_or_assign( "type", type() );
input.attrs.insert_or_assign( "ident", ident );
input.attrs.insert_or_assign( "version", version );
for ( auto &[name, value] : url.query )
{
if ( name == "unpack" )
{
input.attrs.insert_or_assign(
"unpack",
Explicit<bool> { ( value != "0" ) && ( value != "false" ) }
);
}
}
return input;
}
std::optional<Input> inputFromAttrs(const Attrs & attrs) override
{
if ( maybeGetStrAttr( attrs, "type" ) != type() ) return {};
for ( auto & [name, value] : attrs )
{
if ( name != "type" &&
name != "ident" &&
name != "version" &&
name != "narHash" &&
name != "lastModified" &&
name != "sha512" &&
name != "unpack"
)
{
throw Error( "unsupported input attribute '%s'", name );
}
}
getStrAttr( attrs, "ident" );
getStrAttr( attrs, "version" );
maybeGetStrAttr( attrs, "sha512" );
maybeGetBoolAttr( attrs, "unpack" );
Input input;
input.attrs = attrs;
return input;
}
ParsedURL toURL( const Input & input ) override
{
auto ident = getStrAttr( input.attrs, "ident");
auto version = getStrAttr( input.attrs, "version");
auto path = ident + "/" + version;
auto unpack = maybeGetBoolAttr( input.attrs, "unpack" );
auto url = ParsedURL {
.scheme = type(),
.path = path,
};
if ( unpack.has_value() )
{
url.query.insert_or_assign( "unpack", ( *unpack ) ? "1" : "0" );
}
return url;
}
bool hasAllInfo( const Input & input ) override
{
return true;
//maybeGetBoolAttr( input.attrs, "unpack" ).has_value();
}
std::optional<std::string> getAccessToken(const std::string & host) const
{
auto tokens = fetchSettings.accessTokens.get();
if ( auto token = get( tokens, host ) )
{
return *token;
}
return {};
}
Headers makeHeadersWithAuthTokens( const std::string & host ) const
{
Headers headers;
auto accessToken = getAccessToken(host);
if ( accessToken )
{
auto hdr = std::pair<std::string, std::string>(
"Authorization", fmt( "token %s", *accessToken )
);
headers.push_back( hdr );
}
return headers;
}
DownloadUrl getDownloadUrl( const Input & input )
{
auto ident = getStrAttr( input.attrs, "ident" );
auto version = getStrAttr( input.attrs, "version" );
auto bname =
ident[0] == '@' ? ident.substr( ident.find_first_of( "/" ) + 1 )
: ident;
auto url = fmt(
"registry.npmjs.org/%s/-/%s-%s.tgz", ident, bname, version
);
// TODO
Headers headers = makeHeadersWithAuthTokens( "registry.npmjs.org" );
return DownloadUrl { url, headers };
}
std::pair<StorePath, Input> fetch(
ref<Store> store, const Input & _input
) override
{
Input input( _input );
auto ident = getStrAttr( input.attrs, "ident" );
auto version = getStrAttr( input.attrs, "version" );
const bool unpack =
maybeGetBoolAttr( input.attrs, "unpack" ).value_or( false );
auto bname =
ident[0] == '@' ? ident.substr( ident.find_first_of( "/" ) + 1 )
: ident;
auto url = getDownloadUrl( input );
auto name = bname + "-" + version + ( unpack ? "" : ".tgz" );
if ( unpack )
{
auto tree = downloadTarball( store, url.url, name, true ).first;
return { std::move( tree.storePath ), input };
}
else
{
auto file = downloadFile( store, url.url, name, true );
return { std::move( file.storePath ), input };
}
}
};
static auto rNpmArchiveInputScheme = OnStartup( [] {
registerInputScheme( std::make_unique<NpmArchiveInputScheme>() );
} );
} /* End `nix::fetchers' */