Skip to content

Commit

Permalink
fix: pipe unwanted logs to /dev/null
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev committed Apr 27, 2024
1 parent f1f4c6b commit ac66612
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/pic/src/http2-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@ export const JSON_HEADER: OutgoingHttpHeaders = {
};

export class Http2Client {
private readonly session: ClientHttp2Session;
private session?: ClientHttp2Session;

constructor(baseUrl: string) {
this.session = http2.connect(baseUrl);
constructor(private readonly baseUrl: string) {
this.connect();
}

public request(init: Request): Promise<Response> {
return new Promise((resolve, reject) => {
let req = this.session.request({
if (!this.session) {
this.connect();
}

if (!this.session) {
throw new Error('Failed to connect to PocketIC server');
}

const req = this.session.request({
[HTTP2_HEADER_PATH]: init.path,
[HTTP2_HEADER_METHOD]: init.method,
'content-length': init.body?.length ?? 0,
Expand Down Expand Up @@ -173,6 +181,19 @@ export class Http2Client {
return resBody;
}
}

private connect(): void {
this.session = http2.connect(this.baseUrl);

this.session.on('close', () => {
console.log('Session closed, reconnecting...');
this.connect();
});

this.session.on('error', err => {
console.error('Session error:', err);
});
}
}

interface StartedOrBusyApiResponse {
Expand Down
15 changes: 15 additions & 0 deletions packages/pic/src/pocket-ic-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isDarwin,
} from './util';
import { StartServerOptions } from './pocket-ic-server-types';
import { Writable } from 'node:stream';

/**
* This class represents the main PocketIC server.
Expand Down Expand Up @@ -74,10 +75,14 @@ export class PocketIcServer {

if (options.showRuntimeLogs) {
serverProcess.stdout.pipe(process.stdout);
} else {
serverProcess.stdout.pipe(new NullStream());
}

if (options.showCanisterLogs) {
serverProcess.stderr.pipe(process.stderr);
} else {
serverProcess.stderr.pipe(new NullStream());
}

serverProcess.on('error', error => {
Expand Down Expand Up @@ -144,3 +149,13 @@ export class PocketIcServer {
chmodSync(binPath, 0o700);
}
}

class NullStream extends Writable {
_write(
_chunk: any,
_encoding: BufferEncoding,
callback: (error?: Error | null) => void,
): void {
callback();
}
}

0 comments on commit ac66612

Please sign in to comment.