Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webpack-sources

Package Overview
Dependencies
Maintainers
5
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webpack-sources - npm Package Compare versions

Comparing version 2.0.0-beta.5 to 2.0.0-beta.6

82

lib/CachedSource.js

@@ -10,14 +10,50 @@ /*

class CachedSource extends Source {
constructor(source) {
constructor(source, cachedData) {
super();
this._source = source;
this._cachedSourceType = cachedData ? cachedData.source : undefined;
this._cachedSource = undefined;
this._cachedBuffer = undefined;
this._cachedSize = undefined;
this._cachedMaps = {};
this._cachedBuffer = cachedData ? cachedData.buffer : undefined;
this._cachedSize = cachedData ? cachedData.size : undefined;
this._cachedMaps = cachedData ? cachedData.maps : new Map();
}
getCachedData() {
// 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,
source:
this._cachedSourceType !== undefined
? this._cachedSourceType
: typeof this._cachedSource === "string"
? true
: Buffer.isBuffer(this._cachedSource)
? false
: undefined,
size: this._cachedSize,
maps: this._cachedMaps
};
}
original() {
return this._source;
}
_ensureSource() {}
source() {
if (typeof this._cachedSource !== "undefined") return this._cachedSource;
return (this._cachedSource = this._source.source());
if (this._cachedSource !== undefined) return this._cachedSource;
if (this._cachedBuffer && this._cachedSourceType !== undefined) {
return (this._cachedSource = this._cachedSourceType
? this._cachedBuffer.toString("utf-8")
: this._cachedBuffer);
} else {
return (this._cachedSource = this._source.source());
}
}

@@ -48,2 +84,5 @@

}
if (typeof this._cachedBuffer !== "undefined") {
return (this._cachedSize = this._cachedBuffer.length);
}
return (this._cachedSize = this._source.size());

@@ -54,14 +93,14 @@ }

const key = options ? JSON.stringify(options) : "{}";
if (typeof this._cachedSource !== "undefined" && key in this._cachedMaps)
let map = this._cachedMaps.get(key);
if (typeof this._cachedSource !== "undefined") {
if (map === undefined) {
map = this._source.map(options);
this._cachedMaps.set(key, map);
}
return {
source: this._cachedSource,
map: this._cachedMaps[key]
map
};
else if (typeof this._cachedSource !== "undefined") {
} else if (map !== undefined) {
return {
source: this._cachedSource,
map: (this._cachedMaps[key] = this._source.map(options))
};
} else if (key in this._cachedMaps) {
return {
source: (this._cachedSource = this._source.source()),

@@ -73,7 +112,4 @@ map: this._cachedMaps[key]

this._cachedSource = result.source;
this._cachedMaps[key] = result.map;
return {
source: this._cachedSource,
map: this._cachedMaps[key]
};
this._cachedMaps.set(key, result.map);
return result;
}

@@ -83,4 +119,8 @@

const key = options ? JSON.stringify(options) : "{}";
if (key in this._cachedMaps) return this._cachedMaps[key];
return (this._cachedMaps[key] = this._source.map());
let map = this._cachedMaps.get(key);
if (map === undefined) {
map = this._source.map(options);
this._cachedMaps.set(key, map);
}
return map;
}

@@ -87,0 +127,0 @@

@@ -8,2 +8,3 @@ /*

const Source = require("./Source");
const RawSource = require("./RawSource");
const { SourceNode, SourceMapConsumer } = require("source-map");

@@ -13,47 +14,48 @@ const { SourceListMap, fromStringWithSourceMap } = require("source-list-map");

const stringsAsRawSources = new WeakSet();
class ConcatSource extends Source {
constructor() {
super();
this.children = [];
this._children = [];
for (let i = 0; i < arguments.length; i++) {
const item = arguments[i];
if (item instanceof ConcatSource) {
const children = item.children;
for (let j = 0; j < children.length; j++) {
this.children.push(children[j]);
for (const child of item._children) {
this._children.push(child);
}
} else {
this.children.push(item);
this._children.push(item);
}
}
this._isOptimized = arguments.length === 0;
}
getChildren() {
if (!this._isOptimized) this._optimize();
return this._children;
}
add(item) {
if (item instanceof ConcatSource) {
const children = item.children;
for (let j = 0; j < children.length; j++) {
this.children.push(children[j]);
for (const child of item._children) {
this._children.push(child);
}
} else {
this.children.push(item);
this._children.push(item);
}
this._isOptimized = false;
}
addAllSkipOptimizing(items) {
for (const item of items) {
this._children.push(item);
}
}
buffer() {
if (!this._isOptimized) this._optimize();
const buffers = [];
let currentString = undefined;
const children = this.children;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (typeof child === "string") {
if (currentString === undefined) {
currentString = child;
} else {
currentString += child;
}
} else if (typeof child.buffer === "function") {
if (currentString !== undefined) {
buffers.push(Buffer.from(currentString, "utf-8"));
currentString = undefined;
}
for (const child of this._children) {
if (typeof child.buffer === "function") {
buffers.push(child.buffer());

@@ -63,19 +65,8 @@ } else {

if (Buffer.isBuffer(bufferOrString)) {
if (currentString !== undefined) {
buffers.push(Buffer.from(currentString, "utf-8"));
currentString = undefined;
}
buffers.push(bufferOrString);
} else {
if (currentString === undefined) {
currentString = bufferOrString;
} else {
currentString += bufferOrString;
}
buffers.push(Buffer.from(bufferOrString, "utf-8"));
}
}
}
if (currentString !== undefined) {
buffers.push(Buffer.from(currentString, "utf-8"));
}
return Buffer.concat(buffers);

@@ -85,7 +76,6 @@ }

source() {
if (!this._isOptimized) this._optimize();
let source = "";
const children = this.children;
for (let i = 0; i < children.length; i++) {
const child = children[i];
source += typeof child === "string" ? child : child.source();
for (const child of this._children) {
source += child.source();
}

@@ -96,10 +86,6 @@ return source;

size() {
if (!this._isOptimized) this._optimize();
let size = 0;
const children = this.children;
for (let i = 0; i < children.length; i++) {
const child = children[i];
size +=
typeof child === "string"
? Buffer.byteLength(child, "utf-8")
: child.size();
for (const child of this._children) {
size += child.size();
}

@@ -118,2 +104,3 @@ return size;

node(options) {
if (!this._isOptimized) this._optimize();
const node = new SourceNode(

@@ -123,4 +110,3 @@ null,

null,
this.children.map(function(item) {
if (typeof item === "string") return item;
this._children.map(function(item) {
if (typeof item.node === "function") return item.node(options);

@@ -142,6 +128,5 @@ const sourceAndMap = item.sourceAndMap(options);

listMap(options) {
if (!this._isOptimized) this._optimize();
const map = new SourceListMap();
const children = this.children;
for (let i = 0; i < children.length; i++) {
const item = children[i];
for (const item of this._children) {
if (typeof item === "string") {

@@ -166,12 +151,88 @@ map.add(item);

updateHash(hash) {
if (!this._isOptimized) this._optimize();
hash.update("ConcatSource");
const children = this.children;
for (let i = 0; i < children.length; i++) {
const item = children[i];
if (typeof item === "string") hash.update(item);
else item.updateHash(hash);
for (const item of this._children) {
item.updateHash(hash);
}
}
_optimize() {
const newChildren = [];
let currentString = undefined;
let currentRawSources = undefined;
const addStringToRawSources = string => {
if (currentRawSources === undefined) {
currentRawSources = string;
} else if (Array.isArray(currentRawSources)) {
currentRawSources.push(string);
} else {
currentRawSources = [
typeof currentRawSources === "string"
? currentRawSources
: currentRawSources.source(),
string
];
}
};
const addSourceToRawSources = source => {
if (currentRawSources === undefined) {
currentRawSources = source;
} else if (Array.isArray(currentRawSources)) {
currentRawSources.push(source.source());
} else {
currentRawSources = [
typeof currentRawSources === "string"
? currentRawSources
: currentRawSources.source(),
source.source()
];
}
};
const mergeRawSources = () => {
if (Array.isArray(currentRawSources)) {
const rawSource = new RawSource(currentRawSources.join(""));
stringsAsRawSources.add(rawSource);
newChildren.push(rawSource);
} else if (typeof currentRawSources === "string") {
const rawSource = new RawSource(currentRawSources);
stringsAsRawSources.add(rawSource);
newChildren.push(rawSource);
} else {
newChildren.push(currentRawSources);
}
};
for (const child of this._children) {
if (typeof child === "string") {
if (currentString === undefined) {
currentString = child;
} else {
currentString += child;
}
} else {
if (currentString !== undefined) {
addStringToRawSources(currentString);
currentString = undefined;
}
if (stringsAsRawSources.has(child)) {
addSourceToRawSources(child);
} else {
if (currentRawSources !== undefined) {
mergeRawSources();
currentRawSources = undefined;
}
newChildren.push(child);
}
}
}
if (currentString !== undefined) {
addStringToRawSources(currentString);
}
if (currentRawSources !== undefined) {
mergeRawSources();
}
this._children = newChildren;
this._isOptimized = true;
}
}
module.exports = ConcatSource;

@@ -21,10 +21,26 @@ /*

super();
this._value = value;
const isBuffer = Buffer.isBuffer(value);
this._value = isBuffer ? undefined : value;
this._valueAsBuffer = isBuffer ? value : undefined;
this._name = name;
}
getName() {
return this._name;
}
source() {
if (this._value === undefined) {
this._value = this._valueAsBuffer.toString("utf-8");
}
return this._value;
}
buffer() {
if (this._valueAsBuffer === undefined) {
this._valueAsBuffer = Buffer.from(this._value, "utf-8");
}
return this._valueAsBuffer;
}
map(options) {

@@ -39,2 +55,5 @@ return getMap(this, options);

node(options) {
if (this._value === undefined) {
this._value = this._valueAsBuffer.toString("utf-8");
}
const value = this._value;

@@ -76,2 +95,5 @@ const name = this._name;

listMap(options) {
if (this._value === undefined) {
this._value = this._valueAsBuffer.toString("utf-8");
}
return new SourceListMap(this._value, this._name, this._value);

@@ -81,4 +103,7 @@ }

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

@@ -85,0 +110,0 @@ }

@@ -8,2 +8,3 @@ /*

const Source = require("./Source");
const RawSource = require("./RawSource");
const { SourceNode } = require("source-map");

@@ -17,9 +18,19 @@ const { getSourceAndMap, getMap } = require("./helpers");

super();
this._source = source;
this._source =
typeof source === "string" || Buffer.isBuffer(source)
? new RawSource(source, true)
: source;
this._prefix = prefix;
}
getPrefix() {
return this._prefix;
}
original() {
return this._source;
}
source() {
const node =
typeof this._source === "string" ? this._source : this._source.source();
const node = this._source.source();
const prefix = this._prefix;

@@ -29,2 +40,4 @@ return prefix + node.replace(REPLACE_REGEX, "\n" + prefix);

// TODO efficient buffer() implementation
map(options) {

@@ -87,4 +100,3 @@ return getMap(this, options);

hash.update("PrefixSource");
if (typeof this._source === "string") hash.update(this._source);
else this._source.updateHash(hash);
this._source.updateHash(hash);
hash.update(this._prefix);

@@ -91,0 +103,0 @@ }

@@ -12,11 +12,31 @@ /*

class RawSource extends Source {
constructor(value) {
constructor(value, convertToString = false) {
super();
this._value = value;
const isBuffer = Buffer.isBuffer(value);
if (!isBuffer && typeof value !== "string") {
throw new TypeError("argument 'value' must be either string of Buffer");
}
this._valueIsBuffer = !convertToString && isBuffer;
this._value = convertToString && isBuffer ? undefined : value;
this._valueAsBuffer = isBuffer ? value : undefined;
}
isBuffer() {
return this._valueIsBuffer;
}
source() {
if (this._value === undefined) {
this._value = this._valueAsBuffer.toString("utf-8");
}
return this._value;
}
buffer() {
if (this._valueAsBuffer === undefined) {
this._valueAsBuffer = Buffer.from(this._value, "utf-8");
}
return this._valueAsBuffer;
}
map(options) {

@@ -27,2 +47,5 @@ return null;

node(options) {
if (this._value === undefined) {
this._value = this._valueAsBuffer.toString("utf-8");
}
return new SourceNode(null, null, null, this._value);

@@ -32,2 +55,5 @@ }

listMap(options) {
if (this._value === undefined) {
this._value = this._valueAsBuffer.toString("utf-8");
}
return new SourceListMap(this._value);

@@ -37,4 +63,7 @@ }

updateHash(hash) {
if (this._valueAsBuffer === undefined) {
this._valueAsBuffer = Buffer.from(this._value, "utf-8");
}
hash.update("RawSource");
hash.update(this._value);
hash.update(this._valueAsBuffer);
}

@@ -41,0 +70,0 @@ }

@@ -31,2 +31,14 @@ /*

getName() {
return this._name;
}
getReplacements() {
const replacements = Array.from(this._replacements);
replacements.sort((a, b) => {
return a.insertIndex - b.insertIndex;
});
return replacements;
}
replace(start, end, newValue, name) {

@@ -33,0 +45,0 @@ if (typeof newValue !== "string")

@@ -25,6 +25,10 @@ /*

source(options) {
source() {
throw this._error();
}
buffer() {
throw this._error();
}
map(options) {

@@ -31,0 +35,0 @@ throw this._error();

@@ -19,3 +19,3 @@ /*

size() {
return Buffer.byteLength(this.source());
return this.buffer().length;
}

@@ -22,0 +22,0 @@

@@ -23,27 +23,150 @@ /*

super();
this._value = value;
const valueIsBuffer = Buffer.isBuffer(value);
this._valueAsString = valueIsBuffer ? undefined : value;
this._valueAsBuffer = valueIsBuffer ? value : undefined;
this._name = name;
this._sourceMap = sourceMap;
this._sourceMapObject = undefined;
this._sourceMapString = undefined;
this._originalSource = originalSource;
this._innerSourceMap = innerSourceMap;
this._innerSourceMapObject = undefined;
this._innerSourceMapString = undefined;
this._hasSourceMap = !!sourceMap;
const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
const sourceMapIsString = typeof sourceMap === "string";
this._sourceMapAsObject =
sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
this._hasOriginalSource = !!originalSource;
const originalSourceIsBuffer = Buffer.isBuffer(originalSource);
this._originalSourceAsString = originalSourceIsBuffer
? undefined
: originalSource;
this._originalSourceAsBuffer = originalSourceIsBuffer
? originalSource
: undefined;
this._hasInnerSourceMap = !!innerSourceMap;
const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
const innerSourceMapIsString = typeof innerSourceMap === "string";
this._innerSourceMapAsObject =
innerSourceMapIsBuffer || innerSourceMapIsString
? undefined
: innerSourceMap;
this._innerSourceMapAsString = innerSourceMapIsString
? innerSourceMap
: undefined;
this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
? innerSourceMap
: undefined;
this._removeOriginalSource = removeOriginalSource;
}
_ensureValueBuffer() {
if (this._valueAsBuffer === undefined) {
this._valueAsBuffer = Buffer.from(this._valueAsString, "utf-8");
}
}
_ensureValueString() {
if (this._valueAsString === undefined) {
this._valueAsString = this._valueAsBuffer.toString("utf-8");
}
}
_ensureOriginalSourceBuffer() {
if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
this._originalSourceAsBuffer = Buffer.from(
this._originalSourceAsString,
"utf-8"
);
}
}
_ensureOriginalSourceString() {
if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
this._originalSourceAsString = this._originalSourceAsBuffer.toString(
"utf-8"
);
}
}
_ensureInnerSourceMapObject() {
if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
this._ensureInnerSourceMapString();
this._innerSourceMapAsObject = JSON.parse(this._innerSourceMapAsString);
}
}
_ensureInnerSourceMapBuffer() {
if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
this._ensureInnerSourceMapString();
this._innerSourceMapAsBuffer = Buffer.from(
this._innerSourceMapAsString,
"utf-8"
);
}
}
_ensureInnerSourceMapString() {
if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
if (this._innerSourceMapAsBuffer !== undefined) {
this._innerSourceMapAsString = this._innerSourceMapAsBuffer.toString(
"utf-8"
);
} else {
this._innerSourceMapAsString = JSON.stringify(
this._innerSourceMapAsObject
);
}
}
}
_ensureSourceMapObject() {
if (this._sourceMapAsObject === undefined) {
this._ensureSourceMapString();
this._sourceMapAsObject = JSON.parse(this._sourceMapAsString);
}
}
_ensureSourceMapBuffer() {
if (this._sourceMapAsBuffer === undefined) {
this._ensureSourceMapString();
this._sourceMapAsBuffer = Buffer.from(this._sourceMapAsString, "utf-8");
}
}
_ensureSourceMapString() {
if (this._sourceMapAsString === undefined) {
if (this._sourceMapAsBuffer !== undefined) {
this._sourceMapAsString = this._sourceMapAsBuffer.toString("utf-8");
} else {
this._sourceMapAsString = JSON.stringify(this._sourceMapAsObject);
}
}
}
getArgsAsBuffers() {
this._ensureValueBuffer();
this._ensureSourceMapBuffer();
this._ensureOriginalSourceBuffer();
this._ensureInnerSourceMapBuffer();
return [
this._valueAsBuffer,
this._name,
this._sourceMapAsBuffer,
this._originalSourceAsBuffer,
this._innerSourceMapAsBuffer,
this._removeOriginalSource
];
}
source() {
return this._value;
this._ensureValueString();
return this._valueAsString;
}
map(options) {
if (!this._innerSourceMap) {
return (
this._sourceMapObject ||
(this._sourceMapObject =
typeof this._sourceMap === "string"
? JSON.parse(this._sourceMap)
: this._sourceMap)
);
if (!this._hasInnerSourceMap) {
this._ensureSourceMapObject();
return this._sourceMapAsObject;
}

@@ -54,11 +177,8 @@ return getMap(this, options);

sourceAndMap(options) {
if (!this._innerSourceMap) {
if (!this._hasInnerSourceMap) {
this._ensureValueString();
this._ensureSourceMapObject();
return {
source: this._value,
map:
this._sourceMapObject ||
(this._sourceMapObject =
typeof this._sourceMap === "string"
? JSON.parse(this._sourceMap)
: this._sourceMap)
source: this._valueAsString,
map: this._sourceMapAsObject
};

@@ -70,23 +190,15 @@ }

node(options) {
const sourceMap =
this._sourceMapObject ||
(this._sourceMapObject =
typeof this._sourceMap === "string"
? JSON.parse(this._sourceMap)
: this._sourceMap);
this._ensureValueString();
this._ensureSourceMapObject();
this._ensureOriginalSourceString();
let node = SourceNode.fromStringWithSourceMap(
this._value,
new SourceMapConsumer(sourceMap)
this._valueAsString,
new SourceMapConsumer(this._sourceMapAsObject)
);
node.setSourceContent(this._name, this._originalSource);
if (this._innerSourceMap) {
const innerSourceMap =
this._innerSourceMapObject ||
(this._innerSourceMapObject =
typeof this._innerSourceMap === "string"
? JSON.parse(this._innerSourceMap)
: this._innerSourceMap);
node.setSourceContent(this._name, this._originalSourceAsString);
if (this._hasInnerSourceMap) {
this._ensureInnerSourceMapObject();
node = applySourceMap(
node,
new SourceMapConsumer(innerSourceMap),
new SourceMapConsumer(this._innerSourceMapAsObject),
this._name,

@@ -100,40 +212,36 @@ this._removeOriginalSource

listMap(options) {
this._ensureValueString();
this._ensureSourceMapObject();
options = options || {};
if (options.module === false)
return new SourceListMap(this._value, this._name, this._value);
return new SourceListMap(
this._valueAsString,
this._name,
this._valueAsString
);
const sourceMap =
this._sourceMapObject ||
(this._sourceMapObject =
typeof this._sourceMap === "string"
? JSON.parse(this._sourceMap)
: this._sourceMap);
return fromStringWithSourceMap(this._value, sourceMap);
return fromStringWithSourceMap(
this._valueAsString,
this._sourceMapAsObject
);
}
updateHash(hash) {
this._ensureValueBuffer();
this._ensureSourceMapBuffer();
this._ensureOriginalSourceBuffer();
this._ensureInnerSourceMapBuffer();
hash.update("SourceMapSource");
hash.update(this._value);
hash.update(this._valueAsBuffer);
const sourceMap =
this._sourceMapString ||
(this._sourceMapString =
typeof this._sourceMap === "string"
? this._sourceMap
: JSON.stringify(this._sourceMap));
hash.update(sourceMap);
hash.update(this._sourceMapAsBuffer);
if (this._originalSource) {
hash.update(this._originalSource);
if (this._hasOriginalSource) {
hash.update(this._originalSourceAsBuffer);
}
if (this._innerSourceMap) {
const innerSourceMap =
this._innerSourceMapString ||
(this._innerSourceMapString =
typeof this._innerSourceMap === "string"
? this._innerSourceMap
: JSON.stringify(this._innerSourceMap));
hash.update(innerSourceMap);
if (this._hasInnerSourceMap) {
hash.update(this._innerSourceMapAsBuffer);
}

@@ -140,0 +248,0 @@

{
"name": "webpack-sources",
"version": "2.0.0-beta.5",
"version": "2.0.0-beta.6",
"description": "Source code handling classes for webpack",

@@ -5,0 +5,0 @@ "main": "./lib/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc