@gjsify/http 0.3.19 → 0.3.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
import{IncomingMessage as e}from"./incoming-message.js";import{OutgoingMessage as t}from"./server.js";import n from"@girs/glib-2.0";import r from"@girs/soup-3.0";import i from"@girs/gio-2.0";import{Buffer as a}from"node:buffer";import{URL as o}from"node:url";import{readBytesAsync as s}from"@gjsify/utils";var
|
|
1
|
+
import{IncomingMessage as e}from"./incoming-message.js";import{OutgoingMessage as t}from"./server.js";import n from"@girs/glib-2.0";import r from"@girs/soup-3.0";import i from"@girs/gio-2.0";import{Buffer as a}from"node:buffer";import{URL as o}from"node:url";import{readBytesAsync as s}from"@gjsify/utils";var ClientRequest=class extends t{method;path;protocol;host;hostname;port;aborted=!1;reusedSocket=!1;maxHeadersCount=2e3;_chunks=[];_session;_message;_cancellable;_timeout=0;_timeoutTimer=null;_responseCallback;constructor(e,t,s){super();let c;if(typeof e==`string`||e instanceof o){let n=typeof e==`string`?new o(e):e;c={protocol:n.protocol,hostname:n.hostname,port:n.port?Number(n.port):void 0,path:n.pathname+n.search,...typeof t==`object`?t:{}},typeof t==`function`&&(s=t)}else c=e,typeof t==`function`&&(s=t);if(this.method=(c.method||`GET`).toUpperCase(),this.protocol=c.protocol||`http:`,this.hostname=c.hostname||c.host?.split(`:`)[0]||`localhost`,this.port=Number(c.port)||(this.protocol===`https:`?443:80),this.path=c.path||`/`,this.host=c.host||`${this.hostname}:${this.port}`,this._timeout=c.timeout||0,s&&(this._responseCallback=s,this.once(`response`,s)),c.headers)for(let[e,t]of Object.entries(c.headers))this.setHeader(e,t);if(c.setHost!==!1&&!this._headers.has(`host`)){let e=this.protocol===`https:`?443:80,t=this.port===e?this.hostname:`${this.hostname}:${this.port}`;this.setHeader(`Host`,t)}c.auth&&!this._headers.has(`authorization`)&&this.setHeader(`Authorization`,`Basic `+a.from(c.auth).toString(`base64`)),c.signal&&(c.signal.aborted?this.abort():c.signal.addEventListener(`abort`,()=>this.abort(),{once:!0}));let l=n.Uri.parse(this._buildUrl(),n.UriFlags.NONE);this._session=new r.Session,this._message=new r.Message({method:this.method,uri:l}),this._cancellable=new i.Cancellable,this._timeout>0&&(this._session.timeout=Math.ceil(this._timeout/1e3),this._timeoutTimer=setTimeout(()=>{this._timeoutTimer=null,this.emit(`timeout`)},this._timeout))}_buildUrl(){let e=this.protocol.endsWith(`:`)?this.protocol:this.protocol+`:`,t=e===`https:`?443:80,n=this.port===t?``:`:${this.port}`;return`${e}//${this.hostname}${n}${this.path}`}getRawHeaderNames(){return Array.from(this._headers.keys())}flushHeaders(){this.headersSent||this._applyHeaders()}setTimeout(e,t){return this._timeout=e,this._timeoutTimer&&=(clearTimeout(this._timeoutTimer),null),t&&this.once(`timeout`,t),e>0&&(this._session.timeout=Math.ceil(e/1e3),this._timeoutTimer=setTimeout(()=>{this._timeoutTimer=null,this.emit(`timeout`)},e)),this}abort(){this.aborted||(this.aborted=!0,this._timeoutTimer&&=(clearTimeout(this._timeoutTimer),null),this._cancellable.cancel(),this.emit(`abort`),this.destroy())}_write(e,t,n){let r=a.isBuffer(e)?e:a.from(e,t);this._chunks.push(r),n()}_final(e){this._sendRequest().then(()=>e()).catch(t=>e(t))}_applyHeaders(){if(this.headersSent)return;this.headersSent=!0;let e=this._message.get_request_headers();for(let[t,n]of this._headers)if(Array.isArray(n))for(let r of n)e.append(t,r);else e.replace(t,n)}async _sendRequest(){this._applyHeaders();let t=a.concat(this._chunks);if(t.length>0){let e=this._headers.get(`content-type`)||`application/octet-stream`;this._message.set_request_body_from_bytes(e,new n.Bytes(t))}try{let t=await new Promise((e,t)=>{this._session.send_async(this._message,n.PRIORITY_DEFAULT,this._cancellable,(n,r)=>{try{e(this._session.send_finish(r))}catch(e){t(e)}})}),r=[];try{let e;for(;(e=await s(t,4096,n.PRIORITY_DEFAULT,this._cancellable))!==null;)r.push(a.from(e))}catch{}let i=new e;i.statusCode=this._message.status_code,i.statusMessage=this._message.get_reason_phrase(),i.httpVersion=`1.1`,this._message.get_response_headers().foreach((e,t)=>{let n=e.toLowerCase();if(i.rawHeaders.push(e,t),n in i.headers){let e=i.headers[n];Array.isArray(e)?e.push(t):i.headers[n]=[e,t]}else i.headers[n]=t}),this.finished=!0,this._timeoutTimer&&=(clearTimeout(this._timeoutTimer),null),this.emit(`response`,i),setTimeout(()=>{for(let e of r)i.push(e);i.push(null),i.complete=!0},0)}catch(e){this.aborted?this.emit(`abort`):this.emit(`error`,e instanceof Error?e:Error(String(e)))}}};export{ClientRequest};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Buffer as e}from"node:buffer";import{Readable as t}from"node:stream";var
|
|
1
|
+
import{Buffer as e}from"node:buffer";import{Readable as t}from"node:stream";var IncomingMessage=class extends t{httpVersion=`1.1`;httpVersionMajor=1;httpVersionMinor=1;headers={};rawHeaders=[];method;url;statusCode;statusMessage;complete=!1;socket=null;aborted=!1;get connection(){return this.socket}_timeoutTimer=null;constructor(){super()}_read(e){}_autoClose(){}_pushBody(t){t&&t.length>0&&this.push(e.from(t)),this.push(null),this.complete=!0,this._timeoutTimer&&=(clearTimeout(this._timeoutTimer),null)}setTimeout(e,t){return this._timeoutTimer&&=(clearTimeout(this._timeoutTimer),null),t&&this.once(`timeout`,t),e>0&&(this._timeoutTimer=setTimeout(()=>{this._timeoutTimer=null,this.emit(`timeout`)},e)),this}destroy(e){return this._timeoutTimer&&=(clearTimeout(this._timeoutTimer),null),this.aborted=!0,super.destroy(e)}};export{IncomingMessage};
|
package/lib/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{METHODS as e,STATUS_CODES as t}from"./constants.js";import{IncomingMessage as n}from"./incoming-message.js";import{OutgoingMessage as r,Server as i,ServerResponse as a}from"./server.js";import{ClientRequest as o}from"./client-request.js";import{validateHeaderName as s,validateHeaderValue as c}from"./validators.js";import{URL as l}from"node:url";var
|
|
1
|
+
import{METHODS as e,STATUS_CODES as t}from"./constants.js";import{IncomingMessage as n}from"./incoming-message.js";import{OutgoingMessage as r,Server as i,ServerResponse as a}from"./server.js";import{ClientRequest as o}from"./client-request.js";import{validateHeaderName as s,validateHeaderValue as c}from"./validators.js";import{URL as l}from"node:url";var Agent=class{defaultPort=80;protocol=`http:`;maxSockets;maxTotalSockets;maxFreeSockets;keepAliveMsecs;keepAlive;scheduling;requests={};sockets={};freeSockets={};constructor(e){this.keepAlive=e?.keepAlive??!1,this.keepAliveMsecs=e?.keepAliveMsecs??1e3,this.maxSockets=e?.maxSockets??1/0,this.maxTotalSockets=e?.maxTotalSockets??1/0,this.maxFreeSockets=e?.maxFreeSockets??256,this.scheduling=e?.scheduling??`lifo`}destroy(){}getName(e){let t=e.host||`localhost`;return e.port&&(t+=`:`+e.port),e.localAddress&&(t+=`:`+e.localAddress),(e.family===4||e.family===6)&&(t+=`:`+e.family),t}};const u=new Agent;function createServer(e,t){return typeof e==`function`?new i(e):new i(t)}function request(e,t,n){return new o(e,t,n)}function get(e,t,n){let r,i=n;typeof e==`string`||e instanceof l?(r=typeof t==`object`?{...t,method:`GET`}:{method:`GET`},typeof t==`function`&&(i=t)):(r={...e,method:`GET`},typeof t==`function`&&(i=t),e=r);let a=typeof e==`string`||e instanceof l?new o(e,{...r,method:`GET`},i):new o({...r,method:`GET`},i);return a.end(),a}const d=16384;function setMaxIdleHTTPParsers(e){}var f={STATUS_CODES:t,METHODS:e,Server:i,IncomingMessage:n,OutgoingMessage:r,ServerResponse:a,ClientRequest:o,Agent,globalAgent:u,createServer,request,get,validateHeaderName:s,validateHeaderValue:c,maxHeaderSize:d,setMaxIdleHTTPParsers};export{Agent,o as ClientRequest,n as IncomingMessage,e as METHODS,r as OutgoingMessage,t as STATUS_CODES,i as Server,a as ServerResponse,createServer,f as default,get,u as globalAgent,d as maxHeaderSize,request,setMaxIdleHTTPParsers,s as validateHeaderName,c as validateHeaderValue};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Duplex as e}from"node:stream";var
|
|
1
|
+
import{Duplex as e}from"node:stream";var ServerRequestSocket=class extends e{remoteAddress;remotePort;localAddress;localPort;remoteFamily=`IPv4`;encrypted;connecting=!1;pending=!1;bytesRead=0;bytesWritten=0;_bridgeRes;_bridgePaused=!1;constructor(e,t,n,r,i,a=!1){super({allowHalfOpen:!0}),this.remoteAddress=e,this.remotePort=t,this.localAddress=n,this.localPort=r,this.encrypted=a,this._bridgeRes=i}pause(){return this._bridgePaused?this:(this._bridgePaused=!0,super.pause())}resume(){return this._bridgePaused&&=!1,super.resume()}_read(e){}_write(e,t,n){n()}destroySoon(){this.writableEnded||this.end(),this.writableFinished?this.destroy():this.once(`finish`,()=>this.destroy())}setTimeout(e,t){return t&&this.once(`timeout`,t),this}setNoDelay(e){return this}setKeepAlive(e,t){return this}ref(){return this}unref(){return this}address(){return{address:this.localAddress,family:`IPv4`,port:this.localPort}}};export{ServerRequestSocket};
|
package/lib/esm/server.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ServerRequestSocket as e}from"./server-request-socket.js";import{STATUS_CODES as t}from"./constants.js";import{IncomingMessage as n}from"./incoming-message.js";import{Buffer as r}from"node:buffer";import{createNodeError as i,deferEmit as a,ensureMainLoop as o}from"@gjsify/utils";import{EventEmitter as s}from"node:events";import{Writable as c}from"node:stream";import{Socket as l}from"@gjsify/net/socket";import{Server as u}from"@gjsify/http-soup-bridge";var
|
|
1
|
+
import{ServerRequestSocket as e}from"./server-request-socket.js";import{STATUS_CODES as t}from"./constants.js";import{IncomingMessage as n}from"./incoming-message.js";import{Buffer as r}from"node:buffer";import{createNodeError as i,deferEmit as a,ensureMainLoop as o}from"@gjsify/utils";import{EventEmitter as s}from"node:events";import{Writable as c}from"node:stream";import{Socket as l}from"@gjsify/net/socket";import{Server as u}from"@gjsify/http-soup-bridge";var OutgoingMessage=class extends c{headersSent=!1;sendDate=!0;finished=!1;socket=null;_headers=new Map;setHeader(e,t){return this._headers.set(e.toLowerCase(),typeof t==`number`?String(t):t),this}getHeader(e){return this._headers.get(e.toLowerCase())}removeHeader(e){this._headers.delete(e.toLowerCase())}hasHeader(e){return this._headers.has(e.toLowerCase())}getHeaderNames(){return Array.from(this._headers.keys())}getHeaders(){let e={};for(let[t,n]of this._headers)e[t]=n;return e}appendHeader(e,t){let n=e.toLowerCase(),r=this._headers.get(n);return r===void 0?this._headers.set(n,t):Array.isArray(r)?Array.isArray(t)?r.push(...t):r.push(t):Array.isArray(t)?this._headers.set(n,[r,...t]):this._headers.set(n,[r,t]),this}flushHeaders(){this.headersSent=!0}_write(e,t,n){n()}},ServerResponse=class extends OutgoingMessage{statusCode=200;statusMessage=``;_streaming=!1;_bridge;_timeoutTimer=null;constructor(e){super(),this._bridge=e,e.connect(`close`,()=>{this.emit(`close`)})}setTimeout(e,t){return this._timeoutTimer&&=(clearTimeout(this._timeoutTimer),null),t&&this.once(`timeout`,t),e>0&&(this._timeoutTimer=setTimeout(()=>{this._timeoutTimer=null,this.emit(`timeout`)},e)),this}writeHead(e,n,r){if(this.statusCode=e,typeof n==`object`&&(r=n,n=void 0),this.statusMessage=n||t[e]||``,r)for(let[e,t]of Object.entries(r))this.setHeader(e,t);return this}writeContinue(e){e&&Promise.resolve().then(e)}writeProcessing(e){e&&Promise.resolve().then(e)}flushHeaders(){this.headersSent||=!0}addTrailers(e){for(let[t,n]of Object.entries(e))this._headers.set(`trailer-`+t.toLowerCase(),n)}_startStreaming(){if(!this._streaming){this._streaming=!0,this.headersSent=!0,this._timeoutTimer&&=(clearTimeout(this._timeoutTimer),null);for(let[e,t]of this._headers)if(Array.isArray(t))for(let n of t)this._bridge.append_header(e,n);else this._bridge.set_header(e,t);this._bridge.write_head(this.statusCode,this.statusMessage||null)}}_write(e,t,n){let i=r.isBuffer(e)?e:r.from(e,t);this._startStreaming(),this._bridge.write_chunk(new Uint8Array(i.buffer,i.byteOffset,i.byteLength)),n()}_final(e){if(!this._streaming){for(let[e,t]of this._headers)if(Array.isArray(t))for(let n of t)this._bridge.append_header(e,n);else this._bridge.set_header(e,t);this._bridge.write_head(this.statusCode,this.statusMessage||null)}this._bridge.end(),this.finished=!0,e()}end(e,t,n){return typeof e==`function`?(n=e,e=void 0):typeof t==`function`&&(n=t,t=void 0),e!=null&&this.write(e,t),super.end(n),this}};const d=new Set;var Server=class extends s{listening=!1;maxHeadersCount=2e3;timeout=0;keepAliveTimeout=5e3;headersTimeout=6e4;requestTimeout=3e5;_bridge=null;_address=null;get soupServer(){return this._bridge?.soup_server??null}constructor(e,t){super();let n=typeof e==`function`?e:t;n&&this.on(`request`,n)}listen(...e){let t=0,n=`0.0.0.0`,r;for(let i of e)typeof i==`number`?t=i:typeof i==`string`?n=i:typeof i==`function`&&(r=i);r&&this.once(`listening`,r);try{this._bridge=new u,this._bridge.connect(`request-received`,(e,t,n)=>{this._handleRequest(t,n)}),this._bridge.connect(`upgrade`,(e,t,n,r)=>{this._handleUpgrade(t,n)}),this._bridge.connect(`error-occurred`,(e,t)=>{this.emit(`error`,Error(t))}),this._bridge.listen(t,n),o(),this.listening=!0,this._address={port:this._bridge.port,family:`IPv4`,address:this._bridge.address||n},d.add(this),a(this,`listening`)}catch(e){let r=i(e,`listen`,{address:n,port:t});a(this,`error`,r)}return this}_handleRequest(t,r){let i=new n,a=new ServerResponse(r);i.method=t.method,i.url=t.url,i.httpVersion=`1.1`;let o=t.header_pairs??[];for(let e=0;e+1<o.length;e+=2){let t=o[e],n=o[e+1],r=t.toLowerCase();if(i.rawHeaders.push(t,n),r in i.headers){let e=i.headers[r];Array.isArray(e)?e.push(n):i.headers[r]=[e,n]}else i.headers[r]=n}i.socket=new e(t.remote_address??`127.0.0.1`,t.remote_port??0,this._address?.address??`127.0.0.1`,this._address?.port??0,r);let s=t.get_body();s.length>0?i._pushBody(s):i._pushBody(null),t.connect(`aborted_signal`,()=>{i.aborted||(i.aborted=!0,i.emit(`aborted`))}),t.connect(`close`,()=>{i.emit(`close`)});try{let e=this.emit(`request`,i,a);(e instanceof Promise||typeof e==`object`&&e&&typeof e.then==`function`)&&e.catch(e=>{if(console.error(`[HTTP] Unhandled error in async request handler:`,e),!a.headersSent)try{a.writeHead(500),a.end(`Internal Server Error`)}catch{}})}catch(e){if(console.error(`[HTTP] Unhandled error in request handler:`,e),!a.headersSent)try{a.writeHead(500),a.end(`Internal Server Error`)}catch{}}}_handleUpgrade(e,t){let i=new n;i.method=e.method,i.url=e.url,i.httpVersion=`1.1`;let a=e.header_pairs??[];for(let e=0;e+1<a.length;e+=2){let t=a[e],n=a[e+1],r=t.toLowerCase();i.rawHeaders.push(t,n),i.headers[r]=n}if(this.listenerCount(`upgrade`)>0){let e=new l;e._attachOutputOnly(t),this.emit(`upgrade`,i,e,r.alloc(0))}}address(){return this._address}addWebSocketHandler(e,t){if(!this._bridge)throw Error(`Server must be listening before adding WebSocket handlers. Call listen() first.`);this._bridge.soup_server.add_websocket_handler(e,null,null,(e,n,r,i)=>{t(i)})}close(e){return e&&this.once(`close`,e),this._bridge&&=(this._bridge.close(),null),this.listening=!1,d.delete(this),a(this,`close`),this}setTimeout(e,t){return this.timeout=e,t&&this.on(`timeout`,t),this}};export{OutgoingMessage,Server,ServerResponse};
|
package/lib/esm/validators.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
function validateHeaderName(e){if(typeof e!=`string`||!/^[\^`\-\w!#$%&'*+.|~]+$/.test(e)){let t=TypeError(`Header name must be a valid HTTP token ["${e}"]`);throw Object.defineProperty(t,`code`,{value:`ERR_INVALID_HTTP_TOKEN`}),t}}function validateHeaderValue(e,t){if(t===void 0){let t=TypeError(`Header "${e}" value must not be undefined`);throw Object.defineProperty(t,`code`,{value:`ERR_HTTP_INVALID_HEADER_VALUE`}),t}if(typeof t==`string`&&/[^\t -~-ÿ]/.test(t)){let t=TypeError(`Invalid character in header content ["${e}"]`);throw Object.defineProperty(t,`code`,{value:`ERR_INVALID_CHAR`}),t}}export{validateHeaderName,validateHeaderValue};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/http",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.21",
|
|
4
4
|
"description": "Node.js http module for Gjs",
|
|
5
5
|
"module": "lib/esm/index.js",
|
|
6
6
|
"types": "lib/types/index.d.ts",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"http"
|
|
35
35
|
],
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@gjsify/cli": "^0.3.
|
|
38
|
-
"@gjsify/unit": "^0.3.
|
|
37
|
+
"@gjsify/cli": "^0.3.21",
|
|
38
|
+
"@gjsify/unit": "^0.3.21",
|
|
39
39
|
"@types/node": "^25.6.2",
|
|
40
40
|
"typescript": "^6.0.3"
|
|
41
41
|
},
|
|
@@ -43,12 +43,12 @@
|
|
|
43
43
|
"@girs/gio-2.0": "2.88.0-4.0.0-rc.14",
|
|
44
44
|
"@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
|
|
45
45
|
"@girs/soup-3.0": "3.6.6-4.0.0-rc.14",
|
|
46
|
-
"@gjsify/buffer": "^0.3.
|
|
47
|
-
"@gjsify/events": "^0.3.
|
|
48
|
-
"@gjsify/http-soup-bridge": "^0.3.
|
|
49
|
-
"@gjsify/net": "^0.3.
|
|
50
|
-
"@gjsify/stream": "^0.3.
|
|
51
|
-
"@gjsify/url": "^0.3.
|
|
52
|
-
"@gjsify/utils": "^0.3.
|
|
46
|
+
"@gjsify/buffer": "^0.3.21",
|
|
47
|
+
"@gjsify/events": "^0.3.21",
|
|
48
|
+
"@gjsify/http-soup-bridge": "^0.3.21",
|
|
49
|
+
"@gjsify/net": "^0.3.21",
|
|
50
|
+
"@gjsify/stream": "^0.3.21",
|
|
51
|
+
"@gjsify/url": "^0.3.21",
|
|
52
|
+
"@gjsify/utils": "^0.3.21"
|
|
53
53
|
}
|
|
54
54
|
}
|