type
stringclasses
7 values
content
stringlengths
4
9.55k
repo
stringlengths
7
96
path
stringlengths
4
178
language
stringclasses
1 value
ArrowFunction
(dir: string): CacheConfig => { const publicFiles = path.join(dir, 'public') const staticFiles = path.join(dir, 'static') const staticPages = path.join(dir, 'static-pages') const nextData = path.join(dir, '_next', 'data') const nextStatic = path.join(dir, '_next', 'static') return filterNonExistentPathKeys({ publicFiles: { path: publicFiles, cacheControl: PUBLIC_DIR_CACHE_CONTROL, prefix: path.relative(dir, publicFiles) + '/', }, staticFiles: { path: staticFiles, cacheControl: PUBLIC_DIR_CACHE_CONTROL, prefix: path.relative(dir, staticFiles) + '/', }, staticPages: { path: staticPages, cacheControl: SERVER_CACHE_CONTROL, prefix: path.relative(dir, staticPages) + '/', }, nextData: { path: nextData, cacheControl: SERVER_CACHE_CONTROL, prefix: path.relative(dir, nextData) + '/', }, nextStatic: { path: nextStatic, cacheControl: IMMUTABLE_CACHE_CONTROL, prefix: path.relative(dir, nextStatic) + '/', }, }) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(invalidationPaths: string[]): string[] => { const wildCardDirectories = invalidationPaths .filter((invalidationPath) => invalidationPath.endsWith('/*')) .map((invalidationPath) => invalidationPath.replace('/*', '')) return invalidationPaths.filter((invalidationPath) => { return !wildCardDirectories.some( (wildCardDirectory) => invalidationPath.startsWith(wildCardDirectory) && invalidationPath !== `${wildCardDirectory}*` && invalidationPath !== `${wildCardDirectory}/*` && wildCardDirectory !== invalidationPath, ) }) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(invalidationPath) => invalidationPath.endsWith('/*')
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(invalidationPath) => invalidationPath.replace('/*', '')
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(invalidationPath) => { return !wildCardDirectories.some( (wildCardDirectory) => invalidationPath.startsWith(wildCardDirectory) && invalidationPath !== `${wildCardDirectory}*` && invalidationPath !== `${wildCardDirectory}/*` && wildCardDirectory !== invalidationPath, ) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(wildCardDirectory) => invalidationPath.startsWith(wildCardDirectory) && invalidationPath !== `${wildCardDirectory}*` && invalidationPath !== `${wildCardDirectory}/*` && wildCardDirectory !== invalidationPath
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(dynamicPath: string) => { const [base] = dynamicPath.split('/:') const [firstSegment] = base.split('/[') // Ensure this is posix path as CloudFront needs forward slash in invalidation return path.posix.join(firstSegment || '/', '*') }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
( manifest: OriginRequestDefaultHandlerManifest, ): string[] => { return [ ...Object.keys(manifest.pages.html.dynamic).map(dynamicPathToInvalidationPath), ...Object.keys(manifest.pages.html.nonDynamic), ...Object.keys(manifest.pages.ssr.dynamic).map(dynamicPathToInvalidationPath), ...Object.keys(manifest.pages.ssr.nonDynamic), ...Object.keys(manifest.pages.ssg?.dynamic || {}).map(dynamicPathToInvalidationPath), ...Object.keys(manifest.pages.ssg?.nonDynamic || {}), ] }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
async (): Promise<OriginRequestApiHandlerManifest | undefined> => { return readJsonFile(path.join(nextBuildDir, 'api-lambda/manifest.json')) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
async (): Promise< OriginRequestDefaultHandlerManifest | undefined > => { return readJsonFile(path.join(nextBuildDir, 'default-lambda/manifest.json')) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
async (): Promise< OriginRequestImageHandlerManifest | undefined > => { return readJsonFile(path.join(nextBuildDir, 'image-lambda/manifest.json')) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
async (): Promise<RoutesManifest | undefined> => { return readJsonFile(path.join(nextBuildDir, 'default-lambda/routes-manifest.json')) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
async (): Promise<PreRenderedManifest | undefined> => { return readJsonFile(path.join(nextBuildDir, 'default-lambda/prerender-manifest.json')) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
async (resolve, reject) => { // The below options are needed to support following symlinks when building zip files: // - nodir: This will prevent symlinks themselves from being copied into the zip. // - follow: This will follow symlinks and copy the files within. const globOptions = { dot: true, nodir: true, follow: true, cwd: directory, } const files = glob.sync('**', globOptions) // The output here is already sorted const shasum = crypto.createHash('sha256') const output = fs.createWriteStream(outputFile) const archive = archiver('zip') archive.on('warning', reject) archive.on('error', reject) archive.on('data', (data) => { shasum.update(data) }) // archive has been finalized and the output file descriptor has closed, resolve promise // this has to be done before calling `finalize` since the events may fire immediately after. // see https://www.npmjs.com/package/archiver output.once('close', () => { const hash = shasum.digest('hex') resolve(hash) }) archive.pipe(output) // Append files serially to ensure file order for (const file of files) { const fullPath = path.resolve(directory, file) const [data, stat] = await Promise.all([fs.readFile(fullPath), fs.stat(fullPath)]) archive.append(data, { name: file, mode: stat.mode, }) } await archive.finalize() }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(data) => { shasum.update(data) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
() => { const hash = shasum.digest('hex') resolve(hash) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
async (entry: string = process.cwd()): Promise<NextBuild> => { const buildDir = path.resolve(entry, nextBuildDir) await fs.remove(buildDir) const builder = new Builder(entry, nextBuildDir, {args: ['build']}) await builder.build() const [ defaultManifest, apiBuildManifest, imageBuildManifest, routesManifest, prerenderManifest, ] = await Promise.all([ getNextDefaultManifest(), getNextApiBuildManifest(), getNextImageBuildManifest(), getNextRoutesManifest(), getNextPrerenderManifest(), ]) const lambdaBuildDir = path.resolve(entry, nextLambdaDir) await fs.mkdir(lambdaBuildDir, {recursive: true}) const zipLambda = async (lambdaName: string): Promise<string> => { const zipped = path.join(lambdaBuildDir, `${lambdaName}.zip`) const hash = await zipDirectory(path.join(buildDir, lambdaName), zipped) const output = path.join(lambdaBuildDir, `${lambdaName}.${hash}.zip`) await fs.rename(zipped, output) return path.basename(output) } const pathPattern = (pattern: string): string => { const {basePath} = routesManifest || {} return basePath && basePath.length > 0 ? `${basePath.slice(1)}/${pattern}` : pattern } const defaultLambdaPath = await zipLambda('default-lambda') let apiLambdaPath = undefined let imageLambdaPath = undefined let regenerationLambdaPath = undefined const apis = apiBuildManifest?.apis const hasApi = apis && (Object.keys(apis.nonDynamic).length > 0 || Object.keys(apis.dynamic).length > 0) if (hasApi) { apiLambdaPath = await zipLambda('api-lambda') } const hasImages = !!imageBuildManifest if (hasImages) { imageLambdaPath = await zipLambda('image-lambda') } const hasISRPages = prerenderManifest && Object.keys(prerenderManifest.routes).some( (key) => typeof prerenderManifest.routes[key].initialRevalidateSeconds === 'number', ) if (hasISRPages) { regenerationLambdaPath = await zipLambda('regeneration-lambda') } const assetsDir = path.join(buildDir, 'assets') const assets = readAssetsDirectory(assetsDir) return { lambdaBuildDir, defaultLambdaPath, apiLambdaPath, imageLambdaPath, regenerationLambdaPath, assets, imagePath: hasImages ? pathPattern('_next/image*') : undefined, dataPath: pathPattern('_next/data/*'), basePath: pathPattern('_next/*'), staticPath: pathPattern('static/*'), apiPath: hasApi ? pathPattern('api/*') : undefined, invalidationPaths: defaultManifest ? reduceInvalidationPaths(readInvalidationPathsFromManifest(defaultManifest)) : undefined, } }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
async (lambdaName: string): Promise<string> => { const zipped = path.join(lambdaBuildDir, `${lambdaName}.zip`) const hash = await zipDirectory(path.join(buildDir, lambdaName), zipped) const output = path.join(lambdaBuildDir, `${lambdaName}.${hash}.zip`) await fs.rename(zipped, output) return path.basename(output) }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(pattern: string): string => { const {basePath} = routesManifest || {} return basePath && basePath.length > 0 ? `${basePath.slice(1)}/${pattern}` : pattern }
nessjs/ness
src/utils/next.ts
TypeScript
ArrowFunction
(key) => typeof prerenderManifest.routes[key].initialRevalidateSeconds === 'number'
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type DynamicPageKeyValue = { [key: string]: { file: string regex: string } }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type OriginRequestApiHandlerManifest = { apis: { dynamic: DynamicPageKeyValue nonDynamic: { [key: string]: string } } domainRedirects: { [key: string]: string } enableHTTPCompression: boolean authentication?: { username: string password: string } }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type OriginRequestDefaultHandlerManifest = { buildId: string logLambdaExecutionTimes: boolean pages: { ssr: { dynamic: DynamicPageKeyValue nonDynamic: { [key: string]: string } } html: { nonDynamic: { [path: string]: string } dynamic: DynamicPageKeyValue } ssg: { nonDynamic: { [path: string]: unknown } dynamic: { [path: string]: unknown } } } publicFiles: { [key: string]: string } trailingSlash: boolean enableHTTPCompression: boolean domainRedirects: { [key: string]: string } authentication?: { username: string password: string } }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type OriginRequestImageHandlerManifest = { enableHTTPCompression: boolean domainRedirects: { [key: string]: string } }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type RedirectData = { statusCode: number source: string destination: string regex: string internal?: boolean }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type RewriteData = { source: string destination: string regex: string }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type Header = { key: string value: string }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type HeaderData = { source: string headers: Header[] regex: string }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type I18nData = { locales: string[] defaultLocale: string }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type RoutesManifest = { basePath: string redirects: RedirectData[] rewrites: RewriteData[] headers: HeaderData[] i18n?: I18nData }
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
type CacheConfig = Record< string, { cacheControl: string path: string prefix: string } >
nessjs/ness
src/utils/next.ts
TypeScript
TypeAliasDeclaration
export type NextBuild = { lambdaBuildDir: string defaultLambdaPath: string imageLambdaPath?: string apiLambdaPath?: string regenerationLambdaPath?: string assets: CacheConfig basePath: string staticPath: string dataPath: string imagePath?: string apiPath?: string invalidationPaths?: string[] }
nessjs/ness
src/utils/next.ts
TypeScript
FunctionDeclaration
/** * Generates types to represent schema definitions in the swagger */ export function generateSchemaTypes(model: CodeModel, project: Project) { // Track models that need to be imported const importedModels = new Set<string>(); const modelsFile = project.createSourceFile(`src/models.ts`, undefined, { overwrite: true }); const objectsDefinitions = buildObjectInterfaces(model, importedModels); const objectTypeAliases = buildPolymorphicAliases(model); modelsFile.addInterfaces(objectsDefinitions); modelsFile.addTypeAliases(objectTypeAliases); }
ffMathy/autorest.typescript
src/restLevelClient/generateSchemaTypes.ts
TypeScript
ArrowFunction
(metadata) => !isNil(metadata)
lnlfps/symphony-joy
packages/server/src/metadata-scanner.ts
TypeScript
ArrowFunction
(prop: string) => { const descriptor = Object.getOwnPropertyDescriptor(prototype, prop); if (descriptor?.set || descriptor?.get) { return false; } // @ts-ignore return !isConstructor(prop) && isFunction(prototype[prop]); }
lnlfps/symphony-joy
packages/server/src/metadata-scanner.ts
TypeScript
ClassDeclaration
export class MetadataScanner { public scanFromPrototype<T extends unknown, R = any>( instance: T, prototype: object, callback: (name: string) => R ): R[] { const methodNames = new Set(this.getAllFilteredMethodNames(prototype)); return iterate(methodNames) .map(callback) .filter((metadata) => !isNil(metadata)) .toArray(); } *getAllFilteredMethodNames(prototype: object): IterableIterator<string> { const isMethod = (prop: string) => { const descriptor = Object.getOwnPropertyDescriptor(prototype, prop); if (descriptor?.set || descriptor?.get) { return false; } // @ts-ignore return !isConstructor(prop) && isFunction(prototype[prop]); }; do { yield* iterate(Object.getOwnPropertyNames(prototype)) .filter(isMethod) .toArray(); } while ( (prototype = Reflect.getPrototypeOf(prototype)!) && prototype !== Object.prototype ); } }
lnlfps/symphony-joy
packages/server/src/metadata-scanner.ts
TypeScript
MethodDeclaration
public scanFromPrototype<T extends unknown, R = any>( instance: T, prototype: object, callback: (name: string) => R ): R[] { const methodNames = new Set(this.getAllFilteredMethodNames(prototype)); return iterate(methodNames) .map(callback) .filter((metadata) => !isNil(metadata)) .toArray(); }
lnlfps/symphony-joy
packages/server/src/metadata-scanner.ts
TypeScript
MethodDeclaration
*getAllFilteredMethodNames(prototype: object): IterableIterator<string> { const isMethod = (prop: string) => { const descriptor = Object.getOwnPropertyDescriptor(prototype, prop); if (descriptor?.set || descriptor?.get) { return false; } // @ts-ignore return !isConstructor(prop) && isFunction(prototype[prop]); }; do { yield* iterate(Object.getOwnPropertyNames(prototype)) .filter(isMethod) .toArray(); } while ( (prototype = Reflect.getPrototypeOf(prototype)!) && prototype !== Object.prototype ); }
lnlfps/symphony-joy
packages/server/src/metadata-scanner.ts
TypeScript
FunctionDeclaration
export function md5(param: string): string { let md5sum = crypto.createHash('md5'); md5sum.update(param, 'utf8'); return md5sum.digest('hex'); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function sha1(param: string): string { let md5sum = crypto.createHash('sha1'); md5sum.update(param, 'utf8'); return md5sum.digest('hex'); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function isInteger(n: number) { return Number(n) === n && n % 1 === 0; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function isFloat(n: number) { return n === Number(n) && n % 1 !== 0; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function createArray(size: number, initValue: any): any[] { let ret = []; for (let i = 0; i < size; i++) { ret.push(initValue); } return ret; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function copyObject(obj: any): any { if (typeof obj !== 'object' || obj === null) { return obj; } let result = {}; for (let key in obj) { if (obj.hasOwnProperty(key)) { result[key] = copyObject(obj[key]); } } return result; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function addObject(left: any, right: any): void { for (let key in right) { if (right.hasOwnProperty(key)) { if (isNaN(parseInt(right[key]))) { continue; } if (!left[key]) { left[key] = right[key]; } else { left[key] += right[key]; } } } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function copyArray(array: Object[]): any[] { let tempArray: any[] = []; array.forEach((item) => { let temp = copyObject(item); tempArray.push(temp); }); return tempArray; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 在预排序数组二分查找search的下界位置 * 说明:查找第一个**大于等于**search的下标 * @param search 查找内容 * @param length 数组长度 * @param getValue 获取比较值方法 * @returns {number} 下界的下标 */ export function lowerBound(search: number, length: number, getValue: (index: number) => number): number { let left = 0, mid = 0, right = length, value; while (left < right) { mid = Math.floor((left + right) / 2); value = getValue(mid); if (value >= search) { right = mid; } else if (value < search) { left = mid + 1; } } return left; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 在预排序数组二分查找search的上界位置 * 说明: 查找第一个**大于**search的下标 * @param search 查找内容 * @param length 数组长度 * @param getValue 获取比较值方法 * @returns {number} 上界的下标 */ export function upperBound(length: number, search: number, getValue: (index: number) => number): number { let left = 0, mid = 0, right = length, value; while (left < right) { mid = Math.floor((left + right) / 2); value = getValue(mid); if (value > search) { right = mid; } else if (value <= search) { left = mid + 1; } } return left; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * * @param obj * @return {boolean} */ export function isEmpty(obj: any): boolean { // null and undefined are "empty" if (obj === null) { return true; } // Assume if it has a length property with a non-zero value // that that property is correct. if (obj.length > 0) { return false; } if (obj.length === 0) { return true; } // Otherwise, does it have any properties of its own? // Note that this doesn't handle // toString and valueOf enumeration bugs in IE < 9 for (let key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { return false; } } return true; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * get random [0, 100] * @return {number} */ export function randChance(): number { return Math.floor(Math.random() * 101); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * get random float number (high <= low also ok) * Math.random return number [0.0, 1.0) * @param low * @param high * @return {number} */ export function randFloat(low: number, high: number): number { return Math.random() * Math.abs(high - low) + Math.min(low, high); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * get random array sequence * @param low * @param high * @return {number}[low, high) */ export function randInt(low: number, high: number): number { return Math.floor(Math.random() * Math.abs(high - low) + Math.min(low, high)); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * get random array sequence * @param array * @param count - if not given, will rand the whole length of array * @return {number[]} */ export function randArray(array: any[], count: number): any[] { let i, cnt = Math.min(array.length, count || array.length), pos = 0, tmp; for (i = 0; i < cnt - 1; i += 1) { pos = randInt(i + 1, array.length); tmp = array[i]; // see more about the performance of swap two values in http://jsperf.com/swap-two-numbers-without-tmp-let/9 array[i] = array[pos]; // use [with a tmp let] for it support not only number value arrays array[pos] = tmp; } return array; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * @param array * @param count * @param bRepeat * @return {Array} */ export function randByWeight(array: number[], count: number, bRepeat?: boolean): number[] { let i, sum: number = 0, result: number[] = [], repeat: boolean = bRepeat || false, cnt: number = Math.min(array.length, count || array.length), value: number = 0; if (cnt === 0) { return []; } for (i = 0; i < array.length; i += 1) { sum += array[i]; } if (isNaN(sum)) { return []; } let j = 0, used = {}; for (i = 0; i < cnt; i += 1) { value = randInt(0, sum); for (j = 0; j < array.length - i; j += 1) { if (!repeat && used[j]) { continue; } if (array[j] > value) { break; } value -= array[j]; } result.push(j); if (!repeat) { used[j] = true; } } return result; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 根据数组中的权重返回出随机的下标 * @param array */ export function randOneByWeight(array: number[]): number { return array.length === 0 ? null : randByWeight(array, 1, false)[0]; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * * @param obj * @param count * @param bRepeat */ export function randObjectByWeight(obj: { [key: number]: number }, count: number, bRepeat?: boolean): number[] { let keys = Object.keys(obj), values = []; keys.forEach((key) => { values.push(obj[key]); }); let rand = randByWeight(values, count, bRepeat); let result: number[] = []; rand.forEach((pos) => { result.push(parseInt(keys[pos])); }); return result; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function randOneObjectByWeight(obj: { [key: number]: number }): number { let result = randObjectByWeight(obj, 1, false); return result.length === 0 ? null : result[0]; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 对象赚数组 * @param obj * @return {Array} */ export function objectToArray(obj: any): any[] { let arr = []; for (let key in obj) { arr.push(obj[key]); } return arr; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 创建进程pid文件 */ export function createPidFile(): void { pidFile = path.join(process.cwd(), '/.pid'); fs.writeFileSync(pidFile, process.pid); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 创建游戏所使用的资源版本号文件 * @param resVersion */ export function createResVersionFile(resVersion): void { curResVersion = resVersion; let versionFile = path.join(process.cwd(), '/.res_version'); fs.writeFileSync(versionFile, curResVersion); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function getResVersion(): string { if (curResVersion === null) { if (fs.existsSync(path.join(process.cwd(), '/.res_version'))) { curResVersion = fs.readFileSync(path.join(process.cwd(), '/.res_version')).toString(); } else { curResVersion = ''; } } return curResVersion; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 字符串插入至指定idx,后续会偏移 * @param idx * @param str * @param strAppend */ export function stringInsert(idx: number, str: string, strAppend: string): string { if (idx > str.length) { console.error('error insert'); return ''; } else if (idx === str.length) { return (str + strAppend); } else if (idx === 0) { return (strAppend + str); } else { let start = str.substring(0, idx), mid = strAppend, end = str.substring(idx); return (start + mid + end); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 字符串反转 * @param str */ export function stringReverse(str: string): string { return str.split('').reverse().join(''); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 获取指定目录下所有文件名称 * @param path * @param reg */ export function fetchFileList(path: string, reg?: RegExp): string[] { if (path[path.length - 1] !== '/') { path = path + '/'; } let fileList: string[] = []; let fileNames = fs.readdirSync(path); fileNames.forEach((fileName) => { let fullPath = path + fileName; let stat = fs.statSync(fullPath); if (!stat.isDirectory()) { if (reg) { if (reg.test(fileName)) { fileList.push(fileName); } } else { fileList.push(fileName); } } }); return fileList; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export async function httpDownload(url, dest) { return new Promise<void>(((resolve, reject) => { let stream = fs.createWriteStream(dest); request .get(url) .on('response', (response) => { if (response.statusCode !== 200) { reject(new Error(url + ' failed')); } else { response .pipe(stream) .on('close', () => { resolve(); }) .on('error', (e) => { reject(new Error(url + e)); }); } }) .on('error', (e) => { reject(new Error(url + e)); }); })); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * @example: httpGet('http://10.1.1.156/test?cmd=redis',function(error,body){},'json') * 获取http://10.1.1.156/test?cmd=redis返回的结果并解析为json对象 */ export async function httpGet(url: string, dataType: HTTP_RES_DATA_TYPE = HTTP_RES_DATA_TYPE.JSON, timeout?: number) { return new Promise((resolve, reject) => { let options = {'url': url, 'form': null, 'timeout': timeout}; request.get(options, (error, response, body) => { let result = parseHttpResBody(error, response, body, dataType); if (error) { reject(result.error); } else { resolve(result.data); } }); }); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * http POST请求 * 比httpGet函数多了个form表单参数(Json对象),其他参数参考httpGet * @example: */ export async function httpPost(url: string, form: any, dataType: HTTP_RES_DATA_TYPE = HTTP_RES_DATA_TYPE.JSON, timeout?: number) { return new Promise((resolve, reject) => { let options = {'url': url, 'form': form, 'timeout': timeout}; request.post(options, (error, response, body) => { let result = parseHttpResBody(error, response, body, dataType); if (result.error) { reject(result.error); } else { resolve(result.data); } }); }); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
//http get/post请求,返回包体解析函数 function parseHttpResBody(error: any, response: any, body: string, dataType: HTTP_RES_DATA_TYPE) { if (error) { //TODO:根据error.code是否为ETIMEDOUT可判断请求或响应是否超时 //如果需要可在这里增加判断是否超时的错误码 return {'error': ERROR_CODE.COMMON.HTTP_NO_RESPONSE, 'data': null}; } if (response.statusCode === 200) { if (body) { if (dataType === HTTP_RES_DATA_TYPE.JSON) { //body返回的结果为json字符串 try { body = body.replace("\n", ''); let data = JSON.parse(body); return {'error': 0, 'data': data}; } catch (e) { //TODO:json字符串解析为json对象失败,可能字符串中含有特殊编码字符 return { 'error': ERROR_CODE.COMMON.JSON_PARSE_ERROR, 'data': null }; } } else { return {'error': 0, 'data': body}; } } else { return {'error': 0, 'data': body}; } } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 字符串中的字母全部大写 * @param str */ export function capitalize(str: string): string { return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase()); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * mkdir -p * @param dirName */ export function mkdirpSync(dirName) { if (fs.existsSync(dirName)) { return true; } else { if (mkdirpSync(path.dirname(dirName))) { fs.mkdirSync(dirName); return true; } } return false; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
export function parseHttpParams(req: express.Request): { [key: string]: any } { let urlData = url.parse(req.url); let args = {}; // 获取get参数 if (urlData.query) { let querySplits = urlData.query.split('&'); for (let Key in querySplits) { if (querySplits[Key]) { let argSplits = querySplits[Key].split('='); args[argSplits[0]] = argSplits[1]; } } } // 如果请求方式为POST,将post参数合并到args中 if (typeof req.body === 'object') { for (let key in req.body) { args[key] = req.body[key]; } } else if (typeof req.body === 'string') { try { let data = JSON.parse(req.body); for (let key in data) { if (typeof data[key] !== 'function') { args[key] = data[key]; } } } catch (e) { Log.sError(`req.body cannot parse to json: ${req.body} `); } } let ipAddress = null; if (req && typeof (req.headers['x-forwarded-for']) !== 'undefined') { let forwardedIpsStr = req.headers['x-forwarded-for'].toString(); let forwardedIps = forwardedIpsStr.split(','); ipAddress = forwardedIps[0]; } if (req && !ipAddress) { ipAddress = req.ip; } args["IP"] = ipAddress; return args; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 中文两个字符,其余一个字符 * @param {string} str * @returns {number} */ export function charCodeLength(str: string): number { let len = 0; for (let i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) { len += 2; } else { len++; } } return len; }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 自动机初始化,敏感词判断 */ export function initStringChecker() { stringChecker = new StringChecker(); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 自动机构建,构建敏感词库 * @param filterStrings */ export function buildFilterString(filterStrings: string[]) { for (let s of filterStrings) { stringChecker.addPattern(s); } stringChecker.build(); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 如果含有敏感词,会将字符串中的敏感词替换成 *** * @param msg */ export function filterString(msg) { return stringChecker.replace(msg); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 删除目录下所有内容 * @param path */ export function rmAll(path) { let files = []; if (fs.existsSync(path)) { files = fs.readdirSync(path); for (let file of files) { let curPath = path + '/' + file; if (fs.statSync(curPath).isDirectory()) { rmAll(curPath); } else { fs.unlinkSync(curPath); } } fs.rmdirSync(path); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
FunctionDeclaration
/** * 注册进程通用事件 * @param shutDownCallBack */ export function registerProcessListener(shutDownCallBack?: Function) { process.on('uncaughtException', (error => { console.error(error); Log.sError(error); // process.exit(1); })); process.on('unhandledRejection', (reason, p) => { console.error('Unhandled Rejection at:', p, 'reason:', reason); Log.sError('Unhandled Rejection at:', p, 'reason:', reason); // process.exit(1); }); process.on("SIGINT", async () => { if (shutDownCallBack) { Global.isAppValid = false; await shutDownCallBack(); } }); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(item) => { let temp = copyObject(item); tempArray.push(temp); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(key) => { values.push(obj[key]); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(pos) => { result.push(parseInt(keys[pos])); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(fileName) => { let fullPath = path + fileName; let stat = fs.statSync(fullPath); if (!stat.isDirectory()) { if (reg) { if (reg.test(fileName)) { fileList.push(fileName); } } else { fileList.push(fileName); } } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(resolve, reject) => { let stream = fs.createWriteStream(dest); request .get(url) .on('response', (response) => { if (response.statusCode !== 200) { reject(new Error(url + ' failed')); } else { response .pipe(stream) .on('close', () => { resolve(); }) .on('error', (e) => { reject(new Error(url + e)); }); } }) .on('error', (e) => { reject(new Error(url + e)); }); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(response) => { if (response.statusCode !== 200) { reject(new Error(url + ' failed')); } else { response .pipe(stream) .on('close', () => { resolve(); }) .on('error', (e) => { reject(new Error(url + e)); }); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
() => { resolve(); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(e) => { reject(new Error(url + e)); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(e) => { reject(new Error(url + e)); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(resolve, reject) => { let options = {'url': url, 'form': null, 'timeout': timeout}; request.get(options, (error, response, body) => { let result = parseHttpResBody(error, response, body, dataType); if (error) { reject(result.error); } else { resolve(result.data); } }); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(error, response, body) => { let result = parseHttpResBody(error, response, body, dataType); if (error) { reject(result.error); } else { resolve(result.data); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(resolve, reject) => { let options = {'url': url, 'form': form, 'timeout': timeout}; request.post(options, (error, response, body) => { let result = parseHttpResBody(error, response, body, dataType); if (result.error) { reject(result.error); } else { resolve(result.data); } }); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(error, response, body) => { let result = parseHttpResBody(error, response, body, dataType); if (result.error) { reject(result.error); } else { resolve(result.data); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(L) => L.toUpperCase()
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
error => { console.error(error); Log.sError(error); // process.exit(1); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(reason, p) => { console.error('Unhandled Rejection at:', p, 'reason:', reason); Log.sError('Unhandled Rejection at:', p, 'reason:', reason); // process.exit(1); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
async () => { if (shutDownCallBack) { Global.isAppValid = false; await shutDownCallBack(); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
() => { this._isRunning = true; this.promise.apply(this.promiseScope) .then(() => { this._isRunning = false; this.run(); }) .catch((e) => { Log.sError(e); if (!this.bOnExceptionQuit) { this._isRunning = false; this.run(); } }); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
() => { this._isRunning = false; this.run(); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(e) => { Log.sError(e); if (!this.bOnExceptionQuit) { this._isRunning = false; this.run(); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
(resolve) => { if (this._isRunning) { setTimeout(() => { this.stop().then(resolve); }, 100); } else { resolve(); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ArrowFunction
() => { this.stop().then(resolve); }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript
ClassDeclaration
/** * 在终止条件之前会一直按照一定的时间间隔 达到loop的效果,保证每两次promise的时间间隔一定是interval时间 */ export class Loop { interval: number; promise: () => Promise<void>; terminateCallBack: () => boolean; promiseScope: any; bOnExceptionQuit: boolean; private _isRunning: boolean; private _isForceQuit: boolean; constructor(promise: () => Promise<void>, promiseScope: any, cb: () => boolean, interval: number, bOnExceptionQuit: boolean = false) { this.interval = interval; this.promise = promise; this.promiseScope = promiseScope; this.terminateCallBack = cb; this._isRunning = false; this._isForceQuit = false; this.bOnExceptionQuit = bOnExceptionQuit; } run() { if (this._isForceQuit || this.terminateCallBack()) { return; } setTimeout(() => { this._isRunning = true; this.promise.apply(this.promiseScope) .then(() => { this._isRunning = false; this.run(); }) .catch((e) => { Log.sError(e); if (!this.bOnExceptionQuit) { this._isRunning = false; this.run(); } }); }, this.interval); } async stop() { this._isForceQuit = true; return new Promise<void>(((resolve) => { if (this._isRunning) { setTimeout(() => { this.stop().then(resolve); }, 100); } else { resolve(); } })); } }
sunwenteng/bgserver
src/lib/util/game_util.ts
TypeScript