Skip to content

Commit 4b459cf

Browse files
committed
DEPRECATE: move checksum code to @dr-js/core
1 parent ef1828e commit 4b459cf

File tree

2 files changed

+14
-101
lines changed

2 files changed

+14
-101
lines changed

source/node/cache/checksum.js

Lines changed: 12 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,12 @@
1-
import { resolve, relative, dirname } from 'path'
2-
import { createReadStream, promises as fsAsync } from 'fs'
3-
import { createHash } from 'crypto'
4-
5-
import { compareString } from '@dr-js/core/module/common/compare.js'
6-
import { createAsyncLane, extendAutoSelectLane } from '@dr-js/core/module/common/module/AsyncLane.js'
1+
import { dirname } from 'path'
2+
import { promises as fsAsync } from 'fs'
73

84
import { calcHash } from '@dr-js/core/module/node/data/Buffer.js'
9-
import { setupStreamPipe, readableStreamToBufferAsync } from '@dr-js/core/module/node/data/Stream.js'
10-
import { PATH_TYPE, getPathStat, getPathTypeFromStat } from '@dr-js/core/module/node/fs/Path.js'
11-
import { getDirInfoTree, walkDirInfoTreeAsync, createDirectory } from '@dr-js/core/module/node/fs/Directory.js'
5+
import { createDirectory } from '@dr-js/core/module/node/fs/Directory.js'
6+
import { describeChecksumOfPathList } from '@dr-js/core/module/node/fs/Checksum.js'
127

138
import { packTime, parseTime, loadStat, saveStat } from './function.js'
149

15-
const getChecksumInfoOfFile = async (
16-
absolutePath, // absolute path of file
17-
fromPath // path will relative to this in output file
18-
) => [ // [ 'relative/path', 'hash-bash64' ]
19-
relative(fromPath, absolutePath),
20-
(await readableStreamToBufferAsync(setupStreamPipe(
21-
createReadStream(absolutePath),
22-
createHash('sha1')
23-
))).toString('base64')
24-
]
25-
26-
const getChecksumInfoListOfPath = async (
27-
path = '.', // relative or absolute path
28-
fromPath = process.cwd(), // path will relative to this in output file
29-
checksumInfoList = []
30-
) => {
31-
path = resolve(fromPath, path) // to absolute path
32-
const collector = async (filePath) => { checksumInfoList.push(await getChecksumInfoOfFile(filePath, fromPath)) }
33-
const pathType = getPathTypeFromStat(await getPathStat(path))
34-
switch (pathType) {
35-
case PATH_TYPE.File:
36-
await collector(path)
37-
break
38-
case PATH_TYPE.Directory: {
39-
const { getTailPromise, pushAuto } = extendAutoSelectLane(createAsyncLane({ laneSize: 4 })) // NOTE: too much or too lane will actually be slower
40-
await walkDirInfoTreeAsync(await getDirInfoTree(path), async (dirInfo) => {
41-
dirInfo.type === PATH_TYPE.File && pushAuto(() => collector(dirInfo.path))
42-
})
43-
await getTailPromise()
44-
break
45-
}
46-
default:
47-
throw new Error(`invalid pathType: ${pathType} for ${path}`)
48-
}
49-
return checksumInfoList
50-
}
51-
52-
const getChecksumInfoListOfPathList = async (
53-
pathList = [], // relative or absolute path
54-
fromPath = process.cwd(), // path will relative to this in output file
55-
checksumInfoList = []
56-
) => {
57-
for (const path of pathList) await getChecksumInfoListOfPath(path, fromPath, checksumInfoList)
58-
return checksumInfoList
59-
}
60-
61-
const describeChecksumInfoList = (checksumInfoList) => checksumInfoList
62-
.sort(([ a ], [ b ]) => compareString(a, b))// sort by relativePath so the order is stable
63-
.map(([ relativePath, hashBase64 ]) => `${hashBase64} ${relativePath}`) // hash first seems better
64-
.join('\n')
65-
66-
const describeChecksumOfPathList = async ({
67-
pathList = [], // relative or absolute path
68-
fromPath = process.cwd() // path will relative to this in output file
69-
}) => describeChecksumInfoList(await getChecksumInfoListOfPathList(pathList, fromPath))
70-
7110
__DEV__ && console.log({
7211
SAMPLE_CHECKSUM_CONFIG: {
7312
pathStatFile: '/absolute/path/to/stat-file', // path of stat file, used to help detect checksum change
@@ -130,13 +69,14 @@ const checksumDetectChange = async (config, isSkipSave = false) => { // set isSk
13069
}
13170

13271
export {
133-
getChecksumInfoOfFile,
134-
getChecksumInfoListOfPath,
135-
getChecksumInfoListOfPathList,
136-
137-
describeChecksumInfoList,
138-
describeChecksumOfPathList,
139-
14072
loadStatFile, saveStatFile,
14173
checksumUpdate, checksumDetectChange
14274
}
75+
76+
export {
77+
getChecksumInfoOfFile, // TODO: DEPRECATE
78+
getChecksumInfoListOfPath, // TODO: DEPRECATE
79+
getChecksumInfoListOfPathList, // TODO: DEPRECATE
80+
describeChecksumInfoList, // TODO: DEPRECATE
81+
describeChecksumOfPathList // TODO: DEPRECATE
82+
} from '@dr-js/core/module/node/fs/Checksum.js'

source/node/cache/checksum.test.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { promises as fsAsync } from 'fs'
22
import { resolve } from 'path'
3-
import { strictEqual, doThrowAsync } from '@dr-js/core/module/common/verify.js'
3+
import { strictEqual } from '@dr-js/core/module/common/verify.js'
44
import { getSampleRange } from '@dr-js/core/module/common/math/sample.js'
55
import { createDirectory, resetDirectory } from '@dr-js/core/module/node/fs/Directory.js'
66
import { modifyDelete } from '@dr-js/core/module/node/fs/Modify.js'
7+
import { describeChecksumOfPathList } from '@dr-js/core/module/node/fs/Checksum.js'
78

89
import {
9-
describeChecksumOfPathList,
10-
1110
loadStatFile, saveStatFile,
1211
checksumUpdate, checksumDetectChange
1312
} from './checksum.js'
@@ -45,32 +44,6 @@ after(async () => {
4544
})
4645

4746
describe('Node.Cache.Checksum', () => {
48-
it('getChecksumFileOfPathList', async () => strictEqual(
49-
await describeChecksumOfPathList({
50-
pathList: [
51-
fromRoot('sample-cache-file-0'),
52-
fromRoot('sample-cache-file-1'),
53-
fromRoot('sample-cache-dir-0/'),
54-
fromRoot('sample-cache-dir-1/')
55-
]
56-
}),
57-
await describeChecksumOfPathList({
58-
pathList: [
59-
fromRoot('sample-cache-dir-0/'),
60-
fromRoot('sample-cache-dir-1/'),
61-
fromRoot('sample-cache-file-0'),
62-
fromRoot('sample-cache-file-1')
63-
]
64-
}),
65-
'path order should not matter'
66-
))
67-
68-
it('getChecksumFileOfPathList path must exist', async () => doThrowAsync(() => describeChecksumOfPathList({
69-
pathList: [
70-
fromRoot('sample-cache-file-not-exist')
71-
]
72-
})))
73-
7447
it('checksumUpdate', async () => {
7548
__DEV__ && info(await describeChecksumOfPathList({ pathList: TEST_CONFIG.pathChecksumList }))
7649

0 commit comments

Comments
 (0)