aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-06-17 22:19:50 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-06-17 22:19:50 -0700
commit77d498e3ce618875ac03daa89f5a6264a78fc827 (patch)
tree9951ed159eac6bbc03b8c848508cdc4b7158885b
parentfefd1c29ef967c8b6013561299c5fa03cb210fa8 (diff)
downloadbun-jarred/some-fixes-for-eventsource.tar.gz
bun-jarred/some-fixes-for-eventsource.tar.zst
bun-jarred/some-fixes-for-eventsource.zip
-rw-r--r--packages/bun-types/globals.d.ts15
-rw-r--r--src/js/builtins/EventSource.ts2
-rw-r--r--src/js/out/WebCoreJSBuiltins.cpp4
3 files changed, 18 insertions, 3 deletions
diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts
index a5d011b52..8717759f2 100644
--- a/packages/bun-types/globals.d.ts
+++ b/packages/bun-types/globals.d.ts
@@ -3276,3 +3276,18 @@ declare var EventSource: {
readonly CONNECTING: number;
readonly OPEN: number;
};
+
+declare var navigator: {
+ /**
+ * `User-Agent` string used by {@link fetch} and {@link EventSource}
+ *
+ * To get the current version of Bun, consider {@link Bun.version} instead.
+ */
+ readonly userAgent: string;
+
+ /**
+ * Number of logical processors available to run threads on the user's
+ * computer.
+ */
+ readonly hardwareConcurrency: number;
+};
diff --git a/src/js/builtins/EventSource.ts b/src/js/builtins/EventSource.ts
index ac7ba9a6d..1ecbc0458 100644
--- a/src/js/builtins/EventSource.ts
+++ b/src/js/builtins/EventSource.ts
@@ -51,7 +51,7 @@ export function getEventSource() {
static #SendRequest(socket: Socket, url: URL) {
const self = socket.data;
const last_event_header = self.#lastEventID ? `Last-Event-ID: ${self.#lastEventID}\r\n` : "";
- const request = `GET ${url.pathname}${url.search} HTTP/1.1\r\nHost: ${url.host}\r\nConnection: Close\r\nContent-Type: text/event-stream\r\nContent-Length: 0\r\n${last_event_header}\r\n`;
+ const request = `GET ${url.pathname}${url.search} HTTP/1.1\r\nHost: ${url.host}\r\nUser-Agent: ${navigator.userAgent}\r\nConnection: Close\r\nContent-Type: text/event-stream\r\nContent-Length: 0\r\n${last_event_header}\r\n`;
const sended = socket.write(request);
if (sended !== request.length) {
self.#send_buffer = request.substring(sended);
diff --git a/src/js/out/WebCoreJSBuiltins.cpp b/src/js/out/WebCoreJSBuiltins.cpp
index a8c351fb4..04c67a22a 100644
--- a/src/js/out/WebCoreJSBuiltins.cpp
+++ b/src/js/out/WebCoreJSBuiltins.cpp
@@ -2916,9 +2916,9 @@ WEBCORE_FOREACH_WRITABLESTREAMDEFAULTCONTROLLER_BUILTIN_CODE(DEFINE_BUILTIN_GENE
const JSC::ConstructAbility s_eventSourceGetEventSourceCodeConstructAbility = JSC::ConstructAbility::CannotConstruct;
const JSC::ConstructorKind s_eventSourceGetEventSourceCodeConstructorKind = JSC::ConstructorKind::None;
const JSC::ImplementationVisibility s_eventSourceGetEventSourceCodeImplementationVisibility = JSC::ImplementationVisibility::Public;
-const int s_eventSourceGetEventSourceCodeLength = 5813;
+const int s_eventSourceGetEventSourceCodeLength = 5725;
static const JSC::Intrinsic s_eventSourceGetEventSourceCodeIntrinsic = JSC::NoIntrinsic;
-const char* const s_eventSourceGetEventSourceCode = "(function (){\"use strict\";class w extends Error{constructor(){super(...arguments)}}Object.defineProperty(w.prototype,\"name\",{value:\"ConnectionError\"});class O extends Error{constructor(){super(...arguments)}}Object.defineProperty(O.prototype,\"name\",{value:\"ProtocolError\"});class j extends EventTarget{#$;#w;#O;#j;#A;#B=!1;#F=null;#G=\"\";#J=\"\";#K=\"\";#L=!0;#M=0;#Q=0;#U=0;#V=null;static#W(A){A.#H()}static#X(A,B){const F=A.data,G=F.#K\?`Last-Event-ID: ${F.#K}\\r\\n`:\"\",J=`GET ${B.pathname}${B.search} HTTP/1.1\\r\\nHost: ${B.host}\\r\\nConnection: Close\\r\\nContent-Type: text/event-stream\\r\\nContent-Length: 0\\r\\n${G}\\r\\n`,K=A.write(J);if(K!==J.length)F.#J=J.substring(K)}static#Y(A,B,F){for(;;){if(F>=B.length)return;let G=-1,J=B.indexOf(\"\\r\\n\",F);const K=J+2;if(J>0)if(A.#M===0){const V=parseInt(B.substring(F,J),16);if(V===0){A.#w=2,A.#F\?.end();return}G=K+V}else G=B.length;else{if(A.#G.length===0){A.#G+=B.substring(F);return}G=B.length}let L=B.substring(K,G);F=G+2;let M=0,Q=L.indexOf(\"\\n\\n\");if(Q==-1){A.#G+=B.substring(K);return}if(A.#G.length)A.#G+=L,L=A.#G,A.#G=\"\";let U=!0;while(U){const V=L.substring(M,Q);let W,X=\"\",Y,Z=0,z=-1;for(;;){let T=V.indexOf(\"\\n\",Z);if(T===-1){if(Z>=V.length)break;T=V.length}const N=V.substring(Z,T);if(N.startsWith(\"data:\"))if(X.length)X+=`\\n${N.substring(5).trim()}`;else X=N.substring(5).trim();else if(N.startsWith(\"event:\"))W=N.substring(6).trim();else if(N.startsWith(\"id:\"))Y=N.substring(3).trim();else if(N.startsWith(\"retry:\")){if(z=parseInt(N.substring(6).trim(),10),@isNaN(z))z=-1}Z=T+1}if(A.#K=Y||\"\",z>=0)A.#U=z;if(X||Y||W)A.dispatchEvent(new MessageEvent(W||\"message\",{data:X||\"\",origin:A.#$.origin,source:A,lastEventId:Y}));if(L.length===Q+2){U=!1;break}const H=L.indexOf(\"\\n\\n\",Q+1);if(H===-1)break;M=Q,Q=H}}}static#Z={open(A){const B=A.data;if(A.timeout(999999999),B.#F=A,!B.#B)j.#X(A,B.#$)},handshake(A,B,F){const G=A.data;if(B)j.#X(A,G.#$);else G.#w=2,G.dispatchEvent(new ErrorEvent(\"error\",{error:F})),A.end()},data(A,B){const F=A.data;switch(F.#w){case 0:{let G=B.toString();const J=G.indexOf(\"\\r\\n\\r\\n\");if(J===-1){F.#G+=G;return}if(F.#G.length)F.#G+=G,G=F.#G,F.#G=\"\";const K=G.substring(0,J),L=K.indexOf(\"\\r\\n\");if(L===-1){F.#w=2,F.dispatchEvent(new ErrorEvent(\"error\",{error:new O(\"Invalid HTTP request\")})),A.end();return}if(K.substring(0,L)!==\"HTTP/1.1 200 OK\"){F.#w=2,F.dispatchEvent(new ErrorEvent(\"error\",{error:new O(\"Unexpected status line:\\n\"+JSON.stringify(K))})),A.end();return}let Q=L+1,U=!1,V=-1;for(;;){let X=K.indexOf(\"\\r\\n\",Q);if(X===-1){if(Q>=K.length){if(!U)F.#w=2,F.dispatchEvent(new ErrorEvent(\"error\",{error:new O(`EventSource HTTP response must have \\\"Content-Type\\\" header set to \"text/event-stream\". Aborting the connection.`)})),A.end();return}X=K.length}const Y=K.substring(Q+1,X),Z=Y.indexOf(\":\"),z=Y.substring(0,Z),H=z.localeCompare(\"content-type\",@undefined,{sensitivity:\"accent\"})===0;if(Q=X+1,H)if(Y.endsWith(\" text/event-stream\"))U=!0;else{F.#w=2,F.dispatchEvent(new ErrorEvent(\"error\",{error:new O(`EventSource HTTP response must have \\\"Content-Type\\\" header set to \"text/event-stream\". Aborting the connection.`)})),A.end();return}else if(z.localeCompare(\"content-length\",@undefined,{sensitivity:\"accent\"})===0){if(V=parseInt(Y.substring(Z+1).trim(),10),@isNaN(V)||V<=0){F.dispatchEvent(new ErrorEvent(\"error\",{error:new Error(`EventSource's Content-Length is invalid. Aborting the connection.`)})),A.end();return}if(U)break}else if(z.localeCompare(\"transfer-encoding\",@undefined,{sensitivity:\"accent\"})===0){if(Y.substring(Z+1).trim()!==\"chunked\"){F.dispatchEvent(new ErrorEvent(\"error\",{error:new O(`EventSource's Transfer-Encoding is invalid. Aborting the connection.`)})),A.end();return}if(V=0,U)break}}F.#M=V,F.#w=1,F.dispatchEvent(new Event(\"open\"));const W=G.substring(J+4);if(j.#Y(F,W,0),F.#M>0){if(F.#Q+=W.length,F.#Q>=F.#M)F.#w=2,A.end()}return}case 1:if(j.#Y(F,B.toString(),2),F.#M>0){if(F.#Q+=B.byteLength,F.#Q>=F.#M)F.#w=2,A.end()}return;default:break}},drain(A){const B=A.data;if(B.#w===0){const F=B.#G;if(F.length){const G=A.write(F);if(G!==F.length)A.data.#J=F.substring(G);else A.data.#J=\"\"}}},close:j.#z,end(A){j.#z(A).dispatchEvent(new ErrorEvent(\"error\",{error:new Error(\"Connection closed by server\")}))},timeout(A){j.#z(A).dispatchEvent(new ErrorEvent(\"error\",{error:new Error(\"Timeout\")}))},binaryType:\"buffer\"};static#z(A){const B=A.data;if(B.#F=null,B.#Q=0,B.#w=2,B.#L){if(B.#V)clearTimeout(B.#V);B.#V=setTimeout(j.#W,B.#U,B)}return B}constructor(A,B=@undefined){super();const F=new URL(A);this.#B=F.protocol===\"https:\",this.#$=F,this.#w=2,process.nextTick(j.#W,this)}ref(){this.#V\?.ref(),this.#F\?.ref()}unref(){this.#V\?.unref(),this.#F\?.unref()}#H(){if(this.#w!==2)return;const A=this.#$,B=this.#B;this.#w=0,@Bun.connect({data:this,socket:j.#Z,hostname:A.hostname,port:parseInt(A.port||(B\?\"443\":\"80\"),10),tls:B\?{requestCert:!0,rejectUnauthorized:!1}:!1}).catch((F)=>{if(this.dispatchEvent(new ErrorEvent(\"error\",{error:F})),this.#L){if(this.#V)this.#V.unref\?.();this.#V=setTimeout(j.#W,1000,this)}})}get url(){return this.#$.href}get readyState(){return this.#w}close(){this.#L=!1,this.#w=2,this.#F\?.unref(),this.#F\?.end()}get onopen(){return this.#A}get onerror(){return this.#O}get onmessage(){return this.#j}set onopen(A){if(this.#A)super.removeEventListener(\"close\",this.#A);super.addEventListener(\"open\",A),this.#A=A}set onerror(A){if(this.#O)super.removeEventListener(\"error\",this.#O);super.addEventListener(\"error\",A),this.#O=A}set onmessage(A){if(this.#j)super.removeEventListener(\"message\",this.#j);super.addEventListener(\"message\",A),this.#j=A}}return Object.defineProperty(j.prototype,\"CONNECTING\",{enumerable:!0,value:0}),Object.defineProperty(j.prototype,\"OPEN\",{enumerable:!0,value:1}),Object.defineProperty(j.prototype,\"CLOSED\",{enumerable:!0,value:2}),j[Symbol.for(\"CommonJS\")]=0,j})\n";
+const char* const s_eventSourceGetEventSourceCode = "(function (){\"use strict\";class L extends Error{constructor(){super(...arguments)}}Object.defineProperty(L.prototype,\"name\",{value:\"ProtocolError\"});class O extends EventTarget{#$;#L;#O;#w;#W;#j=!1;#A=null;#B=\"\";#F=\"\";#G=\"\";#J=!0;#K=0;#M=0;#Q=0;#U=null;static#V(w){w.#T()}static#X(w,W){const j=w.data,A=j.#G\?`Last-Event-ID: ${j.#G}\\r\\n`:\"\",B=`GET ${W.pathname}${W.search} HTTP/1.1\\r\\nHost: ${W.host}\\r\\nUser-Agent: ${navigator.userAgent}\\r\\nConnection: Close\\r\\nContent-Type: text/event-stream\\r\\nContent-Length: 0\\r\\n${A}\\r\\n`,F=w.write(B);if(F!==B.length)j.#F=B.substring(F)}static#Y(w,W,j){for(;;){if(j>=W.length)return;let A=-1,B=W.indexOf(\"\\r\\n\",j);const F=B+2;if(B>0)if(w.#K===0){const Q=parseInt(W.substring(j,B),16);if(Q===0){w.#L=2,w.#A\?.end();return}A=F+Q}else A=W.length;else{if(w.#B.length===0){w.#B+=W.substring(j);return}A=W.length}let G=W.substring(F,A);j=A+2;let J=0,K=G.indexOf(\"\\n\\n\");if(K==-1){w.#B+=W.substring(F);return}if(w.#B.length)w.#B+=G,G=w.#B,w.#B=\"\";let M=!0;while(M){const Q=G.substring(J,K);let U,V=\"\",X,Y=0,Z=-1;for(;;){let T=Q.indexOf(\"\\n\",Y);if(T===-1){if(Y>=Q.length)break;T=Q.length}const z=Q.substring(Y,T);if(z.startsWith(\"data:\"))if(V.length)V+=`\\n${z.substring(5).trim()}`;else V=z.substring(5).trim();else if(z.startsWith(\"event:\"))U=z.substring(6).trim();else if(z.startsWith(\"id:\"))X=z.substring(3).trim();else if(z.startsWith(\"retry:\")){if(Z=parseInt(z.substring(6).trim(),10),@isNaN(Z))Z=-1}Y=T+1}if(w.#G=X||\"\",Z>=0)w.#Q=Z;if(V||X||U)w.dispatchEvent(new MessageEvent(U||\"message\",{data:V||\"\",origin:w.#$.origin,source:w,lastEventId:X}));if(G.length===K+2){M=!1;break}const H=G.indexOf(\"\\n\\n\",K+1);if(H===-1)break;J=K,K=H}}}static#Z={open(w){const W=w.data;if(w.timeout(999999999),W.#A=w,!W.#j)O.#X(w,W.#$)},handshake(w,W,j){const A=w.data;if(W)O.#X(w,A.#$);else A.#L=2,A.dispatchEvent(new ErrorEvent(\"error\",{error:j})),w.end()},data(w,W){const j=w.data;switch(j.#L){case 0:{let A=W.toString();const B=A.indexOf(\"\\r\\n\\r\\n\");if(B===-1){j.#B+=A;return}if(j.#B.length)j.#B+=A,A=j.#B,j.#B=\"\";const F=A.substring(0,B),G=F.indexOf(\"\\r\\n\");if(G===-1){j.#L=2,j.dispatchEvent(new ErrorEvent(\"error\",{error:new L(\"Invalid HTTP request\")})),w.end();return}if(F.substring(0,G)!==\"HTTP/1.1 200 OK\"){j.#L=2,j.dispatchEvent(new ErrorEvent(\"error\",{error:new L(\"Unexpected status line\\n\"+JSON.stringify(F))})),w.end();return}let K=G+1,M=!1,Q=-1;for(;;){let V=F.indexOf(\"\\r\\n\",K);if(V===-1){if(K>=F.length){if(!M)j.#L=2,j.dispatchEvent(new ErrorEvent(\"error\",{error:new L(`EventSource HTTP response must have \\\"Content-Type\\\" header set to \"text/event-stream\". Aborting the connection.`)})),w.end();return}V=F.length}const X=F.substring(K+1,V),Y=X.indexOf(\":\"),Z=X.substring(0,Y),H=Z.localeCompare(\"content-type\",@undefined,{sensitivity:\"accent\"})===0;if(K=V+1,H)if(X.endsWith(\" text/event-stream\"))M=!0;else{j.#L=2,j.dispatchEvent(new ErrorEvent(\"error\",{error:new L(`EventSource HTTP response must have \\\"Content-Type\\\" header set to \"text/event-stream\". Aborting the connection.`)})),w.end();return}else if(Z.localeCompare(\"content-length\",@undefined,{sensitivity:\"accent\"})===0){if(Q=parseInt(X.substring(Y+1).trim(),10),@isNaN(Q)||Q<=0){j.dispatchEvent(new ErrorEvent(\"error\",{error:new Error(`EventSource's Content-Length is invalid. Aborting the connection.`)})),w.end();return}if(M)break}else if(Z.localeCompare(\"transfer-encoding\",@undefined,{sensitivity:\"accent\"})===0){if(X.substring(Y+1).trim()!==\"chunked\"){j.dispatchEvent(new ErrorEvent(\"error\",{error:new L(`EventSource's Transfer-Encoding is invalid. Aborting the connection.`)})),w.end();return}if(Q=0,M)break}}j.#K=Q,j.#L=1,j.dispatchEvent(new Event(\"open\"));const U=A.substring(B+4);if(O.#Y(j,U,0),j.#K>0){if(j.#M+=U.length,j.#M>=j.#K)j.#L=2,w.end()}return}case 1:if(O.#Y(j,W.toString(),2),j.#K>0){if(j.#M+=W.byteLength,j.#M>=j.#K)j.#L=2,w.end()}return;default:break}},drain(w){const W=w.data;if(W.#L===0){const j=W.#B;if(j.length){const A=w.write(j);if(A!==j.length)w.data.#F=j.substring(A);else w.data.#F=\"\"}}},close:O.#H,end(w){O.#H(w).dispatchEvent(new ErrorEvent(\"error\",{error:new Error(\"Connection closed by server\")}))},timeout(w){O.#H(w).dispatchEvent(new ErrorEvent(\"error\",{error:new Error(\"Timeout\")}))},binaryType:\"buffer\"};static#H(w){const W=w.data;if(W.#A=null,W.#M=0,W.#L=2,W.#J){if(W.#U)clearTimeout(W.#U);W.#U=setTimeout(O.#V,W.#Q,W)}return W}constructor(w,W=@undefined){super();const j=new URL(w);this.#j=j.protocol===\"https:\",this.#$=j,this.#L=2,process.nextTick(O.#V,this)}ref(){this.#U\?.ref(),this.#A\?.ref()}unref(){this.#U\?.unref(),this.#A\?.unref()}#T(){if(this.#L!==2)return;const w=this.#$,W=this.#j;this.#L=0,@Bun.connect({data:this,socket:O.#Z,hostname:w.hostname,port:parseInt(w.port||(W\?\"443\":\"80\"),10),tls:W\?{requestCert:!0,rejectUnauthorized:!1}:!1}).catch((j)=>{if(this.dispatchEvent(new ErrorEvent(\"error\",{error:j})),this.#J){if(this.#U)this.#U.unref\?.();this.#U=setTimeout(O.#V,1000,this)}})}get url(){return this.#$.href}get readyState(){return this.#L}close(){this.#J=!1,this.#L=2,this.#A\?.unref(),this.#A\?.end()}get onopen(){return this.#W}get onerror(){return this.#O}get onmessage(){return this.#w}set onopen(w){if(this.#W)super.removeEventListener(\"close\",this.#W);super.addEventListener(\"open\",w),this.#W=w}set onerror(w){if(this.#O)super.removeEventListener(\"error\",this.#O);super.addEventListener(\"error\",w),this.#O=w}set onmessage(w){if(this.#w)super.removeEventListener(\"message\",this.#w);super.addEventListener(\"message\",w),this.#w=w}}return Object.defineProperty(O.prototype,\"CONNECTING\",{enumerable:!0,value:0}),Object.defineProperty(O.prototype,\"OPEN\",{enumerable:!0,value:1}),Object.defineProperty(O.prototype,\"CLOSED\",{enumerable:!0,value:2}),O[Symbol.for(\"CommonJS\")]=0,O})\n";
#define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \