experimental
The Pear API enables applications to interact with Pear platform features.
Most application peer-to-peer functionality is provided by ecosystem modules rather than the API.
Platform APIs are unchangeable. Compatibility cannot break. So the Pear API surface aims to be (and remain) as small as possible.
The Pear Platform API is made available globally as Pear
.
The Pear
API is designed to be small and immutable.
Any future changes to the Pear
API will be non-breaking additions.
Contains application configuration data.
The application key, null
in development mode.
The config.key
object holds both Hexadecimal and Z-Base-32 encodings of the key, and is of the form { z32: <String>, hex: <String> }
,
Application is local (loaded from disk).
Equivalent to pear.config.key === null
.
Runtime scenario (dev, staging or production)
Application storage path
Application name
Application entry file
Application release/staging channel, null
in development mode.
Configuration options.
The pear
configuration object as supplied via an applications package.json
file.
References
The environment variables that an application was started with, as key-value pairs in an object.
The current working directory that an application was started from.
Parsed command-line flag values as supplied when an application was started.
Indicates whether or not Devtools is enabled.
Indicates whether or not Watch-Reload functionality is enabled.
Application storage path.
Command-line application arguments passed like pear run --dev . --some arg
.
The current release length as marked by the pear release
command.
References
Pear application link. Can be a pear://
link or a local directory.
Can include a fragment link eg. pear://link#fragment
.
References
Holds trusted Pear application links and domains as specified in the links
field inside package.json
.
References
Holds just the data portion of a Pear link.
The Pear link of an application. Takes the form pear://<key>/<data>
.
In development, pear://dev/<data>
.
References
Holds state as set by Pear.checkpoint()
. When an application restarts it will hold the most recent value passed to Pear.checkpoint()
.
Stores state that will be available as Pear.config.checkpoint
next time the application starts.
The Pear.config.checkpoint
property immediately reflects the latest checkpoint.
The returned Promise
will resolve once the checkpoint has been successfully stored.
References
Application release sequence integer, null
in development mode.
Parsed runtime flags. For internal/advanced use.
Pear application link. May be a pear://
link or a local directory.
Can also include entrypoint and fragment eg. pear://link#fragment
.
Application dependencies.
Root directory of project.
Directory for Pear runtime.
Stores state that will be available as Pear.config.checkpoint
next time the application starts.
The Pear.config.checkpoint
property immediately reflects the latest checkpoint.
The returned Promise
will resolve once the checkpoint has been successfully stored.
References
A function which accepts a pattern object and returns an Iambus
subscriber (which inherits from streamx
Readable
) which emits message objects matching a provided pattern object.
If no pattern object or an empty pattern object is provided all messages will be emitted. A pattern object is an object (typically) containing a subset of matching values for a given target object. Message objects can be user generated or platform generated.
The subscriber stream has a data
event which can be listened to, it can also be consumed with for await
and an listener function can be passed in addition to pattern (message(pattern, listener)
) or as a single argument (messages(listener)
) (indicating a catch-all pattern).
A message object may have any properties. Platform-generated messages are given a type
property.
Listen for an internal platform message using a pattern object and listener function:
const { messages } = Pear
messages({ type: 'pear/wakeup' }, ({ data, link }) => {
console.log('pear/wakeup', data, link)
})
Tiny utility module which logs all messages using for await
:
const { messages } = Pear
for await (const message of messages()) {
if (global.LOGBUS) console.log('BUS:', message)
}
Use message
to create an application message:
const { message, messages } = Pear
const ctaClicks = messages({ type: 'my-app/user-cta' })
ctaClicks.on('data', (msg) => { console.log('cta click', msg) })
// elsewhere
onUserClickCta((event, data) => {
message({ type: 'my-app/user-cta', event, data })
})
Send a message which will be:
const { message, messages } = Pear
async function logMessages () {
for await (const message of messages()) console.log(message)
}
logMessages().catch(console.error)
let count = 0
do {
await message({ type: 'tick', count })
await new Promise((resolve) => setTimeout(resolve, 1000))
} while (count++ < 1000)
Pear Worker is used to spawn processes and facilitate communication between the parent and child processes in the Pear Runtime.
The spawned worker process inherits standard input, output, and error from the parent process.
A bidirectional pipe is also created which enables communication between the parent and worker process.
Reference counting is handled automatically to manage the sidecar lifecycle.
Runs a Pear Worker by spawning a Pear Terminal Application process from the specified link
parameter. The Worker uses the flags of the parent application but any application arguments must be passed using the args
parameter, the args
parameter also sets Pear.config.args
. Returns a pipe (a streamx
Duplex
stream) for Worker communication.
Returns the pipe (a streamx
Duplex
stream) created to the worker process.
Media interface
Resolves to: <String>
If access to the microphone is available, resolved value will be 'granted'
.
Any other string indicates lack of permission. Possible values are 'granted'
, 'not-determined'
, 'denied'
, 'restricted'
, 'unknown'
.
Resolves to: <String>
If access to the camera is available, resolved value will be 'granted'
.
Any other string indicates lack of permission. Possible values are 'granted'
, 'not-determined'
, 'denied'
, 'restricted'
, 'unknown'
.
Resolves to: <String>
If access to the screen is available, resolved value will be 'granted'
.
Any other string indicates lack of permission. Possible values are 'granted'
, 'not-determined'
, 'denied'
, 'restricted'
, 'unknown'
.
Resolves to: <Boolean>
Request access to the microphone. Resolves to true
if permission is granted.
Resolves to: <Boolean>
Request access to the camera. Resolves to true
if permission is granted.
Resolves to: <Boolean>
Request access to screen sharing. Resolves to true
if permission is granted.
Captures available desktop sources. Resolves to an array of objects with shape { id <String>, name <String>, thumbnail <NativeImage>, display_id <String>, appIcon <NativeImage> }
. The id
is the window or screen identifier. The name
is the window title or 'Screen <index>'
in multiscreen scenarios or else Entire Screen
. The display_id
identifies the screen. The thumbnail is a scaled down screen capture of the window/screen.
Options
types <Array<String>>
- Default:['screen', 'window']
. Filter by types. Types are'screen'
and'window'
.thumbnailSize <Object>
- Default:{width: 150, height: 150}
. Set thumbnail scaling (pixels)fetchWindowIcons <Boolean>
- Default:false
. PopulateappIcon
with Window icons, or elsenull
.
References
- win.getMediaSourceId()
- view.getMediaSourceId()
- self.getMediaSourceId()
- parent.getMediaSourceId()
- https://www.electronjs.org/docs/latest/api/desktop-capturer#desktopcapturergetsourcesoptions
- https://www.electronjs.org/docs/latest/api/structures/desktop-capturer-source
<NativeImage>
Function that returns a promise which resolves to a Pear versions object with the shape { fork <Integer>, length <Integer>, key <Buffer> }
.
The key
is a Buffer of the run key. The length
is the size of the relevant Hypercore. The fork
property is determined by data truncation.
These three properties together are a unique identifier for the entire state of both applications and the Pear platform.
The platform version.
The application version.
The versions of runtimes.
References
Register application clean-up handlers to be called when an application begins to unload.
May be called multiple times to register multiple teardown handlers.
Functions supplied to teardown will be executed in order of registration when an application begins to unload. Any promise returned from each supplied function will be waited upon until resolution before calling the next teardown handler.
Soft-restart Terminal applications (keeps I/O), refresh application in Desktop applications.
Restart the application.
Exits the process with the provided exit code.
The listener
function is called for every incoming update with an update
object of the form:
{
type: 'pear/updates',
version: { fork <Integer>, length <Integer>, key <String(hex)>, } | null,
app <Boolean>,
diff <Array <String> >,
}
version
is a Pear version object holding incoming version informationapp
indicates whether the update represents an application (true
) or platform (false
) updatediff
requires--update-diffs
flag (elsenull
). An array of objects of form{ type, key}
.type
<String>
- Operation typeupdate
ordelete
key
<String>
- Drive key for a given updated file e.g./path/to/file.txt
Also returns a streamx
Readable
) stream.
A wakeup occurs in the following cases:
- when a trusted
pear://
link is clicked and the application for that link is already open - when used with
pear run --detached pear://<key>[/data]
The listener
function is called for every incoming wakeup with a wakeup
object of the form:
{
type: 'pear/wakeup',
link: <String>,
linkData: <String>,
fragment: <String>,
entrypoint: <String>
}
link
is thepear://
link for the application receiving the wakeuplinkData
is everything after the key in thepear://
link - this would bepathname
of aURL
object but without the leading slash (/
). Givenpear://8ts9yz9dtucxzwbxafygnjasqe9ti3dt3w7rm6sbiu8prmidacao/some/more/stuff
thedata
string would holdsome/more/stuff
.fragment
is thefragment
part ofpear://link#fragment
(location hash without the#
prefix).entrypoint
includesentrypoint
ofpear://link/some/entry/point
(URL pathname).
Also returns a streamx
Readable
) stream.
Desktop Applications only.
Create a new Window
instance.
Options
show <Boolean>
Default:true
- show the window as soon as it has been openedx <Integer>
- the horizontal position of left side of the window (pixels)y <Integer>
- vertical window position (pixels)width <Integer>
- the width of the window (pixels)height <Integer>
- the height of the window (pixels)animate <Boolean>
Default:false
- animate the dimensional change. MacOS only, ignored on other OS's.center <Boolean
- center the window upon openingminWidth <Integer>
- window minimum width (pixels)minHeight <Integer>
- window minimum height (pixels)maxWidth <Integer>
- window maximum width (pixels)maxHeight <Integer>
- window maximum height (pixels)resizable <Boolean>
- window resizabilitymovable <Boolean>
- window movabilityminimizable <Boolean>
- window minimizabilitymaximizable <Boolean>
- window maximizabilityclosable <Boolean>
- window closabilityfocusable <Boolean>
- window focusabilityalwaysOnTop <Boolean>
- Set window to always be on topfullscreen <Boolean>
- Set window to fullscreen upon openkiosk <Boolean>
- Set window to enter kiosk mode upon openautoHideMenuBar <Boolean>
- Hide menu bar unless Alt key is pressed (Linux, Windows)hasShadow <Boolean>
- Set window shadowopacity <Number>
- Set window opacity (0.0 - 1.0) (Windows, macOS)transparent <Boolean>
- Set window transparencybackgroundColor <String>
Default:'#FFF'
- window default background color. Hex, RGB, RGBA, HSL HSLA, CSS color
Receive a message from the window. The received args
array is deserialized via JSON.parse
.
References
Resolves to: <Boolean>
Open the window.
Options
show
Default:true
- show the window as soon as it has been openedx <Integer>
- the horizontal position of left side of the window (pixels)y <Integer>
- vertical window position (pixels)width <Integer>
- the width of the window (pixels)height <Integer>
- the height of the window (pixels)animate <Boolean>
Default:false
- animate the dimensional change. MacOS only, ignored on other OS's.center <Boolean
- center the window upon openingminWidth <Integer>
- window minimum width (pixels)minHeight <Integer>
- window minimum height (pixels)maxWidth <Integer>
- window maximum width (pixels)maxHeight <Integer>
- window maximum height (pixels)resizable <Boolean>
- window resizabilitymovable <Boolean>
- window movabilityminimizable <Boolean>
- window minimizabilitymaximizable <Boolean>
- window maximizabilityclosable <Boolean>
- window closabilityfocusable <Boolean>
- window focusabilityalwaysOnTop <Boolean>
- Set window to always be on topfullscreen <Boolean>
- Set window to fullscreen upon openkiosk <Boolean>
- Set window to enter kiosk mode upon openautoHideMenuBar <Boolean>
- Hide menu bar unless Alt key is pressed (Linux, Windows)hasShadow <Boolean>
- Set window shadowopacity <Number>
- Set window opacity (0.0 - 1.0) (Windows, macOS)transparent <Boolean>
- Set window transparencybackgroundColor <String>
Default:'#FFF'
- window default background color. Hex, RGB, RGBA, HSL HSLA, CSS color
Resolves to: <Boolean>
Close the window.
Resolves to: <Boolean>
Show the window.
Resolves to: <Boolean>
Hide the window.
Resolves to: <Boolean>
Focus the window.
Resolves to: <Boolean>
Blur the window.
Resolves to: <Boolean>
Minimize the window.
Resolves to: <Boolean>
Maximize the window.
Resolves to: <Boolean>
Unmaximize/unminimize the window if it is currently maximized/minimized.
Send arguments to the window. They will be serialized with JSON.stringify
.
Resolves to: <String>
Correlates to the id
property of objects in the array returned from Pear.media.desktopSources.
References
- Pear.media.desktopSources
- https://www.electronjs.org/docs/latest/api/browser-window#wingetmediasourceid
Resolves to: {x <Integer>, y <Integer>, width <Integer>, height <Integer>} | null
.
The height, width, horizontal (x
), vertical (y
) position of the window relative to the screen.
All units are (pixels)
If the window is closed this will resolve to null
.
References
const win = new Pear.Window('./some.html', {
x: 10,
y: 450,
width: 300,
height: 350
})
await win.open()
await new Promise((resolve) => setTimeout(resolve, 1000))
await win.dimensions({
x: 20,
y: 50,
width: 550,
height: 300,
animate: true // only has an effect on macOS
})
Sets the dimensions of the window.
Options
x <Integer>
- the horizontal position of left side of the window (pixels)y <Integer>
- the vertical position of the top of the window (pixels)width <Integer>
- the width of the window (pixels)height <Integer>
- the height of the window (pixels)animate <Boolean>
Default:false
- animate the dimensional change. MacOS only, ignored on other OS's.position <String>
- may be'center'
to set the window in the center of the screen or elseundefined
.
References
Resolves to: <Boolean>
Whether the window is visible.
Resolves to: <Boolean>
Whether the window is minimized.
Resolves to: <Boolean>
Whether the window is maximized.
Resolves to: <Boolean>
Whether the window is closed.
Desktop Applications only.
Create a new View
instance. Views provide isolated content views. Frameless, chromeless windows that can be embedded inside other windows and views.
Options
x <Integer>
- the horizontal position of left side of the view (pixels)y <Integer>
- vertical view position (pixels)width <Integer>
- the width of the view (pixels)height <Integer>
- the height of the view (pixels)backgroundColor <String>
Default:'#FFF'
- view default background color. Hex, RGB, RGBA, HSL HSLA, CSS colorautoresize <Object>
Default{ width=true, height=true, vertical=false, horizontal=false }
- dimensions for the view to autoresize alongside. For example, ifwidth
istrue
and the view container increases/decreases in width, the view will increase/decrease in width at the same rate.
References
- https://www.electronjs.org/docs/latest/api/browser-view#viewsetautoresizeoptions-experimental
- https://www.electronjs.org/docs/latest/api/browser-view#viewsetbackgroundcolorcolor-experimental
Receive a message from the view. The received args
array is deserialized via JSON.parse
.
References
Resolves to: <Boolean>
Open the view.
Options
x <Integer>
- the horizontal position of left side of the view (pixels)y <Integer>
- vertical view position (pixels)width <Integer>
- the width of the view (pixels)height <Integer>
- the height of the view (pixels)backgroundColor <String>
Default:'#FFF'
- view default background color. Hex, RGB, RGBA, HSL HSLA, CSS colorautoresize <Object>
Default{ width=true, height=true, vertical=false, horizontal=false }
- dimensions for the view to autoresize alongside. For example, ifwidth
istrue
and the view container increases/decreases in width, the view will increase/decrease in width at the same rate.
Resolves to: <Boolean>
Close the view.
Resolves to: <Boolean>
Show the view.
Resolves to: <Boolean>
Hide the view.
Resolves to: <Boolean>
Focus the view.
Resolves to: <Boolean>
Blur the view.
Send arguments to the view. They will be serialized with JSON.stringify
.
Resolves to: <String>
Supplies the id
property of objects in the array returned from Pear.media.desktopSources.
References
- Pear.media.desktopSources
- https://www.electronjs.org/docs/latest/api/browser-window#wingetmediasourceid
Resolves to: {x <Integer>, y <Integer>, width <Integer>, height <Integer>} | null
.
The height, width, horizontal (x
), vertical (y
) position of the window relative to the screen.
All units are (pixels)
If the Window is closed this will resolve to null
.
References
const view = new Pear.View('./some.html', {
x: 10,
y: 450,
width: 300,
height: 350
})
await view.open()
await new Promise((resolve) => setTimeout(resolve, 1000))
await view.dimensions({
x: 20,
y: 50,
width: 550,
height: 300
})
Sets the dimensions of the view.
Options
x <Integer>
- the horizontal position of left side of the window (pixels)y <Integer>
- the vertical position of the top of the window (pixels)width <Integer>
- the width of the window (pixels)height <Integer>
- the height of the window (pixels)
References
Resolves to: <Boolean>
Whether the view is visible.
Resolves to: <Boolean>
Whether the view is closed.
Resolves to: <Boolean>
Focus current view or window.
Resolves to: <Boolean>
Blur current view or window.
Resolves to: <Boolean>
Show current view or window.
Resolves to: <Boolean>
Hide current view or window.
Get the sourceId of the current window or view.
References
Resolves to: <Boolean>
Minimize current window.
Throws a TypeError
if self
is a view.
Resolves to: <Boolean>
Maximize current window.
Throws a TypeError
if self
is a view.
Resolves to: <Boolean>
Unmaximize/unminimize the current window if it is currently maximized/minimized.
Throws a TypeError
if self
is a view.
Resolves to: <Boolean>
Closes the current view or window.
Resolves to: <Boolean>
Whether the current window or view is visible.
Resolves to: <Boolean>
Whether the current window is maximized. Throws a TypeError
if self
is a view.
Resolves to: <Boolean>
Whether the current window is minimized. Throws a TypeError
if self
is a view.
Receive a message from the parent window or view. The received args
array is deserialized via JSON.parse
.
Send arguments to the parent view or window. They will be serialized with JSON.stringify
.
Resolves to: <Boolean>
Focus parent view or window.
Resolves to: <Boolean>
Blur parent view or window.
Resolves to: <Boolean>
Show parent view or window.
Resolves to: <Boolean>
Hide parent view or window.
Get the sourceId of the parent window or view.
References
Resolves to: <Boolean>
Minimize parent window.
Throws a TypeError
if parent
is a view.
Resolves to: <Boolean>
Maximize parent window.
Throws a TypeError
if parent
is a view.
Resolves to: <Boolean>
Unmaximize/unminimize the parent window if it is currently maximized/minimized.
Throws a TypeError
if parent
is a view.
Resolves to: <Boolean>
Closes the parent view or window.
Resolves to: <Boolean>
Whether the parent window or view is visible.
Resolves to: <Boolean>
Whether the parent window is maximized. Throws a TypeError
if parent
is a view.
Resolves to: <Boolean>
Whether the parent window is minimized. Throws a TypeError
if parent
is a view.
Most Web APIs will work as-is.
This section details deviations in behavior from and notable aspects of Web APIs as they relate to Pear.
The window.open
Web API function will ignore all arguments except for the URL parameter.
In browsers, window.open
opens a new browser window. The opened window belongs to the same browser from which window.open
is called.
In Pear, window.open
loads the URL in the default system browser. It does not create a new application window (use Pear.Window
to create application windows).
Therefore Pear's window.open
only supports a single URL argument. The target
and windowFeatures
parameters that browsers support are discarded.
Like browsers, there is no support for CommonJS (e.g. the require
function as used by Node.js is not supported in Pear Applications).
Like browsers, there is support for native EcmaScript Modules (ESM). A JavaScript Script has no module capabilities. A JavaScript Module has ESM capabilities.
Use <script type="module" src="path/to/my-file.js">
to load a JavaScript Module.
Use <script src="path/to/my-file.js">
to load a JavaScript Script.