Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/brow-2-brow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build:dev": "mkdir -p dist && ln -fs ../src/index.html dist/index.html",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/brow-2-brow",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"build": "ts-bridge --project tsconfig.build.json --no-references --clean",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/cli",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"scripts": {
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/create-package",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
6 changes: 3 additions & 3 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:browser": "OPEN_BROWSER=true yarn build:dev --watch",
"build:vite": "vite build --configLoader runner --config vite.config.ts",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/extension",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down Expand Up @@ -63,7 +63,7 @@
"@ocap/cli": "workspace:^",
"@ocap/kernel-test": "workspace:^",
"@ocap/repo-tools": "workspace:^",
"@playwright/test": "^1.54.2",
"@playwright/test": "^1.57.0",
"@testing-library/jest-dom": "^6.6.3",
"@types/chrome": "^0.0.313",
"@types/react": "^17.0.11",
Expand All @@ -83,7 +83,7 @@
"eslint-plugin-prettier": "^5.2.6",
"eslint-plugin-promise": "^7.2.1",
"jsdom": "^27.4.0",
"playwright": "^1.54.2",
"playwright": "^1.57.0",
"prettier": "^3.5.3",
"rimraf": "^6.0.1",
"tsx": "^4.20.6",
Expand Down
8 changes: 6 additions & 2 deletions packages/extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
makeCapTPNotification,
isCapTPNotification,
getCapTPMessage,
isConsoleForwardMessage,
handleConsoleForwardMessage,
} from '@metamask/kernel-browser-runtime';
import type {
KernelFacade,
Expand Down Expand Up @@ -115,9 +117,11 @@ async function main(): Promise<void> {
logger.info(result);
};

// Handle incoming CapTP messages from the kernel
// Handle incoming messages from offscreen (CapTP and console-forward)
const drainPromise = offscreenStream.drain((message) => {
if (isCapTPNotification(message)) {
if (isConsoleForwardMessage(message)) {
handleConsoleForwardMessage(message, '[offscreen]');
} else if (isCapTPNotification(message)) {
const captpMessage = getCapTPMessage(message);
backgroundCapTP.dispatch(captpMessage);
} else {
Expand Down
4 changes: 4 additions & 0 deletions packages/extension/src/offscreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
makeIframeVatWorker,
PlatformServicesServer,
createRelayQueryString,
setupConsoleForwarding,
} from '@metamask/kernel-browser-runtime';
import { delay, isJsonRpcMessage } from '@metamask/kernel-utils';
import type { JsonRpcMessage } from '@metamask/kernel-utils';
Expand Down Expand Up @@ -31,6 +32,9 @@ async function main(): Promise<void> {
JsonRpcMessage
>(chrome.runtime, 'offscreen', 'background', isJsonRpcMessage);

// Set up console forwarding to background for Playwright capture
setupConsoleForwarding(backgroundStream);

const kernelStream = await makeKernelWorker();

// Handle messages from the background script / kernel
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-agents-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"build": "ts-bridge --project tsconfig.build.json --no-references --clean",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/kernel-agents-repl",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-agents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"build": "ts-bridge --project tsconfig.build.json --no-references --clean",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/kernel-agents",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-browser-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/kernel-browser-runtime",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/kernel-browser-runtime",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
3 changes: 3 additions & 0 deletions packages/kernel-browser-runtime/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ describe('index', () => {
'createRelayQueryString',
'getCapTPMessage',
'getRelaysFromCurrentLocation',
'handleConsoleForwardMessage',
'isCapTPNotification',
'isConsoleForwardMessage',
'makeBackgroundCapTP',
'makeCapTPNotification',
'makeIframeVatWorker',
'parseRelayQueryString',
'receiveInternalConnections',
'rpcHandlers',
'rpcMethodSpecs',
'setupConsoleForwarding',
]);
});
});
94 changes: 94 additions & 0 deletions packages/kernel-browser-runtime/src/utils/console-forwarding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import type { JsonRpcMessage } from '@metamask/kernel-utils';
import type { DuplexStream } from '@metamask/streams';
import type { JsonRpcNotification } from '@metamask/utils';

/**
* Message type for forwarding console output from one context to another.
* Used to capture console logs from offscreen documents in Playwright tests.
*/
export type ConsoleForwardMessage = JsonRpcNotification & {
method: 'console-forward';
params: {
method: 'log' | 'debug' | 'info' | 'warn' | 'error';
args: string[];
};
};

/**
* Type guard for console-forward messages.
*
* @param value - The value to check.
* @returns Whether the value is a ConsoleForwardMessage.
*/
export const isConsoleForwardMessage = (
value: unknown,
): value is ConsoleForwardMessage =>
typeof value === 'object' &&
value !== null &&
'method' in value &&
(value as { method: unknown }).method === 'console-forward';

/**
* Wraps console methods to forward messages to background via a stream.
* This enables capturing console output from contexts that Playwright cannot
* directly access (like offscreen documents).
*
* Call this early after the stream is created. After setup, console output
* will be forwarded to the stream recipient where it can be replayed.
*
* @param stream - The stream to write console messages to.
*/
export function setupConsoleForwarding(
stream: DuplexStream<JsonRpcMessage, JsonRpcMessage>,
): void {
const originalConsole = { ...console };
const consoleMethods = ['log', 'debug', 'info', 'warn', 'error'] as const;

consoleMethods.forEach((consoleMethod) => {
// eslint-disable-next-line no-console
console[consoleMethod] = (...args: unknown[]) => {
// Call original console method
originalConsole[consoleMethod](...args);

// Forward to background via stream
const message: ConsoleForwardMessage = {
jsonrpc: '2.0',
method: 'console-forward',
params: {
method: consoleMethod,
args: args.map((arg) => {
if (typeof arg === 'string') {
return arg;
}
if (typeof arg === 'number' || typeof arg === 'boolean') {
return String(arg);
}
// Objects, arrays, null, undefined, functions, symbols, etc.
return JSON.stringify(arg);
}),
},
};
stream.write(message).catch(() => {
// Ignore errors if stream isn't ready
});
};
});

harden(globalThis.console);
}

/**
* Handles a console-forward message by replaying it to the local console.
* Use this in the stream handler to replay forwarded console output.
*
* @param message - The console-forward message to handle.
* @param prefix - Optional prefix to add to the message (e.g., '[offscreen]').
*/
export function handleConsoleForwardMessage(
message: ConsoleForwardMessage,
prefix?: string,
): void {
const { method, args } = message.params;
// eslint-disable-next-line no-console
console[method](...(prefix ? [prefix, ...args] : args));
}
3 changes: 3 additions & 0 deletions packages/kernel-browser-runtime/src/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ describe('index', () => {
expect(Object.keys(indexModule).sort()).toStrictEqual([
'createRelayQueryString',
'getRelaysFromCurrentLocation',
'handleConsoleForwardMessage',
'isConsoleForwardMessage',
'parseRelayQueryString',
'setupConsoleForwarding',
]);
});
});
1 change: 1 addition & 0 deletions packages/kernel-browser-runtime/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './console-forwarding.ts';
export * from './relay-query-string.ts';
2 changes: 1 addition & 1 deletion packages/kernel-errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/kernel-errors",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/kernel-errors",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-language-model-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"build": "ts-bridge --project tsconfig.build.json --no-references --clean",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/kernel-language-model-service",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-platforms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"build": "ts-bridge --project tsconfig.build.json --no-references --clean",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/kernel-platforms",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-rpc-methods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/kernel-rpc-methods",
"changelog:update": "../../scripts/update-changelog.sh @metamask/kernel-rpc-methods",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-shims/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/kernel-shims",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/kernel-shims",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/kernel-store",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/kernel-store",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-test-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"type": "module",
"scripts": {
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"scripts": {
"build": "ocap bundle src/vats",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo './src/**/*.bundle'",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo './src/**/*.bundle' ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/kernel-ui",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/kernel-ui",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/kernel-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/kernel-utils",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/kernel-utils",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/logger",
"changelog:update": "../../scripts/update-changelog.sh @metamask/logger",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs-test-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"build": "ts-bridge --project tsconfig.build.json --no-references --clean",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/nodejs-test-workers",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"build": "ts-bridge --project tsconfig.build.json --no-references --clean",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/nodejs",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
2 changes: 1 addition & 1 deletion packages/ocap-kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/ocap-kernel",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/ocap-kernel",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo",
"clean": "rimraf --glob './*.tsbuildinfo' ./.eslintcache ./coverage ./dist ./.turbo ./logs",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies",
"lint:dependencies": "depcheck --quiet",
"lint:eslint": "eslint . --cache",
Expand Down
Loading
Loading