UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

3 lines (2 loc) 6.99 kB
"use strict";var __createBinding=exports&&exports.__createBinding||(Object.create?(function(o,m,k,k2){k2===void 0&&(k2=k);var desc=Object.getOwnPropertyDescriptor(m,k);(!desc||("get"in desc?!m.__esModule:desc.writable||desc.configurable))&&(desc={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,desc)}):(function(o,m,k,k2){k2===void 0&&(k2=k),o[k2]=m[k]})),__setModuleDefault=exports&&exports.__setModuleDefault||(Object.create?(function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}):function(o,v){o.default=v}),__importStar=exports&&exports.__importStar||(function(){var ownKeys=function(o){return ownKeys=Object.getOwnPropertyNames||function(o2){var ar=[];for(var k in o2)Object.prototype.hasOwnProperty.call(o2,k)&&(ar[ar.length]=k);return ar},ownKeys(o)};return function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k=ownKeys(mod),i=0;i<k.length;i++)k[i]!=="default"&&__createBinding(result,mod,k[i]);return __setModuleDefault(result,mod),result}})();Object.defineProperty(exports,"__esModule",{value:!0}),exports.clearLargeFileFingerprintCache=clearLargeFileFingerprintCache,exports.fingerprint=fingerprint,exports.contentFingerprint=contentFingerprint;var crypto=()=>{var tmp=__importStar(require("crypto"));return crypto=()=>tmp,tmp},fs=()=>{var tmp=__importStar(require("fs"));return fs=()=>tmp,tmp},path=()=>{var tmp=__importStar(require("path"));return path=()=>tmp,tmp},fingerprint_disk_cache_1=()=>{var tmp=require("./fingerprint-disk-cache");return fingerprint_disk_cache_1=()=>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},literal_string_1=()=>{var tmp=require("../private/literal-string");return literal_string_1=()=>tmp,tmp};const CTRL_SOH="",CTRL_SOT="",CTRL_ETX="";function clearLargeFileFingerprintCache(){}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),cache=new(fingerprint_disk_cache_1()).FingerprintDiskCache(resolvedRoot);function _contentFingerprint(file){const stats=fs().statSync(file,{bigint:!0});return contentFingerprintWithStats(file,stats,cache)}if(isDir)_processDirectory(fileOrDirectory,fileOrDirectory);else{const hashComponent=path().relative(fileOrDirectory,fileOrDirectory).replace(/\\/g,"/");_hashField(hash,`file:${hashComponent}`,_contentFingerprint(fileOrDirectory))}return cache.save(),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,cache))}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,void 0)}function contentFingerprintWithStats(file,stats,cache){const cacheKey=`${stats.ino}|${stats.mtimeMs}|${stats.size}`;return cache?.obtain(cacheKey,()=>contentFingerprintMiss(file))??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)}