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-12 23:43:52 +01:00
|
|
|
import * as crypto from 'crypto';
|
2025-02-10 19:22:32 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as util from 'util';
|
|
|
|
|
|
|
|
|
|
export type Platform = {
|
2025-02-12 23:43:52 +01:00
|
|
|
calculateSha1(text: string): Promise<string>;
|
|
|
|
|
createGuid: () => string;
|
2025-02-10 19:22:32 +01:00
|
|
|
fs: () => typeof fs;
|
|
|
|
|
inspectCustom: symbol | undefined;
|
2025-02-12 23:43:52 +01:00
|
|
|
path: () => typeof path;
|
2025-02-13 03:03:23 +01:00
|
|
|
pathSeparator: string;
|
2025-02-11 17:28:28 +01:00
|
|
|
ws?: (url: string) => WebSocket;
|
2025-02-10 19:22:32 +01:00
|
|
|
};
|
|
|
|
|
|
2025-02-12 23:43:52 +01:00
|
|
|
export const nodePlatform: Platform = {
|
|
|
|
|
calculateSha1: (text: string) => {
|
|
|
|
|
const sha1 = crypto.createHash('sha1');
|
|
|
|
|
sha1.update(text);
|
|
|
|
|
return Promise.resolve(sha1.digest('hex'));
|
2025-02-10 19:22:32 +01:00
|
|
|
},
|
|
|
|
|
|
2025-02-12 23:43:52 +01:00
|
|
|
createGuid: () => crypto.randomBytes(16).toString('hex'),
|
2025-02-10 19:22:32 +01:00
|
|
|
|
|
|
|
|
fs: () => fs,
|
2025-02-12 23:43:52 +01:00
|
|
|
|
2025-02-10 19:22:32 +01:00
|
|
|
inspectCustom: util.inspect.custom,
|
2025-02-12 23:43:52 +01:00
|
|
|
|
|
|
|
|
path: () => path,
|
2025-02-13 03:03:23 +01:00
|
|
|
|
|
|
|
|
pathSeparator: path.sep
|
2025-02-10 19:22:32 +01:00
|
|
|
};
|
2025-02-11 17:28:28 +01:00
|
|
|
|
|
|
|
|
export const webPlatform: Platform = {
|
2025-02-12 23:43:52 +01:00
|
|
|
calculateSha1: async (text: string) => {
|
|
|
|
|
const bytes = new TextEncoder().encode(text);
|
|
|
|
|
const hashBuffer = await crypto.subtle.digest('SHA-1', bytes);
|
|
|
|
|
return Array.from(new Uint8Array(hashBuffer), b => b.toString(16).padStart(2, '0')).join('');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
createGuid: () => {
|
|
|
|
|
return Array.from(crypto.getRandomValues(new Uint8Array(16)), b => b.toString(16).padStart(2, '0')).join('');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
fs: () => {
|
|
|
|
|
throw new Error('File system is not available');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
inspectCustom: undefined,
|
|
|
|
|
|
|
|
|
|
path: () => {
|
|
|
|
|
throw new Error('Path module is not available');
|
|
|
|
|
},
|
|
|
|
|
|
2025-02-13 03:03:23 +01:00
|
|
|
pathSeparator: '/',
|
|
|
|
|
|
2025-02-11 17:28:28 +01:00
|
|
|
ws: (url: string) => new WebSocket(url),
|
|
|
|
|
};
|