Socket
Socket
Sign inDemoInstall

open

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

open - npm Package Compare versions

Comparing version 8.4.2 to 9.0.0

226

index.d.ts

@@ -1,153 +0,157 @@

import {ChildProcess} from 'child_process';
import {type ChildProcess} from 'node:child_process';
declare namespace open {
interface Options {
/**
Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app.
export type Options = {
/**
Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app.
Note that it waits for the app to exit, not just for the window to close.
Note that it waits for the app to exit, not just for the window to close.
On Windows, you have to explicitly specify an app for it to be able to wait.
On Windows, you have to explicitly specify an app for it to be able to wait.
@default false
*/
readonly wait?: boolean;
@default false
*/
readonly wait?: boolean;
/**
__macOS only__
/**
__macOS only__
Do not bring the app to the foreground.
Do not bring the app to the foreground.
@default false
*/
readonly background?: boolean;
@default false
*/
readonly background?: boolean;
/**
__macOS only__
/**
__macOS only__
Open a new instance of the app even it's already running.
Open a new instance of the app even it's already running.
A new instance is always opened on other platforms.
A new instance is always opened on other platforms.
@default false
*/
readonly newInstance?: boolean;
@default false
*/
readonly newInstance?: boolean;
/**
Specify the `name` of the app to open the `target` with, and optionally, app `arguments`. `app` can be an array of apps to try to open and `name` can be an array of app names to try. If each app fails, the last error will be thrown.
/**
Specify the `name` of the app to open the `target` with, and optionally, app `arguments`. `app` can be an array of apps to try to open and `name` can be an array of app names to try. If each app fails, the last error will be thrown.
The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use [`open.apps`](#openapps) which auto-detects the correct binary to use.
The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use `apps` which auto-detects the correct binary to use.
You may also pass in the app's full path. For example on WSL, this can be `/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe` for the Windows installation of Chrome.
You may also pass in the app's full path. For example on WSL, this can be `/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe` for the Windows installation of Chrome.
The app `arguments` are app dependent. Check the app's documentation for what arguments it accepts.
*/
readonly app?: App | readonly App[];
The app `arguments` are app dependent. Check the app's documentation for what arguments it accepts.
*/
readonly app?: App | readonly App[];
/**
Allow the opened app to exit with nonzero exit code when the `wait` option is `true`.
/**
Allow the opened app to exit with nonzero exit code when the `wait` option is `true`.
We do not recommend setting this option. The convention for success is exit code zero.
We do not recommend setting this option. The convention for success is exit code zero.
@default false
*/
readonly allowNonzeroExitCode?: boolean;
}
@default false
*/
readonly allowNonzeroExitCode?: boolean;
};
interface OpenAppOptions extends Omit<Options, 'app'> {
/**
Arguments passed to the app.
export type OpenAppOptions = {
/**
Arguments passed to the app.
These arguments are app dependent. Check the app's documentation for what arguments it accepts.
*/
readonly arguments?: readonly string[];
}
These arguments are app dependent. Check the app's documentation for what arguments it accepts.
*/
readonly arguments?: readonly string[];
} & Omit<Options, 'app'>;
type AppName =
| 'chrome'
| 'firefox'
| 'edge';
export type AppName =
| 'chrome'
| 'firefox'
| 'edge'
| 'browser'
| 'browserPrivate';
type App = {
name: string | readonly string[];
arguments?: readonly string[];
};
}
export type App = {
name: string | readonly string[];
arguments?: readonly string[];
};
// eslint-disable-next-line no-redeclare
declare const open: {
/**
Open stuff like URLs, files, executables. Cross-platform.
/**
An object containing auto-detected binary names for common apps. Useful to work around cross-platform differences.
Uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
@example
```
import open, {apps} from 'open';
There is a caveat for [double-quotes on Windows](https://github.com/sindresorhus/open#double-quotes-on-windows) where all double-quotes are stripped from the `target`.
await open('https://google.com', {
app: {
name: apps.chrome
}
});
```
*/
export const apps: Record<AppName, string | readonly string[]>;
@param target - The thing you want to open. Can be a URL, file, or executable. Opens in the default app for the file type. For example, URLs open in your default browser.
@returns The [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process.
/**
Open stuff like URLs, files, executables. Cross-platform.
@example
```
import open = require('open');
Uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
// Opens the image in the default image viewer
await open('unicorn.png', {wait: true});
console.log('The image viewer app closed');
There is a caveat for [double-quotes on Windows](https://github.com/sindresorhus/open#double-quotes-on-windows) where all double-quotes are stripped from the `target`.
// Opens the url in the default browser
await open('https://sindresorhus.com');
@param target - The thing you want to open. Can be a URL, file, or executable. Opens in the default app for the file type. For example, URLs open in your default browser.
@returns The [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process.
// Opens the URL in a specified browser.
await open('https://sindresorhus.com', {app: {name: 'firefox'}});
@example
```
import open, {apps} from 'open';
// Specify app arguments.
await open('https://sindresorhus.com', {app: {name: 'google chrome', arguments: ['--incognito']}});
```
*/
(
target: string,
options?: open.Options
): Promise<ChildProcess>;
// Opens the image in the default image viewer.
await open('unicorn.png', {wait: true});
console.log('The image viewer app quit');
/**
An object containing auto-detected binary names for common apps. Useful to work around cross-platform differences.
// Opens the URL in the default browser.
await open('https://sindresorhus.com');
@example
```
import open = require('open');
// Opens the URL in a specified browser.
await open('https://sindresorhus.com', {app: {name: 'firefox'}});
await open('https://google.com', {
app: {
name: open.apps.chrome
}
});
```
*/
apps: Record<open.AppName, string | readonly string[]>;
// Specify app arguments.
await open('https://sindresorhus.com', {app: {name: 'google chrome', arguments: ['--incognito']}});
/**
Open an app. Cross-platform.
// Opens the URL in the default browser in incognito mode.
await open('https://sindresorhus.com', {app: {name: apps.browserPrivate}});
```
*/
export default function open(
target: string,
options?: Options
): Promise<ChildProcess>;
Uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
/**
Open an app. Cross-platform.
@param name - The app you want to open. Can be either builtin supported `open.apps` names or other name supported in platform.
@returns The [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process.
Uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
@example
```
const {apps, openApp} = require('open');
@param name - The app you want to open. Can be either builtin supported `apps` names or other name supported in platform.
@returns The [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process.
// Open Firefox
await openApp(apps.firefox);
@example
```
import open, {openApp, apps} from 'open';
// Open Chrome incognito mode
await openApp(apps.chrome, {arguments: ['--incognito']});
// Open Firefox.
await openApp(apps.firefox);
// Open Xcode
await openApp('xcode');
```
*/
openApp: (name: open.App['name'], options?: open.OpenAppOptions) => Promise<ChildProcess>;
};
// Open Chrome in incognito mode.
await openApp(apps.chrome, {arguments: ['--incognito']});
export = open;
// Open default browser.
await openApp(apps.browser);
// Open default browser in incognito mode.
await openApp(apps.browserPrivate);
// Open Xcode.
await openApp('xcode');
```
*/
export function openApp(name: App['name'], options?: OpenAppOptions): Promise<ChildProcess>;

@@ -1,9 +0,15 @@

const path = require('path');
const childProcess = require('child_process');
const {promises: fs, constants: fsConstants} = require('fs');
const isWsl = require('is-wsl');
const isDocker = require('is-docker');
const defineLazyProperty = require('define-lazy-prop');
import process from 'node:process';
import {Buffer} from 'node:buffer';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import childProcess from 'node:child_process';
import fs from 'node:fs/promises';
import {constants as fsConstants} from 'node:fs'; // TODO: Move this to the above import when targeting Node.js 18.
import isWsl from 'is-wsl';
import defineLazyProperty from 'define-lazy-prop';
import defaultBrowser from 'default-browser';
import isInsideContainer from 'is-inside-container';
// Path to included `xdg-open`.
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const localXdgOpenPath = path.join(__dirname, 'xdg-open');

@@ -13,21 +19,2 @@

// Podman detection
const hasContainerEnv = () => {
try {
fs.statSync('/run/.containerenv');
return true;
} catch {
return false;
}
};
let cachedResult;
function isInsideContainer() {
if (cachedResult === undefined) {
cachedResult = hasContainerEnv() || isDocker();
}
return cachedResult;
}
/**

@@ -98,3 +85,3 @@ Get the mount point for fixed drives in WSL.

allowNonzeroExitCode: false,
...options
...options,
};

@@ -105,7 +92,7 @@

...options,
app: singleApp
app: singleApp,
}));
}
let {name: app, arguments: appArguments = []} = options.app || {};
let {name: app, arguments: appArguments = []} = options.app ?? {};
appArguments = [...appArguments];

@@ -118,7 +105,46 @@

name: appName,
arguments: appArguments
}
arguments: appArguments,
},
}));
}
if (app === 'browser' || app === 'browserPrivate') {
// IDs from default-browser for macOS and windows are the same
const ids = {
'com.google.chrome': 'chrome',
'google-chrome.desktop': 'chrome',
'org.mozilla.firefox': 'firefox',
'firefox.desktop': 'firefox',
'com.microsoft.msedge': 'edge',
'com.microsoft.edge': 'edge',
'microsoft-edge.desktop': 'edge',
};
// Incognito flags for each browser in `apps`.
const flags = {
chrome: '--incognito',
firefox: '--private-window',
edge: '--inPrivate',
};
const browser = await defaultBrowser();
if (browser.id in ids) {
const browserName = ids[browser.id];
if (app === 'browserPrivate') {
appArguments.push(flags[browserName]);
}
return baseOpen({
...options,
app: {
name: apps[browserName],
arguments: appArguments,
},
});
}
throw new Error(`${browser.name} is not supported as a default browser`);
}
let command;

@@ -149,5 +175,5 @@ const cliArguments = [];

command = isWsl ?
`${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe` :
`${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell`;
command = isWsl
? `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe`
: `${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell`;

@@ -157,5 +183,5 @@ cliArguments.push(

'-NonInteractive',
'–ExecutionPolicy',
'-ExecutionPolicy',
'Bypass',
'-EncodedCommand'
'-EncodedCommand',
);

@@ -176,5 +202,5 @@

// Inner quotes are delimited for PowerShell interpretation with backticks.
encodedArguments.push(`"\`"${app}\`""`, '-ArgumentList');
encodedArguments.push(`"\`"${app}\`""`);
if (options.target) {
appArguments.unshift(options.target);
appArguments.push(options.target);
}

@@ -187,3 +213,3 @@ } else if (options.target) {

appArguments = appArguments.map(arg => `"\`"${arg}\`""`);
encodedArguments.push(appArguments.join(','));
encodedArguments.push('-ArgumentList', appArguments.join(','));
}

@@ -207,4 +233,4 @@

const useSystemXdgOpen = process.versions.electron ||
platform === 'android' || isBundled || !exeLocalXdgOpen;
const useSystemXdgOpen = process.versions.electron
?? (platform === 'android' || isBundled || !exeLocalXdgOpen);
command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath;

@@ -262,7 +288,7 @@ }

...options,
target
target,
});
};
const openApp = (name, options) => {
export const openApp = (name, options) => {
if (typeof name !== 'string') {

@@ -272,3 +298,3 @@ throw new TypeError('Expected a `name`');

const {arguments: appArguments = []} = options || {};
const {arguments: appArguments = []} = options ?? {};
if (appArguments !== undefined && appArguments !== null && !Array.isArray(appArguments)) {

@@ -282,4 +308,4 @@ throw new TypeError('Expected `appArguments` as Array type');

name,
arguments: appArguments
}
arguments: appArguments,
},
});

@@ -314,3 +340,3 @@ };

const apps = {};
export const apps = {};

@@ -320,8 +346,8 @@ defineLazyProperty(apps, 'chrome', () => detectPlatformBinary({

win32: 'chrome',
linux: ['google-chrome', 'google-chrome-stable', 'chromium']
linux: ['google-chrome', 'google-chrome-stable', 'chromium'],
}, {
wsl: {
ia32: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe',
x64: ['/mnt/c/Program Files/Google/Chrome/Application/chrome.exe', '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe']
}
x64: ['/mnt/c/Program Files/Google/Chrome/Application/chrome.exe', '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'],
},
}));

@@ -332,5 +358,5 @@

win32: 'C:\\Program Files\\Mozilla Firefox\\firefox.exe',
linux: 'firefox'
linux: 'firefox',
}, {
wsl: '/mnt/c/Program Files/Mozilla Firefox/firefox.exe'
wsl: '/mnt/c/Program Files/Mozilla Firefox/firefox.exe',
}));

@@ -341,10 +367,11 @@

win32: 'msedge',
linux: ['microsoft-edge', 'microsoft-edge-dev']
linux: ['microsoft-edge', 'microsoft-edge-dev'],
}, {
wsl: '/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'
wsl: '/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe',
}));
open.apps = apps;
open.openApp = openApp;
defineLazyProperty(apps, 'browser', () => 'browser');
module.exports = open;
defineLazyProperty(apps, 'browserPrivate', () => 'browserPrivate');
export default open;
{
"name": "open",
"version": "8.4.2",
"version": "9.0.0",
"description": "Open stuff like URLs, files, executables. Cross-platform.",

@@ -13,4 +13,9 @@ "license": "MIT",

},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": ">=12"
"node": ">=14.16"
},

@@ -52,12 +57,13 @@ "scripts": {

"dependencies": {
"define-lazy-prop": "^2.0.0",
"is-docker": "^2.1.1",
"default-browser": "^3.1.0",
"define-lazy-prop": "^3.0.0",
"is-inside-container": "^1.0.0",
"is-wsl": "^2.2.0"
},
"devDependencies": {
"@types/node": "^15.0.0",
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.39.1"
"@types/node": "^18.15.3",
"ava": "^5.2.0",
"tsd": "^0.28.0",
"xo": "^0.53.1"
}
}

@@ -26,6 +26,8 @@ # open

**Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you will have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function. Please don't open issues for questions regarding CommonJS / ESM.
## Usage
```js
const open = require('open');
import open, {openApp, apps} from 'open';

@@ -45,7 +47,10 @@ // Opens the image in the default image viewer and waits for the opened app to quit.

// Open an app
await open.openApp('xcode');
// Opens the URL in the default browser in incognito mode.
await open('https://sindresorhus.com', {app: {name: apps.browserPrivate}});
// Open an app with arguments
await open.openApp(open.apps.chrome, {arguments: ['--incognito']});
// Open an app.
await openApp('xcode');
// Open an app with arguments.
await openApp(apps.chrome, {arguments: ['--incognito']});
```

@@ -106,3 +111,3 @@

The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use [`open.apps`](#openapps) which auto-detects the correct binary to use.
The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use [`apps`](#apps) which auto-detects the correct binary to use.

@@ -122,24 +127,4 @@ You may also pass in the app's full path. For example on WSL, this can be `/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe` for the Windows installation of Chrome.

### open.apps
### openApp(name, options?)
An object containing auto-detected binary names for common apps. Useful to work around [cross-platform differences](#app).
```js
const open = require('open');
await open('https://google.com', {
app: {
name: open.apps.chrome
}
});
```
#### Supported apps
- [`chrome`](https://www.google.com/chrome) - Web browser
- [`firefox`](https://www.mozilla.org/firefox) - Web browser
- [`edge`](https://www.microsoft.com/edge) - Web browser
### open.openApp(name, options?)
Open an app.

@@ -153,3 +138,3 @@

The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use [`open.apps`](#openapps) which auto-detects the correct binary to use.
The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use [`apps`](#apps) which auto-detects the correct binary to use.

@@ -173,2 +158,28 @@ You may also pass in the app's full path. For example on WSL, this can be `/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe` for the Windows installation of Chrome.

### apps
An object containing auto-detected binary names for common apps. Useful to work around [cross-platform differences](#app).
```js
import open, {apps} from 'open';
await open('https://google.com', {
app: {
name: apps.chrome
}
});
```
`browser` and `browserPrivate` can also be used to access the user's default browser through [`default-browser`](https://github.com/sindresorhus/default-browser).
#### Supported apps
- [`chrome`](https://www.google.com/chrome) - Web browser
- [`firefox`](https://www.mozilla.org/firefox) - Web browser
- [`edge`](https://www.microsoft.com/edge) - Web browser
- `browser` - Default web browser
- `browserPrivate` - Default web browser in incognito mode
`browser` and `browserPrivate` only supports `chrome`, `firefox`, and `edge`.
## Related

@@ -175,0 +186,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc