@ms-cloudpack/inline-scripts 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/lib/bootstrap.inline.d.ts +1 -0
- package/lib/bootstrap.inline.d.ts.map +1 -0
- package/lib/bootstrap.inline.js +39 -0
- package/lib/bootstrap.inline.js.map +1 -0
- package/lib/defineProcess.inline.d.ts +1 -0
- package/lib/defineProcess.inline.d.ts.map +1 -0
- package/lib/defineProcess.inline.js +8 -0
- package/lib/defineProcess.inline.js.map +1 -0
- package/lib/errorHandler.inline.d.ts +8 -0
- package/lib/errorHandler.inline.d.ts.map +1 -0
- package/lib/errorHandler.inline.js +19 -0
- package/lib/errorHandler.inline.js.map +1 -0
- package/lib/getBrowserCacheRatio.inline.d.ts +11 -0
- package/lib/getBrowserCacheRatio.inline.d.ts.map +1 -0
- package/lib/getBrowserCacheRatio.inline.js +35 -0
- package/lib/getBrowserCacheRatio.inline.js.map +1 -0
- package/lib/getInlineScript.d.ts +10 -0
- package/lib/getInlineScript.d.ts.map +1 -0
- package/lib/getInlineScript.js +27 -0
- package/lib/getInlineScript.js.map +1 -0
- package/lib/getInlineScripts.d.ts +6 -0
- package/lib/getInlineScripts.d.ts.map +1 -0
- package/lib/getInlineScripts.js +15 -0
- package/lib/getInlineScripts.js.map +1 -0
- package/lib/getPageLoadTime.inline.d.ts +2 -0
- package/lib/getPageLoadTime.inline.d.ts.map +1 -0
- package/lib/getPageLoadTime.inline.js +98 -0
- package/lib/getPageLoadTime.inline.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -0
- package/lib/readInlineScript.d.ts +2 -0
- package/lib/readInlineScript.d.ts.map +1 -0
- package/lib/readInlineScript.js +8 -0
- package/lib/readInlineScript.js.map +1 -0
- package/lib/registerDefineFlags.inline.d.ts +3 -0
- package/lib/registerDefineFlags.inline.d.ts.map +1 -0
- package/lib/registerDefineFlags.inline.js +22 -0
- package/lib/registerDefineFlags.inline.js.map +1 -0
- package/package.json +35 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=bootstrap.inline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bootstrap.inline.d.ts","sourceRoot":"","sources":["../src/bootstrap.inline.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
window.__cloudpack ??= {};
|
|
3
|
+
window.__cloudpack.bootstrap = ({ inlineScripts, overlayScript, importMap, entryScripts }) => {
|
|
4
|
+
window.__cloudpack ??= {};
|
|
5
|
+
if (window.__cloudpack.hasBootstrapRun) {
|
|
6
|
+
console.warn('Cloudpack bootstrap has already run, skipping.');
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
window.__cloudpack.hasBootstrapRun = true;
|
|
10
|
+
// Inject the import map
|
|
11
|
+
const importMapScriptTag = document.createElement('script');
|
|
12
|
+
importMapScriptTag.type = 'importmap';
|
|
13
|
+
importMapScriptTag.textContent = JSON.stringify(importMap);
|
|
14
|
+
document.head.appendChild(importMapScriptTag);
|
|
15
|
+
// Inject the inline scripts
|
|
16
|
+
for (const inlineScript of inlineScripts) {
|
|
17
|
+
const inlineTag = document.createElement('script');
|
|
18
|
+
inlineTag.type = 'module';
|
|
19
|
+
inlineTag.textContent = inlineScript;
|
|
20
|
+
document.head.appendChild(inlineTag);
|
|
21
|
+
}
|
|
22
|
+
// Inject the overlay script
|
|
23
|
+
if (overlayScript) {
|
|
24
|
+
const overlayScriptTag = document.createElement('script');
|
|
25
|
+
overlayScriptTag.type = 'module';
|
|
26
|
+
overlayScriptTag.src = overlayScript;
|
|
27
|
+
document.head.appendChild(overlayScriptTag);
|
|
28
|
+
}
|
|
29
|
+
// Inject the main script
|
|
30
|
+
if (entryScripts) {
|
|
31
|
+
for (const entryScript of entryScripts) {
|
|
32
|
+
const entryScriptTag = document.createElement('script');
|
|
33
|
+
entryScriptTag.type = 'module';
|
|
34
|
+
entryScriptTag.src = entryScript;
|
|
35
|
+
document.head.appendChild(entryScriptTag);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=bootstrap.inline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bootstrap.inline.js","sourceRoot":"","sources":["../src/bootstrap.inline.ts"],"names":[],"mappings":";AAAA,MAAM,CAAC,WAAW,KAAK,EAAE,CAAC;AAC1B,MAAM,CAAC,WAAW,CAAC,SAAS,GAAG,CAAC,EAAE,aAAa,EAAE,aAAa,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,EAAE;IAC3F,MAAM,CAAC,WAAW,KAAK,EAAE,CAAC;IAC1B,IAAI,MAAM,CAAC,WAAW,CAAC,eAAe,EAAE,CAAC;QACvC,OAAO,CAAC,IAAI,CAAC,gDAAgD,CAAC,CAAC;QAC/D,OAAO;IACT,CAAC;IACD,MAAM,CAAC,WAAW,CAAC,eAAe,GAAG,IAAI,CAAC;IAC1C,wBAAwB;IACxB,MAAM,kBAAkB,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAC5D,kBAAkB,CAAC,IAAI,GAAG,WAAW,CAAC;IACtC,kBAAkB,CAAC,WAAW,GAAG,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IAC3D,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC;IAE9C,4BAA4B;IAC5B,KAAK,MAAM,YAAY,IAAI,aAAa,EAAE,CAAC;QACzC,MAAM,SAAS,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACnD,SAAS,CAAC,IAAI,GAAG,QAAQ,CAAC;QAC1B,SAAS,CAAC,WAAW,GAAG,YAAY,CAAC;QACrC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IACvC,CAAC;IAED,4BAA4B;IAC5B,IAAI,aAAa,EAAE,CAAC;QAClB,MAAM,gBAAgB,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAC1D,gBAAgB,CAAC,IAAI,GAAG,QAAQ,CAAC;QACjC,gBAAgB,CAAC,GAAG,GAAG,aAAa,CAAC;QACrC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC;IAC9C,CAAC;IAED,yBAAyB;IACzB,IAAI,YAAY,EAAE,CAAC;QACjB,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE,CAAC;YACvC,MAAM,cAAc,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;YACxD,cAAc,CAAC,IAAI,GAAG,QAAQ,CAAC;YAC/B,cAAc,CAAC,GAAG,GAAG,WAAW,CAAC;YACjC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;AACH,CAAC,CAAC","sourcesContent":["window.__cloudpack ??= {};\nwindow.__cloudpack.bootstrap = ({ inlineScripts, overlayScript, importMap, entryScripts }) => {\n window.__cloudpack ??= {};\n if (window.__cloudpack.hasBootstrapRun) {\n console.warn('Cloudpack bootstrap has already run, skipping.');\n return;\n }\n window.__cloudpack.hasBootstrapRun = true;\n // Inject the import map\n const importMapScriptTag = document.createElement('script');\n importMapScriptTag.type = 'importmap';\n importMapScriptTag.textContent = JSON.stringify(importMap);\n document.head.appendChild(importMapScriptTag);\n\n // Inject the inline scripts\n for (const inlineScript of inlineScripts) {\n const inlineTag = document.createElement('script');\n inlineTag.type = 'module';\n inlineTag.textContent = inlineScript;\n document.head.appendChild(inlineTag);\n }\n\n // Inject the overlay script\n if (overlayScript) {\n const overlayScriptTag = document.createElement('script');\n overlayScriptTag.type = 'module';\n overlayScriptTag.src = overlayScript;\n document.head.appendChild(overlayScriptTag);\n }\n\n // Inject the main script\n if (entryScripts) {\n for (const entryScript of entryScripts) {\n const entryScriptTag = document.createElement('script');\n entryScriptTag.type = 'module';\n entryScriptTag.src = entryScript;\n document.head.appendChild(entryScriptTag);\n }\n }\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=defineProcess.inline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineProcess.inline.d.ts","sourceRoot":"","sources":["../src/defineProcess.inline.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// This file must NOT import other files, because they won't be resolved at runtime.
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
+
window.process ??= {};
|
|
5
|
+
window.process.env ??= {};
|
|
6
|
+
// Assign true to process.browser for packages like readable-stream
|
|
7
|
+
process.browser = true;
|
|
8
|
+
//# sourceMappingURL=defineProcess.inline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineProcess.inline.js","sourceRoot":"","sources":["../src/defineProcess.inline.ts"],"names":[],"mappings":";AAAA,oFAAoF;AAEpF,8DAA8D;AAC9D,MAAM,CAAC,OAAO,KAAK,EAAS,CAAC;AAC7B,MAAM,CAAC,OAAO,CAAC,GAAG,KAAK,EAAE,CAAC;AAE1B,mEAAmE;AAClE,OAA2C,CAAC,OAAO,GAAG,IAAI,CAAC","sourcesContent":["// This file must NOT import other files, because they won't be resolved at runtime.\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nwindow.process ??= {} as any;\nwindow.process.env ??= {};\n\n// Assign true to process.browser for packages like readable-stream\n(process as unknown as { browser: boolean }).browser = true;\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare const pageErrors: {
|
|
2
|
+
uncaughtErrors: ErrorEvent[];
|
|
3
|
+
uncaughtRejections: PromiseRejectionEvent[];
|
|
4
|
+
unregister: () => void;
|
|
5
|
+
};
|
|
6
|
+
declare function handleError(event: ErrorEvent): void;
|
|
7
|
+
declare function handleRejection(event: PromiseRejectionEvent): void;
|
|
8
|
+
//# sourceMappingURL=errorHandler.inline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.inline.d.ts","sourceRoot":"","sources":["../src/errorHandler.inline.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,UAAU;oBACQ,UAAU,EAAE;wBACR,qBAAqB,EAAE;;CAKjD,CAAC;AAEH,iBAAS,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAE5C;AAED,iBAAS,eAAe,CAAC,KAAK,EAAE,qBAAqB,GAAG,IAAI,CAE3D"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// This file must NOT import other files, because they won't be resolved at runtime.
|
|
3
|
+
const pageErrors = (window.__pageErrors = {
|
|
4
|
+
uncaughtErrors: [],
|
|
5
|
+
uncaughtRejections: [],
|
|
6
|
+
unregister: () => {
|
|
7
|
+
window.removeEventListener('error', handleError);
|
|
8
|
+
window.removeEventListener('unhandledrejection', handleRejection);
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
function handleError(event) {
|
|
12
|
+
pageErrors.uncaughtErrors.push(event);
|
|
13
|
+
}
|
|
14
|
+
function handleRejection(event) {
|
|
15
|
+
pageErrors.uncaughtRejections.push(event);
|
|
16
|
+
}
|
|
17
|
+
window.addEventListener('error', handleError);
|
|
18
|
+
window.addEventListener('unhandledrejection', handleRejection);
|
|
19
|
+
//# sourceMappingURL=errorHandler.inline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.inline.js","sourceRoot":"","sources":["../src/errorHandler.inline.ts"],"names":[],"mappings":";AAAA,oFAAoF;AAEpF,MAAM,UAAU,GAAG,CAAC,MAAM,CAAC,YAAY,GAAG;IACxC,cAAc,EAAE,EAAkB;IAClC,kBAAkB,EAAE,EAA6B;IACjD,UAAU,EAAE,GAAG,EAAE;QACf,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QACjD,MAAM,CAAC,mBAAmB,CAAC,oBAAoB,EAAE,eAAe,CAAC,CAAC;IACpE,CAAC;CACF,CAAC,CAAC;AAEH,SAAS,WAAW,CAAC,KAAiB;IACpC,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC;AAED,SAAS,eAAe,CAAC,KAA4B;IACnD,UAAU,CAAC,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC5C,CAAC;AAED,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;AAC9C,MAAM,CAAC,gBAAgB,CAAC,oBAAoB,EAAE,eAAe,CAAC,CAAC","sourcesContent":["// This file must NOT import other files, because they won't be resolved at runtime.\n\nconst pageErrors = (window.__pageErrors = {\n uncaughtErrors: [] as ErrorEvent[],\n uncaughtRejections: [] as PromiseRejectionEvent[],\n unregister: () => {\n window.removeEventListener('error', handleError);\n window.removeEventListener('unhandledrejection', handleRejection);\n },\n});\n\nfunction handleError(event: ErrorEvent): void {\n pageErrors.uncaughtErrors.push(event);\n}\n\nfunction handleRejection(event: PromiseRejectionEvent): void {\n pageErrors.uncaughtRejections.push(event);\n}\n\nwindow.addEventListener('error', handleError);\nwindow.addEventListener('unhandledrejection', handleRejection);\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare const scriptLoadResults: Record<string, 'cache' | 'network'>;
|
|
2
|
+
declare const bundleServerUrl: string;
|
|
3
|
+
declare function isPerformanceResourceTiming(entry: PerformanceEntry): entry is PerformanceResourceTiming;
|
|
4
|
+
declare function handlePerformanceEntry(item: PerformanceEntry): void;
|
|
5
|
+
declare const perfObserver: PerformanceObserver;
|
|
6
|
+
declare function getBrowserCacheRatio(): {
|
|
7
|
+
ratio: number;
|
|
8
|
+
hit: number;
|
|
9
|
+
total: number;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=getBrowserCacheRatio.inline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getBrowserCacheRatio.inline.d.ts","sourceRoot":"","sources":["../src/getBrowserCacheRatio.inline.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,CAAM,CAAC;AAMlE,QAAA,MAAM,eAAe,QAAwD,CAAC;AAE9E,iBAAS,2BAA2B,CAAC,KAAK,EAAE,gBAAgB,GAAG,KAAK,IAAI,yBAAyB,CAEhG;AAED,iBAAS,sBAAsB,CAAC,IAAI,EAAE,gBAAgB,GAAG,IAAI,CAI5D;AAED,QAAA,MAAM,YAAY,qBAIhB,CAAC;AAUH,iBAAS,oBAAoB,IAAI;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAM7E"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// This file must NOT import other files (except types), because they won't be resolved at runtime.
|
|
3
|
+
const scriptLoadResults = {};
|
|
4
|
+
if (!window.__cloudpack?.pageSessionContext) {
|
|
5
|
+
throw new Error('window.__cloudpack.pageSessionContext is not defined');
|
|
6
|
+
}
|
|
7
|
+
const bundleServerUrl = window.__cloudpack.pageSessionContext.bundleServerUrl;
|
|
8
|
+
function isPerformanceResourceTiming(entry) {
|
|
9
|
+
return entry.entryType === 'resource';
|
|
10
|
+
}
|
|
11
|
+
function handlePerformanceEntry(item) {
|
|
12
|
+
if (isPerformanceResourceTiming(item) && item.name.startsWith(bundleServerUrl)) {
|
|
13
|
+
scriptLoadResults[item.name] = item.transferSize === 0 ? 'cache' : 'network';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const perfObserver = new PerformanceObserver((list) => {
|
|
17
|
+
for (const item of list.getEntries()) {
|
|
18
|
+
handlePerformanceEntry(item);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
// Get all the resources that were loaded before this script
|
|
22
|
+
for (const entry of performance.getEntriesByType('resource')) {
|
|
23
|
+
handlePerformanceEntry(entry);
|
|
24
|
+
}
|
|
25
|
+
// Observe all resources that are loaded after this script
|
|
26
|
+
perfObserver.observe({ type: 'resource', buffered: true });
|
|
27
|
+
function getBrowserCacheRatio() {
|
|
28
|
+
const cacheCount = Object.values(scriptLoadResults).filter((source) => source == 'cache').length;
|
|
29
|
+
const totalCount = Object.keys(scriptLoadResults).length;
|
|
30
|
+
const ratio = cacheCount / totalCount;
|
|
31
|
+
return { ratio, hit: cacheCount, total: totalCount };
|
|
32
|
+
}
|
|
33
|
+
window.__cloudpack ??= {};
|
|
34
|
+
window.__cloudpack.getBrowserCacheRatio = getBrowserCacheRatio;
|
|
35
|
+
//# sourceMappingURL=getBrowserCacheRatio.inline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getBrowserCacheRatio.inline.js","sourceRoot":"","sources":["../src/getBrowserCacheRatio.inline.ts"],"names":[],"mappings":";AAAA,mGAAmG;AAEnG,MAAM,iBAAiB,GAAwC,EAAE,CAAC;AAElE,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,kBAAkB,EAAE,CAAC;IAC5C,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;AAC1E,CAAC;AAED,MAAM,eAAe,GAAG,MAAM,CAAC,WAAW,CAAC,kBAAkB,CAAC,eAAe,CAAC;AAE9E,SAAS,2BAA2B,CAAC,KAAuB;IAC1D,OAAO,KAAK,CAAC,SAAS,KAAK,UAAU,CAAC;AACxC,CAAC;AAED,SAAS,sBAAsB,CAAC,IAAsB;IACpD,IAAI,2BAA2B,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,EAAE,CAAC;QAC/E,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IAC/E,CAAC;AACH,CAAC;AAED,MAAM,YAAY,GAAG,IAAI,mBAAmB,CAAC,CAAC,IAAI,EAAE,EAAE;IACpD,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;QACrC,sBAAsB,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,4DAA4D;AAC5D,KAAK,MAAM,KAAK,IAAI,WAAW,CAAC,gBAAgB,CAAC,UAAU,CAAC,EAAE,CAAC;IAC7D,sBAAsB,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAED,0DAA0D;AAC1D,YAAY,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AAE3D,SAAS,oBAAoB;IAC3B,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,IAAI,OAAO,CAAC,CAAC,MAAM,CAAC;IACjG,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAAC;IACzD,MAAM,KAAK,GAAG,UAAU,GAAG,UAAU,CAAC;IAEtC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,EAAW,CAAC;AAChE,CAAC;AAED,MAAM,CAAC,WAAW,KAAK,EAAE,CAAC;AAC1B,MAAM,CAAC,WAAW,CAAC,oBAAoB,GAAG,oBAAoB,CAAC","sourcesContent":["// This file must NOT import other files (except types), because they won't be resolved at runtime.\n\nconst scriptLoadResults: Record<string, 'cache' | 'network'> = {};\n\nif (!window.__cloudpack?.pageSessionContext) {\n throw new Error('window.__cloudpack.pageSessionContext is not defined');\n}\n\nconst bundleServerUrl = window.__cloudpack.pageSessionContext.bundleServerUrl;\n\nfunction isPerformanceResourceTiming(entry: PerformanceEntry): entry is PerformanceResourceTiming {\n return entry.entryType === 'resource';\n}\n\nfunction handlePerformanceEntry(item: PerformanceEntry): void {\n if (isPerformanceResourceTiming(item) && item.name.startsWith(bundleServerUrl)) {\n scriptLoadResults[item.name] = item.transferSize === 0 ? 'cache' : 'network';\n }\n}\n\nconst perfObserver = new PerformanceObserver((list) => {\n for (const item of list.getEntries()) {\n handlePerformanceEntry(item);\n }\n});\n\n// Get all the resources that were loaded before this script\nfor (const entry of performance.getEntriesByType('resource')) {\n handlePerformanceEntry(entry);\n}\n\n// Observe all resources that are loaded after this script\nperfObserver.observe({ type: 'resource', buffered: true });\n\nfunction getBrowserCacheRatio(): { ratio: number; hit: number; total: number } {\n const cacheCount = Object.values(scriptLoadResults).filter((source) => source == 'cache').length;\n const totalCount = Object.keys(scriptLoadResults).length;\n const ratio = cacheCount / totalCount;\n\n return { ratio, hit: cacheCount, total: totalCount } as const;\n}\n\nwindow.__cloudpack ??= {};\nwindow.__cloudpack.getBrowserCacheRatio = getBrowserCacheRatio;\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type KnownInlineScript = 'bootstrap' | 'registerDefineFlags' | 'defineProcess';
|
|
2
|
+
/**
|
|
3
|
+
* Reads the inline script file from the file system and returns it.
|
|
4
|
+
* It caches the result for subsequent calls.
|
|
5
|
+
* @param script The name of the inline script to read.
|
|
6
|
+
* @returns The contents of the inline script file.
|
|
7
|
+
* @throws Error if the script is unknown
|
|
8
|
+
*/
|
|
9
|
+
export declare function getInlineScript(script: KnownInlineScript): Promise<string>;
|
|
10
|
+
//# sourceMappingURL=getInlineScript.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getInlineScript.d.ts","sourceRoot":"","sources":["../src/getInlineScript.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG,qBAAqB,GAAG,eAAe,CAAC;AAUtF;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,CAahF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { readInlineScript } from './readInlineScript.js';
|
|
4
|
+
const scriptCache = {};
|
|
5
|
+
const scripts = {
|
|
6
|
+
bootstrap: 'bootstrap.inline.js',
|
|
7
|
+
registerDefineFlags: 'registerDefineFlags.inline.js',
|
|
8
|
+
defineProcess: 'defineProcess.inline.js',
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Reads the inline script file from the file system and returns it.
|
|
12
|
+
* It caches the result for subsequent calls.
|
|
13
|
+
* @param script The name of the inline script to read.
|
|
14
|
+
* @returns The contents of the inline script file.
|
|
15
|
+
* @throws Error if the script is unknown
|
|
16
|
+
*/
|
|
17
|
+
export async function getInlineScript(script) {
|
|
18
|
+
const scriptFileName = scripts[script];
|
|
19
|
+
if (!scriptFileName) {
|
|
20
|
+
throw new Error(`Unknown inline script: ${script}`);
|
|
21
|
+
}
|
|
22
|
+
if (!scriptCache[script]) {
|
|
23
|
+
scriptCache[script] = await readInlineScript(path.join(path.dirname(fileURLToPath(import.meta.url)), scriptFileName));
|
|
24
|
+
}
|
|
25
|
+
return scriptCache[script];
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=getInlineScript.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getInlineScript.js","sourceRoot":"","sources":["../src/getInlineScript.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAIzD,MAAM,WAAW,GAA+C,EAAE,CAAC;AAEnE,MAAM,OAAO,GAAsC;IACjD,SAAS,EAAE,qBAAqB;IAChC,mBAAmB,EAAE,+BAA+B;IACpD,aAAa,EAAE,yBAAyB;CACzC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,MAAyB;IAC7D,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACvC,IAAI,CAAC,cAAc,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,EAAE,CAAC,CAAC;IACtD,CAAC;IAED,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC;QACzB,WAAW,CAAC,MAAM,CAAC,GAAG,MAAM,gBAAgB,CAC1C,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,cAAc,CAAC,CACxE,CAAC;IACJ,CAAC;IAED,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC;AAC7B,CAAC","sourcesContent":["import path from 'path';\nimport { fileURLToPath } from 'url';\nimport { readInlineScript } from './readInlineScript.js';\n\nexport type KnownInlineScript = 'bootstrap' | 'registerDefineFlags' | 'defineProcess';\n\nconst scriptCache: Partial<Record<KnownInlineScript, string>> = {};\n\nconst scripts: Record<KnownInlineScript, string> = {\n bootstrap: 'bootstrap.inline.js',\n registerDefineFlags: 'registerDefineFlags.inline.js',\n defineProcess: 'defineProcess.inline.js',\n};\n\n/**\n * Reads the inline script file from the file system and returns it.\n * It caches the result for subsequent calls.\n * @param script The name of the inline script to read.\n * @returns The contents of the inline script file.\n * @throws Error if the script is unknown\n */\nexport async function getInlineScript(script: KnownInlineScript): Promise<string> {\n const scriptFileName = scripts[script];\n if (!scriptFileName) {\n throw new Error(`Unknown inline script: ${script}`);\n }\n\n if (!scriptCache[script]) {\n scriptCache[script] = await readInlineScript(\n path.join(path.dirname(fileURLToPath(import.meta.url)), scriptFileName),\n );\n }\n\n return scriptCache[script];\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads all the inline scripts except bootstrap from the file system and returns them. It caches the result for subsequent calls.
|
|
3
|
+
* @returns The inline scripts excluding bootstrap.
|
|
4
|
+
*/
|
|
5
|
+
export declare function getInlineScripts(): Promise<string[]>;
|
|
6
|
+
//# sourceMappingURL=getInlineScripts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getInlineScripts.d.ts","sourceRoot":"","sources":["../src/getInlineScripts.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAS1D"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { globSourceFiles } from '@ms-cloudpack/path-utilities';
|
|
2
|
+
import { readInlineScript } from './readInlineScript.js';
|
|
3
|
+
let inlineScripts;
|
|
4
|
+
/**
|
|
5
|
+
* Reads all the inline scripts except bootstrap from the file system and returns them. It caches the result for subsequent calls.
|
|
6
|
+
* @returns The inline scripts excluding bootstrap.
|
|
7
|
+
*/
|
|
8
|
+
export async function getInlineScripts() {
|
|
9
|
+
if (!inlineScripts) {
|
|
10
|
+
const inlineScriptFiles = (await globSourceFiles(import.meta.url, '*.inline')).filter((file) => !file.includes('bootstrap.inline'));
|
|
11
|
+
inlineScripts = await Promise.all(inlineScriptFiles.map((filename) => readInlineScript(filename)));
|
|
12
|
+
}
|
|
13
|
+
return inlineScripts;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=getInlineScripts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getInlineScripts.js","sourceRoot":"","sources":["../src/getInlineScripts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,IAAI,aAAmC,CAAC;AAExC;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,iBAAiB,GAAG,CAAC,MAAM,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,MAAM,CACnF,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAC7C,CAAC;QACF,aAAa,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACrG,CAAC;IAED,OAAO,aAAa,CAAC;AACvB,CAAC","sourcesContent":["import { globSourceFiles } from '@ms-cloudpack/path-utilities';\nimport { readInlineScript } from './readInlineScript.js';\n\nlet inlineScripts: string[] | undefined;\n\n/**\n * Reads all the inline scripts except bootstrap from the file system and returns them. It caches the result for subsequent calls.\n * @returns The inline scripts excluding bootstrap.\n */\nexport async function getInlineScripts(): Promise<string[]> {\n if (!inlineScripts) {\n const inlineScriptFiles = (await globSourceFiles(import.meta.url, '*.inline')).filter(\n (file) => !file.includes('bootstrap.inline'),\n );\n inlineScripts = await Promise.all(inlineScriptFiles.map((filename) => readInlineScript(filename)));\n }\n\n return inlineScripts;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPageLoadTime.inline.d.ts","sourceRoot":"","sources":["../src/getPageLoadTime.inline.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// This file must NOT import other files (except types), because they won't be resolved at runtime.
|
|
2
|
+
// Typing hack to ensure this ID is correct while working around the limitation on runtime imports
|
|
3
|
+
const overlayRootDivId = 'cloudpack-overlay-root';
|
|
4
|
+
/**
|
|
5
|
+
* Performance mark name for page load time.
|
|
6
|
+
* This makes it easier to see the page load time in the performance tab in dev tools
|
|
7
|
+
*/
|
|
8
|
+
const pageLoadMarker = 'CLOUDPACK_PAGE_LOAD_TIME';
|
|
9
|
+
// The below dom elements will be ignored by mutation observer
|
|
10
|
+
const excludedIds = [overlayRootDivId];
|
|
11
|
+
const defaultPltEntryType = 'mark';
|
|
12
|
+
const defaultPltEntryTimeout = 5 * 60 * 1000; // 5 minutes
|
|
13
|
+
function debounce(func, wait) {
|
|
14
|
+
let timeout;
|
|
15
|
+
return function (...args) {
|
|
16
|
+
if (timeout) {
|
|
17
|
+
clearTimeout(timeout);
|
|
18
|
+
}
|
|
19
|
+
timeout = setTimeout(() => {
|
|
20
|
+
timeout = null;
|
|
21
|
+
func(...args);
|
|
22
|
+
}, wait);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function hasMutationNonExcludedNodes(mutation) {
|
|
26
|
+
return Array.from(mutation.addedNodes).some((node) => {
|
|
27
|
+
const id = node.id;
|
|
28
|
+
if (id) {
|
|
29
|
+
return !excludedIds.includes(id);
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
const waitForPerformancEntry = (pltPerfEntry) => {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
let timeout = null;
|
|
37
|
+
const observer = new PerformanceObserver((list) => {
|
|
38
|
+
const entries = list.getEntriesByName(pltPerfEntry.name, pltPerfEntry.type);
|
|
39
|
+
if (entries.length > 0) {
|
|
40
|
+
observer.disconnect();
|
|
41
|
+
if (timeout) {
|
|
42
|
+
clearTimeout(timeout);
|
|
43
|
+
timeout = null;
|
|
44
|
+
}
|
|
45
|
+
resolve(entries[entries.length - 1]);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
observer.observe({ entryTypes: [pltPerfEntry.type ?? defaultPltEntryType] });
|
|
49
|
+
// Add a timeout to reject the promise if the entry is not found within a reasonable time
|
|
50
|
+
timeout = setTimeout(() => {
|
|
51
|
+
observer.disconnect();
|
|
52
|
+
reject(new Error('Performance entry not found within the timeout period'));
|
|
53
|
+
}, pltPerfEntry.timeout ?? defaultPltEntryTimeout);
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
const pageLoadTimePromise = new Promise((resolve, reject) => {
|
|
57
|
+
const pltPerfEntry = window.__cloudpack?.pageSessionContext?.pageLoadTimePerformanceEntry;
|
|
58
|
+
if (pltPerfEntry) {
|
|
59
|
+
// If the pageLoadTimePerformanceMark is set, wait for it and resolve the promise.
|
|
60
|
+
// No need to observe the DOM changes.
|
|
61
|
+
void waitForPerformancEntry(pltPerfEntry)
|
|
62
|
+
.then((entry) => {
|
|
63
|
+
resolve(entry.entryType == 'mark' ? entry.startTime : entry.duration);
|
|
64
|
+
})
|
|
65
|
+
.catch(reject);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const observer = new MutationObserver(domChanged);
|
|
69
|
+
function reportLastAnimationTime(time) {
|
|
70
|
+
// Stop observing the DOM
|
|
71
|
+
observer.disconnect();
|
|
72
|
+
// Resolve the promise with the page load time
|
|
73
|
+
resolve(time);
|
|
74
|
+
}
|
|
75
|
+
const debouncedReportLastAnimationTime = debounce(reportLastAnimationTime, 2000 /* wait ms */);
|
|
76
|
+
function domChanged(mutations) {
|
|
77
|
+
const shouldProcess = mutations.filter(hasMutationNonExcludedNodes).length > 0;
|
|
78
|
+
if (shouldProcess) {
|
|
79
|
+
requestAnimationFrame(() => {
|
|
80
|
+
// Get the current time in ms since the time when navigation has started in window
|
|
81
|
+
const time = performance.now();
|
|
82
|
+
// Clear previous marks
|
|
83
|
+
performance.clearMarks(pageLoadMarker);
|
|
84
|
+
// Mark the page load time
|
|
85
|
+
performance.mark(pageLoadMarker);
|
|
86
|
+
// Report the page load time
|
|
87
|
+
debouncedReportLastAnimationTime(time);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
observer.observe(document.body, {
|
|
92
|
+
childList: true,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
window.__cloudpack ??= {};
|
|
96
|
+
window.__cloudpack.getPageLoadTime = () => pageLoadTimePromise;
|
|
97
|
+
export {};
|
|
98
|
+
//# sourceMappingURL=getPageLoadTime.inline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPageLoadTime.inline.js","sourceRoot":"","sources":["../src/getPageLoadTime.inline.ts"],"names":[],"mappings":"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","sourcesContent":["// This file must NOT import other files (except types), because they won't be resolved at runtime.\n\nimport type { PageLoadTimePerformanceEntry } from '@ms-cloudpack/common-types-browser';\nimport type { elementIds } from '@ms-cloudpack/overlay/constants';\n\n// Typing hack to ensure this ID is correct while working around the limitation on runtime imports\nconst overlayRootDivId: (typeof elementIds)['root'] = 'cloudpack-overlay-root';\n\n/**\n * Performance mark name for page load time.\n * This makes it easier to see the page load time in the performance tab in dev tools\n */\nconst pageLoadMarker = 'CLOUDPACK_PAGE_LOAD_TIME';\n\n// The below dom elements will be ignored by mutation observer\nconst excludedIds: string[] = [overlayRootDivId];\n\ntype DebounceFunc<T extends unknown[]> = (...args: T) => void;\n\nconst defaultPltEntryType = 'mark';\nconst defaultPltEntryTimeout = 5 * 60 * 1000; // 5 minutes\n\nfunction debounce<T extends unknown[]>(func: DebounceFunc<T>, wait: number): DebounceFunc<T> {\n let timeout: ReturnType<typeof setTimeout> | null;\n\n return function (...args: T) {\n if (timeout) {\n clearTimeout(timeout);\n }\n timeout = setTimeout(() => {\n timeout = null;\n func(...args);\n }, wait);\n };\n}\n\nfunction hasMutationNonExcludedNodes(mutation: MutationRecord): boolean {\n return Array.from(mutation.addedNodes).some((node) => {\n const id = (node as { id?: string }).id;\n\n if (id) {\n return !excludedIds.includes(id);\n }\n\n return true;\n });\n}\n\nconst waitForPerformancEntry = (pltPerfEntry: PageLoadTimePerformanceEntry): Promise<PerformanceEntry> => {\n return new Promise((resolve, reject) => {\n let timeout: ReturnType<typeof setTimeout> | null = null;\n\n const observer = new PerformanceObserver((list) => {\n const entries = list.getEntriesByName(pltPerfEntry.name, pltPerfEntry.type);\n if (entries.length > 0) {\n observer.disconnect();\n if (timeout) {\n clearTimeout(timeout);\n timeout = null;\n }\n\n resolve(entries[entries.length - 1]);\n }\n });\n\n observer.observe({ entryTypes: [pltPerfEntry.type ?? defaultPltEntryType] });\n\n // Add a timeout to reject the promise if the entry is not found within a reasonable time\n timeout = setTimeout(() => {\n observer.disconnect();\n reject(new Error('Performance entry not found within the timeout period'));\n }, pltPerfEntry.timeout ?? defaultPltEntryTimeout);\n });\n};\n\nconst pageLoadTimePromise = new Promise<number>((resolve, reject) => {\n const pltPerfEntry = window.__cloudpack?.pageSessionContext?.pageLoadTimePerformanceEntry;\n if (pltPerfEntry) {\n // If the pageLoadTimePerformanceMark is set, wait for it and resolve the promise.\n // No need to observe the DOM changes.\n void waitForPerformancEntry(pltPerfEntry)\n .then((entry) => {\n resolve(entry.entryType == 'mark' ? entry.startTime : entry.duration);\n })\n .catch(reject);\n return;\n }\n\n const observer = new MutationObserver(domChanged);\n\n function reportLastAnimationTime(time: number) {\n // Stop observing the DOM\n observer.disconnect();\n\n // Resolve the promise with the page load time\n resolve(time);\n }\n\n const debouncedReportLastAnimationTime = debounce(reportLastAnimationTime, 2000 /* wait ms */);\n\n function domChanged(mutations: MutationRecord[]) {\n const shouldProcess = mutations.filter(hasMutationNonExcludedNodes).length > 0;\n\n if (shouldProcess) {\n requestAnimationFrame(() => {\n // Get the current time in ms since the time when navigation has started in window\n const time = performance.now();\n\n // Clear previous marks\n performance.clearMarks(pageLoadMarker);\n\n // Mark the page load time\n performance.mark(pageLoadMarker);\n\n // Report the page load time\n debouncedReportLastAnimationTime(time);\n });\n }\n }\n\n observer.observe(document.body, {\n childList: true,\n });\n});\n\nwindow.__cloudpack ??= {};\nwindow.__cloudpack.getPageLoadTime = () => pageLoadTimePromise;\n"]}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,KAAK,iBAAiB,EAAE,MAAM,sBAAsB,CAAC"}
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,eAAe,EAA0B,MAAM,sBAAsB,CAAC","sourcesContent":["export { getInlineScripts } from './getInlineScripts.js';\nexport { getInlineScript, type KnownInlineScript } from './getInlineScript.js';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readInlineScript.d.ts","sourceRoot":"","sources":["../src/readInlineScript.ts"],"names":[],"mappings":"AAEA,wBAAsB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAOxE"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import fsPromises from 'fs/promises';
|
|
2
|
+
export async function readInlineScript(fileName) {
|
|
3
|
+
let content = await fsPromises.readFile(fileName, 'utf-8');
|
|
4
|
+
// Remove source maps from inline scripts to avoid warnings in the browser.
|
|
5
|
+
content = content.replace('# sourceMappingURL=', ' ');
|
|
6
|
+
return content;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=readInlineScript.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readInlineScript.js","sourceRoot":"","sources":["../src/readInlineScript.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,aAAa,CAAC;AAErC,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,QAAgB;IACrD,IAAI,OAAO,GAAG,MAAM,UAAU,CAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAE3D,2EAA2E;IAC3E,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,qBAAqB,EAAE,GAAG,CAAC,CAAC;IAEtD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import fsPromises from 'fs/promises';\n\nexport async function readInlineScript(fileName: string): Promise<string> {\n let content = await fsPromises.readFile(fileName, 'utf-8');\n\n // Remove source maps from inline scripts to avoid warnings in the browser.\n content = content.replace('# sourceMappingURL=', ' ');\n\n return content;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registerDefineFlags.inline.d.ts","sourceRoot":"","sources":["../src/registerDefineFlags.inline.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAC;AAEtE,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,IAAI,CAqBpG"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function registerDefineFlags(target, defineFlags) {
|
|
2
|
+
for (const [key, value] of Object.entries(defineFlags ?? {})) {
|
|
3
|
+
const keys = key.split('.');
|
|
4
|
+
let current = target;
|
|
5
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
6
|
+
const k = keys[i];
|
|
7
|
+
// https://codeql.github.com/codeql-query-help/javascript/js-prototype-pollution-utility/
|
|
8
|
+
if (k === '__proto__' || k === 'constructor') {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
if (typeof current[k] !== 'object' || current[k] === null) {
|
|
12
|
+
current[k] = {};
|
|
13
|
+
}
|
|
14
|
+
current = current[k];
|
|
15
|
+
}
|
|
16
|
+
current[keys[keys.length - 1]] = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (typeof window !== 'undefined' && window.__cloudpack?.pageSessionContext) {
|
|
20
|
+
registerDefineFlags(window, window.__cloudpack.pageSessionContext.define ?? {});
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=registerDefineFlags.inline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registerDefineFlags.inline.js","sourceRoot":"","sources":["../src/registerDefineFlags.inline.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,mBAAmB,CAAC,MAA+B,EAAE,WAAyB;IAC5F,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,WAAW,IAAI,EAAE,CAAC,EAAE,CAAC;QAC7D,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC5B,IAAI,OAAO,GAAG,MAAM,CAAC;QAErB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YACzC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;YAClB,yFAAyF;YACzF,IAAI,CAAC,KAAK,WAAW,IAAI,CAAC,KAAK,aAAa,EAAE,CAAC;gBAC7C,SAAS;YACX,CAAC;YAED,IAAI,OAAO,OAAO,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;gBAC1D,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;YAClB,CAAC;YAED,OAAO,GAAG,OAAO,CAAC,CAAC,CAA4B,CAAC;QAClD,CAAC;QAED,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;IACzC,CAAC;AACH,CAAC;AAED,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,WAAW,EAAE,kBAAkB,EAAE,CAAC;IAC5E,mBAAmB,CAAC,MAA4C,EAAE,MAAM,CAAC,WAAW,CAAC,kBAAkB,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC;AACxH,CAAC","sourcesContent":["import type { DefineFlags } from '@ms-cloudpack/common-types-browser';\n\nexport function registerDefineFlags(target: Record<string, unknown>, defineFlags?: DefineFlags): void {\n for (const [key, value] of Object.entries(defineFlags ?? {})) {\n const keys = key.split('.');\n let current = target;\n\n for (let i = 0; i < keys.length - 1; i++) {\n const k = keys[i];\n // https://codeql.github.com/codeql-query-help/javascript/js-prototype-pollution-utility/\n if (k === '__proto__' || k === 'constructor') {\n continue;\n }\n\n if (typeof current[k] !== 'object' || current[k] === null) {\n current[k] = {};\n }\n\n current = current[k] as Record<string, unknown>;\n }\n\n current[keys[keys.length - 1]] = value;\n }\n}\n\nif (typeof window !== 'undefined' && window.__cloudpack?.pageSessionContext) {\n registerDefineFlags(window as unknown as Record<string, unknown>, window.__cloudpack.pageSessionContext.define ?? {});\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ms-cloudpack/inline-scripts",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A package that provides inline scripts that needs to be injected into the page for Cloudpack to work properly.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"source": "./src/index.ts",
|
|
12
|
+
"types": "./lib/index.d.ts",
|
|
13
|
+
"import": "./lib/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@ms-cloudpack/overlay": "^0.17.149",
|
|
18
|
+
"@ms-cloudpack/path-utilities": "^3.0.8"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@ms-cloudpack/common-types-browser": "^0.5.5",
|
|
22
|
+
"@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
|
|
23
|
+
"@ms-cloudpack/scripts": "^0.0.1"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"api": "cloudpack-scripts api",
|
|
27
|
+
"build:watch": "cloudpack-scripts build-watch",
|
|
28
|
+
"build": "cloudpack-scripts build",
|
|
29
|
+
"lint:update": "cloudpack-scripts lint-update",
|
|
30
|
+
"lint": "cloudpack-scripts lint"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"lib/**/!(*.test.*)"
|
|
34
|
+
]
|
|
35
|
+
}
|