Skip to content

Commit

Permalink
Address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mknichel committed Jul 8, 2024
1 parent dc41538 commit a7a085f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
21 changes: 9 additions & 12 deletions lib/RawSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class RawSource extends Source {
throw new TypeError("argument 'value' must be either string or Buffer");
}
this._valueIsBuffer = !convertToString && isBuffer;
const internedString =
typeof value === "string" ? internString(value) : undefined;
this._value =
convertToString && isBuffer
? undefined
: typeof value === "string"
? internString(value)
? internedString
: value;
this._valueAsBuffer = isBuffer ? value : undefined;
this._valueAsString = isBuffer ? undefined : internString(value);
this._valueAsString = isBuffer ? undefined : internedString;
}

isBuffer() {
Expand All @@ -36,9 +38,9 @@ class RawSource extends Source {

source() {
if (this._value === undefined) {
const value = internString(this._valueAsBuffer.toString("utf-8"));
const value = this._valueAsBuffer.toString("utf-8");
if (isDualStringBufferCachingEnabled()) {
this._value = value;
this._value = internString(value);
}
return value;
}
Expand Down Expand Up @@ -68,17 +70,12 @@ class RawSource extends Source {
* @returns {void}
*/
streamChunks(options, onChunk, onSource, onName) {
if (this._value === undefined && isDualStringBufferCachingEnabled()) {
this._value = Buffer.from(this._valueAsBuffer, "utf-8");
}
let strValue = this._valueAsString;
if (strValue === undefined) {
strValue =
typeof this._value === "string"
? this._value
: internString(this._value.toString("utf-8"));
const value = this.source();
strValue = typeof value === "string" ? value : value.toString("utf-8");
if (isDualStringBufferCachingEnabled()) {
this._valueAsString = strValue;
this._valueAsString = internString(strValue);
}
}
return streamChunksOfRawSource(
Expand Down
7 changes: 6 additions & 1 deletion lib/helpers/stringBufferUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ const interningStringMap = new Map();
* of the same value if it has already been interned.
*/
function internString(str) {
if (!isStringInterningEnabled() || !str || typeof str !== "string") {
if (
!isStringInterningEnabled() ||
!str ||
str.length < 128 ||
typeof str !== "string"
) {
return str;
}
let internedString = interningStringMap.get(str);
Expand Down

0 comments on commit a7a085f

Please sign in to comment.