[rs-commit] r42 - /redwax-signtext/trunk/src/linux/firefox/content.js
rs-commit at redwax.eu
rs-commit at redwax.eu
Mon Sep 12 11:18:53 CEST 2022
Author: minfrin at redwax.eu
Date: Mon Sep 12 11:18:52 2022
New Revision: 42
Log:
Remove the streams for now, they need a lot of work.
Modified:
redwax-signtext/trunk/src/linux/firefox/content.js
Modified: redwax-signtext/trunk/src/linux/firefox/content.js
==============================================================================
--- redwax-signtext/trunk/src/linux/firefox/content.js (original)
+++ redwax-signtext/trunk/src/linux/firefox/content.js Mon Sep 12 11:18:52 2022
@@ -15,23 +15,29 @@
*/
if (typeof exportFunction == 'function') {
- function stringReader(str) {
- var offset = 0;
- const chunkSize = 10;
- return new ReadableStream({
- pull(controller) {
- var nextOffset = offset + chunkSize;
- if (nextOffset >= str.length) {
- nextOffset = str.length;
- controller.enqueue(str.substring(offset, nextOffset));
- controller.close();
- } else {
- controller.enqueue(str.substring(offset, nextOffset));
- }
- offset = nextOffset;
- }
- });
- }
+ function stringReader(str) {
+ var offset = 0;
+ const chunkSize = 10;
+
+ const source = new window.wrappedJSObject.Object();
+
+ source.pull = exportFunction(
+ function(controller) {
+console.log("reader pull: " + " desiredsize: " + controller.desiredSize);
+ var nextOffset = offset + chunkSize;
+ if (nextOffset >= str.length) {
+ nextOffset = str.length;
+ controller.enqueue(str.substring(offset, nextOffset));
+ controller.close();
+ } else {
+ controller.enqueue(str.substring(offset, nextOffset));
+ }
+ offset = nextOffset;
+ }, window.wrappedJSObject
+ );
+
+ return new window.wrappedJSObject.ReadableStream(source);
+ }
function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
@@ -42,6 +48,8 @@
function signTextReader(requestStream, options, ...CAs) {
const uuid = uuidv4();
const reader = requestStream.getReader();
+
+console.log("signtextreader: ");
underlyingSource = new window.wrappedJSObject.Object();
@@ -144,26 +152,36 @@
const reader = stream.getReader();
let result = '';
- 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) );
- });
+console.log("readerStreamToString: ");
+
+ let processText = exportFunction(
+ 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);
+ }
+ }, window.wrappedJSObject
+ );
+
+ return new window.wrappedJSObject.Promise(
+ exportFunction(
+ function(resolve, reject) {
+ resolve( reader.read().then(processText) );
+ }, window.wrappedJSObject
+ )
+ );
}
signText = function SignText(source, options, ...CAs) {
- var requestStream;
+ const uuid = uuidv4();
+
+console.log("SignText: ");
if (typeof source === 'undefined') {
@@ -193,29 +211,8 @@
/* pass a string, we return a string */
if (typeof source === 'string') {
- var requestStream = stringReader(source);
- }
-
- /* consume a ReadableStream */
- else if (source instanceof ReadableStream) {
- var requestStream = source;
- } else {
-
- const response = new window.wrappedJSObject.Error('error:unknownType');
-
- return new window.wrappedJSObject.Promise(
- exportFunction(
- function(resolve, reject) {
- reject(response);
- }, window.wrappedJSObject
- )
- );
- }
-
- const responseStream = signTextReader(requestStream, options, CAs);
-
- /* pass a string, we return a string */
- if (typeof source === 'string') {
+
+ var result = [];
/* take promise functions outside for later callback */
@@ -231,17 +228,83 @@
)
);
- /* 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 */
+ const signTextHandler = function(e) {
+ /* message not for us */
+ if (!(e.uuid === undefined) && !(e.uuid === uuid)) {
+ /* do nothing */
+ }
+ /* an error was received, cancel the stream */
+ else if (e.error) {
+ const error = new window.wrappedJSObject.Error(e.error);
+ stringReject(error);
+ port.onMessage.removeListener(signTextHandler);
+ }
+ /* an ack was received, send the next chunk */
+ else if (e.response === true) {
+ port.postMessage({
+ protocol: location.protocol,
+ hostname: location.hostname,
+ contentType: 'text/plain',
+ uuid: uuid,
+ request: {
+ signText: options,
+ CAs: CAs
+ }
+ });
+ }
+ /* an eos was received, we're done */
+ else if (e.response === false) {
+ stringResolve(result.join(""));
+ port.onMessage.removeListener(signTextHandler);
+ }
+ /* a response was received, send ack */
+ else {
+ result.push(e.response);
+ port.postMessage({
+ protocol: location.protocol,
+ hostname: location.hostname,
+ contentType: 'text/plain',
+ uuid: uuid,
+ request: true
+ });
+ }
+ };
+
+ /* add our listener */
+ port.onMessage.addListener(signTextHandler);
+
+ port.onDisconnect.addListener((p) => {
+ if (p.error) {
+ console.log(`Redwax SignText: disconnected due to an error: ${p.error.message}`);
+ } else {
+ console.log(`Redwax SignText: disconnected`);
+ }
+ });
+
+
+ /* fire off our initial message */
+ port.postMessage({
+ protocol: location.protocol,
+ hostname: location.hostname,
+ contentType: 'text/plain',
+ uuid: uuid,
+ request: source
+ });
+
return promise;
-
}
- /* return a ReadableStream */
else {
- return responseStream;
+
+ const response = new window.wrappedJSObject.Error('error:unknownType');
+
+ return new window.wrappedJSObject.Promise(
+ exportFunction(
+ function(resolve, reject) {
+ reject(response);
+ }, window.wrappedJSObject
+ )
+ );
}
};
More information about the rs-commit
mailing list