Skip to content

Commit

Permalink
Update header script
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Sep 9, 2023
1 parent 4a42b07 commit 270f5ba
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 10 deletions.
111 changes: 111 additions & 0 deletions packages/seroval/src/core/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* This file is used to represent the minified header script located at ./keys.ts
* If anything, this code is the unminified, readable version of it.
*/

// Global references array. Global because we (ideally) want <script> elements to share it.
$R = [];

// Promise constructor, used to construct deferred Promises
function $P(success, failure, promise) {
promise = new Promise(function (resolve, reject) {
success = resolve;
failure = reject;
});

promise.s = success;
promise.f = failure;

return promise;
}

// This unsets the custom properties of the Promise instance
function $uP(promise) {
delete promise.s;
delete promise.f;
}

function $Ps(referenceID, data) {
$R[referenceID].s(data);
}

function $Pf(referenceID, data) {
$R[referenceID].f(data);
}

// Unset stream
function $uS(stream) {
delete stream.q;
delete stream.e;
delete stream.c;
}

function $iS(streamController, callSignal) {
switch (callSignal[0]) {
case 0: return streamController.enqueue(callSignal[1]);
case 1: return streamController.error(callSignal[1]);
case 2: return streamController.close();
}
}


// Emits the signal and records it
function $wS(buffer, listeners, callType, data, i, len) {
buffer.push([callType, data]);
for (i = 0, len = listeners.length; i < len; i++) {
listeners[i]([callType, data]);
}
}

function $fS(buffer, controller, i, len) {
for (i = 0, len = buffer.length; i < len; i++) {
$iS(controller, buffer[i]);
}
}


// ReadableStream constructor. This is a special kind of stream because
// it's basically a Transformer stream with a buffer, so that it records
// past values, emits the recorded values on "subscription", and then
// continues recording it.
function $S(buffer, listeners, stream) {
// Buffer contains tuples of stream calls where the tuple is [callType, data]
// callType is either 0 (enqueue), 1 (error) or 2 (close)
buffer = [];

// An array of listeners
listeners = [];

stream = new ReadableStream({
start: function (controller) {
$fS(buffer, controller);
listeners.push(function (callSignal) {
$iS(controller, callSignal);
});
}
});

stream.q = function (data) {
$wS(buffer, listeners, 0, data);
};
stream.e = function (data) {
$wS(buffer, 1, data);
$uS(stream);
};
stream.c = function () {
$wS(buffer, listeners, 2);
$uS(stream);
};

return stream;
}

function $Sq(referenceID, data) {
$R[referenceID].q(data);
}
function $Se(referenceID, data) {
$R[referenceID].e(data);
}
function $Sc(referenceID) {
$R[referenceID].c();
}
24 changes: 14 additions & 10 deletions packages/seroval/src/core/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ export const GLOBAL_CONTEXT_STREAM_ERROR = '$Se';

export const ROOT_REFERENCE = 't';

const GLOBAL_HEADER = `function ${GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR}(s,f,p){return (p=new Promise(function(a,b){s=a,f=b})).${LOCAL_CONTEXT_PROMISE_RESOLVE}=s,p.${LOCAL_CONTEXT_PROMISE_REJECT}=f,p}
function $uP(p){delete p.${LOCAL_CONTEXT_PROMISE_RESOLVE};delete p.${LOCAL_CONTEXT_PROMISE_REJECT}}
function ${GLOBAL_CONTEXT_PROMISE_RESOLVE}(i,d){$R[i].${LOCAL_CONTEXT_PROMISE_RESOLVE}(d);$uP($R[i])}
function ${GLOBAL_CONTEXT_PROMISE_REJECT}(i,d){$R[i].${LOCAL_CONTEXT_PROMISE_REJECT}(d);$uP($R[i])}
function ${GLOBAL_CONTEXT_STREAM_CONSTRUCTOR}(n,e,t){function u(n,e){switch(e[0]){case 0:return n.enqueue(e[1]);case 1:return n.error(e[1]);case 2:return n.close()}}function r(t,u,r,c){for(n.push([t,u]),r=0,c=e.length;r<c;r++)e[r]([t,u])}return n=[],e=[],(t=new ReadableStream({start:function(t){!function(e,t,r){for(t=0,r=n.length;t<r;t++)u(e,n[t])}(t),e.push((function(n){u(t,n)}))}})).${LOCAL_CONTEXT_STREAM_ENQUEUE}=function(n){r(0,n)},function z(){delete t.${LOCAL_CONTEXT_STREAM_ENQUEUE};delete t.${LOCAL_CONTEXT_STREAM_ERROR};delete t.${LOCAL_CONTEXT_STREAM_CLOSE}},t.${LOCAL_CONTEXT_STREAM_ERROR}=function(n){r(1,n),z()},t.${LOCAL_CONTEXT_STREAM_CLOSE}=function(n){r(2),z()},t}
function ${GLOBAL_CONTEXT_STREAM_ENQUEUE}(i,d){$R[i].${LOCAL_CONTEXT_STREAM_ENQUEUE}(d)}
function ${GLOBAL_CONTEXT_STREAM_ERROR}(i,d){$R[i].${LOCAL_CONTEXT_STREAM_ERROR}(d)}
function ${GLOBAL_CONTEXT_STREAM_CLOSE}(i){$R[i].${LOCAL_CONTEXT_STREAM_CLOSE}()}
export const CROSS_REFERENCE_HEADER = `
${GLOBAL_CONTEXT_REFERENCES}=[];
function ${GLOBAL_CONTEXT_PROMISE_CONSTRUCTOR}(s,f,p){return (p=new Promise(function(a,b){s=a,f=b})).${LOCAL_CONTEXT_PROMISE_RESOLVE}=s,p.${LOCAL_CONTEXT_PROMISE_REJECT}=f,p}
function $uP(i,p){delete (p=${GLOBAL_CONTEXT_REFERENCES}[i]).${LOCAL_CONTEXT_PROMISE_RESOLVE};delete p.${LOCAL_CONTEXT_PROMISE_REJECT}}
function $Ps(i,d){${GLOBAL_CONTEXT_REFERENCES}[i].${LOCAL_CONTEXT_PROMISE_RESOLVE}(d),$uP(i)}
function $Pf(i,d){${GLOBAL_CONTEXT_REFERENCES}[i].${LOCAL_CONTEXT_PROMISE_REJECT}(d),$uP(i)}
function $uS(s){delete s.${LOCAL_CONTEXT_STREAM_ENQUEUE};delete s.${LOCAL_CONTEXT_STREAM_ERROR};delete s.${LOCAL_CONTEXT_STREAM_CLOSE}}
function $iS(c,d){switch(d[0]){case 0:return c.enqueue(d[1]);case 1:return c.error(d[1]);case 2:return c.close()}}
function $wS(b,l,t,d,i,k){for(b.push([t,d]),i=0,k=l.length;i<k;i++)l[i]([t,d])}
function $fS(b,c,i,k){for(i=0,k=b.length;i<k;i++)$iS(c,b[i])}
function $S(b,l,s){return b=[],l=[],(s=new ReadableStream({start:function(c){$fS(b,c),l.push(function(d){$iS(c,d)})}})).q=function(d){$wS(b,l,0,d)},s.e=function(d){$wS(b,l,1,d),$uS(s)},s.c=function(){$wS(b,l,2),$uS(s)},s}
function $Sq(i,d){$R[i].q(d)}
function $Se(i,d){$R[i].e(d)}
function $Sc(i){$R[i].c()}
`;

export const CROSS_REFERENCE_HEADER = `${GLOBAL_CONTEXT_REFERENCES}=[];${GLOBAL_HEADER}`;

0 comments on commit 270f5ba

Please sign in to comment.