@milaboratories/pl-drivers 1.3.2 → 1.3.3

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 (44) hide show
  1. package/README.md +5 -3
  2. package/dist/clients/download.d.ts.map +1 -1
  3. package/dist/clients/progress.d.ts.map +1 -1
  4. package/dist/clients/upload.d.ts.map +1 -1
  5. package/dist/drivers/download_and_logs_blob.d.ts.map +1 -1
  6. package/dist/drivers/download_url.d.ts.map +1 -1
  7. package/dist/drivers/helpers/helpers.d.ts.map +1 -1
  8. package/dist/drivers/helpers/test_helpers.d.ts.map +1 -1
  9. package/dist/drivers/logs.d.ts.map +1 -1
  10. package/dist/drivers/logs_stream.d.ts.map +1 -1
  11. package/dist/drivers/ls.d.ts +1 -11
  12. package/dist/drivers/ls.d.ts.map +1 -1
  13. package/dist/drivers/types.d.ts +11 -0
  14. package/dist/drivers/types.d.ts.map +1 -1
  15. package/dist/drivers/virtual_storages.d.ts +10 -0
  16. package/dist/drivers/virtual_storages.d.ts.map +1 -0
  17. package/dist/helpers/download.d.ts.map +1 -1
  18. package/dist/index.d.ts +1 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +2 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/index.mjs +446 -546
  23. package/dist/index.mjs.map +1 -1
  24. package/package.json +4 -4
  25. package/src/clients/download.test.ts +9 -5
  26. package/src/clients/download.ts +5 -8
  27. package/src/clients/progress.ts +1 -4
  28. package/src/clients/upload.test.ts +2 -5
  29. package/src/clients/upload.ts +10 -44
  30. package/src/drivers/download_and_logs_blob.ts +32 -109
  31. package/src/drivers/download_blob.test.ts +37 -55
  32. package/src/drivers/download_url.test.ts +1 -1
  33. package/src/drivers/download_url.ts +6 -25
  34. package/src/drivers/helpers/helpers.ts +3 -12
  35. package/src/drivers/helpers/test_helpers.ts +1 -3
  36. package/src/drivers/logs.test.ts +27 -65
  37. package/src/drivers/logs.ts +17 -66
  38. package/src/drivers/logs_stream.ts +12 -48
  39. package/src/drivers/ls.test.ts +7 -6
  40. package/src/drivers/ls.ts +33 -42
  41. package/src/drivers/types.ts +15 -0
  42. package/src/drivers/virtual_storages.ts +44 -0
  43. package/src/helpers/download.ts +2 -4
  44. package/src/index.ts +1 -0
package/src/drivers/ls.ts CHANGED
@@ -19,9 +19,12 @@ import {
19
19
  parseIndexHandle,
20
20
  parseUploadHandle
21
21
  } from './helpers/ls_list_entry';
22
- import { createLocalStorageHandle, createRemoteStorageHandle, parseStorageHandle } from './helpers/ls_storage_entry';
23
- import { LocalStorageProjection } from './types';
24
- import * as os from 'node:os';
22
+ import {
23
+ createLocalStorageHandle,
24
+ createRemoteStorageHandle,
25
+ parseStorageHandle
26
+ } from './helpers/ls_storage_entry';
27
+ import { LocalStorageProjection, VirtualLocalStorageSpec } from './types';
25
28
  import { createLsFilesClient } from '../clients/helpers';
26
29
  import { validateAbsolute } from '../helpers/validate';
27
30
 
@@ -42,31 +45,6 @@ export type OpenFileDialogCallback = (
42
45
  ops?: OpenDialogOps
43
46
  ) => Promise<undefined | string[]>;
44
47
 
45
- /** Allows to add parts of local FS as virtual storages, presenting homogeneous API to UI */
46
- export type VirtualLocalStorageSpec = {
47
- /** Virtual storage ID, must not intersect with other storage ids */
48
- readonly name: string;
49
-
50
- /** Local path to "chroot" the API in */
51
- readonly root: string;
52
-
53
- /** Used as hint to UI controls to, set as initial path during browsing */
54
- readonly initialPath: string;
55
- };
56
-
57
- export function DefaultVirtualLocalStorages(): VirtualLocalStorageSpec[] {
58
- const home = os.homedir();
59
- return path.sep === '/'
60
- ? [{ name: 'local', root: '/', initialPath: home }]
61
- : [
62
- {
63
- name: 'local',
64
- root: path.parse(home).root, // disk where home directory resides
65
- initialPath: home
66
- }
67
- ];
68
- }
69
-
70
48
  export class LsDriver implements InternalLsDriver {
71
49
  private constructor(
72
50
  private readonly logger: MiLogger,
@@ -180,18 +158,29 @@ export class LsDriver implements InternalLsDriver {
180
158
  }
181
159
 
182
160
  public async getStorageList(): Promise<sdk.StorageEntry[]> {
183
- return [
184
- ...[...this.virtualStoragesMap.values()].map((vs) => ({
185
- name: vs.name,
186
- handle: createLocalStorageHandle(vs.name, vs.root),
187
- initialFullPath: vs.initialPath
188
- })),
189
- ...Object.entries(this.storageIdToResourceId!).map(([storageId, resourceId]) => ({
161
+ const virtualStorages = [...this.virtualStoragesMap.values()].map((s) => ({
162
+ name: s.name,
163
+ handle: createLocalStorageHandle(s.name, s.root),
164
+ initialFullPath: s.initialPath,
165
+ isInitialPathHome: s.isInitialPathHome
166
+ }));
167
+
168
+ const otherStorages = Object.entries(this.storageIdToResourceId!).map(
169
+ ([storageId, resourceId]) => ({
190
170
  name: storageId,
191
171
  handle: createRemoteStorageHandle(storageId, resourceId),
192
- initialFullPath: '' // we don't have any additional information from where to start browsing remote storages
193
- }))
194
- ] as sdk.StorageEntry[];
172
+ initialFullPath: '', // we don't have any additional information from where to start browsing remote storages
173
+ isInitialPathHome: false
174
+ })
175
+ );
176
+
177
+ // root must be a storage so we can index any file,
178
+ // but for UI it's enough
179
+ // to have local virtual storage on *nix,
180
+ // and local_disk_${drive} on Windows.
181
+ const noRoot = otherStorages.filter((it) => it.name !== 'root');
182
+
183
+ return [...virtualStorages, ...noRoot];
195
184
  }
196
185
 
197
186
  public async listFiles(
@@ -213,10 +202,12 @@ export class LsDriver implements InternalLsDriver {
213
202
  } else {
214
203
  if (path.sep === '/' && fullPath === '') fullPath = '/';
215
204
 
216
- const lsRoot =
217
- storageData.rootPath === ''
218
- ? validateAbsolute(fullPath)
219
- : path.join(storageData.rootPath, fullPath);
205
+ if (storageData.rootPath === '') {
206
+ validateAbsolute(fullPath);
207
+ }
208
+ const lsRoot = path.isAbsolute(fullPath)
209
+ ? fullPath
210
+ : path.join(storageData.rootPath, fullPath);
220
211
 
221
212
  const entries: LsEntry[] = [];
222
213
  for await (const dirent of await fsp.opendir(lsRoot)) {
@@ -39,3 +39,18 @@ export type LocalStorageProjection = {
39
39
  * */
40
40
  readonly localPath: string;
41
41
  };
42
+
43
+ /** Allows to add parts of local FS as virtual storages, presenting homogeneous API to UI */
44
+ export type VirtualLocalStorageSpec = {
45
+ /** Virtual storage ID, must not intersect with other storage ids */
46
+ readonly name: string;
47
+
48
+ /** Local path to "chroot" the API in */
49
+ readonly root: string;
50
+
51
+ /** Used as hint to UI controls to, set as initial path during browsing */
52
+ readonly initialPath: string;
53
+
54
+ /** Shows if the initial path is home, so this storage should be chosen by default. */
55
+ readonly isInitialPathHome: boolean;
56
+ };
@@ -0,0 +1,44 @@
1
+ import path from 'path';
2
+ import os from 'os';
3
+ import util from 'util';
4
+ import { exec } from 'child_process';
5
+ import { VirtualLocalStorageSpec } from './types';
6
+
7
+ export async function DefaultVirtualLocalStorages(): Promise<VirtualLocalStorageSpec[]> {
8
+ const home = os.homedir();
9
+ if (path.sep == '/')
10
+ return [
11
+ {
12
+ name: 'local',
13
+ root: '/',
14
+ initialPath: home,
15
+ isInitialPathHome: true
16
+ }
17
+ ];
18
+
19
+ const homeRoot = path.parse(home).root; // e.g. C:\
20
+ const homeDrive = homeRoot.replaceAll(':\\', ''); // e.g. C drive.
21
+ const wmic = await util.promisify(exec)('wmic logicaldisk get name');
22
+ const drives = windowsDrives(wmic.stdout);
23
+
24
+ return windowsStorages(drives, homeDrive, home);
25
+ }
26
+
27
+ export function windowsDrives(wmicStdout: string): string[] {
28
+ return wmicStdout
29
+ .split('\r\n')
30
+ .filter((line) => line.includes(':'))
31
+ .map((line) => line.trim().replaceAll(':', ''));
32
+ }
33
+
34
+ export function windowsStorages(drives: string[], homeDrive: string, home: string) {
35
+ return drives.map((d) => {
36
+ const isInitialPathHome = d == homeDrive;
37
+ return {
38
+ name: `local_disk_${d}`,
39
+ root: `${d}:\\`,
40
+ initialPath: isInitialPathHome ? home : `${d}:\\`,
41
+ isInitialPathHome
42
+ };
43
+ });
44
+ }
@@ -28,7 +28,7 @@ export class DownloadHelper {
28
28
  const webBody = Readable.toWeb(body);
29
29
 
30
30
  if (statusCode != 200) {
31
- const textBody = await text(webBody)
31
+ const textBody = await text(webBody);
32
32
  const beginning = textBody.substring(0, Math.min(textBody.length, 1000));
33
33
 
34
34
  if (400 <= statusCode && statusCode < 500) {
@@ -37,9 +37,7 @@ export class DownloadHelper {
37
37
  );
38
38
  }
39
39
 
40
- throw new Error(
41
- `Http error: statusCode: ${statusCode} url: ${url.toString()}`
42
- );
40
+ throw new Error(`Http error: statusCode: ${statusCode} url: ${url.toString()}`);
43
41
  }
44
42
 
45
43
  return {
package/src/index.ts CHANGED
@@ -11,6 +11,7 @@ export * from './drivers/logs_stream';
11
11
  export * from './drivers/logs';
12
12
  export * from './drivers/download_url';
13
13
  export * from './drivers/ls';
14
+ export * from './drivers/virtual_storages';
14
15
  export * from './drivers/helpers/helpers';
15
16
  export * from './drivers/helpers/polling_ops';
16
17