miniflare 0.0.0-e5ebdb143 → 0.0.0-e62b097bb

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.
@@ -8633,9 +8633,62 @@ var PerformanceTimer = class {
8633
8633
  }
8634
8634
  };
8635
8635
 
8636
+ // ../workers-shared/utils/responses.ts
8637
+ var OkResponse = class extends Response {
8638
+ constructor(body, init) {
8639
+ super(body, {
8640
+ ...init,
8641
+ status: 200
8642
+ });
8643
+ }
8644
+ }, NotFoundResponse = class extends Response {
8645
+ constructor(...[body, init]) {
8646
+ super(body, {
8647
+ ...init,
8648
+ status: 404,
8649
+ statusText: "Not Found"
8650
+ });
8651
+ }
8652
+ }, MethodNotAllowedResponse = class extends Response {
8653
+ constructor(...[body, init]) {
8654
+ super(body, {
8655
+ ...init,
8656
+ status: 405,
8657
+ statusText: "Method Not Allowed"
8658
+ });
8659
+ }
8660
+ }, InternalServerErrorResponse = class extends Response {
8661
+ constructor(err, init) {
8662
+ super(null, {
8663
+ ...init,
8664
+ status: 500
8665
+ });
8666
+ }
8667
+ }, NotModifiedResponse = class extends Response {
8668
+ constructor(...[_body, init]) {
8669
+ super(null, {
8670
+ ...init,
8671
+ status: 304,
8672
+ statusText: "Not Modified"
8673
+ });
8674
+ }
8675
+ }, TemporaryRedirectResponse = class extends Response {
8676
+ constructor(location, init) {
8677
+ super(null, {
8678
+ ...init,
8679
+ status: 307,
8680
+ statusText: "Temporary Redirect",
8681
+ headers: {
8682
+ ...init?.headers,
8683
+ Location: location
8684
+ }
8685
+ });
8686
+ }
8687
+ };
8688
+
8636
8689
  // ../workers-shared/utils/sentry.ts
8637
8690
  var import_toucan_js = __toESM(require_index_cjs());
8638
- function setupSentry(request, context, dsn, clientId, clientSecret) {
8691
+ function setupSentry(request, context, dsn, clientId, clientSecret, coloMetadata, accountId, scriptId) {
8639
8692
  if (!(dsn && clientId && clientSecret))
8640
8693
  return;
8641
8694
  let sentry = new import_toucan_js.Toucan({
@@ -8662,10 +8715,37 @@ function setupSentry(request, context, dsn, clientId, clientSecret) {
8662
8715
  "CF-Access-Client-Secret": clientSecret
8663
8716
  }
8664
8717
  }
8665
- }), colo = request.cf?.colo ?? "UNKNOWN";
8666
- sentry.setTag("colo", colo);
8667
- let userAgent = request.headers.get("user-agent") ?? "UA UNKNOWN";
8668
- return sentry.setUser({ userAgent, colo }), sentry;
8718
+ });
8719
+ return coloMetadata && (sentry.setTag("colo", coloMetadata.coloId), sentry.setTag("metal", coloMetadata.metalId)), accountId && scriptId && (sentry.setTag("accountId", accountId), sentry.setTag("scriptId", scriptId)), sentry.setUser({ id: accountId?.toString() }), sentry;
8720
+ }
8721
+
8722
+ // ../workers-shared/utils/tracing.ts
8723
+ function mockJaegerBindingSpan() {
8724
+ return {
8725
+ addLogs: () => {
8726
+ },
8727
+ setTags: () => {
8728
+ },
8729
+ end: () => {
8730
+ },
8731
+ isRecording: !0
8732
+ };
8733
+ }
8734
+ function mockJaegerBinding() {
8735
+ return {
8736
+ enterSpan: (_, span, ...args) => span(mockJaegerBindingSpan(), ...args),
8737
+ getSpanContext: () => ({
8738
+ traceId: "test-trace",
8739
+ spanId: "test-span",
8740
+ parentSpanId: "test-parent-span",
8741
+ traceFlags: 0
8742
+ }),
8743
+ runWithSpanContext: (_, callback, ...args) => callback(...args),
8744
+ traceId: "test-trace",
8745
+ spanId: "test-span",
8746
+ parentSpanId: "test-parent-span",
8747
+ cfTraceIdHeader: "test-trace:test-span:0"
8748
+ };
8669
8749
  }
8670
8750
 
8671
8751
  // ../workers-shared/asset-worker/src/analytics.ts
@@ -8684,9 +8764,8 @@ var Analytics = class {
8684
8764
  write() {
8685
8765
  this.readyAnalytics && this.readyAnalytics.logEvent({
8686
8766
  version: 1,
8687
- accountId: 0,
8688
- // TODO: need to plumb through
8689
- indexId: this.data.hostname?.substring(0, 96),
8767
+ accountId: this.data.accountId,
8768
+ indexId: this.data.scriptId?.toString(),
8690
8769
  doubles: [
8691
8770
  this.data.requestTime ?? -1,
8692
8771
  // double1
@@ -8731,7 +8810,10 @@ var AssetsManifest = class {
8731
8810
  return entry ? contentHashToKey(entry) : null;
8732
8811
  }
8733
8812
  }, hashPath = async (path) => {
8734
- let data = new TextEncoder().encode(path), hashBuffer = await crypto.subtle.digest("SHA-256", data.buffer);
8813
+ let data = new TextEncoder().encode(path), hashBuffer = await crypto.subtle.digest(
8814
+ "SHA-256",
8815
+ data.buffer
8816
+ );
8735
8817
  return new Uint8Array(hashBuffer, 0, 16);
8736
8818
  }, binarySearch = (arr, searchValue) => {
8737
8819
  if (arr.byteLength === 0)
@@ -8774,63 +8856,14 @@ var AssetsManifest = class {
8774
8856
  )].map((b) => b.toString(16).padStart(2, "0")).join("");
8775
8857
 
8776
8858
  // ../workers-shared/asset-worker/src/configuration.ts
8777
- var applyConfigurationDefaults = (configuration) => ({
8778
- html_handling: configuration?.html_handling ?? "auto-trailing-slash",
8779
- not_found_handling: configuration?.not_found_handling ?? "none",
8780
- serve_directly: configuration?.serve_directly ?? !0
8781
- });
8782
-
8783
- // ../workers-shared/asset-worker/src/responses.ts
8784
- var OkResponse = class extends Response {
8785
- constructor(body, init) {
8786
- super(body, {
8787
- ...init,
8788
- status: 200
8789
- });
8790
- }
8791
- }, NotFoundResponse = class extends Response {
8792
- constructor(...[body, init]) {
8793
- super(body, {
8794
- ...init,
8795
- status: 404,
8796
- statusText: "Not Found"
8797
- });
8798
- }
8799
- }, MethodNotAllowedResponse = class extends Response {
8800
- constructor(...[body, init]) {
8801
- super(body, {
8802
- ...init,
8803
- status: 405,
8804
- statusText: "Method Not Allowed"
8805
- });
8806
- }
8807
- }, InternalServerErrorResponse = class extends Response {
8808
- constructor(err, init) {
8809
- super(null, {
8810
- ...init,
8811
- status: 500
8812
- });
8813
- }
8814
- }, NotModifiedResponse = class extends Response {
8815
- constructor(...[_body, init]) {
8816
- super(null, {
8817
- ...init,
8818
- status: 304,
8819
- statusText: "Not Modified"
8820
- });
8821
- }
8822
- }, TemporaryRedirectResponse = class extends Response {
8823
- constructor(location, init) {
8824
- super(null, {
8825
- ...init,
8826
- status: 307,
8827
- statusText: "Temporary Redirect",
8828
- headers: {
8829
- ...init?.headers,
8830
- Location: location
8831
- }
8832
- });
8833
- }
8859
+ var applyConfigurationDefaults = (configuration) => {
8860
+ let runWorkerFirst;
8861
+ return configuration?.run_worker_first !== void 0 ? runWorkerFirst = configuration?.run_worker_first : configuration?.serve_directly !== void 0 ? runWorkerFirst = !configuration.serve_directly : runWorkerFirst = !1, {
8862
+ html_handling: configuration?.html_handling ?? "auto-trailing-slash",
8863
+ not_found_handling: configuration?.not_found_handling ?? "none",
8864
+ run_worker_first: runWorkerFirst,
8865
+ serve_directly: !runWorkerFirst
8866
+ };
8834
8867
  };
8835
8868
 
8836
8869
  // ../workers-shared/asset-worker/src/constants.ts
@@ -8854,29 +8887,51 @@ var handleRequest = async (request, env, configuration, exists, getByETag) => {
8854
8887
  decodedPathname = decodedPathname.replace(/\/+/g, "/");
8855
8888
  let intent = await getIntent(decodedPathname, configuration, exists);
8856
8889
  if (!intent)
8857
- return new NotFoundResponse();
8890
+ return env.JAEGER.enterSpan("no_intent", (span) => (span.setTags({
8891
+ decodedPathname,
8892
+ configuration: JSON.stringify(configuration),
8893
+ status: 404
8894
+ }), new NotFoundResponse()));
8858
8895
  let method = request.method.toUpperCase();
8859
8896
  if (!["GET", "HEAD"].includes(method))
8860
- return new MethodNotAllowedResponse();
8897
+ return env.JAEGER.enterSpan("method_not_allowed", (span) => (span.setTags({
8898
+ method,
8899
+ status: 405
8900
+ }), new MethodNotAllowedResponse()));
8861
8901
  let decodedDestination = intent.redirect ?? decodedPathname, encodedDestination = encodePath(decodedDestination);
8862
8902
  if (encodedDestination !== pathname && intent.asset || intent.redirect)
8863
- return new TemporaryRedirectResponse(encodedDestination + search);
8903
+ return env.JAEGER.enterSpan("redirect", (span) => (span.setTags({
8904
+ originalPath: pathname,
8905
+ location: encodedDestination !== pathname ? encodedDestination : intent.redirect ?? "<unknown>",
8906
+ status: 307
8907
+ }), new TemporaryRedirectResponse(encodedDestination + search)));
8864
8908
  if (!intent.asset)
8865
- return new InternalServerErrorResponse(new Error("Unknown action"));
8909
+ return env.JAEGER.enterSpan("unknown_action", (span) => (span.setTags({
8910
+ pathname,
8911
+ status: 500
8912
+ }), new InternalServerErrorResponse(new Error("Unknown action"))));
8866
8913
  let asset = await env.JAEGER.enterSpan("getByETag", async (span) => (span.setTags({
8867
8914
  pathname,
8868
8915
  eTag: intent.asset.eTag,
8869
8916
  status: intent.asset.status
8870
8917
  }), 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") || "";
8871
- if ([weakETag, strongETag].includes(ifNoneMatch))
8872
- return new NotModifiedResponse(null, { headers });
8873
- let body = method === "HEAD" ? null : asset.readableStream;
8874
- switch (intent.asset.status) {
8875
- case 404:
8876
- return new NotFoundResponse(body, { headers });
8877
- case 200:
8878
- return new OkResponse(body, { headers });
8879
- }
8918
+ return [weakETag, strongETag].includes(ifNoneMatch) ? env.JAEGER.enterSpan("matched_etag", (span) => (span.setTags({
8919
+ matchedEtag: ifNoneMatch,
8920
+ status: 304
8921
+ }), new NotModifiedResponse(null, { headers }))) : env.JAEGER.enterSpan("response", (span) => {
8922
+ span.setTags({
8923
+ etag: intent.asset.eTag,
8924
+ status: intent.asset.status,
8925
+ head: method === "HEAD"
8926
+ });
8927
+ let body = method === "HEAD" ? null : asset.readableStream;
8928
+ switch (intent.asset.status) {
8929
+ case 404:
8930
+ return new NotFoundResponse(body, { headers });
8931
+ case 200:
8932
+ return new OkResponse(body, { headers });
8933
+ }
8934
+ });
8880
8935
  }, getIntent = async (pathname, configuration, exists, skipRedirects = !1) => {
8881
8936
  switch (configuration.html_handling) {
8882
8937
  case "auto-trailing-slash":
@@ -9285,46 +9340,17 @@ async function getAssetWithMetadataFromKV(assetsKVNamespace, assetKey, sentry, r
9285
9340
  ), retriedAsset;
9286
9341
  }
9287
9342
  return asset;
9288
- } catch {
9289
- if (attempts >= retries)
9290
- throw new Error(
9291
- `Requested asset ${assetKey} could not be fetched from KV namespace.`
9292
- );
9343
+ } catch (err) {
9344
+ if (attempts >= retries) {
9345
+ let message = `KV GET ${assetKey} failed.`;
9346
+ throw err instanceof Error && (message = `KV GET ${assetKey} failed: ${err.message}`), new Error(message);
9347
+ }
9293
9348
  await new Promise(
9294
9349
  (resolvePromise) => setTimeout(resolvePromise, Math.pow(2, attempts++) * 1e3)
9295
9350
  );
9296
9351
  }
9297
9352
  }
9298
9353
 
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
-
9328
9354
  // ../workers-shared/asset-worker/src/index.ts
9329
9355
  var src_default = class extends WorkerEntrypoint {
9330
9356
  async fetch(request) {
@@ -9335,15 +9361,15 @@ var src_default = class extends WorkerEntrypoint {
9335
9361
  this.ctx,
9336
9362
  this.env.SENTRY_DSN,
9337
9363
  this.env.SENTRY_ACCESS_CLIENT_ID,
9338
- this.env.SENTRY_ACCESS_CLIENT_SECRET
9364
+ this.env.SENTRY_ACCESS_CLIENT_SECRET,
9365
+ this.env.COLO_METADATA,
9366
+ this.env.CONFIG?.account_id,
9367
+ this.env.CONFIG?.script_id
9339
9368
  );
9340
- let config = applyConfigurationDefaults(this.env.CONFIG), userAgent = request.headers.get("user-agent") ?? "UA UNKNOWN";
9341
- if (sentry) {
9342
- let colo = this.env.COLO_METADATA.coloId;
9343
- sentry.setTag("colo", this.env.COLO_METADATA.coloId), sentry.setTag("metal", this.env.COLO_METADATA.metalId), sentry.setUser({ userAgent, colo });
9344
- }
9345
- let url = new URL(request.url);
9346
- return this.env.COLO_METADATA && this.env.VERSION_METADATA && analytics.setData({
9369
+ let config = applyConfigurationDefaults(this.env.CONFIG), userAgent = request.headers.get("user-agent") ?? "UA UNKNOWN", url = new URL(request.url);
9370
+ return this.env.COLO_METADATA && this.env.VERSION_METADATA && this.env.CONFIG && analytics.setData({
9371
+ accountId: this.env.CONFIG.account_id,
9372
+ scriptId: this.env.CONFIG.script_id,
9347
9373
  coloId: this.env.COLO_METADATA.coloId,
9348
9374
  metalId: this.env.COLO_METADATA.metalId,
9349
9375
  coloTier: this.env.COLO_METADATA.coloTier,
@@ -9353,7 +9379,7 @@ var src_default = class extends WorkerEntrypoint {
9353
9379
  htmlHandling: config.html_handling,
9354
9380
  notFoundHandling: config.not_found_handling,
9355
9381
  userAgent
9356
- }), this.env.JAEGER.enterSpan("handleRequest", async (span) => (span.setTags({
9382
+ }), await this.env.JAEGER.enterSpan("handleRequest", async (span) => (span.setTags({
9357
9383
  hostname: url.hostname,
9358
9384
  eyeballPath: url.pathname,
9359
9385
  env: this.env.ENVIRONMENT,
@@ -9364,10 +9390,11 @@ var src_default = class extends WorkerEntrypoint {
9364
9390
  config,
9365
9391
  this.unstable_exists.bind(this),
9366
9392
  this.unstable_getByETag.bind(this)
9367
- ))).catch((err) => this.handleError(sentry, analytics, err)).finally(() => this.submitMetrics(analytics, performance, startTimeMs));
9393
+ )));
9368
9394
  } catch (err) {
9369
- let errorResponse = this.handleError(sentry, analytics, err);
9370
- return this.submitMetrics(analytics, performance, startTimeMs), errorResponse;
9395
+ return this.handleError(sentry, analytics, err);
9396
+ } finally {
9397
+ this.submitMetrics(analytics, performance, startTimeMs);
9371
9398
  }
9372
9399
  }
9373
9400
  handleError(sentry, analytics, err) {
@@ -9385,6 +9412,7 @@ var src_default = class extends WorkerEntrypoint {
9385
9412
  console.error("Error submitting metrics", e);
9386
9413
  }
9387
9414
  }
9415
+ // TODO: Trace unstable methods
9388
9416
  async unstable_canFetch(request) {
9389
9417
  let url = new URL(request.url), decodedPathname = decodePath(url.pathname);
9390
9418
  return await getIntent(