piral-cli 0.15.0-alpha.3933 → 0.15.0-alpha.3975

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,6 +5,7 @@ import { platform, tmpdir } from 'os';
5
5
  import { createWriteStream } from 'fs';
6
6
  import { log } from './log';
7
7
  import { axios, FormData } from '../external';
8
+ import { PiletPublishScheme } from '../types';
8
9
 
9
10
  const os = platform();
10
11
  const standardHeaders = {
@@ -67,25 +68,42 @@ export interface PostFileResult {
67
68
 
68
69
  export function postFile(
69
70
  target: string,
71
+ scheme: PiletPublishScheme,
70
72
  key: string,
71
73
  file: Buffer,
72
- fields: Record<string, string> = {},
74
+ customFields: Record<string, string> = {},
75
+ customHeaders: Record<string, string> = {},
73
76
  ca?: Buffer,
74
77
  ): Promise<PostFileResult> {
75
78
  const form = new FormData();
76
79
  const httpsAgent = ca ? new Agent({ ca }) : undefined;
77
80
 
78
- Object.keys(fields).forEach((key) => form.append(key, fields[key]));
81
+ Object.keys(customFields).forEach((key) => form.append(key, customFields[key]));
79
82
 
80
83
  form.append('file', file, 'pilet.tgz');
81
84
 
82
85
  const headers: Record<string, string> = {
83
86
  ...form.getHeaders(),
84
87
  ...standardHeaders,
88
+ ...customHeaders,
85
89
  };
86
90
 
87
91
  if (key) {
88
- headers.authorization = `Basic ${key}`;
92
+ switch (scheme) {
93
+ case 'basic':
94
+ headers.authorization = `Basic ${key}`;
95
+ break;
96
+ case 'bearer':
97
+ headers.authorization = `Bearer ${key}`;
98
+ break;
99
+ case 'digest':
100
+ headers.authorization = `Digest ${key}`;
101
+ break;
102
+ case 'none':
103
+ default:
104
+ headers.authorization = key;
105
+ break;
106
+ }
89
107
  }
90
108
 
91
109
  return axios.default
@@ -20,6 +20,7 @@ export * from './merge';
20
20
  export * from './npm';
21
21
  export * from './pack';
22
22
  export * from './package';
23
+ export * from './parallel';
23
24
  export * from './patcher';
24
25
  export * from './patches';
25
26
  export * from './port';
@@ -1,3 +1,5 @@
1
+ import { cpus } from 'os';
2
+
1
3
  const info = require('../../package.json');
2
4
 
3
5
  export function findCompatVersion(version: string) {
@@ -18,3 +20,4 @@ export const compatVersion = findCompatVersion(cliVersion);
18
20
  export const repositoryUrl = info.repository.url;
19
21
  export const isWindows = process.platform === 'win32';
20
22
  export const pathSeparator = isWindows ? ';' : ':';
23
+ export const cpuCount = cpus().length;
@@ -0,0 +1,28 @@
1
+ import { concurrentWorkers } from './parallel';
2
+
3
+ describe('Concurrent Workers', () => {
4
+ it('Can run against no entries', async () => {
5
+ const result = await concurrentWorkers([], 10, () => Promise.resolve('foo'));
6
+ expect(result).toEqual([]);
7
+ });
8
+
9
+ it('Can run against single entry', async () => {
10
+ const result = await concurrentWorkers(['bar'], 10, (item) => Promise.resolve('foo' + item));
11
+ expect(result).toEqual(['foobar']);
12
+ });
13
+
14
+ it('Can run against less entries than concurrency', async () => {
15
+ const result = await concurrentWorkers(['bar', 'rba', 'abr', 'rab', 'arb', 'bra'], 10, (item) => Promise.resolve('foo' + item));
16
+ expect(result).toEqual(['foobar', 'foorba', 'fooabr', 'foorab', 'fooarb', 'foobra']);
17
+ });
18
+
19
+ it('Can run against more entries than concurrency', async () => {
20
+ const result = await concurrentWorkers(['bar', 'rba', 'abr', 'rab', 'arb', 'bra'], 2, (item) => Promise.resolve('foo' + item));
21
+ expect(result).toEqual(['foobar', 'foorba', 'fooabr', 'foorab', 'fooarb', 'foobra']);
22
+ });
23
+
24
+ it('Can run against equal entries than concurrency', async () => {
25
+ const result = await concurrentWorkers(['bar', 'rba', 'abr', 'rab', 'arb', 'bra'], 6, (item) => Promise.resolve('foo' + item));
26
+ expect(result).toEqual(['foobar', 'foorba', 'fooabr', 'foorab', 'fooarb', 'foobra']);
27
+ });
28
+ });
@@ -0,0 +1,21 @@
1
+ export async function concurrentWorkers<T, R>(
2
+ items: Array<T>,
3
+ concurrency: number,
4
+ worker: (item: T) => Promise<R>,
5
+ ): Promise<Array<R>> {
6
+ const maxItems = items.length;
7
+ const results: Array<R> = new Array(maxItems);
8
+ let offset = 0;
9
+
10
+ await Promise.all(
11
+ items.slice(0, concurrency).map(async () => {
12
+ while (offset < maxItems) {
13
+ const i = offset++;
14
+ const item = items[i];
15
+ results[i] = await worker(item);
16
+ }
17
+ }),
18
+ );
19
+
20
+ return results;
21
+ }
package/src/helpers.ts CHANGED
@@ -7,9 +7,11 @@ import {
7
7
  PiletPublishSource,
8
8
  PiralBuildType,
9
9
  PiletBuildType,
10
+ PiletPublishScheme,
10
11
  } from './types';
11
12
 
12
13
  export const schemaKeys: Array<PiletSchemaVersion> = ['v0', 'v1', 'v2', 'none'];
14
+ export const publishModeKeys: Array<PiletPublishScheme> = ['none', 'basic', 'bearer', 'digest'];
13
15
  export const fromKeys: Array<PiletPublishSource> = ['local', 'remote', 'npm'];
14
16
  export const piralBuildTypeKeys: Array<PiralBuildType> = ['all', 'release', 'emulator', 'emulator-sources'];
15
17
  export const piletBuildTypeKeys: Array<PiletBuildType> = ['default', 'standalone', 'manifest'];
@@ -218,6 +218,8 @@ export interface BundlerDefinition {
218
218
 
219
219
  export type PiletSchemaVersion = 'none' | 'v0' | 'v1' | 'v2';
220
220
 
221
+ export type PiletPublishScheme = 'none' | 'digest' | 'bearer' | 'basic';
222
+
221
223
  export type PiletPublishSource = 'local' | 'npm' | 'remote';
222
224
 
223
225
  export type PiralBuildType = 'all' | 'release' | 'emulator' | 'emulator-sources';