Improve Node.js install script

This commit is contained in:
Wilson Lin 2020-01-05 13:55:20 +11:00
parent 74e6352900
commit 5e05cbcff8
4 changed files with 43 additions and 22 deletions

View File

@ -1,7 +1,25 @@
const childProcess = require('child_process');
const fs = require('fs');
const path = require('path');
const request = require('request');
const build = () => {
console.log(`Building from source...`);
const {status, signal, error} = childProcess.spawnSync(`neon`, [`build`, `--release`], {
stdio: ['ignore', 'inherit', 'inherit'],
encoding: 'utf8',
});
if (error) {
throw error;
}
if (signal) {
throw new Error(`Build exited with signal ${signal}`);
}
if (status !== 0) {
throw new Error(`Build exited with status ${status}`);
}
};
const binaryPath = path.join(__dirname, "native", "index.node");
const binaryName = [
require('./package').version,
@ -32,17 +50,22 @@ const binaryName = [
).join('');
const binaryUrl = `https://wilsonl.in/hyperbuild/bin/${binaryName}`;
console.log(`Fetching ${binaryUrl}...`);
request(binaryUrl)
.on('response', res => {
if (res.statusCode !== 200) {
console.error(`Failed to download prebuilt native module with status ${res.statusCode}, will build from source`);
process.exit(1);
}
})
.on('error', err => {
console.error(err);
console.error(`Could not download prebuilt native module, building from source...`);
process.exit(1);
})
.pipe(fs.createWriteStream(binaryPath));
if (process.env.HYPERBUILD_NODEJS_SKIP_BIN_DOWNLOAD) {
console.log(`Skipping download of prebuilt native module binary`);
build();
} else {
console.log(`Fetching ${binaryUrl}...`);
request(binaryUrl)
.on('response', res => {
if (res.statusCode !== 200) {
console.error(`Failed to download prebuilt native module with status ${res.statusCode}`);
build();
}
})
.on('error', err => {
console.error(err);
console.error(`Could not download prebuilt native module`);
build();
})
.pipe(fs.createWriteStream(binaryPath));
}

View File

@ -25,7 +25,7 @@
"request": "^2.88.0"
},
"scripts": {
"install": "node install.js || neon build --release",
"install": "node install.js",
"prepack": "cp ../README.md .",
"postpack": "rm README.md"
},

View File

@ -279,12 +279,13 @@ pub fn process_attr_value(proc: &mut Processor, should_collapse_and_trim_ws: boo
};
if should_encode {
let encoded = ENCODED[&c];
optimal_write_next -= encoded.len();
proc.code[optimal_write_next + 1..optimal_write_next + 1 + encoded.len()].copy_from_slice(encoded);
// Make extra room for entity (only have room for 1 char currently).
optimal_write_next -= encoded.len() - 1;
proc.code[optimal_write_next..optimal_write_next + encoded.len()].copy_from_slice(encoded);
} else {
proc.code[optimal_write_next] = c;
optimal_write_next -= 1;
};
optimal_write_next -= 1;
// Break before decrementing to prevent underflow.
if is_first {

View File

@ -1,5 +1,4 @@
use crate::err::ProcessingResult;
use crate::pattern::TrieNode;
use crate::proc::{Processor, ProcessorRange};
use crate::spec::codepoint::is_whitespace;
use crate::spec::tag::content::CONTENT_TAGS;
@ -24,8 +23,6 @@ enum ContentType {
Text,
}
include!(concat!(env!("OUT_DIR"), "/gen_trie_CONTENT_TYPE.rs"));
impl ContentType {
fn is_comment_bang_opening_tag(&self) -> bool {
match self {
@ -35,7 +32,7 @@ impl ContentType {
}
fn peek(proc: &mut Processor) -> ContentType {
// Manually write out matching for fast performance as this is hot spot.
// Manually write out matching for fast performance as this is hot spot; don't use generated trie.
match proc.peek_eof() {
None => ContentType::End,
Some(b'<') => match proc.peek_offset_eof(1) {