piral-cli 0.15.0-beta.4466 → 0.15.0-beta.4544

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.
@@ -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
- meta: PiletMetadata;
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, basePath: string, metaFile: string) {
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.meta = meta;
55
- return JSON.stringify(meta);
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 piletApi: string;
78
+ private serverConfig: KrasConfiguration;
79
79
  private indexPath: string;
80
80
 
81
- constructor(options: PiletInjectorConfig, config: KrasConfiguration, core: EventEmitter) {
82
- this.config = options;
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
- // either take a full URI or make it an absolute path relative to the current origin
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] = (msg: string) => e.ws.send(msg);
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,13 @@ export default class PiletInjector implements KrasInjector {
103
102
 
104
103
  pilets.forEach((p, i) =>
105
104
  p.bundler.on(() => {
106
- const basePath = `${this.piletApi}/${i}/`;
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](meta);
108
+ const { baseUrl, notify } = cbs[id];
109
+ const basePath = this.getPiletApi(baseUrl);
110
+ const meta = JSON.stringify(p.getMeta(basePath));
111
+ notify(meta);
111
112
  }
112
113
  }),
113
114
  );
@@ -132,9 +133,23 @@ export default class PiletInjector implements KrasInjector {
132
133
 
133
134
  setOptions() {}
134
135
 
135
- async getMeta() {
136
+ getPiletApi(baseUrl: string) {
137
+ const { api } = this.config;
138
+
139
+ if (/^https?:/.test(api)) {
140
+ return api;
141
+ } else if (baseUrl) {
142
+ return `${baseUrl}${api}`;
143
+ } else {
144
+ const { ssl, port } = this.serverConfig;
145
+ const { host } = commonConfig;
146
+ return `${ssl ? 'https' : 'http'}://${host}:${port}${api}`;
147
+ }
148
+ }
149
+
150
+ async getMeta(baseUrl: string) {
136
151
  const { pilets, feed } = this.config;
137
- const localPilets = pilets.map((pilet) => pilet.meta).filter(Boolean);
152
+ const localPilets = pilets.map((pilet) => pilet.getMeta?.(baseUrl)).filter(Boolean);
138
153
  const mergedPilets = this.mergePilets(localPilets, await this.loadRemoteFeed(feed));
139
154
  return JSON.stringify(mergedPilets);
140
155
  }
@@ -187,7 +202,7 @@ export default class PiletInjector implements KrasInjector {
187
202
  return this.sendContent(content, type, url);
188
203
  }
189
204
 
190
- async sendResponse(path: string, url: string): Promise<KrasResult> {
205
+ async sendResponse(path: string, url: string, baseUrl: string): Promise<KrasResult> {
191
206
  const { pilets } = this.config;
192
207
  const [index, ...rest] = path.split('/');
193
208
  const pilet = pilets[+index];
@@ -195,7 +210,7 @@ export default class PiletInjector implements KrasInjector {
195
210
 
196
211
  if (!path) {
197
212
  await bundler?.ready();
198
- const content = await this.getMeta();
213
+ const content = await this.getMeta(baseUrl);
199
214
  return this.sendContent(content, 'application/json', url);
200
215
  } else {
201
216
  return bundler?.ready().then(() => {
@@ -208,11 +223,11 @@ export default class PiletInjector implements KrasInjector {
208
223
  }
209
224
  }
210
225
 
211
- sendIndexFile(target: string, url: string): KrasResponse {
226
+ sendIndexFile(target: string, url: string, baseUrl: string): KrasResponse {
212
227
  const indexHtml = readFileSync(target, 'utf8');
213
228
 
214
229
  // mechanism to inject server side debug piletApi config into piral emulator
215
- const windowInjectionScript = `window['dbg:pilet-api'] = '${this.piletApi}';`;
230
+ const windowInjectionScript = `window['dbg:pilet-api'] = '${this.getPiletApi(baseUrl)}';`;
216
231
  const findStr = `<script`;
217
232
  const replaceStr = `<script>/* Pilet Debugging Emulator Config Injection */${windowInjectionScript}</script><script`;
218
233
  const content = indexHtml.replace(`${findStr}`, `${replaceStr}`);
@@ -222,6 +237,7 @@ export default class PiletInjector implements KrasInjector {
222
237
 
223
238
  handle(req: KrasRequest): KrasResponse {
224
239
  const { app, api, publicUrl } = this.config;
240
+ const baseUrl = req.headers.host ? `${req.encrypted ? 'https' : 'http'}://${req.headers.host}` : undefined;
225
241
 
226
242
  if (!req.target) {
227
243
  if (req.url.startsWith(publicUrl)) {
@@ -230,7 +246,7 @@ export default class PiletInjector implements KrasInjector {
230
246
 
231
247
  if (existsSync(target) && statSync(target).isFile()) {
232
248
  if (req.url === this.indexPath) {
233
- return this.sendIndexFile(target, req.url);
249
+ return this.sendIndexFile(target, req.url, baseUrl);
234
250
  } else {
235
251
  return this.sendFile(target, req.url);
236
252
  }
@@ -245,7 +261,7 @@ export default class PiletInjector implements KrasInjector {
245
261
  return undefined;
246
262
  } else if (req.target === api) {
247
263
  const path = req.url.substring(1).split('?')[0];
248
- return this.sendResponse(path, req.url);
264
+ return this.sendResponse(path, req.url, baseUrl);
249
265
  }
250
266
  }
251
267
  }