@observertc/observer-js 1.3.0-rc.5 → 1.3.0-rc.6

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.mjs CHANGED
@@ -7985,9 +7985,96 @@ var TrendTester = class {
7985
7985
  }
7986
7986
  };
7987
7987
 
7988
- // src/sinks/InMemorySink.ts
7988
+ // src/sinks/RollingJsonlFileSink.ts
7989
+ import * as fs2 from "fs";
7989
7990
  import { EventEmitter as EventEmitter8 } from "events";
7990
- var InMemorySink = class extends EventEmitter8 {
7991
+ var RollingJsonlFileSink = class extends EventEmitter8 {
7992
+ id;
7993
+ _options;
7994
+ _serializeSample;
7995
+ _stream;
7996
+ _path;
7997
+ _index = -1;
7998
+ _openedAt = 0;
7999
+ _bytes = 0;
8000
+ _ended = false;
8001
+ // Resolves once every file closed so far has closed and been announced, in index order: the
8002
+ // streams close independently, so a later file could otherwise be announced before an earlier one.
8003
+ _closing = Promise.resolve();
8004
+ constructor(options) {
8005
+ super();
8006
+ this._options = options;
8007
+ this._serializeSample = options.serializeSample ?? ((record) => JSON.stringify(record));
8008
+ this._open();
8009
+ this.id = options.id ?? this._path;
8010
+ }
8011
+ /** The file currently written to. */
8012
+ get path() {
8013
+ return this._path;
8014
+ }
8015
+ /** The index of the file currently written to. */
8016
+ get index() {
8017
+ return this._index;
8018
+ }
8019
+ get ended() {
8020
+ return this._ended;
8021
+ }
8022
+ write(record) {
8023
+ if (this._ended) return false;
8024
+ const line = `${this._serializeSample(record)}
8025
+ `;
8026
+ const bytes = Buffer.byteLength(line);
8027
+ const { maxBytes, maxAgeMs } = this._options;
8028
+ if (maxAgeMs !== void 0 && maxAgeMs <= Date.now() - this._openedAt) this.roll();
8029
+ if (maxBytes !== void 0 && maxBytes < this._bytes + bytes) this.roll();
8030
+ this._bytes += bytes;
8031
+ return this._stream.write(line);
8032
+ }
8033
+ /** Close the current file and continue in a new one. A no-op on an empty file or an ended sink. */
8034
+ roll() {
8035
+ if (this._ended || this._bytes === 0) return;
8036
+ this._closeCurrent();
8037
+ this._open();
8038
+ }
8039
+ /** No more writes: emits `ended`, closes the current file (`file-closed`), then emits `close`. */
8040
+ end() {
8041
+ if (this._ended) return;
8042
+ this._ended = true;
8043
+ this.emit("ended");
8044
+ this._closeCurrent();
8045
+ void this._closing.then(() => {
8046
+ this.emit("finish");
8047
+ this.emit("close");
8048
+ });
8049
+ }
8050
+ _open() {
8051
+ ++this._index;
8052
+ this._openedAt = Date.now();
8053
+ this._bytes = 0;
8054
+ this._path = this._options.path({ index: this._index, openedAt: this._openedAt });
8055
+ const stream = fs2.createWriteStream(this._path, { flags: "a" });
8056
+ stream.on("drain", () => this.emit("drain"));
8057
+ stream.on("error", (err) => this.emit("error", err));
8058
+ this._stream = stream;
8059
+ }
8060
+ _closeCurrent() {
8061
+ const stream = this._stream;
8062
+ const path = this._path;
8063
+ const index = this._index;
8064
+ const closed = new Promise((resolve) => {
8065
+ stream.once("close", () => resolve());
8066
+ stream.once("error", () => resolve());
8067
+ });
8068
+ this._closing = Promise.all([this._closing, closed]).then(() => {
8069
+ this.emit("file-closed", path, index);
8070
+ });
8071
+ stream.end();
8072
+ }
8073
+ };
8074
+
8075
+ // src/sinks/InMemorySink.ts
8076
+ import { EventEmitter as EventEmitter9 } from "events";
8077
+ var InMemorySink = class extends EventEmitter9 {
7991
8078
  id;
7992
8079
  samples;
7993
8080
  _ended = false;
@@ -8247,6 +8334,7 @@ export {
8247
8334
  RTT_HISTOGRAM_NUMBER_OF_BUCKETS,
8248
8335
  RemoteTrackResolver,
8249
8336
  RemoteTrackResolverValidator,
8337
+ RollingJsonlFileSink,
8250
8338
  SCORE_HISTOGRAM_BUCKET_WIDTH,
8251
8339
  SCORE_HISTOGRAM_NUMBER_OF_BUCKETS,
8252
8340
  SfuCongestionDetector,