rx-postmessenger 3.0.0 → 4.0.0
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/README.md +77 -64
- package/{esm → dist}/MessageFactory.d.ts +1 -1
- package/dist/MessageFactory.js +34 -0
- package/{esm → dist}/MessageIDGenerator.d.ts +1 -1
- package/dist/MessageIDGenerator.js +19 -0
- package/{cjs → dist}/MessageValidator.d.ts +3 -3
- package/{esm → dist}/MessageValidator.js +19 -19
- package/{esm → dist}/Messenger.d.ts +6 -6
- package/dist/Messenger.js +79 -0
- package/{cjs → dist}/PostmessageAdapter.d.ts +1 -1
- package/dist/PostmessageAdapter.js +11 -0
- package/{esm → dist}/RxPostmessengerRequest.d.ts +4 -4
- package/dist/RxPostmessengerRequest.js +23 -0
- package/dist/functions/GUIDGenerator.js +10 -0
- package/dist/index.d.ts +4 -0
- package/{esm → dist}/index.js +8 -8
- package/{rx-postmessenger.d.ts → dist/public-api.d.ts} +11 -31
- package/{esm → dist}/types.d.ts +6 -6
- package/dist/types.js +1 -0
- package/package.json +33 -35
- package/CHANGELOG.md +0 -169
- package/cjs/MessageFactory.d.ts +0 -9
- package/cjs/MessageFactory.js +0 -38
- package/cjs/MessageIDGenerator.d.ts +0 -8
- package/cjs/MessageIDGenerator.js +0 -24
- package/cjs/MessageValidator.js +0 -44
- package/cjs/Messenger.d.ts +0 -52
- package/cjs/Messenger.js +0 -69
- package/cjs/PostmessageAdapter.js +0 -14
- package/cjs/RxPostmessengerRequest.d.ts +0 -11
- package/cjs/RxPostmessengerRequest.js +0 -28
- package/cjs/functions/GUIDGenerator.js +0 -59
- package/cjs/index.d.ts +0 -3
- package/cjs/index.js +0 -16
- package/cjs/types.d.ts +0 -58
- package/cjs/types.js +0 -2
- package/esm/MessageFactory.js +0 -35
- package/esm/MessageIDGenerator.js +0 -21
- package/esm/MessageValidator.d.ts +0 -29
- package/esm/Messenger.js +0 -66
- package/esm/PostmessageAdapter.d.ts +0 -7
- package/esm/PostmessageAdapter.js +0 -11
- package/esm/RxPostmessengerRequest.js +0 -25
- package/esm/functions/GUIDGenerator.d.ts +0 -1
- package/esm/functions/GUIDGenerator.js +0 -55
- package/esm/index.d.ts +0 -3
- package/jest.config.js +0 -10
- package/typedoc.json +0 -6
- package/umd/rx-postmessenger.js +0 -2069
- package/umd/rx-postmessenger.js.map +0 -1
- package/umd/rx-postmessenger.min.js +0 -2
- package/umd/rx-postmessenger.min.js.map +0 -1
- /package/{cjs → dist}/functions/GUIDGenerator.d.ts +0 -0
- /package/{esm/types.js → dist/public-api.js} +0 -0
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# rx-postmessenger
|
|
2
|
+
|
|
2
3
|
[](https://badge.fury.io/js/rx-postmessenger)
|
|
3
|
-
[](https://coveralls.io/github/JJWesterkamp/rx-postmessenger?branch=master)
|
|
4
|
+
[](https://github.com/JJWesterkamp/rx-postmessenger/actions/workflows/ci.yml)
|
|
5
5
|
|
|
6
6
|
Minimal [RxJS][rxgh] adapter for the [`Window # postMessage`][postmessage] API for request-response streams and notification streams across frame windows.
|
|
7
7
|
|
|
@@ -13,11 +13,11 @@ Each instance targets _one single_ `Window` object. It propagates incoming `Mess
|
|
|
13
13
|
|
|
14
14
|
## RxJS Interoperability
|
|
15
15
|
|
|
16
|
-
|
|
|
17
|
-
|
|
18
|
-
| RxJS v5.x
|
|
19
|
-
| RxJS v6.x
|
|
20
|
-
| RxJS v7.x
|
|
16
|
+
| | RxPostmessenger v1.x | RxPostmessenger v2.x | RxPostmessenger v3.x |
|
|
17
|
+
| --------- | :------------------: | :------------------: | :------------------: |
|
|
18
|
+
| RxJS v5.x | ✅ | ❌ | ❌ |
|
|
19
|
+
| RxJS v6.x | ❌ | ✅ | ✅ |
|
|
20
|
+
| RxJS v7.x | ❌ | ❌ | ✅ |
|
|
21
21
|
|
|
22
22
|
[View changelog.][changelog]
|
|
23
23
|
|
|
@@ -31,31 +31,31 @@ $ npm install rx-postmessenger --save
|
|
|
31
31
|
|
|
32
32
|
**Static methods**
|
|
33
33
|
|
|
34
|
-
|Method|Description|
|
|
35
|
-
|
|
36
|
-
|[`connect()`](#connecting-2-window-objects)|Connect `Window` objects by creating messenger instances
|
|
34
|
+
| Method | Description |
|
|
35
|
+
| :------------------------------------------ | :-------------------------------------------------------- |
|
|
36
|
+
| [`connect()`](#connecting-2-window-objects) | Connect `Window` objects by creating messenger instances. |
|
|
37
37
|
|
|
38
38
|
**`Messenger` Instance methods**
|
|
39
39
|
|
|
40
|
-
|Method|Description|
|
|
41
|
-
|
|
42
|
-
|[`notify()`](#sending-notifications)|Send notifications to the connected window
|
|
43
|
-
|[`notifications()`](#listening-for-inbound-notifications)|Listen for inbound notifications
|
|
44
|
-
|[`request()`](#sending-requests)|Send requests to the connected window
|
|
45
|
-
|[`requests()`](#listening-for-inbound-requests)|Listen for inbound requests
|
|
40
|
+
| Method | Description |
|
|
41
|
+
| :-------------------------------------------------------- | :------------------------------------------ |
|
|
42
|
+
| [`notify()`](#sending-notifications) | Send notifications to the connected window. |
|
|
43
|
+
| [`notifications()`](#listening-for-inbound-notifications) | Listen for inbound notifications. |
|
|
44
|
+
| [`request()`](#sending-requests) | Send requests to the connected window. |
|
|
45
|
+
| [`requests()`](#listening-for-inbound-requests) | Listen for inbound requests. |
|
|
46
46
|
|
|
47
47
|
**`Request` Instance methods**
|
|
48
48
|
|
|
49
|
-
|Method|Description|
|
|
50
|
-
|
|
51
|
-
|[`respond()`](#sending-request-responses)|Respond to the request with a certain payload.
|
|
49
|
+
| Method | Description |
|
|
50
|
+
| :---------------------------------------- | :--------------------------------------------- |
|
|
51
|
+
| [`respond()`](#sending-request-responses) | Respond to the request with a certain payload. |
|
|
52
52
|
|
|
53
53
|
---
|
|
54
54
|
|
|
55
55
|
## Usage
|
|
56
56
|
|
|
57
57
|
```javascript
|
|
58
|
-
import RxPostmessenger from 'rx-postmessenger'
|
|
58
|
+
import RxPostmessenger from 'rx-postmessenger'
|
|
59
59
|
```
|
|
60
60
|
|
|
61
61
|
### Static methods
|
|
@@ -71,54 +71,53 @@ Both ends of the connection should implement this package. One in a _parent_ pro
|
|
|
71
71
|
_At parent window - `https://parent-project.com`_
|
|
72
72
|
|
|
73
73
|
```javascript
|
|
74
|
-
const childMessenger = RxPostmessenger.connect(
|
|
75
|
-
someIFrame.contentWindow,
|
|
76
|
-
'https://child-project.com'
|
|
77
|
-
);
|
|
74
|
+
const childMessenger = RxPostmessenger.connect(someIFrame.contentWindow, 'https://child-project.com')
|
|
78
75
|
```
|
|
79
76
|
|
|
80
77
|
_At child window - `https://child-project.com`_
|
|
81
78
|
|
|
82
79
|
```javascript
|
|
83
|
-
const parentMessenger = RxPostmessenger.connect(
|
|
84
|
-
window.parent,
|
|
85
|
-
'https://parent-project.com'
|
|
86
|
-
);
|
|
80
|
+
const parentMessenger = RxPostmessenger.connect(window.parent, 'https://parent-project.com')
|
|
87
81
|
```
|
|
88
82
|
|
|
89
83
|
### `Messenger` Instance methods
|
|
90
84
|
|
|
91
85
|
#### Sending notifications
|
|
86
|
+
|
|
92
87
|
> ```typescript
|
|
93
88
|
> Messenger.notify<T>(channel: string, payload?: T): void
|
|
94
89
|
> ```
|
|
95
90
|
|
|
96
|
-
The messenger instances give you a way to send notifications to the other `Window` through the `notify()` method.
|
|
97
|
-
The notify method is void -- notifications are fire-and-forget.
|
|
91
|
+
The messenger instances give you a way to send notifications to the other `Window` through the `notify()` method.
|
|
92
|
+
The notify method is void -- notifications are fire-and-forget.
|
|
98
93
|
Use [`request()`](#sending-requests) instead if you require data back.
|
|
99
94
|
Consider an example where we want to notify a child window of price changes:
|
|
100
95
|
|
|
101
96
|
```javascript
|
|
102
97
|
childMessenger.notify('price-changed', {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
})
|
|
98
|
+
oldPrice: 12.5,
|
|
99
|
+
newPrice: 14.5,
|
|
100
|
+
})
|
|
106
101
|
```
|
|
107
102
|
|
|
108
103
|
#### Listening for inbound notifications
|
|
104
|
+
|
|
109
105
|
> ```typescript
|
|
110
106
|
> Messenger.notifications<T = any>(channel: string): Observable<T>
|
|
111
107
|
> ```
|
|
112
108
|
|
|
113
|
-
The child project can request an Observable stream for a certain notification channel.
|
|
114
|
-
In this case we're interested in `'price-changed'` events, but only the ones where the price increased.
|
|
109
|
+
The child project can request an Observable stream for a certain notification channel.
|
|
110
|
+
In this case we're interested in `'price-changed'` events, but only the ones where the price increased.
|
|
115
111
|
The ability to use RxJS operators can help us out:
|
|
116
112
|
|
|
117
113
|
```javascript
|
|
118
|
-
parentMessenger
|
|
114
|
+
parentMessenger
|
|
115
|
+
.notifications('price-changed')
|
|
116
|
+
.pipe(
|
|
119
117
|
filter(({ oldPrice, newPrice }) => newPrice > oldPrice),
|
|
120
118
|
map(({ oldPrice, newPrice }) => newPrice - oldPrice),
|
|
121
|
-
)
|
|
119
|
+
)
|
|
120
|
+
.subscribe((increase) => console.log(`Price increased with €${increase}!`))
|
|
122
121
|
|
|
123
122
|
// > 'Price increased with €2!'
|
|
124
123
|
```
|
|
@@ -129,46 +128,43 @@ parentMessenger.notifications('price-changed').pipe(
|
|
|
129
128
|
> Messenger.request<T = any, U = any>(channel: string, payload?: T): Observable<U>
|
|
130
129
|
> ```
|
|
131
130
|
|
|
132
|
-
RxPostmessenger also supports request - response communication.
|
|
133
|
-
At the requester side a request is initiated by calling the `request()` method with 1 or 2 arguments.
|
|
131
|
+
RxPostmessenger also supports request - response communication.
|
|
132
|
+
At the requester side a request is initiated by calling the `request()` method with 1 or 2 arguments.
|
|
134
133
|
The first is a request alias (actually just another channel) of our choice.
|
|
135
134
|
|
|
136
135
|
_A notification-channel and a request-channel can both have the same channel name without any problem._
|
|
137
136
|
|
|
138
|
-
An observable is returned that emits the response when arrived, and then completes.
|
|
137
|
+
An observable is returned that emits the response when arrived, and then completes.
|
|
139
138
|
Let's request a greeting from the child window, and tell it to localize the response to `'en'`:
|
|
140
139
|
|
|
141
140
|
```javascript
|
|
142
141
|
const greetingResponse$ = childMessenger.request('greeting', {
|
|
143
|
-
|
|
144
|
-
})
|
|
142
|
+
language: 'en',
|
|
143
|
+
})
|
|
145
144
|
```
|
|
146
145
|
|
|
147
146
|
We can then subscribe to the greeting response stream.
|
|
148
147
|
Provided that the greeting says something nice, we'll log it for everyone to see:
|
|
149
148
|
|
|
150
149
|
```javascript
|
|
151
|
-
greetingResponse$.pipe(
|
|
152
|
-
filter((greeting) => isNiceGreeting(greeting)),
|
|
153
|
-
).subscribe(console.log);
|
|
150
|
+
greetingResponse$.pipe(filter((greeting) => isNiceGreeting(greeting))).subscribe(console.log)
|
|
154
151
|
|
|
155
152
|
// > 'Hi parent!'
|
|
156
153
|
```
|
|
157
154
|
|
|
158
155
|
#### Listening for inbound requests
|
|
156
|
+
|
|
159
157
|
> ```typescript
|
|
160
158
|
> Messenger.requests<T = any, U = any>(channel: string): Observable<RxPostmessenger.Request<T, U>>
|
|
161
159
|
> ```
|
|
162
160
|
|
|
163
|
-
No greeting would ever be received by `parentMessenger` when the child project does not listen
|
|
164
|
-
for requests to handle and respond to. Let's not be rude and create a request stream for
|
|
165
|
-
`'greeting'` requests, and subscribe to it. We'll pass the `RxPostmessenger.Request` objects
|
|
161
|
+
No greeting would ever be received by `parentMessenger` when the child project does not listen
|
|
162
|
+
for requests to handle and respond to. Let's not be rude and create a request stream for
|
|
163
|
+
`'greeting'` requests, and subscribe to it. We'll pass the `RxPostmessenger.Request` objects
|
|
166
164
|
that the subscription receives into a function `handleGreetingRequest()`:
|
|
167
165
|
|
|
168
166
|
```javascript
|
|
169
|
-
parentMessenger
|
|
170
|
-
.requests('greeting')
|
|
171
|
-
.subscribe(handleGreetingRequest);
|
|
167
|
+
parentMessenger.requests('greeting').subscribe(handleGreetingRequest)
|
|
172
168
|
```
|
|
173
169
|
|
|
174
170
|
---
|
|
@@ -181,25 +177,21 @@ parentMessenger
|
|
|
181
177
|
> RxPostmessenger.Request<T, U> ~ respond(payload: U): void
|
|
182
178
|
> ```
|
|
183
179
|
|
|
184
|
-
The `requests` method returns an observable of `RxPostmessenger.Request` objects.
|
|
185
|
-
They provide a single method `respond` that accepts one argument: the response payload.
|
|
180
|
+
The `requests` method returns an observable of `RxPostmessenger.Request` objects.
|
|
181
|
+
They provide a single method `respond` that accepts one argument: the response payload.
|
|
186
182
|
Let's use the method on the requests we give to `handleGreetingRequest`:
|
|
187
183
|
|
|
188
184
|
```javascript
|
|
189
185
|
const handleGreetingRequest = (request) => {
|
|
186
|
+
// The data that was sent along with the request
|
|
187
|
+
const requestPayload = request.payload
|
|
190
188
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// A hypothetical greeting translator
|
|
195
|
-
const localizedGreeting = translateGreeting(
|
|
196
|
-
'Hi parent!',
|
|
197
|
-
requestPayload.language
|
|
198
|
-
);
|
|
189
|
+
// A hypothetical greeting translator
|
|
190
|
+
const localizedGreeting = translateGreeting('Hi parent!', requestPayload.language)
|
|
199
191
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
192
|
+
// Eventually respond to the request with some data (payload)
|
|
193
|
+
request.respond(localizedGreeting)
|
|
194
|
+
}
|
|
203
195
|
```
|
|
204
196
|
|
|
205
197
|
## License
|
|
@@ -211,3 +203,24 @@ The MIT License (MIT). See [license file] for more information.
|
|
|
211
203
|
[changelog]: https://github.com/JJWesterkamp/rx-postmessenger/tree/master/CHANGELOG.md
|
|
212
204
|
[postmessage]: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
|
|
213
205
|
[rxgh]: https://github.com/ReactiveX/RxJS
|
|
206
|
+
|
|
207
|
+
## Development
|
|
208
|
+
|
|
209
|
+
Use Node.js 24.15+ (`nvm use`) and install dependencies with `npm ci`.
|
|
210
|
+
|
|
211
|
+
- `npm run build` compiles `src` with `tsconfig.app.json` into `dist`, including declarations.
|
|
212
|
+
- `npm run dev` watches the library sources.
|
|
213
|
+
- `npm run check` checks both the app and spec TypeScript configs without emitting files.
|
|
214
|
+
- `npm test` runs the browser API tests in Vitest with jsdom.
|
|
215
|
+
- `npm run fmt` checks formatting; `npm run lint` runs type-aware Oxlint.
|
|
216
|
+
- `npm run lint:fix` formats files and applies available lint fixes.
|
|
217
|
+
- `npm run test:watch` watches tests; `npm run test:coverage` generates coverage reports.
|
|
218
|
+
|
|
219
|
+
The package ships ESM only, with generated types exposed through the package exports.
|
|
220
|
+
|
|
221
|
+
CI runs on GitHub Actions for pushes and pull requests in three stages:
|
|
222
|
+
static analysis (formatting, lint, types, and build), coverage tests with artifacts retained
|
|
223
|
+
for one week, and a non-blocking production dependency audit.
|
|
224
|
+
|
|
225
|
+
Vitest requires 100% statement, branch, function, and line coverage. CI publishes a
|
|
226
|
+
coverage table in the workflow summary, including when tests or coverage thresholds fail.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IMessageFactory, IMessageIDGenerator, INotificationObject, IRequestObject, IResponseObject } from './types';
|
|
1
|
+
import type { IMessageFactory, IMessageIDGenerator, INotificationObject, IRequestObject, IResponseObject } from './types.js';
|
|
2
2
|
export declare class MessageFactory implements IMessageFactory {
|
|
3
3
|
protected readonly IDGenerator: IMessageIDGenerator;
|
|
4
4
|
constructor(IDGenerator: IMessageIDGenerator);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export class MessageFactory {
|
|
2
|
+
IDGenerator;
|
|
3
|
+
constructor(IDGenerator) {
|
|
4
|
+
this.IDGenerator = IDGenerator;
|
|
5
|
+
}
|
|
6
|
+
makeNotification(channel, payload) {
|
|
7
|
+
return {
|
|
8
|
+
channel,
|
|
9
|
+
id: this.IDGenerator.generateID(),
|
|
10
|
+
payload,
|
|
11
|
+
type: 'notification',
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
makeRequest(channel, payload) {
|
|
15
|
+
return {
|
|
16
|
+
channel,
|
|
17
|
+
id: this.IDGenerator.generateID(),
|
|
18
|
+
payload,
|
|
19
|
+
type: 'request',
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
makeResponse(requestId, channel, payload) {
|
|
23
|
+
return {
|
|
24
|
+
channel,
|
|
25
|
+
id: this.IDGenerator.generateID(),
|
|
26
|
+
payload,
|
|
27
|
+
requestId,
|
|
28
|
+
type: 'response',
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
invalidateID(id) {
|
|
32
|
+
this.IDGenerator.invalidateID(id);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IMessageIDGenerator } from './types';
|
|
1
|
+
import type { IMessageIDGenerator } from './types.js';
|
|
2
2
|
export declare class MessageIDGenerator implements IMessageIDGenerator {
|
|
3
3
|
protected readonly gen: Iterator<string>;
|
|
4
4
|
protected readonly usedIDValues: string[];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { GUIDGenerator } from './functions/GUIDGenerator.js';
|
|
2
|
+
export class MessageIDGenerator {
|
|
3
|
+
gen;
|
|
4
|
+
usedIDValues = [];
|
|
5
|
+
constructor(gen = GUIDGenerator()) {
|
|
6
|
+
this.gen = gen;
|
|
7
|
+
}
|
|
8
|
+
generateID() {
|
|
9
|
+
let newID;
|
|
10
|
+
do {
|
|
11
|
+
newID = this.gen.next().value;
|
|
12
|
+
} while (this.usedIDValues.indexOf(newID) >= 0);
|
|
13
|
+
this.invalidateID(newID);
|
|
14
|
+
return newID;
|
|
15
|
+
}
|
|
16
|
+
invalidateID(id) {
|
|
17
|
+
this.usedIDValues.push(id);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnyMessage, IMessageValidator, IOwnMessageEvent } from './types';
|
|
1
|
+
import type { AnyMessage, IMessageValidator, IOwnMessageEvent } from './types.js';
|
|
2
2
|
export declare class MessageValidator implements IMessageValidator {
|
|
3
3
|
protected acceptedSource: Window;
|
|
4
4
|
protected acceptedOrigin: string;
|
|
@@ -22,8 +22,8 @@ export declare class MessageValidator implements IMessageValidator {
|
|
|
22
22
|
* object. This serves as runtime data format validation. If messages do not
|
|
23
23
|
* comply to the AnyMessage compound interface, the entire event is ignored.
|
|
24
24
|
*
|
|
25
|
-
* @param {
|
|
26
|
-
* @
|
|
25
|
+
* @param {any} message
|
|
26
|
+
* @returns {boolean}
|
|
27
27
|
*/
|
|
28
28
|
isWellFormedMessage(message: any): message is AnyMessage;
|
|
29
29
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export class MessageValidator {
|
|
2
|
+
acceptedSource;
|
|
3
|
+
acceptedOrigin;
|
|
4
|
+
constructor(acceptedSource, acceptedOrigin) {
|
|
3
5
|
this.acceptedSource = acceptedSource;
|
|
4
6
|
this.acceptedOrigin = acceptedOrigin;
|
|
5
7
|
}
|
|
@@ -16,26 +18,24 @@ var MessageValidator = /** @class */ (function () {
|
|
|
16
18
|
* same remoteOrigin, and still being able to distinguish between messages from
|
|
17
19
|
* such frames.
|
|
18
20
|
*/
|
|
19
|
-
|
|
20
|
-
return message instanceof MessageEvent
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
21
|
+
validate(message) {
|
|
22
|
+
return (message instanceof MessageEvent &&
|
|
23
|
+
message.origin === this.acceptedOrigin &&
|
|
24
|
+
message.source === this.acceptedSource &&
|
|
25
|
+
this.isWellFormedMessage(message.data));
|
|
26
|
+
}
|
|
25
27
|
/**
|
|
26
28
|
* Tests whether the data sent through postMessage is a well-formed message
|
|
27
29
|
* object. This serves as runtime data format validation. If messages do not
|
|
28
30
|
* comply to the AnyMessage compound interface, the entire event is ignored.
|
|
29
31
|
*
|
|
30
|
-
* @param {
|
|
31
|
-
* @
|
|
32
|
+
* @param {any} message
|
|
33
|
+
* @returns {boolean}
|
|
32
34
|
*/
|
|
33
|
-
|
|
34
|
-
return (typeof message.id === 'string'
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
}());
|
|
41
|
-
export { MessageValidator };
|
|
35
|
+
isWellFormedMessage(message) {
|
|
36
|
+
return (typeof message.id === 'string' &&
|
|
37
|
+
['request', 'response', 'notification'].indexOf(message.type) >= 0 &&
|
|
38
|
+
typeof message.channel === 'string' &&
|
|
39
|
+
(message.type !== 'response' || typeof message.requestId === 'string'));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Messenger as IMessenger, Request } from '
|
|
2
|
-
import type { AnyMessage, INotificationObject, IRequestObject, IResponseObject, MappedMessage, MessageType, IMessageFactory, IMessageValidator, IPostmessageAdapter } from './types';
|
|
3
|
-
import { Observable } from 'rxjs';
|
|
1
|
+
import type { Messenger as IMessenger, Request } from './public-api.js';
|
|
2
|
+
import type { AnyMessage, INotificationObject, IRequestObject, IResponseObject, MappedMessage, MessageType, IMessageFactory, IMessageValidator, IPostmessageAdapter } from './types.js';
|
|
3
|
+
import type { Observable } from 'rxjs';
|
|
4
4
|
export declare class Messenger implements IMessenger {
|
|
5
5
|
protected readonly messageFactory: IMessageFactory;
|
|
6
6
|
protected readonly messageValidator: IMessageValidator;
|
|
@@ -23,9 +23,9 @@ export declare class Messenger implements IMessenger {
|
|
|
23
23
|
*/
|
|
24
24
|
readonly responses$: Observable<IResponseObject>;
|
|
25
25
|
/**
|
|
26
|
-
* @param {IMessageFactory}
|
|
27
|
-
* @param {IMessageValidator}
|
|
28
|
-
* @param {IPostmessageAdapter} adapter
|
|
26
|
+
* @param {IMessageFactory} messageFactory - Factory instance for scalar message objects.
|
|
27
|
+
* @param {IMessageValidator} messageValidator - Validator instance for incoming messages.
|
|
28
|
+
* @param {IPostmessageAdapter} adapter - Adapter for interacting with the postMessage API
|
|
29
29
|
*/
|
|
30
30
|
constructor(messageFactory: IMessageFactory, messageValidator: IMessageValidator, adapter: IPostmessageAdapter);
|
|
31
31
|
request<T = any, U = any>(channel: string, payload?: T): Observable<U>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { fromEvent, share } from 'rxjs';
|
|
2
|
+
import { filter, map, pluck, take } from 'rxjs/operators';
|
|
3
|
+
import { RxPostmessengerRequest } from './RxPostmessengerRequest.js';
|
|
4
|
+
export class Messenger {
|
|
5
|
+
messageFactory;
|
|
6
|
+
messageValidator;
|
|
7
|
+
adapter;
|
|
8
|
+
/**
|
|
9
|
+
* Observable stream of all incoming messages that originate from remoteWindow with a
|
|
10
|
+
* remoteOrigin url matching this.remoteOrigin.
|
|
11
|
+
*/
|
|
12
|
+
inboundMessages$;
|
|
13
|
+
/**
|
|
14
|
+
* Filtered subset of inboundMessages$, emitting all messages of type 'notification'
|
|
15
|
+
*/
|
|
16
|
+
notifications$;
|
|
17
|
+
/**
|
|
18
|
+
* Filtered subset of inboundMessages$, emitting all messages of type 'request'
|
|
19
|
+
*/
|
|
20
|
+
requests$;
|
|
21
|
+
/**
|
|
22
|
+
* Filtered subset of inboundMessages$, emitting all messages of type 'response'
|
|
23
|
+
*/
|
|
24
|
+
responses$;
|
|
25
|
+
/**
|
|
26
|
+
* @param {IMessageFactory} messageFactory - Factory instance for scalar message objects.
|
|
27
|
+
* @param {IMessageValidator} messageValidator - Validator instance for incoming messages.
|
|
28
|
+
* @param {IPostmessageAdapter} adapter - Adapter for interacting with the postMessage API
|
|
29
|
+
*/
|
|
30
|
+
constructor(messageFactory, messageValidator, adapter) {
|
|
31
|
+
this.messageFactory = messageFactory;
|
|
32
|
+
this.messageValidator = messageValidator;
|
|
33
|
+
this.adapter = adapter;
|
|
34
|
+
this.inboundMessages$ = fromEvent(window, 'message').pipe(filter((message) => this.messageValidator.validate(message)), pluck('data'), share());
|
|
35
|
+
this.requests$ = this.messagesOfType('request');
|
|
36
|
+
this.responses$ = this.messagesOfType('response');
|
|
37
|
+
this.notifications$ = this.messagesOfType('notification');
|
|
38
|
+
this.inboundMessages$.subscribe(({ id }) => this.messageFactory.invalidateID(id));
|
|
39
|
+
}
|
|
40
|
+
request(channel, payload) {
|
|
41
|
+
const request = this.messageFactory.makeRequest(channel, payload);
|
|
42
|
+
const responseObservable = this.createResponseObservable(request.id);
|
|
43
|
+
this.adapter.postMessage(request);
|
|
44
|
+
return responseObservable;
|
|
45
|
+
}
|
|
46
|
+
notify(channel, payload) {
|
|
47
|
+
this.adapter.postMessage(this.messageFactory.makeNotification(channel, payload));
|
|
48
|
+
}
|
|
49
|
+
requests(channel) {
|
|
50
|
+
return this.requests$.pipe(filter((request) => request.channel === channel), map((request) => new RxPostmessengerRequest(request.id, request.channel, request.payload, (payload) => this.respond(request.id, channel, payload))));
|
|
51
|
+
}
|
|
52
|
+
notifications(channel) {
|
|
53
|
+
return this.notifications$.pipe(filter((notification) => notification.channel === channel), pluck('payload'));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Sends a response through given channel to the remote window, carrying given payload.
|
|
57
|
+
*/
|
|
58
|
+
respond(requestId, channel, payload) {
|
|
59
|
+
this.adapter.postMessage(this.messageFactory.makeResponse(requestId, channel, payload));
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates an Observable that completes after a single emission of the MessageEvent
|
|
64
|
+
* response for request of given requestId. If no matching response is ever received,
|
|
65
|
+
* the Observable never completes.
|
|
66
|
+
*/
|
|
67
|
+
createResponseObservable(requestId) {
|
|
68
|
+
return this.responses$.pipe(filter((response) => response.requestId === requestId), pluck('payload'), take(1));
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns an Observable emitting all inbound messages` of given type.
|
|
72
|
+
* Returns an Observable emitting a subset of MessageEvent objects emitted
|
|
73
|
+
* by this.inboundMessages$, passing through all MessageEvent objects whose
|
|
74
|
+
* data.type property matches given type.
|
|
75
|
+
*/
|
|
76
|
+
messagesOfType(type) {
|
|
77
|
+
return this.inboundMessages$.pipe(filter((message) => message.type === type));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnyMessage, IPostmessageAdapter } from './types';
|
|
1
|
+
import type { AnyMessage, IPostmessageAdapter } from './types.js';
|
|
2
2
|
export declare class PostmessageAdapter implements IPostmessageAdapter {
|
|
3
3
|
readonly targetWindow: Window;
|
|
4
4
|
readonly targetOrigin: string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export class PostmessageAdapter {
|
|
2
|
+
targetWindow;
|
|
3
|
+
targetOrigin;
|
|
4
|
+
constructor(targetWindow, targetOrigin) {
|
|
5
|
+
this.targetWindow = targetWindow;
|
|
6
|
+
this.targetOrigin = targetOrigin;
|
|
7
|
+
}
|
|
8
|
+
postMessage(data) {
|
|
9
|
+
this.targetWindow.postMessage(data, this.targetOrigin);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Request } from '
|
|
1
|
+
import type { Request } from './public-api.js';
|
|
2
2
|
export declare class RxPostmessengerRequest<T, U> implements Request<T, U> {
|
|
3
3
|
readonly id: string;
|
|
4
4
|
readonly channel: string;
|
|
5
5
|
readonly payload: T;
|
|
6
|
-
private readonly
|
|
7
|
-
private
|
|
8
|
-
constructor(id: string, channel: string, payload: T,
|
|
6
|
+
private readonly injectResponse;
|
|
7
|
+
private handled;
|
|
8
|
+
constructor(id: string, channel: string, payload: T, injectResponse: (data: U) => void);
|
|
9
9
|
get isHandled(): boolean;
|
|
10
10
|
respond(data: U): void;
|
|
11
11
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export class RxPostmessengerRequest {
|
|
2
|
+
id;
|
|
3
|
+
channel;
|
|
4
|
+
payload;
|
|
5
|
+
injectResponse;
|
|
6
|
+
handled = false;
|
|
7
|
+
constructor(id, channel, payload, injectResponse) {
|
|
8
|
+
this.id = id;
|
|
9
|
+
this.channel = channel;
|
|
10
|
+
this.payload = payload;
|
|
11
|
+
this.injectResponse = injectResponse;
|
|
12
|
+
}
|
|
13
|
+
get isHandled() {
|
|
14
|
+
return this.handled;
|
|
15
|
+
}
|
|
16
|
+
respond(data) {
|
|
17
|
+
if (this.handled) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.injectResponse(data);
|
|
21
|
+
this.handled = true;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function* GUIDGenerator() {
|
|
2
|
+
const s4 = () => Math.floor((1 + Math.random()) * 0x10000)
|
|
3
|
+
.toString(16)
|
|
4
|
+
.substring(1);
|
|
5
|
+
const IDSegment = (size = 1) => Array(size).fill(null).map(s4).join('');
|
|
6
|
+
const guid = () => [IDSegment(2), IDSegment(), IDSegment(), IDSegment(), IDSegment(3)].join('-');
|
|
7
|
+
while (true) {
|
|
8
|
+
yield guid();
|
|
9
|
+
}
|
|
10
|
+
}
|
package/dist/index.d.ts
ADDED
package/{esm → dist}/index.js
RENAMED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { MessageFactory } from './MessageFactory';
|
|
2
|
-
import { MessageIDGenerator } from './MessageIDGenerator';
|
|
3
|
-
import { MessageValidator } from './MessageValidator';
|
|
4
|
-
import { Messenger } from './Messenger';
|
|
5
|
-
import { PostmessageAdapter } from './PostmessageAdapter';
|
|
6
|
-
|
|
7
|
-
connect
|
|
1
|
+
import { MessageFactory } from './MessageFactory.js';
|
|
2
|
+
import { MessageIDGenerator } from './MessageIDGenerator.js';
|
|
3
|
+
import { MessageValidator } from './MessageValidator.js';
|
|
4
|
+
import { Messenger } from './Messenger.js';
|
|
5
|
+
import { PostmessageAdapter } from './PostmessageAdapter.js';
|
|
6
|
+
const defaultNamespace = {
|
|
7
|
+
connect(remoteWindow, remoteOrigin) {
|
|
8
8
|
if (window === remoteWindow) {
|
|
9
9
|
throw new Error('Remote window can not be the same as the local window environment');
|
|
10
10
|
}
|
|
11
11
|
return new Messenger(new MessageFactory(new MessageIDGenerator()), new MessageValidator(remoteWindow, remoteOrigin), new PostmessageAdapter(remoteWindow, remoteOrigin));
|
|
12
|
-
}
|
|
12
|
+
},
|
|
13
13
|
};
|
|
14
14
|
export default defaultNamespace;
|