playwright/packages/playwright-core/src/common/platform.ts

122 lines
2.9 KiB
TypeScript
Raw Normal View History

/**
* 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.
*/
import { webColors, noColors } from '../utils/isomorphic/colors';
import type * as fs from 'fs';
import type * as path from 'path';
import type { Colors } from '../utils/isomorphic/colors';
export type Zone = {
push(data: unknown): Zone;
pop(): Zone;
run<R>(func: () => R): R;
data<T>(): T | undefined;
};
const noopZone: Zone = {
push: () => noopZone,
pop: () => noopZone,
run: func => func(),
data: () => undefined,
};
export type Platform = {
name: 'node' | 'web' | 'empty';
calculateSha1(text: string): Promise<string>;
colors: Colors;
createGuid: () => string;
fs: () => typeof fs;
inspectCustom: symbol | undefined;
isLogEnabled(name: 'api' | 'channel'): boolean;
log(name: 'api' | 'channel', message: string | Error | object): void;
path: () => typeof path;
pathSeparator: string;
zones: { empty: Zone, current: () => Zone; };
};
export const webPlatform: Platform = {
name: 'web',
calculateSha1: async (text: string) => {
const bytes = new TextEncoder().encode(text);
const hashBuffer = await window.crypto.subtle.digest('SHA-1', bytes);
return Array.from(new Uint8Array(hashBuffer), b => b.toString(16).padStart(2, '0')).join('');
},
colors: webColors,
createGuid: () => {
return Array.from(window.crypto.getRandomValues(new Uint8Array(16)), b => b.toString(16).padStart(2, '0')).join('');
},
fs: () => {
throw new Error('File system is not available');
},
inspectCustom: undefined,
isLogEnabled(name: 'api' | 'channel') {
return false;
},
log(name: 'api' | 'channel', message: string | Error | object) {},
path: () => {
throw new Error('Path module is not available');
},
pathSeparator: '/',
zones: { empty: noopZone, current: () => noopZone },
};
export const emptyPlatform: Platform = {
name: 'empty',
calculateSha1: async () => {
throw new Error('Not implemented');
},
colors: noColors,
createGuid: () => {
throw new Error('Not implemented');
},
fs: () => {
throw new Error('Not implemented');
},
inspectCustom: undefined,
isLogEnabled(name: 'api' | 'channel') {
return false;
},
log(name: 'api' | 'channel', message: string | Error | object) { },
path: () => {
throw new Error('Function not implemented.');
},
pathSeparator: '/',
zones: { empty: noopZone, current: () => noopZone },
};