piral-cli 0.15.0-beta.4472 → 0.15.0-beta.4549
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/lib/apps/new-piral.js +1 -2
- package/lib/apps/new-piral.js.map +1 -1
- package/lib/common/emulator.js +7 -0
- package/lib/common/emulator.js.map +1 -1
- package/lib/common/importmap.d.ts +1 -1
- package/lib/common/importmap.js +17 -2
- package/lib/common/importmap.js.map +1 -1
- package/lib/common/package.d.ts +7 -2
- package/lib/common/package.js +85 -48
- package/lib/common/package.js.map +1 -1
- package/lib/injectors/pilet-injector.d.ts +8 -6
- package/lib/injectors/pilet-injector.js +46 -25
- package/lib/injectors/pilet-injector.js.map +1 -1
- package/package.json +2 -2
- package/src/apps/new-piral.ts +3 -11
- package/src/common/emulator.ts +8 -0
- package/src/common/importmap.ts +20 -2
- package/src/common/package.test.ts +38 -5
- package/src/common/package.ts +71 -23
- package/src/injectors/pilet-injector.test.ts +3 -9
- package/src/injectors/pilet-injector.ts +59 -38
|
@@ -5,16 +5,14 @@ import { readFileSync, existsSync, statSync } from 'fs';
|
|
|
5
5
|
import { KrasInjector, KrasResponse, KrasRequest, KrasInjectorConfig, KrasConfiguration, KrasResult } from 'kras';
|
|
6
6
|
import { log } from '../common/log';
|
|
7
7
|
import { getPiletSpecMeta } from '../common/spec';
|
|
8
|
-
import { config } from '../common/config';
|
|
8
|
+
import { config as commonConfig } from '../common/config';
|
|
9
9
|
import { axios, mime, jju } from '../external';
|
|
10
10
|
import { Bundler } from '../types';
|
|
11
11
|
|
|
12
|
-
const { host } = config;
|
|
13
|
-
|
|
14
12
|
interface Pilet {
|
|
15
13
|
bundler: Bundler;
|
|
16
14
|
root: string;
|
|
17
|
-
|
|
15
|
+
getMeta(basePath: string): PiletMetadata;
|
|
18
16
|
}
|
|
19
17
|
|
|
20
18
|
export interface PiletInjectorConfig extends KrasInjectorConfig {
|
|
@@ -32,7 +30,7 @@ interface PiletMetadata {
|
|
|
32
30
|
[key: string]: unknown;
|
|
33
31
|
}
|
|
34
32
|
|
|
35
|
-
function fillPiletMeta(pilet: Pilet,
|
|
33
|
+
function fillPiletMeta(pilet: Pilet, metaFile: string, subPath: string) {
|
|
36
34
|
const { root, bundler } = pilet;
|
|
37
35
|
const metaPath = join(root, metaFile);
|
|
38
36
|
const packagePath = join(root, 'package.json');
|
|
@@ -40,19 +38,21 @@ function fillPiletMeta(pilet: Pilet, basePath: string, metaFile: string) {
|
|
|
40
38
|
const metaOverride = existsSync(metaPath) ? jju.parse(readFileSync(metaPath, 'utf8')) : undefined;
|
|
41
39
|
const file = bundler.bundle.name.replace(/^[\/\\]/, '');
|
|
42
40
|
const target = join(bundler.bundle.dir, file);
|
|
43
|
-
const url = new URL(file, basePath);
|
|
44
|
-
const meta = {
|
|
45
|
-
custom: def.custom,
|
|
46
|
-
config: def.piletConfig,
|
|
47
|
-
...metaOverride,
|
|
48
|
-
name: def.name,
|
|
49
|
-
version: def.version,
|
|
50
|
-
link: `${url.href}?updated=${Date.now()}`,
|
|
51
|
-
...getPiletSpecMeta(target, basePath),
|
|
52
|
-
};
|
|
53
41
|
|
|
54
|
-
pilet.
|
|
55
|
-
|
|
42
|
+
pilet.getMeta = (parentPath) => {
|
|
43
|
+
const basePath = `${parentPath}${subPath}`;
|
|
44
|
+
const url = new URL(file, basePath);
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
custom: def.custom,
|
|
48
|
+
config: def.piletConfig,
|
|
49
|
+
...metaOverride,
|
|
50
|
+
name: def.name,
|
|
51
|
+
version: def.version,
|
|
52
|
+
link: `${url.href}?updated=${Date.now()}`,
|
|
53
|
+
...getPiletSpecMeta(target, basePath),
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
async function loadFeed(feed: string) {
|
|
@@ -75,25 +75,24 @@ async function loadFeed(feed: string) {
|
|
|
75
75
|
|
|
76
76
|
export default class PiletInjector implements KrasInjector {
|
|
77
77
|
public config: PiletInjectorConfig;
|
|
78
|
-
private
|
|
78
|
+
private serverConfig: KrasConfiguration;
|
|
79
79
|
private indexPath: string;
|
|
80
80
|
|
|
81
|
-
constructor(
|
|
82
|
-
this.config =
|
|
81
|
+
constructor(config: PiletInjectorConfig, serverConfig: KrasConfiguration, core: EventEmitter) {
|
|
82
|
+
this.config = config;
|
|
83
|
+
this.serverConfig = serverConfig;
|
|
83
84
|
|
|
84
85
|
if (this.config.active) {
|
|
85
|
-
|
|
86
|
-
this.piletApi = /^https?:/.test(options.api)
|
|
87
|
-
? options.api
|
|
88
|
-
: `${config.ssl ? 'https' : 'http'}://${host}:${config.port}${options.api}`;
|
|
89
|
-
|
|
90
|
-
const { pilets, api, publicUrl } = options;
|
|
86
|
+
const { pilets, api, publicUrl } = config;
|
|
91
87
|
this.indexPath = `${publicUrl}index.html`;
|
|
92
88
|
const cbs = {};
|
|
93
89
|
|
|
94
90
|
core.on('user-connected', (e) => {
|
|
95
91
|
if (e.target === '*' && e.url === api.substring(1)) {
|
|
96
|
-
cbs[e.id] =
|
|
92
|
+
cbs[e.id] = {
|
|
93
|
+
baseUrl: e.req.headers.origin,
|
|
94
|
+
notify: (msg: string) => e.ws.send(msg),
|
|
95
|
+
};
|
|
97
96
|
}
|
|
98
97
|
});
|
|
99
98
|
|
|
@@ -103,11 +102,12 @@ export default class PiletInjector implements KrasInjector {
|
|
|
103
102
|
|
|
104
103
|
pilets.forEach((p, i) =>
|
|
105
104
|
p.bundler.on(() => {
|
|
106
|
-
|
|
107
|
-
const meta = fillPiletMeta(p, basePath, options.meta);
|
|
105
|
+
fillPiletMeta(p, config.meta, `/${i}/`);
|
|
108
106
|
|
|
109
107
|
for (const id of Object.keys(cbs)) {
|
|
110
|
-
cbs[id]
|
|
108
|
+
const { baseUrl, notify } = cbs[id];
|
|
109
|
+
const meta = this.getPiletMeta(baseUrl, p);
|
|
110
|
+
notify(meta);
|
|
111
111
|
}
|
|
112
112
|
}),
|
|
113
113
|
);
|
|
@@ -132,9 +132,29 @@ export default class PiletInjector implements KrasInjector {
|
|
|
132
132
|
|
|
133
133
|
setOptions() {}
|
|
134
134
|
|
|
135
|
-
|
|
135
|
+
getPiletApi(baseUrl: string) {
|
|
136
|
+
const { api } = this.config;
|
|
137
|
+
|
|
138
|
+
if (/^https?:/.test(api)) {
|
|
139
|
+
return api;
|
|
140
|
+
} else if (baseUrl) {
|
|
141
|
+
return `${baseUrl}${api}`;
|
|
142
|
+
} else {
|
|
143
|
+
const { ssl, port } = this.serverConfig;
|
|
144
|
+
const { host } = commonConfig;
|
|
145
|
+
return `${ssl ? 'https' : 'http'}://${host}:${port}${api}`;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getPiletMeta(baseUrl: string, pilet: Pilet) {
|
|
150
|
+
const basePath = this.getPiletApi(baseUrl);
|
|
151
|
+
return JSON.stringify(pilet.getMeta(basePath));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async getIndexMeta(baseUrl: string) {
|
|
136
155
|
const { pilets, feed } = this.config;
|
|
137
|
-
const
|
|
156
|
+
const basePath = this.getPiletApi(baseUrl);
|
|
157
|
+
const localPilets = pilets.map((pilet) => pilet.getMeta?.(basePath)).filter(Boolean);
|
|
138
158
|
const mergedPilets = this.mergePilets(localPilets, await this.loadRemoteFeed(feed));
|
|
139
159
|
return JSON.stringify(mergedPilets);
|
|
140
160
|
}
|
|
@@ -187,7 +207,7 @@ export default class PiletInjector implements KrasInjector {
|
|
|
187
207
|
return this.sendContent(content, type, url);
|
|
188
208
|
}
|
|
189
209
|
|
|
190
|
-
async sendResponse(path: string, url: string): Promise<KrasResult> {
|
|
210
|
+
async sendResponse(path: string, url: string, baseUrl: string): Promise<KrasResult> {
|
|
191
211
|
const { pilets } = this.config;
|
|
192
212
|
const [index, ...rest] = path.split('/');
|
|
193
213
|
const pilet = pilets[+index];
|
|
@@ -195,7 +215,7 @@ export default class PiletInjector implements KrasInjector {
|
|
|
195
215
|
|
|
196
216
|
if (!path) {
|
|
197
217
|
await bundler?.ready();
|
|
198
|
-
const content = await this.
|
|
218
|
+
const content = await this.getIndexMeta(baseUrl);
|
|
199
219
|
return this.sendContent(content, 'application/json', url);
|
|
200
220
|
} else {
|
|
201
221
|
return bundler?.ready().then(() => {
|
|
@@ -208,11 +228,11 @@ export default class PiletInjector implements KrasInjector {
|
|
|
208
228
|
}
|
|
209
229
|
}
|
|
210
230
|
|
|
211
|
-
sendIndexFile(target: string, url: string): KrasResponse {
|
|
231
|
+
sendIndexFile(target: string, url: string, baseUrl: string): KrasResponse {
|
|
212
232
|
const indexHtml = readFileSync(target, 'utf8');
|
|
213
233
|
|
|
214
234
|
// mechanism to inject server side debug piletApi config into piral emulator
|
|
215
|
-
const windowInjectionScript = `window['dbg:pilet-api'] = '${this.
|
|
235
|
+
const windowInjectionScript = `window['dbg:pilet-api'] = '${this.getPiletApi(baseUrl)}';`;
|
|
216
236
|
const findStr = `<script`;
|
|
217
237
|
const replaceStr = `<script>/* Pilet Debugging Emulator Config Injection */${windowInjectionScript}</script><script`;
|
|
218
238
|
const content = indexHtml.replace(`${findStr}`, `${replaceStr}`);
|
|
@@ -222,6 +242,7 @@ export default class PiletInjector implements KrasInjector {
|
|
|
222
242
|
|
|
223
243
|
handle(req: KrasRequest): KrasResponse {
|
|
224
244
|
const { app, api, publicUrl } = this.config;
|
|
245
|
+
const baseUrl = req.headers.host ? `${req.encrypted ? 'https' : 'http'}://${req.headers.host}` : undefined;
|
|
225
246
|
|
|
226
247
|
if (!req.target) {
|
|
227
248
|
if (req.url.startsWith(publicUrl)) {
|
|
@@ -230,7 +251,7 @@ export default class PiletInjector implements KrasInjector {
|
|
|
230
251
|
|
|
231
252
|
if (existsSync(target) && statSync(target).isFile()) {
|
|
232
253
|
if (req.url === this.indexPath) {
|
|
233
|
-
return this.sendIndexFile(target, req.url);
|
|
254
|
+
return this.sendIndexFile(target, req.url, baseUrl);
|
|
234
255
|
} else {
|
|
235
256
|
return this.sendFile(target, req.url);
|
|
236
257
|
}
|
|
@@ -245,7 +266,7 @@ export default class PiletInjector implements KrasInjector {
|
|
|
245
266
|
return undefined;
|
|
246
267
|
} else if (req.target === api) {
|
|
247
268
|
const path = req.url.substring(1).split('?')[0];
|
|
248
|
-
return this.sendResponse(path, req.url);
|
|
269
|
+
return this.sendResponse(path, req.url, baseUrl);
|
|
249
270
|
}
|
|
250
271
|
}
|
|
251
272
|
}
|