1 | 'use strict';
|
2 |
|
3 | const os = require('os');
|
4 | const path = require('path');
|
5 | const crypto = require('crypto');
|
6 | const util = require('util');
|
7 | const chalk = require('chalk');
|
8 | const lockfile = require('lockfile');
|
9 |
|
10 | const lfLock = util.promisify(lockfile.lock);
|
11 | const lfUnlock = util.promisify(lockfile.unlock);
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function warn( message )
|
19 | {
|
20 | const prefix = chalk.hex('#CC4A8B')('WARNING:');
|
21 |
|
22 | console.warn(chalk`${prefix} ${message}`);
|
23 | }
|
24 |
|
25 | warn.cache = new Set();
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | warn.once = function( message ) {
|
33 | if ( warn.cache.has( message ) ) {
|
34 | return;
|
35 | }
|
36 |
|
37 | warn( message );
|
38 |
|
39 | warn.cache.add( message );
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function maybeArrayWrap( data )
|
47 | {
|
48 | return Array.isArray( data ) ? data : [ data ];
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function filterHashes( hashes )
|
58 | {
|
59 | const validHashes = crypto.getHashes();
|
60 |
|
61 | return hashes.filter( hash => {
|
62 | if ( validHashes.includes(hash) ) {
|
63 | return true;
|
64 | }
|
65 |
|
66 | warn(chalk`{blueBright ${hash}} is not a supported hash algorithm`);
|
67 |
|
68 | return false;
|
69 | });
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function getSRIHash( hashes, content )
|
80 | {
|
81 | return Array.isArray( hashes ) ? hashes.map( hash => {
|
82 | const integrity = crypto.createHash(hash).update(content, 'utf8').digest('base64');
|
83 |
|
84 | return `${hash}-${integrity}`;
|
85 | }).join(' ') : '';
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function varType( v )
|
95 | {
|
96 | const [ , type ] = Object.prototype.toString.call( v ).match(/^\[object (\w+)\]$/);
|
97 |
|
98 | return type;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function isObject( arg )
|
107 | {
|
108 | return varType( arg ) === 'Object';
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function getSortedObject(object, compareFunction)
|
119 | {
|
120 | return Object.keys( object ).sort( compareFunction ).reduce(
|
121 | (sorted, key) => (sorted[ key ] = object[ key ], sorted),
|
122 | Object.create(null),
|
123 | );
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | function findMapKeysByValue( map )
|
132 | {
|
133 | const entries = [ ...map.entries() ];
|
134 |
|
135 | return searchValue => entries
|
136 | .filter( ([ , value ]) => value === searchValue )
|
137 | .map( ([ name ]) => name );
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | function group( arr, getGroup, mapper = item => item )
|
148 | {
|
149 | return arr.reduce(
|
150 | (obj, item) => {
|
151 | const group = getGroup( item );
|
152 |
|
153 | if ( group ) {
|
154 | obj[ group ] = obj[ group ] || [];
|
155 | obj[ group ].push( mapper( item, group ) );
|
156 | }
|
157 |
|
158 | return obj;
|
159 | },
|
160 | Object.create(null),
|
161 | );
|
162 | }
|
163 |
|
164 | function md5( data )
|
165 | {
|
166 | return crypto.createHash('md5').update( data ).digest('hex');
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | function getLockFilename( filename )
|
175 | {
|
176 | const name = path.basename( filename );
|
177 | const dirHash = md5( path.dirname( filename ) );
|
178 |
|
179 | return path.join( os.tmpdir(), `${dirHash}-${name}.lock` );
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | async function lock( filename )
|
188 | {
|
189 | await lfLock(
|
190 | getLockFilename( filename ),
|
191 | {
|
192 | wait: 6000,
|
193 | retryWait: 100,
|
194 | stale: 5000,
|
195 | retries: 100,
|
196 | },
|
197 | );
|
198 | }
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | async function unlock( filename )
|
206 | {
|
207 | await lfUnlock( getLockFilename( filename ) );
|
208 | }
|
209 |
|
210 | module.exports = {
|
211 | maybeArrayWrap,
|
212 | filterHashes,
|
213 | getSRIHash,
|
214 | warn,
|
215 | varType,
|
216 | isObject,
|
217 | getSortedObject,
|
218 | findMapKeysByValue,
|
219 | group,
|
220 | getLockFilename,
|
221 | lock,
|
222 | unlock,
|
223 | };
|