penpal 7.0.3 → 7.0.4

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 (111) hide show
  1. package/dist/penpal.cjs +953 -0
  2. package/dist/penpal.cjs.map +1 -0
  3. package/dist/penpal.d.cts +234 -0
  4. package/dist/penpal.d.ts +234 -0
  5. package/dist/penpal.js +890 -1033
  6. package/dist/penpal.js.map +1 -0
  7. package/dist/penpal.min.js +1 -1
  8. package/dist/penpal.min.js.map +1 -1
  9. package/dist/penpal.mjs +943 -0
  10. package/dist/penpal.mjs.map +1 -0
  11. package/package.json +13 -21
  12. package/cjs/CallOptions.d.ts +0 -10
  13. package/cjs/CallOptions.js +0 -16
  14. package/cjs/ErrorCodeObj.d.ts +0 -9
  15. package/cjs/ErrorCodeObj.js +0 -14
  16. package/cjs/PenpalBugError.d.ts +0 -8
  17. package/cjs/PenpalBugError.js +0 -12
  18. package/cjs/PenpalError.d.ts +0 -6
  19. package/cjs/PenpalError.js +0 -11
  20. package/cjs/Reply.d.ts +0 -9
  21. package/cjs/Reply.js +0 -16
  22. package/cjs/backwardCompatibility.d.ts +0 -56
  23. package/cjs/backwardCompatibility.js +0 -134
  24. package/cjs/connect.d.ts +0 -33
  25. package/cjs/connect.js +0 -78
  26. package/cjs/connectCallHandler.d.ts +0 -8
  27. package/cjs/connectCallHandler.js +0 -90
  28. package/cjs/connectRemoteProxy.d.ts +0 -12
  29. package/cjs/connectRemoteProxy.js +0 -139
  30. package/cjs/debug.d.ts +0 -3
  31. package/cjs/debug.js +0 -8
  32. package/cjs/errorSerialization.d.ts +0 -9
  33. package/cjs/errorSerialization.js +0 -26
  34. package/cjs/generateId.d.ts +0 -8
  35. package/cjs/generateId.js +0 -11
  36. package/cjs/getPromiseWithResolvers.d.ts +0 -6
  37. package/cjs/getPromiseWithResolvers.js +0 -19
  38. package/cjs/guards.d.ts +0 -10
  39. package/cjs/guards.js +0 -40
  40. package/cjs/index.d.ts +0 -12
  41. package/cjs/index.js +0 -21
  42. package/cjs/indexForBundle.d.ts +0 -31
  43. package/cjs/indexForBundle.js +0 -22
  44. package/cjs/messengers/Messenger.d.ts +0 -14
  45. package/cjs/messengers/Messenger.js +0 -2
  46. package/cjs/messengers/PortMessenger.d.ts +0 -21
  47. package/cjs/messengers/PortMessenger.js +0 -47
  48. package/cjs/messengers/WindowMessenger.d.ts +0 -29
  49. package/cjs/messengers/WindowMessenger.js +0 -178
  50. package/cjs/messengers/WorkerMessenger.d.ts +0 -23
  51. package/cjs/messengers/WorkerMessenger.js +0 -86
  52. package/cjs/methodSerialization.d.ts +0 -22
  53. package/cjs/methodSerialization.js +0 -48
  54. package/cjs/namespace.d.ts +0 -2
  55. package/cjs/namespace.js +0 -3
  56. package/cjs/once.d.ts +0 -2
  57. package/cjs/once.js +0 -15
  58. package/cjs/shakeHands.d.ts +0 -76
  59. package/cjs/shakeHands.js +0 -190
  60. package/cjs/types.d.ts +0 -89
  61. package/cjs/types.js +0 -2
  62. package/lib/CallOptions.d.ts +0 -10
  63. package/lib/CallOptions.js +0 -14
  64. package/lib/ErrorCodeObj.d.ts +0 -9
  65. package/lib/ErrorCodeObj.js +0 -12
  66. package/lib/PenpalBugError.d.ts +0 -8
  67. package/lib/PenpalBugError.js +0 -10
  68. package/lib/PenpalError.d.ts +0 -6
  69. package/lib/PenpalError.js +0 -9
  70. package/lib/Reply.d.ts +0 -9
  71. package/lib/Reply.js +0 -14
  72. package/lib/backwardCompatibility.d.ts +0 -56
  73. package/lib/backwardCompatibility.js +0 -128
  74. package/lib/connect.d.ts +0 -33
  75. package/lib/connect.js +0 -76
  76. package/lib/connectCallHandler.d.ts +0 -8
  77. package/lib/connectCallHandler.js +0 -88
  78. package/lib/connectRemoteProxy.d.ts +0 -12
  79. package/lib/connectRemoteProxy.js +0 -137
  80. package/lib/debug.d.ts +0 -3
  81. package/lib/debug.js +0 -6
  82. package/lib/errorSerialization.d.ts +0 -9
  83. package/lib/errorSerialization.js +0 -21
  84. package/lib/generateId.d.ts +0 -8
  85. package/lib/generateId.js +0 -9
  86. package/lib/getPromiseWithResolvers.d.ts +0 -6
  87. package/lib/getPromiseWithResolvers.js +0 -17
  88. package/lib/guards.d.ts +0 -10
  89. package/lib/guards.js +0 -28
  90. package/lib/index.d.ts +0 -12
  91. package/lib/index.js +0 -9
  92. package/lib/indexForBundle.d.ts +0 -31
  93. package/lib/indexForBundle.js +0 -20
  94. package/lib/messengers/Messenger.d.ts +0 -14
  95. package/lib/messengers/Messenger.js +0 -1
  96. package/lib/messengers/PortMessenger.d.ts +0 -21
  97. package/lib/messengers/PortMessenger.js +0 -45
  98. package/lib/messengers/WindowMessenger.d.ts +0 -29
  99. package/lib/messengers/WindowMessenger.js +0 -176
  100. package/lib/messengers/WorkerMessenger.d.ts +0 -23
  101. package/lib/messengers/WorkerMessenger.js +0 -84
  102. package/lib/methodSerialization.d.ts +0 -22
  103. package/lib/methodSerialization.js +0 -42
  104. package/lib/namespace.d.ts +0 -2
  105. package/lib/namespace.js +0 -1
  106. package/lib/once.d.ts +0 -2
  107. package/lib/once.js +0 -13
  108. package/lib/shakeHands.d.ts +0 -76
  109. package/lib/shakeHands.js +0 -188
  110. package/lib/types.d.ts +0 -89
  111. package/lib/types.js +0 -1
package/cjs/connect.js DELETED
@@ -1,78 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const PenpalError_js_1 = require("./PenpalError.js");
4
- const shakeHands_js_1 = require("./shakeHands.js");
5
- const guards_js_1 = require("./guards.js");
6
- const once_js_1 = require("./once.js");
7
- const namespace_js_1 = require("./namespace.js");
8
- const usedMessengers = new WeakSet();
9
- /**
10
- * Attempts to establish communication with the remote.
11
- */
12
- const connect = ({ messenger, methods = {}, timeout, channel, log, }) => {
13
- if (!messenger) {
14
- throw new PenpalError_js_1.default('INVALID_ARGUMENT', 'messenger must be defined');
15
- }
16
- if (usedMessengers.has(messenger)) {
17
- throw new PenpalError_js_1.default('INVALID_ARGUMENT', 'A messenger can only be used for a single connection');
18
- }
19
- usedMessengers.add(messenger);
20
- const connectionDestroyedHandlers = [messenger.destroy];
21
- const destroyConnection = (0, once_js_1.default)((notifyOtherParticipant) => {
22
- if (notifyOtherParticipant) {
23
- const destroyMessage = {
24
- namespace: namespace_js_1.default,
25
- channel,
26
- type: 'DESTROY',
27
- };
28
- try {
29
- messenger.sendMessage(destroyMessage);
30
- }
31
- catch (_) {
32
- // We do our best to notify the other participant of the connection, but
33
- // if there's an error in doing so (e.g., maybe the handshake hasn't
34
- // completed and a messenger can't send the message), it's probably not
35
- // worth bothering the consumer with an error.
36
- }
37
- }
38
- for (const connectionDestroyedHandler of connectionDestroyedHandlers) {
39
- connectionDestroyedHandler();
40
- }
41
- log?.('Connection destroyed');
42
- });
43
- const validateReceivedMessage = (data) => {
44
- return (0, guards_js_1.isMessage)(data) && data.channel === channel;
45
- };
46
- const promise = (async () => {
47
- try {
48
- messenger.initialize({ log, validateReceivedMessage });
49
- messenger.addMessageHandler((message) => {
50
- if ((0, guards_js_1.isDestroyMessage)(message)) {
51
- destroyConnection(false);
52
- }
53
- });
54
- const { remoteProxy, destroy } = await (0, shakeHands_js_1.default)({
55
- messenger,
56
- methods,
57
- timeout,
58
- channel,
59
- log,
60
- });
61
- connectionDestroyedHandlers.push(destroy);
62
- return remoteProxy;
63
- }
64
- catch (error) {
65
- destroyConnection(true);
66
- throw error;
67
- }
68
- })();
69
- return {
70
- promise,
71
- // Why we don't reject the connection promise when consumer calls destroy():
72
- // https://github.com/Aaronius/penpal/issues/51
73
- destroy: () => {
74
- destroyConnection(true);
75
- },
76
- };
77
- };
78
- exports.default = connect;
@@ -1,8 +0,0 @@
1
- import { Methods, Log } from './types.js';
2
- import Messenger from './messengers/Messenger.js';
3
- /**
4
- * Listens for "call" messages from the remote, executes the corresponding method,
5
- * and responds with the return value or error.
6
- */
7
- declare const connectCallHandler: (messenger: Messenger, methods: Methods, channel: string | undefined, log: Log | undefined) => () => void;
8
- export default connectCallHandler;
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const errorSerialization_js_1 = require("./errorSerialization.js");
4
- const Reply_js_1 = require("./Reply.js");
5
- const PenpalError_js_1 = require("./PenpalError.js");
6
- const methodSerialization_js_1 = require("./methodSerialization.js");
7
- const guards_js_1 = require("./guards.js");
8
- const namespace_js_1 = require("./namespace.js");
9
- const createErrorReplyMessage = (channel, callId, error) => ({
10
- namespace: namespace_js_1.default,
11
- channel,
12
- type: 'REPLY',
13
- callId,
14
- isError: true,
15
- ...(error instanceof Error
16
- ? { value: (0, errorSerialization_js_1.serializeError)(error), isSerializedErrorInstance: true }
17
- : { value: error }),
18
- });
19
- /**
20
- * Listens for "call" messages from the remote, executes the corresponding method,
21
- * and responds with the return value or error.
22
- */
23
- const connectCallHandler = (messenger, methods, channel, log) => {
24
- let isDestroyed = false;
25
- const handleMessage = async (message) => {
26
- if (isDestroyed) {
27
- // It's possible to throw an error here, but it would only be catchable
28
- // using window.onerror since we're in an asynchronously-called function.
29
- // There is no method call the consumer is making that they could wrap in
30
- // a try-catch. Even if the consumer were to catch the error somehow,
31
- // the value of doing so is questionable.
32
- return;
33
- }
34
- if (!(0, guards_js_1.isCallMessage)(message)) {
35
- return;
36
- }
37
- log?.(`Received ${(0, methodSerialization_js_1.formatMethodPath)(message.methodPath)}() call`, message);
38
- const { methodPath, args, id: callId } = message;
39
- let replyMessage;
40
- let transferables;
41
- try {
42
- const method = (0, methodSerialization_js_1.getMethodAtMethodPath)(methodPath, methods);
43
- if (!method) {
44
- throw new PenpalError_js_1.default('METHOD_NOT_FOUND', `Method \`${(0, methodSerialization_js_1.formatMethodPath)(methodPath)}\` is not found.`);
45
- }
46
- let value = await method(...args);
47
- if (value instanceof Reply_js_1.default) {
48
- transferables = value.transferables;
49
- value = await value.value;
50
- }
51
- replyMessage = {
52
- namespace: namespace_js_1.default,
53
- channel,
54
- type: 'REPLY',
55
- callId,
56
- value,
57
- };
58
- }
59
- catch (error) {
60
- replyMessage = createErrorReplyMessage(channel, callId, error);
61
- }
62
- // Although we checked this at the beginning of the function, we need to
63
- // check it again because we've made async calls, and the connection may
64
- // have been destroyed in the meantime.
65
- if (isDestroyed) {
66
- return;
67
- }
68
- try {
69
- log?.(`Sending ${(0, methodSerialization_js_1.formatMethodPath)(methodPath)}() reply`, replyMessage);
70
- messenger.sendMessage(replyMessage, transferables);
71
- }
72
- catch (error) {
73
- // If a consumer attempts to send an object that's not
74
- // cloneable (e.g., window), we want to ensure the receiver's promise
75
- // gets rejected.
76
- if (error.name === 'DataCloneError') {
77
- replyMessage = createErrorReplyMessage(channel, callId, error);
78
- log?.(`Sending ${(0, methodSerialization_js_1.formatMethodPath)(methodPath)}() reply`, replyMessage);
79
- messenger.sendMessage(replyMessage);
80
- }
81
- throw error;
82
- }
83
- };
84
- messenger.addMessageHandler(handleMessage);
85
- return () => {
86
- isDestroyed = true;
87
- messenger.removeMessageHandler(handleMessage);
88
- };
89
- };
90
- exports.default = connectCallHandler;
@@ -1,12 +0,0 @@
1
- import { RemoteProxy, Methods, Log } from './types.js';
2
- import Messenger from './messengers/Messenger.js';
3
- /**
4
- * Creates a proxy. When methods are called on the proxy, a "call" message will
5
- * be sent to the remote, the remote's corresponding method will be
6
- * executed, and the method's return value will be returned via a message.
7
- */
8
- declare const connectRemoteProxy: <TMethods extends Methods>(messenger: Messenger, channel: string | undefined, log: Log | undefined) => {
9
- remoteProxy: RemoteProxy<TMethods>;
10
- destroy: () => void;
11
- };
12
- export default connectRemoteProxy;
@@ -1,139 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const generateId_js_1 = require("./generateId.js");
4
- const errorSerialization_js_1 = require("./errorSerialization.js");
5
- const methodSerialization_js_1 = require("./methodSerialization.js");
6
- const CallOptions_js_1 = require("./CallOptions.js");
7
- const PenpalError_js_1 = require("./PenpalError.js");
8
- const guards_js_1 = require("./guards.js");
9
- const namespace_js_1 = require("./namespace.js");
10
- const methodsToTreatAsNative = new Set(['apply', 'call', 'bind']);
11
- const createRemoteProxy = (callback, log, path = []) => {
12
- return new Proxy(path.length
13
- ? () => {
14
- // Intentionally empty
15
- }
16
- : Object.create(null), {
17
- get(target, prop) {
18
- // If a promise is resolved with this proxy object, the JavaScript
19
- // runtime will look for a `then` property on this object to determine
20
- // if it should be treated as a promise (to support promise chaining).
21
- // If we don't return undefined here, the JavaScript runtime will treat
22
- // this object as a promise and attempt to call `then`, which will
23
- // then send a call message to the remote. This is not what we want.
24
- if (prop === 'then') {
25
- return;
26
- }
27
- // Because we're using a proxy and because Penpal supports developers
28
- // exposing nested methods, we have a predicament. If a developer
29
- // calls, for example, remote.auth.apply(), are they
30
- // attempting to call a nested apply() method that a developer has
31
- // explicitly exposed from the remote? Could they instead be attempting
32
- // to call Function.prototype.apply() on the remote.auth() method?
33
- // Without the remote telling the local Penpal which methods the
34
- // developer has exposed, it has no way of knowing (and the main reason
35
- // we use a proxy is so that Penpal doesn't have to communicate which
36
- // methods are exposed). So, we treat certain methods as native methods
37
- // and return the native method rather than a proxy. The downside of
38
- // this is that if a developer has explicitly exposed a nested method
39
- // with the same name as one of these native method names, the developer
40
- // will be unable to call the exposed remote method because they will
41
- // be calling the method on the Function prototype instead.
42
- if (path.length && methodsToTreatAsNative.has(prop)) {
43
- return Reflect.get(target, prop);
44
- }
45
- return createRemoteProxy(callback, log, [...path, prop]);
46
- },
47
- apply(target, _thisArg, args) {
48
- return callback(path, args);
49
- },
50
- });
51
- };
52
- const getDestroyedConnectionMethodCallError = (methodPath) => {
53
- return new PenpalError_js_1.default('CONNECTION_DESTROYED', `Method call ${(0, methodSerialization_js_1.formatMethodPath)(methodPath)}() failed due to destroyed connection`);
54
- };
55
- /**
56
- * Creates a proxy. When methods are called on the proxy, a "call" message will
57
- * be sent to the remote, the remote's corresponding method will be
58
- * executed, and the method's return value will be returned via a message.
59
- */
60
- const connectRemoteProxy = (messenger, channel, log) => {
61
- let isDestroyed = false;
62
- const replyHandlers = new Map();
63
- const handleMessage = (message) => {
64
- if (!(0, guards_js_1.isReplyMessage)(message)) {
65
- return;
66
- }
67
- const { callId, value, isError, isSerializedErrorInstance } = message;
68
- const replyHandler = replyHandlers.get(callId);
69
- if (!replyHandler) {
70
- return;
71
- }
72
- replyHandlers.delete(callId);
73
- log?.(`Received ${(0, methodSerialization_js_1.formatMethodPath)(replyHandler.methodPath)}() call`, message);
74
- if (isError) {
75
- replyHandler.reject(isSerializedErrorInstance ? (0, errorSerialization_js_1.deserializeError)(value) : value);
76
- }
77
- else {
78
- replyHandler.resolve(value);
79
- }
80
- };
81
- messenger.addMessageHandler(handleMessage);
82
- const remoteProxy = createRemoteProxy((methodPath, args) => {
83
- if (isDestroyed) {
84
- throw getDestroyedConnectionMethodCallError(methodPath);
85
- }
86
- const callId = (0, generateId_js_1.default)();
87
- const lastArg = args[args.length - 1];
88
- const lastArgIsOptions = lastArg instanceof CallOptions_js_1.default;
89
- const { timeout, transferables } = lastArgIsOptions ? lastArg : {};
90
- const argsWithoutOptions = lastArgIsOptions ? args.slice(0, -1) : args;
91
- return new Promise((resolve, reject) => {
92
- // We reference `window.setTimeout` instead of just `setTimeout`
93
- // so that the TypeScript engine doesn't
94
- // get confused when running tests. Something within
95
- // Karma + @rollup/plugin-typescript leaks node types into source
96
- // files when running tests. Node's setTimeout has a return type of
97
- // Timeout rather than number, resulting in a build error when
98
- // running tests if we don't disambiguate the browser setTimeout
99
- // from node's setTimeout. There may be a better way to configure
100
- // Karma + Rollup + Typescript to avoid node type leakage.
101
- const timeoutId = timeout !== undefined
102
- ? window.setTimeout(() => {
103
- replyHandlers.delete(callId);
104
- reject(new PenpalError_js_1.default('METHOD_CALL_TIMEOUT', `Method call ${(0, methodSerialization_js_1.formatMethodPath)(methodPath)}() timed out after ${timeout}ms`));
105
- }, timeout)
106
- : undefined;
107
- replyHandlers.set(callId, { methodPath, resolve, reject, timeoutId });
108
- try {
109
- const callMessage = {
110
- namespace: namespace_js_1.default,
111
- channel,
112
- type: 'CALL',
113
- id: callId,
114
- methodPath,
115
- args: argsWithoutOptions,
116
- };
117
- log?.(`Sending ${(0, methodSerialization_js_1.formatMethodPath)(methodPath)}() call`, callMessage);
118
- messenger.sendMessage(callMessage, transferables);
119
- }
120
- catch (error) {
121
- reject(new PenpalError_js_1.default('TRANSMISSION_FAILED', error.message));
122
- }
123
- });
124
- }, log);
125
- const destroy = () => {
126
- isDestroyed = true;
127
- messenger.removeMessageHandler(handleMessage);
128
- for (const { methodPath, reject, timeoutId } of replyHandlers.values()) {
129
- clearTimeout(timeoutId);
130
- reject(getDestroyedConnectionMethodCallError(methodPath));
131
- }
132
- replyHandlers.clear();
133
- };
134
- return {
135
- remoteProxy,
136
- destroy,
137
- };
138
- };
139
- exports.default = connectRemoteProxy;
package/cjs/debug.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { Log } from './types.js';
2
- declare const debug: (prefix?: string) => Log;
3
- export default debug;
package/cjs/debug.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const debug = (prefix) => {
4
- return (...args) => {
5
- console.log(`✍️ %c${prefix}%c`, 'font-weight: bold;', '', ...args);
6
- };
7
- };
8
- exports.default = debug;
@@ -1,9 +0,0 @@
1
- import { SerializedError } from './types.js';
2
- /**
3
- * Converts an error object into a plain object.
4
- */
5
- export declare const serializeError: (error: Error) => SerializedError;
6
- /**
7
- * Converts a plain object into an error object.
8
- */
9
- export declare const deserializeError: ({ name, message, stack, penpalCode, }: SerializedError) => Error;
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deserializeError = exports.serializeError = void 0;
4
- const PenpalError_js_1 = require("./PenpalError.js");
5
- /**
6
- * Converts an error object into a plain object.
7
- */
8
- const serializeError = (error) => ({
9
- name: error.name,
10
- message: error.message,
11
- stack: error.stack,
12
- penpalCode: error instanceof PenpalError_js_1.default ? error.code : undefined,
13
- });
14
- exports.serializeError = serializeError;
15
- /**
16
- * Converts a plain object into an error object.
17
- */
18
- const deserializeError = ({ name, message, stack, penpalCode, }) => {
19
- const deserializedError = penpalCode
20
- ? new PenpalError_js_1.default(penpalCode, message)
21
- : new Error(message);
22
- deserializedError.name = name;
23
- deserializedError.stack = stack;
24
- return deserializedError;
25
- };
26
- exports.deserializeError = deserializeError;
@@ -1,8 +0,0 @@
1
- /**
2
- * @return A unique ID
3
- */
4
- declare const _default: {
5
- (): `${string}-${string}-${string}-${string}-${string}`;
6
- (): `${string}-${string}-${string}-${string}-${string}`;
7
- };
8
- export default _default;
package/cjs/generateId.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /**
4
- * @return A unique ID
5
- */
6
- // crypto.randomUUID is not available in insecure contexts.
7
- exports.default = crypto.randomUUID?.bind(crypto) ??
8
- (() => new Array(4)
9
- .fill(0)
10
- .map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16))
11
- .join('-'));
@@ -1,6 +0,0 @@
1
- declare const getPromiseWithResolvers: <ResolvedValueType, RejectedValueType>() => {
2
- promise: Promise<ResolvedValueType>;
3
- resolve: (value: ResolvedValueType) => void;
4
- reject: (error: RejectedValueType) => void;
5
- };
6
- export default getPromiseWithResolvers;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- // Just use the native Promise.withResolvers() once it gains a bit more
4
- // adoption. Safari was the last major browser to support it, which happened
5
- // on March 5, 2024 in Safari 17.4.
6
- const getPromiseWithResolvers = () => {
7
- let resolve;
8
- let reject;
9
- const promise = new Promise((res, rej) => {
10
- resolve = res;
11
- reject = rej;
12
- });
13
- return {
14
- promise,
15
- resolve: resolve,
16
- reject: reject,
17
- };
18
- };
19
- exports.default = getPromiseWithResolvers;
package/cjs/guards.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import { Ack2Message, CallMessage, Message, ReplyMessage, Ack1Message, SynMessage, DestroyMessage } from './types.js';
2
- export declare const isObject: (value: unknown) => value is Record<string | number | symbol, unknown>;
3
- export declare const isFunction: (value: unknown) => value is Function;
4
- export declare const isMessage: (data: unknown) => data is Message;
5
- export declare const isSynMessage: (message: Message) => message is SynMessage;
6
- export declare const isAck1Message: (message: Message) => message is Ack1Message;
7
- export declare const isAck2Message: (message: Message) => message is Ack2Message;
8
- export declare const isCallMessage: (message: Message) => message is CallMessage;
9
- export declare const isReplyMessage: (message: Message) => message is ReplyMessage;
10
- export declare const isDestroyMessage: (message: Message) => message is DestroyMessage;
package/cjs/guards.js DELETED
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isDestroyMessage = exports.isReplyMessage = exports.isCallMessage = exports.isAck2Message = exports.isAck1Message = exports.isSynMessage = exports.isMessage = exports.isFunction = exports.isObject = void 0;
4
- const namespace_js_1 = require("./namespace.js");
5
- const isObject = (value) => {
6
- return typeof value === 'object' && value !== null;
7
- };
8
- exports.isObject = isObject;
9
- const isFunction = (value) => {
10
- return typeof value === 'function';
11
- };
12
- exports.isFunction = isFunction;
13
- const isMessage = (data) => {
14
- return (0, exports.isObject)(data) && data.namespace === namespace_js_1.default;
15
- };
16
- exports.isMessage = isMessage;
17
- const isSynMessage = (message) => {
18
- return message.type === 'SYN';
19
- };
20
- exports.isSynMessage = isSynMessage;
21
- const isAck1Message = (message) => {
22
- return message.type === 'ACK1';
23
- };
24
- exports.isAck1Message = isAck1Message;
25
- const isAck2Message = (message) => {
26
- return message.type === 'ACK2';
27
- };
28
- exports.isAck2Message = isAck2Message;
29
- const isCallMessage = (message) => {
30
- return message.type === 'CALL';
31
- };
32
- exports.isCallMessage = isCallMessage;
33
- const isReplyMessage = (message) => {
34
- return message.type === 'REPLY';
35
- };
36
- exports.isReplyMessage = isReplyMessage;
37
- const isDestroyMessage = (message) => {
38
- return message.type === 'DESTROY';
39
- };
40
- exports.isDestroyMessage = isDestroyMessage;
package/cjs/index.d.ts DELETED
@@ -1,12 +0,0 @@
1
- export { default as connect } from './connect.js';
2
- export { default as WindowMessenger } from './messengers/WindowMessenger.js';
3
- export { default as WorkerMessenger } from './messengers/WorkerMessenger.js';
4
- export { default as PortMessenger } from './messengers/PortMessenger.js';
5
- export { default as CallOptions } from './CallOptions.js';
6
- export { default as Reply } from './Reply.js';
7
- export { default as PenpalError } from './PenpalError.js';
8
- export { default as ErrorCode } from './ErrorCodeObj.js';
9
- export { default as debug } from './debug.js';
10
- export { Connection, RemoteProxy, Methods } from './types.js';
11
- export { default as Messenger, InitializeMessengerOptions, MessageHandler, } from './messengers/Messenger.js';
12
- export { Log, Message, SynMessage, Ack1Message, Ack2Message, CallMessage, ReplyMessage, DestroyMessage, } from './types.js';
package/cjs/index.js DELETED
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.debug = exports.ErrorCode = exports.PenpalError = exports.Reply = exports.CallOptions = exports.PortMessenger = exports.WorkerMessenger = exports.WindowMessenger = exports.connect = void 0;
4
- var connect_js_1 = require("./connect.js");
5
- Object.defineProperty(exports, "connect", { enumerable: true, get: function () { return connect_js_1.default; } });
6
- var WindowMessenger_js_1 = require("./messengers/WindowMessenger.js");
7
- Object.defineProperty(exports, "WindowMessenger", { enumerable: true, get: function () { return WindowMessenger_js_1.default; } });
8
- var WorkerMessenger_js_1 = require("./messengers/WorkerMessenger.js");
9
- Object.defineProperty(exports, "WorkerMessenger", { enumerable: true, get: function () { return WorkerMessenger_js_1.default; } });
10
- var PortMessenger_js_1 = require("./messengers/PortMessenger.js");
11
- Object.defineProperty(exports, "PortMessenger", { enumerable: true, get: function () { return PortMessenger_js_1.default; } });
12
- var CallOptions_js_1 = require("./CallOptions.js");
13
- Object.defineProperty(exports, "CallOptions", { enumerable: true, get: function () { return CallOptions_js_1.default; } });
14
- var Reply_js_1 = require("./Reply.js");
15
- Object.defineProperty(exports, "Reply", { enumerable: true, get: function () { return Reply_js_1.default; } });
16
- var PenpalError_js_1 = require("./PenpalError.js");
17
- Object.defineProperty(exports, "PenpalError", { enumerable: true, get: function () { return PenpalError_js_1.default; } });
18
- var ErrorCodeObj_js_1 = require("./ErrorCodeObj.js");
19
- Object.defineProperty(exports, "ErrorCode", { enumerable: true, get: function () { return ErrorCodeObj_js_1.default; } });
20
- var debug_js_1 = require("./debug.js");
21
- Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return debug_js_1.default; } });
@@ -1,31 +0,0 @@
1
- import WindowMessenger from './messengers/WindowMessenger.js';
2
- import WorkerMessenger from './messengers/WorkerMessenger.js';
3
- import PortMessenger from './messengers/PortMessenger.js';
4
- import CallOptions from './CallOptions.js';
5
- import Reply from './Reply.js';
6
- import PenpalError from './PenpalError.js';
7
- declare const _default: {
8
- connect: <TMethods extends import("./types.js").Methods>({ messenger, methods, timeout, channel, log, }: {
9
- messenger: import("./index.js").Messenger;
10
- methods?: import("./types.js").Methods;
11
- timeout?: number;
12
- channel?: string;
13
- log?: import("./types.js").Log;
14
- }) => import("./types.js").Connection<TMethods>;
15
- WindowMessenger: typeof WindowMessenger;
16
- WorkerMessenger: typeof WorkerMessenger;
17
- PortMessenger: typeof PortMessenger;
18
- CallOptions: typeof CallOptions;
19
- Reply: typeof Reply;
20
- PenpalError: typeof PenpalError;
21
- debug: (prefix?: string) => import("./types.js").Log;
22
- ErrorCode: {
23
- readonly ConnectionDestroyed: "CONNECTION_DESTROYED";
24
- readonly ConnectionTimeout: "CONNECTION_TIMEOUT";
25
- readonly InvalidArgument: "INVALID_ARGUMENT";
26
- readonly MethodCallTimeout: "METHOD_CALL_TIMEOUT";
27
- readonly MethodNotFound: "METHOD_NOT_FOUND";
28
- readonly TransmissionFailed: "TRANSMISSION_FAILED";
29
- };
30
- };
31
- export default _default;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const connect_js_1 = require("./connect.js");
4
- const WindowMessenger_js_1 = require("./messengers/WindowMessenger.js");
5
- const WorkerMessenger_js_1 = require("./messengers/WorkerMessenger.js");
6
- const PortMessenger_js_1 = require("./messengers/PortMessenger.js");
7
- const CallOptions_js_1 = require("./CallOptions.js");
8
- const Reply_js_1 = require("./Reply.js");
9
- const PenpalError_js_1 = require("./PenpalError.js");
10
- const ErrorCodeObj_js_1 = require("./ErrorCodeObj.js");
11
- const debug_js_1 = require("./debug.js");
12
- exports.default = {
13
- connect: connect_js_1.default,
14
- WindowMessenger: WindowMessenger_js_1.default,
15
- WorkerMessenger: WorkerMessenger_js_1.default,
16
- PortMessenger: PortMessenger_js_1.default,
17
- CallOptions: CallOptions_js_1.default,
18
- Reply: Reply_js_1.default,
19
- PenpalError: PenpalError_js_1.default,
20
- debug: debug_js_1.default,
21
- ErrorCode: ErrorCodeObj_js_1.default,
22
- };
@@ -1,14 +0,0 @@
1
- import { Message, Log } from '../types.js';
2
- export type MessageHandler = (message: Message) => void;
3
- export type InitializeMessengerOptions = {
4
- log?: Log;
5
- validateReceivedMessage: (data: unknown) => data is Message;
6
- };
7
- interface Messenger {
8
- sendMessage: (message: Message, transferables?: Transferable[]) => void;
9
- addMessageHandler: (callback: MessageHandler) => void;
10
- removeMessageHandler: (callback: MessageHandler) => void;
11
- initialize: (options: InitializeMessengerOptions) => void;
12
- destroy: () => void;
13
- }
14
- export default Messenger;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,21 +0,0 @@
1
- import { Message } from '../types.js';
2
- import Messenger, { InitializeMessengerOptions, MessageHandler } from './Messenger.js';
3
- type Options = {
4
- /**
5
- * The port used to communicate to the other port of the port pair.
6
- */
7
- port: MessagePort;
8
- };
9
- /**
10
- * Handles the details of communicating on a MessagePort.
11
- */
12
- declare class PortMessenger implements Messenger {
13
- #private;
14
- constructor({ port }: Options);
15
- initialize: ({ validateReceivedMessage }: InitializeMessengerOptions) => void;
16
- sendMessage: (message: Message, transferables?: Transferable[]) => void;
17
- addMessageHandler: (callback: MessageHandler) => void;
18
- removeMessageHandler: (callback: MessageHandler) => void;
19
- destroy: () => void;
20
- }
21
- export default PortMessenger;