Skip to content

Commit

Permalink
refactor: Refactor commands creation
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Nov 2, 2018
1 parent 12f2609 commit 7b1eade
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 76 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ event to work as expected. A diffrent value can be used but the output values
from the stream will be impacted. In particular a log level of `0` will cause
the stream not to emit data correctly.

### Security

Values given by the package are not sanitized, you just get the raw output from
the `7z` binary. Remember to never trust user input and sanitize accordingly.

***
With :heart: from [quentinrossetti](http://quentinrossetti.me/)

Expand Down
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Package:
✔ Rewite scripts @done(18-10-17 20:36)
✘ Helper script to optionnaly fetch 7zip bins? @cancelled(18-09-09 19:02)
☐ 7zip-bin as peer dependecy?
☐ upload to npm
Switches:
☐ Add auto-`--` switch when file name starts with `-`
☐ Auto-`-an` (Disable parsing of archive_name) switch
Expand All @@ -28,7 +29,7 @@ Maintenance:
✔ Upgrade codebase to ES6 @done(18-09-11 22:58)
✔ Spaces in achive name, file name or in switches @done(18-08-23 12:36)
☐ Keep a Promise and callback style interface on top (do not recommand using it)
☐ Check all issues from GitHub and test it
☐ Check all issues from GitHub and test them
Features:
✔ Streams? Buffer or Object mode? @done(18-09-09 18:58)
✔ 7z path option @done(18-09-02 19:34)
Expand Down
113 changes: 46 additions & 67 deletions lib/commands.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,73 @@
import { SevenZipStream } from './stream.js'
import {
matchBodyHash,
matchBodyList,
matchBodySymbol,
matchEndOfBodyHyphen,
matchEndOfBodySymbol,
matchEndOfHeadersHyphen,
matchEndOfHeadersSymbol
} from './parser.js'

function setTarget (opts, target) {
const isTargetMultiple = (Array.isArray(target))
if (isTargetMultiple) {
opts._commandArgs = opts._commandArgs.concat(target)
} else if (target) {
opts._commandArgs.push(target)
}
return opts
//
// Stream API
// ==========
//

export function add (archive, source, options) {
return getStreamStandard('a', archive, source, options)._setStandardParsers()
}

function commandStandard (commandLetter, archive, source, options) {
export function remove (archive, source, options) {
return getStreamStandard('d', archive, source, options)._setStandardParsers()
}

export function extract (archive, output, cherryPick, options) {
return getStreamExtract('e', archive, output, cherryPick, options)._setStandardParsers()
}

export function extractFull (archive, output, cherryPick, options) {
return getStreamExtract('x', archive, output, cherryPick, options)._setStandardParsers()
}

export function hash (target, options) {
return getStreamHash('h', target, options)._setHashParsers()
}

export function list (archive, target, options) {
return getStreamStandard('l', archive, target, options)._setListParsers()
}

//
// Library
// =======
//

function getStreamStandard (commandLetter, archive, source, options) {
let opts = Object.assign({}, options)
opts._commandArgs = [commandLetter]
opts._commandArgs.push(archive)
opts = setTarget(opts, source)
opts._matchBodyData = matchBodySymbol
opts._matchEndOfHeaders = matchEndOfHeadersSymbol
opts._matchEndOfBody = matchEndOfBodySymbol
const stream = new SevenZipStream(opts)
let stream = new SevenZipStream(opts)
return stream
}

function commandExtract (commandLetter, archive, output, target, options) {
function getStreamExtract (commandLetter, archive, output, target, options) {
let opts = Object.assign({}, options)
opts._commandArgs = [commandLetter]
opts._commandArgs.push(archive)
if (output) {
opts['o'] = output
}
opts = setTarget(opts, target)
opts._matchBodyData = matchBodySymbol
opts._matchEndOfHeaders = matchEndOfHeadersSymbol
opts._matchEndOfBody = matchEndOfBodySymbol
const stream = new SevenZipStream(opts)
let stream = new SevenZipStream(opts)
return stream
}

function commandHash (commandLetter, target, options) {
function getStreamHash (commandLetter, target, options) {
let opts = Object.assign({}, options)
opts._commandArgs = [commandLetter]
opts = setTarget(opts, target)
opts._matchBodyData = matchBodyHash
opts._matchEndOfHeaders = matchEndOfHeadersHyphen
opts._matchEndOfBody = matchEndOfBodyHyphen
const stream = new SevenZipStream(opts)
return stream
}

function commandList (commandLetter, archive, source, options) {
let opts = Object.assign({}, options)
opts._commandArgs = [commandLetter]
opts._commandArgs.push(archive)
opts = setTarget(opts, source)
opts._matchBodyData = matchBodyList
opts._matchEndOfHeaders = matchEndOfHeadersHyphen
opts._matchEndOfBody = matchEndOfBodyHyphen
const stream = new SevenZipStream(opts)
return stream
}

export function add (archive, source, options) {
return commandStandard('a', archive, source, options)
return new SevenZipStream(opts)
}

export function remove (archive, source, options) {
return commandStandard('d', archive, source, options)
}

export function extract (archive, output, cherryPick, options) {
return commandExtract('e', archive, output, cherryPick, options)
}

export function extractFull (archive, output, cherryPick, options) {
return commandExtract('x', archive, output, cherryPick, options)
}

export function hash (target, options) {
return commandHash('h', target, options)
}

export function list (archive, target, options) {
return commandList('l', archive, target, options)
function setTarget (opts, target) {
const isTargetMultiple = (Array.isArray(target))
if (isTargetMultiple) {
opts._commandArgs = opts._commandArgs.concat(target)
} else if (target) {
opts._commandArgs.push(target)
}
return opts
}
47 changes: 41 additions & 6 deletions lib/stream.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import { spawn } from 'cross-spawn'
import debugModule from 'debug'
import { Readable } from 'stream'
import { matchBodyProgress, matchInfos } from './parser.js'
import { STAGE_BODY, STAGE_FOOTERS, STAGE_HEADERS } from './references.js'
import { LINE_SPLIT, ERR_ONE_LINE, ERR_MULTIPLE_LINE } from './regexp.js'
import {
matchBodyProgress,
matchInfos,
matchBodySymbol,
matchEndOfHeadersSymbol,
matchEndOfBodySymbol,
matchBodyHash,
matchEndOfHeadersHyphen,
matchEndOfBodyHyphen,
matchBodyList
} from './parser.js'
import {
STAGE_BODY,
STAGE_FOOTERS,
STAGE_HEADERS
} from './references.js'
import {
LINE_SPLIT,
ERR_ONE_LINE,
ERR_MULTIPLE_LINE
} from './regexp.js'
import { transformBinToString, transformSpecialArrayToArgs } from './special.js'
import { transformSwitchesToArgs } from './switches.js'

Expand All @@ -29,9 +47,6 @@ export class SevenZipStream extends Readable {
this._binSpawn = binSpawn
this._args = args
this._childProcess = options.$childProcess
this._matchBodyData = options._matchBodyData
this._matchEndOfHeaders = options._matchEndOfHeaders
this._matchEndOfBody = options._matchEndOfBody
this._progressSwitch = args.includes('-bsp1')
this._stage = STAGE_HEADERS
this.info = new Map()
Expand Down Expand Up @@ -89,6 +104,26 @@ export class SevenZipStream extends Readable {
// [https://github.com/nodejs/help/issues/963#issuecomment-372007824]
_read (size) {}

_setStandardParsers () {
this._matchBodyData = matchBodySymbol
this._matchEndOfHeaders = matchEndOfHeadersSymbol
this._matchEndOfBody = matchEndOfBodySymbol
return this
}

_setHashParsers () {
this._matchBodyData = matchBodyHash
this._matchEndOfHeaders = matchEndOfHeadersHyphen
this._matchEndOfBody = matchEndOfBodyHyphen
return this
}

_setListParsers () {
this._matchBodyData = matchBodyList
this._matchEndOfHeaders = matchEndOfHeadersHyphen
this._matchEndOfBody = matchEndOfBodyHyphen
return this
}
// Analyse each line of the current data buffer. If the current line match a
// given condition (e.g: line is a progress value) the current iteration of
// the loop will stop and the next line is analysed from the top of the loop
Expand Down
4 changes: 2 additions & 2 deletions test/unit/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ describe('Unit: parser.js', function () {
// })
const opts = { $defer: true }
opts._commandArgs = ['a']
opts._matchBodyData = matchBodySymbol
opts._matchEndOfHeaders = matchEndOfHeadersSymbol
const stub = new SevenZipStream(opts)
stub._matchBodyData = matchBodySymbol
stub._matchEndOfHeaders = matchEndOfHeadersSymbol

it('should return false on non match', function () {
const r = matchEndOfHeadersSymbol(stub, 'Colon info: type colon info')
Expand Down

0 comments on commit 7b1eade

Please sign in to comment.