The pre-release of version 7 is here! #100
Aaronius
announced in
Announcements
Replies: 1 comment 1 reply
-
I have tried this new release. I found that the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The pre-release of version 7 is here! It contains quite a few new features and breaking changes. I’d love your feedback, so please try it out and leave a comment below.
The full documentation can be found in the README.
Installation and migration
Install the pre-release of version 7 by running
npm install penpal@next
.To ease the pain of migration, running Penpal v7 in a parent window is compatible with running Penpal v6 in a child iframe. Therefore, if it's difficult to upgrade code in the parent window and iframe at the same time, update code in the parent window first and then update code in the child iframe. Compatibility with v6 will be removed in v8. Note that running Penpal v6 in the parent window is not compatible with running Penpal v7 in the child iframe.
Breaking changes
A
WindowMessenger
is now requiredWhat
Message transmission details between a parent window and a child iframe have been moved to a new
WindowMessenger
class. You'll now pass an instance ofWindowMessenger
as themessenger
option when callingconnect
. See Usage with an Iframe in the README.Why
Separating message transmission into a dedicated class allows Penpal to support additional contexts—such as communication between a window and a worker—while keeping the shared, high-level logic inside
connect
.connectToChild
andconnectToParent
have been renamedWhat
connectToChild
andconnectToParent
are now unified into a singleconnect
function.Why
With
WindowMessenger
receiving a reference to the iframe window instead ofconnectToChild
, theconnectToChild
andconnectToParent
functions essentially became identical and no longer needed to be separate functions.Origin defaults have changed
What
When calling
connectToChild
, the default value for thechildOrigin
option was previously derived from the iframe’ssrc
attribute. This is no longer the case. Penpal now defaults to restricting communication to the origin of the parent window’s HTML document. If you'd like to derive the origin from the iframe’ssrc
when specifying the child origin, you can usenew URL(iframe.src).origin
. See Usage with an Iframe in the README.When calling
connectToParent
, the default value for theparentOrigin
option was*
. This has been changed as well. Penpal now defaults to restricting communication to the origin of the child iframe’s HTML document.Why
In version 7, Penpal only receives a reference to the iframe’s content window (
iframe.contentWindow
) rather than the iframe itself, so it no longer attempts to derive the child’s origin when connecting.When connecting to the parent window from the child iframe, I wasn’t comfortable with Penpal using
*
as the defaultparentOrigin
. Even though the README recommended against using the default, developers might easily overlook it. As this library is security-conscious, it seems more appropriate to encourage developers to manually specify*
when necessary, rather than rely on the default.Debugging has changed
What
Previously, to enable debugging, you needed to set the
debug
option totrue
when callingconnectToChild
orconnectToParent
. Now, you’ll need to set thelog
option to a function of your choice. Penpal exports a simpledebug
function that logs debug messages to the console, and you can use this as the value for thelog
option. See Debugging in the README.Why
Allowing a function to be provided instead of a boolean increases flexibility. For instance, you can use a more advanced logging library, like the popular debug package, or write your own custom logging function.
Errors have changed
What
Errors thrown by Penpal were previously instances of the native
Error
class. They are now instances of a newPenpalError
class, which extends fromError
. Additionally, error codes (found on thecode
property) have been updated. The codeConnectionDestroyed
is nowCONNECTION_DESTROYED
, andConnectionTimeout
has changed toCONNECTION_TIMEOUT
. If you were usingErrorCode.ConnectionDestroyed
orErrorCode.ConnectionTimeout
properties to reference the error code strings, these properties remain unchanged. TheNoIframeSrc
error code has been removed because Penpal is now only provided with a reference to the iframe’s content window (iframe.contentWindow
) rather than the iframe itself.Note that new error codes have been added in version 7. See Errors in the README for all the latest error codes.
Why
I added the
PenpalError
class because I was uncomfortable with Penpal adding acode
property directly to the nativeError
instance. TypeScript was uncomfortable with it as well. It made more sense to create a new class that extendsError
and properly supports thecode
property.Object containing remote methods is now a
Proxy
objectWhat
In earlier versions of Penpal,
connection.promise
resolved to an object containing methods that reflected the remote methods. These methods were defined as standard properties on the object. In version 7, however, this object is now aProxy
and the methods are no longer defined as standard properties.To illustrate, let's assume we're following this example from version 6 where
multiply
anddivide
methods are being exposed from an iframe and used in the parent window.Note how
multiply
anddivide
are both keys onchild
. Let's see how this plays out in version 7:The calls to
multiply
anddivide
are still the same and function properly, but notice howObject.keys
returns an empty array. Thechild
object is not pre-built withmultiply
anddivide
properties, but instead dynamically handles method calls as they occur.Why
Using a proxy object simplifies and reduces the amount of work Penpal needs to perform as well as minimizes required code. Penpal no longer needs to extract names and "paths" of methods, send them to the remote, and then rebuild objects with the same structure.
Types have changed
What
The
AsyncMethodReturns
type has been renamed toRemoteProxy
. TheCallSender
type has been removed.Why
The name
AsyncMethodReturns
was overly focused on implementation details. The new name,RemoteProxy
, more accurately reflects its role as a proxy for methods exposed by the remote. Additionally, the type has been enhanced to support new features such as method call timeouts and transferable objects.The
CallSender
type was redundant. The existingMethods
type serves as the replacement forCallSender
.New features
Support for workers, windows, and other things using ports
Penpal now supports workers (dedicated workers, shared workers, and service workers), windows opened using
window.open()
, and other things that use MessagePorts. This is facilitated by the newWindowMessenger
,WorkerMessenger
, andPortMessenger
classes. See the usage examples in the README for more information.Method call timeouts
When calling a remote method, you can now specify a timeout. If the timeout elapses before hearing back from the remote, the promise returned from the method call with be rejected with an error. See Method Call Timeouts for more information.
Support for transferable objects
Browsers natively support transferable objects for transferring large data between contexts. This functionality is now available in Penpal. See Transferring Large Data for more details.
Support for specifying multiple origins from either side of the connection
When connecting from an iframe to a parent window, you've been able to use a regular expression to restrict communication to multiple origins. However, this feature was not available when connecting from the parent window to an iframe. Now, this capability is available and identical when connecting from either direction. Furthermore, instead of specifying just one origin or regular expression, you can now provide an array of origins or regular expressions, making things easier and more flexible.
Enhanced handshake protocol
When establishing a connection, Penpal performs a handshake to ensure both participants can send and receive messages. Previously, this process required calling
connectToChild
in the parent window before callingconnectToParent
in the iframe. Penpal now uses an enhanced handshake protocol that allows either participant to callconnect
first.MessagePort usage
In previous versions, Penpal transmitted all messages between the parent window and iframe using
postMessage
on the respective window objects. Now, Penpal usespostMessage
only during the handshake, while all subsequent communication occurs over MessagePorts. This approach reduces noise on global window objects and reduces security check overhead.Parallel connections
In rare cases, you may wish to establish parallel connections between two participants. Penpal now supports this through the use of channels. See Parallel Connections for more details.
Bug fixes
No bug fixes. There are no known bugs at the moment.
Beta Was this translation helpful? Give feedback.
All reactions