piral-cli 1.4.0-beta.6247 → 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.
Files changed (35) hide show
  1. package/lib/apps/add-piral-instance-pilet.js +1 -4
  2. package/lib/apps/add-piral-instance-pilet.js.map +1 -1
  3. package/lib/apps/new-pilet.js +4 -3
  4. package/lib/apps/new-pilet.js.map +1 -1
  5. package/lib/apps/run-emulator-piral.js +1 -1
  6. package/lib/apps/run-emulator-piral.js.map +1 -1
  7. package/lib/apps/upgrade-pilet.js +3 -2
  8. package/lib/apps/upgrade-pilet.js.map +1 -1
  9. package/lib/common/package.d.ts +1 -2
  10. package/lib/common/package.js +25 -33
  11. package/lib/common/package.js.map +1 -1
  12. package/lib/common/shell.d.ts +2 -2
  13. package/lib/common/shell.js +45 -14
  14. package/lib/common/shell.js.map +1 -1
  15. package/lib/common/website.d.ts +1 -2
  16. package/lib/common/website.js +6 -4
  17. package/lib/common/website.js.map +1 -1
  18. package/lib/injectors/pilet-injector.d.ts +12 -10
  19. package/lib/injectors/pilet-injector.js +103 -60
  20. package/lib/injectors/pilet-injector.js.map +1 -1
  21. package/lib/injectors/piral-injector.d.ts +5 -5
  22. package/lib/injectors/piral-injector.js +64 -34
  23. package/lib/injectors/piral-injector.js.map +1 -1
  24. package/package.json +2 -2
  25. package/src/apps/add-piral-instance-pilet.ts +1 -16
  26. package/src/apps/new-pilet.ts +4 -10
  27. package/src/apps/run-emulator-piral.ts +1 -1
  28. package/src/apps/upgrade-pilet.ts +3 -2
  29. package/src/common/package.ts +20 -35
  30. package/src/common/shell.ts +59 -18
  31. package/src/common/website.ts +6 -4
  32. package/src/injectors/pilet-injector.test.ts +4 -4
  33. package/src/injectors/pilet-injector.ts +87 -41
  34. package/src/injectors/piral-injector.test.ts +2 -2
  35. package/src/injectors/piral-injector.ts +31 -20
@@ -1,15 +1,10 @@
1
1
  import { join } from 'path';
2
2
  import { EventEmitter } from 'events';
3
- import { readFileSync, existsSync, statSync } from 'fs';
4
- import { KrasInjector, KrasResponse, KrasRequest, KrasInjectorConfig, KrasConfiguration } from 'kras';
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): KrasResponse {
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): KrasResponse {
84
- const indexHtml = readFileSync(target, 'utf8');
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): KrasResponse {
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 || !existsSync(target) || !statSync(target).isFile()) {
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): KrasResponse {
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('?')[0]);
126
- return bundler.ready().then(() => this.sendResponse(path, target, dir, req.url));
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
  }