2025-02-10 19:22:32 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-13 06:13:25 +01:00
|
|
|
import { webColors, noColors } from '../utils/isomorphic/colors';
|
|
|
|
|
|
2025-02-14 01:15:11 +01:00
|
|
|
import type * as fs from 'fs';
|
|
|
|
|
import type * as path from 'path';
|
2025-02-14 03:33:17 +01:00
|
|
|
import type { Readable, Writable } from 'stream';
|
2025-02-15 00:10:50 +01:00
|
|
|
import type { Colors } from '@isomorphic/colors';
|
2025-02-14 03:33:17 +01:00
|
|
|
import type * as channels from '@protocol/channels';
|
2025-02-13 06:13:25 +01:00
|
|
|
|
2025-02-13 22:06:03 +01:00
|
|
|
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,
|
|
|
|
|
};
|
2025-02-13 06:13:25 +01:00
|
|
|
|
2025-02-10 19:22:32 +01:00
|
|
|
export type Platform = {
|
2025-02-14 01:15:11 +01:00
|
|
|
name: 'node' | 'web' | 'empty';
|
|
|
|
|
|
2025-02-15 00:10:50 +01:00
|
|
|
boxedStackPrefixes: () => string[];
|
|
|
|
|
calculateSha1: (text: string) => Promise<string>;
|
2025-02-13 06:13:25 +01:00
|
|
|
colors: Colors;
|
2025-02-15 00:10:50 +01:00
|
|
|
coreDir?: string;
|
2025-02-12 23:43:52 +01:00
|
|
|
createGuid: () => string;
|
2025-02-15 00:10:50 +01:00
|
|
|
defaultMaxListeners: () => number;
|
2025-02-10 19:22:32 +01:00
|
|
|
fs: () => typeof fs;
|
|
|
|
|
inspectCustom: symbol | undefined;
|
2025-02-15 00:10:50 +01:00
|
|
|
isDebugMode: () => boolean;
|
|
|
|
|
isJSDebuggerAttached: () => boolean;
|
|
|
|
|
isLogEnabled: (name: 'api' | 'channel') => boolean;
|
|
|
|
|
isUnderTest: () => boolean,
|
|
|
|
|
log: (name: 'api' | 'channel', message: string | Error | object) => void;
|
2025-02-12 23:43:52 +01:00
|
|
|
path: () => typeof path;
|
2025-02-13 03:03:23 +01:00
|
|
|
pathSeparator: string;
|
2025-02-15 00:10:50 +01:00
|
|
|
streamFile: (path: string, writable: Writable) => Promise<void>,
|
2025-02-14 03:33:17 +01:00
|
|
|
streamReadable: (channel: channels.StreamChannel) => Readable,
|
|
|
|
|
streamWritable: (channel: channels.WritableStreamChannel) => Writable,
|
2025-02-13 22:06:03 +01:00
|
|
|
zones: { empty: Zone, current: () => Zone; };
|
2025-02-10 19:22:32 +01:00
|
|
|
};
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
export const emptyPlatform: Platform = {
|
|
|
|
|
name: 'empty',
|
2025-02-14 01:15:11 +01:00
|
|
|
|
2025-02-15 00:10:50 +01:00
|
|
|
boxedStackPrefixes: () => [],
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
calculateSha1: async () => {
|
|
|
|
|
throw new Error('Not implemented');
|
2025-02-12 23:43:52 +01:00
|
|
|
},
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
colors: noColors,
|
2025-02-13 06:13:25 +01:00
|
|
|
|
2025-02-12 23:43:52 +01:00
|
|
|
createGuid: () => {
|
2025-02-14 03:33:17 +01:00
|
|
|
throw new Error('Not implemented');
|
2025-02-12 23:43:52 +01:00
|
|
|
},
|
|
|
|
|
|
2025-02-15 00:10:50 +01:00
|
|
|
defaultMaxListeners: () => 10,
|
|
|
|
|
|
2025-02-12 23:43:52 +01:00
|
|
|
fs: () => {
|
2025-02-14 03:33:17 +01:00
|
|
|
throw new Error('Not implemented');
|
2025-02-12 23:43:52 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
inspectCustom: undefined,
|
|
|
|
|
|
2025-02-15 00:10:50 +01:00
|
|
|
isDebugMode: () => false,
|
|
|
|
|
|
|
|
|
|
isJSDebuggerAttached: () => false,
|
2025-02-13 04:27:24 +01:00
|
|
|
|
|
|
|
|
isLogEnabled(name: 'api' | 'channel') {
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
|
2025-02-15 00:10:50 +01:00
|
|
|
isUnderTest: () => false,
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
log(name: 'api' | 'channel', message: string | Error | object) { },
|
2025-02-13 04:27:24 +01:00
|
|
|
|
2025-02-12 23:43:52 +01:00
|
|
|
path: () => {
|
2025-02-14 03:33:17 +01:00
|
|
|
throw new Error('Function not implemented.');
|
2025-02-12 23:43:52 +01:00
|
|
|
},
|
|
|
|
|
|
2025-02-13 03:03:23 +01:00
|
|
|
pathSeparator: '/',
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
streamFile(path: string, writable: Writable): Promise<void> {
|
|
|
|
|
throw new Error('Streams are not available');
|
2025-02-13 04:27:24 +01:00
|
|
|
},
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
streamReadable: (channel: channels.StreamChannel) => {
|
|
|
|
|
throw new Error('Streams are not available');
|
2025-02-13 04:27:24 +01:00
|
|
|
},
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
streamWritable: (channel: channels.WritableStreamChannel) => {
|
|
|
|
|
throw new Error('Streams are not available');
|
2025-02-13 04:27:24 +01:00
|
|
|
},
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
zones: { empty: noopZone, current: () => noopZone },
|
|
|
|
|
};
|
2025-02-14 01:46:24 +01:00
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
export const webPlatform: Platform = {
|
|
|
|
|
...emptyPlatform,
|
2025-02-13 04:27:24 +01:00
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
name: 'web',
|
2025-02-13 04:27:24 +01:00
|
|
|
|
2025-02-15 00:10:50 +01:00
|
|
|
boxedStackPrefixes: () => [],
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
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('');
|
2025-02-13 04:27:24 +01:00
|
|
|
},
|
|
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
colors: webColors,
|
2025-02-13 22:06:03 +01:00
|
|
|
|
2025-02-14 03:33:17 +01:00
|
|
|
createGuid: () => {
|
|
|
|
|
return Array.from(window.crypto.getRandomValues(new Uint8Array(16)), b => b.toString(16).padStart(2, '0')).join('');
|
|
|
|
|
},
|
2025-02-13 04:27:24 +01:00
|
|
|
};
|