@verdaccio/store 9.0.0-next-9.21 → 9.0.0-next-9.22

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.
package/build/storage.js CHANGED
@@ -22,6 +22,9 @@ _verdaccio_search = require_runtime.__toESM(_verdaccio_search);
22
22
  let _verdaccio_tarball = require("@verdaccio/tarball");
23
23
  //#region src/storage.ts
24
24
  var debug$1 = (0, debug.default)("verdaccio:storage");
25
+ var noSuchFile = "ENOENT";
26
+ var resourceNotAvailable = "EAGAIN";
27
+ var PROTO_NAME = "__proto__";
25
28
  var Storage = class {
26
29
  localStorage;
27
30
  filters;
@@ -168,85 +171,105 @@ var Storage = class {
168
171
  if (cachedManifest?._distfiles && typeof cachedManifest?._distfiles[filename]?.url === "string") {
169
172
  debug$1("dist file found, using it %o", cachedManifest?._distfiles[filename].url);
170
173
  const distFile = cachedManifest._distfiles[filename];
171
- let current_length = 0;
172
- let expected_length;
173
- const passThroughRemoteStream = new node_stream.PassThrough();
174
- const proxy = this.getUpLinkForDistFile(name, distFile);
175
- const remoteStream = proxy.fetchTarball(distFile.url, {});
176
- remoteStream.on("request", async () => {
177
- try {
178
- debug$1("remote stream request");
179
- const storage = this.getPrivatePackageStorage(name);
180
- if (proxy.config.cache === true && storage) await (0, node_stream_promises.pipeline)(remoteStream, passThroughRemoteStream, await storage.writeTarball(filename, { signal }), { signal });
181
- else await (0, node_stream_promises.pipeline)(remoteStream, passThroughRemoteStream, { signal });
182
- } catch (err) {
183
- debug$1("error on pipeline downloading tarball for package %o", name);
184
- passThroughRemoteStream.emit("error", err);
185
- }
186
- });
187
- remoteStream.on("response", async (res) => {
188
- if (res.statusCode === _verdaccio_core.HTTP_STATUS.NOT_FOUND) {
189
- debug$1("remote stream response 404");
190
- passThroughRemoteStream.emit("error", _verdaccio_core.errorUtils.getNotFound(_verdaccio_core.errorUtils.API_ERROR.NOT_FILE_UPLINK));
191
- return;
192
- }
193
- if (res.statusCode === _verdaccio_core.HTTP_STATUS.UNAUTHORIZED) {
194
- debug$1("remote stream response 401");
195
- passThroughRemoteStream.emit("error", _verdaccio_core.errorUtils.getUnauthorized(_verdaccio_core.errorUtils.API_ERROR.UNAUTHORIZED_ACCESS));
196
- return;
197
- }
198
- if (!(res.statusCode >= _verdaccio_core.HTTP_STATUS.OK && res.statusCode < _verdaccio_core.HTTP_STATUS.MULTIPLE_CHOICES)) {
199
- debug$1("remote stream response %o", res.statusCode);
200
- passThroughRemoteStream.emit("error", _verdaccio_core.errorUtils.getInternalError(`bad uplink status code: ${res.statusCode}`));
201
- return;
202
- }
203
- if (res.headers[_verdaccio_core.HEADER_TYPE.CONTENT_LENGTH]) {
204
- expected_length = res.headers[_verdaccio_core.HEADER_TYPE.CONTENT_LENGTH];
205
- debug$1("remote stream response content length %o", expected_length);
206
- passThroughRemoteStream.emit(_verdaccio_core.HEADER_TYPE.CONTENT_LENGTH, res.headers[_verdaccio_core.HEADER_TYPE.CONTENT_LENGTH]);
207
- }
208
- }).on("downloadProgress", (progress) => {
209
- current_length = progress.transferred;
210
- if (typeof expected_length === "undefined" && progress.total) expected_length = progress.total;
211
- }).on("end", () => {
212
- if (expected_length && current_length != expected_length) {
213
- debug$1("stream end, but length mismatch %o %o", current_length, expected_length);
214
- passThroughRemoteStream.emit("error", _verdaccio_core.errorUtils.getInternalError(_verdaccio_core.API_ERROR.CONTENT_MISMATCH));
215
- }
216
- debug$1("remote stream end");
217
- }).on("error", (err) => {
218
- debug$1("remote stream error %o", err);
219
- passThroughRemoteStream.emit("error", err);
220
- });
221
- return passThroughRemoteStream;
174
+ return this.fetchTarballFromDistFile(name, filename, distFile, { signal });
222
175
  } else {
223
176
  debug$1("dist file not found, proceed update upstream");
224
- const passThroughRemoteStream = new node_stream.PassThrough();
225
177
  const [updatedManifest] = await this.syncUplinksMetadata(name, cachedManifest, { uplinksLook: true });
226
178
  const distFile = updatedManifest?._distfiles?.[filename];
227
179
  if (updatedManifest === null || !distFile) {
228
180
  debug$1("remote tarball not found");
229
181
  throw _verdaccio_core.errorUtils.getNotFound(_verdaccio_core.API_ERROR.NO_SUCH_FILE);
230
182
  }
231
- const proxy = this.getUpLinkForDistFile(name, distFile);
232
- const remoteStream = proxy.fetchTarball(distFile.url, {});
233
- remoteStream.on("response", async () => {
183
+ return this.fetchTarballFromDistFile(name, filename, distFile, { signal });
184
+ }
185
+ }
186
+ fetchTarballFromDistFile(name, filename, distFile, { signal }) {
187
+ let current_length = 0;
188
+ let expected_length;
189
+ let uplinkStatusError = null;
190
+ const passThroughRemoteStream = new node_stream.PassThrough();
191
+ const proxy = this.getUpLinkForDistFile(name, distFile);
192
+ const remoteStream = proxy.fetchTarball(distFile.url, { throwHttpErrors: false });
193
+ const readRemoteBody = () => new Promise((resolve) => {
194
+ const chunks = [];
195
+ remoteStream.on("data", (chunk) => chunks.push(chunk));
196
+ remoteStream.once("end", () => {
197
+ const raw = Buffer.concat(chunks);
198
+ if (raw.length === 0) {
199
+ resolve(void 0);
200
+ return;
201
+ }
234
202
  try {
235
- const storage = this.getPrivatePackageStorage(name);
236
- if (proxy.config.cache === true && storage) {
237
- debug$1("cache remote tarball enabled");
238
- await (0, node_stream_promises.pipeline)(remoteStream, passThroughRemoteStream, await storage.writeTarball(filename, { signal }), { signal });
239
- } else {
240
- debug$1("cache remote tarball disabled");
241
- await (0, node_stream_promises.pipeline)(remoteStream, passThroughRemoteStream, { signal });
242
- }
243
- } catch (err) {
244
- debug$1("error on pipeline downloading tarball for package %o", name);
245
- passThroughRemoteStream.emit("error", err);
203
+ resolve(JSON.parse(raw.toString("utf8")));
204
+ } catch {
205
+ resolve(void 0);
246
206
  }
247
207
  });
248
- return passThroughRemoteStream;
249
- }
208
+ remoteStream.once("error", () => resolve(void 0));
209
+ });
210
+ remoteStream.on("response", async (res) => {
211
+ if (res.statusCode === _verdaccio_core.HTTP_STATUS.NOT_FOUND) {
212
+ debug$1("remote stream response 404");
213
+ remoteStream.resume();
214
+ uplinkStatusError = _verdaccio_core.errorUtils.getNotFound(_verdaccio_core.errorUtils.API_ERROR.NOT_FILE_UPLINK);
215
+ passThroughRemoteStream.emit("error", uplinkStatusError);
216
+ return;
217
+ }
218
+ if (res.statusCode === _verdaccio_core.HTTP_STATUS.UNAUTHORIZED) {
219
+ debug$1("remote stream response 401");
220
+ remoteStream.resume();
221
+ uplinkStatusError = _verdaccio_core.errorUtils.getUnauthorized(_verdaccio_core.errorUtils.API_ERROR.UNAUTHORIZED_ACCESS);
222
+ passThroughRemoteStream.emit("error", uplinkStatusError);
223
+ return;
224
+ }
225
+ if (res.statusCode === _verdaccio_core.HTTP_STATUS.FORBIDDEN) {
226
+ debug$1("remote stream response 403");
227
+ const body = await readRemoteBody();
228
+ const message = body?.detail || body?.title || body?.message || "tarball not available: forbidden by uplink";
229
+ uplinkStatusError = _verdaccio_core.errorUtils.getForbidden(message);
230
+ passThroughRemoteStream.emit("error", uplinkStatusError);
231
+ return;
232
+ }
233
+ if (!(res.statusCode >= _verdaccio_core.HTTP_STATUS.OK && res.statusCode < _verdaccio_core.HTTP_STATUS.MULTIPLE_CHOICES)) {
234
+ debug$1("remote stream response %o", res.statusCode);
235
+ remoteStream.resume();
236
+ uplinkStatusError = _verdaccio_core.errorUtils.getInternalError(`bad uplink status code: ${res.statusCode}`);
237
+ passThroughRemoteStream.emit("error", uplinkStatusError);
238
+ return;
239
+ }
240
+ if (res.headers[_verdaccio_core.HEADER_TYPE.CONTENT_LENGTH]) {
241
+ expected_length = res.headers[_verdaccio_core.HEADER_TYPE.CONTENT_LENGTH];
242
+ debug$1("remote stream response content length %o", expected_length);
243
+ passThroughRemoteStream.emit(_verdaccio_core.HEADER_TYPE.CONTENT_LENGTH, res.headers[_verdaccio_core.HEADER_TYPE.CONTENT_LENGTH]);
244
+ }
245
+ try {
246
+ debug$1("remote stream response ok, starting pipeline");
247
+ const storage = this.getPrivatePackageStorage(name);
248
+ if (proxy.config.cache === true && storage) {
249
+ const localStorageWriteStream = await storage.writeTarball(filename, { signal });
250
+ await (0, node_stream_promises.pipeline)(remoteStream, passThroughRemoteStream, localStorageWriteStream, { signal });
251
+ } else await (0, node_stream_promises.pipeline)(remoteStream, passThroughRemoteStream, { signal });
252
+ } catch (err) {
253
+ if (uplinkStatusError) return;
254
+ debug$1("error on pipeline downloading tarball for package %o", name);
255
+ passThroughRemoteStream.emit("error", err);
256
+ }
257
+ }).on("downloadProgress", (progress) => {
258
+ current_length = progress.transferred;
259
+ if (typeof expected_length === "undefined" && progress.total) expected_length = progress.total;
260
+ }).on("end", () => {
261
+ if (uplinkStatusError) return;
262
+ if (expected_length && current_length != expected_length) {
263
+ debug$1("stream end, but length mismatch %o %o", current_length, expected_length);
264
+ passThroughRemoteStream.emit("error", _verdaccio_core.errorUtils.getInternalError(_verdaccio_core.API_ERROR.CONTENT_MISMATCH));
265
+ }
266
+ debug$1("remote stream end");
267
+ }).on("error", (err) => {
268
+ if (uplinkStatusError) return;
269
+ debug$1("remote stream error %o", err);
270
+ passThroughRemoteStream.emit("error", err);
271
+ });
272
+ return passThroughRemoteStream;
250
273
  }
251
274
  /**
252
275
  *
@@ -1318,6 +1341,9 @@ var Storage = class {
1318
1341
  }
1319
1342
  };
1320
1343
  //#endregion
1344
+ exports.PROTO_NAME = PROTO_NAME;
1321
1345
  exports.Storage = Storage;
1346
+ exports.noSuchFile = noSuchFile;
1347
+ exports.resourceNotAvailable = resourceNotAvailable;
1322
1348
 
1323
1349
  //# sourceMappingURL=storage.js.map