@nxtedition/lib 19.7.2 → 19.8.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 (2) hide show
  1. package/couch.js +38 -1
  2. package/package.json +1 -1
package/couch.js CHANGED
@@ -1029,6 +1029,12 @@ class StreamOutput extends stream.Readable {
1029
1029
  #state
1030
1030
  #decoder
1031
1031
  #headers
1032
+ #startTime = performance.now()
1033
+ #stats = {
1034
+ connect: -1,
1035
+ headers: -1,
1036
+ ttfb: -1,
1037
+ }
1032
1038
 
1033
1039
  constructor({ highWaterMark = 256 }) {
1034
1040
  super({ objectMode: true, highWaterMark })
@@ -1038,6 +1044,10 @@ class StreamOutput extends stream.Readable {
1038
1044
  return this.#headers
1039
1045
  }
1040
1046
 
1047
+ get stats() {
1048
+ return this.#stats
1049
+ }
1050
+
1041
1051
  _read() {
1042
1052
  this.#resume?.()
1043
1053
  }
@@ -1048,6 +1058,8 @@ class StreamOutput extends stream.Readable {
1048
1058
  }
1049
1059
 
1050
1060
  onConnect(abort) {
1061
+ this.#stats.connect = performance.now() - this.#startTime
1062
+
1051
1063
  if (this.destroyed) {
1052
1064
  abort(this.errored)
1053
1065
  } else if (this.#state) {
@@ -1061,6 +1073,10 @@ class StreamOutput extends stream.Readable {
1061
1073
  }
1062
1074
 
1063
1075
  onHeaders(statusCode, rawHeaders, resume, statusText) {
1076
+ if (this.#stats.headers === -1) {
1077
+ this.#stats.headers = performance.now() - this.#startTime - this.#stats.connect
1078
+ }
1079
+
1064
1080
  if (statusCode >= 300 || statusCode < 200) {
1065
1081
  throw new Error('invalid status code: ' + statusCode)
1066
1082
  }
@@ -1070,6 +1086,10 @@ class StreamOutput extends stream.Readable {
1070
1086
  }
1071
1087
 
1072
1088
  onData(data) {
1089
+ if (this.#stats.ttfb === -1) {
1090
+ this.#stats.ttfb = performance.now() - this.#startTime - this.#stats.headers
1091
+ }
1092
+
1073
1093
  const lines = this.#decoder.decode(data, { stream: true }).split(/\r?\n+/)
1074
1094
  lines[0] = this.#str + lines[0]
1075
1095
  this.#str = lines.pop() ?? ''
@@ -1115,6 +1135,12 @@ class PromiseOutput {
1115
1135
  #str
1116
1136
  #decoder
1117
1137
  #headers
1138
+ #startTime = performance.now()
1139
+ #stats = {
1140
+ connect: -1,
1141
+ headers: -1,
1142
+ ttfb: -1,
1143
+ }
1118
1144
 
1119
1145
  constructor({ resolve, reject }) {
1120
1146
  this.#resolve = resolve
@@ -1122,11 +1148,16 @@ class PromiseOutput {
1122
1148
  }
1123
1149
 
1124
1150
  onConnect() {
1151
+ this.#stats.connect = performance.now() - this.#startTime
1125
1152
  this.#decoder = new TextDecoder()
1126
1153
  this.#str = ''
1127
1154
  }
1128
1155
 
1129
1156
  onHeaders(statusCode, rawHeaders, resume, statusText) {
1157
+ if (this.#stats.headers === -1) {
1158
+ this.#stats.headers = performance.now() - this.#startTime - this.#stats.connect
1159
+ }
1160
+
1130
1161
  if (statusCode >= 300 || statusCode < 200) {
1131
1162
  throw new Error('invalid status code: ' + statusCode)
1132
1163
  }
@@ -1134,13 +1165,19 @@ class PromiseOutput {
1134
1165
  }
1135
1166
 
1136
1167
  onData(data) {
1168
+ if (this.#stats.ttfb === -1) {
1169
+ this.#stats.ttfb = performance.now() - this.#startTime - this.#stats.headers
1170
+ }
1171
+
1137
1172
  this.#str += this.#decoder.decode(data, { stream: true })
1138
1173
  }
1139
1174
 
1140
1175
  onComplete() {
1141
1176
  this.#str += this.#decoder.decode(undefined, { stream: false })
1142
1177
 
1143
- this.#resolve(Object.assign(JSON.parse(this.#str), { headers: this.#headers }))
1178
+ this.#resolve(
1179
+ Object.assign(JSON.parse(this.#str), { headers: this.#headers, stats: this.#stats }),
1180
+ )
1144
1181
  }
1145
1182
 
1146
1183
  onError(err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "19.7.2",
3
+ "version": "19.8.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",