@podium/client 5.0.0-next.9 → 5.0.0
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/CHANGELOG.md +245 -0
- package/README.md +31 -9
- package/client.d.ts +140 -0
- package/lib/http-outgoing.js +16 -0
- package/lib/http.js +23 -0
- package/lib/resolver.content.js +213 -203
- package/lib/resolver.fallback.js +90 -100
- package/lib/resolver.js +5 -3
- package/lib/resolver.manifest.js +164 -137
- package/lib/resource.js +9 -16
- package/lib/response.js +5 -9
- package/lib/utils.js +40 -0
- package/package.json +23 -20
- package/index.d.ts +0 -63
package/lib/resource.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-param-reassign */
|
|
2
2
|
|
|
3
3
|
import Metrics from '@metrics/client';
|
|
4
|
-
import stream from 'stream';
|
|
5
4
|
import abslog from 'abslog';
|
|
6
5
|
import assert from 'assert';
|
|
7
6
|
|
|
@@ -63,22 +62,16 @@ export default class PodiumClientResource {
|
|
|
63
62
|
|
|
64
63
|
this.#state.setInitializingState();
|
|
65
64
|
|
|
66
|
-
const
|
|
67
|
-
const to = new stream.Writable({
|
|
68
|
-
write: (chunk, encoding, next) => {
|
|
69
|
-
chunks.push(chunk);
|
|
70
|
-
next();
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
stream.pipeline([outgoing, to], () => {
|
|
75
|
-
// noop
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
const { manifest, headers, redirect } = await this.#resolver.resolve(
|
|
65
|
+
const { manifest, headers, redirect, isFallback } = await this.#resolver.resolve(
|
|
79
66
|
outgoing,
|
|
80
67
|
);
|
|
81
68
|
|
|
69
|
+
const chunks = [];
|
|
70
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
71
|
+
for await (const chunk of outgoing) {
|
|
72
|
+
chunks.push(chunk)
|
|
73
|
+
}
|
|
74
|
+
|
|
82
75
|
const content = !outgoing.redirect
|
|
83
76
|
? Buffer.concat(chunks).toString()
|
|
84
77
|
: '';
|
|
@@ -86,8 +79,8 @@ export default class PodiumClientResource {
|
|
|
86
79
|
return new Response({
|
|
87
80
|
headers,
|
|
88
81
|
content,
|
|
89
|
-
css: manifest.css,
|
|
90
|
-
js: manifest.js,
|
|
82
|
+
css: utils.filterAssets(isFallback ? "fallback" : "content", manifest.css),
|
|
83
|
+
js: utils.filterAssets(isFallback ? "fallback" : "content", manifest.js),
|
|
91
84
|
redirect,
|
|
92
85
|
});
|
|
93
86
|
}
|
package/lib/response.js
CHANGED
|
@@ -60,15 +60,11 @@ export default class PodiumClientResponse {
|
|
|
60
60
|
|
|
61
61
|
[inspect]() {
|
|
62
62
|
return {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
async: this.async,
|
|
69
|
-
defer: this.defer,
|
|
70
|
-
type: this.type,
|
|
71
|
-
data: this.data,
|
|
63
|
+
redirect: this.redirect,
|
|
64
|
+
content: this.content,
|
|
65
|
+
headers: this.headers,
|
|
66
|
+
css: this.css,
|
|
67
|
+
js: this.js,
|
|
72
68
|
};
|
|
73
69
|
}
|
|
74
70
|
|
package/lib/utils.js
CHANGED
|
@@ -32,4 +32,44 @@ export const hasManifestChange = item => {
|
|
|
32
32
|
return oldVersion !== newVersion;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Check if a value is a HttpIncoming object or not. If not, it
|
|
38
|
+
* assume the incoming value is a context
|
|
39
|
+
*
|
|
40
|
+
* @param {Object} incoming A object
|
|
41
|
+
*
|
|
42
|
+
* @returns {HttpIncoming}
|
|
43
|
+
*/
|
|
35
44
|
export const validateIncoming = (incoming = {}) => (Object.prototype.toString.call(incoming) === '[object PodiumHttpIncoming]');
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {import("@podium/utils").AssetCss | import("@podium/utils").AssetJs} Asset
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Filter assets array based on scope.
|
|
52
|
+
* If scope property is not present, asset will be included (backwards compatibility)
|
|
53
|
+
* If scope property is set to "all", asset will be included.
|
|
54
|
+
* If scope is set to "content" and asset scope property is set to "fallback", asset will not be included
|
|
55
|
+
* If scope is set to "fallback" and asset scope property is set to "content", asset will not be included
|
|
56
|
+
* @param {"content" | "fallback" | "all"} scope
|
|
57
|
+
* @param {Asset[]} assets
|
|
58
|
+
* @returns {Asset[]}
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```
|
|
62
|
+
* // plain objects work
|
|
63
|
+
* const assets = filterAssets("content", [{..., scope: "content"}, {..., scope: "fallback"}]);
|
|
64
|
+
* // as do AssetJs and AssetCSS objects
|
|
65
|
+
* const assets = filterAssets("content", [new AssetCss(), new AssetCss()]);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export const filterAssets = (scope, assets) => {
|
|
69
|
+
// if undefined or null, passthrough
|
|
70
|
+
if (!assets) return assets;
|
|
71
|
+
// if a non array value is given, throw
|
|
72
|
+
if (!Array.isArray(assets)) throw new TypeError(`Asset definition must be of type array. Got ${typeof assets}`);
|
|
73
|
+
// filter the array of asset definitions to matchin scope or anything with all. Treat no scope the same as "all" for backwards compatibility.
|
|
74
|
+
return assets.filter(asset => !asset.scope || asset.scope === scope || asset.scope === "all");
|
|
75
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@podium/client",
|
|
3
|
-
"version": "5.0.0
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"files": [
|
|
22
22
|
"package.json",
|
|
23
23
|
"CHANGELOG.md",
|
|
24
|
-
"
|
|
24
|
+
"client.d.ts",
|
|
25
25
|
"README.md",
|
|
26
26
|
"LICENSE",
|
|
27
27
|
"dist",
|
|
@@ -32,37 +32,40 @@
|
|
|
32
32
|
"scripts": {
|
|
33
33
|
"lint": "eslint .",
|
|
34
34
|
"lint:fix": "eslint --fix .",
|
|
35
|
-
"test": "tap --
|
|
35
|
+
"test": "tap --disable-coverage --allow-empty-coverage"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@hapi/boom": "^10.0.0",
|
|
39
|
-
"@metrics/client": "2.5.
|
|
40
|
-
"@podium/schemas": "5.0.0
|
|
41
|
-
"@podium/utils": "5.0.0
|
|
39
|
+
"@metrics/client": "2.5.2",
|
|
40
|
+
"@podium/schemas": "5.0.0",
|
|
41
|
+
"@podium/utils": "5.0.0",
|
|
42
42
|
"abslog": "2.4.0",
|
|
43
43
|
"http-cache-semantics": "^4.0.3",
|
|
44
44
|
"lodash.clonedeep": "^4.5.0",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
45
|
+
"ttl-mem-cache": "4.1.0",
|
|
46
|
+
"undici": "^5.10.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@podium/test-utils": "2.5.2",
|
|
50
|
-
"@semantic-release/changelog": "6.0.
|
|
50
|
+
"@semantic-release/changelog": "6.0.3",
|
|
51
51
|
"@semantic-release/git": "10.0.1",
|
|
52
|
-
"@
|
|
53
|
-
"@
|
|
52
|
+
"@babel/eslint-parser": "7.23.3",
|
|
53
|
+
"@semantic-release/github": "8.1.0",
|
|
54
|
+
"@semantic-release/npm": "8.0.3",
|
|
55
|
+
"@semantic-release/release-notes-generator": "10.0.3",
|
|
56
|
+
"@sinonjs/fake-timers": "11.2.2",
|
|
54
57
|
"benchmark": "2.1.4",
|
|
55
|
-
"eslint": "8.
|
|
58
|
+
"eslint": "8.54.0",
|
|
56
59
|
"eslint-config-airbnb-base": "15.0.0",
|
|
57
|
-
"eslint-config-prettier": "
|
|
58
|
-
"eslint-plugin-import": "2.
|
|
59
|
-
"eslint-plugin-prettier": "
|
|
60
|
-
"express": "4.18.
|
|
61
|
-
"get-stream": "
|
|
60
|
+
"eslint-config-prettier": "9.0.0",
|
|
61
|
+
"eslint-plugin-import": "2.29.0",
|
|
62
|
+
"eslint-plugin-prettier": "5.0.1",
|
|
63
|
+
"express": "4.18.2",
|
|
64
|
+
"get-stream": "8.0.1",
|
|
62
65
|
"http-proxy": "1.18.1",
|
|
63
66
|
"is-stream": "3.0.0",
|
|
64
|
-
"prettier": "
|
|
65
|
-
"semantic-release": "
|
|
66
|
-
"tap": "
|
|
67
|
+
"prettier": "3.1.0",
|
|
68
|
+
"semantic-release": "22.0.8",
|
|
69
|
+
"tap": "18.6.1"
|
|
67
70
|
}
|
|
68
71
|
}
|
package/index.d.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { HttpIncoming, AssetJs, AssetCss } from '@podium/utils';
|
|
2
|
-
import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
|
|
3
|
-
|
|
4
|
-
declare interface PodiumClientResourceOptions {
|
|
5
|
-
pathname?: string;
|
|
6
|
-
headers?: OutgoingHttpHeaders;
|
|
7
|
-
query?: any;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
declare interface PodiumClientResponse {
|
|
11
|
-
readonly redirect: PodiumRedirect;
|
|
12
|
-
readonly content: string;
|
|
13
|
-
readonly headers: IncomingHttpHeaders;
|
|
14
|
-
readonly js: Array<AssetJs>;
|
|
15
|
-
readonly css: Array<AssetCss>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
declare class PodiumClientResource {
|
|
19
|
-
readonly name: string;
|
|
20
|
-
|
|
21
|
-
readonly uri: string;
|
|
22
|
-
|
|
23
|
-
fetch(
|
|
24
|
-
incoming: HttpIncoming,
|
|
25
|
-
options?: PodiumClientResourceOptions,
|
|
26
|
-
): Promise<PodiumClientResponse>;
|
|
27
|
-
|
|
28
|
-
stream(
|
|
29
|
-
incoming: HttpIncoming,
|
|
30
|
-
options?: PodiumClientResourceOptions,
|
|
31
|
-
): ReadableStream<PodiumClientResponse>;
|
|
32
|
-
|
|
33
|
-
refresh(): Promise<boolean>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
declare interface RegisterOptions {
|
|
37
|
-
uri: string;
|
|
38
|
-
name: string;
|
|
39
|
-
retries?: number;
|
|
40
|
-
timeout?: number;
|
|
41
|
-
throwable?: boolean;
|
|
42
|
-
redirectable?: boolean;
|
|
43
|
-
resolveJs?: boolean;
|
|
44
|
-
resolveCss?: boolean;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
declare interface PodiumRedirect {
|
|
48
|
-
readonly statusCode: number;
|
|
49
|
-
readonly location: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export default class PodiumClient {
|
|
53
|
-
readonly state:
|
|
54
|
-
| 'instantiated'
|
|
55
|
-
| 'initializing'
|
|
56
|
-
| 'unstable'
|
|
57
|
-
| 'stable'
|
|
58
|
-
| 'unhealthy';
|
|
59
|
-
|
|
60
|
-
register(options: RegisterOptions): PodiumClientResource;
|
|
61
|
-
|
|
62
|
-
refreshManifests(): void;
|
|
63
|
-
}
|