@stainlessdev/xray-core 0.5.0-dev.ce9d113 → 0.5.1-branch.bg-client-ip-addr.341865f

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/dist/index.cjs CHANGED
@@ -768,23 +768,226 @@ function setHeaderAttributes(span, headers, prefix) {
768
768
  span.setAttribute(prefix + key.toLowerCase(), Array.isArray(values) ? values : [values]);
769
769
  }
770
770
  }
771
- function setRequestAttributes(span, method, urlFull) {
772
- span.setAttribute(import_incubating.ATTR_HTTP_REQUEST_METHOD, method);
773
- if (urlFull) {
774
- span.setAttribute(import_incubating.ATTR_URL_FULL, urlFull);
775
- const path = extractPath(urlFull);
771
+ function setRequestAttributes(span, request, urlFull) {
772
+ span.setAttribute(import_incubating.ATTR_HTTP_REQUEST_METHOD, request.method);
773
+ const effectiveUrl = urlFull ?? request.url;
774
+ if (effectiveUrl) {
775
+ span.setAttribute(import_incubating.ATTR_URL_FULL, effectiveUrl);
776
+ const scheme = extractScheme(effectiveUrl);
777
+ if (scheme) {
778
+ span.setAttribute(import_incubating.ATTR_URL_SCHEME, scheme);
779
+ }
780
+ const path = extractPath(effectiveUrl);
776
781
  if (path) {
777
782
  span.setAttribute(import_incubating.ATTR_URL_PATH, path);
778
783
  }
784
+ const query = extractQuery(effectiveUrl);
785
+ if (query) {
786
+ span.setAttribute(import_incubating.ATTR_URL_QUERY, query);
787
+ }
788
+ }
789
+ const clientAddress = clientAddressForRequest(request.headers, request.remoteAddress);
790
+ if (clientAddress) {
791
+ span.setAttribute(import_incubating.ATTR_CLIENT_ADDRESS, clientAddress);
792
+ }
793
+ if (request.protocolVersion) {
794
+ span.setAttribute(import_incubating.ATTR_NETWORK_PROTOCOL_VERSION, request.protocolVersion);
795
+ }
796
+ const transport = networkTransportForRemoteAddress(request.remoteAddress);
797
+ if (transport) {
798
+ span.setAttribute(import_incubating.ATTR_NETWORK_TRANSPORT, transport);
779
799
  }
780
800
  }
781
801
  function extractPath(url) {
802
+ if (!url) {
803
+ return void 0;
804
+ }
782
805
  try {
783
- return new URL(url).pathname;
806
+ const pathname = new URL(url).pathname;
807
+ return pathname || "/";
784
808
  } catch {
785
809
  const match = url.match(/^[^?#]*/);
786
- return match?.[0] || void 0;
810
+ if (!match) {
811
+ return void 0;
812
+ }
813
+ return match[0] || "/";
814
+ }
815
+ }
816
+ function extractScheme(url) {
817
+ if (!url) {
818
+ return void 0;
819
+ }
820
+ try {
821
+ const protocol = new URL(url).protocol;
822
+ if (!protocol) {
823
+ return void 0;
824
+ }
825
+ const normalized = protocol.endsWith(":") ? protocol.slice(0, -1) : protocol;
826
+ return normalized || void 0;
827
+ } catch {
828
+ return void 0;
829
+ }
830
+ }
831
+ function extractQuery(url) {
832
+ if (!url) {
833
+ return void 0;
834
+ }
835
+ try {
836
+ const query = new URL(url).search;
837
+ if (!query || query === "?") {
838
+ return void 0;
839
+ }
840
+ return query.startsWith("?") ? query.slice(1) : query;
841
+ } catch {
842
+ const match = url.match(/\?([^#]*)/);
843
+ if (!match) {
844
+ return void 0;
845
+ }
846
+ const value = match[1] ?? "";
847
+ return value ? value : void 0;
848
+ }
849
+ }
850
+ function clientAddressForRequest(headers, remoteAddress) {
851
+ const forwarded = forwardedClientAddress(headerValues(headers, "forwarded"));
852
+ if (forwarded) {
853
+ return forwarded;
854
+ }
855
+ const xForwarded = xForwardedForClientAddress(headerValues(headers, "x-forwarded-for"));
856
+ if (xForwarded) {
857
+ return xForwarded;
858
+ }
859
+ const xRealIp = xRealIpClientAddress(headerValues(headers, "x-real-ip"));
860
+ if (xRealIp) {
861
+ return xRealIp;
862
+ }
863
+ if (!remoteAddress) {
864
+ return void 0;
865
+ }
866
+ return remoteAddrHost(remoteAddress);
867
+ }
868
+ function forwardedClientAddress(values) {
869
+ for (const value of values) {
870
+ if (!value) {
871
+ continue;
872
+ }
873
+ const entries = value.split(",");
874
+ for (const entry of entries) {
875
+ const params = entry.split(";");
876
+ for (const param of params) {
877
+ const [rawKey, ...rest] = param.split("=");
878
+ if (!rawKey) {
879
+ continue;
880
+ }
881
+ if (rawKey.trim().toLowerCase() !== "for") {
882
+ continue;
883
+ }
884
+ const rawValue = rest.join("=").trim();
885
+ const normalized = normalizeKnownAddress(rawValue);
886
+ if (normalized) {
887
+ return normalized;
888
+ }
889
+ }
890
+ }
891
+ }
892
+ return void 0;
893
+ }
894
+ function xForwardedForClientAddress(values) {
895
+ for (const value of values) {
896
+ if (!value) {
897
+ continue;
898
+ }
899
+ const entries = value.split(",");
900
+ for (const entry of entries) {
901
+ const normalized = normalizeKnownAddress(entry);
902
+ if (normalized) {
903
+ return normalized;
904
+ }
905
+ }
906
+ }
907
+ return void 0;
908
+ }
909
+ function xRealIpClientAddress(values) {
910
+ for (const value of values) {
911
+ if (!value) {
912
+ continue;
913
+ }
914
+ const normalized = normalizeKnownAddress(value);
915
+ if (normalized) {
916
+ return normalized;
917
+ }
918
+ }
919
+ return void 0;
920
+ }
921
+ function normalizeKnownAddress(value) {
922
+ const normalized = normalizeAddress(value);
923
+ if (!normalized) {
924
+ return void 0;
925
+ }
926
+ if (normalized.toLowerCase() === "unknown") {
927
+ return void 0;
928
+ }
929
+ return normalized;
930
+ }
931
+ function normalizeAddress(value) {
932
+ if (!value) {
933
+ return void 0;
934
+ }
935
+ let trimmed = value.trim();
936
+ if (!trimmed) {
937
+ return void 0;
938
+ }
939
+ if (trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length > 1) {
940
+ trimmed = trimmed.slice(1, -1).trim();
941
+ }
942
+ if (!trimmed) {
943
+ return void 0;
944
+ }
945
+ if (trimmed.startsWith("[")) {
946
+ const end = trimmed.indexOf("]");
947
+ if (end !== -1) {
948
+ return trimmed.slice(1, end);
949
+ }
950
+ }
951
+ const colonCount = (trimmed.match(/:/g) ?? []).length;
952
+ if (colonCount === 1) {
953
+ const host = trimmed.split(":")[0];
954
+ return host || void 0;
955
+ }
956
+ return trimmed;
957
+ }
958
+ function remoteAddrHost(value) {
959
+ return normalizeAddress(value);
960
+ }
961
+ function headerValues(headers, name) {
962
+ if (!headers) {
963
+ return [];
964
+ }
965
+ const target = name.toLowerCase();
966
+ const values = [];
967
+ for (const [key, value] of Object.entries(headers)) {
968
+ if (key.toLowerCase() !== target) {
969
+ continue;
970
+ }
971
+ if (Array.isArray(value)) {
972
+ values.push(...value);
973
+ } else {
974
+ values.push(value);
975
+ }
976
+ }
977
+ return values;
978
+ }
979
+ function networkTransportForRemoteAddress(remoteAddress) {
980
+ if (!remoteAddress) {
981
+ return void 0;
982
+ }
983
+ const trimmed = remoteAddress.trim();
984
+ if (!trimmed) {
985
+ return void 0;
986
+ }
987
+ if (trimmed.startsWith("/") || trimmed.startsWith("@")) {
988
+ return "unix";
787
989
  }
990
+ return "tcp";
788
991
  }
789
992
  function setRequestBodyAttributes(span, body) {
790
993
  if (!body.value) {
@@ -915,7 +1118,7 @@ function createSpanProcessor(mode, exporter) {
915
1118
  }
916
1119
  function sdkVersion() {
917
1120
  if (true) {
918
- return "0.5.0";
1121
+ return "0.5.1";
919
1122
  }
920
1123
  return "unknown";
921
1124
  }
@@ -1111,7 +1314,7 @@ function endRequest(config, ctx, res, err) {
1111
1314
  const span = state.span;
1112
1315
  if (span) {
1113
1316
  try {
1114
- setRequestAttributes(span, request.method, redacted.url);
1317
+ setRequestAttributes(span, request, redacted.url);
1115
1318
  setRequestIdAttribute(span, redacted.requestId);
1116
1319
  span.setAttribute("service.name", config.serviceName);
1117
1320
  if (redacted.statusCode != null) {