Skip to content

Commit

Permalink
feat: 增加在线签名功能 (#51)
Browse files Browse the repository at this point in the history
Co-authored-by: 72161674 <[email protected]>
  • Loading branch information
xingfujiang and 72161674 authored Sep 13, 2023
1 parent 790efe1 commit 3a1915a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 3 deletions.
83 changes: 83 additions & 0 deletions packages/hap-packager/src/plugins/sign-online-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import http from 'http'
import fs from 'fs'
import path from 'path'
import { URL } from 'url'
import { colorconsole } from '@hap-toolkit/shared-utils'

function SignOnlinePlugin(options) {
this.options = options
}

SignOnlinePlugin.prototype.apply = function (compiler) {
const options = this.options

compiler.hooks.done.tapAsync('SignOnlinePlugin', function (stats, cb) {
const requestPath = options.request
if (!requestPath || typeof requestPath !== 'string') {
colorconsole.error(`### App Server ### 请求线上接口无效,请检查`)
cb()
return
}
const ext = options.signOnlineRpks ? 'rpks' : 'rpk'
const pkgName = options.name
const versionName = options.versionName
const noSignFile = `${pkgName}.nosign.${versionName}.${ext}`

const noSignFilePath = path.join(options.output, noSignFile)

const distFileName = `${pkgName}.${options.sign}.${versionName}.${ext}`
const distFilePath = path.join(options.output, distFileName)
const distFileStream = fs.createWriteStream(distFilePath)

let form = null

const file = fs.createReadStream(noSignFilePath)

if (options.formData && options.formData(file)) {
form = options.formData(file)
}

form && Object.assign(options.headers(), form.getHeaders())

const url = new URL(requestPath)
const param = {
host: url.hostname,
port: url.port,
path: url.pathname,
method: 'POST',
timeout: 10000,
headers: options.headers
}
const req = http
.request(param, (res) => {
colorconsole.log(`### App Loader ### 请求接口的状态码:${res.statusCode}`)
res.pipe(distFileStream, { end: false })
res.on('end', (data) => {
if (res.statusCode === 200) {
distFileStream.end()
colorconsole.log(
`### App Loader ### 请求线上签名成功,dist目录生成文件:${distFileName}`
)
cb()
}
})
})
.on('error', (err) => {
colorconsole.error(`### App Server ### 请求线上签名错误,错误信息: ${err.message} $`)
cb(err)
})
.on('timeout', function () {
colorconsole.warn(
`### App Server ### 请求线上签名网络超时,请检查网络与接口地址 ${
form
? ''
: '缺少FormData对象,请在配置文件quickapp.config.js里面定义的params函数返回FormData对象'
}`
)
req.abort()
})
form && form.pipe(req)
})
}

export default SignOnlinePlugin
2 changes: 1 addition & 1 deletion packages/hap-packager/src/subpackages/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function allocateResourceToPackages(files, base, fullPackage, subPackages, build

files.forEach((fileBuildPath) => {
const fileAbsPath = path.join(base, fileBuildPath)
const fileContentBuffer = fs.readFileSync(fileAbsPath.replace(/\\/g, '/'))
const fileContentBuffer = fs.readFileSync(fileAbsPath)
const fileContentDigest = calcDataDigest(fileContentBuffer)
// 资源基本信息
const resourceInfo = [fileBuildPath, fileContentBuffer, fileContentDigest]
Expand Down
17 changes: 17 additions & 0 deletions packages/hap-packager/src/webpack.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './plugins'
import { genPriorities, getBabelConfigJsPath } from './common/utils'
import { getSkeletonConfig } from './common/info'
import SignOnlinePlugin from './plugins/sign-online-plugin'

/**
* 配置关联
Expand Down Expand Up @@ -166,6 +167,22 @@ function postHook(webpackConf, defaultsOptions, quickappConfig = {}) {
})
)

if (compileOptionsObject['signOnline']) {
// 发送接口进行线上签名
webpackConf.plugins.push(
new SignOnlinePlugin({
signOnlineRpks: compileOptionsObject.signOnlineRpks,
sign: webpackConf.mode === 'development' ? 'debug' : 'release',
name: appPackageName,
versionName,
output: pathDist,
request: globalConfig.signOnLineConfig.signOnLine,
headers: globalConfig.signOnLineConfig.headers,
formData: globalConfig.signOnLineConfig.params
})
)
}

// 解决错误信息定位问题
if (compileOptionsObject.matchSourcemap) {
webpackConf.plugins.push(new SourcemapFixPlugin())
Expand Down
1 change: 1 addition & 0 deletions packages/hap-shared-utils/src/compilation-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const compileOptionsObject = {
*/
function mergeCompileOptionsObject(argopts) {
// TODO release memeory, use optimize-prop only
compileOptionsObject.devtool = argopts.devtool ? argopts.devtool : false
Object.assign(compileOptionsObject, argopts)
}

Expand Down
5 changes: 4 additions & 1 deletion packages/hap-shared-utils/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ export default {
// 代码风格规则
isSmartMode: false,
// 记录 watch 模式下哪些 .ux 文件对应的 .js 文件改变了
changedJS: {}
changedJS: {},
signOnLineConfig: {
signOnLine: ''
}
}
18 changes: 17 additions & 1 deletion packages/hap-toolkit/src/commands/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

import webpack from 'webpack'
import adbCommander from 'adb-commander'
import { setCustomConfig, colorconsole } from '@hap-toolkit/shared-utils'
import {
setCustomConfig,
colorconsole,
globalConfig,
compileOptionsMeta
} from '@hap-toolkit/shared-utils'
import genWebpackConf from '../gen-webpack-conf'
import { summaryErrors, summaryWarnings } from './utils'

Expand Down Expand Up @@ -70,6 +75,17 @@ export function compile(platform, mode, watch, options = {}) {

const webpackMode = mode === 'prod' ? 'production' : 'development'

if (options.disableSignOnline) {
options['signOnline'] = false
} else if (
globalConfig.signOnLineConfig.signOnLine.match(
/^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/
)
) {
options['signOnline'] = true
options['signMode'] = compileOptionsMeta.signModeEnum.NULL
}

try {
const webpackConfig = await genWebpackConf(options, webpackMode)

Expand Down

0 comments on commit 3a1915a

Please sign in to comment.