Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tomphttp/bare-client

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tomphttp/bare-client - npm Package Compare versions

Comparing version 1.0.1-beta to 1.0.2-beta

scripts/prepare.js

41

dist/BareClient.d.ts

@@ -45,20 +45,23 @@ export * from './Client';

};
export declare type BareClientData = {
maintainer?: {
email?: string;
website?: string;
};
project?: {
name?: string;
description?: string;
email?: string;
website?: string;
repository?: string;
};
export declare type BareMaintainer = {
email?: string;
website?: string;
};
export declare type BareProject = {
name?: string;
description?: string;
email?: string;
website?: string;
repository?: string;
};
export declare type BareLanguage = 'JS' | 'TS' | 'Java' | 'PHP' | 'Rust' | 'C' | 'C++' | 'C#' | 'Ruby' | 'Go' | 'Crystal' | 'Bash' | string;
export declare type BareManifest = {
maintainer?: BareMaintainer;
project?: BareProject;
versions: string[];
language: 'JS' | 'TS' | 'Java' | 'PHP' | 'Rust' | 'C' | 'C++' | 'C#' | 'Ruby' | 'Go' | 'Crystal' | 'Bash' | string;
language: BareLanguage;
memoryUsage?: number;
};
export default class BareClient {
data: BareClientData | undefined;
data: BareManifest | undefined;
private client;

@@ -72,3 +75,3 @@ private server;

*/
constructor(server: string | URL, data?: BareClientData);
constructor(server: string | URL, data?: BareManifest);
private loadData;

@@ -78,3 +81,11 @@ private work;

connect(requestHeaders: BareHeaders, protocol: BareWSProtocol, host: string, port: string | number, path: string): Promise<BareWebSocket>;
/**
*
* @param url
* @param protocols
* @param origin Location of client that created the WebSocket
* @returns
*/
createWebSocket(url: urlLike, headers?: BareHeaders, protocols?: string | string[]): Promise<BareWebSocket>;
fetch(url: urlLike, init?: BareFetchInit): Promise<BareResponseFetch>;
}

@@ -30,2 +30,13 @@ const statusEmpty = [101, 204, 205, 304];

const reserveChar = '%';
function validProtocol(protocol) {
for (let i = 0; i < protocol.length; i++) {
const char = protocol[i];
if (!validChars.includes(char)) {
return false;
}
}
return true;
}
function encodeProtocol(protocol) {

@@ -604,2 +615,9 @@ let result = '';

const MAX_HEADER_VALUE = 3072;
/**
*
* Splits headers according to spec
* @param headers
* @returns Split headers
*/
function splitHeaders(headers) {

@@ -626,3 +644,5 @@ const output = new Headers(headers);

/**
* Joins headers in object, according to spec
* Joins headers according to spec
* @param headers
* @returns Joined headers
*/

@@ -808,6 +828,3 @@

// Implements the protocol for requesting bare data from a server
const clientCtors = {
v1: ClientV1,
v2: ClientV2
};
const clientCtors = [['v2', ClientV2], ['v1', ClientV1]];
const maxRedirects = 20;

@@ -837,5 +854,3 @@ class BareClient {

for (const version in clientCtors) {
const ctor = clientCtors[version];
for (const [version, ctor] of clientCtors) {
if (data.versions.includes(version)) {

@@ -879,3 +894,42 @@ this.client = new ctor(this.server);

}
/**
*
* @param url
* @param protocols
* @param origin Location of client that created the WebSocket
* @returns
*/
async createWebSocket(url, headers = {}, protocols = []) {
const requestHeaders = headers instanceof Headers ? Object.fromEntries(headers) : headers;
url = new URL(url); // user is expected to specify user-agent and origin
// both are in spec
requestHeaders['Host'] = url.host; // requestHeaders['Origin'] = origin;
requestHeaders['Pragma'] = 'no-cache';
requestHeaders['Cache-Control'] = 'no-cache';
requestHeaders['Upgrade'] = 'websocket'; // requestHeaders['User-Agent'] = navigator.userAgent;
requestHeaders['Connection'] = 'Upgrade';
if (typeof protocols === 'string') {
protocols = [protocols];
}
for (const proto of protocols) {
if (!validProtocol(proto)) {
throw new DOMException(`Failed to construct 'WebSocket': The subprotocol '${proto}' is invalid.`);
}
}
if (protocols.length) {
headers['Sec-Websocket-Protocol'] = protocols.join(', ');
}
await this.work();
return this.client.connect(headers, url.protocol, url.hostname, url.port, url.pathname + url.search);
}
async fetch(url, init = {}) {

@@ -882,0 +936,0 @@ url = new URL(url);

@@ -36,2 +36,13 @@ (function (global, factory) {

const reserveChar = '%';
function validProtocol(protocol) {
for (let i = 0; i < protocol.length; i++) {
const char = protocol[i];
if (!validChars.includes(char)) {
return false;
}
}
return true;
}
function encodeProtocol(protocol) {

@@ -610,2 +621,9 @@ let result = '';

const MAX_HEADER_VALUE = 3072;
/**
*
* Splits headers according to spec
* @param headers
* @returns Split headers
*/
function splitHeaders(headers) {

@@ -632,3 +650,5 @@ const output = new Headers(headers);

/**
* Joins headers in object, according to spec
* Joins headers according to spec
* @param headers
* @returns Joined headers
*/

@@ -814,6 +834,3 @@

// Implements the protocol for requesting bare data from a server
const clientCtors = {
v1: ClientV1,
v2: ClientV2
};
const clientCtors = [['v2', ClientV2], ['v1', ClientV1]];
const maxRedirects = 20;

@@ -843,5 +860,3 @@ class BareClient {

for (const version in clientCtors) {
const ctor = clientCtors[version];
for (const [version, ctor] of clientCtors) {
if (data.versions.includes(version)) {

@@ -885,3 +900,42 @@ this.client = new ctor(this.server);

}
/**
*
* @param url
* @param protocols
* @param origin Location of client that created the WebSocket
* @returns
*/
async createWebSocket(url, headers = {}, protocols = []) {
const requestHeaders = headers instanceof Headers ? Object.fromEntries(headers) : headers;
url = new URL(url); // user is expected to specify user-agent and origin
// both are in spec
requestHeaders['Host'] = url.host; // requestHeaders['Origin'] = origin;
requestHeaders['Pragma'] = 'no-cache';
requestHeaders['Cache-Control'] = 'no-cache';
requestHeaders['Upgrade'] = 'websocket'; // requestHeaders['User-Agent'] = navigator.userAgent;
requestHeaders['Connection'] = 'Upgrade';
if (typeof protocols === 'string') {
protocols = [protocols];
}
for (const proto of protocols) {
if (!validProtocol(proto)) {
throw new DOMException(`Failed to construct 'WebSocket': The subprotocol '${proto}' is invalid.`);
}
}
if (protocols.length) {
headers['Sec-Websocket-Protocol'] = protocols.join(', ');
}
await this.work();
return this.client.connect(headers, url.protocol, url.hostname, url.port, url.pathname + url.search);
}
async fetch(url, init = {}) {

@@ -888,0 +942,0 @@ url = new URL(url);

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).BareClient=t()}(this,(function(){"use strict";const e=[101,204,205,304],t=[301,302,303,307,308];class s extends Error{status;body;constructor(e,t){super(t.message||t.code),this.status=e,this.body=t}}class a{base;constructor(e,t){this.base=new URL(`./v${e}/`,t)}}function r(e){let t="";for(let s=0;s<e.length;s++){const a=e[s];if("!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~".includes(a)&&"%"!==a)t+=a;else{t+="%"+a.charCodeAt(0).toString(16).padStart(2,"0")}}return t}function n(e,t){const s=(65535&e)+(65535&t);return(e>>16)+(t>>16)+(s>>16)<<16|65535&s}function o(e,t,s,a,r,o){return n((i=n(n(t,e),n(a,o)))<<(c=r)|i>>>32-c,s);var i,c}function i(e,t,s,a,r,n,i){return o(t&s|~t&a,e,t,r,n,i)}function c(e,t,s,a,r,n,i){return o(t&a|s&~a,e,t,r,n,i)}function h(e,t,s,a,r,n,i){return o(t^s^a,e,t,r,n,i)}function d(e,t,s,a,r,n,i){return o(s^(t|~a),e,t,r,n,i)}function w(e,t){e[t>>5]|=128<<t%32,e[14+(t+64>>>9<<4)]=t;let s=1732584193,a=-271733879,r=-1732584194,o=271733878;for(let t=0;t<e.length;t+=16){const w=s,u=a,f=r,l=o;s=i(s,a,r,o,e[t],7,-680876936),o=i(o,s,a,r,e[t+1],12,-389564586),r=i(r,o,s,a,e[t+2],17,606105819),a=i(a,r,o,s,e[t+3],22,-1044525330),s=i(s,a,r,o,e[t+4],7,-176418897),o=i(o,s,a,r,e[t+5],12,1200080426),r=i(r,o,s,a,e[t+6],17,-1473231341),a=i(a,r,o,s,e[t+7],22,-45705983),s=i(s,a,r,o,e[t+8],7,1770035416),o=i(o,s,a,r,e[t+9],12,-1958414417),r=i(r,o,s,a,e[t+10],17,-42063),a=i(a,r,o,s,e[t+11],22,-1990404162),s=i(s,a,r,o,e[t+12],7,1804603682),o=i(o,s,a,r,e[t+13],12,-40341101),r=i(r,o,s,a,e[t+14],17,-1502002290),a=i(a,r,o,s,e[t+15],22,1236535329),s=c(s,a,r,o,e[t+1],5,-165796510),o=c(o,s,a,r,e[t+6],9,-1069501632),r=c(r,o,s,a,e[t+11],14,643717713),a=c(a,r,o,s,e[t],20,-373897302),s=c(s,a,r,o,e[t+5],5,-701558691),o=c(o,s,a,r,e[t+10],9,38016083),r=c(r,o,s,a,e[t+15],14,-660478335),a=c(a,r,o,s,e[t+4],20,-405537848),s=c(s,a,r,o,e[t+9],5,568446438),o=c(o,s,a,r,e[t+14],9,-1019803690),r=c(r,o,s,a,e[t+3],14,-187363961),a=c(a,r,o,s,e[t+8],20,1163531501),s=c(s,a,r,o,e[t+13],5,-1444681467),o=c(o,s,a,r,e[t+2],9,-51403784),r=c(r,o,s,a,e[t+7],14,1735328473),a=c(a,r,o,s,e[t+12],20,-1926607734),s=h(s,a,r,o,e[t+5],4,-378558),o=h(o,s,a,r,e[t+8],11,-2022574463),r=h(r,o,s,a,e[t+11],16,1839030562),a=h(a,r,o,s,e[t+14],23,-35309556),s=h(s,a,r,o,e[t+1],4,-1530992060),o=h(o,s,a,r,e[t+4],11,1272893353),r=h(r,o,s,a,e[t+7],16,-155497632),a=h(a,r,o,s,e[t+10],23,-1094730640),s=h(s,a,r,o,e[t+13],4,681279174),o=h(o,s,a,r,e[t],11,-358537222),r=h(r,o,s,a,e[t+3],16,-722521979),a=h(a,r,o,s,e[t+6],23,76029189),s=h(s,a,r,o,e[t+9],4,-640364487),o=h(o,s,a,r,e[t+12],11,-421815835),r=h(r,o,s,a,e[t+15],16,530742520),a=h(a,r,o,s,e[t+2],23,-995338651),s=d(s,a,r,o,e[t],6,-198630844),o=d(o,s,a,r,e[t+7],10,1126891415),r=d(r,o,s,a,e[t+14],15,-1416354905),a=d(a,r,o,s,e[t+5],21,-57434055),s=d(s,a,r,o,e[t+12],6,1700485571),o=d(o,s,a,r,e[t+3],10,-1894986606),r=d(r,o,s,a,e[t+10],15,-1051523),a=d(a,r,o,s,e[t+1],21,-2054922799),s=d(s,a,r,o,e[t+8],6,1873313359),o=d(o,s,a,r,e[t+15],10,-30611744),r=d(r,o,s,a,e[t+6],15,-1560198380),a=d(a,r,o,s,e[t+13],21,1309151649),s=d(s,a,r,o,e[t+4],6,-145523070),o=d(o,s,a,r,e[t+11],10,-1120210379),r=d(r,o,s,a,e[t+2],15,718787259),a=d(a,r,o,s,e[t+9],21,-343485551),s=n(s,w),a=n(a,u),r=n(r,f),o=n(o,l)}return[s,a,r,o]}function u(e){let t="";const s=32*e.length;for(let a=0;a<s;a+=8)t+=String.fromCharCode(e[a>>5]>>>a%32&255);return t}function f(e){const t=[],s=e.length>>2;for(let e=0;e<s;e+=1)t[e]=0;const a=8*e.length;for(let s=0;s<a;s+=8)t[s>>5]|=(255&e.charCodeAt(s/8))<<s%32;return t}function l(e){const t="0123456789abcdef";let s="";for(let a=0;a<e.length;a+=1){const r=e.charCodeAt(a);s+=t.charAt(r>>>4&15)+t.charAt(15&r)}return s}function b(e){return unescape(encodeURIComponent(e))}function p(e){return function(e){return u(w(f(e),8*e.length))}(b(e))}function g(e,t){return function(e,t){let s=f(e);const a=[],r=[];s.length>16&&(s=w(s,8*e.length));for(let e=0;e<16;e+=1)a[e]=909522486^s[e],r[e]=1549556828^s[e];const n=w(a.concat(f(t)),512+8*t.length);return u(w(r.concat(n),640))}(b(e),b(t))}function x(e,t,s){return t?s?g(t,e):l(g(t,e)):s?p(e):l(p(e))}const y=3072;const R={v1:class extends a{ws;http;newMeta;getMeta;constructor(e){super(1,e),this.ws=new URL(this.base),this.http=new URL(this.base),this.newMeta=new URL("ws-new-meta",this.base),this.getMeta=new URL("ws-meta",this.base),"https:"===this.ws.protocol?this.ws.protocol="wss:":this.ws.protocol="ws:"}async connect(e,t,a,n,o){const i=await fetch(this.newMeta,{method:"GET"});if(!i.ok)throw new s(i.status,await i.json());const c=await i.text(),h=new WebSocket(this.ws,["bare",r(JSON.stringify({remote:{protocol:t,host:a,port:n,path:o},headers:e,forward_headers:["accept-encoding","accept-language","sec-websocket-extensions","sec-websocket-key","sec-websocket-version"],id:c}))]);return h.meta=new Promise(((e,t)=>{h.addEventListener("open",(async()=>{const a=await fetch(this.getMeta,{headers:{"x-bare-id":c},method:"GET"});a.ok||t(new s(a.status,await a.json())),e(await a.json())})),h.addEventListener("error",t)})),h}async request(t,s,a,r,n,o,i,c,h){if(r.startsWith("blob:")){const e=await fetch(`blob:${location.origin}${i}`),t=new Response(e.body,e);return t.rawHeaders=Object.fromEntries(e.headers),t.rawResponse=e,t}const d={};if(s instanceof Headers)for(const[e,t]of s)d[e]=t;else for(const e in s)d[e]=s[e];const w={credentials:"omit",method:t,signal:h};void 0!==a&&(w.body=a);const u=new Request(this.http,w);this.writeBareRequest(u,r,n,i,o,d,["accept-encoding","accept-language"]);const f=await fetch(u),l=await this.readBareResponse(f),b=new Response(e.includes(l.status)?void 0:f.body,{status:l.status,statusText:l.statusText??void 0,headers:l.headers});return b.rawHeaders=l.rawHeaders,b.rawResponse=f,b}async readBareResponse(e){if(!e.ok)throw new s(e.status,await e.json());const t=["x-bare-status","x-bare-status-text","x-bare-headers"];for(const a of t)if(!e.headers.has(a))throw new s(500,{code:"IMPL_MISSING_BARE_HEADER",id:`response.headers.${a}`});const a=parseInt(e.headers.get("x-bare-status")),r=e.headers.get("x-bare-status-text"),n=JSON.parse(e.headers.get("x-bare-headers"));return{status:a,statusText:r,rawHeaders:n,headers:new Headers(n)}}writeBareRequest(e,t,s,a,r,n,o){e.headers.set("x-bare-protocol",t),e.headers.set("x-bare-host",s),e.headers.set("x-bare-path",a),e.headers.set("x-bare-port",r.toString()),e.headers.set("x-bare-headers",JSON.stringify(n)),e.headers.set("x-bare-forward-headers",JSON.stringify(o))}},v2:class extends a{ws;http;newMeta;getMeta;constructor(e){super(2,e),this.ws=new URL(this.base),this.http=new URL(this.base),this.newMeta=new URL("./ws-new-meta",this.base),this.getMeta=new URL("./ws-meta",this.base),"https:"===this.ws.protocol?this.ws.protocol="wss:":this.ws.protocol="ws:"}async connect(e,t,a,r,n){const o=new Request(this.newMeta,{headers:this.createBareHeaders(t,a,n,r,e)}),i=await fetch(o);if(!i.ok)throw new s(i.status,await i.json());const c=await i.text(),h=new WebSocket(this.ws,[c]);return h.meta=new Promise(((e,t)=>{h.addEventListener("open",(async()=>{const t=await fetch(this.getMeta,{headers:{"x-bare-id":c},method:"GET"});e(await await this.readBareResponse(t))})),h.addEventListener("error",t)})),h}async request(t,s,a,r,n,o,i,c,h){if(r.startsWith("blob:")){const e=await fetch(`blob:${location.origin}${i}`),t=new Response(e.body,e);return t.rawHeaders=Object.fromEntries(e.headers),t.rawResponse=e,t}const d={};if(s instanceof Headers)for(const[e,t]of s)d[e]=t;else for(const e in s)d[e]=s[e];const w={credentials:"omit",method:t,signal:h};"only-if-cached"!==c&&(w.cache=c),void 0!==a&&(w.body=a),w.headers=this.createBareHeaders(r,n,i,o,d);const u=new Request(this.http+"?cache="+x(`${r}${n}${o}${i}`),w),f=await fetch(u),l=await this.readBareResponse(f),b=new Response(e.includes(l.status)?void 0:f.body,{status:l.status,statusText:l.statusText??void 0,headers:l.headers});return b.rawHeaders=l.rawHeaders,b.rawResponse=f,b}async readBareResponse(e){if(!e.ok)throw new s(e.status,await e.json());const t=function(e){const t=new Headers(e),a="x-bare-headers";if(e.has(`${a}-0`)){const r=[];for(const[n,o]of e)if(n.startsWith(a)){if(!o.startsWith(";"))throw new s(400,{code:"INVALID_BARE_HEADER",id:`request.headers.${n}`,message:"Value didn't begin with semi-colon."});r[parseInt(n.slice(a.length+1))]=o.slice(1),t.delete(n)}t.set(a,r.join(""))}return t}(e.headers),a={};return t.has("x-bare-status")&&(a.status=parseInt(t.get("x-bare-status"))),t.has("x-bare-status-text")&&(a.statusText=t.get("x-bare-status-text")),t.has("x-bare-headers")&&(a.rawHeaders=JSON.parse(t.get("x-bare-headers")),a.headers=new Headers(a.rawHeaders)),a}createBareHeaders(e,t,s,a,r,n=[],o=[],i=[]){const c=new Headers;c.set("x-bare-protocol",e),c.set("x-bare-host",t),c.set("x-bare-path",s),c.set("x-bare-port",a.toString()),c.set("x-bare-headers",JSON.stringify(r));for(const e of n)c.append("x-bare-forward-headers",e);for(const e of o)c.append("x-bare-pass-headers",e);for(const e of i)c.append("x-bare-pass-status",e.toString());return function(e){const t=new Headers(e);if(e.has("x-bare-headers")){const s=e.get("x-bare-headers");if(s.length>y){t.delete("x-bare-headers");let e=0;for(let a=0;a<s.length;a+=y){const r=s.slice(a,a+y),n=e++;t.set(`x-bare-headers-${n}`,`;${r}`)}}}}(c),c}}};return class{data;client;server;ready;constructor(e,t){this.server=new URL(e),this.ready=!1,"object"==typeof t&&this.loadData(t)}loadData(e){let t=!1;for(const s in R){const a=R[s];if(e.versions.includes(s)){this.client=new a(this.server),t=!0;break}}if(!t)throw new Error("Unable to find compatible client version.");this.data=e,this.ready=!0}async work(){if(!0===this.ready)return;const e=await fetch(this.server);if(!e.ok)throw new Error(`Unable to fetch Bare meta: ${e.status} ${await e.text()}`);this.loadData(await e.json())}async request(e,t,s,a,r,n,o,i,c){return await this.work(),await this.client.request(e,t,s,a,r,n,o,i,c)}async connect(e,t,s,a,r){return await this.work(),this.client.connect(e,t,s,a,r)}async fetch(e,s={}){let a,r,n,o,i;e=new URL(e),a="string"==typeof s.method?s.method:"GET",void 0!==s.body&&null!==s.body&&(r=s.body),n="object"==typeof s.headers&&null!==s.headers?s.headers instanceof Headers?Object.fromEntries(s.headers):s.headers:{},o="string"==typeof s.cache?s.cache:"default",s.signal instanceof AbortSignal&&(i=s.signal);for(let c=0;;c++){let h;h=""===e.port?"https:"===e.protocol?"443":"80":e.port,n.host=e.host;const d=await this.request(a,n,r,e.protocol,e.hostname,h,e.pathname+e.search,o,i);if(d.finalURL=e.toString(),!t.includes(d.status))return d;switch(s.redirect){default:case"follow":if(20>c&&d.headers.has("location")){e=new URL(d.headers.get("location"),e);continue}throw new TypeError("Failed to fetch");case"error":throw new TypeError("Failed to fetch");case"manual":return d}}}}}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).BareClient=t()}(this,(function(){"use strict";const e=[101,204,205,304],t=[301,302,303,307,308];class s extends Error{status;body;constructor(e,t){super(t.message||t.code),this.status=e,this.body=t}}class a{base;constructor(e,t){this.base=new URL(`./v${e}/`,t)}}const r="!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~";function n(e){for(let t=0;t<e.length;t++){const s=e[t];if(!r.includes(s))return!1}return!0}function o(e){let t="";for(let s=0;s<e.length;s++){const a=e[s];if(r.includes(a)&&"%"!==a)t+=a;else{t+="%"+a.charCodeAt(0).toString(16).padStart(2,"0")}}return t}function i(e,t){const s=(65535&e)+(65535&t);return(e>>16)+(t>>16)+(s>>16)<<16|65535&s}function c(e,t,s,a,r,n){return i((o=i(i(t,e),i(a,n)))<<(c=r)|o>>>32-c,s);var o,c}function h(e,t,s,a,r,n,o){return c(t&s|~t&a,e,t,r,n,o)}function d(e,t,s,a,r,n,o){return c(t&a|s&~a,e,t,r,n,o)}function w(e,t,s,a,r,n,o){return c(t^s^a,e,t,r,n,o)}function u(e,t,s,a,r,n,o){return c(s^(t|~a),e,t,r,n,o)}function f(e,t){e[t>>5]|=128<<t%32,e[14+(t+64>>>9<<4)]=t;let s=1732584193,a=-271733879,r=-1732584194,n=271733878;for(let t=0;t<e.length;t+=16){const o=s,c=a,f=r,l=n;s=h(s,a,r,n,e[t],7,-680876936),n=h(n,s,a,r,e[t+1],12,-389564586),r=h(r,n,s,a,e[t+2],17,606105819),a=h(a,r,n,s,e[t+3],22,-1044525330),s=h(s,a,r,n,e[t+4],7,-176418897),n=h(n,s,a,r,e[t+5],12,1200080426),r=h(r,n,s,a,e[t+6],17,-1473231341),a=h(a,r,n,s,e[t+7],22,-45705983),s=h(s,a,r,n,e[t+8],7,1770035416),n=h(n,s,a,r,e[t+9],12,-1958414417),r=h(r,n,s,a,e[t+10],17,-42063),a=h(a,r,n,s,e[t+11],22,-1990404162),s=h(s,a,r,n,e[t+12],7,1804603682),n=h(n,s,a,r,e[t+13],12,-40341101),r=h(r,n,s,a,e[t+14],17,-1502002290),a=h(a,r,n,s,e[t+15],22,1236535329),s=d(s,a,r,n,e[t+1],5,-165796510),n=d(n,s,a,r,e[t+6],9,-1069501632),r=d(r,n,s,a,e[t+11],14,643717713),a=d(a,r,n,s,e[t],20,-373897302),s=d(s,a,r,n,e[t+5],5,-701558691),n=d(n,s,a,r,e[t+10],9,38016083),r=d(r,n,s,a,e[t+15],14,-660478335),a=d(a,r,n,s,e[t+4],20,-405537848),s=d(s,a,r,n,e[t+9],5,568446438),n=d(n,s,a,r,e[t+14],9,-1019803690),r=d(r,n,s,a,e[t+3],14,-187363961),a=d(a,r,n,s,e[t+8],20,1163531501),s=d(s,a,r,n,e[t+13],5,-1444681467),n=d(n,s,a,r,e[t+2],9,-51403784),r=d(r,n,s,a,e[t+7],14,1735328473),a=d(a,r,n,s,e[t+12],20,-1926607734),s=w(s,a,r,n,e[t+5],4,-378558),n=w(n,s,a,r,e[t+8],11,-2022574463),r=w(r,n,s,a,e[t+11],16,1839030562),a=w(a,r,n,s,e[t+14],23,-35309556),s=w(s,a,r,n,e[t+1],4,-1530992060),n=w(n,s,a,r,e[t+4],11,1272893353),r=w(r,n,s,a,e[t+7],16,-155497632),a=w(a,r,n,s,e[t+10],23,-1094730640),s=w(s,a,r,n,e[t+13],4,681279174),n=w(n,s,a,r,e[t],11,-358537222),r=w(r,n,s,a,e[t+3],16,-722521979),a=w(a,r,n,s,e[t+6],23,76029189),s=w(s,a,r,n,e[t+9],4,-640364487),n=w(n,s,a,r,e[t+12],11,-421815835),r=w(r,n,s,a,e[t+15],16,530742520),a=w(a,r,n,s,e[t+2],23,-995338651),s=u(s,a,r,n,e[t],6,-198630844),n=u(n,s,a,r,e[t+7],10,1126891415),r=u(r,n,s,a,e[t+14],15,-1416354905),a=u(a,r,n,s,e[t+5],21,-57434055),s=u(s,a,r,n,e[t+12],6,1700485571),n=u(n,s,a,r,e[t+3],10,-1894986606),r=u(r,n,s,a,e[t+10],15,-1051523),a=u(a,r,n,s,e[t+1],21,-2054922799),s=u(s,a,r,n,e[t+8],6,1873313359),n=u(n,s,a,r,e[t+15],10,-30611744),r=u(r,n,s,a,e[t+6],15,-1560198380),a=u(a,r,n,s,e[t+13],21,1309151649),s=u(s,a,r,n,e[t+4],6,-145523070),n=u(n,s,a,r,e[t+11],10,-1120210379),r=u(r,n,s,a,e[t+2],15,718787259),a=u(a,r,n,s,e[t+9],21,-343485551),s=i(s,o),a=i(a,c),r=i(r,f),n=i(n,l)}return[s,a,r,n]}function l(e){let t="";const s=32*e.length;for(let a=0;a<s;a+=8)t+=String.fromCharCode(e[a>>5]>>>a%32&255);return t}function b(e){const t=[],s=e.length>>2;for(let e=0;e<s;e+=1)t[e]=0;const a=8*e.length;for(let s=0;s<a;s+=8)t[s>>5]|=(255&e.charCodeAt(s/8))<<s%32;return t}function p(e){const t="0123456789abcdef";let s="";for(let a=0;a<e.length;a+=1){const r=e.charCodeAt(a);s+=t.charAt(r>>>4&15)+t.charAt(15&r)}return s}function g(e){return unescape(encodeURIComponent(e))}function x(e){return function(e){return l(f(b(e),8*e.length))}(g(e))}function y(e,t){return function(e,t){let s=b(e);const a=[],r=[];s.length>16&&(s=f(s,8*e.length));for(let e=0;e<16;e+=1)a[e]=909522486^s[e],r[e]=1549556828^s[e];const n=f(a.concat(b(t)),512+8*t.length);return l(f(r.concat(n),640))}(g(e),g(t))}function m(e,t,s){return t?s?y(t,e):p(y(t,e)):s?x(e):p(x(e))}const R=3072;const E=[["v2",class extends a{ws;http;newMeta;getMeta;constructor(e){super(2,e),this.ws=new URL(this.base),this.http=new URL(this.base),this.newMeta=new URL("./ws-new-meta",this.base),this.getMeta=new URL("./ws-meta",this.base),"https:"===this.ws.protocol?this.ws.protocol="wss:":this.ws.protocol="ws:"}async connect(e,t,a,r,n){const o=new Request(this.newMeta,{headers:this.createBareHeaders(t,a,n,r,e)}),i=await fetch(o);if(!i.ok)throw new s(i.status,await i.json());const c=await i.text(),h=new WebSocket(this.ws,[c]);return h.meta=new Promise(((e,t)=>{h.addEventListener("open",(async()=>{const t=await fetch(this.getMeta,{headers:{"x-bare-id":c},method:"GET"});e(await await this.readBareResponse(t))})),h.addEventListener("error",t)})),h}async request(t,s,a,r,n,o,i,c,h){if(r.startsWith("blob:")){const e=await fetch(`blob:${location.origin}${i}`),t=new Response(e.body,e);return t.rawHeaders=Object.fromEntries(e.headers),t.rawResponse=e,t}const d={};if(s instanceof Headers)for(const[e,t]of s)d[e]=t;else for(const e in s)d[e]=s[e];const w={credentials:"omit",method:t,signal:h};"only-if-cached"!==c&&(w.cache=c),void 0!==a&&(w.body=a),w.headers=this.createBareHeaders(r,n,i,o,d);const u=new Request(this.http+"?cache="+m(`${r}${n}${o}${i}`),w),f=await fetch(u),l=await this.readBareResponse(f),b=new Response(e.includes(l.status)?void 0:f.body,{status:l.status,statusText:l.statusText??void 0,headers:l.headers});return b.rawHeaders=l.rawHeaders,b.rawResponse=f,b}async readBareResponse(e){if(!e.ok)throw new s(e.status,await e.json());const t=function(e){const t=new Headers(e),a="x-bare-headers";if(e.has(`${a}-0`)){const r=[];for(const[n,o]of e)if(n.startsWith(a)){if(!o.startsWith(";"))throw new s(400,{code:"INVALID_BARE_HEADER",id:`request.headers.${n}`,message:"Value didn't begin with semi-colon."});r[parseInt(n.slice(a.length+1))]=o.slice(1),t.delete(n)}t.set(a,r.join(""))}return t}(e.headers),a={};return t.has("x-bare-status")&&(a.status=parseInt(t.get("x-bare-status"))),t.has("x-bare-status-text")&&(a.statusText=t.get("x-bare-status-text")),t.has("x-bare-headers")&&(a.rawHeaders=JSON.parse(t.get("x-bare-headers")),a.headers=new Headers(a.rawHeaders)),a}createBareHeaders(e,t,s,a,r,n=[],o=[],i=[]){const c=new Headers;c.set("x-bare-protocol",e),c.set("x-bare-host",t),c.set("x-bare-path",s),c.set("x-bare-port",a.toString()),c.set("x-bare-headers",JSON.stringify(r));for(const e of n)c.append("x-bare-forward-headers",e);for(const e of o)c.append("x-bare-pass-headers",e);for(const e of i)c.append("x-bare-pass-status",e.toString());return function(e){const t=new Headers(e);if(e.has("x-bare-headers")){const s=e.get("x-bare-headers");if(s.length>R){t.delete("x-bare-headers");let e=0;for(let a=0;a<s.length;a+=R){const r=s.slice(a,a+R),n=e++;t.set(`x-bare-headers-${n}`,`;${r}`)}}}}(c),c}}],["v1",class extends a{ws;http;newMeta;getMeta;constructor(e){super(1,e),this.ws=new URL(this.base),this.http=new URL(this.base),this.newMeta=new URL("ws-new-meta",this.base),this.getMeta=new URL("ws-meta",this.base),"https:"===this.ws.protocol?this.ws.protocol="wss:":this.ws.protocol="ws:"}async connect(e,t,a,r,n){const i=await fetch(this.newMeta,{method:"GET"});if(!i.ok)throw new s(i.status,await i.json());const c=await i.text(),h=new WebSocket(this.ws,["bare",o(JSON.stringify({remote:{protocol:t,host:a,port:r,path:n},headers:e,forward_headers:["accept-encoding","accept-language","sec-websocket-extensions","sec-websocket-key","sec-websocket-version"],id:c}))]);return h.meta=new Promise(((e,t)=>{h.addEventListener("open",(async()=>{const a=await fetch(this.getMeta,{headers:{"x-bare-id":c},method:"GET"});a.ok||t(new s(a.status,await a.json())),e(await a.json())})),h.addEventListener("error",t)})),h}async request(t,s,a,r,n,o,i,c,h){if(r.startsWith("blob:")){const e=await fetch(`blob:${location.origin}${i}`),t=new Response(e.body,e);return t.rawHeaders=Object.fromEntries(e.headers),t.rawResponse=e,t}const d={};if(s instanceof Headers)for(const[e,t]of s)d[e]=t;else for(const e in s)d[e]=s[e];const w={credentials:"omit",method:t,signal:h};void 0!==a&&(w.body=a);const u=new Request(this.http,w);this.writeBareRequest(u,r,n,i,o,d,["accept-encoding","accept-language"]);const f=await fetch(u),l=await this.readBareResponse(f),b=new Response(e.includes(l.status)?void 0:f.body,{status:l.status,statusText:l.statusText??void 0,headers:l.headers});return b.rawHeaders=l.rawHeaders,b.rawResponse=f,b}async readBareResponse(e){if(!e.ok)throw new s(e.status,await e.json());const t=["x-bare-status","x-bare-status-text","x-bare-headers"];for(const a of t)if(!e.headers.has(a))throw new s(500,{code:"IMPL_MISSING_BARE_HEADER",id:`response.headers.${a}`});const a=parseInt(e.headers.get("x-bare-status")),r=e.headers.get("x-bare-status-text"),n=JSON.parse(e.headers.get("x-bare-headers"));return{status:a,statusText:r,rawHeaders:n,headers:new Headers(n)}}writeBareRequest(e,t,s,a,r,n,o){e.headers.set("x-bare-protocol",t),e.headers.set("x-bare-host",s),e.headers.set("x-bare-path",a),e.headers.set("x-bare-port",r.toString()),e.headers.set("x-bare-headers",JSON.stringify(n)),e.headers.set("x-bare-forward-headers",JSON.stringify(o))}}]];return class{data;client;server;ready;constructor(e,t){this.server=new URL(e),this.ready=!1,"object"==typeof t&&this.loadData(t)}loadData(e){let t=!1;for(const[s,a]of E)if(e.versions.includes(s)){this.client=new a(this.server),t=!0;break}if(!t)throw new Error("Unable to find compatible client version.");this.data=e,this.ready=!0}async work(){if(!0===this.ready)return;const e=await fetch(this.server);if(!e.ok)throw new Error(`Unable to fetch Bare meta: ${e.status} ${await e.text()}`);this.loadData(await e.json())}async request(e,t,s,a,r,n,o,i,c){return await this.work(),await this.client.request(e,t,s,a,r,n,o,i,c)}async connect(e,t,s,a,r){return await this.work(),this.client.connect(e,t,s,a,r)}async createWebSocket(e,t={},s=[]){const a=t instanceof Headers?Object.fromEntries(t):t;e=new URL(e),a.Host=e.host,a.Pragma="no-cache",a["Cache-Control"]="no-cache",a.Upgrade="websocket",a.Connection="Upgrade","string"==typeof s&&(s=[s]);for(const e of s)if(!n(e))throw new DOMException(`Failed to construct 'WebSocket': The subprotocol '${e}' is invalid.`);return s.length&&(t["Sec-Websocket-Protocol"]=s.join(", ")),await this.work(),this.client.connect(t,e.protocol,e.hostname,e.port,e.pathname+e.search)}async fetch(e,s={}){let a,r,n,o,i;e=new URL(e),a="string"==typeof s.method?s.method:"GET",void 0!==s.body&&null!==s.body&&(r=s.body),n="object"==typeof s.headers&&null!==s.headers?s.headers instanceof Headers?Object.fromEntries(s.headers):s.headers:{},o="string"==typeof s.cache?s.cache:"default",s.signal instanceof AbortSignal&&(i=s.signal);for(let c=0;;c++){let h;h=""===e.port?"https:"===e.protocol?"443":"80":e.port,n.host=e.host;const d=await this.request(a,n,r,e.protocol,e.hostname,h,e.pathname+e.search,o,i);if(d.finalURL=e.toString(),!t.includes(d.status))return d;switch(s.redirect){default:case"follow":if(20>c&&d.headers.has("location")){e=new URL(d.headers.get("location"),e);continue}throw new TypeError("Failed to fetch");case"error":throw new TypeError("Failed to fetch");case"manual":return d}}}}}));

@@ -0,5 +1,13 @@

/**
*
* Splits headers according to spec
* @param headers
* @returns Split headers
*/
export declare function splitHeaders(headers: Readonly<Headers>): Headers;
/**
* Joins headers in object, according to spec
* Joins headers according to spec
* @param headers
* @returns Joined headers
*/
export declare function joinHeaders(headers: Readonly<Headers>): Headers;
{
"name": "@tomphttp/bare-client",
"version": "1.0.1-beta",
"version": "1.0.2-beta",
"homepage": "https://github.com/tomphttp",

@@ -20,5 +20,5 @@ "bugs": {

"build": "rollup -c",
"build:watch": "rollup -cw",
"prepare": "node ./scripts/prepare.js",
"lint": "eslint ./src/",
"publish": ""
"lint": "eslint ./src/"
},

@@ -37,3 +37,4 @@ "type": "module",

"files": [
"dist"
"dist",
"scripts"
],

@@ -40,0 +41,0 @@ "devDependencies": {

@@ -5,3 +5,3 @@ # Bare Client

## Quick Start
## Quickstart

@@ -8,0 +8,0 @@ Script tag:

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc