Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sdk/cosmosdb/cosmos/src/request/defaultAgent-react-native.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import type { Agent } from "node:http";
/**
* @hidden
*/
export let defaultHttpAgent: Agent;
/**
* @hidden
*/
export let defaultHttpsAgent: Agent;
48 changes: 48 additions & 0 deletions sdk/cosmosdb/cosmos/src/utils/atob-react-native.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// Use the same polyfill implementation as browser for React Native
let safeatob: (data: string) => string;

// base64 character set, plus padding character (=)
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// Regular expression to check formal correctness of base64 encoded strings
const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;

if (typeof atob === "undefined") {
// Custom atob implementation for React Native
safeatob = (str: string): string => {
let fixedStr = String(str).replace(/[\t\n\f\r ]+/g, "");
if (!b64re.test(fixedStr)) {
throw new TypeError(
"Failed to execute 'atob': The string to be decoded is not correctly encoded.",
);
}

fixedStr += "==".slice(2 - (fixedStr.length & 3));
let bitmap;
let result = "";
let r1;
let r2;
let i = 0;
for (; i < fixedStr.length;) {
bitmap =
(b64.indexOf(fixedStr.charAt(i++)) << 18) |
(b64.indexOf(fixedStr.charAt(i++)) << 12) |
((r1 = b64.indexOf(fixedStr.charAt(i++))) << 6) |
(r2 = b64.indexOf(fixedStr.charAt(i++)));

result +=
r1 === 64
? String.fromCharCode((bitmap >> 16) & 255)
: r2 === 64
? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
: String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
}
return result;
};
} else {
safeatob = atob;
}

export default safeatob;
4 changes: 4 additions & 0 deletions sdk/cosmosdb/cosmos/src/utils/envUtils-react-native.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

export const diagnosticLevelFromEnv: string | undefined = undefined;
20 changes: 20 additions & 0 deletions sdk/cosmosdb/cosmos/src/utils/hmac-react-native.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { encodeUTF8, encodeBase64 } from "./encode.js";
import atob from "./atob.js";
import { globalCrypto } from "./globalCrypto.js";

export async function hmac(key: string, message: string): Promise<string> {
const importParams: HmacImportParams = { name: "HMAC", hash: { name: "SHA-256" } };
const encodedMessage = new Uint8Array(
[...unescape(encodeURIComponent(message))].map((c) => c.charCodeAt(0)),
);
const encodedKey = encodeUTF8(atob(key));
const cryptoKey = await globalCrypto.subtle.importKey("raw", encodedKey, importParams, false, [
"sign",
]);
const signature = await globalCrypto.subtle.sign(importParams, cryptoKey, encodedMessage);

return encodeBase64(signature);
}
Loading