forked from pnp/pnpjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app catalog fixes, test-recorder initial work
- Loading branch information
1 parent
fe58a93
commit c94a447
Showing
12 changed files
with
153 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ coverage | |
# node-waf configuration | ||
.lock-wscript | ||
|
||
.test-recording | ||
|
||
# Dependency directory | ||
node_modules | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
import { addProp } from "@pnp/queryable/add-prop.js"; | ||
import { _Web } from "../webs/types.js"; | ||
import { AppCatalog, IAppCatalog } from "./types.js"; | ||
|
||
declare module "../webs/types" { | ||
interface _Web { | ||
getAppCatalog(url?: string | _Web): IAppCatalog; | ||
appcatalog: IAppCatalog; | ||
} | ||
interface IWeb { | ||
/** | ||
* Gets this web (default) or the web specifed by the optional string case | ||
* as an IAppCatalog instance | ||
* | ||
* @param url [Optional] Url of the web to get (default: current web) | ||
* Gets the appcatalog (if it exists associated with this web) | ||
*/ | ||
getAppCatalog(url?: string | _Web): IAppCatalog; | ||
appcatalog: IAppCatalog; | ||
} | ||
} | ||
|
||
_Web.prototype.getAppCatalog = function (this: _Web, url?: string | _Web): IAppCatalog { | ||
return AppCatalog(url || this); | ||
}; | ||
addProp(_Web, "appcatalog", AppCatalog); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { isFunc, TimelinePipe, dateAdd, getHashCode } from "@pnp/core"; | ||
import { Queryable } from "@pnp/queryable"; | ||
import { statSync, readFileSync, existsSync, writeFileSync, mkdirSync } from "fs"; | ||
import { join, basename } from "path"; | ||
import { getSync } from "stacktrace-js"; | ||
|
||
export function RequestRecorderCache(resolvedRecordingPath: string, mode: "readonly" | "record", isExpired?: (Date) => boolean): TimelinePipe { | ||
|
||
const today = new Date(); | ||
const _isExpired = isFunc(isExpired) ? isExpired : (d: Date) => dateAdd(d, "week", 2) < today; | ||
const recorderFileKey = Symbol.for("recorder_file_key"); | ||
const recorderFilePath = Symbol.for("recorder_file_path"); | ||
|
||
if (!existsSync(resolvedRecordingPath)) { | ||
mkdirSync(resolvedRecordingPath); | ||
} | ||
|
||
return (instance: Queryable) => { | ||
|
||
instance.on.pre(async function (this: Queryable, url: string, init: RequestInit, result: any): Promise<[string, RequestInit, any]> { | ||
|
||
const stack = getSync(); | ||
|
||
this[recorderFileKey] = getHashCode(`${init.method}:${url}:${basename(stack[0].fileName)}:${stack[0].lineNumber}:${stack[0].columnNumber}`).toString(); | ||
this[recorderFilePath] = join(resolvedRecordingPath, `result.${this[recorderFileKey]}.json`); | ||
|
||
if (existsSync(this[recorderFilePath])) { | ||
|
||
const stats = statSync(this[recorderFilePath]); | ||
if (!_isExpired(stats.mtime)) { | ||
result = JSON.parse(readFileSync(this[recorderFilePath]).toString()); | ||
return [url, init, result]; | ||
} | ||
} | ||
|
||
if (mode === "record") { | ||
|
||
this.on.post(async function (url: URL, result: any) { | ||
|
||
if (Reflect.has(this, recorderFilePath)) { | ||
writeFileSync(this[recorderFilePath], JSON.stringify(result)); | ||
} | ||
|
||
return [url, result]; | ||
}); | ||
} | ||
|
||
return [url, init, result]; | ||
}); | ||
|
||
instance.on.dispose(function () { | ||
delete this[recorderFileKey]; | ||
delete this[recorderFilePath]; | ||
}); | ||
|
||
return instance; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters