@azure/core-util 1.0.0-beta.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Release History
2
2
 
3
+ ## 1.0.0 (2022-05-05)
4
+
5
+ ### Features Added
6
+
7
+ - Add helpers `isObject`, `isError`, `getErrorMessage` for handling unknown Error objects.
8
+ - Add helper `getRandomIntegerInclusive` for randomly selecting a whole integer value from a given range.
9
+
10
+ ### Other Changes
11
+
12
+ - Updates package to work with the react native bundler. [PR #17783](https://github.com/Azure/azure-sdk-for-js/pull/17783)
13
+
3
14
  ## 1.0.0-beta.1 (2021-05-06)
4
15
 
5
16
  ### Features Added
package/README.md CHANGED
@@ -6,7 +6,12 @@ This library is intended to provide various shared utility functions for client
6
6
 
7
7
  ### Requirements
8
8
 
9
- - [Node.js](https://nodejs.org) version > 8.x
9
+ ### Currently supported environments
10
+
11
+ - [LTS versions of Node.js](https://nodejs.org/about/releases/)
12
+ - Latest versions of Safari, Chrome, Edge, and Firefox.
13
+
14
+ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) for more details.
10
15
 
11
16
  ### Installation
12
17
 
@@ -30,6 +35,6 @@ If you run into issues while using this library, please feel free to [file an is
30
35
 
31
36
  ## Contributing
32
37
 
33
- 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.
38
+ 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.
34
39
 
35
40
  ![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fcore%2Fcore-util%2FREADME.png)
package/dist/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var crypto = require('crypto');
6
+
5
7
  // Copyright (c) Microsoft Corporation.
6
8
  // Licensed under the MIT license.
7
9
  var _a;
@@ -21,6 +23,107 @@ function delay(timeInMs) {
21
23
  return new Promise((resolve) => setTimeout(() => resolve(), timeInMs));
22
24
  }
23
25
 
26
+ // Copyright (c) Microsoft Corporation.
27
+ // Licensed under the MIT license.
28
+ /**
29
+ * Returns a random integer value between a lower and upper bound,
30
+ * inclusive of both bounds.
31
+ * Note that this uses Math.random and isn't secure. If you need to use
32
+ * this for any kind of security purpose, find a better source of random.
33
+ * @param min - The smallest integer value allowed.
34
+ * @param max - The largest integer value allowed.
35
+ */
36
+ function getRandomIntegerInclusive(min, max) {
37
+ // Make sure inputs are integers.
38
+ min = Math.ceil(min);
39
+ max = Math.floor(max);
40
+ // Pick a random offset from zero to the size of the range.
41
+ // Since Math.random() can never return 1, we have to make the range one larger
42
+ // in order to be inclusive of the maximum value after we take the floor.
43
+ const offset = Math.floor(Math.random() * (max - min + 1));
44
+ return offset + min;
45
+ }
46
+
47
+ // Copyright (c) Microsoft Corporation.
48
+ // Licensed under the MIT license.
49
+ /**
50
+ * Helper to determine when an input is a generic JS object.
51
+ * @returns true when input is an object type that is not null, Array, RegExp, or Date.
52
+ */
53
+ function isObject(input) {
54
+ return (typeof input === "object" &&
55
+ input !== null &&
56
+ !Array.isArray(input) &&
57
+ !(input instanceof RegExp) &&
58
+ !(input instanceof Date));
59
+ }
60
+
61
+ // Copyright (c) Microsoft Corporation.
62
+ /**
63
+ * Typeguard for an error object shape (has name and message)
64
+ * @param e - Something caught by a catch clause.
65
+ */
66
+ function isError(e) {
67
+ if (isObject(e)) {
68
+ const hasName = typeof e.name === "string";
69
+ const hasMessage = typeof e.message === "string";
70
+ return hasName && hasMessage;
71
+ }
72
+ return false;
73
+ }
74
+ /**
75
+ * Given what is thought to be an error object, return the message if possible.
76
+ * If the message is missing, returns a stringified version of the input.
77
+ * @param e - Something thrown from a try block
78
+ * @returns The error message or a string of the input
79
+ */
80
+ function getErrorMessage(e) {
81
+ if (isError(e)) {
82
+ return e.message;
83
+ }
84
+ else {
85
+ let stringified;
86
+ try {
87
+ if (typeof e === "object" && e) {
88
+ stringified = JSON.stringify(e);
89
+ }
90
+ else {
91
+ stringified = String(e);
92
+ }
93
+ }
94
+ catch (err) {
95
+ stringified = "[unable to stringify input]";
96
+ }
97
+ return `Unknown error ${stringified}`;
98
+ }
99
+ }
100
+
101
+ // Copyright (c) Microsoft Corporation.
102
+ /**
103
+ * Generates a SHA-256 HMAC signature.
104
+ * @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
105
+ * @param stringToSign - The data to be signed.
106
+ * @param encoding - The textual encoding to use for the returned HMAC digest.
107
+ */
108
+ async function computeSha256Hmac(key, stringToSign, encoding) {
109
+ const decodedKey = Buffer.from(key, "base64");
110
+ return crypto.createHmac("sha256", decodedKey).update(stringToSign).digest(encoding);
111
+ }
112
+ /**
113
+ * Generates a SHA-256 hash.
114
+ * @param content - The data to be included in the hash.
115
+ * @param encoding - The textual encoding to use for the returned hash.
116
+ */
117
+ async function computeSha256Hash(content, encoding) {
118
+ return crypto.createHash("sha256").update(content).digest(encoding);
119
+ }
120
+
121
+ exports.computeSha256Hash = computeSha256Hash;
122
+ exports.computeSha256Hmac = computeSha256Hmac;
24
123
  exports.delay = delay;
124
+ exports.getErrorMessage = getErrorMessage;
125
+ exports.getRandomIntegerInclusive = getRandomIntegerInclusive;
126
+ exports.isError = isError;
25
127
  exports.isNode = isNode;
128
+ exports.isObject = isObject;
26
129
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/isNode.ts","../src/delay.ts"],"sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * A constant that indicates whether the environment the code is running is Node.JS.\n */\nexport const isNode =\n typeof process !== \"undefined\" && Boolean(process.version) && Boolean(process.versions?.node);\n","// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * A wrapper for setTimeout that resolves a promise after timeInMs milliseconds.\n * @param timeInMs - The number of milliseconds to be delayed.\n * @returns Promise that is resolved after timeInMs\n */\nexport function delay(timeInMs: number): Promise<void> {\n return new Promise((resolve) => setTimeout(() => resolve(), timeInMs));\n}\n"],"names":[],"mappings":";;;;AAAA;AACA;;AAEA;;;MAGa,MAAM,GACjB,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAA,OAAO,CAAC,QAAQ,0CAAE,IAAI;;ACP9F;AACA;AAEA;;;;;AAKA,SAAgB,KAAK,CAAC,QAAgB;IACpC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,KAAK,UAAU,CAAC,MAAM,OAAO,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC;AACzE,CAAC;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/isNode.ts","../src/delay.ts","../src/random.ts","../src/object.ts","../src/error.ts","../src/sha256.ts"],"sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * A constant that indicates whether the environment the code is running is Node.JS.\n */\nexport const isNode =\n typeof process !== \"undefined\" && Boolean(process.version) && Boolean(process.versions?.node);\n","// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * A wrapper for setTimeout that resolves a promise after timeInMs milliseconds.\n * @param timeInMs - The number of milliseconds to be delayed.\n * @returns Promise that is resolved after timeInMs\n */\nexport function delay(timeInMs: number): Promise<void> {\n return new Promise((resolve) => setTimeout(() => resolve(), timeInMs));\n}\n","// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Returns a random integer value between a lower and upper bound,\n * inclusive of both bounds.\n * Note that this uses Math.random and isn't secure. If you need to use\n * this for any kind of security purpose, find a better source of random.\n * @param min - The smallest integer value allowed.\n * @param max - The largest integer value allowed.\n */\nexport function getRandomIntegerInclusive(min: number, max: number): number {\n // Make sure inputs are integers.\n min = Math.ceil(min);\n max = Math.floor(max);\n // Pick a random offset from zero to the size of the range.\n // Since Math.random() can never return 1, we have to make the range one larger\n // in order to be inclusive of the maximum value after we take the floor.\n const offset = Math.floor(Math.random() * (max - min + 1));\n return offset + min;\n}\n","// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * A generic shape for a plain JS object.\n */\nexport type UnknownObject = { [s: string]: unknown };\n\n/**\n * Helper to determine when an input is a generic JS object.\n * @returns true when input is an object type that is not null, Array, RegExp, or Date.\n */\nexport function isObject(input: unknown): input is UnknownObject {\n return (\n typeof input === \"object\" &&\n input !== null &&\n !Array.isArray(input) &&\n !(input instanceof RegExp) &&\n !(input instanceof Date)\n );\n}\n","// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { isObject } from \"./object\";\n\n/**\n * Typeguard for an error object shape (has name and message)\n * @param e - Something caught by a catch clause.\n */\nexport function isError(e: unknown): e is Error {\n if (isObject(e)) {\n const hasName = typeof e.name === \"string\";\n const hasMessage = typeof e.message === \"string\";\n return hasName && hasMessage;\n }\n return false;\n}\n\n/**\n * Given what is thought to be an error object, return the message if possible.\n * If the message is missing, returns a stringified version of the input.\n * @param e - Something thrown from a try block\n * @returns The error message or a string of the input\n */\nexport function getErrorMessage(e: unknown): string {\n if (isError(e)) {\n return e.message;\n } else {\n let stringified: string;\n try {\n if (typeof e === \"object\" && e) {\n stringified = JSON.stringify(e);\n } else {\n stringified = String(e);\n }\n } catch (err: any) {\n stringified = \"[unable to stringify input]\";\n }\n return `Unknown error ${stringified}`;\n }\n}\n","// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { createHash, createHmac } from \"crypto\";\n\n/**\n * Generates a SHA-256 HMAC signature.\n * @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.\n * @param stringToSign - The data to be signed.\n * @param encoding - The textual encoding to use for the returned HMAC digest.\n */\nexport async function computeSha256Hmac(\n key: string,\n stringToSign: string,\n encoding: \"base64\" | \"hex\"\n): Promise<string> {\n const decodedKey = Buffer.from(key, \"base64\");\n\n return createHmac(\"sha256\", decodedKey).update(stringToSign).digest(encoding);\n}\n\n/**\n * Generates a SHA-256 hash.\n * @param content - The data to be included in the hash.\n * @param encoding - The textual encoding to use for the returned hash.\n */\nexport async function computeSha256Hash(\n content: string,\n encoding: \"base64\" | \"hex\"\n): Promise<string> {\n return createHash(\"sha256\").update(content).digest(encoding);\n}\n"],"names":["createHmac","createHash"],"mappings":"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"}
@@ -0,0 +1,35 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ /**
4
+ * Converts a base64 string into a byte array.
5
+ * @param content - The base64 string to convert.
6
+ * @internal
7
+ */
8
+ export function base64ToBytes(content) {
9
+ if (typeof atob !== "function") {
10
+ throw new Error(`Your browser environment is missing the global "atob" function.`);
11
+ }
12
+ const binary = atob(content);
13
+ const bytes = new Uint8Array(binary.length);
14
+ for (let i = 0; i < binary.length; i++) {
15
+ bytes[i] = binary.charCodeAt(i);
16
+ }
17
+ return bytes;
18
+ }
19
+ /**
20
+ * Converts an ArrayBuffer to base64 string.
21
+ * @param buffer - Raw binary data.
22
+ * @internal
23
+ */
24
+ export function bufferToBase64(buffer) {
25
+ if (typeof btoa !== "function") {
26
+ throw new Error(`Your browser environment is missing the global "btoa" function.`);
27
+ }
28
+ const bytes = new Uint8Array(buffer);
29
+ let binary = "";
30
+ for (const byte of bytes) {
31
+ binary += String.fromCharCode(byte);
32
+ }
33
+ return btoa(binary);
34
+ }
35
+ //# sourceMappingURL=base64.browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.browser.js","sourceRoot":"","sources":["../../src/base64.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAQlC;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,OAAe;IAC3C,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;KACpF;IAED,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACtC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;KACjC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,MAAmB;IAChD,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;KACpF;IAED,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACxB,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;KACrC;IACD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;AACtB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\ndeclare global {\n // stub these out for the browser\n function btoa(input: string): string;\n function atob(input: string): string;\n}\n\n/**\n * Converts a base64 string into a byte array.\n * @param content - The base64 string to convert.\n * @internal\n */\nexport function base64ToBytes(content: string): Uint8Array {\n if (typeof atob !== \"function\") {\n throw new Error(`Your browser environment is missing the global \"atob\" function.`);\n }\n\n const binary = atob(content);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i);\n }\n\n return bytes;\n}\n\n/**\n * Converts an ArrayBuffer to base64 string.\n * @param buffer - Raw binary data.\n * @internal\n */\nexport function bufferToBase64(buffer: ArrayBuffer): string {\n if (typeof btoa !== \"function\") {\n throw new Error(`Your browser environment is missing the global \"btoa\" function.`);\n }\n\n const bytes = new Uint8Array(buffer);\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n"]}
@@ -0,0 +1,42 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ import { isObject } from "./object";
4
+ /**
5
+ * Typeguard for an error object shape (has name and message)
6
+ * @param e - Something caught by a catch clause.
7
+ */
8
+ export function isError(e) {
9
+ if (isObject(e)) {
10
+ const hasName = typeof e.name === "string";
11
+ const hasMessage = typeof e.message === "string";
12
+ return hasName && hasMessage;
13
+ }
14
+ return false;
15
+ }
16
+ /**
17
+ * Given what is thought to be an error object, return the message if possible.
18
+ * If the message is missing, returns a stringified version of the input.
19
+ * @param e - Something thrown from a try block
20
+ * @returns The error message or a string of the input
21
+ */
22
+ export function getErrorMessage(e) {
23
+ if (isError(e)) {
24
+ return e.message;
25
+ }
26
+ else {
27
+ let stringified;
28
+ try {
29
+ if (typeof e === "object" && e) {
30
+ stringified = JSON.stringify(e);
31
+ }
32
+ else {
33
+ stringified = String(e);
34
+ }
35
+ }
36
+ catch (err) {
37
+ stringified = "[unable to stringify input]";
38
+ }
39
+ return `Unknown error ${stringified}`;
40
+ }
41
+ }
42
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/error.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAC,CAAU;IAChC,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE;QACf,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC;QAC3C,MAAM,UAAU,GAAG,OAAO,CAAC,CAAC,OAAO,KAAK,QAAQ,CAAC;QACjD,OAAO,OAAO,IAAI,UAAU,CAAC;KAC9B;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,CAAU;IACxC,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE;QACd,OAAO,CAAC,CAAC,OAAO,CAAC;KAClB;SAAM;QACL,IAAI,WAAmB,CAAC;QACxB,IAAI;YACF,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,EAAE;gBAC9B,WAAW,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;aACjC;iBAAM;gBACL,WAAW,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;aACzB;SACF;QAAC,OAAO,GAAQ,EAAE;YACjB,WAAW,GAAG,6BAA6B,CAAC;SAC7C;QACD,OAAO,iBAAiB,WAAW,EAAE,CAAC;KACvC;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { isObject } from \"./object\";\n\n/**\n * Typeguard for an error object shape (has name and message)\n * @param e - Something caught by a catch clause.\n */\nexport function isError(e: unknown): e is Error {\n if (isObject(e)) {\n const hasName = typeof e.name === \"string\";\n const hasMessage = typeof e.message === \"string\";\n return hasName && hasMessage;\n }\n return false;\n}\n\n/**\n * Given what is thought to be an error object, return the message if possible.\n * If the message is missing, returns a stringified version of the input.\n * @param e - Something thrown from a try block\n * @returns The error message or a string of the input\n */\nexport function getErrorMessage(e: unknown): string {\n if (isError(e)) {\n return e.message;\n } else {\n let stringified: string;\n try {\n if (typeof e === \"object\" && e) {\n stringified = JSON.stringify(e);\n } else {\n stringified = String(e);\n }\n } catch (err: any) {\n stringified = \"[unable to stringify input]\";\n }\n return `Unknown error ${stringified}`;\n }\n}\n"]}
@@ -0,0 +1,21 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ /**
4
+ * Converts an ArrayBuffer to a hexadecimal string.
5
+ * @param buffer - Raw binary data.
6
+ * @internal
7
+ */
8
+ export function bufferToHex(buffer) {
9
+ const bytes = new Uint8Array(buffer);
10
+ return Array.prototype.map.call(bytes, byteToHex).join("");
11
+ }
12
+ /**
13
+ * Converts a byte to a hexadecimal string.
14
+ * @param byte - An integer representation of a byte.
15
+ * @internal
16
+ */
17
+ function byteToHex(byte) {
18
+ const hex = byte.toString(16);
19
+ return hex.length === 2 ? hex : `0${hex}`;
20
+ }
21
+ //# sourceMappingURL=hex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hex.js","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,MAAmB;IAC7C,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,OAAO,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC7D,CAAC;AAED;;;;GAIG;AACH,SAAS,SAAS,CAAC,IAAY;IAC7B,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC9B,OAAO,GAAG,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,CAAC;AAC5C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Converts an ArrayBuffer to a hexadecimal string.\n * @param buffer - Raw binary data.\n * @internal\n */\nexport function bufferToHex(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n return Array.prototype.map.call(bytes, byteToHex).join(\"\");\n}\n\n/**\n * Converts a byte to a hexadecimal string.\n * @param byte - An integer representation of a byte.\n * @internal\n */\nfunction byteToHex(byte: number): string {\n const hex = byte.toString(16);\n return hex.length === 2 ? hex : `0${hex}`;\n}\n"]}
@@ -2,4 +2,8 @@
2
2
  // Licensed under the MIT license.
3
3
  export { isNode } from "./isNode";
4
4
  export { delay } from "./delay";
5
+ export { getRandomIntegerInclusive } from "./random";
6
+ export { isObject } from "./object";
7
+ export { isError, getErrorMessage } from "./error";
8
+ export { computeSha256Hash, computeSha256Hmac } from "./sha256";
5
9
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nexport { isNode } from \"./isNode\";\nexport { delay } from \"./delay\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAiB,MAAM,UAAU,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nexport { isNode } from \"./isNode\";\nexport { delay } from \"./delay\";\nexport { getRandomIntegerInclusive } from \"./random\";\nexport { isObject, UnknownObject } from \"./object\";\nexport { isError, getErrorMessage } from \"./error\";\nexport { computeSha256Hash, computeSha256Hmac } from \"./sha256\";\n"]}
@@ -0,0 +1,14 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ /**
4
+ * Helper to determine when an input is a generic JS object.
5
+ * @returns true when input is an object type that is not null, Array, RegExp, or Date.
6
+ */
7
+ export function isObject(input) {
8
+ return (typeof input === "object" &&
9
+ input !== null &&
10
+ !Array.isArray(input) &&
11
+ !(input instanceof RegExp) &&
12
+ !(input instanceof Date));
13
+ }
14
+ //# sourceMappingURL=object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object.js","sourceRoot":"","sources":["../../src/object.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAOlC;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACrB,CAAC,CAAC,KAAK,YAAY,MAAM,CAAC;QAC1B,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,CACzB,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * A generic shape for a plain JS object.\n */\nexport type UnknownObject = { [s: string]: unknown };\n\n/**\n * Helper to determine when an input is a generic JS object.\n * @returns true when input is an object type that is not null, Array, RegExp, or Date.\n */\nexport function isObject(input: unknown): input is UnknownObject {\n return (\n typeof input === \"object\" &&\n input !== null &&\n !Array.isArray(input) &&\n !(input instanceof RegExp) &&\n !(input instanceof Date)\n );\n}\n"]}
@@ -0,0 +1,21 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ /**
4
+ * Returns a random integer value between a lower and upper bound,
5
+ * inclusive of both bounds.
6
+ * Note that this uses Math.random and isn't secure. If you need to use
7
+ * this for any kind of security purpose, find a better source of random.
8
+ * @param min - The smallest integer value allowed.
9
+ * @param max - The largest integer value allowed.
10
+ */
11
+ export function getRandomIntegerInclusive(min, max) {
12
+ // Make sure inputs are integers.
13
+ min = Math.ceil(min);
14
+ max = Math.floor(max);
15
+ // Pick a random offset from zero to the size of the range.
16
+ // Since Math.random() can never return 1, we have to make the range one larger
17
+ // in order to be inclusive of the maximum value after we take the floor.
18
+ const offset = Math.floor(Math.random() * (max - min + 1));
19
+ return offset + min;
20
+ }
21
+ //# sourceMappingURL=random.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random.js","sourceRoot":"","sources":["../../src/random.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;;;;;;GAOG;AACH,MAAM,UAAU,yBAAyB,CAAC,GAAW,EAAE,GAAW;IAChE,iCAAiC;IACjC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACrB,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACtB,2DAA2D;IAC3D,+EAA+E;IAC/E,yEAAyE;IACzE,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IAC3D,OAAO,MAAM,GAAG,GAAG,CAAC;AACtB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Returns a random integer value between a lower and upper bound,\n * inclusive of both bounds.\n * Note that this uses Math.random and isn't secure. If you need to use\n * this for any kind of security purpose, find a better source of random.\n * @param min - The smallest integer value allowed.\n * @param max - The largest integer value allowed.\n */\nexport function getRandomIntegerInclusive(min: number, max: number): number {\n // Make sure inputs are integers.\n min = Math.ceil(min);\n max = Math.floor(max);\n // Pick a random offset from zero to the size of the range.\n // Since Math.random() can never return 1, we have to make the range one larger\n // in order to be inclusive of the maximum value after we take the floor.\n const offset = Math.floor(Math.random() * (max - min + 1));\n return offset + min;\n}\n"]}
@@ -0,0 +1,61 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ import { base64ToBytes, bufferToBase64 } from "./base64.browser";
4
+ import { bufferToHex } from "./hex";
5
+ import { utf8ToBytes } from "./utf8.browser";
6
+ let subtleCrypto;
7
+ /**
8
+ * Returns a cached reference to the Web API crypto.subtle object.
9
+ * @internal
10
+ */
11
+ function getCrypto() {
12
+ if (subtleCrypto) {
13
+ return subtleCrypto;
14
+ }
15
+ if (!self.crypto || !self.crypto.subtle) {
16
+ throw new Error("Your browser environment does not support cryptography functions.");
17
+ }
18
+ subtleCrypto = self.crypto.subtle;
19
+ return subtleCrypto;
20
+ }
21
+ /**
22
+ * Generates a SHA-256 HMAC signature.
23
+ * @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
24
+ * @param stringToSign - The data to be signed.
25
+ * @param encoding - The textual encoding to use for the returned HMAC digest.
26
+ */
27
+ export async function computeSha256Hmac(key, stringToSign, encoding) {
28
+ const crypto = getCrypto();
29
+ const keyBytes = base64ToBytes(key);
30
+ const stringToSignBytes = utf8ToBytes(stringToSign);
31
+ const cryptoKey = await crypto.importKey("raw", keyBytes, {
32
+ name: "HMAC",
33
+ hash: { name: "SHA-256" },
34
+ }, false, ["sign"]);
35
+ const signature = await crypto.sign({
36
+ name: "HMAC",
37
+ hash: { name: "SHA-256" },
38
+ }, cryptoKey, stringToSignBytes);
39
+ switch (encoding) {
40
+ case "base64":
41
+ return bufferToBase64(signature);
42
+ case "hex":
43
+ return bufferToHex(signature);
44
+ }
45
+ }
46
+ /**
47
+ * Generates a SHA-256 hash.
48
+ * @param content - The data to be included in the hash.
49
+ * @param encoding - The textual encoding to use for the returned hash.
50
+ */
51
+ export async function computeSha256Hash(content, encoding) {
52
+ const contentBytes = utf8ToBytes(content);
53
+ const digest = await getCrypto().digest({ name: "SHA-256" }, contentBytes);
54
+ switch (encoding) {
55
+ case "base64":
56
+ return bufferToBase64(digest);
57
+ case "hex":
58
+ return bufferToHex(digest);
59
+ }
60
+ }
61
+ //# sourceMappingURL=sha256.browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sha256.browser.js","sourceRoot":"","sources":["../../src/sha256.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AA6C7C,IAAI,YAAsC,CAAC;AAE3C;;;GAGG;AACH,SAAS,SAAS;IAChB,IAAI,YAAY,EAAE;QAChB,OAAO,YAAY,CAAC;KACrB;IAED,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;QACvC,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;KACtF;IAED,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAClC,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,GAAW,EACX,YAAoB,EACpB,QAA0B;IAE1B,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,MAAM,QAAQ,GAAG,aAAa,CAAC,GAAG,CAAC,CAAC;IACpC,MAAM,iBAAiB,GAAG,WAAW,CAAC,YAAY,CAAC,CAAC;IAEpD,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,SAAS,CACtC,KAAK,EACL,QAAQ,EACR;QACE,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;KAC1B,EACD,KAAK,EACL,CAAC,MAAM,CAAC,CACT,CAAC;IACF,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,IAAI,CACjC;QACE,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;KAC1B,EACD,SAAS,EACT,iBAAiB,CAClB,CAAC;IAEF,QAAQ,QAAQ,EAAE;QAChB,KAAK,QAAQ;YACX,OAAO,cAAc,CAAC,SAAS,CAAC,CAAC;QACnC,KAAK,KAAK;YACR,OAAO,WAAW,CAAC,SAAS,CAAC,CAAC;KACjC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAAe,EACf,QAA0B;IAE1B,MAAM,YAAY,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,YAAY,CAAC,CAAC;IAE3E,QAAQ,QAAQ,EAAE;QAChB,KAAK,QAAQ;YACX,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;QAChC,KAAK,KAAK;YACR,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC;KAC9B;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { base64ToBytes, bufferToBase64 } from \"./base64.browser\";\nimport { bufferToHex } from \"./hex\";\nimport { utf8ToBytes } from \"./utf8.browser\";\n\n// stubs for browser self.crypto\ninterface JsonWebKey {}\ninterface CryptoKey {}\ntype KeyUsage =\n | \"decrypt\"\n | \"deriveBits\"\n | \"deriveKey\"\n | \"encrypt\"\n | \"sign\"\n | \"unwrapKey\"\n | \"verify\"\n | \"wrapKey\";\ninterface Algorithm {\n name: string;\n}\ninterface SubtleCrypto {\n importKey(\n format: string,\n keyData: JsonWebKey,\n algorithm: HmacImportParams,\n extractable: boolean,\n usage: KeyUsage[]\n ): Promise<CryptoKey>;\n sign(\n algorithm: HmacImportParams,\n key: CryptoKey,\n data: ArrayBufferView | ArrayBuffer\n ): Promise<ArrayBuffer>;\n digest(algorithm: Algorithm, data: ArrayBufferView | ArrayBuffer): Promise<ArrayBuffer>;\n}\ninterface Crypto {\n readonly subtle: SubtleCrypto;\n getRandomValues<T extends ArrayBufferView | null>(array: T): T;\n}\ndeclare const self: {\n crypto: Crypto;\n};\ninterface HmacImportParams {\n name: string;\n hash: Algorithm;\n length?: number;\n}\n\nlet subtleCrypto: SubtleCrypto | undefined;\n\n/**\n * Returns a cached reference to the Web API crypto.subtle object.\n * @internal\n */\nfunction getCrypto(): SubtleCrypto {\n if (subtleCrypto) {\n return subtleCrypto;\n }\n\n if (!self.crypto || !self.crypto.subtle) {\n throw new Error(\"Your browser environment does not support cryptography functions.\");\n }\n\n subtleCrypto = self.crypto.subtle;\n return subtleCrypto;\n}\n\n/**\n * Generates a SHA-256 HMAC signature.\n * @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.\n * @param stringToSign - The data to be signed.\n * @param encoding - The textual encoding to use for the returned HMAC digest.\n */\nexport async function computeSha256Hmac(\n key: string,\n stringToSign: string,\n encoding: \"base64\" | \"hex\"\n): Promise<string> {\n const crypto = getCrypto();\n const keyBytes = base64ToBytes(key);\n const stringToSignBytes = utf8ToBytes(stringToSign);\n\n const cryptoKey = await crypto.importKey(\n \"raw\",\n keyBytes,\n {\n name: \"HMAC\",\n hash: { name: \"SHA-256\" },\n },\n false,\n [\"sign\"]\n );\n const signature = await crypto.sign(\n {\n name: \"HMAC\",\n hash: { name: \"SHA-256\" },\n },\n cryptoKey,\n stringToSignBytes\n );\n\n switch (encoding) {\n case \"base64\":\n return bufferToBase64(signature);\n case \"hex\":\n return bufferToHex(signature);\n }\n}\n\n/**\n * Generates a SHA-256 hash.\n * @param content - The data to be included in the hash.\n * @param encoding - The textual encoding to use for the returned hash.\n */\nexport async function computeSha256Hash(\n content: string,\n encoding: \"base64\" | \"hex\"\n): Promise<string> {\n const contentBytes = utf8ToBytes(content);\n const digest = await getCrypto().digest({ name: \"SHA-256\" }, contentBytes);\n\n switch (encoding) {\n case \"base64\":\n return bufferToBase64(digest);\n case \"hex\":\n return bufferToHex(digest);\n }\n}\n"]}
@@ -0,0 +1,22 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ import { createHash, createHmac } from "crypto";
4
+ /**
5
+ * Generates a SHA-256 HMAC signature.
6
+ * @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
7
+ * @param stringToSign - The data to be signed.
8
+ * @param encoding - The textual encoding to use for the returned HMAC digest.
9
+ */
10
+ export async function computeSha256Hmac(key, stringToSign, encoding) {
11
+ const decodedKey = Buffer.from(key, "base64");
12
+ return createHmac("sha256", decodedKey).update(stringToSign).digest(encoding);
13
+ }
14
+ /**
15
+ * Generates a SHA-256 hash.
16
+ * @param content - The data to be included in the hash.
17
+ * @param encoding - The textual encoding to use for the returned hash.
18
+ */
19
+ export async function computeSha256Hash(content, encoding) {
20
+ return createHash("sha256").update(content).digest(encoding);
21
+ }
22
+ //# sourceMappingURL=sha256.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sha256.js","sourceRoot":"","sources":["../../src/sha256.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,GAAW,EACX,YAAoB,EACpB,QAA0B;IAE1B,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IAE9C,OAAO,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAChF,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAAe,EACf,QAA0B;IAE1B,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC/D,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { createHash, createHmac } from \"crypto\";\n\n/**\n * Generates a SHA-256 HMAC signature.\n * @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.\n * @param stringToSign - The data to be signed.\n * @param encoding - The textual encoding to use for the returned HMAC digest.\n */\nexport async function computeSha256Hmac(\n key: string,\n stringToSign: string,\n encoding: \"base64\" | \"hex\"\n): Promise<string> {\n const decodedKey = Buffer.from(key, \"base64\");\n\n return createHmac(\"sha256\", decodedKey).update(stringToSign).digest(encoding);\n}\n\n/**\n * Generates a SHA-256 hash.\n * @param content - The data to be included in the hash.\n * @param encoding - The textual encoding to use for the returned hash.\n */\nexport async function computeSha256Hash(\n content: string,\n encoding: \"base64\" | \"hex\"\n): Promise<string> {\n return createHash(\"sha256\").update(content).digest(encoding);\n}\n"]}
@@ -0,0 +1,26 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT license.
3
+ let encoder;
4
+ /**
5
+ * Returns a cached TextEncoder.
6
+ * @internal
7
+ */
8
+ function getTextEncoder() {
9
+ if (encoder) {
10
+ return encoder;
11
+ }
12
+ if (typeof TextEncoder === "undefined") {
13
+ throw new Error(`Your browser environment is missing "TextEncoder".`);
14
+ }
15
+ encoder = new TextEncoder();
16
+ return encoder;
17
+ }
18
+ /**
19
+ * Converts a utf8 string into a byte array.
20
+ * @param content - The utf8 string to convert.
21
+ * @internal
22
+ */
23
+ export function utf8ToBytes(content) {
24
+ return getTextEncoder().encode(content);
25
+ }
26
+ //# sourceMappingURL=utf8.browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8.browser.js","sourceRoot":"","sources":["../../src/utf8.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAalC,IAAI,OAAgC,CAAC;AAErC;;;GAGG;AACH,SAAS,cAAc;IACrB,IAAI,OAAO,EAAE;QACX,OAAO,OAAO,CAAC;KAChB;IAED,IAAI,OAAO,WAAW,KAAK,WAAW,EAAE;QACtC,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;KACvE;IAED,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IAC5B,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,OAAe;IACzC,OAAO,cAAc,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n// stubs for browser TextEncoder\ninterface TextEncoder {\n encode(input?: string): Uint8Array;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-redeclare\ndeclare const TextEncoder: {\n prototype: TextEncoder;\n new (): TextEncoder;\n};\n\nlet encoder: TextEncoder | undefined;\n\n/**\n * Returns a cached TextEncoder.\n * @internal\n */\nfunction getTextEncoder(): TextEncoder {\n if (encoder) {\n return encoder;\n }\n\n if (typeof TextEncoder === \"undefined\") {\n throw new Error(`Your browser environment is missing \"TextEncoder\".`);\n }\n\n encoder = new TextEncoder();\n return encoder;\n}\n\n/**\n * Converts a utf8 string into a byte array.\n * @param content - The utf8 string to convert.\n * @internal\n */\nexport function utf8ToBytes(content: string): Uint8Array {\n return getTextEncoder().encode(content);\n}\n"]}
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@azure/core-util",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0",
4
4
  "description": "Core library for shared utility methods",
5
5
  "sdk-type": "client",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist-esm/src/index.js",
8
8
  "browser": {
9
- "./dist-esm/src/isNode.js": "./dist-esm/src/isNode.browser.js"
9
+ "./dist-esm/src/isNode.js": "./dist-esm/src/isNode.browser.js",
10
+ "./dist-esm/src/sha256.js": "./dist-esm/src/sha256.browser.js"
11
+ },
12
+ "react-native": {
13
+ "./dist/index.js": "./dist-esm/src/index.js"
10
14
  },
11
15
  "types": "types/latest/core-util.d.ts",
12
16
  "typesVersions": {
@@ -18,30 +22,27 @@
18
22
  },
19
23
  "scripts": {
20
24
  "audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
21
- "build:samples": "echo Skipped.",
22
- "build:test": "echo Just call build instead",
23
- "build:ts": "tsc -p .",
25
+ "build:samples": "echo Obsolete",
26
+ "build:test": "tsc -p . && dev-tool run bundle",
24
27
  "build:types": "downlevel-dts types/latest/ types/3.1/",
25
- "build": "npm run build:ts && rollup -c 2>&1 && api-extractor run --local && npm run build:types",
26
- "check-format": "prettier --list-different \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
27
- "clean": "rimraf dist dist-* types *.tgz *.log",
28
+ "build": "npm run clean && tsc -p . && dev-tool run bundle && api-extractor run --local && npm run build:types",
29
+ "check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
30
+ "clean": "rimraf dist dist-* temp types *.tgz *.log",
28
31
  "execute:samples": "echo skipped",
29
- "extract-api": "npm run build:ts && api-extractor run --local",
30
- "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
32
+ "extract-api": "tsc -p . && api-extractor run --local",
33
+ "format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
31
34
  "integration-test:browser": "echo skipped",
32
35
  "integration-test:node": "echo skipped",
33
36
  "integration-test": "npm run integration-test:node && npm run integration-test:browser",
34
37
  "lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]",
35
38
  "lint": "eslint package.json api-extractor.json src test --ext .ts",
36
39
  "pack": "npm pack 2>&1",
37
- "prebuild": "npm run clean",
38
- "test:browser": "npm run build:test:browser && npm run unit-test:browser && npm run integration-test:browser",
39
- "test:node": "npm run build:test:node && npm run unit-test:node && npm run integration-test:node",
40
- "test": "npm run unit-test:node && npm run build && npm run unit-test:browser && npm run integration-test:node && npm run integration-test:browser",
40
+ "test:browser": "npm run clean && npm run build:test && npm run unit-test:browser && npm run integration-test:browser",
41
+ "test:node": "npm run clean && tsc -p . && npm run unit-test:node && npm run integration-test:node",
42
+ "test": "npm run clean && tsc -p . && npm run unit-test:node && dev-tool run bundle && npm run unit-test:browser && npm run integration-test",
41
43
  "unit-test:browser": "karma start --single-run",
42
- "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\"",
43
- "unit-test": "npm run unit-test:node && npm run unit-test:browser",
44
- "docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --stripInternal --mode file --out ./dist/docs ./src"
44
+ "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\"",
45
+ "unit-test": "npm run unit-test:node && npm run unit-test:browser"
45
46
  },
46
47
  "files": [
47
48
  "dist/",
@@ -62,29 +63,24 @@
62
63
  "url": "https://github.com/Azure/azure-sdk-for-js/issues"
63
64
  },
64
65
  "engines": {
65
- "node": ">=8.0.0"
66
+ "node": ">=12.0.0"
66
67
  },
67
- "homepage": "https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/core/core-util/",
68
+ "homepage": "https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-util/",
68
69
  "sideEffects": false,
69
70
  "prettier": "@azure/eslint-plugin-azure-sdk/prettier.json",
70
71
  "dependencies": {
71
- "tslib": "^2.0.0"
72
+ "tslib": "^2.2.0"
72
73
  },
73
74
  "devDependencies": {
74
75
  "@azure/dev-tool": "^1.0.0",
75
- "@microsoft/api-extractor": "7.7.11",
76
- "@rollup/plugin-commonjs": "11.0.2",
77
- "@rollup/plugin-json": "^4.0.0",
78
- "@rollup/plugin-multi-entry": "^3.0.0",
79
- "@rollup/plugin-node-resolve": "^8.0.0",
80
- "@rollup/plugin-replace": "^2.2.0",
76
+ "@microsoft/api-extractor": "7.18.11",
81
77
  "@types/chai": "^4.1.6",
82
78
  "@types/mocha": "^7.0.2",
83
- "@types/node": "^8.0.0",
79
+ "@types/node": "^12.0.0",
84
80
  "@types/sinon": "^9.0.4",
85
81
  "@azure/eslint-plugin-azure-sdk": "^3.0.0",
86
82
  "chai": "^4.2.0",
87
- "downlevel-dts": "~0.4.0",
83
+ "downlevel-dts": "^0.8.0",
88
84
  "cross-env": "^7.0.2",
89
85
  "eslint": "^7.15.0",
90
86
  "inherits": "^2.0.3",
@@ -100,16 +96,11 @@
100
96
  "karma-mocha-reporter": "^2.2.5",
101
97
  "karma-sourcemap-loader": "^0.3.8",
102
98
  "mocha": "^7.1.1",
103
- "mocha-junit-reporter": "^1.18.0",
104
- "prettier": "^1.16.4",
99
+ "mocha-junit-reporter": "^2.0.0",
100
+ "prettier": "^2.5.1",
105
101
  "rimraf": "^3.0.0",
106
- "rollup": "^1.16.3",
107
- "rollup-plugin-sourcemaps": "^0.4.2",
108
- "rollup-plugin-terser": "^5.1.1",
109
- "rollup-plugin-visualizer": "^4.0.4",
110
102
  "sinon": "^9.0.2",
111
- "typescript": "~4.2.0",
112
- "util": "^0.12.1",
113
- "typedoc": "0.15.2"
103
+ "typescript": "~4.6.0",
104
+ "util": "^0.12.1"
114
105
  }
115
106
  }
@@ -1,11 +1,56 @@
1
+ /**
2
+ * Generates a SHA-256 hash.
3
+ * @param content - The data to be included in the hash.
4
+ * @param encoding - The textual encoding to use for the returned hash.
5
+ */
6
+ export declare function computeSha256Hash(content: string, encoding: "base64" | "hex"): Promise<string>;
7
+ /**
8
+ * Generates a SHA-256 HMAC signature.
9
+ * @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
10
+ * @param stringToSign - The data to be signed.
11
+ * @param encoding - The textual encoding to use for the returned HMAC digest.
12
+ */
13
+ export declare function computeSha256Hmac(key: string, stringToSign: string, encoding: "base64" | "hex"): Promise<string>;
1
14
  /**
2
15
  * A wrapper for setTimeout that resolves a promise after timeInMs milliseconds.
3
16
  * @param timeInMs - The number of milliseconds to be delayed.
4
17
  * @returns Promise that is resolved after timeInMs
5
18
  */
6
19
  export declare function delay(timeInMs: number): Promise<void>;
20
+ /**
21
+ * Given what is thought to be an error object, return the message if possible.
22
+ * If the message is missing, returns a stringified version of the input.
23
+ * @param e - Something thrown from a try block
24
+ * @returns The error message or a string of the input
25
+ */
26
+ export declare function getErrorMessage(e: unknown): string;
27
+ /**
28
+ * Returns a random integer value between a lower and upper bound,
29
+ * inclusive of both bounds.
30
+ * Note that this uses Math.random and isn't secure. If you need to use
31
+ * this for any kind of security purpose, find a better source of random.
32
+ * @param min - The smallest integer value allowed.
33
+ * @param max - The largest integer value allowed.
34
+ */
35
+ export declare function getRandomIntegerInclusive(min: number, max: number): number;
36
+ /**
37
+ * Typeguard for an error object shape (has name and message)
38
+ * @param e - Something caught by a catch clause.
39
+ */
40
+ export declare function isError(e: unknown): e is Error;
7
41
  /**
8
42
  * A constant that indicates whether the environment the code is running is Node.JS.
9
43
  */
10
44
  export declare const isNode: boolean;
45
+ /**
46
+ * Helper to determine when an input is a generic JS object.
47
+ * @returns true when input is an object type that is not null, Array, RegExp, or Date.
48
+ */
49
+ export declare function isObject(input: unknown): input is UnknownObject;
50
+ /**
51
+ * A generic shape for a plain JS object.
52
+ */
53
+ export declare type UnknownObject = {
54
+ [s: string]: unknown;
55
+ };
11
56
  export {};
@@ -1,3 +1,17 @@
1
+ /**
2
+ * Generates a SHA-256 hash.
3
+ * @param content - The data to be included in the hash.
4
+ * @param encoding - The textual encoding to use for the returned hash.
5
+ */
6
+ export declare function computeSha256Hash(content: string, encoding: "base64" | "hex"): Promise<string>;
7
+
8
+ /**
9
+ * Generates a SHA-256 HMAC signature.
10
+ * @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
11
+ * @param stringToSign - The data to be signed.
12
+ * @param encoding - The textual encoding to use for the returned HMAC digest.
13
+ */
14
+ export declare function computeSha256Hmac(key: string, stringToSign: string, encoding: "base64" | "hex"): Promise<string>;
1
15
 
2
16
  /**
3
17
  * A wrapper for setTimeout that resolves a promise after timeInMs milliseconds.
@@ -6,9 +20,46 @@
6
20
  */
7
21
  export declare function delay(timeInMs: number): Promise<void>;
8
22
 
23
+ /**
24
+ * Given what is thought to be an error object, return the message if possible.
25
+ * If the message is missing, returns a stringified version of the input.
26
+ * @param e - Something thrown from a try block
27
+ * @returns The error message or a string of the input
28
+ */
29
+ export declare function getErrorMessage(e: unknown): string;
30
+
31
+ /**
32
+ * Returns a random integer value between a lower and upper bound,
33
+ * inclusive of both bounds.
34
+ * Note that this uses Math.random and isn't secure. If you need to use
35
+ * this for any kind of security purpose, find a better source of random.
36
+ * @param min - The smallest integer value allowed.
37
+ * @param max - The largest integer value allowed.
38
+ */
39
+ export declare function getRandomIntegerInclusive(min: number, max: number): number;
40
+
41
+ /**
42
+ * Typeguard for an error object shape (has name and message)
43
+ * @param e - Something caught by a catch clause.
44
+ */
45
+ export declare function isError(e: unknown): e is Error;
46
+
9
47
  /**
10
48
  * A constant that indicates whether the environment the code is running is Node.JS.
11
49
  */
12
50
  export declare const isNode: boolean;
13
51
 
52
+ /**
53
+ * Helper to determine when an input is a generic JS object.
54
+ * @returns true when input is an object type that is not null, Array, RegExp, or Date.
55
+ */
56
+ export declare function isObject(input: unknown): input is UnknownObject;
57
+
58
+ /**
59
+ * A generic shape for a plain JS object.
60
+ */
61
+ export declare type UnknownObject = {
62
+ [s: string]: unknown;
63
+ };
64
+
14
65
  export { }