MCPcopy Create free account
hub / github.com/ChinaSiro/claude-code-sourcemap / _read

Method _read

package/cli.js:2498–2498  ·  view source on GitHub ↗
(q)

Source from the content-addressed store, hash-verified

2496${JSON.stringify(Object.fromEntries(_))}`));return}let Y=parseInt(z);if(isNaN(Y)){this.fireError(Error(`Content-Length value must be a number. Got ${z}`));return}this.nextMessageLength=Y}let K=this.buffer.tryReadBody(this.nextMessageLength);if(K===void 0){this.setPartialMessageTimer();return}this.clearPartialMessageTimer(),this.nextMessageLength=-1,this.readSemaphore.lock(async()=>{let _=this.options.contentDecoder!==void 0?await this.options.contentDecoder.decode(K):K,z=await this.options.contentTypeDecoder.decode(_,this.options);this.callback(z)}).catch((_)=>{this.fireError(_)})}}catch(K){this.fireError(K)}}clearPartialMessageTimer(){if(this.partialMessageTimer)this.partialMessageTimer.dispose(),this.partialMessageTimer=void 0}setPartialMessageTimer(){if(this.clearPartialMessageTimer(),this._partialMessageTimeout<=0)return;this.partialMessageTimer=(0,go1.default)().timer.setTimeout((q,K)=>{if(this.partialMessageTimer=void 0,q===this.messageToken)this.firePartialMessage({messageToken:q,waitingTime:K}),this.setPartialMessageTimer()},this._partialMessageTimeout,this.messageToken,this._partialMessageTimeout)}}b9K.ReadableStreamMessageReader=C9K});var Q9K=m((F9K)=>{Object.defineProperty(F9K,"__esModule",{value:!0});F9K.WriteableStreamMessageWriter=F9K.AbstractMessageWriter=F9K.MessageWriter=void 0;var u9K=fK6(),V68=wL6(),xcz=po1(),m9K=jL6(),Icz="Content-Length: ",p9K=`\r
2497`,B9K;(function(q){function K(_){let z=_;return z&&V68.func(z.dispose)&&V68.func(z.onClose)&&V68.func(z.onError)&&V68.func(z.write)}q.is=K})(B9K||(F9K.MessageWriter=B9K={}));class do1{constructor(){this.errorEmitter=new m9K.Emitter,this.closeEmitter=new m9K.Emitter}dispose(){this.errorEmitter.dispose(),this.closeEmitter.dispose()}get onError(){return this.errorEmitter.event}fireError(q,K,_){this.errorEmitter.fire([this.asError(q),K,_])}get onClose(){return this.closeEmitter.event}fireClose(){this.closeEmitter.fire(void 0)}asError(q){if(q instanceof Error)return q;else return Error(`Writer received error. Reason: ${V68.string(q.message)?q.message:"unknown"}`)}}F9K.AbstractMessageWriter=do1;var Qo1;(function(q){function K(_){if(_===void 0||typeof _==="string")return{charset:_??"utf-8",contentTypeEncoder:(0,u9K.default)().applicationJson.encoder};else return{charset:_.charset??"utf-8",contentEncoder:_.contentEncoder,contentTypeEncoder:_.contentTypeEncoder??(0,u9K.default)().applicationJson.encoder}}q.fromOptions=K})(Qo1||(Qo1={}));class g9K extends do1{constructor(q,K){super();this.writable=q,this.options=Qo1.fromOptions(K),this.errorCount=0,this.writeSemaphore=new xcz.Semaphore(1),this.writable.onError((_)=>this.fireError(_)),this.writable.onClose(()=>this.fireClose())}async write(q){return this.writeSemaphore.lock(async()=>{return this.options.contentTypeEncoder.encode(q,this.options).then((_)=>{if(this.options.contentEncoder!==void 0)return this.options.contentEncoder.encode(_);else return _}).then((_)=>{let z=[];return z.push(Icz,_.byteLength.toString(),p9K),z.push(p9K),this.doWrite(q,z,_)},(_)=>{throw this.fireError(_),_})})}async doWrite(q,K,_){try{return await this.writable.write(K.join(""),"ascii"),this.writable.write(_)}catch(z){return this.handleError(z,q),Promise.reject(z)}}handleError(q,K){this.errorCount++,this.fireError(q,K,this.errorCount)}end(){this.writable.end()}}F9K.WriteableStreamMessageWriter=g9K});var n9K=m((c9K)=>{Object.defineProperty(c9K,"__esModule",{value:!0});c9K.AbstractMessageBuffer=void 0;var pcz=13,Bcz=10,gcz=`\r
2498`;class d9K{constructor(q="utf-8"){this._encoding=q,this._chunks=[],this._totalLength=0}get encoding(){return this._encoding}append(q){let K=typeof q==="string"?this.fromString(q,this._encoding):q;this._chunks.push(K),this._totalLength+=K.byteLength}tryReadHeaders(q=!1){if(this._chunks.length===0)return;let K=0,_=0,z=0,Y=0;q:while(_<this._chunks.length){let w=this._chunks[_];z=0;K:while(z<w.length){switch(w[z]){case pcz:switch(K){case 0:K=1;break;case 2:K=3;break;default:K=0}break;case Bcz:switch(K){case 1:K=2;break;case 3:K=4,z++;break q;default:K=0}break;default:K=0}z++}Y+=w.byteLength,_++}if(K!==4)return;let $=this._read(Y+z),A=new Map,O=this.toString($,"ascii").split(gcz);if(O.length<2)return A;for(let w=0;w<O.length-2;w++){let j=O[w],H=j.indexOf(":");if(H===-1)throw Error(`Message header must separate key and value using ':'
2499${j}`);let J=j.substr(0,H),M=j.substr(H+1).trim();A.set(q?J.toLowerCase():J,M)}return A}tryReadBody(q){if(this._totalLength<q)return;return this._read(q)}get numberOfBytes(){return this._totalLength}_read(q){if(q===0)return this.emptyBuffer();if(q>this._totalLength)throw Error("Cannot read so many bytes!");if(this._chunks[0].byteLength===q){let Y=this._chunks[0];return this._chunks.shift(),this._totalLength-=q,this.asNative(Y)}if(this._chunks[0].byteLength>q){let Y=this._chunks[0],$=this.asNative(Y,q);return this._chunks[0]=Y.slice(q),this._totalLength-=q,$}let K=this.allocNative(q),_=0,z=0;while(q>0){let Y=this._chunks[z];if(Y.byteLength>q){let $=Y.slice(0,q);K.set($,_),_+=q,this._chunks[z]=Y.slice(q),this._totalLength-=q,q-=q}else K.set(Y,_),_+=Y.byteLength,this._chunks.shift(),this._totalLength-=Y.byteLength,q-=Y.byteLength}return K}}c9K.AbstractMessageBuffer=d9K});var z_K=m((t9K)=>{Object.defineProperty(t9K,"__esModule",{value:!0});t9K.createMessageConnection=t9K.ConnectionOptions=t9K.MessageStrategy=t9K.CancellationStrategy=t9K.CancellationSenderStrategy=t9K.CancellationReceiverStrategy=t9K.RequestCancellationReceiverStrategy=t9K.IdCancellationReceiverStrategy=t9K.ConnectionStrategy=t9K.ConnectionError=t9K.ConnectionErrors=t9K.LogTraceNotification=t9K.SetTraceNotification=t9K.TraceFormat=t9K.TraceValues=t9K.Trace=t9K.NullLogger=t9K.ProgressType=t9K.ProgressToken=void 0;var i9K=fK6(),BH=wL6(),D3=So1(),r9K=bo1(),N68=jL6(),co1=nx8(),L68;(function(q){q.type=new D3.NotificationType("$/cancelRequest")})(L68||(L68={}));var lo1;(function(q){function K(_){return typeof _==="string"||typeof _==="number"}q.is=K})(lo1||(t9K.ProgressToken=lo1={}));var y68;(function(q){q.type=new D3.NotificationType("$/progress")})(y68||(y68={}));class s9K{constructor(){}}t9K.ProgressType=s9K;var no1;(function(q){function K(_){return BH.func(_)}q.is=K})(no1||(no1={}));t9K.NullLogger=Object.freeze({error:()=>{},warn:()=>{},info:()=>{},log:()=>{}});var qA;(function(q){q[q.Off=0]="Off",q[q.Messages=1]="Messages",q[q.Compact=2]="Compact",q[q.Verbose=3]="Verbose"})(qA||(t9K.Trace=qA={}));var o9K;(function(q){q.Off="off",q.Messages="messages",q.Compact="compact",q.Verbose="verbose"})(o9K||(t9K.TraceValues=o9K={}));(function(q){function K(z){if(!BH.string(z))return q.Off;switch(z=z.toLowerCase(),z){case"off":return q.Off;case"messages":return q.Messages;case"compact":return q.Compact;case"verbose":return q.Verbose;default:return q.Off}}q.fromString=K;function _(z){switch(z){case q.Off:return"off";case q.Messages:return"messages";case q.Compact:return"compact";case q.Verbose:return"verbose";default:return"off"}}q.toString=_})(qA||(t9K.Trace=qA={}));var KR;(function(q){q.Text="text",q.JSON="json"})(KR||(t9K.TraceFormat=KR={}));(function(q){function K(_){if(!BH.string(_))return q.Text;if(_=_.toLowerCase(),_==="json")return q.JSON;else return q.Text}q.fromString=K})(KR||(t9K.TraceFormat=KR={}));var io1;(function(q){q.type=new D3.NotificationType("$/setTrace")})(io1||(t9K.SetTraceNotification=io1={}));var ix8;(function(q){q.type=new D3.NotificationType("$/logTrace")})(ix8||(t9K.LogTraceNotification=ix8={}));var E68;(function(q){q[q.Closed=1]="Closed",q[q.Disposed=2]="Disposed",q[q.AlreadyListening=3]="AlreadyListening"})(E68||(t9K.ConnectionErrors=E68={}));class JL6 extends Error{constructor(q,K){super(K);this.code=q,Object.setPrototypeOf(this,JL6.prototype)}}t9K.ConnectionError=JL6;var ro1;(function(q){function K(_){let z=_;return z&&BH.func(z.cancelUndispatched)}q.is=K})(ro1||(t9K.ConnectionStrategy=ro1={}));var rx8;(function(q){function K(_){let z=_;return z&&(z.kind===void 0||z.kind==="id")&&BH.func(z.createCancellationTokenSource)&&(z.dispose===void 0||BH.func(z.dispose))}q.is=K})(rx8||(t9K.IdCancellationReceiverStrategy=rx8={}));var oo1;(function(q){function K(_){let z=_;return z&&z.kind==="request"&&BH.func(z.createCancellationTokenSource)&&(z.dispose===void 0||BH.func(z.dispose))}q.is=K})(oo1||(t9K.RequestCancellationReceiverStrategy=oo1={}));var ox8;(function(q){q.Message=Object.freeze({createCancellationTokenSource(_){return new co1.CancellationTokenSource}});function K(_){return rx8.is(_)||oo1.is(_)}q.is=K})(ox8||(t9K.CancellationReceiverStrategy=ox8={}));var ax8;(function(q){q.Message=Object.freeze({sendCancellation(_,z){return _.sendNotification(L68.type,{id:z})},cleanup(_){}});function K(_){let z=_;return z&&BH.func(z.sendCancellation)&&BH.func(z.cleanup)}q.is=K})(ax8||(t9K.CancellationSenderStrategy=ax8={}));var sx8;(function(q){q.Message=Object.freeze({receiver:ox8.Message,sender:ax8.Message});function K(_){let z=_;return z&&ox8.is(z.receiver)&&ax8.is(z.sender)}q.is=K})(sx8||(t9K.CancellationStrategy=sx8={}));var tx8;(function(q){function K(_){let z=_;return z&&BH.func(z.handleMessage)}q.is=K})(tx8||(t9K.MessageStrategy=tx8={}));var a9K;(function(q){function K(_){let z=_;return z&&(sx8.is(z.cancellationStrategy)||ro1.is(z.connectionStrategy)||tx8.is(z.messageStrategy))}q.is=K})(a9K||(t9K.ConnectionOptions=a9K={}));var tm;(function(q){q[q.New=1]="New",q[q.Listening=2]="Listening",q[q.Closed=3]="Closed",q[q.Disposed=4]="Disposed"})(tm||(tm={}));function Fcz(q,K,_,z){let Y=_!==void 0?_:t9K.NullLogger,$=0,A=0,O=0,w="2.0",j=void 0,H=new Map,J=void 0,M=new Map,X=new Map,P,W=new r9K.LinkedMap,D=new Map,f=new Set,G=new Map,Z=qA.Off,T=KR.Text,v,V=tm.New,E=new N68.Emitter,S=new N68.Emitter,R=new N68.Emitter,x=new N68.Emitter,I=new N68.Emitter,B=z&&z.cancellationStrategy?z.cancellationStrategy:sx8.Message;function p(E6){if(E6===null)throw Error("Can't send requests with id null since the response can't be correlated.");return"req-"+E6.toString()}function C(E6){if(E6===null)return"res-unknown-"+(++O).toString();else return"res-"+E6.toString()}function g(){return"not-"+(++A).toString()}function F(E6,R6){if(D3.Message.isRequest(R6))E6.set(p(R6.id),R6);else if(D3.Message.isResponse(R6))E6.set(C(R6.id),R6);else E6.set(g(),R6)}function U(E6){return}function c(){return V===tm.Listening}function K6(){return V===tm.Closed}function t(){return V===tm.Disposed}function q6(){if(V===tm.New||V===tm.Listening)V=tm.Closed,S.fire(void 0)}function e(E6){E.fire([E6,void 0,void 0])}function i(E6){E.fire(E6)}q.onClose(q6),q.onError(e),K.onClose(q6),K.onError(i);function $6(){if(P||W.size===0)return;P=(0,i9K.default)().timer.setImmediate(()=>{P=void 0,j6()})}function M6(E6){if(D3.Message.isRequest(E6))w6(E6);else if(D3.Message.isNotification(E6))s(E6);else if(D3.Message.isResponse(E6))o(E6);else a(E6)}function j6(){if(W.size===0)return;let E6=W.shift();try{let R6=z?.messageStrategy;if(tx8.is(R6))R6.handleMessage(E6,M6);else M6(E6)}finally{$6()}}let G6=(E6)=>{try{if(D3.Message.isNotification(E6)&&E6.method===L68.type.method){let R6=E6.params.id,h6=p(R6),x6=W.get(h6);if(D3.Message.isRequest(x6)){let Q6=z?.connectionStrategy,Y6=Q6&&Q6.cancelUndispatched?Q6.cancelUndispatched(x6,U):U(x6);if(Y6&&(Y6.error!==void 0||Y6.result!==void 0)){W.delete(h6),G.delete(R6),Y6.id=x6.id,J6(Y6,E6.method,Date.now()),K.write(Y6).catch(()=>Y.error("Sending response for canceled message failed."));return}}let b6=G.get(R6);if(b6!==void 0){b6.cancel(),f6(E6);return}else f.add(R6)}F(W,E6)}finally{$6()}};function w6(E6){if(t())return;function R6(F6,W6,v6){let m6={jsonrpc:w,id:E6.id};if(F6 instanceof D3.ResponseError)m6.error=F6.toJson();else m6.result=F6===void 0?null:F6;J6(m6,W6,v6),K.write(m6).catch(()=>Y.error("Sending response failed."))}function h6(F6,W6,v6){let m6={jsonrpc:w,id:E6.id,error:F6.toJson()};J6(m6,W6,v6),K.write(m6).catch(()=>Y.error("Sending response failed."))}function x6(F6,W6,v6){if(F6===void 0)F6=null;let m6={jsonrpc:w,id:E6.id,result:F6};J6(m6,W6,v6),K.write(m6).catch(()=>Y.error("Sending response failed."))}X6(E6);let b6=H.get(E6.method),Q6,Y6;if(b6)Q6=b6.type,Y6=b6.handler;let _6=Date.now();if(Y6||j){let F6=E6.id??String(Date.now()),W6=rx8.is(B.receiver)?B.receiver.createCancellationTokenSource(F6):B.receiver.createCancellationTokenSource(E6);if(E6.id!==null&&f.has(E6.id))W6.cancel();if(E6.id!==null)G.set(F6,W6);try{let v6;if(Y6)if(E6.params===void 0){if(Q6!==void 0&&Q6.numberOfParams!==0){h6(new D3.ResponseError(D3.ErrorCodes.InvalidParams,`Request ${E6.method} defines ${Q6.numberOfParams} params but received none.`),E6.method,_6);return}v6=Y6(W6.token)}else if(Array.isArray(E6.params)){if(Q6!==void 0&&Q6.parameterStructures===D3.ParameterStructures.byName){h6(new D3.ResponseError(D3.ErrorCodes.InvalidParams,`Request ${E6.method} defines parameters by name but received parameters by position`),E6.method,_6);return}v6=Y6(...E6.params,W6.token)}else{if(Q6!==void 0&&Q6.parameterStructures===D3.ParameterStructures.byPosition){h6(new D3.ResponseError(D3.ErrorCodes.InvalidParams,`Request ${E6.method} defines parameters by position but received parameters by name`),E6.method,_6);return}v6=Y6(E6.params,W6.token)}else if(j)v6=j(E6.method,E6.params,W6.token);let m6=v6;if(!v6)G.delete(F6),x6(v6,E6.method,_6);else if(m6.then)m6.then((K8)=>{G.delete(F6),R6(K8,E6.method,_6)},(K8)=>{if(G.delete(F6),K8 instanceof D3.ResponseError)h6(K8,E6.method,_6);else if(K8&&BH.string(K8.message))h6(new D3.ResponseError(D3.ErrorCodes.InternalError,`Request ${E6.method} failed with message: ${K8.message}`),E6.method,_6);else h6(new D3.ResponseError(D3.ErrorCodes.InternalError,`Request ${E6.method} failed unexpectedly without providing any details.`),E6.method,_6)});else G.delete(F6),R6(v6,E6.method,_6)}catch(v6){if(G.delete(F6),v6 instanceof D3.ResponseError)R6(v6,E6.method,_6);else if(v6&&BH.string(v6.message))h6(new D3.ResponseError(D3.ErrorCodes.InternalError,`Request ${E6.method} failed with message: ${v6.message}`),E6.method,_6);else h6(new D3.ResponseError(D3.ErrorCodes.InternalError,`Request ${E6.method} failed unexpectedly without providing any details.`),E6.method,_6)}}else h6(new D3.ResponseError(D3.ErrorCodes.MethodNotFound,`Unhandled method ${E6.method}`),E6.method,_6)}function o(E6){if(t())return;if(E6.id===null)if(E6.error)Y.error(`Received response message without id: Error is:
2500${JSON.stringify(E6.error,void 0,4)}`);else Y.error("Received response message without id. No further error information provided.");else{let R6=E6.id,h6=D.get(R6);if(N6(E6,h6),h6!==void 0){D.delete(R6);try{if(E6.error){let x6=E6.error;h6.reject(new D3.ResponseError(x6.code,x6.message,x6.data))}else if(E6.result!==void 0)h6.resolve(E6.result);else throw Error("Should never happen.")}catch(x6){if(x6.message)Y.error(`Response handler '${h6.method}' failed with message: ${x6.message}`);else Y.error(`Response handler '${h6.method}' failed unexpectedly.`)}}}}function s(E6){if(t())return;let R6=void 0,h6;if(E6.method===L68.type.method){let x6=E6.params.id;f.delete(x6),f6(E6);return}else{let x6=M.get(E6.method);if(x6)h6=x6.handler,R6=x6.type}if(h6||J)try{if(f6(E6),h6)if(E6.params===void 0){if(R6!==void 0){if(R6.numberOfParams!==0&&R6.parameterStructures!==D3.ParameterStructures.byName)Y.error(`Notification ${E6.method} defines ${R6.numberOfParams} params but received none.`)}h6()}else if(Array.isArray(E6.params)){let x6=E6.params;if(E6.method===y68.type.method&&x6.length===2&&lo1.is(x6[0]))h6({token:x6[0],value:x6[1]});else{if(R6!==void 0){if(R6.parameterStructures===D3.ParameterStructures.byName)Y.error(`Notification ${E6.method} defines parameters by name but received parameters by position`);if(R6.numberOfParams!==E6.params.length)Y.error(`Notification ${E6.method} defines ${R6.numberOfParams} params but received ${x6.length} arguments`)}h6(...x6)}}else{if(R6!==void 0&&R6.parameterStructures===D3.ParameterStructures.byPosition)Y.error(`Notification ${E6.method} defines parameters by position but received parameters by name`);h6(E6.params)}else if(J)J(E6.method,E6.params)}catch(x6){if(x6.message)Y.error(`Notification handler '${E6.method}' failed with message: ${x6.message}`);else Y.error(`Notification handler '${E6.method}' failed unexpectedly.`)}else R.fire(E6)}function a(E6){if(!E6){Y.error("Received empty message.");return}Y.error(`Received message which is neither a response nor a notification message:
2501${JSON.stringify(E6,null,4)}`);let R6=E6;if(BH.string(R6.id)||BH.number(R6.id)){let h6=R6.id,x6=D.get(h6);if(x6)x6.reject(Error("The received response has neither a result nor an error property."))}}function n(E6){if(E6===void 0||E6===null)return;switch(Z){case qA.Verbose:return JSON.stringify(E6,null,4);case qA.Compact:return JSON.stringify(E6);default:return}}function l(E6){if(Z===qA.Off||!v)return;if(T===KR.Text){let R6=void 0;if((Z===qA.Verbose||Z===qA.Compact)&&E6.params)R6=`Params: ${n(E6.params)}

Callers 2

tryReadHeadersMethod · 0.95
tryReadBodyMethod · 0.95

Calls 6

emptyBufferMethod · 0.80
asNativeMethod · 0.80
allocNativeMethod · 0.80
shiftMethod · 0.45
sliceMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected