@dxos/rpc-tunnel 0.8.4-main.84f28bd → 0.8.4-main.a4bbb77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +27 -11
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +27 -11
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/port-muxer.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -3
- package/src/index.ts +1 -1
- package/src/port-muxer.ts +1 -1
|
@@ -152,19 +152,21 @@ var createWorkerPort = ({ port, channel, subscribe }) => ({
|
|
|
152
152
|
|
|
153
153
|
// src/port-muxer.ts
|
|
154
154
|
import { log as log3 } from "@dxos/log";
|
|
155
|
+
function _define_property(obj, key, value) {
|
|
156
|
+
if (key in obj) {
|
|
157
|
+
Object.defineProperty(obj, key, {
|
|
158
|
+
value,
|
|
159
|
+
enumerable: true,
|
|
160
|
+
configurable: true,
|
|
161
|
+
writable: true
|
|
162
|
+
});
|
|
163
|
+
} else {
|
|
164
|
+
obj[key] = value;
|
|
165
|
+
}
|
|
166
|
+
return obj;
|
|
167
|
+
}
|
|
155
168
|
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/rpc-tunnel/src/port-muxer.ts";
|
|
156
169
|
var PortMuxer = class {
|
|
157
|
-
constructor(_messagePort) {
|
|
158
|
-
this._messagePort = _messagePort;
|
|
159
|
-
this._activeChannels = /* @__PURE__ */ new Map();
|
|
160
|
-
this._rpcPorts = /* @__PURE__ */ new Map();
|
|
161
|
-
if (this._messagePort) {
|
|
162
|
-
this._messagePort.onmessage = (event) => this.onWorkerMessage(event);
|
|
163
|
-
}
|
|
164
|
-
if (typeof window !== "undefined") {
|
|
165
|
-
window.addEventListener("message", (event) => this.onWindowMessage(event));
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
170
|
createWorkerPort(options) {
|
|
169
171
|
if (!this._messagePort) {
|
|
170
172
|
throw new Error("Message port is required to create worker ports");
|
|
@@ -211,6 +213,20 @@ var PortMuxer = class {
|
|
|
211
213
|
C: (f, a) => f(...a)
|
|
212
214
|
});
|
|
213
215
|
}
|
|
216
|
+
constructor(_messagePort) {
|
|
217
|
+
_define_property(this, "_messagePort", void 0);
|
|
218
|
+
_define_property(this, "_activeChannels", void 0);
|
|
219
|
+
_define_property(this, "_rpcPorts", void 0);
|
|
220
|
+
this._messagePort = _messagePort;
|
|
221
|
+
this._activeChannels = /* @__PURE__ */ new Map();
|
|
222
|
+
this._rpcPorts = /* @__PURE__ */ new Map();
|
|
223
|
+
if (this._messagePort) {
|
|
224
|
+
this._messagePort.onmessage = (event) => this.onWorkerMessage(event);
|
|
225
|
+
}
|
|
226
|
+
if (typeof window !== "undefined") {
|
|
227
|
+
window.addEventListener("message", (event) => this.onWindowMessage(event));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
214
230
|
};
|
|
215
231
|
export {
|
|
216
232
|
PortMuxer,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/ports/iframe.ts", "../../../src/ports/worker.ts", "../../../src/port-muxer.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { UAParser } from 'ua-parser-js';\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from '../message';\n\nlet browser: string | undefined;\nlet os: string | undefined;\n\nif (typeof navigator !== 'undefined') {\n // TODO(wittjosiah): Stop user agent parsing.\n const parser = new UAParser(navigator.userAgent);\n browser = parser.getBrowser().name;\n os = parser.getOS().name;\n}\n\nconst sendToIFrame = (iframe: HTMLIFrameElement, origin: string, message: MessageData) => {\n if (!iframe.contentWindow) {\n log('IFrame content window missing', { origin });\n return;\n }\n\n if (browser === 'Chrome' && os === 'iOS') {\n iframe.contentWindow.postMessage(message, origin);\n } else {\n iframe.contentWindow.postMessage(message, origin, [message.payload]);\n }\n};\n\nconst sendToParentWindow = (origin: string, message: MessageData) => {\n if (browser === 'Chrome' && os === 'iOS') {\n window.parent.postMessage(message, origin);\n } else {\n window.parent.postMessage(message, origin, [message.payload]);\n }\n};\n\nexport type IFramePortOptions = {\n channel: string;\n iframe?: HTMLIFrameElement;\n origin?: string;\n onOrigin?: (origin: string) => void;\n};\n\n/**\n * Create a RPC port with an iframe over window messaging.\n * @param options.channel Identifier for sent/recieved messages.\n * @param options.iframe Instance of the iframe if sending to child.\n * @param options.origin Origin of the destination window.\n * @param options.onOrigin Callback triggered when origin of destination window is verified.\n * @returns RPC port for messaging.\n */\nexport const createIFramePort = ({ channel, iframe, origin, onOrigin }: IFramePortOptions): RpcPort => {\n return {\n send: async (data) => {\n if (!origin) {\n log('no origin set', { channel });\n return;\n }\n\n log('sending', { channel, data: data.length });\n const payload = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);\n const message = { channel, payload };\n if (iframe) {\n sendToIFrame(iframe, origin, message);\n } else {\n sendToParentWindow(origin, message);\n }\n },\n\n subscribe: (callback) => {\n const handler = (event: MessageEvent<unknown>) => {\n if (!iframe && event.source !== window.parent) {\n // Not from parent window.\n return;\n } else if (iframe && event.source !== iframe.contentWindow) {\n // Not from child window.\n return;\n }\n\n const isMessageData =\n event.data && typeof event.data === 'object' && 'channel' in event.data && 'payload' in event.data;\n const message = isMessageData ? (event.data as MessageData) : undefined;\n if (message?.channel !== channel) {\n return;\n }\n\n if (!origin) {\n origin = event.origin;\n onOrigin?.(origin);\n }\n\n log('received', message);\n callback(new Uint8Array(message.payload));\n };\n\n window.addEventListener('message', handler);\n return () => window.removeEventListener('message', handler);\n },\n };\n};\n\nexport type CreateIFrameOptions = {\n hidden?: boolean;\n allow?: string;\n};\n\n/**\n * Create a hidden iframe and insert it into the DOM.\n * If an element with the same id already exists it will be returned instead.\n * @param source Source of the iframe.\n * @param id DOM id of the iframe.\n * @returns The created iframe.\n */\nexport const createIFrame = (source: string, id: string, { hidden = true, allow }: CreateIFrameOptions = {}) => {\n const create = () => {\n const iframe = document.createElement('iframe') as HTMLIFrameElement;\n iframe.id = id;\n iframe.src = source;\n hidden && iframe.setAttribute('style', 'display: none;');\n allow && iframe.setAttribute('allow', allow);\n document.body.appendChild(iframe);\n return iframe;\n };\n\n return (document.getElementById(id) as HTMLIFrameElement) ?? create();\n};\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from '../message';\n\nexport type WorkerPortOptions = {\n port: MessagePort;\n channel?: string;\n subscribe?: RpcPort['subscribe'];\n};\n\n/**\n * Create a RPC port for a worker.\n * @param options.port Message port to send message on.\n * @param options.channel Identifier for sent/recieved messages.\n * @param options.subscribe\n * @returns RPC port for messaging.\n */\n// TODO(wittjosiah): Rename for more general purpose MessagePort.\nexport const createWorkerPort = ({ port, channel, subscribe }: WorkerPortOptions): RpcPort => ({\n send: async (message) => {\n // Based on https://stackoverflow.com/a/54646864/2804332.\n const payload = message.buffer.slice(message.byteOffset, message.byteOffset + message.byteLength);\n port.postMessage(\n {\n channel,\n payload,\n },\n [payload],\n );\n },\n\n subscribe:\n subscribe ??\n ((callback) => {\n const handler = (event: MessageEvent<MessageData>) => {\n const message = event.data;\n if (channel && message.channel !== channel) {\n return;\n }\n\n log.debug('received', { message });\n callback(new Uint8Array(message.payload));\n };\n\n port.onmessage = handler;\n return () => {\n port.onmessage = null;\n };\n }),\n});\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from './message';\nimport {
|
|
5
|
-
"mappings": ";AAIA,SAASA,gBAAgB;AAEzB,SAASC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,UAAU;AAChGT,SAAKU,YACH;MACET;MACAI;IACF,GACA;MAACA;KAAQ;EAEb;EAEAH,WACEA,cACC,CAACS,aAAAA;AACA,UAAMC,UAAU,CAACC,UAAAA;AACf,YAAMT,UAAUS,MAAMC;AACtB,UAAIb,WAAWG,QAAQH,YAAYA,SAAS;AAC1C;MACF;AAEAH,MAAAA,KAAIiB,MAAM,YAAY;QAAEX;MAAQ,GAAA;;;;;;AAChCO,eAAS,IAAIK,WAAWZ,QAAQC,OAAO,CAAA;IACzC;AAEAL,SAAKiB,YAAYL;AACjB,WAAO,MAAA;AACLZ,WAAKiB,YAAY;IACnB;EACF;AACJ;;;AClDA,SAASC,OAAAA,YAAW
|
|
6
|
-
"names": ["UAParser", "log", "browser", "os", "navigator", "parser", "userAgent", "getBrowser", "name", "getOS", "sendToIFrame", "iframe", "origin", "message", "contentWindow", "postMessage", "payload", "sendToParentWindow", "window", "parent", "createIFramePort", "channel", "onOrigin", "send", "data", "length", "buffer", "slice", "byteOffset", "byteLength", "subscribe", "callback", "handler", "event", "source", "isMessageData", "undefined", "Uint8Array", "addEventListener", "removeEventListener", "createIFrame", "id", "hidden", "allow", "create", "document", "createElement", "src", "setAttribute", "body", "appendChild", "getElementById", "log", "createWorkerPort", "port", "channel", "subscribe", "send", "message", "payload", "buffer", "slice", "byteOffset", "byteLength", "postMessage", "callback", "handler", "event", "data", "debug", "Uint8Array", "onmessage", "log", "PortMuxer", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { UAParser } from 'ua-parser-js';\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from '../message';\n\nlet browser: string | undefined;\nlet os: string | undefined;\n\nif (typeof navigator !== 'undefined') {\n // TODO(wittjosiah): Stop user agent parsing.\n const parser = new UAParser(navigator.userAgent);\n browser = parser.getBrowser().name;\n os = parser.getOS().name;\n}\n\nconst sendToIFrame = (iframe: HTMLIFrameElement, origin: string, message: MessageData) => {\n if (!iframe.contentWindow) {\n log('IFrame content window missing', { origin });\n return;\n }\n\n if (browser === 'Chrome' && os === 'iOS') {\n iframe.contentWindow.postMessage(message, origin);\n } else {\n iframe.contentWindow.postMessage(message, origin, [message.payload]);\n }\n};\n\nconst sendToParentWindow = (origin: string, message: MessageData) => {\n if (browser === 'Chrome' && os === 'iOS') {\n window.parent.postMessage(message, origin);\n } else {\n window.parent.postMessage(message, origin, [message.payload]);\n }\n};\n\nexport type IFramePortOptions = {\n channel: string;\n iframe?: HTMLIFrameElement;\n origin?: string;\n onOrigin?: (origin: string) => void;\n};\n\n/**\n * Create a RPC port with an iframe over window messaging.\n * @param options.channel Identifier for sent/recieved messages.\n * @param options.iframe Instance of the iframe if sending to child.\n * @param options.origin Origin of the destination window.\n * @param options.onOrigin Callback triggered when origin of destination window is verified.\n * @returns RPC port for messaging.\n */\nexport const createIFramePort = ({ channel, iframe, origin, onOrigin }: IFramePortOptions): RpcPort => {\n return {\n send: async (data) => {\n if (!origin) {\n log('no origin set', { channel });\n return;\n }\n\n log('sending', { channel, data: data.length });\n const payload = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);\n const message = { channel, payload };\n if (iframe) {\n sendToIFrame(iframe, origin, message);\n } else {\n sendToParentWindow(origin, message);\n }\n },\n\n subscribe: (callback) => {\n const handler = (event: MessageEvent<unknown>) => {\n if (!iframe && event.source !== window.parent) {\n // Not from parent window.\n return;\n } else if (iframe && event.source !== iframe.contentWindow) {\n // Not from child window.\n return;\n }\n\n const isMessageData =\n event.data && typeof event.data === 'object' && 'channel' in event.data && 'payload' in event.data;\n const message = isMessageData ? (event.data as MessageData) : undefined;\n if (message?.channel !== channel) {\n return;\n }\n\n if (!origin) {\n origin = event.origin;\n onOrigin?.(origin);\n }\n\n log('received', message);\n callback(new Uint8Array(message.payload));\n };\n\n window.addEventListener('message', handler);\n return () => window.removeEventListener('message', handler);\n },\n };\n};\n\nexport type CreateIFrameOptions = {\n hidden?: boolean;\n allow?: string;\n};\n\n/**\n * Create a hidden iframe and insert it into the DOM.\n * If an element with the same id already exists it will be returned instead.\n * @param source Source of the iframe.\n * @param id DOM id of the iframe.\n * @returns The created iframe.\n */\nexport const createIFrame = (source: string, id: string, { hidden = true, allow }: CreateIFrameOptions = {}) => {\n const create = () => {\n const iframe = document.createElement('iframe') as HTMLIFrameElement;\n iframe.id = id;\n iframe.src = source;\n hidden && iframe.setAttribute('style', 'display: none;');\n allow && iframe.setAttribute('allow', allow);\n document.body.appendChild(iframe);\n return iframe;\n };\n\n return (document.getElementById(id) as HTMLIFrameElement) ?? create();\n};\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from '../message';\n\nexport type WorkerPortOptions = {\n port: MessagePort;\n channel?: string;\n subscribe?: RpcPort['subscribe'];\n};\n\n/**\n * Create a RPC port for a worker.\n * @param options.port Message port to send message on.\n * @param options.channel Identifier for sent/recieved messages.\n * @param options.subscribe\n * @returns RPC port for messaging.\n */\n// TODO(wittjosiah): Rename for more general purpose MessagePort.\nexport const createWorkerPort = ({ port, channel, subscribe }: WorkerPortOptions): RpcPort => ({\n send: async (message) => {\n // Based on https://stackoverflow.com/a/54646864/2804332.\n const payload = message.buffer.slice(message.byteOffset, message.byteOffset + message.byteLength);\n port.postMessage(\n {\n channel,\n payload,\n },\n [payload],\n );\n },\n\n subscribe:\n subscribe ??\n ((callback) => {\n const handler = (event: MessageEvent<MessageData>) => {\n const message = event.data;\n if (channel && message.channel !== channel) {\n return;\n }\n\n log.debug('received', { message });\n callback(new Uint8Array(message.payload));\n };\n\n port.onmessage = handler;\n return () => {\n port.onmessage = null;\n };\n }),\n});\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from './message';\nimport { type IFramePortOptions, type WorkerPortOptions, createIFramePort, createWorkerPort } from './ports';\n\n/**\n * Facilitates the multiplexing of multiple RpcPorts over a single MessagePort.\n */\nexport class PortMuxer {\n private readonly _activeChannels = new Map<string, (msg: Uint8Array) => void>();\n\n private readonly _rpcPorts = new Map<string, RpcPort>();\n\n constructor(private readonly _messagePort?: MessagePort) {\n if (this._messagePort) {\n this._messagePort.onmessage = (event) => this.onWorkerMessage(event);\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('message', (event) => this.onWindowMessage(event));\n }\n }\n\n createWorkerPort(options: Omit<WorkerPortOptions, 'port' | 'subscribe' | 'channel'> & { channel: string }): RpcPort {\n if (!this._messagePort) {\n throw new Error('Message port is required to create worker ports');\n }\n\n const port = createWorkerPort({\n ...options,\n port: this._messagePort,\n subscribe: (callback) => {\n this._activeChannels.set(options.channel, callback);\n return () => this._activeChannels.delete(options.channel);\n },\n });\n this._rpcPorts.set(options.channel, port);\n\n return port;\n }\n\n createIFramePort(options: IFramePortOptions): RpcPort {\n const port = createIFramePort(options);\n this._rpcPorts.set(options.channel, port);\n\n return port;\n }\n\n private onWorkerMessage(event: MessageEvent<MessageData>): void {\n const message = event.data;\n log.debug('Recieved message from worker port', {\n channel: message.channel,\n payload: message.payload,\n });\n\n const callback = this._activeChannels.get(message.channel);\n callback?.(new Uint8Array(message.payload));\n }\n\n private onWindowMessage(event: MessageEvent<MessageData>): void {\n const message = event.data;\n log.debug('Recieved message from window', {\n channel: message.channel,\n payload: message.payload,\n });\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,gBAAgB;AAEzB,SAASC,WAAW;;AAKpB,IAAIC;AACJ,IAAIC;AAEJ,IAAI,OAAOC,cAAc,aAAa;AAEpC,QAAMC,SAAS,IAAIL,SAASI,UAAUE,SAAS;AAC/CJ,YAAUG,OAAOE,WAAU,EAAGC;AAC9BL,OAAKE,OAAOI,MAAK,EAAGD;AACtB;AAEA,IAAME,eAAe,CAACC,QAA2BC,QAAgBC,YAAAA;AAC/D,MAAI,CAACF,OAAOG,eAAe;AACzBb,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kBAAkB,oBAAIgB,IAAAA;SAEtBZ,YAAY,oBAAIY,IAAAA;AAG/B,QAAI,KAAKrB,cAAc;AACrB,WAAKA,aAAasB,YAAY,CAACV,UAAU,KAAKD,gBAAgBC,KAAAA;IAChE;AAEA,QAAI,OAAOW,WAAW,aAAa;AACjCA,aAAOC,iBAAiB,WAAW,CAACZ,UAAU,KAAKQ,gBAAgBR,KAAAA,CAAAA;IACrE;EACF;AA6CF;",
|
|
6
|
+
"names": ["UAParser", "log", "browser", "os", "navigator", "parser", "userAgent", "getBrowser", "name", "getOS", "sendToIFrame", "iframe", "origin", "message", "contentWindow", "postMessage", "payload", "sendToParentWindow", "window", "parent", "createIFramePort", "channel", "onOrigin", "send", "data", "length", "buffer", "slice", "byteOffset", "byteLength", "subscribe", "callback", "handler", "event", "source", "isMessageData", "undefined", "Uint8Array", "addEventListener", "removeEventListener", "createIFrame", "id", "hidden", "allow", "create", "document", "createElement", "src", "setAttribute", "body", "appendChild", "getElementById", "log", "createWorkerPort", "port", "channel", "subscribe", "send", "message", "payload", "buffer", "slice", "byteOffset", "byteLength", "postMessage", "callback", "handler", "event", "data", "debug", "Uint8Array", "onmessage", "log", "PortMuxer", "createWorkerPort", "options", "_messagePort", "Error", "port", "subscribe", "callback", "_activeChannels", "set", "channel", "delete", "_rpcPorts", "createIFramePort", "onWorkerMessage", "event", "message", "data", "log", "debug", "payload", "get", "Uint8Array", "onWindowMessage", "Map", "onmessage", "window", "addEventListener"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/
|
|
1
|
+
{"inputs":{"src/ports/iframe.ts":{"bytes":13983,"imports":[{"path":"ua-parser-js","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}],"format":"esm"},"src/ports/worker.ts":{"bytes":4832,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true}],"format":"esm"},"src/ports/index.ts":{"bytes":542,"imports":[{"path":"src/ports/iframe.ts","kind":"import-statement","original":"./iframe"},{"path":"src/ports/worker.ts","kind":"import-statement","original":"./worker"}],"format":"esm"},"src/port-muxer.ts":{"bytes":8073,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"src/ports/index.ts","kind":"import-statement","original":"./ports"}],"format":"esm"},"src/index.ts":{"bytes":587,"imports":[{"path":"src/ports/index.ts","kind":"import-statement","original":"./ports"},{"path":"src/port-muxer.ts","kind":"import-statement","original":"./port-muxer"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12872},"dist/lib/browser/index.mjs":{"imports":[{"path":"ua-parser-js","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}],"exports":["PortMuxer","createIFrame","createIFramePort","createWorkerPort"],"entryPoint":"src/index.ts","inputs":{"src/ports/iframe.ts":{"bytesInOutput":3186},"src/ports/index.ts":{"bytesInOutput":0},"src/ports/worker.ts":{"bytesInOutput":902},"src/index.ts":{"bytesInOutput":0},"src/port-muxer.ts":{"bytesInOutput":2244}},"bytes":6516}}}
|
|
@@ -154,19 +154,21 @@ var createWorkerPort = ({ port, channel, subscribe }) => ({
|
|
|
154
154
|
|
|
155
155
|
// src/port-muxer.ts
|
|
156
156
|
import { log as log3 } from "@dxos/log";
|
|
157
|
+
function _define_property(obj, key, value) {
|
|
158
|
+
if (key in obj) {
|
|
159
|
+
Object.defineProperty(obj, key, {
|
|
160
|
+
value,
|
|
161
|
+
enumerable: true,
|
|
162
|
+
configurable: true,
|
|
163
|
+
writable: true
|
|
164
|
+
});
|
|
165
|
+
} else {
|
|
166
|
+
obj[key] = value;
|
|
167
|
+
}
|
|
168
|
+
return obj;
|
|
169
|
+
}
|
|
157
170
|
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/rpc-tunnel/src/port-muxer.ts";
|
|
158
171
|
var PortMuxer = class {
|
|
159
|
-
constructor(_messagePort) {
|
|
160
|
-
this._messagePort = _messagePort;
|
|
161
|
-
this._activeChannels = /* @__PURE__ */ new Map();
|
|
162
|
-
this._rpcPorts = /* @__PURE__ */ new Map();
|
|
163
|
-
if (this._messagePort) {
|
|
164
|
-
this._messagePort.onmessage = (event) => this.onWorkerMessage(event);
|
|
165
|
-
}
|
|
166
|
-
if (typeof window !== "undefined") {
|
|
167
|
-
window.addEventListener("message", (event) => this.onWindowMessage(event));
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
172
|
createWorkerPort(options) {
|
|
171
173
|
if (!this._messagePort) {
|
|
172
174
|
throw new Error("Message port is required to create worker ports");
|
|
@@ -213,6 +215,20 @@ var PortMuxer = class {
|
|
|
213
215
|
C: (f, a) => f(...a)
|
|
214
216
|
});
|
|
215
217
|
}
|
|
218
|
+
constructor(_messagePort) {
|
|
219
|
+
_define_property(this, "_messagePort", void 0);
|
|
220
|
+
_define_property(this, "_activeChannels", void 0);
|
|
221
|
+
_define_property(this, "_rpcPorts", void 0);
|
|
222
|
+
this._messagePort = _messagePort;
|
|
223
|
+
this._activeChannels = /* @__PURE__ */ new Map();
|
|
224
|
+
this._rpcPorts = /* @__PURE__ */ new Map();
|
|
225
|
+
if (this._messagePort) {
|
|
226
|
+
this._messagePort.onmessage = (event) => this.onWorkerMessage(event);
|
|
227
|
+
}
|
|
228
|
+
if (typeof window !== "undefined") {
|
|
229
|
+
window.addEventListener("message", (event) => this.onWindowMessage(event));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
216
232
|
};
|
|
217
233
|
export {
|
|
218
234
|
PortMuxer,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/ports/iframe.ts", "../../../src/ports/worker.ts", "../../../src/port-muxer.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { UAParser } from 'ua-parser-js';\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from '../message';\n\nlet browser: string | undefined;\nlet os: string | undefined;\n\nif (typeof navigator !== 'undefined') {\n // TODO(wittjosiah): Stop user agent parsing.\n const parser = new UAParser(navigator.userAgent);\n browser = parser.getBrowser().name;\n os = parser.getOS().name;\n}\n\nconst sendToIFrame = (iframe: HTMLIFrameElement, origin: string, message: MessageData) => {\n if (!iframe.contentWindow) {\n log('IFrame content window missing', { origin });\n return;\n }\n\n if (browser === 'Chrome' && os === 'iOS') {\n iframe.contentWindow.postMessage(message, origin);\n } else {\n iframe.contentWindow.postMessage(message, origin, [message.payload]);\n }\n};\n\nconst sendToParentWindow = (origin: string, message: MessageData) => {\n if (browser === 'Chrome' && os === 'iOS') {\n window.parent.postMessage(message, origin);\n } else {\n window.parent.postMessage(message, origin, [message.payload]);\n }\n};\n\nexport type IFramePortOptions = {\n channel: string;\n iframe?: HTMLIFrameElement;\n origin?: string;\n onOrigin?: (origin: string) => void;\n};\n\n/**\n * Create a RPC port with an iframe over window messaging.\n * @param options.channel Identifier for sent/recieved messages.\n * @param options.iframe Instance of the iframe if sending to child.\n * @param options.origin Origin of the destination window.\n * @param options.onOrigin Callback triggered when origin of destination window is verified.\n * @returns RPC port for messaging.\n */\nexport const createIFramePort = ({ channel, iframe, origin, onOrigin }: IFramePortOptions): RpcPort => {\n return {\n send: async (data) => {\n if (!origin) {\n log('no origin set', { channel });\n return;\n }\n\n log('sending', { channel, data: data.length });\n const payload = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);\n const message = { channel, payload };\n if (iframe) {\n sendToIFrame(iframe, origin, message);\n } else {\n sendToParentWindow(origin, message);\n }\n },\n\n subscribe: (callback) => {\n const handler = (event: MessageEvent<unknown>) => {\n if (!iframe && event.source !== window.parent) {\n // Not from parent window.\n return;\n } else if (iframe && event.source !== iframe.contentWindow) {\n // Not from child window.\n return;\n }\n\n const isMessageData =\n event.data && typeof event.data === 'object' && 'channel' in event.data && 'payload' in event.data;\n const message = isMessageData ? (event.data as MessageData) : undefined;\n if (message?.channel !== channel) {\n return;\n }\n\n if (!origin) {\n origin = event.origin;\n onOrigin?.(origin);\n }\n\n log('received', message);\n callback(new Uint8Array(message.payload));\n };\n\n window.addEventListener('message', handler);\n return () => window.removeEventListener('message', handler);\n },\n };\n};\n\nexport type CreateIFrameOptions = {\n hidden?: boolean;\n allow?: string;\n};\n\n/**\n * Create a hidden iframe and insert it into the DOM.\n * If an element with the same id already exists it will be returned instead.\n * @param source Source of the iframe.\n * @param id DOM id of the iframe.\n * @returns The created iframe.\n */\nexport const createIFrame = (source: string, id: string, { hidden = true, allow }: CreateIFrameOptions = {}) => {\n const create = () => {\n const iframe = document.createElement('iframe') as HTMLIFrameElement;\n iframe.id = id;\n iframe.src = source;\n hidden && iframe.setAttribute('style', 'display: none;');\n allow && iframe.setAttribute('allow', allow);\n document.body.appendChild(iframe);\n return iframe;\n };\n\n return (document.getElementById(id) as HTMLIFrameElement) ?? create();\n};\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from '../message';\n\nexport type WorkerPortOptions = {\n port: MessagePort;\n channel?: string;\n subscribe?: RpcPort['subscribe'];\n};\n\n/**\n * Create a RPC port for a worker.\n * @param options.port Message port to send message on.\n * @param options.channel Identifier for sent/recieved messages.\n * @param options.subscribe\n * @returns RPC port for messaging.\n */\n// TODO(wittjosiah): Rename for more general purpose MessagePort.\nexport const createWorkerPort = ({ port, channel, subscribe }: WorkerPortOptions): RpcPort => ({\n send: async (message) => {\n // Based on https://stackoverflow.com/a/54646864/2804332.\n const payload = message.buffer.slice(message.byteOffset, message.byteOffset + message.byteLength);\n port.postMessage(\n {\n channel,\n payload,\n },\n [payload],\n );\n },\n\n subscribe:\n subscribe ??\n ((callback) => {\n const handler = (event: MessageEvent<MessageData>) => {\n const message = event.data;\n if (channel && message.channel !== channel) {\n return;\n }\n\n log.debug('received', { message });\n callback(new Uint8Array(message.payload));\n };\n\n port.onmessage = handler;\n return () => {\n port.onmessage = null;\n };\n }),\n});\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from './message';\nimport {
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,gBAAgB;AAEzB,SAASC,WAAW;;AAKpB,IAAIC;AACJ,IAAIC;AAEJ,IAAI,OAAOC,cAAc,aAAa;AAEpC,QAAMC,SAAS,IAAIL,SAASI,UAAUE,SAAS;AAC/CJ,YAAUG,OAAOE,WAAU,EAAGC;AAC9BL,OAAKE,OAAOI,MAAK,EAAGD;AACtB;AAEA,IAAME,eAAe,CAACC,QAA2BC,QAAgBC,YAAAA;AAC/D,MAAI,CAACF,OAAOG,eAAe;AACzBb,QAAI,iCAAiC;MAAEW;IAAO,GAAA;;;;;;AAC9C;EACF;AAEA,MAAIV,YAAY,YAAYC,OAAO,OAAO;AACxCQ,WAAOG,cAAcC,YAAYF,SAASD,MAAAA;EAC5C,OAAO;AACLD,WAAOG,cAAcC,YAAYF,SAASD,QAAQ;MAACC,QAAQG;KAAQ;EACrE;AACF;AAEA,IAAMC,qBAAqB,CAACL,QAAgBC,YAAAA;AAC1C,MAAIX,YAAY,YAAYC,OAAO,OAAO;AACxCe,WAAOC,OAAOJ,YAAYF,SAASD,MAAAA;EACrC,OAAO;AACLM,WAAOC,OAAOJ,YAAYF,SAASD,QAAQ;MAACC,QAAQG;KAAQ;EAC9D;AACF;AAiBO,IAAMI,mBAAmB,CAAC,EAAEC,SAASV,QAAQC,QAAQU,SAAQ,MAAqB;AACvF,SAAO;IACLC,MAAM,OAAOC,SAAAA;AACX,UAAI,CAACZ,QAAQ;AACXX,YAAI,iBAAiB;UAAEoB;QAAQ,GAAA;;;;;;AAC/B;MACF;AAEApB,UAAI,WAAW;QAAEoB;QAASG,MAAMA,KAAKC;MAAO,GAAA;;;;;;AAC5C,YAAMT,UAAU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AeV,EAAAA,KAA6BG,OAAAA;AAC/D;;;AC/HA,SAASQ,OAAAA,YAAW;;AAmBb,IAAMC,mBAAmB,CAAC,EAAEC,MAAMC,SAASC,UAAS,OAAoC;EAC7FC,MAAM,OAAOC,YAAAA;AAEX,UAAMC,UAAUD,QAAQE,OAAOC,MAAMH,QAAQI,YAAYJ,QAAQI,aAAaJ,QAAQK,UAAU;AAChGT,SAAKU,YACH;MACET;MACAI;IACF,GACA;MAACA;KAAQ;EAEb;EAEAH,WACEA,cACC,CAACS,aAAAA;AACA,UAAMC,UAAU,CAACC,UAAAA;AACf,YAAMT,UAAUS,MAAMC;AACtB,UAAIb,WAAWG,QAAQH,YAAYA,SAAS;AAC1C;MACF;AAEAH,MAAAA,KAAIiB,MAAM,YAAY;QAAEX;MAAQ,GAAA;;;;;;AAChCO,eAAS,IAAIK,WAAWZ,QAAQC,OAAO,CAAA;IACzC;AAEAL,SAAKiB,YAAYL;AACjB,WAAO,MAAA;AACLZ,WAAKiB,YAAY;IACnB;EACF;AACJ;;;AClDA,SAASC,OAAAA,YAAW
|
|
6
|
-
"names": ["UAParser", "log", "browser", "os", "navigator", "parser", "userAgent", "getBrowser", "name", "getOS", "sendToIFrame", "iframe", "origin", "message", "contentWindow", "postMessage", "payload", "sendToParentWindow", "window", "parent", "createIFramePort", "channel", "onOrigin", "send", "data", "length", "buffer", "slice", "byteOffset", "byteLength", "subscribe", "callback", "handler", "event", "source", "isMessageData", "undefined", "Uint8Array", "addEventListener", "removeEventListener", "createIFrame", "id", "hidden", "allow", "create", "document", "createElement", "src", "setAttribute", "body", "appendChild", "getElementById", "log", "createWorkerPort", "port", "channel", "subscribe", "send", "message", "payload", "buffer", "slice", "byteOffset", "byteLength", "postMessage", "callback", "handler", "event", "data", "debug", "Uint8Array", "onmessage", "log", "PortMuxer", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { UAParser } from 'ua-parser-js';\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from '../message';\n\nlet browser: string | undefined;\nlet os: string | undefined;\n\nif (typeof navigator !== 'undefined') {\n // TODO(wittjosiah): Stop user agent parsing.\n const parser = new UAParser(navigator.userAgent);\n browser = parser.getBrowser().name;\n os = parser.getOS().name;\n}\n\nconst sendToIFrame = (iframe: HTMLIFrameElement, origin: string, message: MessageData) => {\n if (!iframe.contentWindow) {\n log('IFrame content window missing', { origin });\n return;\n }\n\n if (browser === 'Chrome' && os === 'iOS') {\n iframe.contentWindow.postMessage(message, origin);\n } else {\n iframe.contentWindow.postMessage(message, origin, [message.payload]);\n }\n};\n\nconst sendToParentWindow = (origin: string, message: MessageData) => {\n if (browser === 'Chrome' && os === 'iOS') {\n window.parent.postMessage(message, origin);\n } else {\n window.parent.postMessage(message, origin, [message.payload]);\n }\n};\n\nexport type IFramePortOptions = {\n channel: string;\n iframe?: HTMLIFrameElement;\n origin?: string;\n onOrigin?: (origin: string) => void;\n};\n\n/**\n * Create a RPC port with an iframe over window messaging.\n * @param options.channel Identifier for sent/recieved messages.\n * @param options.iframe Instance of the iframe if sending to child.\n * @param options.origin Origin of the destination window.\n * @param options.onOrigin Callback triggered when origin of destination window is verified.\n * @returns RPC port for messaging.\n */\nexport const createIFramePort = ({ channel, iframe, origin, onOrigin }: IFramePortOptions): RpcPort => {\n return {\n send: async (data) => {\n if (!origin) {\n log('no origin set', { channel });\n return;\n }\n\n log('sending', { channel, data: data.length });\n const payload = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);\n const message = { channel, payload };\n if (iframe) {\n sendToIFrame(iframe, origin, message);\n } else {\n sendToParentWindow(origin, message);\n }\n },\n\n subscribe: (callback) => {\n const handler = (event: MessageEvent<unknown>) => {\n if (!iframe && event.source !== window.parent) {\n // Not from parent window.\n return;\n } else if (iframe && event.source !== iframe.contentWindow) {\n // Not from child window.\n return;\n }\n\n const isMessageData =\n event.data && typeof event.data === 'object' && 'channel' in event.data && 'payload' in event.data;\n const message = isMessageData ? (event.data as MessageData) : undefined;\n if (message?.channel !== channel) {\n return;\n }\n\n if (!origin) {\n origin = event.origin;\n onOrigin?.(origin);\n }\n\n log('received', message);\n callback(new Uint8Array(message.payload));\n };\n\n window.addEventListener('message', handler);\n return () => window.removeEventListener('message', handler);\n },\n };\n};\n\nexport type CreateIFrameOptions = {\n hidden?: boolean;\n allow?: string;\n};\n\n/**\n * Create a hidden iframe and insert it into the DOM.\n * If an element with the same id already exists it will be returned instead.\n * @param source Source of the iframe.\n * @param id DOM id of the iframe.\n * @returns The created iframe.\n */\nexport const createIFrame = (source: string, id: string, { hidden = true, allow }: CreateIFrameOptions = {}) => {\n const create = () => {\n const iframe = document.createElement('iframe') as HTMLIFrameElement;\n iframe.id = id;\n iframe.src = source;\n hidden && iframe.setAttribute('style', 'display: none;');\n allow && iframe.setAttribute('allow', allow);\n document.body.appendChild(iframe);\n return iframe;\n };\n\n return (document.getElementById(id) as HTMLIFrameElement) ?? create();\n};\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from '../message';\n\nexport type WorkerPortOptions = {\n port: MessagePort;\n channel?: string;\n subscribe?: RpcPort['subscribe'];\n};\n\n/**\n * Create a RPC port for a worker.\n * @param options.port Message port to send message on.\n * @param options.channel Identifier for sent/recieved messages.\n * @param options.subscribe\n * @returns RPC port for messaging.\n */\n// TODO(wittjosiah): Rename for more general purpose MessagePort.\nexport const createWorkerPort = ({ port, channel, subscribe }: WorkerPortOptions): RpcPort => ({\n send: async (message) => {\n // Based on https://stackoverflow.com/a/54646864/2804332.\n const payload = message.buffer.slice(message.byteOffset, message.byteOffset + message.byteLength);\n port.postMessage(\n {\n channel,\n payload,\n },\n [payload],\n );\n },\n\n subscribe:\n subscribe ??\n ((callback) => {\n const handler = (event: MessageEvent<MessageData>) => {\n const message = event.data;\n if (channel && message.channel !== channel) {\n return;\n }\n\n log.debug('received', { message });\n callback(new Uint8Array(message.payload));\n };\n\n port.onmessage = handler;\n return () => {\n port.onmessage = null;\n };\n }),\n});\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { type RpcPort } from '@dxos/rpc';\n\nimport { type MessageData } from './message';\nimport { type IFramePortOptions, type WorkerPortOptions, createIFramePort, createWorkerPort } from './ports';\n\n/**\n * Facilitates the multiplexing of multiple RpcPorts over a single MessagePort.\n */\nexport class PortMuxer {\n private readonly _activeChannels = new Map<string, (msg: Uint8Array) => void>();\n\n private readonly _rpcPorts = new Map<string, RpcPort>();\n\n constructor(private readonly _messagePort?: MessagePort) {\n if (this._messagePort) {\n this._messagePort.onmessage = (event) => this.onWorkerMessage(event);\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('message', (event) => this.onWindowMessage(event));\n }\n }\n\n createWorkerPort(options: Omit<WorkerPortOptions, 'port' | 'subscribe' | 'channel'> & { channel: string }): RpcPort {\n if (!this._messagePort) {\n throw new Error('Message port is required to create worker ports');\n }\n\n const port = createWorkerPort({\n ...options,\n port: this._messagePort,\n subscribe: (callback) => {\n this._activeChannels.set(options.channel, callback);\n return () => this._activeChannels.delete(options.channel);\n },\n });\n this._rpcPorts.set(options.channel, port);\n\n return port;\n }\n\n createIFramePort(options: IFramePortOptions): RpcPort {\n const port = createIFramePort(options);\n this._rpcPorts.set(options.channel, port);\n\n return port;\n }\n\n private onWorkerMessage(event: MessageEvent<MessageData>): void {\n const message = event.data;\n log.debug('Recieved message from worker port', {\n channel: message.channel,\n payload: message.payload,\n });\n\n const callback = this._activeChannels.get(message.channel);\n callback?.(new Uint8Array(message.payload));\n }\n\n private onWindowMessage(event: MessageEvent<MessageData>): void {\n const message = event.data;\n log.debug('Recieved message from window', {\n channel: message.channel,\n payload: message.payload,\n });\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,gBAAgB;AAEzB,SAASC,WAAW;;AAKpB,IAAIC;AACJ,IAAIC;AAEJ,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AAMC,UAAUD,MAAME;AACtBC,IAAAA,KAAIC,MAAM,gCAAgC;MACxCT,SAASM,QAAQN;MACjBU,SAASJ,QAAQI;IACnB,GAAA;;;;;;EACF;EApDA,YAA6BjB,cAA4B;;AAJzD,qBAAA,MAAiBK,mBAAjB,MAAA;AAEA,qBAAA,MAAiBI,aAAjB,MAAA;SAE6BT,eAAAA;SAJZK,kBAAkB,oBAAIgB,IAAAA;SAEtBZ,YAAY,oBAAIY,IAAAA;AAG/B,QAAI,KAAKrB,cAAc;AACrB,WAAKA,aAAasB,YAAY,CAACV,UAAU,KAAKD,gBAAgBC,KAAAA;IAChE;AAEA,QAAI,OAAOW,WAAW,aAAa;AACjCA,aAAOC,iBAAiB,WAAW,CAACZ,UAAU,KAAKQ,gBAAgBR,KAAAA,CAAAA;IACrE;EACF;AA6CF;",
|
|
6
|
+
"names": ["UAParser", "log", "browser", "os", "navigator", "parser", "userAgent", "getBrowser", "name", "getOS", "sendToIFrame", "iframe", "origin", "message", "contentWindow", "postMessage", "payload", "sendToParentWindow", "window", "parent", "createIFramePort", "channel", "onOrigin", "send", "data", "length", "buffer", "slice", "byteOffset", "byteLength", "subscribe", "callback", "handler", "event", "source", "isMessageData", "undefined", "Uint8Array", "addEventListener", "removeEventListener", "createIFrame", "id", "hidden", "allow", "create", "document", "createElement", "src", "setAttribute", "body", "appendChild", "getElementById", "log", "createWorkerPort", "port", "channel", "subscribe", "send", "message", "payload", "buffer", "slice", "byteOffset", "byteLength", "postMessage", "callback", "handler", "event", "data", "debug", "Uint8Array", "onmessage", "log", "PortMuxer", "createWorkerPort", "options", "_messagePort", "Error", "port", "subscribe", "callback", "_activeChannels", "set", "channel", "delete", "_rpcPorts", "createIFramePort", "onWorkerMessage", "event", "message", "data", "log", "debug", "payload", "get", "Uint8Array", "onWindowMessage", "Map", "onmessage", "window", "addEventListener"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/
|
|
1
|
+
{"inputs":{"src/ports/iframe.ts":{"bytes":13983,"imports":[{"path":"ua-parser-js","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}],"format":"esm"},"src/ports/worker.ts":{"bytes":4832,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true}],"format":"esm"},"src/ports/index.ts":{"bytes":542,"imports":[{"path":"src/ports/iframe.ts","kind":"import-statement","original":"./iframe"},{"path":"src/ports/worker.ts","kind":"import-statement","original":"./worker"}],"format":"esm"},"src/port-muxer.ts":{"bytes":8073,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"src/ports/index.ts","kind":"import-statement","original":"./ports"}],"format":"esm"},"src/index.ts":{"bytes":587,"imports":[{"path":"src/ports/index.ts","kind":"import-statement","original":"./ports"},{"path":"src/port-muxer.ts","kind":"import-statement","original":"./port-muxer"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12874},"dist/lib/node-esm/index.mjs":{"imports":[{"path":"ua-parser-js","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}],"exports":["PortMuxer","createIFrame","createIFramePort","createWorkerPort"],"entryPoint":"src/index.ts","inputs":{"src/ports/iframe.ts":{"bytesInOutput":3186},"src/ports/index.ts":{"bytesInOutput":0},"src/ports/worker.ts":{"bytesInOutput":902},"src/index.ts":{"bytesInOutput":0},"src/port-muxer.ts":{"bytesInOutput":2244}},"bytes":6609}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,mBAAmB,WAAW,CAAC;AAC/B,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"port-muxer.d.ts","sourceRoot":"","sources":["../../../src/port-muxer.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAGzC,OAAO,
|
|
1
|
+
{"version":3,"file":"port-muxer.d.ts","sourceRoot":"","sources":["../../../src/port-muxer.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAGzC,OAAO,EAAE,KAAK,iBAAiB,EAAE,KAAK,iBAAiB,EAAsC,MAAM,SAAS,CAAC;AAE7G;;GAEG;AACH,qBAAa,SAAS;IAKR,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC;IAJ1C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAgD;IAEhF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8B;gBAE3B,YAAY,CAAC,EAAE,WAAW,YAAA;IAUvD,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAAC,GAAG;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO;IAkBnH,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO;IAOrD,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,eAAe;CAOxB"}
|