utilitas 1995.1.21 → 1995.1.23

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.
@@ -13,7 +13,8 @@ const _NEED = [
13
13
  '@google-cloud/speech',
14
14
  '@google-cloud/text-to-speech',
15
15
  '@google-cloud/vision',
16
- ]
16
+ 'google-gax',
17
+ ];
17
18
 
18
19
  const defaultAlgorithm = 'sha256';
19
20
  const defaultEncryption = 'aes-256-gcm';
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1995.1.21",
4
+ "version": "1995.1.23",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
@@ -17,17 +17,18 @@ const manifest = {
17
17
  },
18
18
  "dependencies": {
19
19
  "file-type": "^18.7.0",
20
- "mathjs": "^12.0.0",
20
+ "mathjs": "^12.1.0",
21
21
  "uuid": "^9.0.1"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@ffmpeg-installer/ffmpeg": "^1.1.0",
25
25
  "@ffprobe-installer/ffprobe": "^2.1.2",
26
26
  "@google-cloud/speech": "^6.1.0",
27
+ "@google-cloud/storage": "^7.6.0",
27
28
  "@google-cloud/text-to-speech": "^5.0.1",
28
29
  "@google-cloud/vision": "^4.0.2",
29
30
  "@mozilla/readability": "^0.4.4",
30
- "@sentry/node": "^7.80.0",
31
+ "@sentry/node": "^7.80.1",
31
32
  "@waylaidwanderer/chatgpt-api": "^1.37.3",
32
33
  "acme-client": "^5.0.0",
33
34
  "browserify-fs": "^1.0.0",
@@ -44,7 +45,7 @@ const manifest = {
44
45
  "node-mailjet": "^6.0.4",
45
46
  "node-polyfill-webpack-plugin": "^2.0.1",
46
47
  "office-text-extractor": "^3.0.2",
47
- "openai": "^4.17.4",
48
+ "openai": "^4.19.0",
48
49
  "ping": "^0.4.4",
49
50
  "say": "^0.16.0",
50
51
  "telegraf": "^4.15.0",
package/lib/storage.mjs CHANGED
@@ -12,8 +12,11 @@ import { homedir, tmpdir } from 'os';
12
12
  import { promisify } from 'util';
13
13
  import { v4 as uuidv4 } from 'uuid';
14
14
 
15
- const _NEED = ['mime-types'];
16
- const mapFilename = (name) => join(name.substr(0, 2), name.substr(2, 2));
15
+ const _NEED = ['mime-types', '@google-cloud/storage'];
16
+ const errorMessage = 'Invalid file.';
17
+ const defaultMetadata = { cacheControl: 'public, max-age=31536000' };
18
+ const getGcUrlByBucket = bucket => `https://storage.cloud.google.com/${bucket}`;
19
+ const mapFilename = name => join(name.substr(0, 2), name.substr(2, 2));
17
20
  const [_zip, _unzip] = [__zip, __unzip].map(promisify);
18
21
 
19
22
  const [NULL, BASE64, BUFFER, FILE, STREAM, encoding, BINARY, mode, dirMode]
@@ -22,8 +25,12 @@ const [NULL, BASE64, BUFFER, FILE, STREAM, encoding, BINARY, mode, dirMode]
22
25
  const [encodeBase64, encodeBinary, encodeNull]
23
26
  = [{ encoding: BASE64 }, { encoding: BINARY }, { encoding: NULL }];
24
27
 
28
+ let client = null;
29
+
25
30
  const readFile = async (name, options) => await fs.readFile(
26
- name, options?.encoding === NULL ? null : (options?.encoding || encoding)
31
+ name, ['NULL', 'BUFFER'].includes(
32
+ ensureString(options?.encoding, { case: 'UP' })
33
+ ) ? null : (options?.encoding || encoding)
27
34
  );
28
35
 
29
36
  const writeFile = async (filename, data, options) => await fs.writeFile(
@@ -278,6 +285,50 @@ const tryRm = async (path, options) => await ignoreErrFunc(async () => {
278
285
  await fs.unlink(path);
279
286
  }, options);
280
287
 
288
+ const init = async (options) => {
289
+ if (options) {
290
+ const provider = ensureString(options?.provider, { case: 'UP' });
291
+ switch (provider) {
292
+ case 'GOOGLE':
293
+ assert(
294
+ options?.apiKey, 'Google Cloud API key is required.', 400
295
+ )
296
+ assert(
297
+ options?.bucket,
298
+ 'Google Cloud Storage bucket is required.', 400
299
+ );
300
+ const url = trim(options?.url) || getGcUrlByBucket(options.bucket);
301
+ ~~process.env.FORKED === 1 && log(`GOOGLE CLOUD STORAGE: ${url}`);
302
+ const { Storage } = await need(
303
+ '@google-cloud/storage', { raw: true }
304
+ );
305
+ client = new Storage({
306
+ credentials: options.apiKey
307
+ }).bucket(options.bucket);
308
+ break;
309
+ default:
310
+ throwError('Invalid cloud storage provider.', 500);
311
+ }
312
+ }
313
+ assert(client, 'Cloud storage has not been initialized.', 501);
314
+ return client;
315
+ };
316
+
317
+ const uploadToCloud = async (data, options) => {
318
+ assert(client, 'Cloud storage has not been initialized.', 500);
319
+ const input = ensureString(options?.input, { case: 'UP' });
320
+ const file = await convert(data, {
321
+ input: options?.input, expected: FILE, errorMessage,
322
+ suffix: options?.suffix, ...options || {},
323
+ });
324
+ const raw = await client.upload(file, {
325
+ gzip: true, destination: options?.destination, metadata: defaultMetadata
326
+ });
327
+ const result = options?.raw ? raw : raw[0].metadata;
328
+ input !== FILE && await tryRm(file.path);
329
+ return result;
330
+ };
331
+
281
332
  export {
282
333
  _NEED,
283
334
  analyzeFile,
@@ -287,8 +338,10 @@ export {
287
338
  exists,
288
339
  getConfig,
289
340
  getConfigFilename,
341
+ getGcUrlByBucket,
290
342
  getTempPath,
291
343
  handleError,
344
+ init,
292
345
  isTextFile,
293
346
  legalFilename,
294
347
  mapFilename,
@@ -300,6 +353,7 @@ export {
300
353
  touchPath,
301
354
  tryRm,
302
355
  unzip,
356
+ uploadToCloud,
303
357
  writeFile,
304
358
  writeJson,
305
359
  writeTempFile,
package/lib/vision.mjs CHANGED
@@ -133,6 +133,24 @@ const read = async (image, options) => {
133
133
  return pages;
134
134
  };
135
135
 
136
+ const read2 = async (image, options) => {
137
+ // assert(client, 'Vision API has not been initialized.', 500);
138
+ // const content = await convert(image, {
139
+ // input: options?.input, expected: BASE64, errorMessage,
140
+ // });
141
+
142
+ // const [response] = await client.asyncBatchAnnotateFiles({
143
+ // requests: [{
144
+ // inputConfig: { mimeType: 'application/pdf', content },
145
+ // features: [{ type: 'DOCUMENT_TEXT_DETECTION' }],
146
+ // pages: [1, 2, 3, 4, 5], // max 5 pages
147
+ // }],
148
+ // });
149
+ // let pages = response.responses[0].responses;
150
+ // options?.raw || (pages = pages.map(x => x.fullTextAnnotation.text));
151
+ // return pages;
152
+ };
153
+
136
154
  export {
137
155
  _NEED,
138
156
  annotateImage,
@@ -141,5 +159,6 @@ export {
141
159
  ocrImageGoogle,
142
160
  ocrImageTesseract,
143
161
  read,
162
+ read2,
144
163
  see,
145
164
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1995.1.21",
4
+ "version": "1995.1.23",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
@@ -28,17 +28,18 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "file-type": "^18.7.0",
31
- "mathjs": "^12.0.0",
31
+ "mathjs": "^12.1.0",
32
32
  "uuid": "^9.0.1"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@ffmpeg-installer/ffmpeg": "^1.1.0",
36
36
  "@ffprobe-installer/ffprobe": "^2.1.2",
37
37
  "@google-cloud/speech": "^6.1.0",
38
+ "@google-cloud/storage": "^7.6.0",
38
39
  "@google-cloud/text-to-speech": "^5.0.1",
39
40
  "@google-cloud/vision": "^4.0.2",
40
41
  "@mozilla/readability": "^0.4.4",
41
- "@sentry/node": "^7.80.0",
42
+ "@sentry/node": "^7.80.1",
42
43
  "@waylaidwanderer/chatgpt-api": "^1.37.3",
43
44
  "acme-client": "^5.0.0",
44
45
  "browserify-fs": "^1.0.0",
@@ -55,7 +56,7 @@
55
56
  "node-mailjet": "^6.0.4",
56
57
  "node-polyfill-webpack-plugin": "^2.0.1",
57
58
  "office-text-extractor": "^3.0.2",
58
- "openai": "^4.17.4",
59
+ "openai": "^4.19.0",
59
60
  "ping": "^0.4.4",
60
61
  "say": "^0.16.0",
61
62
  "telegraf": "^4.15.0",