Socket
Socket
Sign inDemoInstall

@parcel/utils

Package Overview
Dependencies
Maintainers
1
Versions
877
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@parcel/utils - npm Package Compare versions

Comparing version 2.0.0-nightly.79 to 2.0.0-nightly.85

131

lib/replaceBundleReferences.js

@@ -6,4 +6,4 @@ "use strict";

});
exports.replaceBundleReferences = replaceBundleReferences;
exports.replaceURLReferences = replaceURLReferences;
exports.replaceInlineReferences = replaceInlineReferences;

@@ -21,9 +21,9 @@ var _stream = require("stream");

/*
* Replaces references to dependency ids with either:
* - in the case of an inline bundle, the packaged contents of that bundle
* - in the case of another bundle reference, the bundle's url from the publicUrl root
* - in the case of a url dependency that Parcel did not handle,
* the original moduleSpecifier. These are external requests.
* Replaces references to dependency ids for URL dependencies with:
* - in the case of an unresolvable url dependency, the original moduleSpecifier.
* These are external requests that Parcel did not bundle.
* - in the case of a reference to another bundle, the relative url to that
* bundle from the current bundle.
*/
async function replaceBundleReferences({
function replaceURLReferences({
bundle,

@@ -33,82 +33,103 @@ bundleGraph,

map,
getInlineReplacement,
getInlineBundleContents
relative = true
}) {
let replacements = new Map();
for (let {
dependency,
bundleGroup
} of bundleGraph.getBundleGroupsReferencedByBundle(bundle)) {
for (let dependency of bundleGraph.getExternalDependencies(bundle)) {
if (!dependency.isURL) {
continue;
}
let bundleGroup = bundleGraph.resolveExternalDependency(dependency);
if (bundleGroup == null) {
replacements.set(dependency.id, {
from: dependency.id,
to: dependency.moduleSpecifier
});
continue;
}
let [entryBundle] = bundleGraph.getBundlesInBundleGroup(bundleGroup);
if (entryBundle.isInline) {
// inline bundles
let packagedBundle = await getInlineBundleContents(entryBundle, bundleGraph);
let packagedContents = (packagedBundle.contents instanceof _stream.Readable ? await (0, _.bufferStream)(packagedBundle.contents) : packagedBundle.contents).toString();
let inlineType = (0, _nullthrows.default)(entryBundle.getMainEntry()).meta.inlineType;
// If a bundle is inline, it should be replaced with inline contents,
// not a URL.
continue;
}
if (inlineType == null || inlineType === 'string') {
replacements.set(dependency.id, getInlineReplacement(dependency, inlineType, packagedContents));
}
} else if (dependency.isURL) {
// url references
replacements.set(dependency.id, getURLReplacement(dependency, entryBundle));
}
replacements.set(dependency.id, getURLReplacement({
dependency,
fromBundle: bundle,
toBundle: entryBundle,
relative
}));
}
collectExternalReferences(bundle, replacements);
return performReplacement(replacements, contents, map);
}
/*
* Replaces references to dependency ids for inline bundles with the packaged
* contents of that bundle.
*/
function replaceURLReferences({
async function replaceInlineReferences({
bundle,
bundleGraph,
contents,
map
map,
getInlineReplacement,
getInlineBundleContents
}) {
let replacements = new Map();
for (let {
dependency,
bundleGroup
} of bundleGraph.getBundleGroupsReferencedByBundle(bundle)) {
let [entryBundle] = bundleGraph.getBundlesInBundleGroup(bundleGroup);
for (let dependency of bundleGraph.getExternalDependencies(bundle)) {
let bundleGroup = bundleGraph.resolveExternalDependency(dependency);
if (dependency.isURL && !entryBundle.isInline) {
// url references
replacements.set(dependency.id, getURLReplacement(dependency, entryBundle));
if (bundleGroup == null) {
continue;
}
}
collectExternalReferences(bundle, replacements);
return performReplacement(replacements, contents, map);
}
let [entryBundle] = bundleGraph.getBundlesInBundleGroup(bundleGroup);
function collectExternalReferences(bundle, replacements) {
bundle.traverse(node => {
if (node.type !== 'dependency') {
return;
if (!entryBundle.isInline) {
continue;
}
let dependency = node.value;
let packagedBundle = await getInlineBundleContents(entryBundle, bundleGraph);
let packagedContents = (packagedBundle.contents instanceof _stream.Readable ? await (0, _.bufferStream)(packagedBundle.contents) : packagedBundle.contents).toString();
let inlineType = (0, _nullthrows.default)(entryBundle.getMainEntry()).meta.inlineType;
if (dependency.isURL && !replacements.has(dependency.id)) {
replacements.set(dependency.id, {
from: dependency.id,
to: dependency.moduleSpecifier
});
if (inlineType == null || inlineType === 'string') {
replacements.set(dependency.id, getInlineReplacement(dependency, inlineType, packagedContents));
}
});
}
return performReplacement(replacements, contents, map);
}
function getURLReplacement(dependency, bundle) {
var _bundle$target$public;
function getURLReplacement({
dependency,
fromBundle,
toBundle,
relative
}) {
let url = _url.default.parse(dependency.moduleSpecifier);
url.pathname = (0, _nullthrows.default)(bundle.name);
let to;
if (relative) {
url.pathname = (0, _.relativeBundlePath)(fromBundle, toBundle, {
leadingDotSlash: false
});
to = _url.default.format(url);
} else {
url.pathname = (0, _nullthrows.default)(toBundle.name);
to = (0, _.urlJoin)((0, _nullthrows.default)(toBundle.target.publicUrl), _url.default.format(url));
}
return {
from: dependency.id,
to: (0, _.urlJoin)((_bundle$target$public = bundle.target.publicUrl) !== null && _bundle$target$public !== void 0 ? _bundle$target$public : '/', _url.default.format(url))
to
};

@@ -115,0 +136,0 @@ }

@@ -172,34 +172,42 @@ "use strict";

}
} else if (schemaNode.oneOf || schemaNode.allOf) {
let list = schemaNode.oneOf || schemaNode.allOf;
let results = [];
for (let f of list) {
let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
if (result) results.push(result);
}
if (schemaNode.oneOf ? results.length == schemaNode.oneOf.length : results.length > 0) {
// return the result with more values / longer key
results.sort((a, b) => Array.isArray(a) || Array.isArray(b) ? Array.isArray(a) && !Array.isArray(b) ? -1 : !Array.isArray(a) && Array.isArray(b) ? 1 : Array.isArray(a) && Array.isArray(b) ? b.length - a.length : 0 : b.dataPath.length - a.dataPath.length);
return results[0];
}
} else if (schemaNode.not) {
let result = walk([schemaNode.not].concat(schemaAncestors), dataNode, dataPath);
if (!result || result.length == 0) {
} else {
if (schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
return {
type: 'other',
dataPath,
dataType: null,
message: schemaNode.__message,
actualValue: dataNode,
type: 'enum',
dataType: 'value',
dataPath: dataPath,
expectedValues: schemaNode.enum,
actualValue: schemaNode,
ancestors: schemaAncestors
};
}
} else if (Object.keys(schemaNode).length == 0) {
// "any"
return undefined;
} else {
throw new Error(`Unimplemented schema?`);
if (schemaNode.oneOf || schemaNode.allOf) {
let list = schemaNode.oneOf || schemaNode.allOf;
let results = [];
for (let f of list) {
let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
if (result) results.push(result);
}
if (schemaNode.oneOf ? results.length == schemaNode.oneOf.length : results.length > 0) {
// return the result with more values / longer key
results.sort((a, b) => Array.isArray(a) || Array.isArray(b) ? Array.isArray(a) && !Array.isArray(b) ? -1 : !Array.isArray(a) && Array.isArray(b) ? 1 : Array.isArray(a) && Array.isArray(b) ? b.length - a.length : 0 : b.dataPath.length - a.dataPath.length);
return results[0];
}
} else if (schemaNode.not) {
let result = walk([schemaNode.not].concat(schemaAncestors), dataNode, dataPath);
if (!result || result.length == 0) {
return {
type: 'other',
dataPath,
dataType: null,
message: schemaNode.__message,
actualValue: dataNode,
ancestors: schemaAncestors
};
}
}
}

@@ -206,0 +214,0 @@

{
"name": "@parcel/utils",
"version": "2.0.0-nightly.79+287ac639",
"version": "2.0.0-nightly.85+e33d9161",
"description": "Blazing fast, zero configuration web application bundler",

@@ -20,6 +20,6 @@ "license": "MIT",

"@iarna/toml": "^2.2.0",
"@parcel/codeframe": "2.0.0-nightly.79+287ac639",
"@parcel/diagnostic": "2.0.0-nightly.79+287ac639",
"@parcel/logger": "2.0.0-nightly.79+287ac639",
"@parcel/markdown-ansi": "2.0.0-nightly.79+287ac639",
"@parcel/codeframe": "2.0.0-nightly.85+e33d9161",
"@parcel/diagnostic": "2.0.0-nightly.85+e33d9161",
"@parcel/logger": "2.0.0-nightly.85+e33d9161",
"@parcel/markdown-ansi": "2.0.0-nightly.85+e33d9161",
"ansi-html": "^0.0.7",

@@ -44,3 +44,3 @@ "chalk": "^2.4.2",

},
"gitHead": "287ac6397ade28595d56ac73971edb832bda8ff7"
"gitHead": "e33d91613d0ab9f600c93c5f8466f20fe9ca9dce"
}
// @flow strict-local
import type SourceMap from '@parcel/source-map';
import type {
Async,
Blob,
Bundle,
BundleResult,
BundleGraph,
Dependency,
} from '@parcel/types';
import type {Async, Blob, Bundle, BundleGraph, Dependency} from '@parcel/types';

@@ -16,3 +9,3 @@ import {Readable} from 'stream';

import URL from 'url';
import {bufferStream, urlJoin} from '../';
import {bufferStream, relativeBundlePath, urlJoin} from '../';

@@ -25,9 +18,9 @@ type ReplacementMap = Map<

/*
* Replaces references to dependency ids with either:
* - in the case of an inline bundle, the packaged contents of that bundle
* - in the case of another bundle reference, the bundle's url from the publicUrl root
* - in the case of a url dependency that Parcel did not handle,
* the original moduleSpecifier. These are external requests.
* Replaces references to dependency ids for URL dependencies with:
* - in the case of an unresolvable url dependency, the original moduleSpecifier.
* These are external requests that Parcel did not bundle.
* - in the case of a reference to another bundle, the relative url to that
* bundle from the current bundle.
*/
export async function replaceBundleReferences({
export function replaceURLReferences({
bundle,

@@ -37,4 +30,3 @@ bundleGraph,

map,
getInlineReplacement,
getInlineBundleContents,
relative = true,
}: {|

@@ -44,52 +36,47 @@ bundle: Bundle,

contents: string,
getInlineReplacement: (
Dependency,
?'string',
string,
) => {|from: string, to: string|},
getInlineBundleContents: (
Bundle,
BundleGraph,
) => Async<{|contents: Blob, map: ?(Readable | string)|}>,
relative?: boolean,
map?: ?SourceMap,
|}): Promise<BundleResult> {
|}): {|+contents: string, +map: ?SourceMap|} {
let replacements = new Map();
for (let {
dependency,
bundleGroup,
} of bundleGraph.getBundleGroupsReferencedByBundle(bundle)) {
for (let dependency of bundleGraph.getExternalDependencies(bundle)) {
if (!dependency.isURL) {
continue;
}
let bundleGroup = bundleGraph.resolveExternalDependency(dependency);
if (bundleGroup == null) {
replacements.set(dependency.id, {
from: dependency.id,
to: dependency.moduleSpecifier,
});
continue;
}
let [entryBundle] = bundleGraph.getBundlesInBundleGroup(bundleGroup);
if (entryBundle.isInline) {
// inline bundles
let packagedBundle = await getInlineBundleContents(
entryBundle,
bundleGraph,
);
let packagedContents = (packagedBundle.contents instanceof Readable
? await bufferStream(packagedBundle.contents)
: packagedBundle.contents
).toString();
// If a bundle is inline, it should be replaced with inline contents,
// not a URL.
continue;
}
let inlineType = nullthrows(entryBundle.getMainEntry()).meta.inlineType;
if (inlineType == null || inlineType === 'string') {
replacements.set(
dependency.id,
getInlineReplacement(dependency, inlineType, packagedContents),
);
}
} else if (dependency.isURL) {
// url references
replacements.set(
dependency.id,
getURLReplacement(dependency, entryBundle),
);
}
replacements.set(
dependency.id,
getURLReplacement({
dependency,
fromBundle: bundle,
toBundle: entryBundle,
relative,
}),
);
}
collectExternalReferences(bundle, replacements);
return performReplacement(replacements, contents, map);
}
export function replaceURLReferences({
/*
* Replaces references to dependency ids for inline bundles with the packaged
* contents of that bundle.
*/
export async function replaceInlineReferences({
bundle,

@@ -99,2 +86,4 @@ bundleGraph,

map,
getInlineReplacement,
getInlineBundleContents,
}: {|

@@ -104,16 +93,40 @@ bundle: Bundle,

contents: string,
getInlineReplacement: (
Dependency,
?'string',
string,
) => {|from: string, to: string|},
getInlineBundleContents: (
Bundle,
BundleGraph,
) => Async<{|contents: Blob, map: ?(Readable | string)|}>,
map?: ?SourceMap,
|}): BundleResult {
let replacements: ReplacementMap = new Map();
|}): Promise<{|+contents: string, +map: ?SourceMap|}> {
let replacements = new Map();
for (let {
dependency,
bundleGroup,
} of bundleGraph.getBundleGroupsReferencedByBundle(bundle)) {
for (let dependency of bundleGraph.getExternalDependencies(bundle)) {
let bundleGroup = bundleGraph.resolveExternalDependency(dependency);
if (bundleGroup == null) {
continue;
}
let [entryBundle] = bundleGraph.getBundlesInBundleGroup(bundleGroup);
if (dependency.isURL && !entryBundle.isInline) {
// url references
if (!entryBundle.isInline) {
continue;
}
let packagedBundle = await getInlineBundleContents(
entryBundle,
bundleGraph,
);
let packagedContents = (packagedBundle.contents instanceof Readable
? await bufferStream(packagedBundle.contents)
: packagedBundle.contents
).toString();
let inlineType = nullthrows(entryBundle.getMainEntry()).meta.inlineType;
if (inlineType == null || inlineType === 'string') {
replacements.set(
dependency.id,
getURLReplacement(dependency, entryBundle),
getInlineReplacement(dependency, inlineType, packagedContents),
);

@@ -123,31 +136,31 @@ }

collectExternalReferences(bundle, replacements);
return performReplacement(replacements, contents, map);
}
function collectExternalReferences(
bundle: Bundle,
replacements: Map<string, {|from: string, to: string|}>,
): void {
bundle.traverse(node => {
if (node.type !== 'dependency') {
return;
}
function getURLReplacement({
dependency,
fromBundle,
toBundle,
relative,
}: {|
dependency: Dependency,
fromBundle: Bundle,
toBundle: Bundle,
relative: boolean,
|}) {
let url = URL.parse(dependency.moduleSpecifier);
let to;
if (relative) {
url.pathname = relativeBundlePath(fromBundle, toBundle, {
leadingDotSlash: false,
});
to = URL.format(url);
} else {
url.pathname = nullthrows(toBundle.name);
to = urlJoin(nullthrows(toBundle.target.publicUrl), URL.format(url));
}
let dependency = node.value;
if (dependency.isURL && !replacements.has(dependency.id)) {
replacements.set(dependency.id, {
from: dependency.id,
to: dependency.moduleSpecifier,
});
}
});
}
function getURLReplacement(dependency: Dependency, bundle: Bundle) {
let url = URL.parse(dependency.moduleSpecifier);
url.pathname = nullthrows(bundle.name);
return {
from: dependency.id,
to: urlJoin(bundle.target.publicUrl ?? '/', URL.format(url)),
to,
};

@@ -160,3 +173,3 @@ }

map?: ?SourceMap,
): BundleResult {
): {|+contents: string, +map: ?SourceMap|} {
let finalContents = contents;

@@ -163,0 +176,0 @@ for (let {from, to} of replacements.values()) {

@@ -13,2 +13,3 @@ // @flow strict-local

| SchemaString
| SchemaEnum
| SchemaOneOf

@@ -43,2 +44,5 @@ | SchemaAllOf

|};
export type SchemaEnum = {|
enum: Array<mixed>,
|};
export type SchemaObject = {|

@@ -112,2 +116,3 @@ type: 'object',

let [schemaNode] = schemaAncestors;
if (schemaNode.type) {

@@ -275,49 +280,57 @@ let type = Array.isArray(dataNode) ? 'array' : typeof dataNode;

}
} else if (schemaNode.oneOf || schemaNode.allOf) {
let list = schemaNode.oneOf || schemaNode.allOf;
let results: Array<SchemaError | Array<SchemaError>> = [];
for (let f of list) {
let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
if (result) results.push(result);
}
if (
schemaNode.oneOf
? results.length == schemaNode.oneOf.length
: results.length > 0
) {
// return the result with more values / longer key
results.sort((a, b) =>
Array.isArray(a) || Array.isArray(b)
? Array.isArray(a) && !Array.isArray(b)
? -1
: !Array.isArray(a) && Array.isArray(b)
? 1
: Array.isArray(a) && Array.isArray(b)
? b.length - a.length
: 0
: b.dataPath.length - a.dataPath.length,
);
return results[0];
}
} else if (schemaNode.not) {
let result = walk(
[schemaNode.not].concat(schemaAncestors),
dataNode,
dataPath,
);
if (!result || result.length == 0) {
} else {
if (schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
return {
type: 'other',
dataPath,
dataType: null,
message: schemaNode.__message,
actualValue: dataNode,
type: 'enum',
dataType: 'value',
dataPath: dataPath,
expectedValues: schemaNode.enum,
actualValue: schemaNode,
ancestors: schemaAncestors,
};
}
} else if (Object.keys(schemaNode).length == 0) {
// "any"
return undefined;
} else {
throw new Error(`Unimplemented schema?`);
if (schemaNode.oneOf || schemaNode.allOf) {
let list = schemaNode.oneOf || schemaNode.allOf;
let results: Array<SchemaError | Array<SchemaError>> = [];
for (let f of list) {
let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
if (result) results.push(result);
}
if (
schemaNode.oneOf
? results.length == schemaNode.oneOf.length
: results.length > 0
) {
// return the result with more values / longer key
results.sort((a, b) =>
Array.isArray(a) || Array.isArray(b)
? Array.isArray(a) && !Array.isArray(b)
? -1
: !Array.isArray(a) && Array.isArray(b)
? 1
: Array.isArray(a) && Array.isArray(b)
? b.length - a.length
: 0
: b.dataPath.length - a.dataPath.length,
);
return results[0];
}
} else if (schemaNode.not) {
let result = walk(
[schemaNode.not].concat(schemaAncestors),
dataNode,
dataPath,
);
if (!result || result.length == 0) {
return {
type: 'other',
dataPath,
dataType: null,
message: schemaNode.__message,
actualValue: dataNode,
ancestors: schemaAncestors,
};
}
}
}

@@ -324,0 +337,0 @@

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc