Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): use primordials in ext/node/polyfills/string_decoder.ts #24296

Merged
Merged
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
25 changes: 17 additions & 8 deletions ext/node/polyfills/string_decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials

// Logic and comments translated pretty much one-to-one from node's impl
// (https://github.com/nodejs/node/blob/ba06c5c509956dc413f91b755c1c93798bb700d4/src/string_decoder.cc)

Expand All @@ -35,11 +32,19 @@ import {
NodeError,
} from "ext:deno_node/internal/errors.ts";

import { primordials } from "ext:core/mod.js";
import { core, primordials } from "ext:core/mod.js";
const {
ArrayBufferIsView,
ObjectDefineProperties,
Symbol,
MathMin,
DataViewPrototypeGetBuffer,
ObjectPrototypeIsPrototypeOf,
String,
TypedArrayPrototypeGetBuffer,
StringPrototypeToLowerCase,
} = primordials;
const { isTypedArray } = core;

const { MAX_STRING_LENGTH } = constants;

Expand All @@ -50,7 +55,7 @@ type Any = any;
function normalizeEncoding(enc?: string): string {
const encoding = castEncoding(enc ?? null);
if (!encoding) {
if (typeof enc !== "string" || enc.toLowerCase() !== "raw") {
if (typeof enc !== "string" || StringPrototypeToLowerCase(enc) !== "raw") {
throw new ERR_UNKNOWN_ENCODING(
enc as Any,
);
Expand All @@ -64,7 +69,8 @@ function normalizeEncoding(enc?: string): string {
*/

function isBufferType(buf: Buffer) {
return buf instanceof Buffer && buf.BYTES_PER_ELEMENT;
return ObjectPrototypeIsPrototypeOf(Buffer.prototype, buf) &&
buf.BYTES_PER_ELEMENT;
}

function normalizeBuffer(buf: Buffer) {
Expand All @@ -79,7 +85,9 @@ function normalizeBuffer(buf: Buffer) {
return buf;
} else {
return Buffer.from(
buf.buffer,
isTypedArray(buf)
? TypedArrayPrototypeGetBuffer(buf)
: DataViewPrototypeGetBuffer(buf),
);
}
}
Expand All @@ -94,6 +102,7 @@ function bufferToString(
if (len > MAX_STRING_LENGTH) {
throw new NodeError("ERR_STRING_TOO_LONG", "string exceeds maximum length");
}
// deno-lint-ignore prefer-primordials
return buf.toString(encoding as Any, start, end);
}

Expand Down Expand Up @@ -136,7 +145,7 @@ function decode(this: StringDecoder, buf: Buffer) {
}
}

const bytesToCopy = Math.min(buf.length - bufIdx, this[kMissingBytes]);
const bytesToCopy = MathMin(buf.length - bufIdx, this[kMissingBytes]);
buf.copy(
this.lastChar,
this[kBufferedBytes],
Expand Down