miniflare 0.0.0-e2e6912bc → 0.0.0-e2f5756c2

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.
@@ -8776,7 +8776,8 @@ var AssetsManifest = class {
8776
8776
  // ../workers-shared/asset-worker/src/configuration.ts
8777
8777
  var applyConfigurationDefaults = (configuration) => ({
8778
8778
  html_handling: configuration?.html_handling ?? "auto-trailing-slash",
8779
- not_found_handling: configuration?.not_found_handling ?? "none"
8779
+ not_found_handling: configuration?.not_found_handling ?? "none",
8780
+ serve_directly: configuration?.serve_directly ?? !0
8780
8781
  });
8781
8782
 
8782
8783
  // ../workers-shared/asset-worker/src/responses.ts
@@ -8848,19 +8849,25 @@ function isCacheable(request) {
8848
8849
  }
8849
8850
 
8850
8851
  // ../workers-shared/asset-worker/src/handler.ts
8851
- var handleRequest = async (request, configuration, exists, getByETag) => {
8852
- let { pathname, search } = new URL(request.url), decodedPathname = decodePath(pathname), intent = await getIntent(decodedPathname, configuration, exists);
8852
+ var handleRequest = async (request, env, configuration, exists, getByETag) => {
8853
+ let { pathname, search } = new URL(request.url), decodedPathname = decodePath(pathname);
8854
+ decodedPathname = decodedPathname.replace(/\/+/g, "/");
8855
+ let intent = await getIntent(decodedPathname, configuration, exists);
8853
8856
  if (!intent)
8854
8857
  return new NotFoundResponse();
8855
8858
  let method = request.method.toUpperCase();
8856
8859
  if (!["GET", "HEAD"].includes(method))
8857
8860
  return new MethodNotAllowedResponse();
8858
8861
  let decodedDestination = intent.redirect ?? decodedPathname, encodedDestination = encodePath(decodedDestination);
8859
- if (encodedDestination !== pathname || intent.redirect)
8862
+ if (encodedDestination !== pathname && intent.asset || intent.redirect)
8860
8863
  return new TemporaryRedirectResponse(encodedDestination + search);
8861
8864
  if (!intent.asset)
8862
8865
  return new InternalServerErrorResponse(new Error("Unknown action"));
8863
- let asset = await getByETag(intent.asset.eTag), headers = getHeaders(intent.asset.eTag, asset.contentType, request), strongETag = `"${intent.asset.eTag}"`, weakETag = `W/${strongETag}`, ifNoneMatch = request.headers.get("If-None-Match") || "";
8866
+ let asset = await env.JAEGER.enterSpan("getByETag", async (span) => (span.setTags({
8867
+ pathname,
8868
+ eTag: intent.asset.eTag,
8869
+ status: intent.asset.status
8870
+ }), await getByETag(intent.asset.eTag))), headers = getHeaders(intent.asset.eTag, asset.contentType, request), strongETag = `"${intent.asset.eTag}"`, weakETag = `W/${strongETag}`, ifNoneMatch = request.headers.get("If-None-Match") || "";
8864
8871
  if ([weakETag, strongETag].includes(ifNoneMatch))
8865
8872
  return new NotModifiedResponse(null, { headers });
8866
8873
  let body = method === "HEAD" ? null : asset.readableStream;
@@ -9238,18 +9245,46 @@ var handleRequest = async (request, configuration, exists, getByETag) => {
9238
9245
  };
9239
9246
  }
9240
9247
  return null;
9241
- }, decodePath = (pathname) => pathname.split("/").map((x) => decodeURIComponent(x)).join("/"), encodePath = (pathname) => pathname.split("/").map((x) => encodeURIComponent(x)).join("/");
9248
+ }, decodePath = (pathname) => pathname.split("/").map((x) => {
9249
+ try {
9250
+ return decodeURIComponent(x);
9251
+ } catch {
9252
+ return x;
9253
+ }
9254
+ }).join("/"), encodePath = (pathname) => pathname.split("/").map((x) => {
9255
+ try {
9256
+ return encodeURIComponent(x);
9257
+ } catch {
9258
+ return x;
9259
+ }
9260
+ }).join("/");
9242
9261
 
9243
9262
  // ../workers-shared/asset-worker/src/utils/kv.ts
9244
- async function getAssetWithMetadataFromKV(assetsKVNamespace, assetKey, retries = 1) {
9263
+ async function getAssetWithMetadataFromKV(assetsKVNamespace, assetKey, sentry, retries = 1) {
9245
9264
  let attempts = 0;
9246
9265
  for (; attempts <= retries; )
9247
9266
  try {
9248
- return await assetsKVNamespace.getWithMetadata(assetKey, {
9249
- type: "stream",
9250
- cacheTtl: 31536e3
9251
- // 1 year
9252
- });
9267
+ let asset = await assetsKVNamespace.getWithMetadata(
9268
+ assetKey,
9269
+ {
9270
+ type: "stream",
9271
+ cacheTtl: 31536e3
9272
+ // 1 year
9273
+ }
9274
+ );
9275
+ if (asset.value === null) {
9276
+ let retriedAsset = await assetsKVNamespace.getWithMetadata(assetKey, {
9277
+ type: "stream",
9278
+ cacheTtl: 60
9279
+ // Minimum value allowed
9280
+ });
9281
+ return retriedAsset.value !== null && sentry && sentry.captureException(
9282
+ new Error(
9283
+ `Initial request for asset ${assetKey} failed, but subsequent request succeeded.`
9284
+ )
9285
+ ), retriedAsset;
9286
+ }
9287
+ return asset;
9253
9288
  } catch {
9254
9289
  if (attempts >= retries)
9255
9290
  throw new Error(
@@ -9261,12 +9296,41 @@ async function getAssetWithMetadataFromKV(assetsKVNamespace, assetKey, retries =
9261
9296
  }
9262
9297
  }
9263
9298
 
9299
+ // ../workers-shared/asset-worker/src/utils/mocks.ts
9300
+ function mockJaegerBindingSpan() {
9301
+ return {
9302
+ addLogs: () => {
9303
+ },
9304
+ setTags: () => {
9305
+ },
9306
+ end: () => {
9307
+ },
9308
+ isRecording: !0
9309
+ };
9310
+ }
9311
+ function mockJaegerBinding() {
9312
+ return {
9313
+ enterSpan: (_, span, ...args) => span(mockJaegerBindingSpan(), ...args),
9314
+ getSpanContext: () => ({
9315
+ traceId: "test-trace",
9316
+ spanId: "test-span",
9317
+ parentSpanId: "test-parent-span",
9318
+ traceFlags: 0
9319
+ }),
9320
+ runWithSpanContext: (_, callback, ...args) => callback(...args),
9321
+ traceId: "test-trace",
9322
+ spanId: "test-span",
9323
+ parentSpanId: "test-parent-span",
9324
+ cfTraceIdHeader: "test-trace:test-span:0"
9325
+ };
9326
+ }
9327
+
9264
9328
  // ../workers-shared/asset-worker/src/index.ts
9265
9329
  var src_default = class extends WorkerEntrypoint {
9266
9330
  async fetch(request) {
9267
9331
  let sentry, analytics = new Analytics(this.env.ANALYTICS), performance = new PerformanceTimer(this.env.UNSAFE_PERFORMANCE), startTimeMs = performance.now();
9268
9332
  try {
9269
- sentry = setupSentry(
9333
+ this.env.JAEGER || (this.env.JAEGER = mockJaegerBinding()), sentry = setupSentry(
9270
9334
  request,
9271
9335
  this.ctx,
9272
9336
  this.env.SENTRY_DSN,
@@ -9278,43 +9342,59 @@ var src_default = class extends WorkerEntrypoint {
9278
9342
  let colo = this.env.COLO_METADATA.coloId;
9279
9343
  sentry.setTag("colo", this.env.COLO_METADATA.coloId), sentry.setTag("metal", this.env.COLO_METADATA.metalId), sentry.setUser({ userAgent, colo });
9280
9344
  }
9281
- if (this.env.COLO_METADATA && this.env.VERSION_METADATA) {
9282
- let url = new URL(request.url);
9283
- analytics.setData({
9284
- coloId: this.env.COLO_METADATA.coloId,
9285
- metalId: this.env.COLO_METADATA.metalId,
9286
- coloTier: this.env.COLO_METADATA.coloTier,
9287
- coloRegion: this.env.COLO_METADATA.coloRegion,
9288
- version: this.env.VERSION_METADATA.id,
9289
- hostname: url.hostname,
9290
- htmlHandling: config.html_handling,
9291
- notFoundHandling: config.not_found_handling,
9292
- userAgent
9293
- });
9294
- }
9295
- return handleRequest(
9345
+ let url = new URL(request.url);
9346
+ return this.env.COLO_METADATA && this.env.VERSION_METADATA && analytics.setData({
9347
+ coloId: this.env.COLO_METADATA.coloId,
9348
+ metalId: this.env.COLO_METADATA.metalId,
9349
+ coloTier: this.env.COLO_METADATA.coloTier,
9350
+ coloRegion: this.env.COLO_METADATA.coloRegion,
9351
+ version: this.env.VERSION_METADATA.id,
9352
+ hostname: url.hostname,
9353
+ htmlHandling: config.html_handling,
9354
+ notFoundHandling: config.not_found_handling,
9355
+ userAgent
9356
+ }), this.env.JAEGER.enterSpan("handleRequest", async (span) => (span.setTags({
9357
+ hostname: url.hostname,
9358
+ eyeballPath: url.pathname,
9359
+ env: this.env.ENVIRONMENT,
9360
+ version: this.env.VERSION_METADATA?.id
9361
+ }), handleRequest(
9296
9362
  request,
9363
+ this.env,
9297
9364
  config,
9298
9365
  this.unstable_exists.bind(this),
9299
9366
  this.unstable_getByETag.bind(this)
9300
- );
9367
+ ))).catch((err) => this.handleError(sentry, analytics, err)).finally(() => this.submitMetrics(analytics, performance, startTimeMs));
9301
9368
  } catch (err) {
9369
+ let errorResponse = this.handleError(sentry, analytics, err);
9370
+ return this.submitMetrics(analytics, performance, startTimeMs), errorResponse;
9371
+ }
9372
+ }
9373
+ handleError(sentry, analytics, err) {
9374
+ try {
9302
9375
  let response = new InternalServerErrorResponse(err);
9303
9376
  return sentry && sentry.captureException(err), err instanceof Error && analytics.setData({ error: err.message }), response;
9304
- } finally {
9377
+ } catch (e) {
9378
+ return console.error("Error handling error", e), new InternalServerErrorResponse(e);
9379
+ }
9380
+ }
9381
+ submitMetrics(analytics, performance, startTimeMs) {
9382
+ try {
9305
9383
  analytics.setData({ requestTime: performance.now() - startTimeMs }), analytics.write();
9384
+ } catch (e) {
9385
+ console.error("Error submitting metrics", e);
9306
9386
  }
9307
9387
  }
9308
9388
  async unstable_canFetch(request) {
9309
- let url = new URL(request.url), method = request.method.toUpperCase(), decodedPathname = decodePath(url.pathname), intent = await getIntent(
9389
+ let url = new URL(request.url), decodedPathname = decodePath(url.pathname);
9390
+ return await getIntent(
9310
9391
  decodedPathname,
9311
9392
  {
9312
9393
  ...applyConfigurationDefaults(this.env.CONFIG),
9313
9394
  not_found_handling: "none"
9314
9395
  },
9315
9396
  this.unstable_exists.bind(this)
9316
- );
9317
- return intent && ["GET", "HEAD"].includes(method) ? new MethodNotAllowedResponse() : intent !== null;
9397
+ ) !== null;
9318
9398
  }
9319
9399
  async unstable_getByETag(eTag) {
9320
9400
  let asset = await getAssetWithMetadataFromKV(