verdaccio 6.7.4 → 6.9.0

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.
Files changed (180) hide show
  1. package/README.md +2 -2
  2. package/bin/verdaccio +5 -1
  3. package/build/_virtual/_rolldown/runtime.js +23 -0
  4. package/build/api/debug/index.js +23 -26
  5. package/build/api/debug/index.js.map +1 -0
  6. package/build/api/debug/index.mjs +19 -0
  7. package/build/api/debug/index.mjs.map +1 -0
  8. package/build/api/endpoint/api/dist-tags.js +62 -75
  9. package/build/api/endpoint/api/dist-tags.js.map +1 -0
  10. package/build/api/endpoint/api/dist-tags.mjs +56 -0
  11. package/build/api/endpoint/api/dist-tags.mjs.map +1 -0
  12. package/build/api/endpoint/api/package.js +91 -99
  13. package/build/api/endpoint/api/package.js.map +1 -0
  14. package/build/api/endpoint/api/package.mjs +89 -0
  15. package/build/api/endpoint/api/package.mjs.map +1 -0
  16. package/build/api/endpoint/api/ping.js +13 -11
  17. package/build/api/endpoint/api/ping.js.map +1 -0
  18. package/build/api/endpoint/api/ping.mjs +11 -0
  19. package/build/api/endpoint/api/ping.mjs.map +1 -0
  20. package/build/api/endpoint/api/publish.d.ts +2 -2
  21. package/build/api/endpoint/api/publish.js +278 -343
  22. package/build/api/endpoint/api/publish.js.map +1 -0
  23. package/build/api/endpoint/api/publish.mjs +280 -0
  24. package/build/api/endpoint/api/publish.mjs.map +1 -0
  25. package/build/api/endpoint/api/search.js +57 -105
  26. package/build/api/endpoint/api/search.js.map +1 -0
  27. package/build/api/endpoint/api/search.mjs +54 -0
  28. package/build/api/endpoint/api/search.mjs.map +1 -0
  29. package/build/api/endpoint/api/star.js +60 -71
  30. package/build/api/endpoint/api/star.js.map +1 -0
  31. package/build/api/endpoint/api/star.mjs +54 -0
  32. package/build/api/endpoint/api/star.mjs.map +1 -0
  33. package/build/api/endpoint/api/stars.js +40 -40
  34. package/build/api/endpoint/api/stars.js.map +1 -0
  35. package/build/api/endpoint/api/stars.mjs +35 -0
  36. package/build/api/endpoint/api/stars.mjs.map +1 -0
  37. package/build/api/endpoint/api/user.js +69 -82
  38. package/build/api/endpoint/api/user.js.map +1 -0
  39. package/build/api/endpoint/api/user.mjs +64 -0
  40. package/build/api/endpoint/api/user.mjs.map +1 -0
  41. package/build/api/endpoint/api/v1/profile.js +50 -67
  42. package/build/api/endpoint/api/v1/profile.js.map +1 -0
  43. package/build/api/endpoint/api/v1/profile.mjs +46 -0
  44. package/build/api/endpoint/api/v1/profile.mjs.map +1 -0
  45. package/build/api/endpoint/api/v1/search.d.ts +2 -1
  46. package/build/api/endpoint/api/v1/search.js +149 -168
  47. package/build/api/endpoint/api/v1/search.js.map +1 -0
  48. package/build/api/endpoint/api/v1/search.mjs +154 -0
  49. package/build/api/endpoint/api/v1/search.mjs.map +1 -0
  50. package/build/api/endpoint/api/v1/token.js +122 -155
  51. package/build/api/endpoint/api/v1/token.js.map +1 -0
  52. package/build/api/endpoint/api/v1/token.mjs +119 -0
  53. package/build/api/endpoint/api/v1/token.mjs.map +1 -0
  54. package/build/api/endpoint/api/whoami.js +13 -13
  55. package/build/api/endpoint/api/whoami.js.map +1 -0
  56. package/build/api/endpoint/api/whoami.mjs +11 -0
  57. package/build/api/endpoint/api/whoami.mjs.map +1 -0
  58. package/build/api/endpoint/index.js +58 -67
  59. package/build/api/endpoint/index.js.map +1 -0
  60. package/build/api/endpoint/index.mjs +54 -0
  61. package/build/api/endpoint/index.mjs.map +1 -0
  62. package/build/api/index.js +85 -106
  63. package/build/api/index.js.map +1 -0
  64. package/build/api/index.mjs +78 -0
  65. package/build/api/index.mjs.map +1 -0
  66. package/build/api/middleware.js +60 -54
  67. package/build/api/middleware.js.map +1 -0
  68. package/build/api/middleware.mjs +55 -0
  69. package/build/api/middleware.mjs.map +1 -0
  70. package/build/api/web/api/index.js +25 -29
  71. package/build/api/web/api/index.js.map +1 -0
  72. package/build/api/web/api/index.mjs +23 -0
  73. package/build/api/web/api/index.mjs.map +1 -0
  74. package/build/api/web/api/package.js +142 -161
  75. package/build/api/web/api/package.js.map +1 -0
  76. package/build/api/web/api/package.mjs +139 -0
  77. package/build/api/web/api/package.mjs.map +1 -0
  78. package/build/api/web/api/search.js +37 -50
  79. package/build/api/web/api/search.js.map +1 -0
  80. package/build/api/web/api/search.mjs +36 -0
  81. package/build/api/web/api/search.mjs.map +1 -0
  82. package/build/api/web/api/user.js +52 -73
  83. package/build/api/web/api/user.js.map +1 -0
  84. package/build/api/web/api/user.mjs +49 -0
  85. package/build/api/web/api/user.mjs.map +1 -0
  86. package/build/api/web/api/utils.js +9 -10
  87. package/build/api/web/api/utils.js.map +1 -0
  88. package/build/api/web/api/utils.mjs +11 -0
  89. package/build/api/web/api/utils.mjs.map +1 -0
  90. package/build/api/web/index.js +76 -88
  91. package/build/api/web/index.js.map +1 -0
  92. package/build/api/web/index.mjs +70 -0
  93. package/build/api/web/index.mjs.map +1 -0
  94. package/build/index.js +43 -60
  95. package/build/index.mjs +5 -0
  96. package/build/lib/auth-utils.js +12 -14
  97. package/build/lib/auth-utils.js.map +1 -0
  98. package/build/lib/auth-utils.mjs +11 -0
  99. package/build/lib/auth-utils.mjs.map +1 -0
  100. package/build/lib/bootstrap.js +125 -157
  101. package/build/lib/bootstrap.js.map +1 -0
  102. package/build/lib/bootstrap.mjs +125 -0
  103. package/build/lib/bootstrap.mjs.map +1 -0
  104. package/build/lib/cli/cli.js +22 -24
  105. package/build/lib/cli/cli.js.map +1 -0
  106. package/build/lib/cli/cli.mjs +23 -0
  107. package/build/lib/cli/cli.mjs.map +1 -0
  108. package/build/lib/cli/commands/info.js +34 -25
  109. package/build/lib/cli/commands/info.js.map +1 -0
  110. package/build/lib/cli/commands/info.mjs +32 -0
  111. package/build/lib/cli/commands/info.mjs.map +1 -0
  112. package/build/lib/cli/commands/init.js +61 -79
  113. package/build/lib/cli/commands/init.js.map +1 -0
  114. package/build/lib/cli/commands/init.mjs +70 -0
  115. package/build/lib/cli/commands/init.mjs.map +1 -0
  116. package/build/lib/cli/commands/version.js +14 -16
  117. package/build/lib/cli/commands/version.js.map +1 -0
  118. package/build/lib/cli/commands/version.mjs +14 -0
  119. package/build/lib/cli/commands/version.mjs.map +1 -0
  120. package/build/lib/cli/utils.d.ts +2 -2
  121. package/build/lib/cli/utils.js +16 -15
  122. package/build/lib/cli/utils.js.map +1 -0
  123. package/build/lib/cli/utils.mjs +14 -0
  124. package/build/lib/cli/utils.mjs.map +1 -0
  125. package/build/lib/cli.js +11 -14
  126. package/build/lib/cli.js.map +1 -0
  127. package/build/lib/cli.mjs +12 -0
  128. package/build/lib/cli.mjs.map +1 -0
  129. package/build/lib/config.js +19 -21
  130. package/build/lib/config.js.map +1 -0
  131. package/build/lib/config.mjs +16 -0
  132. package/build/lib/config.mjs.map +1 -0
  133. package/build/lib/constants.js +100 -93
  134. package/build/lib/constants.js.map +1 -0
  135. package/build/lib/constants.mjs +28 -0
  136. package/build/lib/constants.mjs.map +1 -0
  137. package/build/lib/experiments.js +19 -27
  138. package/build/lib/experiments.js.map +1 -0
  139. package/build/lib/experiments.mjs +20 -0
  140. package/build/lib/experiments.mjs.map +1 -0
  141. package/build/lib/local-storage.d.ts +5 -2
  142. package/build/lib/local-storage.js +606 -757
  143. package/build/lib/local-storage.js.map +1 -0
  144. package/build/lib/local-storage.mjs +600 -0
  145. package/build/lib/local-storage.mjs.map +1 -0
  146. package/build/lib/logger/index.js +24 -19
  147. package/build/lib/logger/index.js.map +1 -0
  148. package/build/lib/logger/index.mjs +17 -0
  149. package/build/lib/logger/index.mjs.map +1 -0
  150. package/build/lib/metadata-utils.js +24 -36
  151. package/build/lib/metadata-utils.js.map +1 -0
  152. package/build/lib/metadata-utils.mjs +23 -0
  153. package/build/lib/metadata-utils.mjs.map +1 -0
  154. package/build/lib/run-server.js +83 -114
  155. package/build/lib/run-server.js.map +1 -0
  156. package/build/lib/run-server.mjs +82 -0
  157. package/build/lib/run-server.mjs.map +1 -0
  158. package/build/lib/storage-utils.d.ts +120 -8
  159. package/build/lib/storage-utils.js +390 -251
  160. package/build/lib/storage-utils.js.map +1 -0
  161. package/build/lib/storage-utils.mjs +400 -0
  162. package/build/lib/storage-utils.mjs.map +1 -0
  163. package/build/lib/storage.d.ts +127 -57
  164. package/build/lib/storage.js +674 -681
  165. package/build/lib/storage.js.map +1 -0
  166. package/build/lib/storage.mjs +665 -0
  167. package/build/lib/storage.mjs.map +1 -0
  168. package/build/lib/up-storage.js +490 -599
  169. package/build/lib/up-storage.js.map +1 -0
  170. package/build/lib/up-storage.mjs +484 -0
  171. package/build/lib/up-storage.mjs.map +1 -0
  172. package/build/lib/uplink-util.js +21 -31
  173. package/build/lib/uplink-util.js.map +1 -0
  174. package/build/lib/uplink-util.mjs +24 -0
  175. package/build/lib/uplink-util.mjs.map +1 -0
  176. package/build/lib/utils.js +172 -231
  177. package/build/lib/utils.js.map +1 -0
  178. package/build/lib/utils.mjs +188 -0
  179. package/build/lib/utils.mjs.map +1 -0
  180. package/package.json +105 -100
@@ -0,0 +1,600 @@
1
+ import { API_ERROR, DIST_TAGS, HTTP_STATUS, STORAGE, SUPPORT_ERRORS, USERS } from "./constants.mjs";
2
+ import { ErrorCode, isObject, tagVersion } from "./utils.mjs";
3
+ import { cleanUpReadme, generatePackageTemplate, generateRevision, getLatestReadme, normalizePackage, prepareSearchPackage } from "./storage-utils.mjs";
4
+ import _ from "lodash";
5
+ import { createTarballHash, getLatestVersion, normalizeContributors, validateName } from "@verdaccio/utils";
6
+ import createDebug from "debug";
7
+ import assert from "assert";
8
+ import { ReadTarball, UploadTarball } from "@verdaccio/streams";
9
+ //#region src/lib/local-storage.ts
10
+ var debug = createDebug("verdaccio:local-storage");
11
+ /**
12
+ * Implements Storage interface (same for storage.js, local-storage.js, up-storage.js).
13
+ */
14
+ var LocalStorage = class {
15
+ config;
16
+ storagePlugin;
17
+ logger;
18
+ constructor(config, logger, localStorage) {
19
+ this.logger = logger;
20
+ this.config = config;
21
+ this.storagePlugin = localStorage;
22
+ }
23
+ addPackage(name, pkg, callback) {
24
+ const storage = this._getLocalStorage(name);
25
+ if (_.isNil(storage)) return callback(ErrorCode.getNotFound("this package cannot be added"));
26
+ storage.createPackage(name, generatePackageTemplate(name), (err) => {
27
+ if (_.isNull(err) === false && (err.code === STORAGE.FILE_EXIST_ERROR || err.code === HTTP_STATUS.CONFLICT)) return callback(ErrorCode.getConflict());
28
+ const latest = getLatestVersion(pkg);
29
+ if (_.isNil(latest) === false && pkg.versions[latest]) return callback(null, pkg.versions[latest]);
30
+ return callback();
31
+ });
32
+ }
33
+ /**
34
+ * Remove package.
35
+ * @param {*} name
36
+ * @param {*} callback
37
+ * @return {Function}
38
+ */
39
+ removePackage(name, callback) {
40
+ const storage = this._getLocalStorage(name);
41
+ debug("[storage] removing package %o", name);
42
+ if (_.isNil(storage)) return callback(ErrorCode.getNotFound());
43
+ storage.readPackage(name, (err, data) => {
44
+ if (_.isNil(err) === false) {
45
+ if (err.code === STORAGE.NO_SUCH_FILE_ERROR || err.code === HTTP_STATUS.NOT_FOUND) return callback(ErrorCode.getNotFound());
46
+ return callback(err);
47
+ }
48
+ data = normalizePackage(data);
49
+ this.storagePlugin.remove(name, (removeFailed) => {
50
+ if (removeFailed) {
51
+ this.logger.error({ name }, `[storage/removePackage] the database is locked, removed has failed for @{name}`);
52
+ return callback(ErrorCode.getBadData(removeFailed.message));
53
+ }
54
+ storage.deletePackage(STORAGE.PACKAGE_FILE_NAME, (err) => {
55
+ if (err) return callback(err);
56
+ const attachments = Object.keys(data._attachments);
57
+ this._deleteAttachments(storage, attachments, callback);
58
+ });
59
+ });
60
+ });
61
+ }
62
+ /**
63
+ * Synchronize remote package info with the local one
64
+ * @param {*} name
65
+ * @param {*} packageInfo
66
+ * @param {*} callback
67
+ */
68
+ updateVersions(name, packageInfo, callback) {
69
+ this._readCreatePackage(name, (err, packageLocalJson) => {
70
+ if (err) return callback(err);
71
+ let change = false;
72
+ packageLocalJson.readme = getLatestReadme(packageInfo);
73
+ if (packageInfo.readme !== packageLocalJson.readme) change = true;
74
+ for (const versionId in packageInfo.versions) if (_.isNil(packageLocalJson.versions[versionId])) {
75
+ let version = packageInfo.versions[versionId];
76
+ version = cleanUpReadme(version);
77
+ version.contributors = normalizeContributors(version.contributors);
78
+ change = true;
79
+ packageLocalJson.versions[versionId] = version;
80
+ if (version.dist && version.dist.tarball) {
81
+ let urlObject;
82
+ try {
83
+ urlObject = new URL(version.dist.tarball, "http://localhost");
84
+ } catch (err) {
85
+ this.logger.warn({
86
+ name,
87
+ version: versionId,
88
+ tarball: version.dist.tarball,
89
+ err: err.message
90
+ }, "skipping distfile for @{name}@@{version}: invalid tarball url @{tarball} (@{err})");
91
+ continue;
92
+ }
93
+ const filename = urlObject.pathname.replace(/^.*\//, "");
94
+ if (_.isNil(packageLocalJson._distfiles[filename])) {
95
+ const hash = packageLocalJson._distfiles[filename] = {
96
+ url: version.dist.tarball,
97
+ sha: version.dist.shasum
98
+ };
99
+ const upLink = version[Symbol.for("__verdaccio_uplink")];
100
+ if (_.isNil(upLink) === false) this._updateUplinkToRemoteProtocol(hash, upLink);
101
+ }
102
+ }
103
+ }
104
+ for (const tag in packageInfo[DIST_TAGS]) if (!packageLocalJson["dist-tags"][tag] || packageLocalJson["dist-tags"][tag] !== packageInfo["dist-tags"][tag]) {
105
+ change = true;
106
+ packageLocalJson[DIST_TAGS][tag] = packageInfo[DIST_TAGS][tag];
107
+ }
108
+ for (const up in packageInfo._uplinks) if (Object.prototype.hasOwnProperty.call(packageInfo._uplinks, up)) {
109
+ if (!isObject(packageLocalJson._uplinks[up]) || packageInfo._uplinks[up].etag !== packageLocalJson._uplinks[up].etag || packageInfo._uplinks[up].fetched !== packageLocalJson._uplinks[up].fetched) {
110
+ change = true;
111
+ packageLocalJson._uplinks[up] = packageInfo._uplinks[up];
112
+ }
113
+ }
114
+ if ("time" in packageInfo && !_.isEqual(packageLocalJson.time, packageInfo.time)) {
115
+ packageLocalJson.time = packageInfo.time;
116
+ change = true;
117
+ }
118
+ if (change) {
119
+ debug("updating package %o info", name);
120
+ this._writePackage(name, packageLocalJson, function(err) {
121
+ callback(err, packageLocalJson);
122
+ });
123
+ } else callback(null, packageLocalJson);
124
+ });
125
+ }
126
+ /**
127
+ * Add a new version to a previous local package.
128
+ * @param {*} name
129
+ * @param {*} version
130
+ * @param {*} metadata
131
+ * @param {*} tag
132
+ * @param {*} callback
133
+ */
134
+ addVersion(name, version, metadata, tag, callback) {
135
+ this._updatePackage(name, (data, cb) => {
136
+ data.readme = metadata.readme;
137
+ metadata = cleanUpReadme(metadata);
138
+ metadata.contributors = normalizeContributors(metadata.contributors);
139
+ if (data.versions[version] != null) return cb(ErrorCode.getConflict());
140
+ if (isObject(metadata.dist) && _.isString(metadata.dist.tarball)) {
141
+ const tarball = metadata.dist.tarball.replace(/.*\//, "");
142
+ if (isObject(data._attachments[tarball])) {
143
+ if (_.isNil(data._attachments[tarball].shasum) === false && _.isNil(metadata.dist.shasum) === false) {
144
+ if (data._attachments[tarball].shasum != metadata.dist.shasum) {
145
+ const errorMessage = `shasum error, ${data._attachments[tarball].shasum} != ${metadata.dist.shasum}`;
146
+ return cb(ErrorCode.getBadRequest(errorMessage));
147
+ }
148
+ }
149
+ const currentDate = (/* @__PURE__ */ new Date()).toISOString();
150
+ if (_.isNil(data.time)) data.time = {};
151
+ data.time["modified"] = currentDate;
152
+ if ("created" in data.time === false) data.time.created = currentDate;
153
+ data.time[version] = currentDate;
154
+ data._attachments[tarball].version = version;
155
+ }
156
+ }
157
+ data.versions[version] = metadata;
158
+ tagVersion(data, version, tag);
159
+ this.storagePlugin.add(name, (addFailed) => {
160
+ if (addFailed) return cb(ErrorCode.getBadData(addFailed.message));
161
+ cb();
162
+ });
163
+ }, callback);
164
+ }
165
+ /**
166
+ * Merge a new list of tags for a local packages with the existing one.
167
+ * @param {*} pkgName
168
+ * @param {*} tags
169
+ * @param {*} callback
170
+ */
171
+ mergeTags(pkgName, tags, callback) {
172
+ this._updatePackage(pkgName, (data, cb) => {
173
+ for (const tag in tags) {
174
+ if (_.isNull(tags[tag])) {
175
+ delete data[DIST_TAGS][tag];
176
+ continue;
177
+ }
178
+ if (_.isNil(data.versions[tags[tag]])) return cb(this._getVersionNotFound());
179
+ const version = tags[tag];
180
+ tagVersion(data, version, tag);
181
+ }
182
+ cb(null);
183
+ }, callback);
184
+ }
185
+ /**
186
+ * Return version not found
187
+ * @return {String}
188
+ * @private
189
+ */
190
+ _getVersionNotFound() {
191
+ return ErrorCode.getNotFound(API_ERROR.VERSION_NOT_EXIST);
192
+ }
193
+ /**
194
+ * Return file no available
195
+ * @return {String}
196
+ * @private
197
+ */
198
+ _getFileNotAvailable() {
199
+ return ErrorCode.getNotFound("no such file available");
200
+ }
201
+ /**
202
+ * Update the package metadata, tags and attachments (tarballs).
203
+ * Note: Currently supports unpublishing and deprecation.
204
+ * @param {*} name
205
+ * @param {*} incomingPkg
206
+ * @param {*} revision
207
+ * @param {*} callback
208
+ * @return {Function}
209
+ */
210
+ changePackage(name, incomingPkg, revision, callback) {
211
+ if (!isObject(incomingPkg.versions) || !isObject(incomingPkg["dist-tags"])) {
212
+ this.logger.error({ name }, `changePackage bad data for @{name}`);
213
+ return callback(ErrorCode.getBadData());
214
+ }
215
+ debug("changePackage udapting package for %o", name);
216
+ this._updatePackage(name, (localData, cb) => {
217
+ for (const version in localData.versions) {
218
+ const incomingVersion = incomingPkg.versions[version];
219
+ if (_.isNil(incomingVersion)) {
220
+ this.logger.info({
221
+ name,
222
+ version
223
+ }, "unpublishing @{name}@@{version}");
224
+ delete localData.versions[version];
225
+ delete localData.time[version];
226
+ for (const file in localData._attachments) if (localData._attachments[file].version === version) delete localData._attachments[file].version;
227
+ } else if (Object.prototype.hasOwnProperty.call(incomingVersion, "deprecated")) {
228
+ const incomingDeprecated = incomingVersion.deprecated;
229
+ if (incomingDeprecated != localData.versions[version].deprecated) {
230
+ if (!incomingDeprecated) {
231
+ this.logger.info({
232
+ name,
233
+ version
234
+ }, "undeprecating @{name}@@{version}");
235
+ delete localData.versions[version].deprecated;
236
+ } else {
237
+ this.logger.info({
238
+ name,
239
+ version
240
+ }, "deprecating @{name}@@{version}");
241
+ localData.versions[version].deprecated = incomingDeprecated;
242
+ }
243
+ localData.time.modified = (/* @__PURE__ */ new Date()).toISOString();
244
+ }
245
+ }
246
+ }
247
+ localData[USERS] = incomingPkg[USERS];
248
+ localData[DIST_TAGS] = incomingPkg[DIST_TAGS];
249
+ cb(null);
250
+ }, function(err) {
251
+ if (err) return callback(err);
252
+ callback();
253
+ });
254
+ }
255
+ /**
256
+ * Remove a tarball.
257
+ * @param {*} name
258
+ * @param {*} filename
259
+ * @param {*} revision
260
+ * @param {*} callback
261
+ */
262
+ removeTarball(name, filename, revision, callback) {
263
+ assert(validateName(filename));
264
+ this._updatePackage(name, (data, cb) => {
265
+ if (data._attachments[filename]) {
266
+ delete data._attachments[filename];
267
+ cb(null);
268
+ } else cb(this._getFileNotAvailable());
269
+ }, (err) => {
270
+ if (err) return callback(err);
271
+ const storage = this._getLocalStorage(name);
272
+ if (storage) storage.deletePackage(filename, callback);
273
+ });
274
+ }
275
+ /**
276
+ * Add a tarball.
277
+ * @param {String} name
278
+ * @param {String} filename
279
+ * @param {DistFile} distFile distfile record to restore when the manifest
280
+ * lost it (storages written by other verdaccio versions); persisted
281
+ * together with the attachment registration once the upload succeeds
282
+ * @return {Stream}
283
+ */
284
+ addTarball(name, filename, distFile) {
285
+ assert(validateName(filename));
286
+ let length = 0;
287
+ const shaOneHash = createTarballHash();
288
+ const uploadStream = new UploadTarball({});
289
+ const _transform = uploadStream._transform;
290
+ const storage = this._getLocalStorage(name);
291
+ uploadStream.abort = function() {};
292
+ uploadStream.done = function() {};
293
+ uploadStream._transform = function(data, ...args) {
294
+ shaOneHash.update(data);
295
+ length += data.length;
296
+ const appliedData = [data, ...args];
297
+ _transform.apply(uploadStream, appliedData);
298
+ };
299
+ if (name === "__proto__") {
300
+ process.nextTick(() => {
301
+ uploadStream.emit("error", ErrorCode.getForbidden());
302
+ });
303
+ return uploadStream;
304
+ }
305
+ if (!storage) {
306
+ process.nextTick(() => {
307
+ uploadStream.emit("error", "can't upload this package");
308
+ });
309
+ return uploadStream;
310
+ }
311
+ const writeStream = storage.writeTarball(filename);
312
+ writeStream.on("error", (err) => {
313
+ if (err.code === STORAGE.FILE_EXIST_ERROR || err.code === HTTP_STATUS.CONFLICT) {
314
+ uploadStream.emit("error", ErrorCode.getConflict());
315
+ uploadStream.abort();
316
+ } else if (err.code === STORAGE.NO_SUCH_FILE_ERROR || err.code === HTTP_STATUS.NOT_FOUND) this.getPackageMetadata(name, function(_err) {
317
+ if (_err) uploadStream.emit("error", _err);
318
+ else uploadStream.emit("error", err);
319
+ });
320
+ else uploadStream.emit("error", err);
321
+ });
322
+ writeStream.on("open", function() {
323
+ uploadStream.emit("open");
324
+ });
325
+ writeStream.on("success", () => {
326
+ this._updatePackage(name, function updater(data, cb) {
327
+ data._attachments[filename] = { shasum: shaOneHash.digest("hex") };
328
+ if (_.isNil(distFile) === false && _.isNil(data._distfiles[filename])) {
329
+ debug("restoring missing distfile record for %o served by %o", filename, distFile.registry);
330
+ data._distfiles[filename] = distFile;
331
+ }
332
+ cb(null);
333
+ }, function(err) {
334
+ if (err) uploadStream.emit("error", err);
335
+ else uploadStream.emit("success");
336
+ });
337
+ });
338
+ uploadStream.abort = function() {
339
+ writeStream.abort();
340
+ };
341
+ uploadStream.done = function() {
342
+ if (!length) {
343
+ uploadStream.emit("error", ErrorCode.getBadData("refusing to accept zero-length file"));
344
+ writeStream.abort();
345
+ } else writeStream.done();
346
+ };
347
+ uploadStream.pipe(writeStream);
348
+ return uploadStream;
349
+ }
350
+ /**
351
+ * Get a tarball.
352
+ * @param {*} name
353
+ * @param {*} filename
354
+ * @return {ReadTarball}
355
+ */
356
+ getTarball(name, filename) {
357
+ assert(validateName(filename));
358
+ const storage = this._getLocalStorage(name);
359
+ if (_.isNil(storage)) return this._createFailureStreamResponse();
360
+ return this._streamSuccessReadTarBall(storage, filename);
361
+ }
362
+ /**
363
+ * Return a stream that emits a read failure.
364
+ * @private
365
+ * @return {ReadTarball}
366
+ */
367
+ _createFailureStreamResponse() {
368
+ const stream = new ReadTarball({});
369
+ process.nextTick(() => {
370
+ stream.emit("error", this._getFileNotAvailable());
371
+ });
372
+ return stream;
373
+ }
374
+ /**
375
+ * Return a stream that emits the tarball data
376
+ * @param {Object} storage
377
+ * @param {String} filename
378
+ * @private
379
+ * @return {ReadTarball}
380
+ */
381
+ _streamSuccessReadTarBall(storage, filename) {
382
+ const stream = new ReadTarball({});
383
+ const readTarballStream = storage.readTarball(filename);
384
+ const e404 = ErrorCode.getNotFound;
385
+ stream.abort = function() {
386
+ if (_.isNil(readTarballStream) === false) readTarballStream.abort();
387
+ };
388
+ readTarballStream.on("error", function(err) {
389
+ if (err.code === STORAGE.NO_SUCH_FILE_ERROR || err.code === HTTP_STATUS.NOT_FOUND) stream.emit("error", e404("no such file available"));
390
+ else stream.emit("error", err);
391
+ });
392
+ readTarballStream.on("content-length", function(content) {
393
+ stream.emit("content-length", content);
394
+ });
395
+ readTarballStream.on("open", function() {
396
+ stream.emit("open");
397
+ readTarballStream.pipe(stream);
398
+ });
399
+ return stream;
400
+ }
401
+ /**
402
+ * Retrieve a package by name.
403
+ * @param {*} name
404
+ * @param {*} callback
405
+ * @return {Function}
406
+ */
407
+ getPackageMetadata(name, callback = () => {}) {
408
+ const storage = this._getLocalStorage(name);
409
+ if (_.isNil(storage)) return callback(ErrorCode.getNotFound());
410
+ this._readPackage(name, storage, callback);
411
+ }
412
+ getPackageMetadataAsync(name) {
413
+ return new Promise((resolve, reject) => {
414
+ this.getPackageMetadata(name, (err, metadata) => {
415
+ if (err) return reject(err);
416
+ resolve(metadata);
417
+ });
418
+ });
419
+ }
420
+ updateVersionsAsync(name, packageInfo) {
421
+ return new Promise((resolve, reject) => {
422
+ this.updateVersions(name, packageInfo, (err, result) => {
423
+ if (err) return reject(err);
424
+ resolve(result);
425
+ });
426
+ });
427
+ }
428
+ /**
429
+ * Search a local package.
430
+ * @param {*} startKey
431
+ * @param {*} options
432
+ * @return {Function}
433
+ */
434
+ search(startKey, options) {
435
+ debug("options for search %o", options);
436
+ const stream = new ReadTarball({ objectMode: true });
437
+ this._searchEachPackage((item, cb) => {
438
+ if (item.time > parseInt(startKey, 10)) this.getPackageMetadata(item.name, (err, data) => {
439
+ if (err) return cb(err);
440
+ const result = prepareSearchPackage(data, new Date(item.time).toISOString());
441
+ if (_.isNil(result) === false) stream.push(result);
442
+ cb(null);
443
+ });
444
+ else cb(null);
445
+ }, function onEnd(err) {
446
+ if (err) {
447
+ stream.emit("error", err);
448
+ return;
449
+ }
450
+ stream.end();
451
+ });
452
+ return stream;
453
+ }
454
+ /**
455
+ * Retrieve a wrapper that provide access to the package location.
456
+ * @param {Object} pkgName package name.
457
+ * @return {Object}
458
+ */
459
+ _getLocalStorage(pkgName) {
460
+ return this.storagePlugin.getPackageStorage(pkgName);
461
+ }
462
+ /**
463
+ * Read a json file from storage.
464
+ * @param {Object} storage
465
+ * @param {Function} callback
466
+ */
467
+ _readPackage(name, storage, callback) {
468
+ storage.readPackage(name, (err, result) => {
469
+ if (err) {
470
+ if (err.code === STORAGE.NO_SUCH_FILE_ERROR || err.code === HTTP_STATUS.NOT_FOUND) return callback(ErrorCode.getNotFound());
471
+ return callback(this._internalError(err, STORAGE.PACKAGE_FILE_NAME, "error reading"));
472
+ }
473
+ callback(err, normalizePackage(result));
474
+ });
475
+ }
476
+ /**
477
+ * Walks through each package and calls `on_package` on them.
478
+ * @param {*} onPackage
479
+ * @param {*} onEnd
480
+ */
481
+ _searchEachPackage(onPackage, onEnd) {
482
+ if (_.isNil(this.storagePlugin.search)) {
483
+ this.logger.warn("plugin search not implemented yet");
484
+ onEnd();
485
+ } else this.storagePlugin.search(onPackage, onEnd, validateName);
486
+ }
487
+ /**
488
+ * Retrieve either a previous created local package or a boilerplate.
489
+ * @param {*} pkgName
490
+ * @param {*} callback
491
+ * @return {Function}
492
+ */
493
+ _readCreatePackage(pkgName, callback) {
494
+ const storage = this._getLocalStorage(pkgName);
495
+ if (_.isNil(storage)) {
496
+ this._createNewPackage(pkgName, callback);
497
+ return;
498
+ }
499
+ storage.readPackage(pkgName, (err, data) => {
500
+ if (_.isNil(err) === false) if (err.code === STORAGE.NO_SUCH_FILE_ERROR || err.code === HTTP_STATUS.NOT_FOUND) data = generatePackageTemplate(pkgName);
501
+ else return callback(this._internalError(err, STORAGE.PACKAGE_FILE_NAME, "error reading"));
502
+ callback(null, normalizePackage(data));
503
+ });
504
+ }
505
+ _createNewPackage(name, callback) {
506
+ return callback(null, normalizePackage(generatePackageTemplate(name)));
507
+ }
508
+ /**
509
+ * Handle internal error
510
+ * @param {*} err
511
+ * @param {*} file
512
+ * @param {*} message
513
+ * @return {Object} Error instance
514
+ */
515
+ _internalError(err, file, message) {
516
+ this.logger.error({
517
+ err,
518
+ file
519
+ }, `${message} @{file}: @{!err.message}`);
520
+ return ErrorCode.getInternalError();
521
+ }
522
+ /**
523
+ * @param {*} name package name
524
+ * @param {*} updateHandler function(package, cb) - update function
525
+ * @param {*} callback callback that gets invoked after it's all updated
526
+ * @return {Function}
527
+ */
528
+ _updatePackage(name, updateHandler, callback) {
529
+ const storage = this._getLocalStorage(name);
530
+ if (!storage) return callback(ErrorCode.getNotFound());
531
+ storage.updatePackage(name, updateHandler, this._writePackage.bind(this), normalizePackage, callback);
532
+ }
533
+ /**
534
+ * Update the revision (_rev) string for a package.
535
+ * @param {*} name
536
+ * @param {*} json
537
+ * @param {*} callback
538
+ * @return {Function}
539
+ */
540
+ _writePackage(name, json, callback) {
541
+ const storage = this._getLocalStorage(name);
542
+ if (_.isNil(storage)) return callback();
543
+ storage.savePackage(name, this._setDefaultRevision(json), callback);
544
+ }
545
+ _setDefaultRevision(json) {
546
+ if (_.isString(json._rev) === false) json._rev = STORAGE.DEFAULT_REVISION;
547
+ if (_.isNil(this.config._debug)) json._rev = generateRevision(json._rev);
548
+ return json;
549
+ }
550
+ _deleteAttachments(storage, attachments, callback) {
551
+ debug("[storage/_deleteAttachments] delete attachments total: %o", attachments?.length);
552
+ const unlinkNext = function(cb) {
553
+ if (_.isEmpty(attachments)) return cb();
554
+ const attachment = attachments.shift();
555
+ storage.deletePackage(attachment, function() {
556
+ unlinkNext(cb);
557
+ });
558
+ };
559
+ unlinkNext(function() {
560
+ storage.removePackage(function(err) {
561
+ callback(err);
562
+ });
563
+ });
564
+ }
565
+ /**
566
+ * Ensure the dist file remains as the same protocol
567
+ * @param {Object} hash metadata
568
+ * @param {String} upLinkKey registry key
569
+ * @private
570
+ */
571
+ _updateUplinkToRemoteProtocol(hash, upLinkKey) {
572
+ const tarballUrl = URL.parse(hash.url);
573
+ const uplinkUrl = URL.parse(this.config.uplinks[upLinkKey].url);
574
+ if (tarballUrl !== null && uplinkUrl !== null && uplinkUrl.host === tarballUrl.host) {
575
+ tarballUrl.protocol = uplinkUrl.protocol;
576
+ hash.registry = upLinkKey;
577
+ hash.url = tarballUrl.href;
578
+ }
579
+ }
580
+ async getSecret(config) {
581
+ const secretKey = await this.storagePlugin.getSecret();
582
+ return this.storagePlugin.setSecret(config.checkSecretKey(secretKey));
583
+ }
584
+ saveToken(token) {
585
+ if (_.isFunction(this.storagePlugin.saveToken) === false) return Promise.reject(ErrorCode.getCode(HTTP_STATUS.SERVICE_UNAVAILABLE, SUPPORT_ERRORS.PLUGIN_MISSING_INTERFACE));
586
+ return this.storagePlugin.saveToken(token);
587
+ }
588
+ deleteToken(user, tokenKey) {
589
+ if (_.isFunction(this.storagePlugin.deleteToken) === false) return Promise.reject(ErrorCode.getCode(HTTP_STATUS.SERVICE_UNAVAILABLE, SUPPORT_ERRORS.PLUGIN_MISSING_INTERFACE));
590
+ return this.storagePlugin.deleteToken(user, tokenKey);
591
+ }
592
+ readTokens(filter) {
593
+ if (_.isFunction(this.storagePlugin.readTokens) === false) return Promise.reject(ErrorCode.getCode(HTTP_STATUS.SERVICE_UNAVAILABLE, SUPPORT_ERRORS.PLUGIN_MISSING_INTERFACE));
594
+ return this.storagePlugin.readTokens(filter);
595
+ }
596
+ };
597
+ //#endregion
598
+ export { LocalStorage as default };
599
+
600
+ //# sourceMappingURL=local-storage.mjs.map