Skip to content

Commit 5c79eed

Browse files
committed
feat(nodejs): use logger
1 parent 07c9b14 commit 5c79eed

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { makeVatLogger } from './logger.ts';
4+
5+
describe('makeVatLogger', () => {
6+
it('should create a logger with the given vatId', () => {
7+
const vatId = '123';
8+
const logger = makeVatLogger(vatId);
9+
expect(logger.label).toContain(vatId);
10+
});
11+
12+
it('should create a logger when no vatId is provided', () => {
13+
const logger = makeVatLogger();
14+
expect(logger.label).toContain('(unknown)');
15+
});
16+
});

packages/nodejs/src/vat/logger.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { VatId } from '@ocap/kernel';
2+
import type { Logger } from '@ocap/utils';
3+
import { makeLogger } from '@ocap/utils';
4+
5+
export const makeVatLogger = (vatId?: VatId): Logger =>
6+
makeLogger(`[vat-worker ${vatId ?? '(unknown)'}]`);

packages/nodejs/src/vat/vat-worker.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ import '@ocap/shims/endoify';
22

33
import type { VatId } from '@ocap/kernel';
44
import { VatSupervisor } from '@ocap/kernel';
5-
import { makeLogger } from '@ocap/utils';
65
import fs from 'node:fs/promises';
76
import url from 'node:url';
87

8+
import { makeVatLogger } from './logger.ts';
99
import { makeCommandStream } from './streams.ts';
1010

1111
const vatId = process.env.NODE_VAT_ID as VatId;
1212

1313
/* eslint-disable n/no-unsupported-features/node-builtins */
1414

15-
if (vatId) {
16-
const logger = makeLogger(`[vat-worker (${vatId})]`);
17-
main().catch((error) => logger.error(error));
18-
} else {
19-
console.log('no vatId set for env variable NODE_VAT_ID');
20-
}
15+
const logger = makeVatLogger(vatId);
16+
17+
main(vatId).catch((error) => logger.error(error));
2118

2219
/**
2320
* Fetch a blob of bytes from a URL
@@ -39,14 +36,20 @@ async function fetchBlob(blobURL: string): Promise<Response> {
3936

4037
/**
4138
* The main function for the iframe.
39+
*
40+
* @param id - The ID of the vat this worker is running.
4241
*/
43-
async function main(): Promise<void> {
42+
async function main(id: VatId): Promise<void> {
43+
if (!id) {
44+
throw new Error('no vat ID set for env variable NODE_VAT_ID');
45+
}
4446
const commandStream = makeCommandStream();
4547
await commandStream.synchronize();
4648
// eslint-disable-next-line no-void
4749
void new VatSupervisor({
48-
id: vatId,
50+
id,
4951
commandStream,
5052
fetchBlob,
53+
logger,
5154
});
5255
}

0 commit comments

Comments
 (0)