Skip to content

Commit

Permalink
Upstream some REPL changes (#9328)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored Nov 7, 2023
1 parent 96185d0 commit 817c778
Show file tree
Hide file tree
Showing 33 changed files with 509 additions and 112 deletions.
23 changes: 14 additions & 9 deletions crates/node-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ pub extern "C" fn napi_wasm_malloc(size: usize) -> *mut u8 {

#[cfg(target_arch = "wasm32")]
mod wasm {
use core::num::NonZeroU32;
use getrandom::register_custom_getrandom;
use getrandom::Error;

// TODO
pub fn always_fail(buf: &mut [u8]) -> Result<(), Error> {
let code = NonZeroU32::new(Error::CUSTOM_START + 42).unwrap();
Err(Error::from(code))
use napi_derive::napi;

#[link(wasm_import_module = "env")]
extern "C" {
fn log(ptr: *const u8, len: usize);
}

register_custom_getrandom!(always_fail);
#[napi]
pub fn init_panic_hook() {
std::panic::set_hook(Box::new(|p| {
let s = p.to_string();
unsafe {
log(s.as_ptr(), s.len());
}
}));
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build-ts": "lerna run build-ts && lerna run check-ts",
"build-native": "node scripts/build-native.js",
"build-native-release": "node scripts/build-native.js --release",
"build-native-wasm": "node scripts/build-native.js --release --wasm",
"clean-test": "rimraf packages/core/integration-tests/.parcel-cache && rimraf packages/core/integration-tests/dist",
"clean": "yarn clean-test && lerna clean --yes && lerna exec -- rimraf ./lib && yarn",
"format": "prettier --write \"./packages/*/*/{src,bin,test}/**/*.{js,json,md}\" && cargo fmt --all",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dotenv": "^7.0.0",
"dotenv-expand": "^5.1.0",
"json5": "^2.2.0",
"msgpackr": "^1.5.4",
"msgpackr": "^1.9.9",
"nullthrows": "^1.1.1",
"semver": "^7.5.2"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/core/core/src/Parcel.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ export function createWorkerFarm(
): WorkerFarm {
return new WorkerFarm({
...options,
workerPath: require.resolve('./worker'),
// $FlowFixMe
workerPath: process.browser
? '@parcel/core/src/worker.js'
: require.resolve('./worker'),
});
}
4 changes: 3 additions & 1 deletion packages/core/core/src/requests/PathRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ export class ResolverRunner {
diagnostic.codeFrames = [
{
filePath,
code: await this.options.inputFS.readFile(filePath, 'utf8'),
code: await this.options.inputFS
.readFile(filePath, 'utf8')
.catch(() => ''),
codeHighlights: dependency.loc
? [convertSourceLocationToHighlight(dependency.loc)]
: [],
Expand Down
23 changes: 18 additions & 5 deletions packages/core/fs/src/MemoryFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,13 +918,26 @@ function makeShared(contents: Buffer | string): Buffer {
return contents;
}

let length = Buffer.byteLength(contents);
let contentsBuffer: Buffer | string = contents;
// $FlowFixMe
if (process.browser) {
// For the polyfilled buffer module, it's faster to always convert once so that the subsequent
// operations are fast (.byteLength and using .set instead of .write)
contentsBuffer =
contentsBuffer instanceof Buffer
? contentsBuffer
: Buffer.from(contentsBuffer);
}

let length = Buffer.byteLength(contentsBuffer);
let shared = new SharedBuffer(length);
let buffer = Buffer.from(shared);
if (typeof contents === 'string') {
buffer.write(contents);
} else {
buffer.set(contents);
if (length > 0) {
if (typeof contentsBuffer === 'string') {
buffer.write(contentsBuffer);
} else {
buffer.set(contentsBuffer);
}
}

return buffer;
Expand Down
6 changes: 4 additions & 2 deletions packages/core/package-manager/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {promisify} from 'util';
export const exec: (
command: string,
options?: child_process$execOpts,
) => Promise<{|stdout: string | Buffer, stderr: string | Buffer|}> =
promisify(_exec);
) => Promise<{|stdout: string | Buffer, stderr: string | Buffer|}> = _exec
? promisify(_exec)
: // _exec is undefined in browser builds
_exec;

export function npmSpecifierFromModuleRequest(
moduleRequest: ModuleRequest,
Expand Down
68 changes: 47 additions & 21 deletions packages/core/rust/browser.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
/* eslint-disable no-undef */
/* global WebAssembly, crypto */

const {Environment, napi} = require('napi-wasm');

module.exports.Resolver = undefined;
let env;
module.exports.init = async function init(input) {
if (module.exports.Resolver == null) {
// input = input ?? new URL('parcel_resolver_node.wasm', import.meta.url);
// const {instance} = await WebAssembly.instantiateStreaming(fetch(input), {
// env: napi,
// });

input =
input ?? require('path').join(__dirname, 'parcel_node_bindings.wasm');
const {instance} = await WebAssembly.instantiate(
require('fs').readFileSync(input),
{
env: napi,
if (env) return;

input = input ?? new URL('parcel_node_bindings.wasm', import.meta.url);
const {instance} = await WebAssembly.instantiateStreaming(fetch(input), {
env: {
...napi,
__getrandom_custom: (ptr, len) => {
let buf = env.memory.subarray(ptr, ptr + len);
crypto.getRandomValues(buf);
},
);
log: (ptr, len) => {
// eslint-disable-next-line no-console
console.log(env.getString(ptr, len));
},
},
});

// input =
// input ?? require('path').join(__dirname, 'parcel_node_bindings.wasm');
// const {instance} = await WebAssembly.instantiate(
// require('fs').readFileSync(input),
// {
// env: napi,
// },
// );

for (let key in instance.exports) {
if (key.startsWith('__napi_register__')) {
instance.exports[key]();
}
for (let key in instance.exports) {
if (key.startsWith('__napi_register__')) {
instance.exports[key]();
}
}

env = new Environment(instance);

let env = new Environment(instance);
Object.assign(module.exports, env.exports);
for (let key in env.exports) {
if (key !== 'transform') {
module.exports[key] = env.exports[key];
}
}
module.exports.transform = function (config) {
let result = env.exports.transform(config);
return {
...result,
// Hydrate Uint8Array into Buffer
code: Buffer.from(result.code),
};
};

env.exports.initPanicHook();
};
4 changes: 2 additions & 2 deletions packages/core/rust/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"node": ">= 12.0.0"
},
"files": [
"native-browser.js",
"browser.js",
"index.d.ts",
"index.js",
"index.js.flow",
Expand All @@ -37,6 +37,6 @@
"build": "napi build --platform --cargo-cwd ../../../crates/node-bindings",
"build-release": "napi build --platform --release --cargo-cwd ../../../crates/node-bindings",
"wasm:build": "cargo build -p parcel-node-bindings --target wasm32-unknown-unknown && cp ../../../target/wasm32-unknown-unknown/debug/parcel_node_bindings.wasm .",
"wasm:build-release": "CARGO_PROFILE_RELEASE_LTO=true cargo build --target wasm32-unknown-unknown --release && wasm-opt --strip-debug -O ../../target/wasm32-unknown-unknown/release/parcel_node_bindings.wasm -o ../../packages/core/rust/parcel_node_bindings.wasm"
"wasm:build-release": "CARGO_PROFILE_RELEASE_LTO=true cargo build -p parcel-node-bindings --target wasm32-unknown-unknown --release && wasm-opt --strip-debug -O ../../../target/wasm32-unknown-unknown/release/parcel_node_bindings.wasm -o parcel_node_bindings.wasm"
}
}
4 changes: 3 additions & 1 deletion packages/core/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@
"nullthrows": "^1.1.1",
"open": "^7.0.3",
"random-int": "^1.0.0",
"snarkdown": "^2.0.0",
"strip-ansi": "^6.0.0",
"terminal-link": "^2.1.1"
},
"browser": {
"./src/generateCertificate.js": false,
"./src/http-server.js": false,
"./src/openInBrowser.js": false
"./src/openInBrowser.js": false,
"@parcel/markdown-ansi": false
}
}
34 changes: 28 additions & 6 deletions packages/core/utils/src/prettyDiagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import type {Diagnostic} from '@parcel/diagnostic';
import type {PluginOptions} from '@parcel/types';

import formatCodeFrame from '@parcel/codeframe';
import mdAnsi from '@parcel/markdown-ansi';
import chalk from 'chalk';
import _mdAnsi from '@parcel/markdown-ansi';
import _chalk from 'chalk';
import path from 'path';
// $FlowFixMe
import terminalLink from 'terminal-link';
import _terminalLink from 'terminal-link';

/* eslint-disable import/no-extraneous-dependencies */
// $FlowFixMe
import snarkdown from 'snarkdown';
/* eslint-enable import/no-extraneous-dependencies */

export type FormattedCodeFrame = {|
location: string,
Expand All @@ -29,6 +34,7 @@ export default async function prettyDiagnostic(
diagnostic: Diagnostic,
options?: PluginOptions,
terminalWidth?: number,
format: 'ansi' | 'html' = 'ansi',
): Promise<AnsiDiagnosticResult> {
let {
origin,
Expand All @@ -40,10 +46,26 @@ export default async function prettyDiagnostic(
documentationURL,
} = diagnostic;

const md = format === 'ansi' ? _mdAnsi : snarkdown;
const terminalLink =
format === 'ansi'
? _terminalLink
: // eslint-disable-next-line no-unused-vars
(text, url, _) => `<a href="${url}">${text}</a>`;
const chalk =
format === 'ansi'
? _chalk
: {
gray: {
underline: v =>
`<span style="color: grey; text-decoration: underline;">${v}</span>`,
},
};

let result = {
message:
mdAnsi(`**${origin ?? 'unknown'}**: `) +
(skipFormatting ? message : mdAnsi(message)),
md(`**${origin ?? 'unknown'}**: `) +
(skipFormatting ? message : md(message)),
stack: '',
codeframe: '',
frames: [],
Expand Down Expand Up @@ -104,7 +126,7 @@ export default async function prettyDiagnostic(

if (Array.isArray(hints) && hints.length) {
result.hints = hints.map(h => {
return mdAnsi(h);
return md(h);
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@parcel/core": "^2.10.2"
},
"browser": {
"./src/cpuCount.js": false,
"./src/process/ProcessWorker.js": false,
"./src/threads/ThreadsWorker.js": false
"./src/threads/ThreadsWorker.js": false,
"./src/core-worker.js": "./src/core-worker.browser.js"
}
}
29 changes: 16 additions & 13 deletions packages/core/workers/src/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,22 @@ export default class Worker extends EventEmitter {
}

async fork(forkModule: FilePath) {
let filteredArgs = process.execArgv.filter(
v => !/^--(debug|inspect|no-opt|max-old-space-size=)/.test(v),
);

for (let i = 0; i < filteredArgs.length; i++) {
let arg = filteredArgs[i];
let isArgWithParam =
((arg === '-r' || arg === '--require') &&
filteredArgs[i + 1] === '@parcel/register') ||
arg === '--title';
if (isArgWithParam) {
filteredArgs.splice(i, 2);
i--;
let filteredArgs = [];
if (process.execArgv) {
filteredArgs = process.execArgv.filter(
v => !/^--(debug|inspect|no-opt|max-old-space-size=)/.test(v),
);

for (let i = 0; i < filteredArgs.length; i++) {
let arg = filteredArgs[i];
let isArgWithParam =
((arg === '-r' || arg === '--require') &&
filteredArgs[i + 1] === '@parcel/register') ||
arg === '--title';
if (isArgWithParam) {
filteredArgs.splice(i, 2);
i--;
}
}
}

Expand Down
Loading

0 comments on commit 817c778

Please sign in to comment.