Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
notable change:
- node: use `request.destroy()|request.destroyed` instead of `request.abort()|request.aborted` follow: nodejs/node#32807
- add: `extractTgzOrTarAsync` to `module/Software/7z`
- script explorer compress code
- package update
  • Loading branch information
dr-js committed Apr 29, 2020
1 parent ba5807f commit d8f2c5f
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 48 deletions.
4 changes: 2 additions & 2 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
+ 📄 [source/module/Auth.js](source/module/Auth.js)
- `AUTH_FILE`, `AUTH_FILE_GROUP`, `AUTH_SKIP`, `DEFAULT_AUTH_KEY`, `configureAuthFile`, `configureAuthFileGroup`, `configureAuthSkip`, `describeAuthFile`, `generateAuthCheckCode`, `generateAuthFile`, `loadAuthFile`, `saveAuthFile`, `verifyAuthCheckCode`
+ 📄 [source/module/Compress.js](source/module/Compress.js)
- `checkBloat`, `compressFile`, `compressFileList`
- `checkBloat`, `compressFile`, `compressFileList`, `isBufferGzip`, `isFileGzip`
+ 📄 [source/module/FactDatabase.js](source/module/FactDatabase.js)
- `INITIAL_FACT_INFO`, `createFactDatabase`, `tryDeleteExtraCache`, `tryLoadFactInfo`
+ 📄 [source/module/FileChunkUpload.js](source/module/FileChunkUpload.js)
Expand Down Expand Up @@ -43,7 +43,7 @@
+ 📄 [source/module/PathAction/extraCompress.js](source/module/PathAction/extraCompress.js)
- `PATH_ACTION_MAP`, `PATH_ACTION_TYPE`
+ 📄 [source/module/Software/7z.js](source/module/Software/7z.js)
- `compressConfig`, `compressFileConfig`, `compressTgzAsync`, `detect`, `extractConfig`, `extractTgzAsync`, `getCommand`, `setCommand`
- `compressConfig`, `compressFileConfig`, `compressTgzAsync`, `detect`, `extractConfig`, `extractTgzAsync`, `extractTgzOrTarAsync`, `getCommand`, `setCommand`
+ 📄 [source/module/Software/function.js](source/module/Software/function.js)
- `createCommandWrap`, `createDetect`
+ 📄 [source/module/Software/git.js](source/module/Software/git.js)
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@dr-js/node",
"version": "0.3.0-dev.4",
"version": "0.3.0-dev.5",
"description": "A collection of strange functions, for node",
"author": "dr-js",
"license": "MIT",
Expand Down Expand Up @@ -38,7 +38,7 @@
"npm": ">=6.14"
},
"dependencies": {
"@dr-js/core": "^0.3.0 || ^0.3.0-dev.9"
"@dr-js/core": "^0.3.0 || ^0.3.0-dev.10"
},
"devDependencies": {
"@dr-js/dev": "0.3.0-dev.8",
Expand Down
21 changes: 19 additions & 2 deletions source/module/Compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createGzip } from 'zlib'
import { createReadStream, createWriteStream, statAsync, unlinkAsync, readableAsync } from '@dr-js/core/module/node/file/function'
import { createReadStream, createWriteStream, readAsync, statAsync, unlinkAsync, readableAsync } from '@dr-js/core/module/node/file/function'

const compressFile = (
inputFile,
Expand Down Expand Up @@ -41,8 +41,25 @@ const checkBloat = async (inputFile, outputFile, bloatRatio) => {
return (outputSize * bloatRatio) >= inputSize
}

// the `0x1f8b08` check for: containing a magic number (1f 8b), the compression method (08 for DEFLATE)
// https://en.wikipedia.org/wiki/Gzip
// https://github.com/kevva/is-gzip
// https://stackoverflow.com/questions/6059302/how-to-check-if-a-file-is-gzip-compressed
const isBufferGzip = (buffer) => (
buffer && buffer.length > 3 &&
buffer[ 0 ] === 0x1f &&
buffer[ 1 ] === 0x8b &&
buffer[ 2 ] === 0x08
)
const isFileGzip = async (file) => {
const buffer = Buffer.allocUnsafe(3)
await readAsync(file, buffer, 0, 3, 0)
return isBufferGzip(buffer)
}

export {
compressFile,
compressFileList,
checkBloat
checkBloat,
isBufferGzip, isFileGzip
}
52 changes: 25 additions & 27 deletions source/module/PathAction/extraCompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dirname } from 'path'
import { createDirectory } from '@dr-js/core/module/node/file/Directory'
import { run } from '@dr-js/core/module/node/system/Run'

import { detect as detect7z, compressConfig as compressConfig7z, extractConfig as extractConfig7z } from 'source/module/Software/7z'
import { detect as detect7z, compressConfig as compressConfig7z, extractConfig as extractConfig7z, compressTgzAsync, extractTgzOrTarAsync } from 'source/module/Software/7z'
import { detect as detectTar, compressConfig as compressConfigTar, extractConfig as extractConfigTar } from 'source/module/Software/tar'

const EXTRA_COMPRESS_7Z = 'extra:compress:7z'
Expand All @@ -16,33 +16,31 @@ const PATH_ACTION_TYPE = {
EXTRA_COMPRESS_TAR,
EXTRA_EXTRACT_TAR
}
const PATH_ACTION_MAP = {} // filled after detect

detect7z(true) && Object.assign(PATH_ACTION_MAP, {
[ EXTRA_COMPRESS_7Z ]: async (sourceDirectory, outputFile) => {
await createDirectory(dirname(outputFile))
return run(compressConfig7z(sourceDirectory, outputFile)).promise
},
[ EXTRA_EXTRACT_7Z ]: async (sourceFile, outputPath) => {
__DEV__ && console.log({
sourceFile,
outputPath
})
await createDirectory(outputPath)
return run(extractConfig7z(sourceFile, outputPath)).promise
}
})

detectTar(true) && Object.assign(PATH_ACTION_MAP, {
[ EXTRA_COMPRESS_TAR ]: async (sourceDirectory, outputFile) => {
await createDirectory(dirname(outputFile))
return run(compressConfigTar(sourceDirectory, outputFile)).promise
},
[ EXTRA_EXTRACT_TAR ]: async (sourceFile, outputPath) => {
await createDirectory(outputPath)
return run(extractConfigTar(sourceFile, outputPath)).promise
}
})
const PATH_ACTION_MAP = { // filled based on detect result
...(detectTar(true) && { // support `.tar`
[ EXTRA_COMPRESS_TAR ]: async (sourceDirectory, outputFile) => {
await createDirectory(dirname(outputFile))
return run(compressConfigTar(sourceDirectory, outputFile)).promise
},
[ EXTRA_EXTRACT_TAR ]: async (sourceFile, outputPath) => {
await createDirectory(outputPath)
return run(extractConfigTar(sourceFile, outputPath)).promise
}
}),
...(detect7z(true) && { // support `.7z|.zip|...`, and prefer `.tar` with 7zip
[ EXTRA_COMPRESS_7Z ]: async (sourceDirectory, outputFile) => run(compressConfig7z(sourceDirectory, outputFile)).promise,
[ EXTRA_EXTRACT_7Z ]: async (sourceFile, outputPath) => run(extractConfig7z(sourceFile, outputPath)).promise,
[ EXTRA_COMPRESS_TAR ]: async (sourceDirectory, outputFile) => {
await createDirectory(dirname(outputFile))
return compressTgzAsync(sourceDirectory, outputFile)
},
[ EXTRA_EXTRACT_TAR ]: async (sourceFile, outputPath) => {
await createDirectory(outputPath)
return extractTgzOrTarAsync(sourceFile, outputPath)
}
})
}

export {
PATH_ACTION_TYPE,
Expand Down
2 changes: 1 addition & 1 deletion source/module/PingRace.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const batchRequestUrlList = (onResponse, urlList, option, body) => {
})
}))
const clear = () => {
for (const request of requestSet) request.abort()
for (const request of requestSet) request.destroy()
return promise
}
return { requestSet, promise, clear }
Expand Down
2 changes: 1 addition & 1 deletion source/module/PingRace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
const { describe, it, info = console.log } = global

const TEST_URL_LIST = [
// TODO: NOTE: the noop dns lookup may take ~10sec on win32 and block node exit, but there's no API to abort dns lookup, check: https://github.com/nodejs/node/issues/7231
// TODO: NOTE: the noop dns lookup may take ~10sec on win32 and block node exit, but there's no API to stop dns lookup, check: https://github.com/nodejs/node/issues/7231
'http://noop.dr.run', // allow non-exist DNS
'https://noop.dr.run', // allow non-exist DNS
'http://dr.run',
Expand Down
23 changes: 18 additions & 5 deletions source/module/Software/7z.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { setupStreamPipe, waitStreamStopAsync } from '@dr-js/core/module/node/da
import { createReadStream, createWriteStream } from '@dr-js/core/module/node/file/function'
import { run } from '@dr-js/core/module/node/system/Run'

import { isFileGzip } from '../Compress'
import { createCommandWrap, createDetect } from './function'

// NOTE: require 7z@>=16.00 for `-bs` switch
Expand All @@ -21,7 +22,7 @@ const detect = createDetect( // test for: `-bs{o|e|p}{0|1|2} : set output stream
const compressConfig = (sourceDirectory, outputFile) => ({
command: getCommand(),
argList: [
'a', resolve(outputFile), // can ends with `.7z` or `.zip`
'a', resolve(outputFile), // can ends with `.7z|.zip|.tar|.gz|...`
resolve(sourceDirectory, '*'),
'-bso0', '-bsp0' // mute extra output
]
Expand All @@ -30,7 +31,7 @@ const compressConfig = (sourceDirectory, outputFile) => ({
const compressFileConfig = (sourceFile, outputFile) => ({
command: getCommand(),
argList: [
'a', resolve(outputFile), // can ends with `.7z` or `.zip`
'a', resolve(outputFile), // can ends with `.7z|.zip|.tar|.gz|...`
resolve(sourceFile),
'-bso0', '-bsp0' // mute extra output
]
Expand Down Expand Up @@ -61,7 +62,11 @@ const compressTgzAsync = async (sourceDirectory, outputFile) => { // for `.tgz`
await Promise.all([
waitStreamStopAsync(setupStreamPipe(subProcess.stdout, createGzip(), outputStream)),
promise
])
]).catch((error) => {
subProcess.kill()
outputStream.destroy()
throw error
})
}

const extractTgzAsync = async (sourceFile, outputPath) => { // for `.tgz` or `.tar.gz`
Expand All @@ -79,14 +84,22 @@ const extractTgzAsync = async (sourceFile, outputPath) => { // for `.tgz` or `.t
await Promise.all([
waitStreamStopAsync(setupStreamPipe(inputStream, createGunzip(), subProcess.stdin)),
promise
])
]).catch((error) => {
subProcess.kill()
inputStream.destroy()
throw error
})
}

const extractTgzOrTarAsync = async (sourceFile, outputPath) => (sourceFile.endsWith('.tar') || await isFileGzip(sourceFile)) // closer to the auto gunzip of `tar`
? run(extractConfig(sourceFile, outputPath)).promise
: extractTgzAsync(sourceFile, outputPath)

export {
getCommand, setCommand, detect,

compressConfig, compressFileConfig,
extractConfig,

compressTgzAsync, extractTgzAsync
compressTgzAsync, extractTgzAsync, extractTgzOrTarAsync
}
17 changes: 16 additions & 1 deletion source/module/Software/7z.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resolve, basename } from 'path'
import { doThrowAsync } from '@dr-js/core/module/common/verify'
import { createDirectory } from '@dr-js/core/module/node/file/Directory'
import { run } from '@dr-js/core/module/node/system/Run'

Expand All @@ -14,7 +15,7 @@ import {
compressConfig, compressFileConfig,
extractConfig,

compressTgzAsync, extractTgzAsync
compressTgzAsync, extractTgzAsync, extractTgzOrTarAsync
} from './7z'

const { describe, it, before, after, info = console.log } = global
Expand Down Expand Up @@ -69,4 +70,18 @@ describe('Node.Module.Software.7z', () => {
info('verifyOutputDirectory')
await verifyOutputDirectory(fromTemp('extractTgzAsync/test.tgz-extract/'))
})

it('extractTgzOrTarAsync()', async () => {
info('compressTgzAsync')
await run(compressConfig(SOURCE_DIRECTORY, fromTemp('compressTgzAsync/test.tar'))).promise
info('extractTgzAsync')
await extractTgzOrTarAsync(fromTemp('compressTgzAsync/test.tar'), fromTemp('extractTgzOrTarAsync/test.tar-extract/'))
info('verifyOutputDirectory')
await verifyOutputDirectory(fromTemp('extractTgzOrTarAsync/test.tar-extract/'))

await doThrowAsync(
async () => extractTgzAsync(fromTemp('compressTgzAsync/test.tar'), fromTemp('extractTgzOrTarAsync/test.tar-extract/')),
'extractTgzAsync() should throw when not gzip'
)
})
})
4 changes: 2 additions & 2 deletions source/server/feature/Explorer/HTML/pathContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ const initPathContent = (
const renderExtraExtractEditList = (relativePath) => {
let actionType
if (IS_EXTRA_7Z && REGEXP_EXTRACT_7Z.test(relativePath)) actionType = PATH_ACTION_TYPE.EXTRA_EXTRACT_7Z
if ((IS_EXTRA_TAR || IS_EXTRA_7Z) && REGEXP_EXTRACT_TAR.test(relativePath)) actionType = IS_EXTRA_TAR ? PATH_ACTION_TYPE.EXTRA_EXTRACT_TAR : PATH_ACTION_TYPE.EXTRA_EXTRACT_7Z // prefer tar for .tgz
if (IS_EXTRA_TAR && REGEXP_EXTRACT_TAR.test(relativePath)) actionType = PATH_ACTION_TYPE.EXTRA_EXTRACT_TAR
return !actionType ? [] : [ cE('button', {
className: 'edit', innerText: TEXT_EXTRACT,
// className: 'edit', innerText: isWideL ? `${TEXT_EXTRACT}${relativePath.split('.').pop()}` : TEXT_EXTRACT,
onclick: async () => pathAction([ '' ], actionType, relativePath, await withPromptModal('Extract To', `${relativePath}.content/`))
}) ]
}
const REGEXP_EXTRACT_7Z = /\.(7z|zip|tbz2?|txz|tar\.(bz2?|xz))$/
const REGEXP_EXTRACT_7Z = /\.(7z|zip|tbz2?|txz|tar(\.bz2?|\.xz))$/
const REGEXP_EXTRACT_TAR = /\.(tgz|tar(\.gz)?)$/

parentElement.innerHTML = ''
Expand Down
2 changes: 1 addition & 1 deletion source/server/feature/Explorer/configureFeaturePack.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const configureFeaturePack = async ({
IS_READ_ONLY = !mergePath, // TODO: should be decided by user permission

IS_EXTRA_7Z = Boolean(EXTRA_COMPRESS_PATH_ACTION_MAP[ EXTRA_COMPRESS_PATH_ACTION_TYPE.EXTRA_COMPRESS_7Z ]),
IS_EXTRA_TAR = !IS_EXTRA_7Z && Boolean(EXTRA_COMPRESS_PATH_ACTION_MAP[ EXTRA_COMPRESS_PATH_ACTION_TYPE.EXTRA_COMPRESS_TAR ]) // prefer 7z
IS_EXTRA_TAR = Boolean(EXTRA_COMPRESS_PATH_ACTION_MAP[ EXTRA_COMPRESS_PATH_ACTION_TYPE.EXTRA_COMPRESS_TAR ])
}) => {
const URL_HTML = `${routePrefix}/explorer`
const URL_PATH_ACTION = `${routePrefix}/path-action`
Expand Down

0 comments on commit d8f2c5f

Please sign in to comment.