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

Commit d8f2c5f

Browse files
committed
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
1 parent ba5807f commit d8f2c5f

File tree

11 files changed

+91
-48
lines changed

11 files changed

+91
-48
lines changed

SPEC.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
+ 📄 [source/module/Auth.js](source/module/Auth.js)
88
- `AUTH_FILE`, `AUTH_FILE_GROUP`, `AUTH_SKIP`, `DEFAULT_AUTH_KEY`, `configureAuthFile`, `configureAuthFileGroup`, `configureAuthSkip`, `describeAuthFile`, `generateAuthCheckCode`, `generateAuthFile`, `loadAuthFile`, `saveAuthFile`, `verifyAuthCheckCode`
99
+ 📄 [source/module/Compress.js](source/module/Compress.js)
10-
- `checkBloat`, `compressFile`, `compressFileList`
10+
- `checkBloat`, `compressFile`, `compressFileList`, `isBufferGzip`, `isFileGzip`
1111
+ 📄 [source/module/FactDatabase.js](source/module/FactDatabase.js)
1212
- `INITIAL_FACT_INFO`, `createFactDatabase`, `tryDeleteExtraCache`, `tryLoadFactInfo`
1313
+ 📄 [source/module/FileChunkUpload.js](source/module/FileChunkUpload.js)
@@ -43,7 +43,7 @@
4343
+ 📄 [source/module/PathAction/extraCompress.js](source/module/PathAction/extraCompress.js)
4444
- `PATH_ACTION_MAP`, `PATH_ACTION_TYPE`
4545
+ 📄 [source/module/Software/7z.js](source/module/Software/7z.js)
46-
- `compressConfig`, `compressFileConfig`, `compressTgzAsync`, `detect`, `extractConfig`, `extractTgzAsync`, `getCommand`, `setCommand`
46+
- `compressConfig`, `compressFileConfig`, `compressTgzAsync`, `detect`, `extractConfig`, `extractTgzAsync`, `extractTgzOrTarAsync`, `getCommand`, `setCommand`
4747
+ 📄 [source/module/Software/function.js](source/module/Software/function.js)
4848
- `createCommandWrap`, `createDetect`
4949
+ 📄 [source/module/Software/git.js](source/module/Software/git.js)

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@dr-js/node",
4-
"version": "0.3.0-dev.4",
4+
"version": "0.3.0-dev.5",
55
"description": "A collection of strange functions, for node",
66
"author": "dr-js",
77
"license": "MIT",
@@ -38,7 +38,7 @@
3838
"npm": ">=6.14"
3939
},
4040
"dependencies": {
41-
"@dr-js/core": "^0.3.0 || ^0.3.0-dev.9"
41+
"@dr-js/core": "^0.3.0 || ^0.3.0-dev.10"
4242
},
4343
"devDependencies": {
4444
"@dr-js/dev": "0.3.0-dev.8",

source/module/Compress.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createGzip } from 'zlib'
2-
import { createReadStream, createWriteStream, statAsync, unlinkAsync, readableAsync } from '@dr-js/core/module/node/file/function'
2+
import { createReadStream, createWriteStream, readAsync, statAsync, unlinkAsync, readableAsync } from '@dr-js/core/module/node/file/function'
33

44
const compressFile = (
55
inputFile,
@@ -41,8 +41,25 @@ const checkBloat = async (inputFile, outputFile, bloatRatio) => {
4141
return (outputSize * bloatRatio) >= inputSize
4242
}
4343

44+
// the `0x1f8b08` check for: containing a magic number (1f 8b), the compression method (08 for DEFLATE)
45+
// https://en.wikipedia.org/wiki/Gzip
46+
// https://github.com/kevva/is-gzip
47+
// https://stackoverflow.com/questions/6059302/how-to-check-if-a-file-is-gzip-compressed
48+
const isBufferGzip = (buffer) => (
49+
buffer && buffer.length > 3 &&
50+
buffer[ 0 ] === 0x1f &&
51+
buffer[ 1 ] === 0x8b &&
52+
buffer[ 2 ] === 0x08
53+
)
54+
const isFileGzip = async (file) => {
55+
const buffer = Buffer.allocUnsafe(3)
56+
await readAsync(file, buffer, 0, 3, 0)
57+
return isBufferGzip(buffer)
58+
}
59+
4460
export {
4561
compressFile,
4662
compressFileList,
47-
checkBloat
63+
checkBloat,
64+
isBufferGzip, isFileGzip
4865
}

source/module/PathAction/extraCompress.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dirname } from 'path'
22
import { createDirectory } from '@dr-js/core/module/node/file/Directory'
33
import { run } from '@dr-js/core/module/node/system/Run'
44

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

88
const EXTRA_COMPRESS_7Z = 'extra:compress:7z'
@@ -16,33 +16,31 @@ const PATH_ACTION_TYPE = {
1616
EXTRA_COMPRESS_TAR,
1717
EXTRA_EXTRACT_TAR
1818
}
19-
const PATH_ACTION_MAP = {} // filled after detect
2019

21-
detect7z(true) && Object.assign(PATH_ACTION_MAP, {
22-
[ EXTRA_COMPRESS_7Z ]: async (sourceDirectory, outputFile) => {
23-
await createDirectory(dirname(outputFile))
24-
return run(compressConfig7z(sourceDirectory, outputFile)).promise
25-
},
26-
[ EXTRA_EXTRACT_7Z ]: async (sourceFile, outputPath) => {
27-
__DEV__ && console.log({
28-
sourceFile,
29-
outputPath
30-
})
31-
await createDirectory(outputPath)
32-
return run(extractConfig7z(sourceFile, outputPath)).promise
33-
}
34-
})
35-
36-
detectTar(true) && Object.assign(PATH_ACTION_MAP, {
37-
[ EXTRA_COMPRESS_TAR ]: async (sourceDirectory, outputFile) => {
38-
await createDirectory(dirname(outputFile))
39-
return run(compressConfigTar(sourceDirectory, outputFile)).promise
40-
},
41-
[ EXTRA_EXTRACT_TAR ]: async (sourceFile, outputPath) => {
42-
await createDirectory(outputPath)
43-
return run(extractConfigTar(sourceFile, outputPath)).promise
44-
}
45-
})
20+
const PATH_ACTION_MAP = { // filled based on detect result
21+
...(detectTar(true) && { // support `.tar`
22+
[ EXTRA_COMPRESS_TAR ]: async (sourceDirectory, outputFile) => {
23+
await createDirectory(dirname(outputFile))
24+
return run(compressConfigTar(sourceDirectory, outputFile)).promise
25+
},
26+
[ EXTRA_EXTRACT_TAR ]: async (sourceFile, outputPath) => {
27+
await createDirectory(outputPath)
28+
return run(extractConfigTar(sourceFile, outputPath)).promise
29+
}
30+
}),
31+
...(detect7z(true) && { // support `.7z|.zip|...`, and prefer `.tar` with 7zip
32+
[ EXTRA_COMPRESS_7Z ]: async (sourceDirectory, outputFile) => run(compressConfig7z(sourceDirectory, outputFile)).promise,
33+
[ EXTRA_EXTRACT_7Z ]: async (sourceFile, outputPath) => run(extractConfig7z(sourceFile, outputPath)).promise,
34+
[ EXTRA_COMPRESS_TAR ]: async (sourceDirectory, outputFile) => {
35+
await createDirectory(dirname(outputFile))
36+
return compressTgzAsync(sourceDirectory, outputFile)
37+
},
38+
[ EXTRA_EXTRACT_TAR ]: async (sourceFile, outputPath) => {
39+
await createDirectory(outputPath)
40+
return extractTgzOrTarAsync(sourceFile, outputPath)
41+
}
42+
})
43+
}
4644

4745
export {
4846
PATH_ACTION_TYPE,

source/module/PingRace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const batchRequestUrlList = (onResponse, urlList, option, body) => {
2222
})
2323
}))
2424
const clear = () => {
25-
for (const request of requestSet) request.abort()
25+
for (const request of requestSet) request.destroy()
2626
return promise
2727
}
2828
return { requestSet, promise, clear }

source/module/PingRace.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
const { describe, it, info = console.log } = global
77

88
const TEST_URL_LIST = [
9-
// 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
9+
// 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
1010
'http://noop.dr.run', // allow non-exist DNS
1111
'https://noop.dr.run', // allow non-exist DNS
1212
'http://dr.run',

source/module/Software/7z.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { setupStreamPipe, waitStreamStopAsync } from '@dr-js/core/module/node/da
44
import { createReadStream, createWriteStream } from '@dr-js/core/module/node/file/function'
55
import { run } from '@dr-js/core/module/node/system/Run'
66

7+
import { isFileGzip } from '../Compress'
78
import { createCommandWrap, createDetect } from './function'
89

910
// NOTE: require 7z@>=16.00 for `-bs` switch
@@ -21,7 +22,7 @@ const detect = createDetect( // test for: `-bs{o|e|p}{0|1|2} : set output stream
2122
const compressConfig = (sourceDirectory, outputFile) => ({
2223
command: getCommand(),
2324
argList: [
24-
'a', resolve(outputFile), // can ends with `.7z` or `.zip`
25+
'a', resolve(outputFile), // can ends with `.7z|.zip|.tar|.gz|...`
2526
resolve(sourceDirectory, '*'),
2627
'-bso0', '-bsp0' // mute extra output
2728
]
@@ -30,7 +31,7 @@ const compressConfig = (sourceDirectory, outputFile) => ({
3031
const compressFileConfig = (sourceFile, outputFile) => ({
3132
command: getCommand(),
3233
argList: [
33-
'a', resolve(outputFile), // can ends with `.7z` or `.zip`
34+
'a', resolve(outputFile), // can ends with `.7z|.zip|.tar|.gz|...`
3435
resolve(sourceFile),
3536
'-bso0', '-bsp0' // mute extra output
3637
]
@@ -61,7 +62,11 @@ const compressTgzAsync = async (sourceDirectory, outputFile) => { // for `.tgz`
6162
await Promise.all([
6263
waitStreamStopAsync(setupStreamPipe(subProcess.stdout, createGzip(), outputStream)),
6364
promise
64-
])
65+
]).catch((error) => {
66+
subProcess.kill()
67+
outputStream.destroy()
68+
throw error
69+
})
6570
}
6671

6772
const extractTgzAsync = async (sourceFile, outputPath) => { // for `.tgz` or `.tar.gz`
@@ -79,14 +84,22 @@ const extractTgzAsync = async (sourceFile, outputPath) => { // for `.tgz` or `.t
7984
await Promise.all([
8085
waitStreamStopAsync(setupStreamPipe(inputStream, createGunzip(), subProcess.stdin)),
8186
promise
82-
])
87+
]).catch((error) => {
88+
subProcess.kill()
89+
inputStream.destroy()
90+
throw error
91+
})
8392
}
8493

94+
const extractTgzOrTarAsync = async (sourceFile, outputPath) => (sourceFile.endsWith('.tar') || await isFileGzip(sourceFile)) // closer to the auto gunzip of `tar`
95+
? run(extractConfig(sourceFile, outputPath)).promise
96+
: extractTgzAsync(sourceFile, outputPath)
97+
8598
export {
8699
getCommand, setCommand, detect,
87100

88101
compressConfig, compressFileConfig,
89102
extractConfig,
90103

91-
compressTgzAsync, extractTgzAsync
104+
compressTgzAsync, extractTgzAsync, extractTgzOrTarAsync
92105
}

source/module/Software/7z.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { resolve, basename } from 'path'
2+
import { doThrowAsync } from '@dr-js/core/module/common/verify'
23
import { createDirectory } from '@dr-js/core/module/node/file/Directory'
34
import { run } from '@dr-js/core/module/node/system/Run'
45

@@ -14,7 +15,7 @@ import {
1415
compressConfig, compressFileConfig,
1516
extractConfig,
1617

17-
compressTgzAsync, extractTgzAsync
18+
compressTgzAsync, extractTgzAsync, extractTgzOrTarAsync
1819
} from './7z'
1920

2021
const { describe, it, before, after, info = console.log } = global
@@ -69,4 +70,18 @@ describe('Node.Module.Software.7z', () => {
6970
info('verifyOutputDirectory')
7071
await verifyOutputDirectory(fromTemp('extractTgzAsync/test.tgz-extract/'))
7172
})
73+
74+
it('extractTgzOrTarAsync()', async () => {
75+
info('compressTgzAsync')
76+
await run(compressConfig(SOURCE_DIRECTORY, fromTemp('compressTgzAsync/test.tar'))).promise
77+
info('extractTgzAsync')
78+
await extractTgzOrTarAsync(fromTemp('compressTgzAsync/test.tar'), fromTemp('extractTgzOrTarAsync/test.tar-extract/'))
79+
info('verifyOutputDirectory')
80+
await verifyOutputDirectory(fromTemp('extractTgzOrTarAsync/test.tar-extract/'))
81+
82+
await doThrowAsync(
83+
async () => extractTgzAsync(fromTemp('compressTgzAsync/test.tar'), fromTemp('extractTgzOrTarAsync/test.tar-extract/')),
84+
'extractTgzAsync() should throw when not gzip'
85+
)
86+
})
7287
})

source/server/feature/Explorer/HTML/pathContent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ const initPathContent = (
158158
const renderExtraExtractEditList = (relativePath) => {
159159
let actionType
160160
if (IS_EXTRA_7Z && REGEXP_EXTRACT_7Z.test(relativePath)) actionType = PATH_ACTION_TYPE.EXTRA_EXTRACT_7Z
161-
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
161+
if (IS_EXTRA_TAR && REGEXP_EXTRACT_TAR.test(relativePath)) actionType = PATH_ACTION_TYPE.EXTRA_EXTRACT_TAR
162162
return !actionType ? [] : [ cE('button', {
163163
className: 'edit', innerText: TEXT_EXTRACT,
164164
// className: 'edit', innerText: isWideL ? `${TEXT_EXTRACT}${relativePath.split('.').pop()}` : TEXT_EXTRACT,
165165
onclick: async () => pathAction([ '' ], actionType, relativePath, await withPromptModal('Extract To', `${relativePath}.content/`))
166166
}) ]
167167
}
168-
const REGEXP_EXTRACT_7Z = /\.(7z|zip|tbz2?|txz|tar\.(bz2?|xz))$/
168+
const REGEXP_EXTRACT_7Z = /\.(7z|zip|tbz2?|txz|tar(\.bz2?|\.xz))$/
169169
const REGEXP_EXTRACT_TAR = /\.(tgz|tar(\.gz)?)$/
170170

171171
parentElement.innerHTML = ''

0 commit comments

Comments
 (0)