@travetto/compiler 4.0.2 → 4.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/compiler",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "The compiler infrastructure for the Travetto framework",
5
5
  "keywords": [
6
6
  "compiler",
@@ -14,6 +14,12 @@ type FetchEventsConfig<T> = {
14
14
  enforceIteration?: boolean;
15
15
  };
16
16
 
17
+ const streamAgent = new Agent({
18
+ keepAlive: true,
19
+ keepAliveMsecs: 10000,
20
+ timeout: 1000 * 60 * 60 * 24
21
+ });
22
+
17
23
  /**
18
24
  * Compiler Client Operations
19
25
  */
@@ -37,7 +43,7 @@ export class CompilerClient {
37
43
  return this.#url;
38
44
  }
39
45
 
40
- async #fetch(rel: string, opts?: RequestInit & { timeout?: number }, logTimeout = true): Promise<Response> {
46
+ async #fetch(rel: string, opts?: RequestInit & { timeout?: number }, logTimeout = true): Promise<{ ok: boolean, text: string }> {
41
47
  const ctrl = new AbortController();
42
48
  opts?.signal?.addEventListener('abort', () => ctrl.abort());
43
49
  const timeoutId = setTimeout(() => {
@@ -45,7 +51,8 @@ export class CompilerClient {
45
51
  ctrl.abort('TIMEOUT');
46
52
  }, opts?.timeout ?? 100).unref();
47
53
  try {
48
- return await fetch(`${this.#url}${rel}`, { ...opts, signal: ctrl.signal });
54
+ const res = await fetch(`${this.#url}${rel}`, { ...opts, signal: ctrl.signal });
55
+ return { ok: res.ok, text: await res.text() };
49
56
  } finally {
50
57
  clearTimeout(timeoutId);
51
58
  }
@@ -53,7 +60,7 @@ export class CompilerClient {
53
60
 
54
61
  /** Get server information, if server is running */
55
62
  info(): Promise<CompilerServerInfo | undefined> {
56
- return this.#fetch('/info', { timeout: 200 }, false).then(v => v.json(), () => undefined)
63
+ return this.#fetch('/info', { timeout: 200 }, false).then(v => JSON.parse(v.text), () => undefined)
57
64
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
58
65
  .then(v => v as CompilerServerInfo);
59
66
  }
@@ -110,18 +117,12 @@ export class CompilerClient {
110
117
  const quit = (): void => ctrl.abort();
111
118
  try {
112
119
  signal.addEventListener('abort', quit);
113
- const res = await new Promise<http.IncomingMessage>(resolve => {
114
- http.get(`${this.#url}/event/${type}`, {
115
- agent: new Agent({
116
- keepAlive: true,
117
- keepAliveMsecs: 10000,
118
- timeout: 1000 * 60 * 60 * 24
119
- }),
120
- signal: ctrl.signal
121
- }, res => resolve(res))
122
- });
123
-
124
- for await (const line of rl.createInterface(res)) {
120
+ const response = await new Promise<http.IncomingMessage>((resolve, reject) =>
121
+ http.get(`${this.#url}/event/${type}`, { agent: streamAgent, signal: ctrl.signal }, resolve)
122
+ .on('error', reject)
123
+ );
124
+
125
+ for await (const line of rl.createInterface(response)) {
125
126
  if (line.trim().charAt(0) === '{') {
126
127
  const val = JSON.parse(line);
127
128
  if (cfg.until?.(val)) {