@metapages/metapage 0.13.7 → 0.13.8

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/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@metapages/metapage",
3
3
  "public": true,
4
- "version": "0.13.7",
4
+ "version": "0.13.8",
5
5
  "description": "Connect web pages together",
6
6
  "repository": "https://github.com/metapages/metapage",
7
7
  "homepage": "https://metapages.org/",
8
8
  "main": "dist/index.js",
9
9
  "exports": "./dist/index.js",
10
10
  "files": [
11
- "dist"
11
+ "dist",
12
+ "src"
12
13
  ],
13
14
  "type": "module",
14
- "browser": "dist/browser/index.js",
15
+ "browser": "dist/index.js",
15
16
  "source": "src/index.ts",
16
17
  "types": "dist/index.d.ts",
17
18
  "dependencies": {
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from "./metapage/Metapage";
2
+ export * from "./metapage/Metaframe";
3
+ export * from "./metapage/MetapageTools";
4
+ export * from "./metapage/Shared";
5
+ export * from "./metapage/MetapageIFrameRpcClient";
6
+ export * from "./metapage/v0_4";
7
+ export * from "./metapage/data";
@@ -0,0 +1,8 @@
1
+ import {MetapageVersionsAll, MetaframeVersionsAll} from "./v0_4";
2
+
3
+ export const METAFRAME_JSON_FILE = "metaframe.json";
4
+ export const METAPAGE_KEY_DEFINITION = "metapage/definition";
5
+ export const METAPAGE_KEY_STATE = "metapage/state";
6
+
7
+ export const VERSION_METAPAGE = MetapageVersionsAll[MetapageVersionsAll.length - 1];
8
+ export const VERSION_METAFRAME = MetaframeVersionsAll[MetaframeVersionsAll.length - 1];
@@ -0,0 +1,547 @@
1
+ import { EventEmitter, ListenerFn } from "eventemitter3";
2
+ import {
3
+ VERSION_METAFRAME,
4
+ METAPAGE_KEY_STATE,
5
+ METAPAGE_KEY_DEFINITION,
6
+ } from "./Constants";
7
+ import {
8
+ MetaframeId,
9
+ MetaframeInputMap,
10
+ MetaframePipeId,
11
+ MetapageId,
12
+ ApiPayloadPluginRequest,
13
+ ApiPayloadPluginRequestMethod,
14
+ JsonRpcMethodsFromParent,
15
+ JsonRpcMethodsFromChild,
16
+ SetupIframeServerResponseData,
17
+ MinimumClientMessage,
18
+ VersionsMetapage,
19
+ } from "./v0_4";
20
+ import {
21
+ isDebugFromUrlsParams,
22
+ stringToRgb,
23
+ log as MetapageToolsLog,
24
+ merge,
25
+ pageLoaded,
26
+ } from "./MetapageTools";
27
+ import { isIframe } from "./Shared";
28
+ import { MetapageEventUrlHashUpdate } from "./v0_4/events";
29
+ import { deserializeInputs, serializeInputs } from "./data";
30
+
31
+ // TODO combine/unify MetaframeEvents and MetaframeLoadingState
32
+ export enum MetaframeLoadingState {
33
+ WaitingForPageLoad = "WaitingForPageLoad",
34
+ SentSetupIframeClientRequest = "SentSetupIframeClientRequest",
35
+ Ready = "Ready",
36
+ }
37
+
38
+ export enum MetaframeEvents {
39
+ Connected = "connected",
40
+ Error = "error",
41
+ Input = "input",
42
+ Inputs = "inputs",
43
+ Message = "message",
44
+ }
45
+
46
+ export type MetaframeOptions = {
47
+ disableHashChangeEvent?: boolean;
48
+ };
49
+
50
+ export class Metaframe extends EventEmitter<
51
+ MetaframeEvents | JsonRpcMethodsFromChild
52
+ > {
53
+ public static readonly version = VERSION_METAFRAME;
54
+
55
+ public static readonly ERROR = MetaframeEvents.Error;
56
+ public static readonly CONNECTED = MetaframeEvents.Connected;
57
+ public static readonly INPUT = MetaframeEvents.Input;
58
+ public static readonly INPUTS = MetaframeEvents.Inputs;
59
+ public static readonly MESSAGE = MetaframeEvents.Message;
60
+
61
+ public static deserializeInputs = deserializeInputs;
62
+ public static serializeInputs = serializeInputs;
63
+
64
+ _inputPipeValues: MetaframeInputMap = {};
65
+ _outputPipeValues: MetaframeInputMap = {};
66
+ _parentId: MetapageId | undefined;
67
+ _parentVersion: VersionsMetapage | undefined;
68
+ _isIframe: boolean;
69
+ _state: MetaframeLoadingState = MetaframeLoadingState.WaitingForPageLoad;
70
+ _messageSendCount = 0;
71
+
72
+ debug: boolean = isDebugFromUrlsParams();
73
+ color: string | undefined;
74
+ plugin: MetaframePlugin | undefined;
75
+ /**
76
+ * If this is false, Files and Blobs will not be automatically serialized and deserialized
77
+ * This is useful to avoid the overhead of serialization/deserialization if you know you won't be using it
78
+ */
79
+ isInputOutputBlobSerialization: boolean = true;
80
+
81
+ /**
82
+ * This is the (locally) unique id that the parent metapage
83
+ * assigns to the metaframe via iframe.name which we get here as window.name
84
+ */
85
+ id: string = window.name;
86
+
87
+ constructor(options?: MetaframeOptions) {
88
+ super();
89
+ this.debug = isDebugFromUrlsParams();
90
+ this._isIframe = isIframe();
91
+
92
+ this.addListener = this.addListener.bind(this);
93
+ this.dispose = this.dispose.bind(this);
94
+ this.error = this.error.bind(this);
95
+ this.getInput = this.getInput.bind(this);
96
+ this.getInputs = this.getInputs.bind(this);
97
+ this.log = this.log.bind(this);
98
+ this.logInternal = this.logInternal.bind(this);
99
+ this.onInput = this.onInput.bind(this);
100
+ this.onInputs = this.onInputs.bind(this);
101
+ this.onMessage = this.onMessage.bind(this);
102
+ this.sendRpc = this.sendRpc.bind(this);
103
+ this.setInput = this.setInput.bind(this);
104
+ this.setInputs = this.setInputs.bind(this);
105
+ this.setInternalInputsAndNotify =
106
+ this.setInternalInputsAndNotify.bind(this);
107
+ this.setOutput = this.setOutput.bind(this);
108
+ this.setOutputs = this.setOutputs.bind(this);
109
+ this.warn = this.warn.bind(this);
110
+ this._resolveSetupIframeServerResponse =
111
+ this._resolveSetupIframeServerResponse.bind(this);
112
+ this.addListenerReturnDisposer = this.addListenerReturnDisposer.bind(this);
113
+ this.connected = this.connected.bind(this);
114
+ this.disableNotifyOnHashUrlChange =
115
+ this.disableNotifyOnHashUrlChange.bind(this);
116
+ this._onHashUrlChange = this._onHashUrlChange.bind(this);
117
+
118
+ if (!this._isIframe) {
119
+ //Don't add any of the machinery, it only works if we're iframes.
120
+ //This will never return
121
+ // this.ready = new Promise((_) => {});
122
+ this.log("Not an iframe, metaframe code disabled");
123
+ return;
124
+ }
125
+
126
+ const thisRef = this;
127
+ // Do no listen or send messages until the page is loaded
128
+ // This iframe is not created UNTIL the parent page is loaded and listening to messages
129
+ pageLoaded().then(() => {
130
+ this.log("pageLoaded");
131
+ window.addEventListener("message", this.onMessage);
132
+ // Now that we're listening, request to the parent to register us so we can talk
133
+ thisRef.sendRpc(JsonRpcMethodsFromChild.SetupIframeClientRequest, {
134
+ version: Metaframe.version,
135
+ });
136
+ thisRef._state = MetaframeLoadingState.SentSetupIframeClientRequest;
137
+ });
138
+
139
+ if (!(options && options.disableHashChangeEvent)) {
140
+ window.addEventListener("hashchange", this._onHashUrlChange);
141
+ }
142
+ }
143
+
144
+ _resolveSetupIframeServerResponse(params: SetupIframeServerResponseData) {
145
+ if (this._state === MetaframeLoadingState.WaitingForPageLoad) {
146
+ throw "Got message but page has not finished loading, we should never get in this state";
147
+ }
148
+
149
+ (async () => {
150
+
151
+ if (!this._parentId) {
152
+ this._parentVersion = params.version;
153
+ this.color = stringToRgb(this.id);
154
+ this._parentId = params.parentId;
155
+ this.log(
156
+ `metapage[${this._parentId}](v${
157
+ this._parentVersion ? this._parentVersion : "unknown"
158
+ }) registered`
159
+ );
160
+
161
+
162
+ if (params.state && params.state.inputs) {
163
+ if (this.isInputOutputBlobSerialization) {
164
+ this._inputPipeValues = await deserializeInputs(params.state.inputs);
165
+ } else {
166
+ this._inputPipeValues = params.state.inputs;
167
+ }
168
+ }
169
+
170
+ // this._inputPipeValues =
171
+ // params.state && params.state.inputs
172
+ // ? this.isInputOutputBlobSerialization
173
+ // ? deserializeInputs(params.state.inputs)
174
+ // : params.state.inputs
175
+ // : this._inputPipeValues;
176
+
177
+ //Tell the parent we have registered.
178
+ this._state = MetaframeLoadingState.Ready;
179
+ // TODO why do we need Metaframe.version here? It was sent in the initial SetupIframeClientRequest
180
+ this.sendRpc(JsonRpcMethodsFromChild.SetupIframeServerResponseAck, {
181
+ version: Metaframe.version,
182
+ });
183
+
184
+ //Send notifications of initial inputs (if non-null)
185
+ //so you don't have to listen to the ready event if you don't want to
186
+ if (
187
+ this._inputPipeValues &&
188
+ Object.keys(this._inputPipeValues).length > 0
189
+ ) {
190
+ this.emit(MetaframeEvents.Inputs, this._inputPipeValues);
191
+ Object.keys(this._inputPipeValues).forEach((pipeId) =>
192
+ this.emit(
193
+ MetaframeEvents.Input,
194
+ pipeId,
195
+ this._inputPipeValues[pipeId]
196
+ )
197
+ );
198
+ }
199
+
200
+ this.emit(MetaframeEvents.Inputs, this._inputPipeValues);
201
+
202
+ // if this is a plugin, initialize the plugin object
203
+ if (params.plugin) {
204
+ this.plugin = new MetaframePlugin(this);
205
+ }
206
+
207
+ //Resolve AFTER sending inputs. This way consumers can either:
208
+ //1) Just listen to inputs updates. The first will be when the metaframe is ready
209
+ //2) Listen to the ready event, get the inputs if desired, and listen to subsequent
210
+ // inputs updates. You may not wish to respond to the first updates but you might
211
+ // want to know when the metaframe is ready
212
+ //*** Does this distinction make sense?
213
+ this.emit(MetaframeEvents.Connected);
214
+ } else {
215
+ this.log(
216
+ "Got JsonRpcMethods.SetupIframeServerResponse but already resolved"
217
+ );
218
+ }
219
+ })();
220
+ }
221
+
222
+ async connected(): Promise<void> {
223
+ if (this._state === MetaframeLoadingState.Ready) {
224
+ return;
225
+ }
226
+ return new Promise((resolve, _) => {
227
+ let disposer: () => void;
228
+ disposer = this.addListenerReturnDisposer(
229
+ MetaframeEvents.Connected,
230
+ () => {
231
+ resolve();
232
+ disposer();
233
+ }
234
+ );
235
+ });
236
+ }
237
+
238
+ addListenerReturnDisposer(
239
+ event: MetaframeEvents | JsonRpcMethodsFromChild,
240
+ listener: ListenerFn<any[]>
241
+ ): () => void {
242
+ super.addListener(event, listener);
243
+ const disposer = () => {
244
+ super.removeListener(event, listener);
245
+ };
246
+ return disposer;
247
+ }
248
+
249
+ public log(o: any, color?: string, backgroundColor?: string) {
250
+ if (!this.debug) {
251
+ return;
252
+ }
253
+ this.logInternal(o, color ? color : this.color);
254
+ }
255
+
256
+ public warn(o: any) {
257
+ if (!this.debug) {
258
+ return;
259
+ }
260
+ this.logInternal(o, "000", this.color);
261
+ }
262
+
263
+ public error(err: any) {
264
+ this.logInternal(err, this.color, "f00");
265
+ }
266
+
267
+ logInternal(o: any, color?: string, backgroundColor?: string) {
268
+ let s: string;
269
+ if (typeof o === "string") {
270
+ s = o as string;
271
+ } else if (typeof o === "number") {
272
+ s = o + "";
273
+ } else {
274
+ s = JSON.stringify(o);
275
+ }
276
+
277
+ color = color ? color + "" : color;
278
+
279
+ s = (this.id ? `Metaframe[${this.id}] ` : "") + `${s}`;
280
+ MetapageToolsLog(s, color, backgroundColor);
281
+ }
282
+
283
+ public dispose() {
284
+ super.removeAllListeners();
285
+ window.removeEventListener("message", this.onMessage);
286
+ this.disableNotifyOnHashUrlChange();
287
+ // @ts-ignore
288
+ this._inputPipeValues = undefined;
289
+ // @ts-ignore
290
+ this._outputPipeValues = undefined;
291
+ }
292
+
293
+ public addListener(
294
+ event: MetaframeEvents | JsonRpcMethodsFromChild,
295
+ listener: ListenerFn<any[]>
296
+ ) {
297
+ super.addListener(event, listener);
298
+
299
+ //If it is an input or output, set the current input/output values when
300
+ //attaching a listener on the next tick to ensure that the listener
301
+ //will always get a value if it exists
302
+ if (event === MetaframeEvents.Inputs) {
303
+ window.setTimeout(() => {
304
+ if (this._inputPipeValues) {
305
+ listener(this._inputPipeValues);
306
+ }
307
+ }, 0);
308
+ }
309
+ return this;
310
+ }
311
+
312
+ public onInput(pipeId: MetaframePipeId, listener: any): () => void {
313
+ return this.addListenerReturnDisposer(
314
+ MetaframeEvents.Input,
315
+ (pipe: MetaframePipeId, value: any) => {
316
+ if (pipeId === pipe) {
317
+ listener(value);
318
+ }
319
+ }
320
+ );
321
+ }
322
+
323
+ public onInputs(listener: (m: MetaframeInputMap) => void): () => void {
324
+ const disposer = this.addListenerReturnDisposer(
325
+ MetaframeEvents.Inputs,
326
+ listener
327
+ );
328
+ return disposer;
329
+ }
330
+
331
+ /**
332
+ * This is a particular use case: metapage inputs are saved outside
333
+ * the iframe, so when this iframe is restarted in the same metapage
334
+ * it will start with this value. So in a way, it can be used for
335
+ * state storage, by the metaframe itself.
336
+ */
337
+ public setInput(pipeId: MetaframePipeId, blob: any) {
338
+ var inputs: MetaframeInputMap = {};
339
+ inputs[pipeId] = blob;
340
+ this.setInputs(inputs);
341
+ }
342
+
343
+ /**
344
+ * This does NOT directly update internal inputs. It tells
345
+ * the metapage parent, which then updates back. So if there
346
+ * is no metapage parent, this will do nothing.
347
+ *
348
+ * @param inputs
349
+ */
350
+ public async setInputs(inputs: MetaframeInputMap) {
351
+ if (this.isInputOutputBlobSerialization) {
352
+ inputs = await deserializeInputs(inputs);
353
+ }
354
+ this.sendRpc(JsonRpcMethodsFromChild.InputsUpdate, inputs);
355
+ }
356
+
357
+ async setInternalInputsAndNotify(inputs: MetaframeInputMap) {
358
+ // this is where we deserialize the inputs
359
+
360
+ if (this.isInputOutputBlobSerialization) {
361
+ inputs = await deserializeInputs(inputs);
362
+ }
363
+
364
+ if (!merge(this._inputPipeValues, inputs)) {
365
+ return;
366
+ }
367
+
368
+ Object.keys(inputs).forEach((pipeId) =>
369
+ this.emit(MetaframeEvents.Input, pipeId, inputs[pipeId])
370
+ );
371
+ this.emit(MetaframeEvents.Inputs, inputs);
372
+
373
+ }
374
+
375
+ public getInput(pipeId: MetaframePipeId): any {
376
+ console.assert(!!pipeId);
377
+ return this._inputPipeValues[pipeId];
378
+ }
379
+
380
+ public getInputs(): MetaframeInputMap {
381
+ return this._inputPipeValues;
382
+ }
383
+
384
+ /**
385
+ * What does setting this to null mean?
386
+ * @param pipeId :MetaframePipeId [description]
387
+ * @param updateBlob :any [description]
388
+ */
389
+ public setOutput(pipeId: MetaframePipeId, updateBlob: any): void {
390
+ console.assert(!!pipeId);
391
+
392
+ var outputs: MetaframeInputMap = {};
393
+ outputs[pipeId] = updateBlob;
394
+
395
+ this.setOutputs(outputs);
396
+ }
397
+
398
+ public async setOutputs(outputs: MetaframeInputMap): Promise<void> {
399
+ if (this.isInputOutputBlobSerialization) {
400
+ outputs = await serializeInputs(outputs);
401
+ }
402
+ if (!merge(this._outputPipeValues, outputs)) {
403
+ return;
404
+ }
405
+ this.sendRpc(JsonRpcMethodsFromChild.OutputsUpdate, outputs);
406
+ }
407
+
408
+ /**
409
+ * If the hash params of our URL changes, e.g. from updating because
410
+ * our state changed, then notify the parent metapage so that the
411
+ * parent metapage can save the state
412
+ */
413
+ public disableNotifyOnHashUrlChange(): void {
414
+ window.removeEventListener("hashchange", this._onHashUrlChange);
415
+ }
416
+
417
+ /** Tell the parent metapage our hash params changed */
418
+ _onHashUrlChange(_: any): void {
419
+ const payload: MetapageEventUrlHashUpdate = {
420
+ hash: window.location.hash,
421
+ metaframe: this.id as MetaframeId,
422
+ };
423
+ this.sendRpc(JsonRpcMethodsFromChild.HashParamsUpdate, payload);
424
+ }
425
+
426
+ sendRpc(method: JsonRpcMethodsFromChild, params: any) {
427
+ if (this._isIframe) {
428
+ const message: MinimumClientMessage<any> = {
429
+ jsonrpc: "2.0",
430
+ id: ++this._messageSendCount, // just increment the counter for the id
431
+ method: method,
432
+ params: params,
433
+ iframeId: this.id,
434
+ parentId: this._parentId, // TODO this is likely not actually needed ? iframes cannot send to anyone but the parent? But the parent does not automatically know where a message comes from
435
+ };
436
+ window.parent.postMessage(message, "*");
437
+ } else {
438
+ this.log(
439
+ "Cannot send JSON-RPC window message: there is no window.parent which means we are not an iframe"
440
+ );
441
+ }
442
+ }
443
+
444
+ onMessage(e: MessageEvent) {
445
+ if (typeof e.data === "object") {
446
+ let jsonrpc: MinimumClientMessage<any> = e.data;
447
+ if (jsonrpc.jsonrpc === "2.0") {
448
+ //Make sure this is a jsonrpc object
449
+ var method = jsonrpc.method as JsonRpcMethodsFromParent;
450
+ if (
451
+ !(
452
+ method == JsonRpcMethodsFromParent.SetupIframeServerResponse ||
453
+ (jsonrpc.parentId == this._parentId && jsonrpc.iframeId == this.id)
454
+ )
455
+ ) {
456
+ this.log(
457
+ `window.message: received message but jsonrpc.parentId=${jsonrpc.parentId} _parentId=${this._parentId} jsonrpc.iframeId=${jsonrpc.iframeId} id=${this.id}`
458
+ );
459
+ return;
460
+ }
461
+
462
+ switch (method) {
463
+ case JsonRpcMethodsFromParent.SetupIframeServerResponse:
464
+ this._resolveSetupIframeServerResponse(jsonrpc.params);
465
+ break; //Handled elsewhere
466
+ case JsonRpcMethodsFromParent.InputsUpdate:
467
+ if (this._state !== MetaframeLoadingState.Ready) {
468
+ throw "Got InputsUpdate but metaframe is not MetaframeLoadingState.Ready";
469
+ }
470
+ this.setInternalInputsAndNotify(jsonrpc.params.inputs);
471
+ break;
472
+ case JsonRpcMethodsFromParent.MessageAck:
473
+ if (this.debug) this.log(`ACK: ${JSON.stringify(jsonrpc)}`);
474
+ break;
475
+ default:
476
+ if (this.debug)
477
+ this.log(
478
+ `window.message: unknown JSON-RPC method: ${JSON.stringify(
479
+ jsonrpc
480
+ )}`
481
+ );
482
+ break;
483
+ }
484
+
485
+ this.emit(MetaframeEvents.Message, jsonrpc);
486
+ }
487
+ }
488
+ }
489
+ }
490
+
491
+ /**
492
+ * A special kind of metaframe that can get and set the metapage definition
493
+ * and metapage state (so quite powerful).
494
+ */
495
+ export class MetaframePlugin {
496
+ _metaframe: Metaframe;
497
+
498
+ constructor(metaframe: Metaframe) {
499
+ this._metaframe = metaframe;
500
+ this.requestState = this.requestState.bind(this);
501
+ this.onState = this.onState.bind(this);
502
+ this.getState = this.getState.bind(this);
503
+ this.setState = this.setState.bind(this);
504
+ this.onDefinition = this.onDefinition.bind(this);
505
+ this.getDefinition = this.getDefinition.bind(this);
506
+ this.setDefinition = this.setDefinition.bind(this);
507
+ }
508
+
509
+ requestState() {
510
+ var payload: ApiPayloadPluginRequest = {
511
+ method: ApiPayloadPluginRequestMethod.State,
512
+ };
513
+ this._metaframe.sendRpc(JsonRpcMethodsFromChild.PluginRequest, payload);
514
+ }
515
+
516
+ onState(listener: (_: any) => void): () => void {
517
+ const disposer = this._metaframe.onInput(METAPAGE_KEY_STATE, listener);
518
+ if (this.getState()) {
519
+ listener(this.getState());
520
+ }
521
+ return disposer;
522
+ }
523
+
524
+ getState(): any {
525
+ return this._metaframe.getInput(METAPAGE_KEY_STATE);
526
+ }
527
+
528
+ setState(state: any) {
529
+ this._metaframe.setOutput(METAPAGE_KEY_STATE, state);
530
+ }
531
+
532
+ onDefinition(listener: (a: any) => void): () => void {
533
+ var disposer = this._metaframe.onInput(METAPAGE_KEY_DEFINITION, listener);
534
+ if (this.getDefinition()) {
535
+ listener(this.getDefinition());
536
+ }
537
+ return disposer;
538
+ }
539
+
540
+ setDefinition(definition: any) {
541
+ this._metaframe.setOutput(METAPAGE_KEY_DEFINITION, definition);
542
+ }
543
+
544
+ getDefinition(): any {
545
+ return this._metaframe.getInput(METAPAGE_KEY_DEFINITION);
546
+ }
547
+ }