Skip to content

Commit

Permalink
PROD-1607 import opn and modify for new window (#487)
Browse files Browse the repository at this point in the history
* import and extend 'open'

Signed-off-by: Mason Fish <[email protected]>

* fix flow

Signed-off-by: Mason Fish <[email protected]>

* include src reference

Signed-off-by: Mason Fish <[email protected]>

* pass args in for pcap

Signed-off-by: Mason Fish <[email protected]>

* add downloadPcaps flow

Signed-off-by: Mason Fish <[email protected]>

Co-authored-by: Mason Fish <[email protected]>
  • Loading branch information
mason-fish and Mason Fish authored Mar 19, 2020
1 parent 3e6b413 commit 213ed33
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 40 deletions.
12 changes: 12 additions & 0 deletions acknowledgments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ Redistribution and use in source and binary forms, with or without modification,

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# End salesforce/hassh/LICENSE.txt

# Begin sindresorhus/open/license
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# End sindresorhus/open/license
16 changes: 8 additions & 8 deletions flow-typed/npm/opn_vx.x.x.js → flow-typed/npm/is-wsl_vx.x.x.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// flow-typed signature: d2bc67bb96cff110dc57534ece391206
// flow-typed version: <<STUB>>/opn_v^6.0.0/flow_v0.98.0
// flow-typed signature: 4c797e4f78a925c20bdd15318d08347f
// flow-typed version: <<STUB>>/is-wsl_v2.1.1/flow_v0.98.0

/**
* This is an autogenerated libdef stub for:
*
* 'opn'
* 'is-wsl'
*
* Fill this stub out by replacing all the `any` types.
*
Expand All @@ -13,7 +13,7 @@
* https://github.com/flowtype/flow-typed
*/

declare module 'opn' {
declare module 'is-wsl' {
declare module.exports: any;
}

Expand All @@ -25,9 +25,9 @@ declare module 'opn' {


// Filename aliases
declare module 'opn/index' {
declare module.exports: $Exports<'opn'>;
declare module 'is-wsl/index' {
declare module.exports: $Exports<'is-wsl'>;
}
declare module 'opn/index.js' {
declare module.exports: $Exports<'opn'>;
declare module 'is-wsl/index.js' {
declare module.exports: $Exports<'is-wsl'>;
}
6 changes: 0 additions & 6 deletions flow-typed/npm/opn.js

This file was deleted.

22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@
"d3": "^5.15.0",
"electron-dl": "^3.0.0",
"fs-extra": "^7.0.0",
"is-wsl": "^2.1.1",
"lodash": "^4.17.15",
"markdown-it": "^10.0.0",
"md5": "^2.2.1",
"moment": "^2.24.0",
"moment-timezone": "^0.5.27",
"mousetrap": "^1.6.3",
"on-idle": "^3.1.4",
"opn": "^6.0.0",
"prismjs": "^1.18.0",
"react": "^16.12.0",
"react-day-picker": "^7.4.0",
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/AboutModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import ModalBox from "./ModalBox/ModalBox"
import TextContent from "./TextContent"
import LogoType from "../icons/LogoType"
import Octocat from "../icons/Octocat"
import {open} from "../lib/System"
import {remote} from "electron"
import {join} from "path"
import open from "../lib/open"

export default function AboutModal() {
const appVersion = remote.app.getVersion()
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/LogDetails/ColumnDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MarkdownIt from "markdown-it"
import React from "react"

import type {Column} from "../../types"
import {open} from "../../lib/System"
import open from "../../lib/open"
import Tip from "../Tip"
import brim from "../../brim"

Expand Down
5 changes: 2 additions & 3 deletions src/js/components/RightPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import React from "react"
import type {DispatchProps} from "../state/types"
import type {Space} from "../state/Spaces/types"
import {XRightPaneExpander} from "./RightPaneExpander"
import {open} from "../lib/System"
import {reactElementProps} from "../test/integration"
import Back from "./icons/back-arrow.svg"
import Forward from "./icons/forward-arrow.svg"
import Log from "../models/Log"
import LogDetails from "../state/LogDetails"
import LogDetailsComponent from "./LogDetails"
import Packets from "../state/Packets"
import Pane, {
PaneHeader,
PaneTitle,
Expand All @@ -26,6 +24,7 @@ import RightPaneCollapser from "./RightPaneCollapser"
import Tab from "../state/Tab"
import View from "../state/View"
import dispatchToProps from "../lib/dispatchToProps"
import {downloadPcap} from "../flows/downloadPcap"

type StateProps = {|
currentLog: Log,
Expand All @@ -52,7 +51,7 @@ export default class RightPane extends React.Component<Props, S> {
}

onPacketsClick = () => {
this.props.dispatch(Packets.fetch(this.props.currentLog)).then(open)
this.props.dispatch(downloadPcap(this.props.currentLog))
}

render() {
Expand Down
6 changes: 3 additions & 3 deletions src/js/electron/menu/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import {
appendQueryNotIn,
appendQuerySortBy
} from "../../flows/searchBar/actions"
import {open} from "../../lib/System"
import open from "../../lib/open"
import {viewLogDetail} from "../../flows/viewLogDetail"
import Log from "../../models/Log"
import Modal from "../../state/Modal"
import Packets from "../../state/Packets"
import SearchBar from "../../state/SearchBar"
import View from "../../state/View"
import action from "./action"
import brim from "../../brim"
import submitSearch from "../../flows/submitSearch"
import tab from "../../state/Tab"
import virusTotal from "../../services/virusTotal"
import {downloadPcap} from "../../flows/downloadPcap"

function buildActions() {
return {
Expand Down Expand Up @@ -122,7 +122,7 @@ function buildActions() {
label: "Download PCAPS",
listener(dispatch, log) {
log = new Log(log.tuple, log.descriptor)
dispatch(Packets.fetch(log)).then(open)
dispatch(downloadPcap(log))
}
}),
sortAsc: action({
Expand Down
11 changes: 11 additions & 0 deletions src/js/flows/downloadPcap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* @flow */
import type {Thunk} from "../state/types"
import Packets from "../state/Packets"
import open from "../lib/open"
import Log from "../models/Log"

export const downloadPcap = (currentLog: Log): Thunk => (dispatch) => {
dispatch(Packets.fetch(currentLog)).then((pcapFile) =>
open(pcapFile, {newWindow: true})
)
}
6 changes: 0 additions & 6 deletions src/js/lib/System.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
/* @flow */

import opn from "opn"

import {remote} from "electron"

import downloads from "./downloadsDir"

export const open = (path: string) => {
return opn(path)
}

export const downloadsDir = () => {
return downloads()
}
Expand Down
2 changes: 2 additions & 0 deletions src/js/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import file from "./file"
import keep from "./keep"
import obj from "./obj"
import win from "./win"
import open from "./open"

let array = {
wrap(item: *) {
Expand All @@ -28,6 +29,7 @@ export default {
win,
array,
animation,
open,
date,
on: (...args: *) => document.addEventListener(...args),
off: (...args: *) => document.removeEventListener(...args),
Expand Down
125 changes: 125 additions & 0 deletions src/js/lib/open.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* @flow */
// code from https://github.com/sindresorhus/open/releases/tag/v6.0.0
"use strict"
const {promisify} = require("util")
const path = require("path")
const childProcess = require("child_process")
const isWsl = require("is-wsl")

const pExecFile = promisify(childProcess.execFile)

// Convert a path from WSL format to Windows format:
// `/mnt/c/Program Files/Example/MyApp.exe` → `C:\Program Files\Example\MyApp.exe``
const wslToWindowsPath = async (path) => {
const {stdout} = await pExecFile("wslpath", ["-w", path])
return stdout.trim()
}

export default async function open(target: string, options?: Object) {
if (typeof target !== "string") {
throw new TypeError("Expected a `target`")
}

options = {
wait: false,
newWindow: false,
...options
}

let command
let appArguments = []
const cliArguments = []
const childProcessOptions = {}

if (Array.isArray(options.app)) {
appArguments = options.app.slice(1)
options.app = options.app[0]
}

if (process.platform === "darwin") {
command = "open"

if (options.newWindow) {
cliArguments.push("-n")
}

if (options.wait) {
cliArguments.push("-W")
}

if (options.app) {
cliArguments.push("-a", options.app)
}
} else if (process.platform === "win32" || isWsl) {
command = "cmd" + (isWsl ? ".exe" : "")
cliArguments.push("/c", "start", '""', "/b")
target = target.replace(/&/g, "^&")

if (options.wait) {
cliArguments.push("/wait")
}

if (options.app) {
if (isWsl && options.app.startsWith("/mnt/")) {
const windowsPath = await wslToWindowsPath(options.app)
options.app = windowsPath
}

cliArguments.push(options.app)
}

if (appArguments.length > 0) {
cliArguments.push(...appArguments)
}
} else {
if (options.app) {
command = options.app
} else {
const useSystemXdgOpen =
process.versions.electron || process.platform === "android"
command = useSystemXdgOpen ? "xdg-open" : path.join(__dirname, "xdg-open")
}

if (appArguments.length > 0) {
cliArguments.push(...appArguments)
}

if (!options.wait) {
// `xdg-open` will block the process unless stdio is ignored
// and it's detached from the parent even if it's unref'd.
childProcessOptions.stdio = "ignore"
childProcessOptions.detached = true
}
}

cliArguments.push(target)

if (process.platform === "darwin" && appArguments.length > 0) {
cliArguments.push("--args", ...appArguments)
}

const subprocess = childProcess.spawn(
command,
cliArguments,
childProcessOptions
)

if (options.wait) {
return new Promise((resolve, reject) => {
subprocess.once("error", reject)

subprocess.once("close", (exitCode) => {
if (exitCode > 0) {
reject(new Error(`Exited with code ${exitCode}`))
return
}

resolve(subprocess)
})
})
}

subprocess.unref()

return subprocess
}

0 comments on commit 213ed33

Please sign in to comment.