Skip to content

Commit

Permalink
policy: increase tests via permutation matrix
Browse files Browse the repository at this point in the history
PR-URL: #34404
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bmeck authored and addaleax committed Sep 22, 2020
1 parent 90a44e1 commit 35ff592
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 438 deletions.
7 changes: 1 addition & 6 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,13 @@ function readPackage(requestPath) {
const existing = packageJsonCache.get(jsonPath);
if (existing !== undefined) return existing;

const result = packageJsonReader.read(path.toNamespacedPath(jsonPath));
const result = packageJsonReader.read(jsonPath);
const json = result.containsKeys === false ? '{}' : result.string;
if (json === undefined) {
packageJsonCache.set(jsonPath, false);
return false;
}

if (manifest) {
const jsonURL = pathToFileURL(jsonPath);
manifest.assertIntegrity(jsonURL, json);
}

try {
const parsed = JSONParse(json);
const filtered = {
Expand Down
21 changes: 14 additions & 7 deletions lib/internal/modules/esm/get_source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';

const { getOptionValue } = require('internal/options');
const manifest = getOptionValue('--experimental-policy') ?
require('internal/process/policy').manifest :
null;

const { Buffer } = require('buffer');

const fs = require('fs');
Expand All @@ -15,20 +20,22 @@ const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;

async function defaultGetSource(url, { format } = {}, defaultGetSource) {
const parsed = new URL(url);
let source;
if (parsed.protocol === 'file:') {
return {
source: await readFileAsync(parsed)
};
source = await readFileAsync(parsed);
} else if (parsed.protocol === 'data:') {
const match = DATA_URL_PATTERN.exec(parsed.pathname);
if (!match) {
throw new ERR_INVALID_URL(url);
}
const [ , base64, body ] = match;
return {
source: Buffer.from(body, base64 ? 'base64' : 'utf8')
};
source = Buffer.from(body, base64 ? 'base64' : 'utf8');
} else {
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
}
if (manifest) {
manifest.assertIntegrity(parsed, source);
}
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
return { source };
}
exports.defaultGetSource = defaultGetSource;
26 changes: 20 additions & 6 deletions lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@

const { SafeMap } = primordials;
const { internalModuleReadJSON } = internalBinding('fs');
const { pathToFileURL } = require('url');
const { toNamespacedPath } = require('path');

const cache = new SafeMap();

/**
*
* @param {string} path
* @param {string} jsonPath
*/
function read(path) {
if (cache.has(path)) {
return cache.get(path);
function read(jsonPath) {
if (cache.has(jsonPath)) {
return cache.get(jsonPath);
}

const [string, containsKeys] = internalModuleReadJSON(path);
const [string, containsKeys] = internalModuleReadJSON(
toNamespacedPath(jsonPath)
);
const result = { string, containsKeys };
cache.set(path, result);
const { getOptionValue } = require('internal/options');
if (string !== undefined) {
const manifest = getOptionValue('--experimental-policy') ?
require('internal/process/policy').manifest :
null;
if (manifest) {
const jsonURL = pathToFileURL(jsonPath);
manifest.assertIntegrity(jsonURL, string);
}
}
cache.set(jsonPath, result);
return result;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ class Worker extends EventEmitter {
cwdCounter: cwdCounter || workerIo.sharedCwdCounter,
workerData: options.workerData,
publicPort: port2,
manifestURL: getOptionValue('--experimental-policy') ?
require('internal/process/policy').url :
null,
manifestSrc: getOptionValue('--experimental-policy') ?
require('internal/process/policy').src :
null,
Expand Down

0 comments on commit 35ff592

Please sign in to comment.