penpal 6.2.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.
Files changed (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +234 -0
  3. package/dist/penpal.js +773 -0
  4. package/dist/penpal.min.js +1 -0
  5. package/dist/penpal.min.js.map +1 -0
  6. package/es5/child/connectToParent.js +111 -0
  7. package/es5/child/handleSynAckMessageFactory.js +58 -0
  8. package/es5/connectCallReceiver.js +101 -0
  9. package/es5/connectCallSender.js +122 -0
  10. package/es5/createDestructor.js +29 -0
  11. package/es5/createLogger.js +19 -0
  12. package/es5/enums.js +47 -0
  13. package/es5/errorSerialization.js +34 -0
  14. package/es5/generateId.js +14 -0
  15. package/es5/index.js +63 -0
  16. package/es5/indexForBundle.js +21 -0
  17. package/es5/methodSerialization.js +94 -0
  18. package/es5/parent/connectToChild.js +107 -0
  19. package/es5/parent/getOriginFromSrc.js +53 -0
  20. package/es5/parent/handleAckMessageFactory.js +66 -0
  21. package/es5/parent/handleSynMessageFactory.js +29 -0
  22. package/es5/parent/monitorIframeRemoval.js +34 -0
  23. package/es5/parent/validateIframeHasSrcOrSrcDoc.js +18 -0
  24. package/es5/startConnectionTimeout.js +30 -0
  25. package/es5/types.js +1 -0
  26. package/lib/child/connectToParent.d.ts +36 -0
  27. package/lib/child/connectToParent.js +73 -0
  28. package/lib/child/handleSynAckMessageFactory.d.ts +7 -0
  29. package/lib/child/handleSynAckMessageFactory.js +41 -0
  30. package/lib/connectCallReceiver.d.ts +7 -0
  31. package/lib/connectCallReceiver.js +71 -0
  32. package/lib/connectCallSender.d.ts +14 -0
  33. package/lib/connectCallSender.js +93 -0
  34. package/lib/createDestructor.d.ts +13 -0
  35. package/lib/createDestructor.js +18 -0
  36. package/lib/createLogger.d.ts +2 -0
  37. package/lib/createLogger.js +10 -0
  38. package/lib/enums.d.ts +22 -0
  39. package/lib/enums.js +27 -0
  40. package/lib/errorSerialization.d.ts +14 -0
  41. package/lib/errorSerialization.js +17 -0
  42. package/lib/generateId.d.ts +5 -0
  43. package/lib/generateId.js +5 -0
  44. package/lib/index.d.ts +4 -0
  45. package/lib/index.js +3 -0
  46. package/lib/indexForBundle.d.ts +21 -0
  47. package/lib/indexForBundle.js +8 -0
  48. package/lib/methodSerialization.d.ts +27 -0
  49. package/lib/methodSerialization.js +67 -0
  50. package/lib/parent/connectToChild.d.ts +31 -0
  51. package/lib/parent/connectToChild.js +66 -0
  52. package/lib/parent/getOriginFromSrc.d.ts +5 -0
  53. package/lib/parent/getOriginFromSrc.js +42 -0
  54. package/lib/parent/handleAckMessageFactory.d.ts +7 -0
  55. package/lib/parent/handleAckMessageFactory.js +47 -0
  56. package/lib/parent/handleSynMessageFactory.d.ts +6 -0
  57. package/lib/parent/handleSynMessageFactory.js +18 -0
  58. package/lib/parent/monitorIframeRemoval.d.ts +12 -0
  59. package/lib/parent/monitorIframeRemoval.js +22 -0
  60. package/lib/parent/validateIframeHasSrcOrSrcDoc.d.ts +2 -0
  61. package/lib/parent/validateIframeHasSrcOrSrcDoc.js +8 -0
  62. package/lib/startConnectionTimeout.d.ts +6 -0
  63. package/lib/startConnectionTimeout.js +18 -0
  64. package/lib/types.d.ts +114 -0
  65. package/lib/types.js +0 -0
  66. package/package.json +90 -0
@@ -0,0 +1,22 @@
1
+ const CHECK_IFRAME_IN_DOC_INTERVAL = 60000;
2
+ /**
3
+ * Monitors for iframe removal and destroys connection if iframe
4
+ * is found to have been removed from DOM. This is to prevent memory
5
+ * leaks when the iframe is removed from the document and the consumer
6
+ * hasn't called destroy(). Without this, event listeners attached to
7
+ * the window would stick around and since the event handlers have a
8
+ * reference to the iframe in their closures, the iframe would stick
9
+ * around too.
10
+ */
11
+ export default (iframe, destructor) => {
12
+ const { destroy, onDestroy } = destructor;
13
+ const checkIframeInDocIntervalId = setInterval(() => {
14
+ if (!iframe.isConnected) {
15
+ clearInterval(checkIframeInDocIntervalId);
16
+ destroy();
17
+ }
18
+ }, CHECK_IFRAME_IN_DOC_INTERVAL);
19
+ onDestroy(() => {
20
+ clearInterval(checkIframeInDocIntervalId);
21
+ });
22
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: (iframe: HTMLIFrameElement) => void;
2
+ export default _default;
@@ -0,0 +1,8 @@
1
+ import { ErrorCode } from '../enums';
2
+ export default (iframe) => {
3
+ if (!iframe.src && !iframe.srcdoc) {
4
+ const error = new Error('Iframe must have src or srcdoc property defined.');
5
+ error.code = ErrorCode.NoIframeSrc;
6
+ throw error;
7
+ }
8
+ };
@@ -0,0 +1,6 @@
1
+ declare const _default: (timeout: number | undefined, callback: Function) => Function;
2
+ /**
3
+ * Starts a timeout and calls the callback with an error
4
+ * if the timeout completes before the stop function is called.
5
+ */
6
+ export default _default;
@@ -0,0 +1,18 @@
1
+ import { ErrorCode } from './enums';
2
+ /**
3
+ * Starts a timeout and calls the callback with an error
4
+ * if the timeout completes before the stop function is called.
5
+ */
6
+ export default (timeout, callback) => {
7
+ let timeoutId;
8
+ if (timeout !== undefined) {
9
+ timeoutId = window.setTimeout(() => {
10
+ const error = new Error(`Connection timed out after ${timeout}ms`);
11
+ error.code = ErrorCode.ConnectionTimeout;
12
+ callback(error);
13
+ }, timeout);
14
+ }
15
+ return () => {
16
+ clearTimeout(timeoutId);
17
+ };
18
+ };
package/lib/types.d.ts ADDED
@@ -0,0 +1,114 @@
1
+ import { ErrorCode, MessageType, Resolution } from './enums';
2
+ /**
3
+ * An ACK handshake message.
4
+ */
5
+ export declare type AckMessage = {
6
+ penpal: MessageType.Ack;
7
+ methodNames: string[];
8
+ };
9
+ /**
10
+ * Extract keys of T whose values are are assignable to U.
11
+ */
12
+ declare type ExtractKeys<T, U> = {
13
+ [P in keyof T]: T[P] extends U ? P : never;
14
+ }[keyof T];
15
+ /**
16
+ * A mapped type to recursively convert non async methods into async methods and exclude
17
+ * any non function properties from T.
18
+ */
19
+ export declare type AsyncMethodReturns<T> = {
20
+ [K in ExtractKeys<T, Function | object>]: T[K] extends (...args: any) => PromiseLike<any> ? T[K] : T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<R> : AsyncMethodReturns<T[K]>;
21
+ };
22
+ /**
23
+ * A method call message.
24
+ */
25
+ export declare type CallMessage = {
26
+ penpal: MessageType.Call;
27
+ id: number;
28
+ methodName: string;
29
+ args: any[];
30
+ };
31
+ /**
32
+ * Methods that may be called that will invoke methods on the remote window.
33
+ */
34
+ export declare type CallSender = {
35
+ [index: string]: Function;
36
+ };
37
+ /**
38
+ * Connection object returned from calling connectToChild or connectToParent.
39
+ */
40
+ export declare type Connection<TCallSender extends object = CallSender> = {
41
+ /**
42
+ * A promise which will be resolved once a connection has been established.
43
+ */
44
+ promise: Promise<AsyncMethodReturns<TCallSender>>;
45
+ /**
46
+ * A method that, when called, will disconnect any messaging channels.
47
+ * You may call this even before a connection has been established.
48
+ */
49
+ destroy: Function;
50
+ };
51
+ /**
52
+ * Methods to expose to the remote window.
53
+ */
54
+ export declare type Methods = {
55
+ [index: string]: Methods | Function;
56
+ };
57
+ /**
58
+ * A map of key path to function. The flatted counterpart of Methods.
59
+ */
60
+ export declare type SerializedMethods = {
61
+ [index: string]: Function;
62
+ };
63
+ /**
64
+ * A Penpal-specific error.
65
+ */
66
+ export declare type PenpalError = Error & {
67
+ code: ErrorCode;
68
+ };
69
+ /**
70
+ * A method response message.
71
+ */
72
+ export declare type ReplyMessage = {
73
+ penpal: MessageType.Reply;
74
+ id: number;
75
+ resolution: Resolution;
76
+ returnValue: any;
77
+ returnValueIsError?: boolean;
78
+ };
79
+ /**
80
+ * A SYN-ACK handshake message.
81
+ */
82
+ export declare type SynAckMessage = {
83
+ penpal: MessageType.SynAck;
84
+ methodNames: string[];
85
+ };
86
+ /**
87
+ * A SYN handshake message.
88
+ */
89
+ export declare type SynMessage = {
90
+ penpal: MessageType.Syn;
91
+ };
92
+ export declare type WindowsInfo = {
93
+ /**
94
+ * A friendly name for the local window.
95
+ */
96
+ localName: 'Parent' | 'Child';
97
+ /**
98
+ * The local window.
99
+ */
100
+ local: Window;
101
+ /**
102
+ * The remote window.
103
+ */
104
+ remote: Window;
105
+ /**
106
+ * Origin that should be used for sending messages to the remote window.
107
+ */
108
+ originForSending: string;
109
+ /**
110
+ * Origin that should be used for receiving messages from the remote window.
111
+ */
112
+ originForReceiving: string;
113
+ };
114
+ export {};
package/lib/types.js ADDED
File without changes
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "penpal",
3
+ "version": "6.2.0",
4
+ "description": "A promise-based library for communicating with iframes via postMessage.",
5
+ "author": "Aaron Hardy <aaron@aaronhardy.com>",
6
+ "license": "MIT",
7
+ "bugs": {
8
+ "url": "https://github.com/Aaronius/penpal/issues"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/Aaronius/penpal.git"
13
+ },
14
+ "keywords": [
15
+ "iframe",
16
+ "postMessage",
17
+ "promise"
18
+ ],
19
+ "main": "es5/index.js",
20
+ "module": "lib/index.js",
21
+ "types": "lib/index.d.ts",
22
+ "scripts": {
23
+ "clean": "rimraf lib es5 dist",
24
+ "build:lib": "tsc",
25
+ "build:es5": "babel ./src --out-dir ./es5 --extensions '.ts'",
26
+ "build:dist": "rollup --config rollup.config.js && terser dist/penpal.js -o dist/penpal.min.js -m --source-map",
27
+ "build:analysis": "node ./scripts/filesize dist/penpal.min.js",
28
+ "build": "npm-run-all clean build:lib build:es5 build:dist build:analysis",
29
+ "lint": "eslint **/*.js",
30
+ "format": "prettier --write \"**/*.{json,ts,js,md,html,babelrc,eslintrc}\"",
31
+ "test": "npm-run-all clean runTests",
32
+ "test:watch": "npm-run-all clean runTests:watch",
33
+ "test:sauce": "npm-run-all build 'runTests -- --sauce'",
34
+ "runTests": "node ./scripts/test.js",
35
+ "runTests:watch": "npm run runTests -- --watch",
36
+ "prepublishOnly": "npm-run-all format lint test build",
37
+ "prepare": "husky install"
38
+ },
39
+ "lint-staged": {
40
+ "**/*.js": [
41
+ "eslint --cache --fix"
42
+ ],
43
+ "**/*.{json,ts,js,md,html,babelrc,eslintrc}": [
44
+ "prettier --write"
45
+ ]
46
+ },
47
+ "browserslist": [
48
+ "last 2 Chrome version",
49
+ "last 2 Firefox version",
50
+ "last 2 Safari version",
51
+ "last 2 Edge version"
52
+ ],
53
+ "files": [
54
+ "lib",
55
+ "es5",
56
+ "dist"
57
+ ],
58
+ "devDependencies": {
59
+ "@babel/cli": "^7.2.3",
60
+ "@babel/core": "^7.3.4",
61
+ "@babel/preset-env": "^7.3.4",
62
+ "@babel/preset-typescript": "^7.8.3",
63
+ "@metahub/karma-rollup-preprocessor": "^6.0.0",
64
+ "@rollup/plugin-typescript": "^4.0.0",
65
+ "connect": "^3.6.6",
66
+ "eslint": "^7.25.0",
67
+ "eslint-config-prettier": "^6.10.1",
68
+ "eslint-plugin-import": "^2.14.0",
69
+ "filesize": "^6.1.0",
70
+ "husky": "^6.0.0",
71
+ "jasmine-core": "^3.8.0",
72
+ "karma": "^5.0.2",
73
+ "karma-babel-preprocessor": "^8.0.0-beta.0",
74
+ "karma-chrome-launcher": "^3.1.0",
75
+ "karma-firefox-launcher": "^1.1.0",
76
+ "karma-jasmine": "^3.1.1",
77
+ "karma-sauce-launcher": "^4.1.3",
78
+ "lint-staged": "^11.0.0",
79
+ "npm-run-all": "^4.1.5",
80
+ "prettier": "2.0.4",
81
+ "rimraf": "^3.0.2",
82
+ "rollup": "^2.6.1",
83
+ "rollup-plugin-babel": "^4.3.2",
84
+ "rollup-plugin-typescript": "^1.0.1",
85
+ "serve-static": "^1.13.2",
86
+ "terser": "^4.6.11",
87
+ "typescript": "^3.8.3",
88
+ "yargs": "^15.3.1"
89
+ }
90
+ }