@oada/client
Advanced tools
Comparing version 2.3.1 to 2.4.0
@@ -10,2 +10,5 @@ /// <reference types="node" /> | ||
private initialConnection; | ||
private concurrency; | ||
private context; | ||
private ws?; | ||
constructor(domain: string, token: string, concurrency?: number); | ||
@@ -12,0 +15,0 @@ disconnect(): Promise<void>; |
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,3 +13,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const cross_fetch_1 = __importDefault(require("cross-fetch")); | ||
const fetch_1 = __importStar(require("./fetch")); | ||
const events_1 = require("events"); | ||
@@ -12,2 +19,3 @@ const ksuid_1 = __importDefault(require("ksuid")); | ||
const debug_1 = __importDefault(require("debug")); | ||
const websocket_1 = require("./websocket"); | ||
const trace = debug_1.default("@oada/client:http:trace"); | ||
@@ -25,2 +33,3 @@ const warn = debug_1.default("@oada/client:http:warn"); | ||
super(); | ||
this.context = fetch_1.context ? fetch_1.context() : { fetch: fetch_1.default }; | ||
this._domain = domain.match(/^http/) ? domain : `https://${domain}`; | ||
@@ -31,6 +40,8 @@ this._domain = this._domain.replace(/\/$/, ""); | ||
trace(`Opening the HTTP connection to HEAD ${this._domain}/bookmarks w/ headers authorization: Bearer ${this._token}`); | ||
this.initialConnection = cross_fetch_1.default(`${this._domain}/bookmarks`, { | ||
this.initialConnection = this.context | ||
.fetch(`${this._domain}/bookmarks`, { | ||
method: "HEAD", | ||
headers: { authorization: `Bearer ${this._token}` }, | ||
}).then((result) => { | ||
}) | ||
.then((result) => { | ||
trace("Initial HEAD returned status: ", result.status); | ||
@@ -48,2 +59,3 @@ if (result.status < 400) { | ||
}); | ||
this.concurrency = concurrency; | ||
this._q = new p_queue_1.default({ concurrency }); | ||
@@ -55,3 +67,6 @@ this._q.on("active", () => { | ||
async disconnect() { | ||
var _a, _b, _c; | ||
this._status = ConnectionStatus.Disconnected; | ||
await ((_b = (_a = this.context).disconnectAll) === null || _b === void 0 ? void 0 : _b.call(_a)); | ||
await ((_c = this.ws) === null || _c === void 0 ? void 0 : _c.disconnect()); | ||
this.emit("close"); | ||
@@ -65,10 +80,14 @@ } | ||
} | ||
request(req, callback, timeout) { | ||
async request(req, callback, timeout) { | ||
var _a; | ||
trace("Starting http request: ", req); | ||
if (req.method === "watch" || req.method === "unwatch") { | ||
throw new Error("HTTP (i.e. non-WebSocket) Client cannot do watches"); | ||
if (req.method === "watch" || req.method === "unwatch" || callback) { | ||
warn("WATCH/UNWATCH not currently supported for http(2), falling-back to ws"); | ||
if (!this.ws) { | ||
const domain = this._domain.replace(/^https?:\/\//, ""); | ||
this.ws = new websocket_1.WebSocketClient(domain, this.concurrency); | ||
await this.ws.awaitConnection(); | ||
} | ||
return (_a = this.ws) === null || _a === void 0 ? void 0 : _a.request(req, callback, timeout); | ||
} | ||
if (callback) { | ||
throw new Error("HTTP (i.e. non-WebSocket) Client cannot handle a watch callback"); | ||
} | ||
if (!req.requestId) | ||
@@ -87,7 +106,9 @@ req.requestId = ksuid_1.default.randomSync().string; | ||
} | ||
const result = await cross_fetch_1.default(`${this._domain}${req.path}`, { | ||
const result = await this.context | ||
.fetch(`${this._domain}${req.path}`, { | ||
method: req.method.toUpperCase(), | ||
body: JSON.stringify(req.data), | ||
headers: req.headers, | ||
}).then((res) => { | ||
}) | ||
.then((res) => { | ||
if (timedout) | ||
@@ -104,3 +125,10 @@ throw new Error("Request timeout"); | ||
const headers = {}; | ||
result.headers.forEach((value, key) => (headers[key] = value)); | ||
if (Array.isArray(result.headers)) { | ||
result.headers.forEach((value, key) => (headers[key] = value)); | ||
} | ||
else { | ||
for (const [key, value] of result.headers.entries()) { | ||
headers[key] = value; | ||
} | ||
} | ||
const length = +(result.headers.get("content-length") || 0); | ||
@@ -107,0 +135,0 @@ let data = null; |
{ | ||
"name": "@oada/client", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"description": "A lightweight client tool to interact with an OADA-compliant server", | ||
"repository": "https://github.com/OADA/client", | ||
"main": "dist/index.js", | ||
"browser": { | ||
"./lib/fetch": "cross-fetch" | ||
}, | ||
"react-native": { | ||
"./lib/fetch": "cross-fetch" | ||
}, | ||
"types": "dist/index.d.ts", | ||
@@ -12,3 +18,3 @@ "files": [ | ||
"scripts": { | ||
"test": "mocha -r ts-node/register test/**/*.test.ts", | ||
"test": "mocha -r ts-node/register test/**/*.test.ts --timeout 10000", | ||
"build": "tsc", | ||
@@ -26,2 +32,3 @@ "prettier": "prettier --write '{lib,test}/**/*.ts'", | ||
"debug": "^4.1.1", | ||
"fetch-h2": "^2.5.1", | ||
"isomorphic-ws": "^4.0.1", | ||
@@ -28,0 +35,0 @@ "ksuid": "^1.2.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68238
23
992
9
2
+ Addedfetch-h2@^2.5.1
+ Added@types/tough-cookie@4.0.5(transitive)
+ Addedalready@1.13.2(transitive)
+ Addedcallguard@2.0.0(transitive)
+ Addedes-abstract@1.23.3(transitive)
+ Addedfetch-h2@2.5.1(transitive)
+ Addedget-stream@6.0.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedpsl@1.10.0(transitive)
+ Addedquerystringify@2.2.0(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedrequires-port@1.0.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedthroat@5.0.0(transitive)
+ Addedthrough2@4.0.2(transitive)
+ Addedto-arraybuffer@1.0.1(transitive)
+ Addedtough-cookie@4.1.4(transitive)
+ Addeduniversalify@0.2.0(transitive)
+ Addedurl-parse@1.5.10(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedes-abstract@1.23.4(transitive)