@typespec/ts-http-runtime 1.0.0-alpha.20231023.3 → 1.0.0-alpha.20231031.3
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/dist/index.js +2 -58
- package/dist/index.js.map +1 -1
- package/dist-esm/src/util/bytesEncoding.browser.js +35 -7
- package/dist-esm/src/util/bytesEncoding.browser.js.map +1 -1
- package/dist-esm/src/util/bytesEncoding.js +2 -58
- package/dist-esm/src/util/bytesEncoding.js.map +1 -1
- package/dist-esm/src/util/sha256.browser.js +6 -18
- package/dist-esm/src/util/sha256.browser.js.map +1 -1
- package/package.json +2 -2
- package/types/ts-http-runtime.d.ts +1 -1
- package/dist-esm/src/util/base64.browser.js +0 -35
- package/dist-esm/src/util/base64.browser.js.map +0 -1
- package/dist-esm/src/util/hex.js +0 -21
- package/dist-esm/src/util/hex.js.map +0 -1
- package/dist-esm/src/util/utf8.browser.js +0 -26
- package/dist-esm/src/util/utf8.browser.js.map +0 -1
|
@@ -14,6 +14,8 @@ export function uint8ArrayToString(bytes, format) {
|
|
|
14
14
|
return uint8ArrayToBase64(bytes);
|
|
15
15
|
case "base64url":
|
|
16
16
|
return uint8ArrayToBase64Url(bytes);
|
|
17
|
+
case "hex":
|
|
18
|
+
return uint8ArrayToHexString(bytes);
|
|
17
19
|
}
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
@@ -30,16 +32,16 @@ export function stringToUint8Array(value, format) {
|
|
|
30
32
|
return base64ToUint8Array(value);
|
|
31
33
|
case "base64url":
|
|
32
34
|
return base64UrlToUint8Array(value);
|
|
35
|
+
case "hex":
|
|
36
|
+
return hexStringToUint8Array(value);
|
|
33
37
|
}
|
|
34
38
|
}
|
|
35
39
|
/**
|
|
36
40
|
* Decodes a Uint8Array into a Base64 string.
|
|
37
41
|
* @internal
|
|
38
42
|
*/
|
|
39
|
-
export function uint8ArrayToBase64(
|
|
40
|
-
|
|
41
|
-
const dataString = decoder.decode(uint8Array);
|
|
42
|
-
return btoa(dataString);
|
|
43
|
+
export function uint8ArrayToBase64(bytes) {
|
|
44
|
+
return btoa([...bytes].map((x) => String.fromCharCode(x)).join(""));
|
|
43
45
|
}
|
|
44
46
|
/**
|
|
45
47
|
* Decodes a Uint8Array into a Base64Url string.
|
|
@@ -52,11 +54,18 @@ export function uint8ArrayToBase64Url(bytes) {
|
|
|
52
54
|
* Decodes a Uint8Array into a javascript string.
|
|
53
55
|
* @internal
|
|
54
56
|
*/
|
|
55
|
-
export function uint8ArrayToUtf8String(
|
|
57
|
+
export function uint8ArrayToUtf8String(bytes) {
|
|
56
58
|
const decoder = new TextDecoder();
|
|
57
|
-
const dataString = decoder.decode(
|
|
59
|
+
const dataString = decoder.decode(bytes);
|
|
58
60
|
return dataString;
|
|
59
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Decodes a Uint8Array into a hex string
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
export function uint8ArrayToHexString(bytes) {
|
|
67
|
+
return [...bytes].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
68
|
+
}
|
|
60
69
|
/**
|
|
61
70
|
* Encodes a JavaScript string into a Uint8Array.
|
|
62
71
|
* @internal
|
|
@@ -69,7 +78,7 @@ export function utf8StringToUint8Array(value) {
|
|
|
69
78
|
* @internal
|
|
70
79
|
*/
|
|
71
80
|
export function base64ToUint8Array(value) {
|
|
72
|
-
return new
|
|
81
|
+
return new Uint8Array([...atob(value)].map((x) => x.charCodeAt(0)));
|
|
73
82
|
}
|
|
74
83
|
/**
|
|
75
84
|
* Encodes a Base64Url string into a Uint8Array.
|
|
@@ -79,4 +88,23 @@ export function base64UrlToUint8Array(value) {
|
|
|
79
88
|
const base64String = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
80
89
|
return base64ToUint8Array(base64String);
|
|
81
90
|
}
|
|
91
|
+
const hexDigits = new Set("0123456789abcdefABCDEF");
|
|
92
|
+
/**
|
|
93
|
+
* Encodes a hex string into a Uint8Array
|
|
94
|
+
* @internal
|
|
95
|
+
*/
|
|
96
|
+
export function hexStringToUint8Array(value) {
|
|
97
|
+
// If value has odd length, the last character will be ignored, consistent with NodeJS Buffer behavior
|
|
98
|
+
const bytes = new Uint8Array(value.length / 2);
|
|
99
|
+
for (let i = 0; i < value.length / 2; ++i) {
|
|
100
|
+
const highNibble = value[2 * i];
|
|
101
|
+
const lowNibble = value[2 * i + 1];
|
|
102
|
+
if (!hexDigits.has(highNibble) || !hexDigits.has(lowNibble)) {
|
|
103
|
+
// Replicate Node Buffer behavior by exiting early when we encounter an invalid byte
|
|
104
|
+
return bytes.slice(0, i);
|
|
105
|
+
}
|
|
106
|
+
bytes[i] = parseInt(`${highNibble}${lowNibble}`, 16);
|
|
107
|
+
}
|
|
108
|
+
return bytes;
|
|
109
|
+
}
|
|
82
110
|
//# sourceMappingURL=bytesEncoding.browser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bytesEncoding.browser.js","sourceRoot":"","sources":["../../../src/util/bytesEncoding.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAWlC;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAiB,EAAE,MAAoB;IACxE,QAAQ,MAAM,EAAE;QACd,KAAK,OAAO;YACV,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAC;QACvC,KAAK,QAAQ;YACX,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAC;QACnC,KAAK,WAAW;YACd,OAAO,qBAAqB,CAAC,KAAK,CAAC,CAAC;KACvC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa,EAAE,MAAoB;IACpE,QAAQ,MAAM,EAAE;QACd,KAAK,OAAO;YACV,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAC;QACvC,KAAK,QAAQ;YACX,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAC;QACnC,KAAK,WAAW;YACd,OAAO,qBAAqB,CAAC,KAAK,CAAC,CAAC;KACvC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,
|
|
1
|
+
{"version":3,"file":"bytesEncoding.browser.js","sourceRoot":"","sources":["../../../src/util/bytesEncoding.browser.ts"],"names":[],"mappings":"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","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/** The supported character encoding type */\nexport type EncodingType = \"utf-8\" | \"base64\" | \"base64url\" | \"hex\";\n\n/**\n * The helper that transforms bytes with specific character encoding into string\n * @param bytes - the uint8array bytes\n * @param format - the format we use to encode the byte\n * @returns a string of the encoded string\n */\nexport function uint8ArrayToString(bytes: Uint8Array, format: EncodingType): string {\n switch (format) {\n case \"utf-8\":\n return uint8ArrayToUtf8String(bytes);\n case \"base64\":\n return uint8ArrayToBase64(bytes);\n case \"base64url\":\n return uint8ArrayToBase64Url(bytes);\n case \"hex\":\n return uint8ArrayToHexString(bytes);\n }\n}\n\n/**\n * The helper that transforms string to specific character encoded bytes array.\n * @param value - the string to be converted\n * @param format - the format we use to decode the value\n * @returns a uint8array\n */\nexport function stringToUint8Array(value: string, format: EncodingType): Uint8Array {\n switch (format) {\n case \"utf-8\":\n return utf8StringToUint8Array(value);\n case \"base64\":\n return base64ToUint8Array(value);\n case \"base64url\":\n return base64UrlToUint8Array(value);\n case \"hex\":\n return hexStringToUint8Array(value);\n }\n}\n\n/**\n * Decodes a Uint8Array into a Base64 string.\n * @internal\n */\nexport function uint8ArrayToBase64(bytes: Uint8Array): string {\n return btoa([...bytes].map((x) => String.fromCharCode(x)).join(\"\"));\n}\n\n/**\n * Decodes a Uint8Array into a Base64Url string.\n * @internal\n */\nexport function uint8ArrayToBase64Url(bytes: Uint8Array): string {\n return uint8ArrayToBase64(bytes).replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=/g, \"\");\n}\n\n/**\n * Decodes a Uint8Array into a javascript string.\n * @internal\n */\nexport function uint8ArrayToUtf8String(bytes: Uint8Array): string {\n const decoder = new TextDecoder();\n const dataString = decoder.decode(bytes);\n return dataString;\n}\n\n/**\n * Decodes a Uint8Array into a hex string\n * @internal\n */\nexport function uint8ArrayToHexString(bytes: Uint8Array): string {\n return [...bytes].map((x) => x.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\n/**\n * Encodes a JavaScript string into a Uint8Array.\n * @internal\n */\nexport function utf8StringToUint8Array(value: string): Uint8Array {\n return new TextEncoder().encode(value);\n}\n\n/**\n * Encodes a Base64 string into a Uint8Array.\n * @internal\n */\nexport function base64ToUint8Array(value: string): Uint8Array {\n return new Uint8Array([...atob(value)].map((x) => x.charCodeAt(0)));\n}\n\n/**\n * Encodes a Base64Url string into a Uint8Array.\n * @internal\n */\nexport function base64UrlToUint8Array(value: string): Uint8Array {\n const base64String = value.replace(/-/g, \"+\").replace(/_/g, \"/\");\n return base64ToUint8Array(base64String);\n}\n\nconst hexDigits = new Set(\"0123456789abcdefABCDEF\");\n\n/**\n * Encodes a hex string into a Uint8Array\n * @internal\n */\nexport function hexStringToUint8Array(value: string): Uint8Array {\n // If value has odd length, the last character will be ignored, consistent with NodeJS Buffer behavior\n const bytes = new Uint8Array(value.length / 2);\n for (let i = 0; i < value.length / 2; ++i) {\n const highNibble = value[2 * i];\n const lowNibble = value[2 * i + 1];\n if (!hexDigits.has(highNibble) || !hexDigits.has(lowNibble)) {\n // Replicate Node Buffer behavior by exiting early when we encounter an invalid byte\n return bytes.slice(0, i);\n }\n\n bytes[i] = parseInt(`${highNibble}${lowNibble}`, 16);\n }\n\n return bytes;\n}\n"]}
|
|
@@ -7,14 +7,7 @@
|
|
|
7
7
|
* @returns a string of the encoded string
|
|
8
8
|
*/
|
|
9
9
|
export function uint8ArrayToString(bytes, format) {
|
|
10
|
-
|
|
11
|
-
case "utf-8":
|
|
12
|
-
return uint8ArrayToUtf8String(bytes);
|
|
13
|
-
case "base64":
|
|
14
|
-
return uint8ArrayToBase64(bytes);
|
|
15
|
-
case "base64url":
|
|
16
|
-
return uint8ArrayToBase64Url(bytes);
|
|
17
|
-
}
|
|
10
|
+
return Buffer.from(bytes).toString(format);
|
|
18
11
|
}
|
|
19
12
|
/**
|
|
20
13
|
* The helper that transforms string to specific character encoded bytes array.
|
|
@@ -23,55 +16,6 @@ export function uint8ArrayToString(bytes, format) {
|
|
|
23
16
|
* @returns a uint8array
|
|
24
17
|
*/
|
|
25
18
|
export function stringToUint8Array(value, format) {
|
|
26
|
-
|
|
27
|
-
case "utf-8":
|
|
28
|
-
return utf8StringToUint8Array(value);
|
|
29
|
-
case "base64":
|
|
30
|
-
return base64ToUint8Array(value);
|
|
31
|
-
case "base64url":
|
|
32
|
-
return base64UrlToUint8Array(value);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Decodes a Uint8Array into a Base64 string.
|
|
37
|
-
* @internal
|
|
38
|
-
*/
|
|
39
|
-
export function uint8ArrayToBase64(bytes) {
|
|
40
|
-
return Buffer.from(bytes).toString("base64");
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Decodes a Uint8Array into a Base64Url string.
|
|
44
|
-
* @internal
|
|
45
|
-
*/
|
|
46
|
-
export function uint8ArrayToBase64Url(bytes) {
|
|
47
|
-
return Buffer.from(bytes).toString("base64url");
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Decodes a Uint8Array into a javascript string.
|
|
51
|
-
* @internal
|
|
52
|
-
*/
|
|
53
|
-
export function uint8ArrayToUtf8String(bytes) {
|
|
54
|
-
return Buffer.from(bytes).toString("utf-8");
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Encodes a JavaScript string into a Uint8Array.
|
|
58
|
-
* @internal
|
|
59
|
-
*/
|
|
60
|
-
export function utf8StringToUint8Array(value) {
|
|
61
|
-
return Buffer.from(value);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Encodes a Base64 string into a Uint8Array.
|
|
65
|
-
* @internal
|
|
66
|
-
*/
|
|
67
|
-
export function base64ToUint8Array(value) {
|
|
68
|
-
return Buffer.from(value, "base64");
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Encodes a Base64Url string into a Uint8Array.
|
|
72
|
-
* @internal
|
|
73
|
-
*/
|
|
74
|
-
export function base64UrlToUint8Array(value) {
|
|
75
|
-
return Buffer.from(value, "base64url");
|
|
19
|
+
return Buffer.from(value, format);
|
|
76
20
|
}
|
|
77
21
|
//# sourceMappingURL=bytesEncoding.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bytesEncoding.js","sourceRoot":"","sources":["../../../src/util/bytesEncoding.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAKlC;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAiB,EAAE,MAAoB;IACxE,
|
|
1
|
+
{"version":3,"file":"bytesEncoding.js","sourceRoot":"","sources":["../../../src/util/bytesEncoding.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAKlC;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAiB,EAAE,MAAoB;IACxE,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa,EAAE,MAAoB;IACpE,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACpC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/** The supported character encoding type */\nexport type EncodingType = \"utf-8\" | \"base64\" | \"base64url\" | \"hex\";\n\n/**\n * The helper that transforms bytes with specific character encoding into string\n * @param bytes - the uint8array bytes\n * @param format - the format we use to encode the byte\n * @returns a string of the encoded string\n */\nexport function uint8ArrayToString(bytes: Uint8Array, format: EncodingType): string {\n return Buffer.from(bytes).toString(format);\n}\n\n/**\n * The helper that transforms string to specific character encoded bytes array.\n * @param value - the string to be converted\n * @param format - the format we use to decode the value\n * @returns a uint8array\n */\nexport function stringToUint8Array(value: string, format: EncodingType): Uint8Array {\n return Buffer.from(value, format);\n}\n"]}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
// Copyright (c) Microsoft Corporation.
|
|
2
2
|
// Licensed under the MIT license.
|
|
3
|
-
import {
|
|
4
|
-
import { bufferToHex } from "./hex";
|
|
5
|
-
import { utf8ToBytes } from "./utf8.browser";
|
|
3
|
+
import { stringToUint8Array, uint8ArrayToString } from "./bytesEncoding.browser";
|
|
6
4
|
let subtleCrypto;
|
|
7
5
|
/**
|
|
8
6
|
* Returns a cached reference to the Web API crypto.subtle object.
|
|
@@ -26,8 +24,8 @@ function getCrypto() {
|
|
|
26
24
|
*/
|
|
27
25
|
export async function computeSha256Hmac(key, stringToSign, encoding) {
|
|
28
26
|
const crypto = getCrypto();
|
|
29
|
-
const keyBytes =
|
|
30
|
-
const stringToSignBytes =
|
|
27
|
+
const keyBytes = stringToUint8Array(key, "base64");
|
|
28
|
+
const stringToSignBytes = stringToUint8Array(stringToSign, "utf-8");
|
|
31
29
|
const cryptoKey = await crypto.importKey("raw", keyBytes, {
|
|
32
30
|
name: "HMAC",
|
|
33
31
|
hash: { name: "SHA-256" },
|
|
@@ -36,12 +34,7 @@ export async function computeSha256Hmac(key, stringToSign, encoding) {
|
|
|
36
34
|
name: "HMAC",
|
|
37
35
|
hash: { name: "SHA-256" },
|
|
38
36
|
}, cryptoKey, stringToSignBytes);
|
|
39
|
-
|
|
40
|
-
case "base64":
|
|
41
|
-
return bufferToBase64(signature);
|
|
42
|
-
case "hex":
|
|
43
|
-
return bufferToHex(signature);
|
|
44
|
-
}
|
|
37
|
+
return uint8ArrayToString(new Uint8Array(signature), encoding);
|
|
45
38
|
}
|
|
46
39
|
/**
|
|
47
40
|
* Generates a SHA-256 hash.
|
|
@@ -49,13 +42,8 @@ export async function computeSha256Hmac(key, stringToSign, encoding) {
|
|
|
49
42
|
* @param encoding - The textual encoding to use for the returned hash.
|
|
50
43
|
*/
|
|
51
44
|
export async function computeSha256Hash(content, encoding) {
|
|
52
|
-
const contentBytes =
|
|
45
|
+
const contentBytes = stringToUint8Array(content, "utf-8");
|
|
53
46
|
const digest = await getCrypto().digest({ name: "SHA-256" }, contentBytes);
|
|
54
|
-
|
|
55
|
-
case "base64":
|
|
56
|
-
return bufferToBase64(digest);
|
|
57
|
-
case "hex":
|
|
58
|
-
return bufferToHex(digest);
|
|
59
|
-
}
|
|
47
|
+
return uint8ArrayToString(new Uint8Array(digest), encoding);
|
|
60
48
|
}
|
|
61
49
|
//# sourceMappingURL=sha256.browser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sha256.browser.js","sourceRoot":"","sources":["../../../src/util/sha256.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"sha256.browser.js","sourceRoot":"","sources":["../../../src/util/sha256.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AA6CjF,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,kBAAkB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IACnD,MAAM,iBAAiB,GAAG,kBAAkB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IAEpE,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,OAAO,kBAAkB,CAAC,IAAI,UAAU,CAAC,SAAS,CAAC,EAAE,QAAQ,CAAC,CAAC;AACjE,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAAe,EACf,QAA0B;IAE1B,MAAM,YAAY,GAAG,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC1D,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,YAAY,CAAC,CAAC;IAE3E,OAAO,kBAAkB,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { stringToUint8Array, uint8ArrayToString } from \"./bytesEncoding.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 = stringToUint8Array(key, \"base64\");\n const stringToSignBytes = stringToUint8Array(stringToSign, \"utf-8\");\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 return uint8ArrayToString(new Uint8Array(signature), 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 const contentBytes = stringToUint8Array(content, \"utf-8\");\n const digest = await getCrypto().digest({ name: \"SHA-256\" }, contentBytes);\n\n return uint8ArrayToString(new Uint8Array(digest), encoding);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/ts-http-runtime",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.20231031.3",
|
|
4
4
|
"description": "Isomorphic client library for making HTTP requests in node.js and browser.",
|
|
5
5
|
"sdk-type": "client",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"clean": "rimraf dist dist-* temp types *.tgz *.log",
|
|
36
36
|
"execute:samples": "echo skipped",
|
|
37
37
|
"extract-api": "tsc -p . && api-extractor run --local",
|
|
38
|
-
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\"
|
|
38
|
+
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
|
|
39
39
|
"integration-test:browser": "echo skipped",
|
|
40
40
|
"integration-test:node": "echo skipped",
|
|
41
41
|
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
|
|
@@ -484,7 +484,7 @@ declare interface DelayOptions_2 extends AbortOptions {
|
|
|
484
484
|
export { DelayOptions_2 as DelayOptions }
|
|
485
485
|
|
|
486
486
|
/** The supported character encoding type */
|
|
487
|
-
export declare type EncodingType = "utf-8" | "base64" | "base64url";
|
|
487
|
+
export declare type EncodingType = "utf-8" | "base64" | "base64url" | "hex";
|
|
488
488
|
|
|
489
489
|
/** The error object. */
|
|
490
490
|
export declare interface ErrorModel {
|
|
@@ -1,35 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base64.browser.js","sourceRoot":"","sources":["../../../src/util/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"]}
|
package/dist-esm/src/util/hex.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hex.js","sourceRoot":"","sources":["../../../src/util/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"]}
|
|
@@ -1,26 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utf8.browser.js","sourceRoot":"","sources":["../../../src/util/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"]}
|