[rs-commit] r30 - /redwax-signtext/trunk/src/macos/experimental/firefox/content.js

rs-commit at redwax.eu rs-commit at redwax.eu
Sun Apr 17 13:35:51 CEST 2022


Author: minfrin at redwax.eu
Date: Sun Apr 17 13:35:50 2022
New Revision: 30

Log:
Return a promise in the page realm, and then keep track of it's reject
and resolve functions.

Perform the concatenation work in the content script, and then return the
string at the end of the process to the page script by calling the
function we kept track of above.

Modified:
    redwax-signtext/trunk/src/macos/experimental/firefox/content.js

Modified: redwax-signtext/trunk/src/macos/experimental/firefox/content.js
==============================================================================
--- redwax-signtext/trunk/src/macos/experimental/firefox/content.js	(original)
+++ redwax-signtext/trunk/src/macos/experimental/firefox/content.js	Sun Apr 17 13:35:50 2022
@@ -42,8 +42,11 @@
 	function signTextReader(requestStream, options, ...CAs) {
 		const uuid = uuidv4();
 		const reader = requestStream.getReader();
-		return new ReadableStream({
-			start(controller) {
+
+		underlyingSource = new window.wrappedJSObject.Object();
+
+                underlyingSource.start = exportFunction(
+			function(controller) {
 
 				function push() {
 					reader.read().then(({
@@ -129,45 +132,33 @@
 				/* fire off initial request */
 				push();
 
-			}
-		});
+			}, window.wrappedJSObject
+		);
+
+//		underlyingSource.type = "bytes";
+
+		return new ReadableStream(underlyingSource);
 	}
 
-	function readerStreamToString(stream) {
+	function readerStreamToString(stream, stringResolve, stringReject) {
 		const reader = stream.getReader();
 		let result = '';
 
-		let processText = exportFunction(function processText({
-			done,
-			value
-		}) {
-
-			return new window.wrappedJSObject.Promise(
-				exportFunction(
-					function(resolve, reject) {
-
-						if (Object.prototype.toString.call(value) === "[object Error]") {
-							reject(value);
-						} else if (done) {
-							console.log("Stream complete:" + result);
-							resolve(result);
-						} else {
-							result += value;
-							resolve(reader.read().then(processText));
-						}
-					}, window.wrappedJSObject
-				)
-			);
-
-		}, window.wrappedJSObject);
-
-		return new window.wrappedJSObject.Promise(
-			exportFunction(
-				function(resolve, reject) {
-					resolve( reader.read().then(processText) );
-				}, window.wrappedJSObject
-			)
-		);
+		let processText = function processText({done, value}) {
+			if (Object.prototype.toString.call(value) === "[object Error]") {
+				stringReject(value);
+			} else if (done) {
+				console.log("Stream complete:" + result);
+				stringResolve(result);
+			} else {
+				result += value;
+				reader.read().then(processText);
+			}
+		};
+
+		return new Promise((resolve, reject) => {
+			resolve( reader.read().then(processText) );
+		});
 	}
 
 	signText = function SignText(source, options, ...CAs) {
@@ -226,15 +217,26 @@
 		/* pass a string, we return a string */
 		if (typeof source === 'string') {
 
-			return new window.wrappedJSObject.Promise(
+			/* take promise functions outside for later callback */
+
+			var stringResolve;
+                        var stringReject;
+
+			var promise = new window.wrappedJSObject.Promise(
 				exportFunction(
 					function(resolve, reject) {
-						resolve(readerStreamToString(responseStream));
+						stringResolve = resolve;
+						stringReject = reject;
 					}, window.wrappedJSObject
 				)
 			);
 
-			//      return readerStreamToString(responseStream);
+			/* start doing the work in the realm of the content script */
+			readerStreamToString(responseStream, stringResolve, stringReject)
+
+			/* send the page our wrapped promise, which we'll resolve above */
+			return promise;
+
 		}
 
 		/* return a ReadableStream */
@@ -384,4 +386,4 @@
 		}
 	});
 
-}
+}



More information about the rs-commit mailing list