-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix GH #21252 Cherry pick of 925a477 Co-authored-by: Daniel Lehenbauer <[email protected]> Co-authored-by: Jason Hartman <[email protected]>
- Loading branch information
1 parent
b84f9bc
commit d02a5e9
Showing
14 changed files
with
182 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
// This file is a Node.js-specific implementation of the base64 encoding functions. | ||
// Aside from the below import statement, this file should be identical to the | ||
// base64EncodingBrowser.ts. | ||
// | ||
// (See 'Isomorphic Code' section in the package README.md.) | ||
import { IsoBuffer } from "./bufferNode.js"; | ||
|
||
/** | ||
* Converts the provided {@link https://en.wikipedia.org/wiki/Base64 | base64}-encoded string | ||
* to {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}. | ||
* | ||
* @internal | ||
*/ | ||
export const fromBase64ToUtf8 = (input: string): string => | ||
IsoBuffer.from(input, "base64").toString("utf8"); | ||
|
||
/** | ||
* Converts the provided {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}-encoded string | ||
* to {@link https://en.wikipedia.org/wiki/Base64 | base64}. | ||
* | ||
* @internal | ||
*/ | ||
export const fromUtf8ToBase64 = (input: string): string => | ||
IsoBuffer.from(input, "utf8").toString("base64"); | ||
|
||
/** | ||
* Convenience function to convert unknown encoding to utf8 that avoids | ||
* buffer copies/encode ops when no conversion is needed. | ||
* @param input - The source string to convert. | ||
* @param encoding - The source string's encoding. | ||
* | ||
* @internal | ||
*/ | ||
export const toUtf8 = (input: string, encoding: string): string => { | ||
switch (encoding) { | ||
case "utf8": | ||
// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged | ||
case "utf-8": { | ||
return input; | ||
} | ||
default: { | ||
return IsoBuffer.from(input, encoding).toString(); | ||
} | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/common/client-utils/src/test/mocha/base64Encoding.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { strict as assert } from "node:assert"; | ||
|
||
import { fromBase64ToUtf8, fromUtf8ToBase64, toUtf8 } from "../../indexNode.js"; | ||
|
||
describe("base64Encoding", () => { | ||
it("round-trips correctly", async () => { | ||
const original = "hello world"; | ||
const base64 = fromUtf8ToBase64(original); | ||
assert.equal(fromBase64ToUtf8(base64), original); | ||
assert.equal(toUtf8(base64, "base64"), original); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/common/client-utils/src/test/mocha/buffer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { strict as assert } from "node:assert"; | ||
|
||
import { IsoBuffer } from "../../indexNode.js"; | ||
|
||
describe("IsoBuffer", () => { | ||
it("is a Buffer", () => { | ||
// Paranoid test that 'IsoBuffer' can be constructed in both CJS/ESM in Node.js environments. | ||
// Note that no export remapping is involved, given that 'IsoBuffer' is imported from 'indexNode.js'. | ||
// | ||
// More comprehensive testing for 'IsoBuffer' is done in 'jest/buffer.spec.ts', which compares | ||
// Node.js and Broswer implementations for equivalence. | ||
assert( | ||
IsoBuffer.from("", "utf8") instanceof Buffer, | ||
"Expected IsoBuffer.from to return a native Node.js Buffer instance.", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { strict as assert } from "node:assert"; | ||
|
||
import { Trace } from "../../indexNode.js"; | ||
|
||
describe("Trace", () => { | ||
it("measure 2ms timeout", async () => { | ||
const trace = Trace.start(); | ||
|
||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
try { | ||
const event = trace.trace(); | ||
|
||
// While we specify a 2ms timeout, it's possible for performance.now() to measure a | ||
// slightly smaller duration due to timing attack and fingerprinting mitigations. | ||
// (See https://developer.mozilla.org/en-US/docs/Web/API/Performance/now#security_requirements) | ||
// | ||
// Therefore, we conservatively require that only 1ms has elapsed. | ||
assert(event.duration >= 1); | ||
assert(event.totalTimeElapsed >= 1); | ||
|
||
resolve(undefined); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}, 2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters