2021-01-08 01:15:34 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-21 03:41:51 +02:00
|
|
|
import type { StackFrame } from '@protocol/channels';
|
|
|
|
|
import type { ActionTraceEvent } from '@trace/trace';
|
2022-03-25 22:12:00 +01:00
|
|
|
import { SplitView } from '@web/components/splitView';
|
2021-01-08 01:15:34 +01:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import { useAsyncMemo } from './helpers';
|
|
|
|
|
import './sourceTab.css';
|
2021-03-11 20:22:59 +01:00
|
|
|
import { StackTraceView } from './stackTrace';
|
2023-03-11 01:22:19 +01:00
|
|
|
import { CodeMirrorWrapper } from '@web/components/codeMirrorWrapper';
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2023-03-11 01:22:19 +01:00
|
|
|
type StackInfo = {
|
2021-02-11 03:52:28 +01:00
|
|
|
frames: StackFrame[];
|
2021-01-19 23:45:26 +01:00
|
|
|
fileContent: Map<string, string>;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-08 01:15:34 +01:00
|
|
|
export const SourceTab: React.FunctionComponent<{
|
2021-05-14 05:41:32 +02:00
|
|
|
action: ActionTraceEvent | undefined,
|
2023-03-11 07:52:31 +01:00
|
|
|
hideStackFrames?: boolean,
|
|
|
|
|
}> = ({ action, hideStackFrames }) => {
|
2021-05-14 05:41:32 +02:00
|
|
|
const [lastAction, setLastAction] = React.useState<ActionTraceEvent | undefined>();
|
2021-01-19 23:45:26 +01:00
|
|
|
const [selectedFrame, setSelectedFrame] = React.useState<number>(0);
|
|
|
|
|
|
2021-05-14 05:41:32 +02:00
|
|
|
if (lastAction !== action) {
|
|
|
|
|
setLastAction(action);
|
2021-01-19 23:45:26 +01:00
|
|
|
setSelectedFrame(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stackInfo = React.useMemo<StackInfo>(() => {
|
2021-05-14 05:41:32 +02:00
|
|
|
if (!action)
|
2023-03-11 01:22:19 +01:00
|
|
|
return { frames: [], fileContent: new Map() };
|
2023-02-28 00:29:20 +01:00
|
|
|
const frames = action.stack || [];
|
2021-02-11 03:52:28 +01:00
|
|
|
return {
|
|
|
|
|
frames,
|
2021-01-19 23:45:26 +01:00
|
|
|
fileContent: new Map(),
|
|
|
|
|
};
|
2021-05-14 05:41:32 +02:00
|
|
|
}, [action]);
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2021-03-11 20:22:59 +01:00
|
|
|
const content = useAsyncMemo<string>(async () => {
|
2023-03-11 01:22:19 +01:00
|
|
|
const filePath = stackInfo.frames[selectedFrame]?.file;
|
|
|
|
|
if (!filePath)
|
|
|
|
|
return '';
|
|
|
|
|
if (!stackInfo.fileContent.has(filePath)) {
|
|
|
|
|
const sha1 = await calculateSha1(filePath);
|
2023-03-11 02:01:30 +01:00
|
|
|
try {
|
|
|
|
|
let response = await fetch(`sha1/src@${sha1}.txt`);
|
|
|
|
|
if (response.status === 404)
|
|
|
|
|
response = await fetch(`file?path=${filePath}`);
|
|
|
|
|
stackInfo.fileContent.set(filePath, await response.text());
|
|
|
|
|
} catch {
|
|
|
|
|
stackInfo.fileContent.set(filePath, `<Unable to read "${filePath}">`);
|
|
|
|
|
}
|
2021-01-19 23:45:26 +01:00
|
|
|
}
|
2023-03-11 01:22:19 +01:00
|
|
|
return stackInfo.fileContent.get(filePath)!;
|
2021-03-11 20:22:59 +01:00
|
|
|
}, [stackInfo, selectedFrame], '');
|
2021-01-19 23:45:26 +01:00
|
|
|
|
2023-03-11 01:22:19 +01:00
|
|
|
const targetLine = stackInfo.frames[selectedFrame]?.line || 0;
|
|
|
|
|
const error = action?.error?.message;
|
2023-03-11 07:52:31 +01:00
|
|
|
return <SplitView sidebarSize={200} orientation='horizontal' sidebarHidden={hideStackFrames}>
|
2023-03-11 01:22:19 +01:00
|
|
|
<CodeMirrorWrapper text={content} language='javascript' highlight={[{ line: targetLine, type: error ? 'error' : 'running', message: error }]} revealLine={targetLine} readOnly={true} lineNumbers={true}></CodeMirrorWrapper>
|
2021-05-14 05:41:32 +02:00
|
|
|
<StackTraceView action={action} selectedFrame={selectedFrame} setSelectedFrame={setSelectedFrame}></StackTraceView>
|
2021-03-11 20:22:59 +01:00
|
|
|
</SplitView>;
|
2021-01-08 01:15:34 +01:00
|
|
|
};
|
2021-10-26 03:56:57 +02:00
|
|
|
|
|
|
|
|
export async function calculateSha1(text: string): Promise<string> {
|
|
|
|
|
const buffer = new TextEncoder().encode(text);
|
|
|
|
|
const hash = await crypto.subtle.digest('SHA-1', buffer);
|
|
|
|
|
const hexCodes = [];
|
|
|
|
|
const view = new DataView(hash);
|
|
|
|
|
for (let i = 0; i < view.byteLength; i += 1) {
|
|
|
|
|
const byte = view.getUint8(i).toString(16).padStart(2, '0');
|
|
|
|
|
hexCodes.push(byte);
|
|
|
|
|
}
|
|
|
|
|
return hexCodes.join('');
|
|
|
|
|
}
|