@source-health/bridge
Advanced tools
+313
| (function webpackUniversalModuleDefinition(root, factory) { | ||
| if(typeof exports === 'object' && typeof module === 'object') | ||
| module.exports = factory(); | ||
| else if(typeof define === 'function' && define.amd) | ||
| define([], factory); | ||
| else if(typeof exports === 'object') | ||
| exports["bridge"] = factory(); | ||
| else | ||
| root["bridge"] = factory(); | ||
| })(this, function() { | ||
| return /******/ (() => { // webpackBootstrap | ||
| /******/ "use strict"; | ||
| /******/ var __webpack_modules__ = ({ | ||
| /***/ "./src/SourceBridge.ts": | ||
| /*!*****************************!*\ | ||
| !*** ./src/SourceBridge.ts ***! | ||
| \*****************************/ | ||
| /***/ (function(__unused_webpack_module, exports, __webpack_require__) { | ||
| 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()); | ||
| }); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
| exports.SourceBridge = void 0; | ||
| const SourceBridgeClient_1 = __webpack_require__(/*! ./SourceBridgeClient */ "./src/SourceBridgeClient.ts"); | ||
| const generateRequestId_1 = __webpack_require__(/*! ./generateRequestId */ "./src/generateRequestId.ts"); | ||
| class SourceBridgeAPI { | ||
| constructor() { | ||
| this.onContextCallbacks = []; | ||
| this.client = new SourceBridgeClient_1.SourceBridgeClient(); | ||
| } | ||
| init() { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| const response = yield this.client.sendRequest({ | ||
| type: 'hello', | ||
| id: (0, generateRequestId_1.generateRequestId)(), | ||
| }); | ||
| this.handleNewAuth(response.payload.auth); | ||
| const info = this.handlePluginInfo(response.payload.plugin_info); | ||
| // Call the context callback with the initial context | ||
| yield this.handleNewContext(response.payload.context); | ||
| // Subscribe to any further context events | ||
| this.client.onEvent('context', (envelope) => __awaiter(this, void 0, void 0, function* () { | ||
| const context = envelope.payload; | ||
| yield this.handleNewContext(context); | ||
| })); | ||
| // Subscribe to any auth events | ||
| // eslint-disable-next-line @typescript-eslint/require-await | ||
| this.client.onEvent('authentication', (envelope) => __awaiter(this, void 0, void 0, function* () { | ||
| this.handleNewAuth(envelope.payload); | ||
| })); | ||
| return info; | ||
| }); | ||
| } | ||
| ready() { | ||
| this.client.sendEvent({ | ||
| type: 'ready', | ||
| id: (0, generateRequestId_1.generateRequestId)(), | ||
| }); | ||
| } | ||
| // This is async because we intend to add 'fetch on demand' when the current token is | ||
| // expired (e.g. if the tab has been throttled by the browser, or the computer has been | ||
| // suspended.) | ||
| // eslint-disable-next-line @typescript-eslint/require-await | ||
| currentToken() { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| if (!this.auth) { | ||
| console.error('[SourceBridge] called currentToken() before init()'); | ||
| throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentToken()'); | ||
| } | ||
| return this.auth; | ||
| }); | ||
| } | ||
| currentContext() { | ||
| if (!this.context) { | ||
| console.error('[SourceBridge] called currentContext() before init()'); | ||
| throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentContext()'); | ||
| } | ||
| return this.context; | ||
| } | ||
| info() { | ||
| if (!this.pluginInfo) { | ||
| console.error('[SourceBridge] called info() before init()'); | ||
| throw new Error('SourceBridge is not yet initialized. Please call `init()` before info()'); | ||
| } | ||
| return this.pluginInfo; | ||
| } | ||
| onContextUpdate(callback) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| this.onContextCallbacks.push(callback); | ||
| // We immediately call the callback if we already have received context, in case | ||
| // the developer forgot to set up the callbacks before running init() | ||
| if (this.context) { | ||
| yield callback(this.context); | ||
| } | ||
| }); | ||
| } | ||
| handleNewContext(context) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| console.log('[SourceBridge] handling new context', context); | ||
| this.context = context; | ||
| yield Promise.allSettled(this.onContextCallbacks.map((callback) => callback(context))); | ||
| }); | ||
| } | ||
| handleNewAuth(auth) { | ||
| console.log('[SourceBridge] handling new application token'); | ||
| this.auth = { | ||
| token: auth.token, | ||
| expiresAt: new Date(auth.expires_at), | ||
| }; | ||
| } | ||
| handlePluginInfo(info) { | ||
| const mapped = { | ||
| application: info.application, | ||
| viewKey: info.view_key, | ||
| surface: info.surface, | ||
| }; | ||
| this.pluginInfo = mapped; | ||
| return mapped; | ||
| } | ||
| } | ||
| exports.SourceBridge = new SourceBridgeAPI(); | ||
| /***/ }), | ||
| /***/ "./src/SourceBridgeClient.ts": | ||
| /*!***********************************!*\ | ||
| !*** ./src/SourceBridgeClient.ts ***! | ||
| \***********************************/ | ||
| /***/ (function(__unused_webpack_module, exports) { | ||
| 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()); | ||
| }); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
| exports.SourceBridgeClient = void 0; | ||
| class SourceBridgeClient { | ||
| constructor() { | ||
| // Map where we keep track of open requests and their promise resolve callbacks | ||
| this.openRequests = new Map(); | ||
| // Map where we keep track of event subscriptions | ||
| this.messageCallbacks = new Map(); | ||
| window.addEventListener('message', (event) => { | ||
| // Because we allow async callbacks, handleEnvelope is async, but addEventListener only takes non-async callbacks | ||
| // so we use `void` operator to allow using an async function here. | ||
| void this.handleEnvelope(event); | ||
| }); | ||
| } | ||
| sendEvent(message) { | ||
| console.log('[SourceBridge] sending message to parent:', message); | ||
| parent.postMessage(JSON.stringify(message), '*'); | ||
| } | ||
| sendRequest(message) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| const promise = new Promise((resolve, _reject) => { | ||
| this.openRequests.set(message.id, resolve); | ||
| }); | ||
| console.log('[SourceBridge] sending request to parent:', message); | ||
| parent.postMessage(JSON.stringify(message), '*'); | ||
| return promise; | ||
| }); | ||
| } | ||
| onEvent(type, callback) { | ||
| const callbacks = this.messageCallbacks.get(type); | ||
| if (callbacks) { | ||
| callbacks.push(callback); | ||
| } | ||
| else { | ||
| this.messageCallbacks.set(type, [callback]); | ||
| } | ||
| } | ||
| handleEnvelope(event) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| if (event.source !== parent) { | ||
| console.log('[SourceBridge] Ignoring message event from non-parent'); | ||
| return; | ||
| } | ||
| const envelope = JSON.parse(event.data); | ||
| // If we have in_reply_to set then this is a response | ||
| if (envelope.in_reply_to) { | ||
| this.handleResponse(envelope); | ||
| } | ||
| else { | ||
| yield this.handleEvent(envelope); | ||
| } | ||
| }); | ||
| } | ||
| handleEvent(message) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| const callbacks = this.messageCallbacks.get(message.type); | ||
| if (callbacks) { | ||
| for (const callback of callbacks) { | ||
| try { | ||
| yield callback(message); | ||
| } | ||
| catch (error) { | ||
| console.error('[SourceBridge] error thrown in event callback:', error); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| handleResponse(message) { | ||
| const requestId = message.in_reply_to; | ||
| console.log(`[SourceBridge] handling response to ${requestId}`, message); | ||
| const resolve = this.openRequests.get(requestId); | ||
| if (!resolve) { | ||
| console.error(`[SourceBridge] Did not find resolve for request ${requestId}`); | ||
| return; | ||
| } | ||
| this.openRequests.delete(requestId); | ||
| resolve(message); | ||
| } | ||
| } | ||
| exports.SourceBridgeClient = SourceBridgeClient; | ||
| /***/ }), | ||
| /***/ "./src/generateRequestId.ts": | ||
| /*!**********************************!*\ | ||
| !*** ./src/generateRequestId.ts ***! | ||
| \**********************************/ | ||
| /***/ ((__unused_webpack_module, exports) => { | ||
| Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
| exports.generateRequestId = void 0; | ||
| function generateRequestId() { | ||
| return Math.random().toString(32).substring(2); | ||
| } | ||
| exports.generateRequestId = generateRequestId; | ||
| /***/ }), | ||
| /***/ "./src/index.ts": | ||
| /*!**********************!*\ | ||
| !*** ./src/index.ts ***! | ||
| \**********************/ | ||
| /***/ (function(__unused_webpack_module, exports, __webpack_require__) { | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
| __exportStar(__webpack_require__(/*! ./SourceBridge */ "./src/SourceBridge.ts"), exports); | ||
| /***/ }) | ||
| /******/ }); | ||
| /************************************************************************/ | ||
| /******/ // The module cache | ||
| /******/ var __webpack_module_cache__ = {}; | ||
| /******/ | ||
| /******/ // The require function | ||
| /******/ function __webpack_require__(moduleId) { | ||
| /******/ // Check if module is in cache | ||
| /******/ var cachedModule = __webpack_module_cache__[moduleId]; | ||
| /******/ if (cachedModule !== undefined) { | ||
| /******/ return cachedModule.exports; | ||
| /******/ } | ||
| /******/ // Create a new module (and put it into the cache) | ||
| /******/ var module = __webpack_module_cache__[moduleId] = { | ||
| /******/ // no module.id needed | ||
| /******/ // no module.loaded needed | ||
| /******/ exports: {} | ||
| /******/ }; | ||
| /******/ | ||
| /******/ // Execute the module function | ||
| /******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
| /******/ | ||
| /******/ // Return the exports of the module | ||
| /******/ return module.exports; | ||
| /******/ } | ||
| /******/ | ||
| /************************************************************************/ | ||
| /******/ | ||
| /******/ // startup | ||
| /******/ // Load entry module and return exports | ||
| /******/ // This entry module is referenced by other modules so it can't be inlined | ||
| /******/ var __webpack_exports__ = __webpack_require__("./src/index.ts"); | ||
| /******/ | ||
| /******/ return __webpack_exports__; | ||
| /******/ })() | ||
| ; | ||
| }); | ||
| //# sourceMappingURL=bridge.map |
| {"version":3,"file":"bridge.js","mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;;;;;;;;;;ACVa;AACb;AACA,4BAA4B,+DAA+D,iBAAiB;AAC5G;AACA,oCAAoC,MAAM,+BAA+B,YAAY;AACrF,mCAAmC,MAAM,mCAAmC,YAAY;AACxF,gCAAgC;AAChC;AACA,KAAK;AACL;AACA,8CAA6C,EAAE,aAAa,EAAC;AAC7D,oBAAoB;AACpB,6BAA6B,mBAAO,CAAC,yDAAsB;AAC3D,4BAA4B,mBAAO,CAAC,uDAAqB;AACzD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACA;AACA;AACA;AACA,aAAa;AACb;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,oBAAoB;;;;;;;;;;;AC7GP;AACb;AACA,4BAA4B,+DAA+D,iBAAiB;AAC5G;AACA,oCAAoC,MAAM,+BAA+B,YAAY;AACrF,mCAAmC,MAAM,mCAAmC,YAAY;AACxF,gCAAgC;AAChC;AACA,KAAK;AACL;AACA,8CAA6C,EAAE,aAAa,EAAC;AAC7D,0BAA0B;AAC1B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA,2DAA2D,UAAU;AACrE;AACA;AACA,6EAA6E,UAAU;AACvF;AACA;AACA;AACA;AACA;AACA;AACA,0BAA0B;;;;;;;;;;;AC1Fb;AACb,8CAA6C,EAAE,aAAa,EAAC;AAC7D,yBAAyB;AACzB;AACA;AACA;AACA,yBAAyB;;;;;;;;;;;ACNZ;AACb;AACA;AACA,mCAAmC,oCAAoC,gBAAgB;AACvF,CAAC;AACD;AACA;AACA,CAAC;AACD;AACA;AACA;AACA,8CAA6C,EAAE,aAAa,EAAC;AAC7D,aAAa,mBAAO,CAAC,6CAAgB;;;;;;;UCZrC;UACA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;UAEA;UACA;;UAEA;UACA;UACA;;;;UEtBA;UACA;UACA;UACA","sources":["webpack://bridge/webpack/universalModuleDefinition","webpack://bridge/./src/SourceBridge.ts","webpack://bridge/./src/SourceBridgeClient.ts","webpack://bridge/./src/generateRequestId.ts","webpack://bridge/./src/index.ts","webpack://bridge/webpack/bootstrap","webpack://bridge/webpack/before-startup","webpack://bridge/webpack/startup","webpack://bridge/webpack/after-startup"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"bridge\"] = factory();\n\telse\n\t\troot[\"bridge\"] = factory();\n})(this, function() {\nreturn ","\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.SourceBridge = void 0;\nconst SourceBridgeClient_1 = require(\"./SourceBridgeClient\");\nconst generateRequestId_1 = require(\"./generateRequestId\");\nclass SourceBridgeAPI {\n constructor() {\n this.onContextCallbacks = [];\n this.client = new SourceBridgeClient_1.SourceBridgeClient();\n }\n init() {\n return __awaiter(this, void 0, void 0, function* () {\n const response = yield this.client.sendRequest({\n type: 'hello',\n id: (0, generateRequestId_1.generateRequestId)(),\n });\n this.handleNewAuth(response.payload.auth);\n const info = this.handlePluginInfo(response.payload.plugin_info);\n // Call the context callback with the initial context\n yield this.handleNewContext(response.payload.context);\n // Subscribe to any further context events\n this.client.onEvent('context', (envelope) => __awaiter(this, void 0, void 0, function* () {\n const context = envelope.payload;\n yield this.handleNewContext(context);\n }));\n // Subscribe to any auth events\n // eslint-disable-next-line @typescript-eslint/require-await\n this.client.onEvent('authentication', (envelope) => __awaiter(this, void 0, void 0, function* () {\n this.handleNewAuth(envelope.payload);\n }));\n return info;\n });\n }\n ready() {\n this.client.sendEvent({\n type: 'ready',\n id: (0, generateRequestId_1.generateRequestId)(),\n });\n }\n // This is async because we intend to add 'fetch on demand' when the current token is\n // expired (e.g. if the tab has been throttled by the browser, or the computer has been\n // suspended.)\n // eslint-disable-next-line @typescript-eslint/require-await\n currentToken() {\n return __awaiter(this, void 0, void 0, function* () {\n if (!this.auth) {\n console.error('[SourceBridge] called currentToken() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentToken()');\n }\n return this.auth;\n });\n }\n currentContext() {\n if (!this.context) {\n console.error('[SourceBridge] called currentContext() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentContext()');\n }\n return this.context;\n }\n info() {\n if (!this.pluginInfo) {\n console.error('[SourceBridge] called info() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before info()');\n }\n return this.pluginInfo;\n }\n onContextUpdate(callback) {\n return __awaiter(this, void 0, void 0, function* () {\n this.onContextCallbacks.push(callback);\n // We immediately call the callback if we already have received context, in case\n // the developer forgot to set up the callbacks before running init()\n if (this.context) {\n yield callback(this.context);\n }\n });\n }\n handleNewContext(context) {\n return __awaiter(this, void 0, void 0, function* () {\n console.log('[SourceBridge] handling new context', context);\n this.context = context;\n yield Promise.allSettled(this.onContextCallbacks.map((callback) => callback(context)));\n });\n }\n handleNewAuth(auth) {\n console.log('[SourceBridge] handling new application token');\n this.auth = {\n token: auth.token,\n expiresAt: new Date(auth.expires_at),\n };\n }\n handlePluginInfo(info) {\n const mapped = {\n application: info.application,\n viewKey: info.view_key,\n surface: info.surface,\n };\n this.pluginInfo = mapped;\n return mapped;\n }\n}\nexports.SourceBridge = new SourceBridgeAPI();\n","\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.SourceBridgeClient = void 0;\nclass SourceBridgeClient {\n constructor() {\n // Map where we keep track of open requests and their promise resolve callbacks\n this.openRequests = new Map();\n // Map where we keep track of event subscriptions\n this.messageCallbacks = new Map();\n window.addEventListener('message', (event) => {\n // Because we allow async callbacks, handleEnvelope is async, but addEventListener only takes non-async callbacks\n // so we use `void` operator to allow using an async function here.\n void this.handleEnvelope(event);\n });\n }\n sendEvent(message) {\n console.log('[SourceBridge] sending message to parent:', message);\n parent.postMessage(JSON.stringify(message), '*');\n }\n sendRequest(message) {\n return __awaiter(this, void 0, void 0, function* () {\n const promise = new Promise((resolve, _reject) => {\n this.openRequests.set(message.id, resolve);\n });\n console.log('[SourceBridge] sending request to parent:', message);\n parent.postMessage(JSON.stringify(message), '*');\n return promise;\n });\n }\n onEvent(type, callback) {\n const callbacks = this.messageCallbacks.get(type);\n if (callbacks) {\n callbacks.push(callback);\n }\n else {\n this.messageCallbacks.set(type, [callback]);\n }\n }\n handleEnvelope(event) {\n return __awaiter(this, void 0, void 0, function* () {\n if (event.source !== parent) {\n console.log('[SourceBridge] Ignoring message event from non-parent');\n return;\n }\n const envelope = JSON.parse(event.data);\n // If we have in_reply_to set then this is a response\n if (envelope.in_reply_to) {\n this.handleResponse(envelope);\n }\n else {\n yield this.handleEvent(envelope);\n }\n });\n }\n handleEvent(message) {\n return __awaiter(this, void 0, void 0, function* () {\n const callbacks = this.messageCallbacks.get(message.type);\n if (callbacks) {\n for (const callback of callbacks) {\n try {\n yield callback(message);\n }\n catch (error) {\n console.error('[SourceBridge] error thrown in event callback:', error);\n }\n }\n }\n });\n }\n handleResponse(message) {\n const requestId = message.in_reply_to;\n console.log(`[SourceBridge] handling response to ${requestId}`, message);\n const resolve = this.openRequests.get(requestId);\n if (!resolve) {\n console.error(`[SourceBridge] Did not find resolve for request ${requestId}`);\n return;\n }\n this.openRequests.delete(requestId);\n resolve(message);\n }\n}\nexports.SourceBridgeClient = SourceBridgeClient;\n","\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.generateRequestId = void 0;\nfunction generateRequestId() {\n return Math.random().toString(32).substring(2);\n}\nexports.generateRequestId = generateRequestId;\n","\"use strict\";\nvar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n}));\nvar __exportStar = (this && this.__exportStar) || function(m, exports) {\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\n__exportStar(require(\"./SourceBridge\"), exports);\n","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","","// startup\n// Load entry module and return exports\n// This entry module is referenced by other modules so it can't be inlined\nvar __webpack_exports__ = __webpack_require__(\"./src/index.ts\");\n",""],"names":[],"sourceRoot":""} |
| !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.bridge=t():e.bridge=t()}(this,(function(){return(()=>{"use strict";var e={168:function(e,t,n){var o=this&&this.__awaiter||function(e,t,n,o){return new(n||(n=Promise))((function(i,r){function s(e){try{a(o.next(e))}catch(e){r(e)}}function c(e){try{a(o.throw(e))}catch(e){r(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,c)}a((o=o.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.SourceBridge=void 0;const i=n(983),r=n(516);t.SourceBridge=new class{constructor(){this.onContextCallbacks=[],this.client=new i.SourceBridgeClient}init(){return o(this,void 0,void 0,(function*(){const e=yield this.client.sendRequest({type:"hello",id:(0,r.generateRequestId)()});this.handleNewAuth(e.payload.auth);const t=this.handlePluginInfo(e.payload.plugin_info);return yield this.handleNewContext(e.payload.context),this.client.onEvent("context",(e=>o(this,void 0,void 0,(function*(){const t=e.payload;yield this.handleNewContext(t)})))),this.client.onEvent("authentication",(e=>o(this,void 0,void 0,(function*(){this.handleNewAuth(e.payload)})))),t}))}ready(){this.client.sendEvent({type:"ready",id:(0,r.generateRequestId)()})}currentToken(){return o(this,void 0,void 0,(function*(){if(!this.auth)throw console.error("[SourceBridge] called currentToken() before init()"),new Error("SourceBridge is not yet initialized. Please call `init()` before currentToken()");return this.auth}))}currentContext(){if(!this.context)throw console.error("[SourceBridge] called currentContext() before init()"),new Error("SourceBridge is not yet initialized. Please call `init()` before currentContext()");return this.context}info(){if(!this.pluginInfo)throw console.error("[SourceBridge] called info() before init()"),new Error("SourceBridge is not yet initialized. Please call `init()` before info()");return this.pluginInfo}onContextUpdate(e){return o(this,void 0,void 0,(function*(){this.onContextCallbacks.push(e),this.context&&(yield e(this.context))}))}handleNewContext(e){return o(this,void 0,void 0,(function*(){console.log("[SourceBridge] handling new context",e),this.context=e,yield Promise.allSettled(this.onContextCallbacks.map((t=>t(e))))}))}handleNewAuth(e){console.log("[SourceBridge] handling new application token"),this.auth={token:e.token,expiresAt:new Date(e.expires_at)}}handlePluginInfo(e){const t={application:e.application,viewKey:e.view_key,surface:e.surface};return this.pluginInfo=t,t}}},983:function(e,t){var n=this&&this.__awaiter||function(e,t,n,o){return new(n||(n=Promise))((function(i,r){function s(e){try{a(o.next(e))}catch(e){r(e)}}function c(e){try{a(o.throw(e))}catch(e){r(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,c)}a((o=o.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.SourceBridgeClient=void 0,t.SourceBridgeClient=class{constructor(){this.openRequests=new Map,this.messageCallbacks=new Map,window.addEventListener("message",(e=>{this.handleEnvelope(e)}))}sendEvent(e){console.log("[SourceBridge] sending message to parent:",e),parent.postMessage(JSON.stringify(e),"*")}sendRequest(e){return n(this,void 0,void 0,(function*(){const t=new Promise(((t,n)=>{this.openRequests.set(e.id,t)}));return console.log("[SourceBridge] sending request to parent:",e),parent.postMessage(JSON.stringify(e),"*"),t}))}onEvent(e,t){const n=this.messageCallbacks.get(e);n?n.push(t):this.messageCallbacks.set(e,[t])}handleEnvelope(e){return n(this,void 0,void 0,(function*(){if(e.source!==parent)return void console.log("[SourceBridge] Ignoring message event from non-parent");const t=JSON.parse(e.data);t.in_reply_to?this.handleResponse(t):yield this.handleEvent(t)}))}handleEvent(e){return n(this,void 0,void 0,(function*(){const t=this.messageCallbacks.get(e.type);if(t)for(const n of t)try{yield n(e)}catch(e){console.error("[SourceBridge] error thrown in event callback:",e)}}))}handleResponse(e){const t=e.in_reply_to;console.log(`[SourceBridge] handling response to ${t}`,e);const n=this.openRequests.get(t);n?(this.openRequests.delete(t),n(e)):console.error(`[SourceBridge] Did not find resolve for request ${t}`)}}},516:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.generateRequestId=void 0,t.generateRequestId=function(){return Math.random().toString(32).substring(2)}},607:function(e,t,n){var o=this&&this.__createBinding||(Object.create?function(e,t,n,o){void 0===o&&(o=n),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[n]}})}:function(e,t,n,o){void 0===o&&(o=n),e[o]=t[n]}),i=this&&this.__exportStar||function(e,t){for(var n in e)"default"===n||Object.prototype.hasOwnProperty.call(t,n)||o(t,e,n)};Object.defineProperty(t,"__esModule",{value:!0}),i(n(168),t)}},t={};return function n(o){var i=t[o];if(void 0!==i)return i.exports;var r=t[o]={exports:{}};return e[o].call(r.exports,r,r.exports,n),r.exports}(607)})()})); | ||
| //# sourceMappingURL=bridge.min.map |
| {"version":3,"file":"bridge.min.js","mappings":"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","sources":["webpack://bridge/webpack/universalModuleDefinition","webpack://bridge/./src/SourceBridge.ts","webpack://bridge/./src/SourceBridgeClient.ts","webpack://bridge/./src/generateRequestId.ts","webpack://bridge/./src/index.ts","webpack://bridge/webpack/bootstrap","webpack://bridge/webpack/startup"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"bridge\"] = factory();\n\telse\n\t\troot[\"bridge\"] = factory();\n})(this, function() {\nreturn ","\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.SourceBridge = void 0;\nconst SourceBridgeClient_1 = require(\"./SourceBridgeClient\");\nconst generateRequestId_1 = require(\"./generateRequestId\");\nclass SourceBridgeAPI {\n constructor() {\n this.onContextCallbacks = [];\n this.client = new SourceBridgeClient_1.SourceBridgeClient();\n }\n init() {\n return __awaiter(this, void 0, void 0, function* () {\n const response = yield this.client.sendRequest({\n type: 'hello',\n id: (0, generateRequestId_1.generateRequestId)(),\n });\n this.handleNewAuth(response.payload.auth);\n const info = this.handlePluginInfo(response.payload.plugin_info);\n // Call the context callback with the initial context\n yield this.handleNewContext(response.payload.context);\n // Subscribe to any further context events\n this.client.onEvent('context', (envelope) => __awaiter(this, void 0, void 0, function* () {\n const context = envelope.payload;\n yield this.handleNewContext(context);\n }));\n // Subscribe to any auth events\n // eslint-disable-next-line @typescript-eslint/require-await\n this.client.onEvent('authentication', (envelope) => __awaiter(this, void 0, void 0, function* () {\n this.handleNewAuth(envelope.payload);\n }));\n return info;\n });\n }\n ready() {\n this.client.sendEvent({\n type: 'ready',\n id: (0, generateRequestId_1.generateRequestId)(),\n });\n }\n // This is async because we intend to add 'fetch on demand' when the current token is\n // expired (e.g. if the tab has been throttled by the browser, or the computer has been\n // suspended.)\n // eslint-disable-next-line @typescript-eslint/require-await\n currentToken() {\n return __awaiter(this, void 0, void 0, function* () {\n if (!this.auth) {\n console.error('[SourceBridge] called currentToken() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentToken()');\n }\n return this.auth;\n });\n }\n currentContext() {\n if (!this.context) {\n console.error('[SourceBridge] called currentContext() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentContext()');\n }\n return this.context;\n }\n info() {\n if (!this.pluginInfo) {\n console.error('[SourceBridge] called info() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before info()');\n }\n return this.pluginInfo;\n }\n onContextUpdate(callback) {\n return __awaiter(this, void 0, void 0, function* () {\n this.onContextCallbacks.push(callback);\n // We immediately call the callback if we already have received context, in case\n // the developer forgot to set up the callbacks before running init()\n if (this.context) {\n yield callback(this.context);\n }\n });\n }\n handleNewContext(context) {\n return __awaiter(this, void 0, void 0, function* () {\n console.log('[SourceBridge] handling new context', context);\n this.context = context;\n yield Promise.allSettled(this.onContextCallbacks.map((callback) => callback(context)));\n });\n }\n handleNewAuth(auth) {\n console.log('[SourceBridge] handling new application token');\n this.auth = {\n token: auth.token,\n expiresAt: new Date(auth.expires_at),\n };\n }\n handlePluginInfo(info) {\n const mapped = {\n application: info.application,\n viewKey: info.view_key,\n surface: info.surface,\n };\n this.pluginInfo = mapped;\n return mapped;\n }\n}\nexports.SourceBridge = new SourceBridgeAPI();\n","\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.SourceBridgeClient = void 0;\nclass SourceBridgeClient {\n constructor() {\n // Map where we keep track of open requests and their promise resolve callbacks\n this.openRequests = new Map();\n // Map where we keep track of event subscriptions\n this.messageCallbacks = new Map();\n window.addEventListener('message', (event) => {\n // Because we allow async callbacks, handleEnvelope is async, but addEventListener only takes non-async callbacks\n // so we use `void` operator to allow using an async function here.\n void this.handleEnvelope(event);\n });\n }\n sendEvent(message) {\n console.log('[SourceBridge] sending message to parent:', message);\n parent.postMessage(JSON.stringify(message), '*');\n }\n sendRequest(message) {\n return __awaiter(this, void 0, void 0, function* () {\n const promise = new Promise((resolve, _reject) => {\n this.openRequests.set(message.id, resolve);\n });\n console.log('[SourceBridge] sending request to parent:', message);\n parent.postMessage(JSON.stringify(message), '*');\n return promise;\n });\n }\n onEvent(type, callback) {\n const callbacks = this.messageCallbacks.get(type);\n if (callbacks) {\n callbacks.push(callback);\n }\n else {\n this.messageCallbacks.set(type, [callback]);\n }\n }\n handleEnvelope(event) {\n return __awaiter(this, void 0, void 0, function* () {\n if (event.source !== parent) {\n console.log('[SourceBridge] Ignoring message event from non-parent');\n return;\n }\n const envelope = JSON.parse(event.data);\n // If we have in_reply_to set then this is a response\n if (envelope.in_reply_to) {\n this.handleResponse(envelope);\n }\n else {\n yield this.handleEvent(envelope);\n }\n });\n }\n handleEvent(message) {\n return __awaiter(this, void 0, void 0, function* () {\n const callbacks = this.messageCallbacks.get(message.type);\n if (callbacks) {\n for (const callback of callbacks) {\n try {\n yield callback(message);\n }\n catch (error) {\n console.error('[SourceBridge] error thrown in event callback:', error);\n }\n }\n }\n });\n }\n handleResponse(message) {\n const requestId = message.in_reply_to;\n console.log(`[SourceBridge] handling response to ${requestId}`, message);\n const resolve = this.openRequests.get(requestId);\n if (!resolve) {\n console.error(`[SourceBridge] Did not find resolve for request ${requestId}`);\n return;\n }\n this.openRequests.delete(requestId);\n resolve(message);\n }\n}\nexports.SourceBridgeClient = SourceBridgeClient;\n","\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.generateRequestId = void 0;\nfunction generateRequestId() {\n return Math.random().toString(32).substring(2);\n}\nexports.generateRequestId = generateRequestId;\n","\"use strict\";\nvar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n}));\nvar __exportStar = (this && this.__exportStar) || function(m, exports) {\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\n__exportStar(require(\"./SourceBridge\"), exports);\n","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","// startup\n// Load entry module and return exports\n// This entry module is referenced by other modules so it can't be inlined\nvar __webpack_exports__ = __webpack_require__(607);\n"],"names":["root","factory","exports","module","define","amd","this","__awaiter","thisArg","_arguments","P","generator","Promise","resolve","reject","fulfilled","value","step","next","e","rejected","result","done","then","apply","Object","defineProperty","SourceBridge","SourceBridgeClient_1","generateRequestId_1","constructor","onContextCallbacks","client","SourceBridgeClient","init","response","sendRequest","type","id","generateRequestId","handleNewAuth","payload","auth","info","handlePluginInfo","plugin_info","handleNewContext","context","onEvent","envelope","ready","sendEvent","currentToken","console","error","Error","currentContext","pluginInfo","onContextUpdate","callback","push","log","allSettled","map","token","expiresAt","Date","expires_at","mapped","application","viewKey","view_key","surface","openRequests","Map","messageCallbacks","window","addEventListener","event","handleEnvelope","message","parent","postMessage","JSON","stringify","promise","_reject","set","callbacks","get","source","parse","data","in_reply_to","handleResponse","handleEvent","requestId","delete","Math","random","toString","substring","__createBinding","create","o","m","k","k2","undefined","enumerable","__exportStar","p","prototype","hasOwnProperty","call","__webpack_module_cache__","__webpack_require__","moduleId","cachedModule","__webpack_modules__"],"sourceRoot":""} |
| (function webpackUniversalModuleDefinition(root, factory) { | ||
| if(typeof exports === 'object' && typeof module === 'object') | ||
| module.exports = factory(); | ||
| else if(typeof define === 'function' && define.amd) | ||
| define([], factory); | ||
| else if(typeof exports === 'object') | ||
| exports["bridge"] = factory(); | ||
| else | ||
| root["bridge"] = factory(); | ||
| })(this, function() { | ||
| return /******/ (() => { // webpackBootstrap | ||
| /******/ "use strict"; | ||
| /******/ var __webpack_modules__ = ({ | ||
| /***/ "./src/SourceBridge.ts": | ||
| /*!*****************************!*\ | ||
| !*** ./src/SourceBridge.ts ***! | ||
| \*****************************/ | ||
| /***/ (function(__unused_webpack_module, exports, __webpack_require__) { | ||
| 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()); | ||
| }); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
| exports.SourceBridge = void 0; | ||
| const SourceBridgeClient_1 = __webpack_require__(/*! ./SourceBridgeClient */ "./src/SourceBridgeClient.ts"); | ||
| const generateRequestId_1 = __webpack_require__(/*! ./generateRequestId */ "./src/generateRequestId.ts"); | ||
| class SourceBridgeAPI { | ||
| constructor() { | ||
| this.onContextCallbacks = []; | ||
| this.client = new SourceBridgeClient_1.SourceBridgeClient(); | ||
| } | ||
| init() { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| const response = yield this.client.sendRequest({ | ||
| type: 'hello', | ||
| id: (0, generateRequestId_1.generateRequestId)(), | ||
| }); | ||
| this.handleNewAuth(response.payload.auth); | ||
| const info = this.handlePluginInfo(response.payload.plugin_info); | ||
| // Call the context callback with the initial context | ||
| yield this.handleNewContext(response.payload.context); | ||
| // Subscribe to any further context events | ||
| this.client.onEvent('context', (envelope) => __awaiter(this, void 0, void 0, function* () { | ||
| const context = envelope.payload; | ||
| yield this.handleNewContext(context); | ||
| })); | ||
| // Subscribe to any auth events | ||
| // eslint-disable-next-line @typescript-eslint/require-await | ||
| this.client.onEvent('authentication', (envelope) => __awaiter(this, void 0, void 0, function* () { | ||
| this.handleNewAuth(envelope.payload); | ||
| })); | ||
| return info; | ||
| }); | ||
| } | ||
| ready() { | ||
| this.client.sendEvent({ | ||
| type: 'ready', | ||
| id: (0, generateRequestId_1.generateRequestId)(), | ||
| }); | ||
| } | ||
| // This is async because we intend to add 'fetch on demand' when the current token is | ||
| // expired (e.g. if the tab has been throttled by the browser, or the computer has been | ||
| // suspended.) | ||
| // eslint-disable-next-line @typescript-eslint/require-await | ||
| currentToken() { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| if (!this.auth) { | ||
| console.error('[SourceBridge] called currentToken() before init()'); | ||
| throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentToken()'); | ||
| } | ||
| return this.auth; | ||
| }); | ||
| } | ||
| currentContext() { | ||
| if (!this.context) { | ||
| console.error('[SourceBridge] called currentContext() before init()'); | ||
| throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentContext()'); | ||
| } | ||
| return this.context; | ||
| } | ||
| info() { | ||
| if (!this.pluginInfo) { | ||
| console.error('[SourceBridge] called info() before init()'); | ||
| throw new Error('SourceBridge is not yet initialized. Please call `init()` before info()'); | ||
| } | ||
| return this.pluginInfo; | ||
| } | ||
| onContextUpdate(callback) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| this.onContextCallbacks.push(callback); | ||
| // We immediately call the callback if we already have received context, in case | ||
| // the developer forgot to set up the callbacks before running init() | ||
| if (this.context) { | ||
| yield callback(this.context); | ||
| } | ||
| }); | ||
| } | ||
| handleNewContext(context) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| console.log('[SourceBridge] handling new context', context); | ||
| this.context = context; | ||
| yield Promise.allSettled(this.onContextCallbacks.map((callback) => callback(context))); | ||
| }); | ||
| } | ||
| handleNewAuth(auth) { | ||
| console.log('[SourceBridge] handling new application token'); | ||
| this.auth = { | ||
| token: auth.token, | ||
| expiresAt: new Date(auth.expires_at), | ||
| }; | ||
| } | ||
| handlePluginInfo(info) { | ||
| const mapped = { | ||
| application: info.application, | ||
| viewKey: info.view_key, | ||
| surface: info.surface, | ||
| }; | ||
| this.pluginInfo = mapped; | ||
| return mapped; | ||
| } | ||
| } | ||
| exports.SourceBridge = new SourceBridgeAPI(); | ||
| /***/ }), | ||
| /***/ "./src/SourceBridgeClient.ts": | ||
| /*!***********************************!*\ | ||
| !*** ./src/SourceBridgeClient.ts ***! | ||
| \***********************************/ | ||
| /***/ (function(__unused_webpack_module, exports) { | ||
| 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()); | ||
| }); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
| exports.SourceBridgeClient = void 0; | ||
| class SourceBridgeClient { | ||
| constructor() { | ||
| // Map where we keep track of open requests and their promise resolve callbacks | ||
| this.openRequests = new Map(); | ||
| // Map where we keep track of event subscriptions | ||
| this.messageCallbacks = new Map(); | ||
| window.addEventListener('message', (event) => { | ||
| // Because we allow async callbacks, handleEnvelope is async, but addEventListener only takes non-async callbacks | ||
| // so we use `void` operator to allow using an async function here. | ||
| void this.handleEnvelope(event); | ||
| }); | ||
| } | ||
| sendEvent(message) { | ||
| console.log('[SourceBridge] sending message to parent:', message); | ||
| parent.postMessage(JSON.stringify(message), '*'); | ||
| } | ||
| sendRequest(message) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| const promise = new Promise((resolve, _reject) => { | ||
| this.openRequests.set(message.id, resolve); | ||
| }); | ||
| console.log('[SourceBridge] sending request to parent:', message); | ||
| parent.postMessage(JSON.stringify(message), '*'); | ||
| return promise; | ||
| }); | ||
| } | ||
| onEvent(type, callback) { | ||
| const callbacks = this.messageCallbacks.get(type); | ||
| if (callbacks) { | ||
| callbacks.push(callback); | ||
| } | ||
| else { | ||
| this.messageCallbacks.set(type, [callback]); | ||
| } | ||
| } | ||
| handleEnvelope(event) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| if (event.source !== parent) { | ||
| console.log('[SourceBridge] Ignoring message event from non-parent'); | ||
| return; | ||
| } | ||
| const envelope = JSON.parse(event.data); | ||
| // If we have in_reply_to set then this is a response | ||
| if (envelope.in_reply_to) { | ||
| this.handleResponse(envelope); | ||
| } | ||
| else { | ||
| yield this.handleEvent(envelope); | ||
| } | ||
| }); | ||
| } | ||
| handleEvent(message) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| const callbacks = this.messageCallbacks.get(message.type); | ||
| if (callbacks) { | ||
| for (const callback of callbacks) { | ||
| try { | ||
| yield callback(message); | ||
| } | ||
| catch (error) { | ||
| console.error('[SourceBridge] error thrown in event callback:', error); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| handleResponse(message) { | ||
| const requestId = message.in_reply_to; | ||
| console.log(`[SourceBridge] handling response to ${requestId}`, message); | ||
| const resolve = this.openRequests.get(requestId); | ||
| if (!resolve) { | ||
| console.error(`[SourceBridge] Did not find resolve for request ${requestId}`); | ||
| return; | ||
| } | ||
| this.openRequests.delete(requestId); | ||
| resolve(message); | ||
| } | ||
| } | ||
| exports.SourceBridgeClient = SourceBridgeClient; | ||
| /***/ }), | ||
| /***/ "./src/generateRequestId.ts": | ||
| /*!**********************************!*\ | ||
| !*** ./src/generateRequestId.ts ***! | ||
| \**********************************/ | ||
| /***/ ((__unused_webpack_module, exports) => { | ||
| Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
| exports.generateRequestId = void 0; | ||
| function generateRequestId() { | ||
| return Math.random().toString(32).substring(2); | ||
| } | ||
| exports.generateRequestId = generateRequestId; | ||
| /***/ }), | ||
| /***/ "./src/index.ts": | ||
| /*!**********************!*\ | ||
| !*** ./src/index.ts ***! | ||
| \**********************/ | ||
| /***/ (function(__unused_webpack_module, exports, __webpack_require__) { | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
| __exportStar(__webpack_require__(/*! ./SourceBridge */ "./src/SourceBridge.ts"), exports); | ||
| /***/ }) | ||
| /******/ }); | ||
| /************************************************************************/ | ||
| /******/ // The module cache | ||
| /******/ var __webpack_module_cache__ = {}; | ||
| /******/ | ||
| /******/ // The require function | ||
| /******/ function __webpack_require__(moduleId) { | ||
| /******/ // Check if module is in cache | ||
| /******/ var cachedModule = __webpack_module_cache__[moduleId]; | ||
| /******/ if (cachedModule !== undefined) { | ||
| /******/ return cachedModule.exports; | ||
| /******/ } | ||
| /******/ // Create a new module (and put it into the cache) | ||
| /******/ var module = __webpack_module_cache__[moduleId] = { | ||
| /******/ // no module.id needed | ||
| /******/ // no module.loaded needed | ||
| /******/ exports: {} | ||
| /******/ }; | ||
| /******/ | ||
| /******/ // Execute the module function | ||
| /******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
| /******/ | ||
| /******/ // Return the exports of the module | ||
| /******/ return module.exports; | ||
| /******/ } | ||
| /******/ | ||
| /************************************************************************/ | ||
| /******/ | ||
| /******/ // startup | ||
| /******/ // Load entry module and return exports | ||
| /******/ // This entry module is referenced by other modules so it can't be inlined | ||
| /******/ var __webpack_exports__ = __webpack_require__("./src/index.ts"); | ||
| /******/ | ||
| /******/ return __webpack_exports__; | ||
| /******/ })() | ||
| ; | ||
| }); | ||
| //# sourceMappingURL=bridge.node.map |
| {"version":3,"file":"bridge.node.js","mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;;;;;;;;;;ACVa;AACb;AACA,4BAA4B,+DAA+D,iBAAiB;AAC5G;AACA,oCAAoC,MAAM,+BAA+B,YAAY;AACrF,mCAAmC,MAAM,mCAAmC,YAAY;AACxF,gCAAgC;AAChC;AACA,KAAK;AACL;AACA,8CAA6C,EAAE,aAAa,EAAC;AAC7D,oBAAoB;AACpB,6BAA6B,mBAAO,CAAC,yDAAsB;AAC3D,4BAA4B,mBAAO,CAAC,uDAAqB;AACzD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACA;AACA;AACA;AACA,aAAa;AACb;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,oBAAoB;;;;;;;;;;;AC7GP;AACb;AACA,4BAA4B,+DAA+D,iBAAiB;AAC5G;AACA,oCAAoC,MAAM,+BAA+B,YAAY;AACrF,mCAAmC,MAAM,mCAAmC,YAAY;AACxF,gCAAgC;AAChC;AACA,KAAK;AACL;AACA,8CAA6C,EAAE,aAAa,EAAC;AAC7D,0BAA0B;AAC1B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA,2DAA2D,UAAU;AACrE;AACA;AACA,6EAA6E,UAAU;AACvF;AACA;AACA;AACA;AACA;AACA;AACA,0BAA0B;;;;;;;;;;;AC1Fb;AACb,8CAA6C,EAAE,aAAa,EAAC;AAC7D,yBAAyB;AACzB;AACA;AACA;AACA,yBAAyB;;;;;;;;;;;ACNZ;AACb;AACA;AACA,mCAAmC,oCAAoC,gBAAgB;AACvF,CAAC;AACD;AACA;AACA,CAAC;AACD;AACA;AACA;AACA,8CAA6C,EAAE,aAAa,EAAC;AAC7D,aAAa,mBAAO,CAAC,6CAAgB;;;;;;;UCZrC;UACA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;UAEA;UACA;;UAEA;UACA;UACA;;;;UEtBA;UACA;UACA;UACA","sources":["webpack://bridge/webpack/universalModuleDefinition","webpack://bridge/./src/SourceBridge.ts","webpack://bridge/./src/SourceBridgeClient.ts","webpack://bridge/./src/generateRequestId.ts","webpack://bridge/./src/index.ts","webpack://bridge/webpack/bootstrap","webpack://bridge/webpack/before-startup","webpack://bridge/webpack/startup","webpack://bridge/webpack/after-startup"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"bridge\"] = factory();\n\telse\n\t\troot[\"bridge\"] = factory();\n})(this, function() {\nreturn ","\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.SourceBridge = void 0;\nconst SourceBridgeClient_1 = require(\"./SourceBridgeClient\");\nconst generateRequestId_1 = require(\"./generateRequestId\");\nclass SourceBridgeAPI {\n constructor() {\n this.onContextCallbacks = [];\n this.client = new SourceBridgeClient_1.SourceBridgeClient();\n }\n init() {\n return __awaiter(this, void 0, void 0, function* () {\n const response = yield this.client.sendRequest({\n type: 'hello',\n id: (0, generateRequestId_1.generateRequestId)(),\n });\n this.handleNewAuth(response.payload.auth);\n const info = this.handlePluginInfo(response.payload.plugin_info);\n // Call the context callback with the initial context\n yield this.handleNewContext(response.payload.context);\n // Subscribe to any further context events\n this.client.onEvent('context', (envelope) => __awaiter(this, void 0, void 0, function* () {\n const context = envelope.payload;\n yield this.handleNewContext(context);\n }));\n // Subscribe to any auth events\n // eslint-disable-next-line @typescript-eslint/require-await\n this.client.onEvent('authentication', (envelope) => __awaiter(this, void 0, void 0, function* () {\n this.handleNewAuth(envelope.payload);\n }));\n return info;\n });\n }\n ready() {\n this.client.sendEvent({\n type: 'ready',\n id: (0, generateRequestId_1.generateRequestId)(),\n });\n }\n // This is async because we intend to add 'fetch on demand' when the current token is\n // expired (e.g. if the tab has been throttled by the browser, or the computer has been\n // suspended.)\n // eslint-disable-next-line @typescript-eslint/require-await\n currentToken() {\n return __awaiter(this, void 0, void 0, function* () {\n if (!this.auth) {\n console.error('[SourceBridge] called currentToken() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentToken()');\n }\n return this.auth;\n });\n }\n currentContext() {\n if (!this.context) {\n console.error('[SourceBridge] called currentContext() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before currentContext()');\n }\n return this.context;\n }\n info() {\n if (!this.pluginInfo) {\n console.error('[SourceBridge] called info() before init()');\n throw new Error('SourceBridge is not yet initialized. Please call `init()` before info()');\n }\n return this.pluginInfo;\n }\n onContextUpdate(callback) {\n return __awaiter(this, void 0, void 0, function* () {\n this.onContextCallbacks.push(callback);\n // We immediately call the callback if we already have received context, in case\n // the developer forgot to set up the callbacks before running init()\n if (this.context) {\n yield callback(this.context);\n }\n });\n }\n handleNewContext(context) {\n return __awaiter(this, void 0, void 0, function* () {\n console.log('[SourceBridge] handling new context', context);\n this.context = context;\n yield Promise.allSettled(this.onContextCallbacks.map((callback) => callback(context)));\n });\n }\n handleNewAuth(auth) {\n console.log('[SourceBridge] handling new application token');\n this.auth = {\n token: auth.token,\n expiresAt: new Date(auth.expires_at),\n };\n }\n handlePluginInfo(info) {\n const mapped = {\n application: info.application,\n viewKey: info.view_key,\n surface: info.surface,\n };\n this.pluginInfo = mapped;\n return mapped;\n }\n}\nexports.SourceBridge = new SourceBridgeAPI();\n","\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.SourceBridgeClient = void 0;\nclass SourceBridgeClient {\n constructor() {\n // Map where we keep track of open requests and their promise resolve callbacks\n this.openRequests = new Map();\n // Map where we keep track of event subscriptions\n this.messageCallbacks = new Map();\n window.addEventListener('message', (event) => {\n // Because we allow async callbacks, handleEnvelope is async, but addEventListener only takes non-async callbacks\n // so we use `void` operator to allow using an async function here.\n void this.handleEnvelope(event);\n });\n }\n sendEvent(message) {\n console.log('[SourceBridge] sending message to parent:', message);\n parent.postMessage(JSON.stringify(message), '*');\n }\n sendRequest(message) {\n return __awaiter(this, void 0, void 0, function* () {\n const promise = new Promise((resolve, _reject) => {\n this.openRequests.set(message.id, resolve);\n });\n console.log('[SourceBridge] sending request to parent:', message);\n parent.postMessage(JSON.stringify(message), '*');\n return promise;\n });\n }\n onEvent(type, callback) {\n const callbacks = this.messageCallbacks.get(type);\n if (callbacks) {\n callbacks.push(callback);\n }\n else {\n this.messageCallbacks.set(type, [callback]);\n }\n }\n handleEnvelope(event) {\n return __awaiter(this, void 0, void 0, function* () {\n if (event.source !== parent) {\n console.log('[SourceBridge] Ignoring message event from non-parent');\n return;\n }\n const envelope = JSON.parse(event.data);\n // If we have in_reply_to set then this is a response\n if (envelope.in_reply_to) {\n this.handleResponse(envelope);\n }\n else {\n yield this.handleEvent(envelope);\n }\n });\n }\n handleEvent(message) {\n return __awaiter(this, void 0, void 0, function* () {\n const callbacks = this.messageCallbacks.get(message.type);\n if (callbacks) {\n for (const callback of callbacks) {\n try {\n yield callback(message);\n }\n catch (error) {\n console.error('[SourceBridge] error thrown in event callback:', error);\n }\n }\n }\n });\n }\n handleResponse(message) {\n const requestId = message.in_reply_to;\n console.log(`[SourceBridge] handling response to ${requestId}`, message);\n const resolve = this.openRequests.get(requestId);\n if (!resolve) {\n console.error(`[SourceBridge] Did not find resolve for request ${requestId}`);\n return;\n }\n this.openRequests.delete(requestId);\n resolve(message);\n }\n}\nexports.SourceBridgeClient = SourceBridgeClient;\n","\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.generateRequestId = void 0;\nfunction generateRequestId() {\n return Math.random().toString(32).substring(2);\n}\nexports.generateRequestId = generateRequestId;\n","\"use strict\";\nvar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n}));\nvar __exportStar = (this && this.__exportStar) || function(m, exports) {\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\n__exportStar(require(\"./SourceBridge\"), exports);\n","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","","// startup\n// Load entry module and return exports\n// This entry module is referenced by other modules so it can't be inlined\nvar __webpack_exports__ = __webpack_require__(\"./src/index.ts\");\n",""],"names":[],"sourceRoot":""} |
+1
-1
| { | ||
| "name": "@source-health/bridge", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Official SDK for Source Health iframe plugins.", |
Network access
Supply chain riskThis module accesses the network.
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
163165
83.18%30
25%1550
63.5%3
50%5
66.67%