@textile/context
Advanced tools
Comparing version 0.8.0 to 0.8.1-alpha.0
@@ -6,2 +6,15 @@ # Change Log | ||
## [0.8.1-alpha.0](https://github.com/textileio/js-threads/compare/@textile/context@0.8.0...@textile/context@0.8.1-alpha.0) (2020-11-13) | ||
### Features | ||
* **builds:** adds module path and build ([9d029ef](https://github.com/textileio/js-threads/commit/9d029ef44c39d3019773c772bf8d483bcdf3be1a)) | ||
* **builds:** rm umd for now; update build scripts to all use bili ([4757daf](https://github.com/textileio/js-threads/commit/4757dafa316b4e2c84c8ea8d2ad35206ad7737d4)) | ||
* **bundle:** try out bili for packaging ([5df79c4](https://github.com/textileio/js-threads/commit/5df79c4c0dbd1def9b3e5a4c84a21ac787a01663)) | ||
# [0.8.0](https://github.com/textileio/js-threads/compare/@textile/context@0.6.8...@textile/context@0.8.0) (2020-09-30) | ||
@@ -8,0 +21,0 @@ |
@@ -0,0 +0,0 @@ import { grpc } from "@improbable-eng/grpc-web"; |
@@ -1,201 +0,2 @@ | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Context = exports.defaultHost = void 0; | ||
const grpc_web_1 = require("@improbable-eng/grpc-web"); | ||
const security_1 = require("@textile/security"); | ||
exports.defaultHost = "https://webapi.hub.textile.io"; | ||
/** | ||
* Context provides context management for gRPC credentials and config settings. | ||
* It is the default implementation for the ContextInterface interface. | ||
*/ | ||
class Context { | ||
/** | ||
* Construct a new Context object. | ||
* @param host The remote gRPC host. This input exists to comply with the Config interface. | ||
*/ | ||
constructor(host = exports.defaultHost) { | ||
// Internal context variables | ||
this._context = {}; | ||
this._context["host"] = host; | ||
} | ||
static fromUserAuth(auth, host = exports.defaultHost) { | ||
const ctx = new Context(host); | ||
const { key, token } = auth, sig = __rest(auth, ["key", "token"]); | ||
return ctx.withAPIKey(key).withAPISig(sig).withToken(token); | ||
} | ||
static fromUserAuthCallback(authCallback, host = exports.defaultHost) { | ||
const ctx = new Context(host); | ||
// @todo: Should we now callback right away? | ||
ctx.authCallback = authCallback; | ||
return ctx; | ||
} | ||
get host() { | ||
return this._context["host"]; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
set(key, value) { | ||
this._context[key] = value; | ||
return this; | ||
} | ||
get(key) { | ||
return this._context[key]; | ||
} | ||
withSession(value) { | ||
if (value === undefined) | ||
return this; | ||
this._context["x-textile-session"] = value; | ||
return this; | ||
} | ||
withThread(value) { | ||
if (value === undefined) | ||
return this; | ||
this._context["x-textile-thread"] = value.toString(); | ||
return this; | ||
} | ||
withThreadName(value) { | ||
if (value === undefined) | ||
return this; | ||
this._context["x-textile-thread-name"] = value; | ||
return this; | ||
} | ||
withOrg(value) { | ||
if (value === undefined) | ||
return this; | ||
this._context["x-textile-org"] = value; | ||
return this; | ||
} | ||
withToken(value) { | ||
if (value === undefined) | ||
return this; | ||
this._context["authorization"] = `bearer ${value}`; | ||
return this; | ||
} | ||
withAPIKey(value) { | ||
if (value === undefined) | ||
return this; | ||
this._context["x-textile-api-key"] = value; | ||
return this; | ||
} | ||
withAPISig(value) { | ||
if (value === undefined) | ||
return this; | ||
const { sig, msg } = value; | ||
this._context["x-textile-api-sig-msg"] = msg; | ||
this._context["x-textile-api-sig"] = sig; | ||
return this; | ||
} | ||
withKeyInfo(key, date) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (key === undefined) | ||
return this; | ||
// Enables the use of insecure / non-signing keys | ||
if (!key.secret) | ||
return this.withAPIKey(key.key); | ||
const sig = yield security_1.createAPISig(key.secret, date); | ||
return this.withAPIKey(key.key).withAPISig(sig); | ||
}); | ||
} | ||
withContext(value) { | ||
if (value === undefined) | ||
return this; | ||
// Spread to copy rather than reference | ||
this._context = value.toJSON(); | ||
return this; | ||
} | ||
/** | ||
* Returns true if this Context contains an api sig msg, and that msg has expired, or if | ||
* it does _not_ have an api sig msg, but it _does_ have an auth callback. | ||
*/ | ||
get isExpired() { | ||
const msg = this.get("x-textile-api-sig-msg"); | ||
const notAuthed = msg === undefined && this.authCallback !== undefined; | ||
const isExpired = msg !== undefined && new Date(msg) <= new Date(); | ||
return isExpired || notAuthed; | ||
} | ||
/** | ||
* Refresh user auth with provided callback. | ||
* If callback is not specified, attempts to use existing callback specified at initialization. | ||
*/ | ||
refreshUserAuth(authCallback) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// If we have a new one, use it... | ||
if (authCallback !== undefined) { | ||
this.authCallback = authCallback; | ||
} | ||
// If we still don't have a callback, throw... | ||
if (this.authCallback === undefined) { | ||
throw new Error("Missing authCallback. See Context.fromUserAuthCallback for details."); | ||
} | ||
// Now do the renewal and return self... | ||
const _a = yield this.authCallback(), { key, token } = _a, sig = __rest(_a, ["key", "token"]); | ||
return this.withAPIKey(key).withAPISig(sig).withToken(token); | ||
}); | ||
} | ||
/** | ||
* Convert Context to plain JSON object. | ||
* @throws If this Context has expired. | ||
* @see toMetadata for an alternative for gRPC clients that supports auto-renewal. | ||
*/ | ||
toJSON() { | ||
const json = __rest(this._context | ||
// If we're expired, throw... | ||
, []); | ||
// If we're expired, throw... | ||
if (this.isExpired) { | ||
throw security_1.expirationError; | ||
} | ||
return json; | ||
} | ||
/** | ||
* Convert Context to grpc Metadata object. | ||
* Will automatically call the auth callback if available. | ||
* @param ctx Additional context object that will be merged with this prior to conversion. | ||
* @see toJSON for an alternative that returns a plain object, and throws when expired. | ||
*/ | ||
toMetadata(ctx) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const context = new Context(); | ||
if (this.isExpired && this.authCallback !== undefined) { | ||
const _a = yield this.authCallback(), { key, token } = _a, sig = __rest(_a, ["key", "token"]); | ||
// We do want to mutate this here because we want to update our auth sig | ||
this.withAPIKey(key).withAPISig(sig).withToken(token); | ||
} | ||
// We merge this context and ctx with the empty context so as to avoid mutating this with ctx | ||
return new grpc_web_1.grpc.Metadata(context.withContext(this).withContext(ctx).toJSON()); | ||
}); | ||
} | ||
/** | ||
* Import various ContextInterface API properties from JSON. | ||
* @param json The JSON object. | ||
* @param host Optional host string. | ||
*/ | ||
static fromJSON(json, host = exports.defaultHost) { | ||
const newContext = Object.assign({}, json); | ||
newContext["host"] = host; | ||
const ctx = new Context(); | ||
ctx._context = newContext; | ||
return ctx; | ||
} | ||
} | ||
exports.Context = Context; | ||
//# sourceMappingURL=index.js.map | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("@improbable-eng/grpc-web"),i=require("@textile/security");function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var o="https://webapi.hub.textile.io",a=function(){function a(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:o;n(this,a),this._context={},this._context.host=t}var h,u,s;return h=a,s=[{key:"fromUserAuth",value:function(e){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o,n=new a(i),r=e.key,h=e.token,u=t.__rest(e,["key","token"]);return n.withAPIKey(r).withAPISig(u).withToken(h)}},{key:"fromUserAuthCallback",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o,i=new a(e);return i.authCallback=t,i}},{key:"fromJSON",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o,i=Object.assign({},t);i.host=e;var n=new a;return n._context=i,n}}],(u=[{key:"set",value:function(t,e){return this._context[t]=e,this}},{key:"get",value:function(t){return this._context[t]}},{key:"withSession",value:function(t){return void 0===t||(this._context["x-textile-session"]=t),this}},{key:"withThread",value:function(t){return void 0===t||(this._context["x-textile-thread"]=t.toString()),this}},{key:"withThreadName",value:function(t){return void 0===t||(this._context["x-textile-thread-name"]=t),this}},{key:"withOrg",value:function(t){return void 0===t||(this._context["x-textile-org"]=t),this}},{key:"withToken",value:function(t){return void 0===t||(this._context.authorization="bearer ".concat(t)),this}},{key:"withAPIKey",value:function(t){return void 0===t||(this._context["x-textile-api-key"]=t),this}},{key:"withAPISig",value:function(t){if(void 0===t)return this;var e=t.sig,i=t.msg;return this._context["x-textile-api-sig-msg"]=i,this._context["x-textile-api-sig"]=e,this}},{key:"withKeyInfo",value:function(e,n){return t.__awaiter(this,void 0,void 0,(function*(){if(void 0===e)return this;if(!e.secret)return this.withAPIKey(e.key);var t=yield i.createAPISig(e.secret,n);return this.withAPIKey(e.key).withAPISig(t)}))}},{key:"withContext",value:function(t){return void 0===t||(this._context=t.toJSON()),this}},{key:"refreshUserAuth",value:function(e){return t.__awaiter(this,void 0,void 0,(function*(){if(void 0!==e&&(this.authCallback=e),void 0===this.authCallback)throw new Error("Missing authCallback. See Context.fromUserAuthCallback for details.");var i=yield this.authCallback(),n=i.key,r=i.token,o=t.__rest(i,["key","token"]);return this.withAPIKey(n).withAPISig(o).withToken(r)}))}},{key:"toJSON",value:function(){var e=t.__rest(this._context,[]);if(this.isExpired)throw i.expirationError;return e}},{key:"toMetadata",value:function(i){return t.__awaiter(this,void 0,void 0,(function*(){var n=new a;if(this.isExpired&&void 0!==this.authCallback){var r=yield this.authCallback(),o=r.key,h=r.token,u=t.__rest(r,["key","token"]);this.withAPIKey(o).withAPISig(u).withToken(h)}return new e.grpc.Metadata(n.withContext(this).withContext(i).toJSON())}))}},{key:"host",get:function(){return this._context.host}},{key:"isExpired",get:function(){var t=this.get("x-textile-api-sig-msg"),e=void 0===t&&void 0!==this.authCallback;return void 0!==t&&new Date(t)<=new Date||e}}])&&r(h.prototype,u),s&&r(h,s),a}();exports.Context=a,exports.defaultHost=o; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@textile/context", | ||
"version": "0.8.0", | ||
"main": "dist/index", | ||
"types": "dist/index", | ||
"version": "0.8.1-alpha.0", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.esm.ts", | ||
"files": [ | ||
@@ -15,6 +16,6 @@ "dist/**/!(*.spec).js?(.map)", | ||
"scripts": { | ||
"build": "bili --config ../../bili.config.js", | ||
"prepublishOnly": "npm run build", | ||
"prepare": "npm run build", | ||
"prebuild": "npm run clean", | ||
"build": "npx tsc -b tsconfig.json", | ||
"clean": "npx rimraf ./dist ./tsconfig.tsbuildinfo", | ||
@@ -34,5 +35,5 @@ "test:browser": "polendina --cleanup --timeout 60 src/**/*.spec.ts --webpack-config ../../webpack.test.js" | ||
"@improbable-eng/grpc-web": "^0.13.0", | ||
"@textile/security": "^0.4.0" | ||
"@textile/security": "^0.4.1-alpha.0" | ||
}, | ||
"gitHead": "3c77bad67b2449240efbff053dcc19cec905d55f" | ||
"gitHead": "6086212ae18b9fd6cadba597d9ac79637668b101" | ||
} |
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
43841
2462
9
212
3
+ Added@textile/security@0.4.1-alpha.1(transitive)
+ Added@types/node@22.9.3(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removed@textile/security@0.4.0(transitive)
- Removed@types/node@22.10.1(transitive)
- Removedundici-types@6.20.0(transitive)