Skip to content

Commit

Permalink
Merge pull request #435 from jsenv/http_build
Browse files Browse the repository at this point in the history
Http build
  • Loading branch information
dmail authored Aug 19, 2024
2 parents 8ec7b70 + 280b095 commit 5c7a5f0
Show file tree
Hide file tree
Showing 92 changed files with 3,250 additions and 532 deletions.
2,069 changes: 2,064 additions & 5 deletions dist/js/ws.js

Large diffs are not rendered by default.

415 changes: 280 additions & 135 deletions dist/jsenv_core.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/core",
"version": "39.3.12",
"version": "39.4.0",
"description": "Tool to develop, test and build js projects",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -69,17 +69,17 @@
"dependencies": {
"@financial-times/polyfill-useragent-normaliser": "1.10.2",
"@jsenv/abort": "4.3.0",
"@jsenv/ast": "6.2.16",
"@jsenv/ast": "6.2.17",
"@jsenv/filesystem": "4.10.2",
"@jsenv/humanize": "1.2.8",
"@jsenv/importmap": "1.2.1",
"@jsenv/integrity": "0.0.2",
"@jsenv/js-module-fallback": "1.3.37",
"@jsenv/js-module-fallback": "1.3.38",
"@jsenv/node-esm-resolution": "1.0.6",
"@jsenv/plugin-bundling": "2.7.7",
"@jsenv/plugin-minification": "1.5.5",
"@jsenv/plugin-supervisor": "1.5.18",
"@jsenv/plugin-transpilation": "1.4.21",
"@jsenv/plugin-supervisor": "1.5.19",
"@jsenv/plugin-transpilation": "1.4.22",
"@jsenv/runtime-compat": "1.3.1",
"@jsenv/server": "15.3.0",
"@jsenv/sourcemap": "1.2.23",
Expand Down
2 changes: 1 addition & 1 deletion packages/independent/eslint-config-relax/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/eslint-config-relax",
"version": "1.1.2",
"version": "1.2.0",
"description": "A pleaseant ESLint configuration for any project",
"license": "MIT",
"author": {
Expand Down
3 changes: 2 additions & 1 deletion packages/independent/eslint-config-relax/src/rules_relax.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export const rulesRelax = {
"no-multi-str": ["error"],
"no-multiple-empty-lines": ["off", { max: 1 }],
"no-native-reassign": ["error"],
"no-negated-condition": ["error"],
// theorically great but too often a pain, does not fix the relax mindset
"no-negated-condition": ["off"],
// disabled because deprecated in favor of no-unsafe-negation
// https://eslint.org/docs/rules/no-negated-in-lhs
"no-negated-in-lhs": ["off"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1818,15 +1818,13 @@ const createDynamicLog = ({
// is that node.js will later throw error if stream gets closed
// while something listening data on it
const spyStreamOutput = (stream, callback) => {
const originalWrite = stream.write;

let output = "";
let installed = true;

const originalWrite = stream.write;
stream.write = function (...args /* chunk, encoding, callback */) {
output += args;
callback(output);
return originalWrite.call(stream, ...args);
return originalWrite.call(this, ...args);
};

const uninstall = () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/independent/humanize/src/log/dynamic_log.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,13 @@ export const createDynamicLog = ({
// is that node.js will later throw error if stream gets closed
// while something listening data on it
const spyStreamOutput = (stream, callback) => {
const originalWrite = stream.write;

let output = "";
let installed = true;

const originalWrite = stream.write;
stream.write = function (...args /* chunk, encoding, callback */) {
output += args;
callback(output);
return originalWrite.call(stream, ...args);
return originalWrite.call(this, ...args);
};

const uninstall = () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/independent/snapshot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/snapshot",
"version": "2.10.2",
"version": "2.10.3",
"description": "Snapshot testing",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@jsenv/assert": "4.4.1",
"@jsenv/ast": "6.2.16",
"@jsenv/ast": "6.2.17",
"@jsenv/exception": "1.1.2",
"@jsenv/humanize": "1.2.8",
"@jsenv/filesystem": "4.10.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export const createCaptureSideEffects = ({
rootDirectoryUrl,
}),
}) => {
if (executionEffects === false) {
executionEffects = {
catch: false,
return: false,
};
}

const detectors = [];
if (logEffects) {
detectors.push(logSideEffects(logEffects === true ? {} : logEffects));
Expand Down Expand Up @@ -283,6 +290,33 @@ export const createCaptureSideEffects = ({
finallyCallbackSet.clear();
};

const onThrowOrReject = (value, isThrow) => {
if (executionEffects.catch === false) {
throw value;
}
if (typeof executionEffects.catch === "function") {
executionEffects.catch(value);
}
if (isThrow) {
onCatch(value);
} else {
onReject(value);
}
};
const onReturnOrResolve = (value, isReturn) => {
if (executionEffects.return === false) {
return;
}
if (typeof executionEffects.return === "function") {
executionEffects.return(value);
}
if (isReturn) {
onReturn(value);
} else {
onResolve(value);
}
};

process.env.CAPTURING_SIDE_EFFECTS = "1";
functionExecutingCount++;
let returnedPromise = false;
Expand All @@ -292,34 +326,22 @@ export const createCaptureSideEffects = ({
onReturn(RETURN_PROMISE);
returnedPromise = valueReturned.then(
(value) => {
onResolve(value);
onReturnOrResolve(value);
onFinally();
return sideEffects;
},
(e) => {
if (executionEffects.catch === false) {
throw e;
}
if (typeof executionEffects.catch === "function") {
executionEffects.catch(e);
}
onReject(e);
onThrowOrReject(e);
onFinally();
return sideEffects;
},
);
return returnedPromise;
}
onReturn(valueReturned);
onReturnOrResolve(valueReturned, true);
return sideEffects;
} catch (e) {
if (executionEffects.catch === false) {
throw e;
}
if (typeof executionEffects.catch === "function") {
executionEffects.catch(e);
}
onCatch(e);
onThrowOrReject(e, true);
return sideEffects;
} finally {
if (!returnedPromise) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const spyFilesystemCalls = (
const fileDescriptorPathMap = new Map();
const fileRestoreMap = new Map();
const dirRestoreMap = new Map();
const onWriteFileDone = (fileUrl, stateBefore, stateAfter) => {
const onFileMutationDone = (stateBefore, stateAfter) => {
if (!stateAfter.found) {
// seems to be possible somehow
return;
Expand All @@ -58,39 +58,42 @@ export const spyFilesystemCalls = (
// - writing file for the 1st time
// - updating file content
// the important part is the file content in the end of the function execution
const reason =
!stateBefore.found && stateAfter.found
? "created"
: Buffer.compare(stateBefore.buffer, stateAfter.buffer)
? "content_modified"
: stateBefore.mtimeMs === stateAfter.mtimeMs
? ""
: "mtime_modified";
if (!reason) {
let reason;
if (!stateBefore.found && stateAfter.found) {
reason = "created";
} else if (Buffer.compare(stateBefore.buffer, stateAfter.buffer)) {
reason = "content_modified";
} else if (stateBefore.mtimeMs !== stateAfter.mtimeMs) {
reason = "mtime_modified";
} else if (stateBefore.url !== stateAfter.url) {
reason = "moved";
} else {
// file is exactly the same
// function did not have any effect on the file
return;
}
const action = getAction(fileUrl);
const beforeUrl = stateBefore.url;
const afterUrl = stateAfter.url;
const action = getAction(beforeUrl);
const shouldCompare =
action === "compare" ||
action === "compare_presence_only" ||
action === true;
if (action === "undo" || shouldCompare) {
if (undoFilesystemSideEffects && !fileRestoreMap.has(fileUrl)) {
if (undoFilesystemSideEffects && !fileRestoreMap.has(beforeUrl)) {
if (stateBefore.found) {
fileRestoreMap.set(fileUrl, () => {
writeFileSync(fileUrl, stateBefore.buffer);
fileRestoreMap.set(beforeUrl, () => {
writeFileSync(beforeUrl, stateBefore.buffer);
});
} else {
fileRestoreMap.set(fileUrl, () => {
removeFileSync(fileUrl, { allowUseless: true });
fileRestoreMap.set(beforeUrl, () => {
removeFileSync(beforeUrl, { allowUseless: true });
});
}
}
}
if (shouldCompare) {
onWriteFile(fileUrl, stateAfter.buffer, reason);
onWriteFile(afterUrl, stateAfter.buffer, reason);
}
// "ignore", false, anything else
};
Expand Down Expand Up @@ -121,9 +124,9 @@ export const spyFilesystemCalls = (
};
const restoreCallbackSet = new Set();

const getFileStateWithinHook = (fileUrl) => {
const getFileStateWithinHook = (filePath) => {
return disableHooksWhileCalling(
() => getFileState(fileUrl),
() => getFileState(filePath),
[openHook, closeHook],
);
};
Expand Down Expand Up @@ -208,14 +211,13 @@ export const spyFilesystemCalls = (
fileDescriptorPathMap.delete(fileDescriptor);
return;
}
const fileUrl = pathToFileURL(filePath);
if (buffer) {
onReadFile(String(fileUrl));
onReadFile(filePath);
}
fileDescriptorPathMap.delete(fileDescriptor);
filesystemStateInfoMap.delete(filePath);
const stateAfter = getFileStateWithinHook(fileUrl);
onWriteFileDone(String(fileUrl), stateBefore, stateAfter);
const stateAfter = getFileStateWithinHook(filePath);
onFileMutationDone(stateBefore, stateAfter);
},
};
},
Expand All @@ -225,12 +227,11 @@ export const spyFilesystemCalls = (
_internalFs,
"writeFileUtf8",
(filePath) => {
const fileUrl = pathToFileURL(filePath);
const stateBefore = getFileStateWithinHook(fileUrl);
const stateBefore = getFileStateWithinHook(filePath);
return {
return: () => {
const stateAfter = getFileStateWithinHook(fileUrl);
onWriteFileDone(String(fileUrl), stateBefore, stateAfter);
const stateAfter = getFileStateWithinHook(filePath);
onFileMutationDone(stateBefore, stateAfter);
},
};
},
Expand All @@ -242,12 +243,44 @@ export const spyFilesystemCalls = (
},
};
});
const copyFileHook = hookIntoMethod(
_internalFs,
"copyFile",
(fromPath, toPath) => {
const stateBefore = getFileStateWithinHook(fromPath);
return {
return: () => {
const stateAfter = getFileStateWithinHook(toPath);
onFileMutationDone(stateBefore, stateAfter);
},
};
},
{ execute: METHOD_EXECUTION_NODE_CALLBACK },
);
const renameHook = hookIntoMethod(
_internalFs,
"rename",
(fromPath, toPath) => {
const stateBefore = getFileStateWithinHook(fromPath);
return {
return: () => {
const stateAfter = getFileStateWithinHook(toPath);
onFileMutationDone(stateBefore, stateAfter);
},
};
},
{
execute: METHOD_EXECUTION_NODE_CALLBACK,
},
);
restoreCallbackSet.add(() => {
mkdirHook.remove();
openHook.remove();
closeHook.remove();
writeFileUtf8Hook.remove();
unlinkHook.remove();
copyFileHook.remove();
renameHook.remove();
});
return {
restore: () => {
Expand Down Expand Up @@ -275,18 +308,21 @@ export const spyFilesystemCalls = (
};
};

const getFileState = (file) => {
const getFileState = (filePath) => {
const fileUrl = pathToFileURL(filePath);
try {
const fileBuffer = readFileSync(file);
const { mtimeMs } = statSync(file);
const fileBuffer = readFileSync(fileUrl);
const { mtimeMs } = statSync(fileUrl);
return {
url: String(fileUrl),
found: true,
mtimeMs,
buffer: fileBuffer,
};
} catch (e) {
if (e.code === "ENOENT") {
return {
url: String(fileUrl),
found: false,
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
Loading

0 comments on commit 5c7a5f0

Please sign in to comment.