Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一些问题修复和代码优化 #640

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
let HttpUrl = Packages.okhttp3.HttpUrl;
let MediaType = Packages.okhttp3.MediaType;
let Headers = Packages.okhttp3.Headers;
let InterruptedIOException = java.io.InterruptedIOException

const stream = require("stream");
let EventTarget = require("./EventTarget.js");
Expand All @@ -24,6 +25,7 @@
///////XMLHttpRequest对象
let XMLHttpRequest = function () {
this.responseType = 'text';
this.timeout = 0;
setReadonlyAttribute(this, 'upload', {})
setReadonlyAttribute(this, 'readyState', 0);
setReadonlyAttribute(this, 'status', 0);
Expand Down Expand Up @@ -105,12 +107,7 @@
XMLHttpRequest.prototype.send = function (body) {
atl.addTask();
const xhr = this;
const {
url,
method,
ac,
headers,
} = this._requestData
const { url, method, ac, headers, } = this._requestData
const responseType = xhr.responseType;

const builder = new Request.Builder();
Expand All @@ -120,12 +117,20 @@
let reqBody = parserReqBody(xhr, body);

let request = builder.url(url.build()).method(method, reqBody).build();
let call = XMLHttpRequest._okHttpClient.newCall(request);
let client = XMLHttpRequest._okHttpClient;
if (xhr.timeout > 0) {
client = new OkHttpClient.Builder().callTimeout(xhr.timeout, java.util.concurrent.TimeUnit.MILLISECONDS)
.followRedirects(true).build();
}
let call = client.newCall(request);
setReadonlyAttribute(xhr, '_call', call, false);
call.enqueue({
onFailure(call, e) {
xhr._setReadyState(4);
setReadonlyAttribute(xhr, 'statusText', e.message)
if (e instanceof InterruptedIOException) {
xhr.dispatchEvent(new Event('timeout'));
}
xhr.dispatchEvent(new Event('error'))
xhr.dispatchEvent(new Event('loadend'))
atl.removeTask()
Expand Down
15 changes: 7 additions & 8 deletions autojs/src/main/assets/modules/npm/process.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
const {
EventEmitter
} = require("events");
const es = require("event-stream");

const { EventEmitter } = require("events");
// const es = require("event-stream");

const process = new EventEmitter();

//events.on('exit', process.emit.bind(process, 'exit'))

/*
process.stdout = es.map(function(data, callback) {
console.log(data);
return callback(null, data)
})
/*

process.stdout.end = function() {};
process.stdout.destroy = function(){};
*/

process.stderr = es.map(function(data, callback) {
console.error(data);
return callback(null, data)
})

*/
process.nextTick = setImmediate;
process.env = {}
//log(process);
Expand Down
45 changes: 28 additions & 17 deletions autojs/src/main/assets/modules/npm/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,42 @@ importClass(java.io.OutputStream);
importClass(java.io.InputStream);
importClass(java.io.FileOutputStream);
importClass(java.io.FileInputStream);
importClass(java.util.concurrent.CompletableFuture);

const stream = require("stream-browserify");
let mainThread;

function addTask(fn, callback) {
CompletableFuture.runAsync(function() {
function addTask(th, fn, callback) {
th.setImmediate(function () {
try {
const data = fn();
return setImmediate(callback, null, data)
return mainThread.setImmediate(callback, null, data)
} catch (e) {
//console.error(e)
return setImmediate(callback, e)
return mainThread.setImmediate(callback, e)
}
})
}
function createThread() {
return threads.start(() => {
setInterval(() => { }, 1000)
})
}

stream.fromInputStream = function(inp, options) {
stream.fromInputStream = function (inp, options) {
if (!mainThread) mainThread = threads.currentThread();
if (!(inp instanceof InputStream)) throw TypeError('需要InputStream流');
options = options || {};
options.highWaterMark = options.highWaterMark || (1024 * 64);
options.autoDestroy = true;
options.emitClose = true;
options.destroy = function(e, cb) {
options.destroy = function (e, cb) {
this._inp.close();
runtime.loopers.removeAsyncTask(this._task)
this._thread.interrupt();
return cb(e)
}
options.read = function(size) {
addTask(() => {
options.read = function (size) {
addTask(this._thread, () => {
const buffer = Buffer.alloc(size);
const bytes = buffer.getBytes();
const i = this._inp.read(bytes);
Expand All @@ -49,30 +56,33 @@ stream.fromInputStream = function(inp, options) {
}
const readable = new stream.Readable(options)
setReadonlyAttribute(readable, '_inp', inp, false)
setReadonlyAttribute(readable, '_thread', createThread(), false)
setReadonlyAttribute(readable, '_task', runtime.loopers.createAndAddAsyncTask('stream'), false)
return readable;
}

stream.fromOutputStream = function(out, options) {
stream.fromOutputStream = function (out, options) {
if (!mainThread) mainThread = threads.currentThread();
if (!(out instanceof OutputStream)) throw TypeError('需要OutputStream流');
options = options || {};
options.highWaterMark = options.highWaterMark || (1024 * 64);
options.autoDestroy = true;
options.emitClose = true;
options.destroy = function(e, cb) {
options.destroy = function (e, cb) {
this._out.close();
runtime.loopers.removeAsyncTask(this._task)
this._thread.interrupt();
return cb(e)
}
options.final = function(callback) {
addTask(() => {
options.final = function (callback) {
addTask(this._thread, () => {
this._out.flush();
}, (err) => {
callback();
})
}
options.write = function(chunk, encoding, callback) {
addTask(() => {
options.write = function (chunk, encoding, callback) {
addTask(this._thread, () => {
const buffer = (chunk instanceof Buffer) ? chunk : Buffer.from(chunk, encoding);
const bytes = buffer.getBytes()
this._out.write(bytes, 0, buffer.length);
Expand All @@ -83,16 +93,17 @@ stream.fromOutputStream = function(out, options) {
}
const writable = new stream.Writable(options)
setReadonlyAttribute(writable, '_out', out, false);
setReadonlyAttribute(writable, '_thread', createThread(), false)
setReadonlyAttribute(writable, '_task', runtime.loopers.createAndAddAsyncTask('stream'), false)
return writable;
}
stream.createFileReadStream = function(path, bufferSize) {
stream.createFileReadStream = function (path, bufferSize) {
return stream.fromInputStream(new FileInputStream(
files.path(path)), {
highWaterMark: bufferSize || (256 * 1024)
})
}
stream.createFileWriteStream = function(path, bufferSize) {
stream.createFileWriteStream = function (path, bufferSize) {
return stream.fromOutputStream(new FileOutputStream(
files.path(path)), {
highWaterMark: bufferSize || (256 * 1024)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ class Loopers(val runtime: ScriptRuntime) {

@Deprecated("使用AsyncTask代替")
fun waitWhenIdle(b: Boolean) {
waitWhenIdle = b
(Thread.currentThread() as? TimerThread)?.let {
it.loopers?.createAndAddAsyncTask("events")
}?: createAndAddAsyncTask("events")
}

fun recycle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.stardust.autojs.core.web.JsBridge

open class JsWebView : WebView {
//val events = EventEmitter()

@RequiresApi(Build.VERSION_CODES.M)
val jsBridge = JsBridge(this)

init {
Expand All @@ -21,7 +21,7 @@ open class JsWebView : WebView {
settings.javaScriptCanOpenWindowsAutomatically = true
settings.domStorageEnabled = true
settings.displayZoomControls = false
webViewClient = JsBridge.SuperWebViewClient()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) webViewClient = JsBridge.SuperWebViewClient()
}

constructor(context: Context) : super(context)
Expand All @@ -32,18 +32,7 @@ open class JsWebView : WebView {
defStyleAttr
)
@RequiresApi(Build.VERSION_CODES.M)
fun injectionJsBridge() {
val context = this.context
val js: String = try {
val inputStream = context.assets.open(JsBridge.sdkPath)
val available = inputStream.available()
val byteArray = ByteArray(available)
inputStream.read(byteArray)
inputStream.close()
String(byteArray)
} catch (e: Exception) {
""
}
jsBridge.evaluateJavascript(js);
fun injectionJsBridge(){
JsBridge.injectionJsBridge(this)
}
}
Loading
Loading