Skip to content

Commit b985ca0

Browse files
committed
1.1.0
1 parent 4aab4e6 commit b985ca0

5 files changed

Lines changed: 115 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grandlinex/core",
3-
"version": "1.1.0-alpha.0",
3+
"version": "1.1.0",
44
"description": "Core module",
55
"type": "module",
66
"exports": {

src/classes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import CoreDBUpdate from './CoreDBUpdate.js';
1717
import CoreBundleModule from './CoreBundleModule.js';
1818
import CoreTriggerService from './CoreTriggerService.js';
1919
import CoreCachedFc from './CoreCachedFc.js';
20+
import CoreKernelExtension from './CoreKernelExtension.js';
2021
import CMap from './CoreMap.js';
2122
import CoreTimeCache, { CachedData } from './CoreTimeCache.js';
2223

@@ -44,6 +45,7 @@ export {
4445
CMap,
4546
CoreTimeCache,
4647
CachedData,
48+
CoreKernelExtension,
4749
};
4850

4951
export * from './annotation/index.js';

src/utils/Type.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default class Type {
2+
static isUUID(str: string): boolean {
3+
const uidReg =
4+
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/;
5+
return uidReg.test(str);
6+
}
7+
8+
static isIsoDate(data: string): boolean {
9+
return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2})?$/.test(
10+
data,
11+
);
12+
}
13+
14+
static isString(a: unknown): a is string {
15+
return typeof a === 'string' && a.length > 0;
16+
}
17+
18+
static isNumber(a: unknown): a is number {
19+
return typeof a === 'number';
20+
}
21+
22+
static isBoolean(a: unknown): a is boolean {
23+
return typeof a === 'boolean';
24+
}
25+
}

src/utils/XUtil.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import fs from 'fs';
22
import * as Path from 'path';
3+
import https from 'https';
4+
import http from 'http';
35
import { ColumnProps, getEntityMeta, IEntity } from '../classes/index.js';
46
import CoreEntity from '../classes/CoreEntity.js';
57
import { Executable, ExecutableOptions } from './Executable.js';
@@ -15,6 +17,42 @@ export type XExecResult = {
1517
stdout: string;
1618
stderr: string;
1719
};
20+
export const MineTypeMap = new Map<string, string>([
21+
['text/html; charset=utf-8', 'html'],
22+
['text/html', 'html'],
23+
['text/plain', 'txt'],
24+
['video/mp4', 'mp4'],
25+
['video/mpeg', 'mpeg'],
26+
['video/webm', 'webm'],
27+
['audio/webm', 'weba'],
28+
['audio/mpeg', 'mp3'],
29+
['audio/wav', 'wav'],
30+
['audio/aac', 'aac'],
31+
['audio/webm', 'aac'],
32+
['audio/opus', 'opus'],
33+
['image/webp', 'webp'],
34+
['image/png', 'png'],
35+
['image/jpeg', 'jpg'],
36+
['image/gif', 'gif'],
37+
['image/webp', 'webp'],
38+
['image/svg+xml', 'svg'],
39+
['application/pdf', 'pdf'],
40+
['application/zip', 'zip'],
41+
['application/x-rar-compressed', 'rar'],
42+
['application/x-7z-compressed', '7z'],
43+
['application/x-tar', 'tar'],
44+
['application/x-bzip2', 'bz2'],
45+
['application/x-gzip', 'gz'],
46+
['application/json; charset=utf-8', 'json'],
47+
]);
48+
49+
function selectClient(url: string) {
50+
if (url.startsWith('https')) {
51+
return https;
52+
}
53+
return http;
54+
}
55+
1856
export class XUtil {
1957
/*
2058
* TIME
@@ -206,4 +244,49 @@ export class XUtil {
206244
const exe = new Executable(cmd, options);
207245
return exe.run(args);
208246
}
247+
248+
static downloadFile(
249+
url: string,
250+
path: string,
251+
extMap = MineTypeMap,
252+
): Promise<{
253+
name: string;
254+
size: number;
255+
type: string;
256+
} | null> {
257+
return new Promise((resolve) => {
258+
try {
259+
selectClient(url)
260+
.get(url, (res) => {
261+
const raw = extMap.get(res.headers['content-type'] as string);
262+
const ext = raw || 'blob';
263+
const fName = `${path}.${ext}`;
264+
const file = fs.createWriteStream(fName);
265+
// A chunk of data has been received.
266+
res.on('data', (chunk) => {
267+
file.write(chunk);
268+
});
269+
270+
// The whole response has been received. Print out the result.
271+
res.on('end', () => {
272+
const cl = res.headers['content-length'];
273+
const cli = parseInt(cl as string, 10);
274+
file.close();
275+
resolve({
276+
size: cli,
277+
name: Path.basename(fName),
278+
type: res.headers['content-type'] || '',
279+
});
280+
});
281+
})
282+
.on('error', (err) => {
283+
console.error(`Error: ${err.message}`);
284+
resolve(null);
285+
});
286+
} catch (e) {
287+
console.error(e);
288+
resolve(null);
289+
}
290+
});
291+
}
209292
}

src/utils/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import Type from './Type.js';
2+
13
export * from './EntityValidator.js';
24
export * from './Executable.js';
35
export * from './XUtil.js';
6+
7+
export { Type };

0 commit comments

Comments
 (0)