-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
078f27b
commit 40f6126
Showing
10 changed files
with
318 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ldd --version | ||
curl https://sh.rustup.rs -sSf | sh -s -- -y | ||
source "$HOME/.cargo/env" | ||
npm run build-release | ||
node -e "console.log(require('./index.node'))" | ||
python3 -m pip install boto3 | ||
python3 .github/workflows/deploy.py index.node hexo-optimize/bin/nodejs/$(node -e "console.log(require('./package.json').version);")/linux__x64.node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
name: Build and publish Node.js package | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [macos-11.0, windows-2019] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Get version | ||
id: version | ||
shell: bash | ||
run: echo ::set-output name=VERSION::"$([[ "$GITHUB_REF" == refs/tags/v* ]] && echo ${GITHUB_REF#refs/tags/v} || echo '0.0.0')" | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v3 | ||
|
||
- name: Set up Rust | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
profile: minimal | ||
default: true | ||
|
||
- name: Build native module | ||
id: module | ||
shell: bash | ||
run: | | ||
npm install | ||
npm run build-release | ||
echo ::set-output name=BINARY_NAME::"$(node -e 'console.log([process.platform, process.arch].join("__"))')" | ||
- name: Upload to R2 | ||
run: | | ||
pip3 install boto3 | ||
python3 .github/workflows/deploy.py index.node hexo-optimize/bin/nodejs/${{ steps.version.outputs.VERSION }}/${{ steps.module.outputs.BINARY_NAME }}.node | ||
env: | ||
ACCOUNT_ID: ${{ secrets.ACCOUNT_ID }} | ||
SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }} | ||
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
import sys | ||
from hashlib import sha256 | ||
import boto3 | ||
from botocore.client import Config | ||
from botocore.exceptions import ClientError | ||
|
||
# https://github.com/cloudflare/cloudflared/blob/135c8e6d13663d2aa2d3c9169cde0cfc1e6e2062/release_pkgs.py#L36 | ||
def upload_pkg_to_r2(account_id, secret_access_key, access_key_id, bucket_name, filename, upload_file_path): | ||
endpoint_url = f"https://{account_id}.r2.cloudflarestorage.com" | ||
|
||
config = Config( | ||
region_name="auto", | ||
s3={ | ||
"addressing_style": "path", | ||
} | ||
) | ||
|
||
r2 = boto3.client( | ||
"s3", | ||
endpoint_url=endpoint_url, | ||
aws_access_key_id=access_key_id, | ||
aws_secret_access_key=secret_access_key, | ||
config=config, | ||
) | ||
|
||
print(f"uploading asset: {filename} to {upload_file_path} in bucket {bucket_name}...") | ||
try: | ||
r2.upload_file(filename, bucket_name, upload_file_path) | ||
except ClientError as e: | ||
raise e | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 3: | ||
print("Usage: python3 deploy.py <filename> <upload_file_path>") | ||
exit(1) | ||
account_id = os.environ["ACCOUNT_ID"] | ||
secret_access_key = os.environ["SECRET_ACCESS_KEY"] | ||
access_key_id = os.environ["ACCESS_KEY_ID"] | ||
bucket_name = "archive" | ||
filename = sys.argv[1] | ||
upload_file_path = sys.argv[2] | ||
|
||
upload_pkg_to_r2(account_id, secret_access_key, access_key_id, bucket_name, filename, upload_file_path) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
node_modules/ | ||
package-lock.json | ||
.DS_Store | ||
tmp/ | ||
tmp/ | ||
Cargo.lock | ||
index.node | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "html-minify" | ||
publish = false | ||
version = "0.10.3" | ||
description = "Node.js bindings for html-minify" | ||
authors = ["Wilson Lin <npm@wilsonl.in>"] | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
minify-html = "0.10.3" | ||
|
||
[dependencies.neon] | ||
version = "0.10" | ||
default-features = false | ||
features = ["napi-1"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/*! | ||
MIT License | ||
Copyright (c) 2020 Wilson Lin | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
https://github.com/wilsonzlin/minify-html/blob/master/nodejs/postinstall.js | ||
*/ | ||
|
||
const fs = require('fs'); | ||
const https = require('https'); | ||
const path = require('path'); | ||
const pkg = require('./package.json'); | ||
const cp = require('child_process'); | ||
|
||
const MAX_DOWNLOAD_ATTEMPTS = 4; | ||
|
||
const binaryName = [process.platform, process.arch].join('__'); | ||
const binaryPath = path.join(__dirname, 'index.node'); | ||
|
||
const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
class StatusError extends Error { | ||
constructor(status) { | ||
super(`Bad status of ${status}`); | ||
this.status = status; | ||
} | ||
} | ||
|
||
const fetch = url => | ||
new Promise((resolve, reject) => { | ||
const stream = https.get(url, resp => { | ||
if (!resp.statusCode || resp.statusCode < 200 || resp.statusCode > 299) { | ||
return reject(new StatusError(resp.statusCode)); | ||
} | ||
const parts = []; | ||
resp.on('data', chunk => parts.push(chunk)); | ||
resp.on('end', () => resolve(Buffer.concat(parts))); | ||
}); | ||
stream.on('error', reject); | ||
}); | ||
|
||
const downloadNativeBinary = async() => { | ||
for (let attempt = 0; ; attempt++) { | ||
let binary; | ||
try { | ||
binary = await fetch( | ||
`https://archive.zsq.im/hexo-optimize/bin/nodejs/${pkg.version}/${binaryName}.node` | ||
); | ||
} catch (e) { | ||
if ( | ||
e instanceof StatusError | ||
&& e.status !== 404 | ||
&& attempt < MAX_DOWNLOAD_ATTEMPTS | ||
) { | ||
await wait((Math.random() * 2500) + 500); | ||
continue; | ||
} | ||
throw e; | ||
} | ||
|
||
fs.writeFileSync(binaryPath, binary); | ||
break; | ||
} | ||
}; | ||
|
||
if ( | ||
!fs.existsSync(path.join(__dirname, '.no-postinstall')) | ||
&& !fs.existsSync(binaryPath) | ||
) { | ||
downloadNativeBinary().then( | ||
() => console.log(`Downloaded ${pkg.name}`), | ||
err => { | ||
console.error( | ||
`Failed to download ${pkg.name}, will build from source: ${err}` | ||
); | ||
const out = cp.spawnSync('npm', ['run', 'build-release'], { | ||
cwd: __dirname, | ||
stdio: ['ignore', 'inherit', 'inherit'] | ||
}); | ||
process.exitCode = out.exitCode; | ||
if (out.error) { | ||
throw out.error; | ||
} | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/*! | ||
MIT License | ||
Copyright (c) 2020 Wilson Lin | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
https://github.com/wilsonzlin/minify-html/blob/master/nodejs/postinstall.js | ||
*/ | ||
|
||
use neon::prelude::*; | ||
use neon::types::buffer::TypedArray; | ||
|
||
fn minify(mut cx: FunctionContext) -> JsResult<JsBuffer> { | ||
let src = cx.argument::<JsBuffer>(0)?; | ||
let opt = cx.argument::<JsObject>(1)?; | ||
let cfg = minify_html::Cfg { | ||
do_not_minify_doctype: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "do_not_minify_doctype")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
ensure_spec_compliant_unquoted_attribute_values: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "ensure_spec_compliant_unquoted_attribute_values")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
keep_closing_tags: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "keep_closing_tags")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
keep_html_and_head_opening_tags: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "keep_html_and_head_opening_tags")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
keep_spaces_between_attributes: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "keep_spaces_between_attributes")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
keep_comments: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "keep_comments")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
minify_css: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "minify_css")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
minify_js: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "minify_js")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
remove_bangs: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "remove_bangs")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
remove_processing_instructions: opt | ||
.get_opt::<JsBoolean, _, _>(&mut cx, "remove_processing_instructions")? | ||
.map(|v| v.value(&mut cx)) | ||
.unwrap_or(false), | ||
}; | ||
let out = minify_html::minify(src.as_slice(&mut cx), &cfg); | ||
Ok(JsBuffer::external(&mut cx, out)) | ||
} | ||
|
||
#[neon::main] | ||
fn main(mut cx: ModuleContext) -> NeonResult<()> { | ||
cx.export_function("minify", minify)?; | ||
Ok(()) | ||
} |