aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
3 lines (2 loc) • 5.8 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.clearLargeFileFingerprintCache=clearLargeFileFingerprintCache,exports.fingerprint=fingerprint,exports.contentFingerprint=contentFingerprint;var crypto=()=>{var tmp=require("crypto");return crypto=()=>tmp,tmp},fs=()=>{var tmp=require("fs");return fs=()=>tmp,tmp},path=()=>{var tmp=require("path");return path=()=>tmp,tmp},ignore_1=()=>{var tmp=require("./ignore");return ignore_1=()=>tmp,tmp},options_1=()=>{var tmp=require("./options");return options_1=()=>tmp,tmp},errors_1=()=>{var tmp=require("../errors");return errors_1=()=>tmp,tmp},cache_1=()=>{var tmp=require("../private/cache");return cache_1=()=>tmp,tmp},literal_string_1=()=>{var tmp=require("../private/literal-string");return literal_string_1=()=>tmp,tmp};const CTRL_SOH="",CTRL_SOT="",CTRL_ETX="",fingerprintCache=new(cache_1()).Cache;function clearLargeFileFingerprintCache(){fingerprintCache.clear()}function fingerprint(fileOrDirectory,options={}){const hash=crypto().createHash("sha256");_hashField(hash,"options.extra",options.extraHash||"");const follow=options.follow||options_1().SymlinkFollowMode.EXTERNAL;_hashField(hash,"options.follow",follow),fileOrDirectory=fs().realpathSync(fileOrDirectory);const isDir=fs().statSync(fileOrDirectory).isDirectory(),rootDirectory=isDir?fileOrDirectory:path().dirname(fileOrDirectory),ignoreMode=options.ignoreMode||options_1().IgnoreMode.GLOB;ignoreMode!=options_1().IgnoreMode.GLOB&&_hashField(hash,"options.ignoreMode",ignoreMode);const resolvedRoot=path().resolve(rootDirectory),ignoreStrategy=ignore_1().IgnoreStrategy.fromCopyOptions(options,fileOrDirectory);if(isDir)_processDirectory(fileOrDirectory,fileOrDirectory);else{const hashComponent=path().relative(fileOrDirectory,fileOrDirectory).replace(/\\/g,"/");_hashField(hash,`file:${hashComponent}`,contentFingerprint(fileOrDirectory))}return hash.digest("hex");function _shouldFollowLink(resolvedLinkTarget){switch(follow){case options_1().SymlinkFollowMode.ALWAYS:return!0;case options_1().SymlinkFollowMode.EXTERNAL:return!resolvedLinkTarget.startsWith(resolvedRoot);case options_1().SymlinkFollowMode.BLOCK_EXTERNAL:return resolvedLinkTarget.startsWith(resolvedRoot);case options_1().SymlinkFollowMode.NEVER:return!1;default:return!1}}function _resolveLinkTarget(realPath,linkTarget){return path().isAbsolute(linkTarget)?linkTarget:path().resolve(path().dirname(realPath),linkTarget)}function _processDirectory(symbolicPath,realPath){const sorted=fs().readdirSync(realPath,{withFileTypes:!0}).sort((a,b)=>a.name<b.name?-1:a.name>b.name?1:0);for(const entry of sorted){const childSymbolicPath=path().join(symbolicPath,entry.name),childRealPath=path().join(realPath,entry.name);if(entry.isSymbolicLink())_processSymlink(childSymbolicPath,childRealPath);else if(entry.isFile()){if(ignoreStrategy.ignores(childSymbolicPath))continue;const hashComponent=path().relative(fileOrDirectory,childSymbolicPath).replace(/\\/g,"/");_hashField(hash,`file:${hashComponent}`,contentFingerprint(childRealPath))}else if(entry.isDirectory()){if(ignoreStrategy.completelyIgnores(childSymbolicPath))continue;_processDirectory(childSymbolicPath,childRealPath)}}}function _processSymlink(symbolicPath,realPath){const linkTarget=fs().readlinkSync(realPath),resolvedLinkTarget=_resolveLinkTarget(realPath,linkTarget);if(!_shouldFollowLink(resolvedLinkTarget)){if(ignoreStrategy.ignores(symbolicPath))return;const hashComponent=path().relative(fileOrDirectory,symbolicPath).replace(/\\/g,"/");_hashField(hash,`link:${hashComponent}`,linkTarget);return}let targetStat;try{targetStat=fs().statSync(resolvedLinkTarget,{bigint:!0})}catch{if(ignoreStrategy.ignores(symbolicPath))return;const hashComponent=path().relative(fileOrDirectory,symbolicPath).replace(/\\/g,"/");_hashField(hash,`link:${hashComponent}`,linkTarget);return}if(targetStat.isDirectory()){if(ignoreStrategy.completelyIgnores(symbolicPath))return;_processDirectory(symbolicPath,resolvedLinkTarget)}else if(targetStat.isFile()){if(ignoreStrategy.ignores(symbolicPath))return;const hashComponent=path().relative(fileOrDirectory,symbolicPath).replace(/\\/g,"/");_hashField(hash,`file:${hashComponent}`,contentFingerprintWithStats(resolvedLinkTarget,targetStat))}else throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`UnableToUnableHashNeither`,`Unable to hash ${symbolicPath}: it is neither a file nor a directory`)}}function contentFingerprint(file){const stats=fs().statSync(file,{bigint:!0});return contentFingerprintWithStats(file,stats)}function contentFingerprintWithStats(file,stats){const cacheKey=JSON.stringify({mtime_unix:stats.mtime.toUTCString(),mtime_ms:stats.mtimeMs.toString(),inode:stats.ino.toString(),size:stats.size.toString()});return fingerprintCache.obtain(cacheKey,()=>contentFingerprintMiss(file))}const CRLF_RE=/\r\n/g,TRAILING_CR_RE=/\r$/,BUFFER_SIZE=8*1024;function contentFingerprintMiss(file){const hash=crypto().createHash("sha256"),buffer=Buffer.alloc(BUFFER_SIZE),fd=fs().openSync(file,fs().constants.O_RDONLY);let size=0,isBinary=!1,lastStr="",read=0;try{for(;(read=fs().readSync(fd,buffer,0,BUFFER_SIZE,null))!==0;){const slicedBuffer=buffer.subarray(0,read);if(size===0&&(isBinary=slicedBuffer.indexOf(0)!==-1),isBinary)size+=read,hash.update(slicedBuffer);else{const str=slicedBuffer.toString();if(TRAILING_CR_RE.test(str)){lastStr+=str;continue}const data=lastStr+str;lastStr="";const normalizedData=data.replace(CRLF_RE,`
`),dataBuffer=Buffer.from(normalizedData);size+=dataBuffer.length,hash.update(dataBuffer)}}lastStr&&hash.update(Buffer.from(lastStr))}finally{fs().closeSync(fd)}return`${size}:${hash.digest("hex")}`}function _hashField(hash,header,value){hash.update(CTRL_SOH).update(header).update(CTRL_SOT).update(value).update(CTRL_ETX)}