piral-cli 1.4.0-beta.6243 → 1.4.0-beta.6250
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/add-piral-instance-pilet.js +1 -6
- package/lib/apps/add-piral-instance-pilet.js.map +1 -1
- package/lib/apps/new-pilet.js +4 -2
- package/lib/apps/new-pilet.js.map +1 -1
- package/lib/apps/run-emulator-piral.js +4 -2
- package/lib/apps/run-emulator-piral.js.map +1 -1
- package/lib/apps/upgrade-pilet.js +3 -2
- package/lib/apps/upgrade-pilet.js.map +1 -1
- package/lib/build/bundler-calls.d.ts +2 -2
- package/lib/build/bundler-calls.js +4 -4
- package/lib/build/bundler-calls.js.map +1 -1
- package/lib/bundler.js +5 -5
- package/lib/bundler.js.map +1 -1
- package/lib/common/emulator.js +5 -4
- package/lib/common/emulator.js.map +1 -1
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.js +1 -0
- package/lib/common/index.js.map +1 -1
- package/lib/common/npm.d.ts +1 -0
- package/lib/common/npm.js +16 -4
- package/lib/common/npm.js.map +1 -1
- package/lib/common/package.d.ts +6 -4
- package/lib/common/package.js +42 -42
- package/lib/common/package.js.map +1 -1
- package/lib/common/shell.d.ts +1 -1
- package/lib/common/shell.js +48 -8
- package/lib/common/shell.js.map +1 -1
- package/lib/common/website.d.ts +1 -0
- package/lib/common/website.js +72 -0
- package/lib/common/website.js.map +1 -0
- package/lib/injectors/pilet-injector.d.ts +12 -10
- package/lib/injectors/pilet-injector.js +103 -60
- package/lib/injectors/pilet-injector.js.map +1 -1
- package/lib/injectors/piral-injector.d.ts +5 -5
- package/lib/injectors/piral-injector.js +64 -34
- package/lib/injectors/piral-injector.js.map +1 -1
- package/lib/types/common.d.ts +22 -0
- package/lib/types/common.js.map +1 -1
- package/lib/types/internal.d.ts +7 -4
- package/lib/types/public.d.ts +2 -1
- package/package.json +2 -2
- package/src/apps/add-piral-instance-pilet.ts +1 -15
- package/src/apps/new-pilet.ts +4 -9
- package/src/apps/run-emulator-piral.ts +4 -2
- package/src/apps/upgrade-pilet.ts +3 -2
- package/src/build/bundler-calls.ts +4 -4
- package/src/bundler.test.ts +5 -5
- package/src/bundler.ts +5 -5
- package/src/common/emulator.ts +28 -32
- package/src/common/index.ts +1 -0
- package/src/common/npm.ts +16 -3
- package/src/common/package.ts +37 -66
- package/src/common/shell.ts +62 -20
- package/src/common/website.ts +69 -0
- package/src/injectors/pilet-injector.test.ts +4 -4
- package/src/injectors/pilet-injector.ts +87 -41
- package/src/injectors/piral-injector.test.ts +2 -2
- package/src/injectors/piral-injector.ts +31 -20
- package/src/types/common.ts +24 -0
- package/src/types/internal.ts +8 -7
- package/src/types/public.ts +2 -1
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { join, resolve } from 'path';
|
|
2
|
+
import { createPiralStubIndexIfNotExists } from './template';
|
|
3
|
+
import { packageJson } from './constants';
|
|
4
|
+
import { ForceOverwrite } from './enums';
|
|
5
|
+
import { createDirectory, writeBinary } from './io';
|
|
6
|
+
import { writeJson } from './io';
|
|
7
|
+
import { progress } from './log';
|
|
8
|
+
import { axios } from '../external';
|
|
9
|
+
import { EmulatorWebsiteManifestFiles, EmulatorWebsiteManifest } from '../types';
|
|
10
|
+
|
|
11
|
+
async function downloadEmulatorFiles(manifestUrl: string, target: string, files: EmulatorWebsiteManifestFiles) {
|
|
12
|
+
const requiredFiles = [files.typings, files.main, files.app];
|
|
13
|
+
|
|
14
|
+
await Promise.all(
|
|
15
|
+
requiredFiles.map(async (file) => {
|
|
16
|
+
const url = new URL(file, manifestUrl);
|
|
17
|
+
const res = await axios.default.get(url.href, { responseType: 'arraybuffer' });
|
|
18
|
+
const data: Buffer = res.data;
|
|
19
|
+
await writeBinary(target, file, data);
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function scaffoldFromEmulatorWebsite(rootDir: string, manifestUrl: string) {
|
|
25
|
+
progress(`Downloading emulator from %s ...`, manifestUrl);
|
|
26
|
+
const response = await axios.default.get(manifestUrl);
|
|
27
|
+
const emulatorJson: EmulatorWebsiteManifest = response.data;
|
|
28
|
+
|
|
29
|
+
const targetDir = resolve(rootDir, 'node_modules', emulatorJson.name);
|
|
30
|
+
const appDirName = 'app';
|
|
31
|
+
const mainFile = 'index.js';
|
|
32
|
+
const appDir = resolve(targetDir, appDirName);
|
|
33
|
+
await createDirectory(appDir);
|
|
34
|
+
|
|
35
|
+
await writeJson(
|
|
36
|
+
targetDir,
|
|
37
|
+
packageJson,
|
|
38
|
+
{
|
|
39
|
+
name: emulatorJson.name,
|
|
40
|
+
description: emulatorJson.description,
|
|
41
|
+
version: emulatorJson.version,
|
|
42
|
+
importmap: emulatorJson.importmap,
|
|
43
|
+
pilets: emulatorJson.scaffolding.pilets,
|
|
44
|
+
piralCLI: {
|
|
45
|
+
version: emulatorJson.scaffolding.cli,
|
|
46
|
+
timestamp: emulatorJson.timestamp,
|
|
47
|
+
source: manifestUrl,
|
|
48
|
+
generated: true,
|
|
49
|
+
},
|
|
50
|
+
files: emulatorJson.files.assets,
|
|
51
|
+
main: `./${join(appDirName, mainFile)}`,
|
|
52
|
+
typings: `./${join(appDirName, emulatorJson.files.typings)}`,
|
|
53
|
+
app: `./${join(appDirName, emulatorJson.files.app)}`,
|
|
54
|
+
peerDependencies: {},
|
|
55
|
+
optionalDependencies: emulatorJson.dependencies.optional,
|
|
56
|
+
devDependencies: emulatorJson.dependencies.included,
|
|
57
|
+
},
|
|
58
|
+
true,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// actually including this one hints that the app shell should have been included - which is forbidden
|
|
62
|
+
await createPiralStubIndexIfNotExists(appDir, mainFile, ForceOverwrite.yes, {
|
|
63
|
+
name: emulatorJson.name,
|
|
64
|
+
outFile: emulatorJson.files.main,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
await downloadEmulatorFiles(manifestUrl, appDir, emulatorJson.files);
|
|
68
|
+
return emulatorJson.name;
|
|
69
|
+
}
|
|
@@ -40,7 +40,7 @@ describe('Piral-CLI pilet injector', () => {
|
|
|
40
40
|
expect(injector.active).toBeFalsy();
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
it('PiletInjector does not send empty content', () => {
|
|
43
|
+
it('PiletInjector does not send empty content', async () => {
|
|
44
44
|
// Arrange
|
|
45
45
|
const core = new EventEmitter();
|
|
46
46
|
const injector = new PiletInjector(optionsMock, configMock, core);
|
|
@@ -51,7 +51,7 @@ describe('Piral-CLI pilet injector', () => {
|
|
|
51
51
|
|
|
52
52
|
// Act
|
|
53
53
|
try {
|
|
54
|
-
injector.sendFile(target, url); // this file does not exist
|
|
54
|
+
await injector.sendFile(target, url); // this file does not exist
|
|
55
55
|
} catch {
|
|
56
56
|
hasFailed = true;
|
|
57
57
|
}
|
|
@@ -113,7 +113,7 @@ describe('Piral-CLI pilet injector', () => {
|
|
|
113
113
|
expect(res).toBeUndefined();
|
|
114
114
|
});
|
|
115
115
|
|
|
116
|
-
it('PiletInjector wont crash on request with no target', () => {
|
|
116
|
+
it('PiletInjector wont crash on request with no target', async () => {
|
|
117
117
|
// Arrange
|
|
118
118
|
const optionsMock = {
|
|
119
119
|
pilets: [],
|
|
@@ -136,7 +136,7 @@ describe('Piral-CLI pilet injector', () => {
|
|
|
136
136
|
} as KrasRequest;
|
|
137
137
|
|
|
138
138
|
// Act
|
|
139
|
-
const res = injector.handle(request);
|
|
139
|
+
const res = await injector.handle(request);
|
|
140
140
|
|
|
141
141
|
// Assert
|
|
142
142
|
expect(res).toBeUndefined();
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { URL } from 'url';
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
|
-
import {
|
|
5
|
-
import { KrasInjector,
|
|
4
|
+
import { readFile, stat, writeFile } from 'fs/promises';
|
|
5
|
+
import { KrasInjector, KrasRequest, KrasInjectorConfig, KrasConfiguration, KrasResult } from 'kras';
|
|
6
6
|
import { log } from '../common/log';
|
|
7
7
|
import { getPiletSpecMeta } from '../common/spec';
|
|
8
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
|
-
interface Pilet {
|
|
13
|
-
bundler: Bundler;
|
|
14
|
-
root: string;
|
|
15
|
-
getMeta(basePath: string): PiletMetadata;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
12
|
export interface PiletInjectorConfig extends KrasInjectorConfig {
|
|
19
13
|
/**
|
|
20
14
|
* The pilets to serve.
|
|
@@ -54,31 +48,43 @@ export interface PiletInjectorConfig extends KrasInjectorConfig {
|
|
|
54
48
|
headers?: Record<string, string>;
|
|
55
49
|
}
|
|
56
50
|
|
|
51
|
+
interface Pilet {
|
|
52
|
+
bundler: Bundler;
|
|
53
|
+
root: string;
|
|
54
|
+
getMeta(basePath: string): PiletMetadata;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
57
|
interface PiletMetadata {
|
|
58
58
|
name?: string;
|
|
59
59
|
config?: Record<string, any>;
|
|
60
60
|
[key: string]: unknown;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
function getMetaOverride(root: string, metaFile: string) {
|
|
63
|
+
async function getMetaOverride(root: string, metaFile: string) {
|
|
64
64
|
if (metaFile) {
|
|
65
65
|
const metaPath = join(root, metaFile);
|
|
66
|
+
const exists = await stat(metaPath).then(
|
|
67
|
+
() => true,
|
|
68
|
+
() => false,
|
|
69
|
+
);
|
|
66
70
|
|
|
67
|
-
if (
|
|
68
|
-
|
|
71
|
+
if (exists) {
|
|
72
|
+
const metaContent = await readFile(metaPath, 'utf8');
|
|
73
|
+
return jju.parse(metaContent);
|
|
69
74
|
}
|
|
70
75
|
}
|
|
71
76
|
|
|
72
77
|
return undefined;
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
function fillPiletMeta(pilet: Pilet, metaFile: string, subPath: string) {
|
|
80
|
+
async function fillPiletMeta(pilet: Pilet, metaFile: string, subPath: string) {
|
|
76
81
|
const { root, bundler } = pilet;
|
|
77
82
|
const packagePath = join(root, 'package.json');
|
|
78
|
-
const
|
|
83
|
+
const jsonContent = await readFile(packagePath, 'utf8');
|
|
84
|
+
const def = jju.parse(jsonContent);
|
|
79
85
|
const file = bundler.bundle.name.replace(/^[\/\\]/, '');
|
|
80
86
|
const target = join(bundler.bundle.dir, file);
|
|
81
|
-
const metaOverride = getMetaOverride(root, metaFile);
|
|
87
|
+
const metaOverride = await getMetaOverride(root, metaFile);
|
|
82
88
|
|
|
83
89
|
pilet.getMeta = (parentPath) => {
|
|
84
90
|
const basePath = `${parentPath}${subPath}`;
|
|
@@ -96,11 +102,11 @@ function fillPiletMeta(pilet: Pilet, metaFile: string, subPath: string) {
|
|
|
96
102
|
};
|
|
97
103
|
}
|
|
98
104
|
|
|
105
|
+
type FeedResponse = { items?: Array<PiletMetadata> } | Array<PiletMetadata> | PiletMetadata;
|
|
106
|
+
|
|
99
107
|
async function loadFeed(feed: string) {
|
|
100
108
|
try {
|
|
101
|
-
const response = await axios.default.get<
|
|
102
|
-
feed,
|
|
103
|
-
);
|
|
109
|
+
const response = await axios.default.get<FeedResponse>(feed);
|
|
104
110
|
|
|
105
111
|
if (Array.isArray(response.data)) {
|
|
106
112
|
return response.data;
|
|
@@ -118,16 +124,33 @@ export default class PiletInjector implements KrasInjector {
|
|
|
118
124
|
public config: PiletInjectorConfig;
|
|
119
125
|
private serverConfig: KrasConfiguration;
|
|
120
126
|
private indexPath: string;
|
|
127
|
+
private proxyInfo?: {
|
|
128
|
+
source: string;
|
|
129
|
+
files: Array<string>;
|
|
130
|
+
date: Date;
|
|
131
|
+
};
|
|
121
132
|
|
|
122
133
|
constructor(config: PiletInjectorConfig, serverConfig: KrasConfiguration, core: EventEmitter) {
|
|
123
134
|
this.config = config;
|
|
124
135
|
this.serverConfig = serverConfig;
|
|
125
136
|
|
|
126
137
|
if (this.config.active) {
|
|
127
|
-
const { pilets, api, publicUrl, assetUrl } = config;
|
|
138
|
+
const { pilets, api, app, publicUrl, assetUrl } = config;
|
|
128
139
|
this.indexPath = `${publicUrl}index.html`;
|
|
129
140
|
const cbs = {};
|
|
130
141
|
|
|
142
|
+
if (app.endsWith('/app')) {
|
|
143
|
+
const packageJson = require(`${app}/../package.json`);
|
|
144
|
+
|
|
145
|
+
if (typeof packageJson.piralCLI.source === 'string') {
|
|
146
|
+
this.proxyInfo = {
|
|
147
|
+
source: packageJson.piralCLI.source,
|
|
148
|
+
files: packageJson.files,
|
|
149
|
+
date: new Date(packageJson.piralCLI.timestamp),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
131
154
|
core.on('user-connected', (e) => {
|
|
132
155
|
const baseUrl = assetUrl || e.req.headers.origin;
|
|
133
156
|
|
|
@@ -144,8 +167,8 @@ export default class PiletInjector implements KrasInjector {
|
|
|
144
167
|
});
|
|
145
168
|
|
|
146
169
|
pilets.forEach((p, i) =>
|
|
147
|
-
p.bundler.on(() => {
|
|
148
|
-
fillPiletMeta(p, config.meta, `/${i}/`);
|
|
170
|
+
p.bundler.on(async () => {
|
|
171
|
+
await fillPiletMeta(p, config.meta, `/${i}/`);
|
|
149
172
|
|
|
150
173
|
for (const id of Object.keys(cbs)) {
|
|
151
174
|
const { baseUrl, notify } = cbs[id];
|
|
@@ -251,7 +274,7 @@ export default class PiletInjector implements KrasInjector {
|
|
|
251
274
|
return merged;
|
|
252
275
|
}
|
|
253
276
|
|
|
254
|
-
sendContent(content: Buffer | string, type: string, url: string):
|
|
277
|
+
sendContent(content: Buffer | string, type: string, url: string): KrasResult {
|
|
255
278
|
const { headers } = this.config;
|
|
256
279
|
|
|
257
280
|
return {
|
|
@@ -269,8 +292,8 @@ export default class PiletInjector implements KrasInjector {
|
|
|
269
292
|
};
|
|
270
293
|
}
|
|
271
294
|
|
|
272
|
-
sendFile(target: string, url: string):
|
|
273
|
-
const content =
|
|
295
|
+
async sendFile(target: string, url: string): Promise<KrasResult> {
|
|
296
|
+
const content = await readFile(target);
|
|
274
297
|
const type = mime.getType(target) ?? 'application/octet-stream';
|
|
275
298
|
return this.sendContent(content, type, url);
|
|
276
299
|
}
|
|
@@ -281,23 +304,25 @@ export default class PiletInjector implements KrasInjector {
|
|
|
281
304
|
const pilet = pilets[+index];
|
|
282
305
|
const bundler = pilet?.bundler;
|
|
283
306
|
|
|
307
|
+
await bundler?.ready();
|
|
308
|
+
|
|
284
309
|
if (!path) {
|
|
285
|
-
await bundler?.ready();
|
|
286
310
|
const content = await this.getIndexMeta(baseUrl);
|
|
287
311
|
return this.sendContent(content, 'application/json', url);
|
|
288
|
-
} else {
|
|
289
|
-
|
|
290
|
-
|
|
312
|
+
} else if (bundler?.bundle) {
|
|
313
|
+
const target = join(bundler.bundle.dir, rest.join('/'));
|
|
314
|
+
const info = await stat(target).catch(() => undefined);
|
|
291
315
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
});
|
|
316
|
+
if (info && info.isFile()) {
|
|
317
|
+
return await this.sendFile(target, url);
|
|
318
|
+
}
|
|
296
319
|
}
|
|
320
|
+
|
|
321
|
+
return undefined;
|
|
297
322
|
}
|
|
298
323
|
|
|
299
|
-
sendIndexFile(target: string, url: string, baseUrl: string):
|
|
300
|
-
const indexHtml =
|
|
324
|
+
async sendIndexFile(target: string, url: string, baseUrl: string): Promise<KrasResult> {
|
|
325
|
+
const indexHtml = await readFile(target, 'utf8');
|
|
301
326
|
|
|
302
327
|
// mechanism to inject server side debug piletApi config into piral emulator
|
|
303
328
|
const windowInjectionScript = `window['dbg:pilet-api'] = '${this.getPiletApi(baseUrl)}';`;
|
|
@@ -308,29 +333,50 @@ export default class PiletInjector implements KrasInjector {
|
|
|
308
333
|
return this.sendContent(content, mime.getType(target), url);
|
|
309
334
|
}
|
|
310
335
|
|
|
311
|
-
|
|
336
|
+
private async shouldLoad(target: string, path: string) {
|
|
337
|
+
if (this.proxyInfo) {
|
|
338
|
+
if (!this.proxyInfo.files.includes(path)) {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const fileInfo = await stat(target).catch(() => undefined);
|
|
343
|
+
|
|
344
|
+
if (!fileInfo || fileInfo.mtime < this.proxyInfo.date) {
|
|
345
|
+
const url = new URL(path, this.proxyInfo.source);
|
|
346
|
+
const response = await axios.default.get(url.href, { responseType: 'arraybuffer' });
|
|
347
|
+
await writeFile(target, response.data);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return true;
|
|
351
|
+
} else {
|
|
352
|
+
const fileInfo = await stat(target).catch(() => undefined);
|
|
353
|
+
return fileInfo && fileInfo.isFile();
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
async handle(req: KrasRequest): Promise<KrasResult> {
|
|
312
358
|
const { app, api, publicUrl, assetUrl } = this.config;
|
|
313
359
|
const baseUrl =
|
|
314
360
|
assetUrl || (req.headers.host ? `${req.encrypted ? 'https' : 'http'}://${req.headers.host}` : undefined);
|
|
315
361
|
|
|
316
362
|
if (!req.target) {
|
|
317
363
|
if (req.url.startsWith(publicUrl)) {
|
|
318
|
-
const path = req.url.substring(publicUrl.length).split('?')
|
|
364
|
+
const path = req.url.substring(publicUrl.length).split('?').shift();
|
|
319
365
|
|
|
320
366
|
if (app) {
|
|
321
367
|
const target = join(app, path);
|
|
322
368
|
|
|
323
|
-
if (
|
|
369
|
+
if (await this.shouldLoad(target, path)) {
|
|
324
370
|
if (req.url === this.indexPath) {
|
|
325
|
-
return this.sendIndexFile(target, req.url, baseUrl);
|
|
371
|
+
return await this.sendIndexFile(target, req.url, baseUrl);
|
|
326
372
|
}
|
|
327
373
|
|
|
328
|
-
return this.sendFile(target, req.url);
|
|
374
|
+
return await this.sendFile(target, req.url);
|
|
329
375
|
}
|
|
330
376
|
}
|
|
331
377
|
|
|
332
378
|
if (req.url !== this.indexPath) {
|
|
333
|
-
return this.handle({
|
|
379
|
+
return await this.handle({
|
|
334
380
|
...req,
|
|
335
381
|
url: this.indexPath,
|
|
336
382
|
});
|
|
@@ -339,8 +385,8 @@ export default class PiletInjector implements KrasInjector {
|
|
|
339
385
|
|
|
340
386
|
return undefined;
|
|
341
387
|
} else if (req.target === api) {
|
|
342
|
-
const path = req.url.substring(1).split('?')
|
|
343
|
-
return this.sendResponse(path, req.url, baseUrl);
|
|
388
|
+
const path = req.url.substring(1).split('?').shift();
|
|
389
|
+
return await this.sendResponse(path, req.url, baseUrl);
|
|
344
390
|
}
|
|
345
391
|
}
|
|
346
392
|
}
|
|
@@ -64,7 +64,7 @@ describe('Piral-CLI piral injector', () => {
|
|
|
64
64
|
expect(injector.active).toBeFalsy();
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
it('PiralInjector can send reponse and fails with invalid path', () => {
|
|
67
|
+
it('PiralInjector can send reponse and fails with invalid path', async () => {
|
|
68
68
|
// Arrange
|
|
69
69
|
const config = {
|
|
70
70
|
bundler: bundlerMock,
|
|
@@ -75,7 +75,7 @@ describe('Piral-CLI piral injector', () => {
|
|
|
75
75
|
const injector = new PiralInjector(config, undefined as any, new EventEmitter());
|
|
76
76
|
|
|
77
77
|
// Act
|
|
78
|
-
const res = injector.sendResponse('some/nice/invalid/path', 'sometarget.file', 'someDir', 'localhost:1234');
|
|
78
|
+
const res = await injector.sendResponse('some/nice/invalid/path', 'sometarget.file', 'someDir', 'localhost:1234');
|
|
79
79
|
|
|
80
80
|
// Assert
|
|
81
81
|
expect(res).toBeUndefined();
|
|
@@ -1,15 +1,10 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
|
-
import {
|
|
4
|
-
import { KrasInjector,
|
|
3
|
+
import { readFile, stat } from 'fs/promises';
|
|
4
|
+
import { KrasInjector, KrasRequest, KrasInjectorConfig, KrasConfiguration, KrasResult } from 'kras';
|
|
5
5
|
import { mime } from '../external';
|
|
6
6
|
import { Bundler } from '../types';
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* The maximum amount of retries when sending a response
|
|
10
|
-
*/
|
|
11
|
-
const maxRetrySendResponse = 4;
|
|
12
|
-
|
|
13
8
|
export interface PiralInjectorConfig extends KrasInjectorConfig {
|
|
14
9
|
bundler: Bundler;
|
|
15
10
|
publicUrl: string;
|
|
@@ -17,6 +12,20 @@ export interface PiralInjectorConfig extends KrasInjectorConfig {
|
|
|
17
12
|
headers?: Record<string, string>;
|
|
18
13
|
}
|
|
19
14
|
|
|
15
|
+
/**
|
|
16
|
+
* The maximum amount of retries when sending a response
|
|
17
|
+
*/
|
|
18
|
+
const maxRetrySendResponse = 4;
|
|
19
|
+
|
|
20
|
+
async function isNoFile(target: string) {
|
|
21
|
+
try {
|
|
22
|
+
const info = await stat(target);
|
|
23
|
+
return !info.isFile();
|
|
24
|
+
} catch {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
20
29
|
export default class PiralInjector implements KrasInjector {
|
|
21
30
|
public config: PiralInjectorConfig;
|
|
22
31
|
|
|
@@ -63,7 +72,7 @@ export default class PiralInjector implements KrasInjector {
|
|
|
63
72
|
|
|
64
73
|
setOptions() {}
|
|
65
74
|
|
|
66
|
-
sendContent(content: Buffer | string, type: string, url: string):
|
|
75
|
+
sendContent(content: Buffer | string, type: string, url: string): KrasResult {
|
|
67
76
|
const { headers } = this.config;
|
|
68
77
|
return {
|
|
69
78
|
injector: { name: this.name },
|
|
@@ -80,8 +89,8 @@ export default class PiralInjector implements KrasInjector {
|
|
|
80
89
|
};
|
|
81
90
|
}
|
|
82
91
|
|
|
83
|
-
sendIndexFile(target: string, url: string):
|
|
84
|
-
const indexHtml =
|
|
92
|
+
async sendIndexFile(target: string, url: string): Promise<KrasResult> {
|
|
93
|
+
const indexHtml = await readFile(target, 'utf8');
|
|
85
94
|
const { feed } = this.config;
|
|
86
95
|
|
|
87
96
|
if (feed) {
|
|
@@ -96,7 +105,7 @@ export default class PiralInjector implements KrasInjector {
|
|
|
96
105
|
return this.sendContent(indexHtml, mime.getType(target), url);
|
|
97
106
|
}
|
|
98
107
|
|
|
99
|
-
sendResponse(path: string, target: string, dir: string, url: string, recursionDepth = 0):
|
|
108
|
+
async sendResponse(path: string, target: string, dir: string, url: string, recursionDepth = 0): Promise<KrasResult> {
|
|
100
109
|
if (recursionDepth > maxRetrySendResponse) {
|
|
101
110
|
return undefined;
|
|
102
111
|
}
|
|
@@ -104,17 +113,18 @@ export default class PiralInjector implements KrasInjector {
|
|
|
104
113
|
const { bundler } = this.config;
|
|
105
114
|
const newTarget = join(bundler.bundle.dir, bundler.bundle.name);
|
|
106
115
|
|
|
107
|
-
if (!path ||
|
|
108
|
-
return this.sendResponse(bundler.bundle.name, newTarget, dir, url, recursionDepth + 1);
|
|
116
|
+
if (!path || (await isNoFile(target))) {
|
|
117
|
+
return await this.sendResponse(bundler.bundle.name, newTarget, dir, url, recursionDepth + 1);
|
|
109
118
|
} else if (target === newTarget) {
|
|
110
|
-
return this.sendIndexFile(target, url);
|
|
119
|
+
return await this.sendIndexFile(target, url);
|
|
120
|
+
} else {
|
|
121
|
+
const type = mime.getType(target) ?? 'application/octet-stream';
|
|
122
|
+
const content = await readFile(target);
|
|
123
|
+
return this.sendContent(content, type, url);
|
|
111
124
|
}
|
|
112
|
-
|
|
113
|
-
const type = mime.getType(target) ?? 'application/octet-stream';
|
|
114
|
-
return this.sendContent(readFileSync(target), type, url);
|
|
115
125
|
}
|
|
116
126
|
|
|
117
|
-
handle(req: KrasRequest):
|
|
127
|
+
async handle(req: KrasRequest): Promise<KrasResult> {
|
|
118
128
|
if (!req.target) {
|
|
119
129
|
const { bundler, publicUrl } = this.config;
|
|
120
130
|
|
|
@@ -122,8 +132,9 @@ export default class PiralInjector implements KrasInjector {
|
|
|
122
132
|
const pathLength = publicUrl.length || 1;
|
|
123
133
|
const path = req.url.substring(pathLength);
|
|
124
134
|
const dir = bundler.bundle.dir;
|
|
125
|
-
const target = join(dir, path.split('?')
|
|
126
|
-
|
|
135
|
+
const target = join(dir, path.split('?').shift());
|
|
136
|
+
await bundler.ready();
|
|
137
|
+
return await this.sendResponse(path, target, dir, req.url);
|
|
127
138
|
}
|
|
128
139
|
}
|
|
129
140
|
}
|
package/src/types/common.ts
CHANGED
|
@@ -4,6 +4,30 @@ export interface Importmap {
|
|
|
4
4
|
exclude?: Array<string>;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
export interface EmulatorWebsiteManifestFiles {
|
|
8
|
+
typings: string;
|
|
9
|
+
main: string;
|
|
10
|
+
app: string;
|
|
11
|
+
assets: Array<string>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface EmulatorWebsiteManifest {
|
|
15
|
+
name: string;
|
|
16
|
+
description: string;
|
|
17
|
+
version: string;
|
|
18
|
+
timestamp: string;
|
|
19
|
+
scaffolding: {
|
|
20
|
+
pilets: PiletsInfo;
|
|
21
|
+
cli: string;
|
|
22
|
+
};
|
|
23
|
+
files: EmulatorWebsiteManifestFiles;
|
|
24
|
+
importmap: Importmap;
|
|
25
|
+
dependencies: {
|
|
26
|
+
optional: Record<string, string>;
|
|
27
|
+
included: Record<string, string>;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
7
31
|
export interface PackageData {
|
|
8
32
|
name: string;
|
|
9
33
|
version: string;
|
package/src/types/internal.ts
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import type { LogLevels } from './common';
|
|
2
2
|
import type { ImportmapVersions, PiletSchemaVersion } from './public';
|
|
3
3
|
|
|
4
|
+
export interface PiralInstanceDetails {
|
|
5
|
+
selected?: boolean;
|
|
6
|
+
port?: number;
|
|
7
|
+
path?: string;
|
|
8
|
+
url?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
4
11
|
/**
|
|
5
12
|
* Shape of the pilet.json
|
|
6
13
|
*/
|
|
7
14
|
export interface PiletDefinition {
|
|
8
15
|
schemaVersion?: PiletSchemaVersion;
|
|
9
16
|
importmapVersions?: ImportmapVersions;
|
|
10
|
-
piralInstances?: Record<
|
|
11
|
-
string,
|
|
12
|
-
{
|
|
13
|
-
selected?: boolean;
|
|
14
|
-
port?: number;
|
|
15
|
-
}
|
|
16
|
-
>;
|
|
17
|
+
piralInstances?: Record<string, PiralInstanceDetails>;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export interface PackageFiles {
|
package/src/types/public.ts
CHANGED
|
@@ -136,6 +136,7 @@ export interface BundlerPrepareArgs<T> {
|
|
|
136
136
|
|
|
137
137
|
export interface BaseBundlerDefinition<T> {
|
|
138
138
|
path: string;
|
|
139
|
+
exec?: string;
|
|
139
140
|
prepare?: BundlerPrepareArgs<T>;
|
|
140
141
|
}
|
|
141
142
|
|
|
@@ -235,7 +236,7 @@ export type PiralBuildType = 'all' | 'release' | 'emulator' | 'emulator-sources'
|
|
|
235
236
|
|
|
236
237
|
export type PiletBuildType = 'default' | 'standalone' | 'manifest';
|
|
237
238
|
|
|
238
|
-
export type PackageType = 'registry' | 'file' | 'git';
|
|
239
|
+
export type PackageType = 'registry' | 'file' | 'git' | 'remote';
|
|
239
240
|
|
|
240
241
|
export type NpmClientType = 'npm' | 'yarn' | 'pnp' | 'pnpm' | 'lerna' | 'rush' | 'bun';
|
|
241
242
|
|