mirror of
https://github.com/oven-sh/setup-bun.git
synced 2025-02-24 03:00:39 +08:00
198 lines
6.8 KiB
JavaScript
198 lines
6.8 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
var abortController = require('@azure/abort-controller');
|
|
var crypto = require('crypto');
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT license.
|
|
var _a;
|
|
/**
|
|
* A constant that indicates whether the environment the code is running is Node.JS.
|
|
*/
|
|
const isNode = typeof process !== "undefined" && Boolean(process.version) && Boolean((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node);
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT license.
|
|
/**
|
|
* Helper TypeGuard that checks if something is defined or not.
|
|
* @param thing - Anything
|
|
*/
|
|
function isDefined(thing) {
|
|
return typeof thing !== "undefined" && thing !== null;
|
|
}
|
|
/**
|
|
* Helper TypeGuard that checks if the input is an object with the specified properties.
|
|
* @param thing - Anything.
|
|
* @param properties - The name of the properties that should appear in the object.
|
|
*/
|
|
function isObjectWithProperties(thing, properties) {
|
|
if (!isDefined(thing) || typeof thing !== "object") {
|
|
return false;
|
|
}
|
|
for (const property of properties) {
|
|
if (!objectHasProperty(thing, property)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
/**
|
|
* Helper TypeGuard that checks if the input is an object with the specified property.
|
|
* @param thing - Any object.
|
|
* @param property - The name of the property that should appear in the object.
|
|
*/
|
|
function objectHasProperty(thing, property) {
|
|
return (isDefined(thing) && typeof thing === "object" && property in thing);
|
|
}
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
const StandardAbortMessage = "The operation was aborted.";
|
|
/**
|
|
* A wrapper for setTimeout that resolves a promise after timeInMs milliseconds.
|
|
* @param timeInMs - The number of milliseconds to be delayed.
|
|
* @param options - The options for delay - currently abort options
|
|
* @returns Promise that is resolved after timeInMs
|
|
*/
|
|
function delay(timeInMs, options) {
|
|
return new Promise((resolve, reject) => {
|
|
let timer = undefined;
|
|
let onAborted = undefined;
|
|
const rejectOnAbort = () => {
|
|
var _a;
|
|
return reject(new abortController.AbortError((_a = options === null || options === void 0 ? void 0 : options.abortErrorMsg) !== null && _a !== void 0 ? _a : StandardAbortMessage));
|
|
};
|
|
const removeListeners = () => {
|
|
if ((options === null || options === void 0 ? void 0 : options.abortSignal) && onAborted) {
|
|
options.abortSignal.removeEventListener("abort", onAborted);
|
|
}
|
|
};
|
|
onAborted = () => {
|
|
if (isDefined(timer)) {
|
|
clearTimeout(timer);
|
|
}
|
|
removeListeners();
|
|
return rejectOnAbort();
|
|
};
|
|
if ((options === null || options === void 0 ? void 0 : options.abortSignal) && options.abortSignal.aborted) {
|
|
return rejectOnAbort();
|
|
}
|
|
timer = setTimeout(() => {
|
|
removeListeners();
|
|
resolve();
|
|
}, timeInMs);
|
|
if (options === null || options === void 0 ? void 0 : options.abortSignal) {
|
|
options.abortSignal.addEventListener("abort", onAborted);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT license.
|
|
/**
|
|
* Returns a random integer value between a lower and upper bound,
|
|
* inclusive of both bounds.
|
|
* Note that this uses Math.random and isn't secure. If you need to use
|
|
* this for any kind of security purpose, find a better source of random.
|
|
* @param min - The smallest integer value allowed.
|
|
* @param max - The largest integer value allowed.
|
|
*/
|
|
function getRandomIntegerInclusive(min, max) {
|
|
// Make sure inputs are integers.
|
|
min = Math.ceil(min);
|
|
max = Math.floor(max);
|
|
// Pick a random offset from zero to the size of the range.
|
|
// Since Math.random() can never return 1, we have to make the range one larger
|
|
// in order to be inclusive of the maximum value after we take the floor.
|
|
const offset = Math.floor(Math.random() * (max - min + 1));
|
|
return offset + min;
|
|
}
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT license.
|
|
/**
|
|
* Helper to determine when an input is a generic JS object.
|
|
* @returns true when input is an object type that is not null, Array, RegExp, or Date.
|
|
*/
|
|
function isObject(input) {
|
|
return (typeof input === "object" &&
|
|
input !== null &&
|
|
!Array.isArray(input) &&
|
|
!(input instanceof RegExp) &&
|
|
!(input instanceof Date));
|
|
}
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
/**
|
|
* Typeguard for an error object shape (has name and message)
|
|
* @param e - Something caught by a catch clause.
|
|
*/
|
|
function isError(e) {
|
|
if (isObject(e)) {
|
|
const hasName = typeof e.name === "string";
|
|
const hasMessage = typeof e.message === "string";
|
|
return hasName && hasMessage;
|
|
}
|
|
return false;
|
|
}
|
|
/**
|
|
* Given what is thought to be an error object, return the message if possible.
|
|
* If the message is missing, returns a stringified version of the input.
|
|
* @param e - Something thrown from a try block
|
|
* @returns The error message or a string of the input
|
|
*/
|
|
function getErrorMessage(e) {
|
|
if (isError(e)) {
|
|
return e.message;
|
|
}
|
|
else {
|
|
let stringified;
|
|
try {
|
|
if (typeof e === "object" && e) {
|
|
stringified = JSON.stringify(e);
|
|
}
|
|
else {
|
|
stringified = String(e);
|
|
}
|
|
}
|
|
catch (err) {
|
|
stringified = "[unable to stringify input]";
|
|
}
|
|
return `Unknown error ${stringified}`;
|
|
}
|
|
}
|
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
/**
|
|
* Generates a SHA-256 HMAC signature.
|
|
* @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
|
|
* @param stringToSign - The data to be signed.
|
|
* @param encoding - The textual encoding to use for the returned HMAC digest.
|
|
*/
|
|
async function computeSha256Hmac(key, stringToSign, encoding) {
|
|
const decodedKey = Buffer.from(key, "base64");
|
|
return crypto.createHmac("sha256", decodedKey).update(stringToSign).digest(encoding);
|
|
}
|
|
/**
|
|
* Generates a SHA-256 hash.
|
|
* @param content - The data to be included in the hash.
|
|
* @param encoding - The textual encoding to use for the returned hash.
|
|
*/
|
|
async function computeSha256Hash(content, encoding) {
|
|
return crypto.createHash("sha256").update(content).digest(encoding);
|
|
}
|
|
|
|
exports.computeSha256Hash = computeSha256Hash;
|
|
exports.computeSha256Hmac = computeSha256Hmac;
|
|
exports.delay = delay;
|
|
exports.getErrorMessage = getErrorMessage;
|
|
exports.getRandomIntegerInclusive = getRandomIntegerInclusive;
|
|
exports.isDefined = isDefined;
|
|
exports.isError = isError;
|
|
exports.isNode = isNode;
|
|
exports.isObject = isObject;
|
|
exports.isObjectWithProperties = isObjectWithProperties;
|
|
exports.objectHasProperty = objectHasProperty;
|
|
//# sourceMappingURL=index.js.map
|