@azure/core-util
Advanced tools
Comparing version 1.0.0-beta.1 to 1.0.0
# Release History | ||
## 1.0.0 (2022-05-05) | ||
### Features Added | ||
- Add helpers `isObject`, `isError`, `getErrorMessage` for handling unknown Error objects. | ||
- Add helper `getRandomIntegerInclusive` for randomly selecting a whole integer value from a given range. | ||
### Other Changes | ||
- Updates package to work with the react native bundler. [PR #17783](https://github.com/Azure/azure-sdk-for-js/pull/17783) | ||
## 1.0.0-beta.1 (2021-05-06) | ||
@@ -4,0 +15,0 @@ |
@@ -5,2 +5,6 @@ // Copyright (c) Microsoft Corporation. | ||
export { delay } from "./delay"; | ||
export { getRandomIntegerInclusive } from "./random"; | ||
export { isObject } from "./object"; | ||
export { isError, getErrorMessage } from "./error"; | ||
export { computeSha256Hash, computeSha256Hmac } from "./sha256"; | ||
//# sourceMappingURL=index.js.map |
@@ -5,2 +5,4 @@ 'use strict'; | ||
var crypto = require('crypto'); | ||
// Copyright (c) Microsoft Corporation. | ||
@@ -25,4 +27,105 @@ // Licensed under the MIT license. | ||
// 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.isError = isError; | ||
exports.isNode = isNode; | ||
exports.isObject = isObject; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@azure/core-util", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0", | ||
"description": "Core library for shared utility methods", | ||
@@ -9,4 +9,8 @@ "sdk-type": "client", | ||
"browser": { | ||
"./dist-esm/src/isNode.js": "./dist-esm/src/isNode.browser.js" | ||
"./dist-esm/src/isNode.js": "./dist-esm/src/isNode.browser.js", | ||
"./dist-esm/src/sha256.js": "./dist-esm/src/sha256.browser.js" | ||
}, | ||
"react-native": { | ||
"./dist/index.js": "./dist-esm/src/index.js" | ||
}, | ||
"types": "types/latest/core-util.d.ts", | ||
@@ -22,12 +26,11 @@ "typesVersions": { | ||
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit", | ||
"build:samples": "echo Skipped.", | ||
"build:test": "echo Just call build instead", | ||
"build:ts": "tsc -p .", | ||
"build:samples": "echo Obsolete", | ||
"build:test": "tsc -p . && dev-tool run bundle", | ||
"build:types": "downlevel-dts types/latest/ types/3.1/", | ||
"build": "npm run build:ts && rollup -c 2>&1 && api-extractor run --local && npm run build:types", | ||
"check-format": "prettier --list-different \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", | ||
"clean": "rimraf dist dist-* types *.tgz *.log", | ||
"build": "npm run clean && tsc -p . && dev-tool run bundle && api-extractor run --local && npm run build:types", | ||
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", | ||
"clean": "rimraf dist dist-* temp types *.tgz *.log", | ||
"execute:samples": "echo skipped", | ||
"extract-api": "npm run build:ts && api-extractor run --local", | ||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", | ||
"extract-api": "tsc -p . && api-extractor run --local", | ||
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"", | ||
"integration-test:browser": "echo skipped", | ||
@@ -39,10 +42,8 @@ "integration-test:node": "echo skipped", | ||
"pack": "npm pack 2>&1", | ||
"prebuild": "npm run clean", | ||
"test:browser": "npm run build:test:browser && npm run unit-test:browser && npm run integration-test:browser", | ||
"test:node": "npm run build:test:node && npm run unit-test:node && npm run integration-test:node", | ||
"test": "npm run unit-test:node && npm run build && npm run unit-test:browser && npm run integration-test:node && npm run integration-test:browser", | ||
"test:browser": "npm run clean && npm run build:test && npm run unit-test:browser && npm run integration-test:browser", | ||
"test:node": "npm run clean && tsc -p . && npm run unit-test:node && npm run integration-test:node", | ||
"test": "npm run clean && tsc -p . && npm run unit-test:node && dev-tool run bundle && npm run unit-test:browser && npm run integration-test", | ||
"unit-test:browser": "karma start --single-run", | ||
"unit-test:node": "mocha -r esm --require ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace \"test/{,!(browser)/**/}*.spec.ts\"", | ||
"unit-test": "npm run unit-test:node && npm run unit-test:browser", | ||
"docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --stripInternal --mode file --out ./dist/docs ./src" | ||
"unit-test:node": "mocha -r esm -r ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace --exclude \"test/**/browser/*.spec.ts\" \"test/**/*.spec.ts\"", | ||
"unit-test": "npm run unit-test:node && npm run unit-test:browser" | ||
}, | ||
@@ -68,25 +69,20 @@ "files": [ | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=12.0.0" | ||
}, | ||
"homepage": "https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/core/core-util/", | ||
"homepage": "https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-util/", | ||
"sideEffects": false, | ||
"prettier": "@azure/eslint-plugin-azure-sdk/prettier.json", | ||
"dependencies": { | ||
"tslib": "^2.0.0" | ||
"tslib": "^2.2.0" | ||
}, | ||
"devDependencies": { | ||
"@azure/dev-tool": "^1.0.0", | ||
"@microsoft/api-extractor": "7.7.11", | ||
"@rollup/plugin-commonjs": "11.0.2", | ||
"@rollup/plugin-json": "^4.0.0", | ||
"@rollup/plugin-multi-entry": "^3.0.0", | ||
"@rollup/plugin-node-resolve": "^8.0.0", | ||
"@rollup/plugin-replace": "^2.2.0", | ||
"@microsoft/api-extractor": "7.18.11", | ||
"@types/chai": "^4.1.6", | ||
"@types/mocha": "^7.0.2", | ||
"@types/node": "^8.0.0", | ||
"@types/node": "^12.0.0", | ||
"@types/sinon": "^9.0.4", | ||
"@azure/eslint-plugin-azure-sdk": "^3.0.0", | ||
"chai": "^4.2.0", | ||
"downlevel-dts": "~0.4.0", | ||
"downlevel-dts": "^0.8.0", | ||
"cross-env": "^7.0.2", | ||
@@ -107,14 +103,9 @@ "eslint": "^7.15.0", | ||
"mocha": "^7.1.1", | ||
"mocha-junit-reporter": "^1.18.0", | ||
"prettier": "^1.16.4", | ||
"mocha-junit-reporter": "^2.0.0", | ||
"prettier": "^2.5.1", | ||
"rimraf": "^3.0.0", | ||
"rollup": "^1.16.3", | ||
"rollup-plugin-sourcemaps": "^0.4.2", | ||
"rollup-plugin-terser": "^5.1.1", | ||
"rollup-plugin-visualizer": "^4.0.4", | ||
"sinon": "^9.0.2", | ||
"typescript": "~4.2.0", | ||
"util": "^0.12.1", | ||
"typedoc": "0.15.2" | ||
"typescript": "~4.6.0", | ||
"util": "^0.12.1" | ||
} | ||
} |
@@ -9,4 +9,9 @@ # Azure Core Util client library for JavaScript (Experimental) | ||
- [Node.js](https://nodejs.org) version > 8.x | ||
### Currently supported environments | ||
- [LTS versions of Node.js](https://nodejs.org/about/releases/) | ||
- Latest versions of Safari, Chrome, Edge, and Firefox. | ||
See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) for more details. | ||
### Installation | ||
@@ -34,4 +39,4 @@ | ||
If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/master/CONTRIBUTING.md) to learn more about how to build and test the code. | ||
If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code. | ||
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fcore%2Fcore-util%2FREADME.png) |
/** | ||
* 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. | ||
*/ | ||
export declare function computeSha256Hash(content: string, encoding: "base64" | "hex"): Promise<string>; | ||
/** | ||
* 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. | ||
*/ | ||
export declare function computeSha256Hmac(key: string, stringToSign: string, encoding: "base64" | "hex"): Promise<string>; | ||
/** | ||
* A wrapper for setTimeout that resolves a promise after timeInMs milliseconds. | ||
@@ -8,5 +21,37 @@ * @param timeInMs - The number of milliseconds to be delayed. | ||
/** | ||
* 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 | ||
*/ | ||
export declare function getErrorMessage(e: unknown): string; | ||
/** | ||
* 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. | ||
*/ | ||
export declare function getRandomIntegerInclusive(min: number, max: number): number; | ||
/** | ||
* Typeguard for an error object shape (has name and message) | ||
* @param e - Something caught by a catch clause. | ||
*/ | ||
export declare function isError(e: unknown): e is Error; | ||
/** | ||
* A constant that indicates whether the environment the code is running is Node.JS. | ||
*/ | ||
export declare const isNode: boolean; | ||
/** | ||
* 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. | ||
*/ | ||
export declare function isObject(input: unknown): input is UnknownObject; | ||
/** | ||
* A generic shape for a plain JS object. | ||
*/ | ||
export declare type UnknownObject = { | ||
[s: string]: unknown; | ||
}; | ||
export {}; |
@@ -0,3 +1,17 @@ | ||
/** | ||
* 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. | ||
*/ | ||
export declare function computeSha256Hash(content: string, encoding: "base64" | "hex"): Promise<string>; | ||
/** | ||
* 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. | ||
*/ | ||
export declare function computeSha256Hmac(key: string, stringToSign: string, encoding: "base64" | "hex"): Promise<string>; | ||
/** | ||
* A wrapper for setTimeout that resolves a promise after timeInMs milliseconds. | ||
@@ -10,2 +24,26 @@ * @param timeInMs - The number of milliseconds to be delayed. | ||
/** | ||
* 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 | ||
*/ | ||
export declare function getErrorMessage(e: unknown): string; | ||
/** | ||
* 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. | ||
*/ | ||
export declare function getRandomIntegerInclusive(min: number, max: number): number; | ||
/** | ||
* Typeguard for an error object shape (has name and message) | ||
* @param e - Something caught by a catch clause. | ||
*/ | ||
export declare function isError(e: unknown): e is Error; | ||
/** | ||
* A constant that indicates whether the environment the code is running is Node.JS. | ||
@@ -15,2 +53,15 @@ */ | ||
/** | ||
* 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. | ||
*/ | ||
export declare function isObject(input: unknown): input is UnknownObject; | ||
/** | ||
* A generic shape for a plain JS object. | ||
*/ | ||
export declare type UnknownObject = { | ||
[s: string]: unknown; | ||
}; | ||
export { } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51166
30
32
497
0
41
1
Updatedtslib@^2.2.0