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 May 23, 2024
1 parent 3d6ab16 commit dc41538
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 130 deletions.
39 changes: 25 additions & 14 deletions lib/CachedSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const Source = require("./Source");
const streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");
const streamChunksOfRawSource = require("./helpers/streamChunksOfRawSource");
const streamAndGetSourceAndMap = require("./helpers/streamAndGetSourceAndMap");
const {
isDualStringBufferCachingEnabled
} = require("./helpers/stringBufferUtils");

const mapToBufferedMap = map => {
if (typeof map !== "object" || !map) return map;
Expand Down Expand Up @@ -63,15 +66,12 @@ class CachedSource extends Source {
bufferedMap: cacheEntry.bufferedMap
});
}
// We don't want to cache strings
// So if we have a caches sources
// create a buffer from it and only store
// if it was a Buffer or string
if (this._cachedSource) {
this.buffer();
}
return {
buffer: this._cachedBuffer,
// We don't want to cache strings
// So if we have a caches sources
// create a buffer from it and only store
// if it was a Buffer or string
buffer: this._cachedSource ? this.buffer() : this._cachedBuffer,
source:
this._cachedSourceType !== undefined
? this._cachedSourceType
Expand Down Expand Up @@ -112,19 +112,26 @@ class CachedSource extends Source {
_getCachedSource() {
if (this._cachedSource !== undefined) return this._cachedSource;
if (this._cachedBuffer && this._cachedSourceType !== undefined) {
return (this._cachedSource = this._cachedSourceType
const value = this._cachedSourceType
? this._cachedBuffer.toString("utf-8")
: this._cachedBuffer);
: this._cachedBuffer;

Check warning on line 117 in lib/CachedSource.js

View check run for this annotation

Codecov / codecov/patch

lib/CachedSource.js#L117

Added line #L117 was not covered by tests
if (isDualStringBufferCachingEnabled()) {
this._cachedSource = value;
}
return value;
}
}

buffer() {
if (this._cachedBuffer !== undefined) return this._cachedBuffer;
if (this._cachedSource !== undefined) {
if (Buffer.isBuffer(this._cachedSource)) {
return (this._cachedBuffer = this._cachedSource);
const value = Buffer.isBuffer(this._cachedSource)
? this._cachedSource
: Buffer.from(this._cachedSource, "utf-8");
if (isDualStringBufferCachingEnabled()) {
this._cachedBuffer = value;
}
return (this._cachedBuffer = Buffer.from(this._cachedSource, "utf-8"));
return value;
}
if (typeof this.original().buffer === "function") {
return (this._cachedBuffer = this.original().buffer());
Expand All @@ -133,7 +140,11 @@ class CachedSource extends Source {
if (Buffer.isBuffer(bufferOrString)) {
return (this._cachedBuffer = bufferOrString);
}
return (this._cachedBuffer = Buffer.from(bufferOrString, "utf-8"));
const value = Buffer.from(bufferOrString, "utf-8");
if (isDualStringBufferCachingEnabled()) {
this._cachedBuffer = value;
}
return value;
}

size() {
Expand Down
20 changes: 14 additions & 6 deletions lib/OriginalSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const splitIntoLines = require("./helpers/splitIntoLines");
const getGeneratedSourceInfo = require("./helpers/getGeneratedSourceInfo");
const Source = require("./Source");
const splitIntoPotentialTokens = require("./helpers/splitIntoPotentialTokens");
const {
isDualStringBufferCachingEnabled
} = require("./helpers/stringBufferUtils");

class OriginalSource extends Source {
constructor(value, name) {
Expand All @@ -25,14 +28,22 @@ class OriginalSource extends Source {

source() {
if (this._value === undefined) {
this._value = this._valueAsBuffer.toString("utf-8");
const value = this._valueAsBuffer.toString("utf-8");
if (isDualStringBufferCachingEnabled()) {
this._value = value;
}
return value;
}
return this._value;
}

buffer() {
if (this._valueAsBuffer === undefined) {
this._valueAsBuffer = Buffer.from(this._value, "utf-8");
const value = Buffer.from(this._value, "utf-8");
if (isDualStringBufferCachingEnabled()) {
this._valueAsBuffer = value;
}
return value;
}
return this._valueAsBuffer;
}
Expand Down Expand Up @@ -123,11 +134,8 @@ class OriginalSource extends Source {
}

updateHash(hash) {
if (this._valueAsBuffer === undefined) {
this._valueAsBuffer = Buffer.from(this._value, "utf-8");
}
hash.update("OriginalSource");
hash.update(this._valueAsBuffer);
hash.update(this.buffer());
hash.update(this._name || "");
}
}
Expand Down
Loading

0 comments on commit dc41538

Please sign in to comment.