-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
util.ts
1278 lines (1214 loc) · 32.1 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
Content,
DateTimeFormatter,
fs,
kebabCase,
path,
posixPath,
Root,
slug as slugFn,
titleCase as titleCaseFn,
toMarkdown,
u,
YAML,
} from "./deps.ts";
import log from "./log.ts";
import {
BaseFeed,
Config,
CustomRequestOptions,
DayInfo,
DBIndex,
DBMeta,
ExpiredValue,
FileConfig,
FileConfigInfo,
FileInfo,
Item,
ItemDetail,
Nav,
Pagination,
PaginationInfo,
ParsedFilename,
ParsedItemsFilePath,
RawConfig,
RawSource,
RawSourceFile,
RawSourceFileWithType,
Source,
WeekOfYear,
} from "./interface.ts";
import {
CONTENT_DIR,
DEFAULT_CATEGORY,
DEV_DOMAIN,
INDEX_MARKDOWN_PATH,
PROD_DOMAIN,
} from "./constant.ts";
import { NotFound } from "./error.ts";
import { underline } from "https://deno.land/std@0.158.0/fmt/colors.ts";
export const SECOND = 1e3;
export const MINUTE = SECOND * 60;
export const HOUR = MINUTE * 60;
export const DAY = HOUR * 24;
export const WEEK = DAY * 7;
const DAYS_PER_WEEK = 7;
enum Day {
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat,
}
export const getDayNumber = (date: Date): number => {
return Number(
`${getFullYear(date)}${(getFullMonth(date))}${(getFullDay(date))}`,
);
};
export const getWeekNumber = (date: Date): number => {
return weekOfYear(date).number;
};
export const parseDayInfo = (day: number): DayInfo => {
const year = Math.floor(day / 10000);
const month = Math.floor(day / 100) % 100;
const dayNumber = day % 100;
const date = new Date(Date.UTC(year, month - 1, dayNumber));
const localeDate = new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "short",
day: "2-digit",
timeZone: "UTC",
}).format(date);
return {
year,
id: `${year}-${addZero(month)}-${addZero(dayNumber)}`,
name: localeDate,
month,
day: dayNumber,
path: `${year}/${addZero(month)}/${addZero(dayNumber)}`,
number: day,
date: date,
};
};
export function startDateOfWeek(date: Date, start_day = 1): Date {
// Returns the start of the week containing a 'date'. Monday 00:00 UTC is
// considered to be the boundary between adjacent weeks, unless 'start_day' is
// specified. A Date object is returned.
date = new Date(date.getTime());
const day_of_month = date.getUTCDate();
const day_of_week = date.getUTCDay();
const difference_in_days = day_of_week >= start_day
? day_of_week - start_day
: day_of_week - start_day + 7;
date.setUTCDate(day_of_month - difference_in_days);
date.setUTCHours(0);
date.setUTCMinutes(0);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return date;
}
export const parseWeekInfo = (week: number): WeekOfYear => {
// check week number 5 or 6 digit
// split by year and week
let year = Math.floor(week / 100);
let weekOfYear = week % 100;
if (week < 100000) {
// week add 0
year = Math.floor(week / 10);
weekOfYear = week % 10;
}
// week to date
const date = weekNumberToDate(week);
return {
year,
week: weekOfYear,
number: week,
path: `${year}/${weekOfYear}`,
id: `${year}-${weekOfYear}`,
name: weekToRange(week),
date,
};
};
export function weekToRange(weekNumber: number): string {
let year = Math.floor(weekNumber / 100);
let week = weekNumber % 100;
if (weekNumber < 100000) {
// week add 0
year = Math.floor(weekNumber / 10);
week = weekNumber % 10;
}
// Get first day of year
const yearStart = new Date(Date.UTC(year, 0, 1));
// year start monday date
const yearStartMondayDate = startDateOfWeek(yearStart);
const yearStartMondayFullYear = yearStartMondayDate.getUTCFullYear();
let yearFirstWeekMonday = yearStartMondayDate;
if (yearStartMondayFullYear !== year) {
// then year first week monday is next +7
yearFirstWeekMonday = new Date(yearStartMondayDate.getTime() + WEEK);
}
const weekMonday = yearFirstWeekMonday.getTime() + WEEK * (week - 1);
const weekSunday = weekMonday + WEEK - 1;
const weekStartYear = new Date(weekMonday).getUTCFullYear();
const start = new Intl.DateTimeFormat("en-US", {
month: "short",
day: "2-digit",
timeZone: "UTC",
}).format(weekMonday);
const end = new Intl.DateTimeFormat("en-US", {
month: "short",
day: "2-digit",
timeZone: "UTC",
}).format(weekSunday);
return `${start} - ${end}, ${weekStartYear}`;
}
export function weekNumberToDate(weekNumber: number): Date {
const year = Math.floor(weekNumber / 100);
const week = weekNumber % 100;
// Get first day of year
const yearStart = new Date(Date.UTC(year, 0, 1));
// year start monday date
const yearStartMondayDate = startDateOfWeek(yearStart);
const yearStartMondayFullYear = yearStartMondayDate.getUTCFullYear();
let yearFirstWeekMonday = yearStartMondayDate;
if (yearStartMondayFullYear !== year) {
// then year first week monday is next +7
yearFirstWeekMonday = new Date(yearStartMondayDate.getTime() + WEEK);
}
const weekMonday = yearFirstWeekMonday.getTime() + WEEK * (week - 1);
const weekSunday = weekMonday + WEEK - 1;
return new Date(weekMonday);
}
export function weekOfYear(date: Date): WeekOfYear {
const workingDate = new Date(
Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),
);
const day = workingDate.getUTCDay();
const nearestThursday = workingDate.getUTCDate() +
Day.Thu -
(day === Day.Sun ? DAYS_PER_WEEK : day);
workingDate.setUTCDate(nearestThursday);
// Get first day of year
const yearStart = new Date(Date.UTC(workingDate.getUTCFullYear(), 0, 1));
const weekYear = workingDate.getUTCFullYear();
// return the calculated full weeks to nearest Thursday
const week = Math.ceil(
(workingDate.getTime() - yearStart.getTime() + DAY) / WEEK,
);
return {
year: weekYear,
week: week,
path: `${workingDate.getUTCFullYear()}/${week}`,
number: Number(`${weekYear}${addZero(week)}`),
date: weekNumberToDate(Number(`${weekYear}${addZero(week)}`)),
id: `${weekYear}-${week}`,
name: weekToRange(week),
};
}
export const addZero = function (num: number): string {
if (num < 10) {
return "0" + num;
} else {
return "" + num;
}
};
// this function is used to get the config from the config file
//
// and parse it to the right format
// return the max value of the array
export const defaultFileType = "list";
// check is dev
export function isDev() {
return Deno.env.get("PROD") !== "1";
}
export function getDomain() {
return isDev() ? DEV_DOMAIN : PROD_DOMAIN;
}
export function isUseCache() {
return true;
// return Deno.env.get("CACHE") === "1";
}
export function isMock() {
if (isDev()) {
return (Deno.env.get("MOCK") !== "0");
} else {
return (Deno.env.get("MOCK") === "1");
}
}
export function getRepoHTMLURL(
url: string,
defaultBranch: string,
file: string,
): string {
return `${url}/blob/${defaultBranch}/${file}`;
}
export function getCachePath(isDb: boolean) {
if (isDb) {
return path.join(getDbPath(), "cache");
}
return path.join(Deno.cwd(), "cache");
}
export async function getConfig(): Promise<Config> {
const rawConfig = YAML.parse(
await Deno.readTextFile("config.yml"),
) as RawConfig;
if (!rawConfig.file_min_updated_hours) {
rawConfig.file_min_updated_hours = 12;
}
const config: Config = {
...rawConfig,
sources: {},
};
for (const key of Object.keys(rawConfig.sources)) {
const value = rawConfig.sources[key];
config.sources[key] = getFormatedSource(key, value);
}
return config;
}
// https://github.com/markedjs/marked/blob/master/src/Slugger.js
export function slugy(value: string): string {
return value
.toLowerCase()
.trim()
// remove html tags
.replace(/<[!\/a-z].*?>/ig, "")
// remove unwanted chars
.replace(
/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g,
"",
)
.replace(/\s/g, "-");
}
export function getIndexFileConfig(
filesConfig: Record<string, FileConfig>,
): FileConfig {
const keys = Object.keys(filesConfig);
for (const key of keys) {
const fileConfig = filesConfig[key];
if (fileConfig.index) {
return fileConfig;
}
}
return filesConfig[keys[0]];
}
export function getAllSourceCategories(config: Config): string[] {
const sources = config.sources;
const sourcesKeys = Object.keys(sources);
const categories: string[] = [];
for (const sourceKey of sourcesKeys) {
const source = sources[sourceKey];
if (!categories.includes(source.category)) {
categories.push(source.category);
}
}
return categories;
}
export function titleCase(str: string): string {
// replace - to space
return titleCaseFn(str.replace(/-/g, " "));
}
export function getFormatedSource(
key: string,
value: null | RawSource | undefined,
): Source {
let url = `https://github.com/${key}`;
const repo = key;
// split repo owner and repo name
let defaultName = titleCase(key.split("/")[1]);
let name = defaultName;
let files: Record<string, FileConfig> = {};
if (value) {
if (value.url) {
url = value.url;
}
if (value.files) {
const keys = Object.keys(value.files);
for (const fileKey of keys) {
let fileConfig: RawSourceFileWithType;
if (typeof value.files === "string") {
fileConfig = formatFileConfigValue(value.files);
} else if (value.files) {
fileConfig = formatFileConfigValue(value.files[fileKey]);
} else {
fileConfig = formatFileConfigValue();
}
if (keys.length === 1) {
fileConfig.index = true;
}
if (fileConfig.name) {
name = fileConfig.name;
} else {
if (fileConfig.index) {
name = defaultName;
} else {
name = `${defaultName} (${fileKey})`;
}
}
files[fileKey] = {
...fileConfig,
filepath: fileKey,
pathname: `/${key}/${
fileConfig.index ? "" : removeExtname(fileKey) +
"/"
}`,
name: name,
};
}
// check is has index file
let isHasIndex = false;
for (const rawFileKey of Object.keys(files)) {
if (files[rawFileKey].index) {
isHasIndex = true;
break;
}
}
if (!isHasIndex) {
throw new Error(`source ${key} has no index file`);
}
} else {
files = {
[INDEX_MARKDOWN_PATH]: {
filepath: INDEX_MARKDOWN_PATH,
pathname: `/${key}/`,
name,
index: true,
options: {
type: defaultFileType,
},
},
};
}
} else {
// todo
files = {
[INDEX_MARKDOWN_PATH]: {
filepath: INDEX_MARKDOWN_PATH,
pathname: `/${key}/`,
name,
index: true,
options: {
type: defaultFileType,
},
},
};
}
const defaultCategory = DEFAULT_CATEGORY;
const sourceConfig: Source = {
identifier: key,
url,
files,
category: value?.category || defaultCategory,
};
if (value && value.default_branch) {
sourceConfig.default_branch = value.default_branch;
}
return sourceConfig;
}
// function for format file config value
function formatFileConfigValue(
fileValue?: string | RawSourceFile | null,
): RawSourceFileWithType {
if (!fileValue) {
return {
options: { type: defaultFileType },
};
} else if (typeof fileValue === "string") {
return {
options: { type: defaultFileType },
};
} else {
return {
...fileValue,
options: { type: defaultFileType, ...fileValue.options },
};
}
}
export function getCurrentPath() {
if (isDev()) {
return "dev-current";
} else {
return "current";
}
}
export function getDistPath() {
if (isDev()) {
return "dist";
} else {
return "prod-dist";
}
}
export function getPublicPath() {
return path.join(getDbPath(), "public");
}
export function getDistRepoPath() {
return path.join(getDistPath());
}
export function getDistRepoContentPath() {
return path.join(getDistPath(), CONTENT_DIR);
}
export function getStaticPath() {
return "static";
}
export function getDistRepoGitUrl() {
const envRepo = Deno.env.get("DIST_REPO");
if (envRepo) {
return envRepo;
} else {
return `git@github.com:trackawesomelist/trackawesomelist.git`;
}
}
export function getDataRawPath() {
return posixPath.join(getCurrentPath(), "1-raw");
}
export function getDbPath() {
if (isDev()) {
return "db";
} else {
return "prod-db";
}
}
export function getSqlitePath() {
return path.join(getDbPath(), "sqlite.db");
}
export function getDataItemsPath() {
return posixPath.join(getDbPath(), "items");
}
export async function walkFile(path: string) {
// ensure path exists
await fs.ensureDir(path);
// check path exist
return fs.walk(path, {
includeDirs: false,
});
}
export async function walkJSON(path: string) {
await fs.ensureDir(path);
return fs.walk(path, {
includeDirs: false,
exts: [".json"],
});
}
export function writeTextFile(path: string, content: string) {
return fs.ensureFile(path).then(() => {
return Deno.writeTextFile(path, content);
});
}
export function readTextFile(path: string) {
return Deno.readTextFile(path);
}
export async function readJSONFile<T>(path: string): Promise<T> {
return JSON.parse(await readTextFile(path));
}
export function parseItemsFilepath(filepath: string): ParsedItemsFilePath {
const relativePath = path.relative(getDataItemsPath(), filepath);
const splited = relativePath.split(path.sep);
const sourceIdentifier = splited[0] + "/" + splited[1];
const repoRelativeFilename = splited.slice(2).join(path.sep);
const originalFilepath = repoRelativeFilename.slice(0, -5);
return {
sourceIdentifier,
originalFilepath,
};
}
export function parseFilename(filename: string): ParsedFilename {
const filenameWithoutExtname = path.basename(
filename,
path.extname(filename),
);
const splited = filenameWithoutExtname.split("_");
const type = splited[0];
return {
name: splited.slice(1).join("_"),
ext: path.extname(filename),
type,
};
}
export function childrenToRoot(children: Content[]): Root {
return u("root", children);
}
export function childrenToMarkdown(children: Content[]): string {
return toMarkdown(childrenToRoot(children));
}
export async function sha1(message: string) {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hash = await crypto.subtle.digest("SHA-1", data);
const hashArray = Array.from(new Uint8Array(hash)); // convert buffer to byte array
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(
"",
); // convert bytes to hex string
return hashHex;
}
export async function exists(filename: string): Promise<boolean> {
try {
await Deno.stat(filename);
// successful, file or directory must exist
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// file or directory does not exist
return false;
} else {
// unexpected error, maybe permissions, pass it along
throw error;
}
}
}
export function removeExtname(filename: string) {
const extname = path.extname(filename);
return filename.slice(0, -extname.length);
}
export function getItemsFilePath(identifier: string, file: string) {
const itemsFilesPath = path.join(
getDataItemsPath(),
identifier,
file + ".json",
);
return itemsFilesPath;
}
export function getDbMetaFilePath() {
const dbMetaFilePath = path.join(
getDbPath(),
"meta.json",
);
return dbMetaFilePath;
}
export function getDbIndexFilePath() {
const dbMetaFilePath = path.join(
getDbPath(),
"index.json",
);
return dbMetaFilePath;
}
export async function getDbMeta(): Promise<DBMeta> {
// first check local
const dbMetaFilePath = getDbMetaFilePath();
const dbMeta = await readJSONFile(dbMetaFilePath) as DBMeta;
return dbMeta;
}
export async function getDbCachedStars(): Promise<
Record<string, ExpiredValue>
> {
// first check local
const dbMetaFilePath = getDbStarsPath();
try {
const dbMeta = await readJSONFile(dbMetaFilePath) as Record<
string,
ExpiredValue
>;
return dbMeta;
} catch (_e) {
return {};
}
}
export function writeDbCachedStars(stars: Record<string, ExpiredValue>) {
const dbMetaFilePath = getDbStarsPath();
return writeJSONFile(dbMetaFilePath, stars);
}
export async function getDbIndex(): Promise<DBIndex> {
// first check local
const dbMetaFilePath = getDbIndexFilePath();
const dbMeta = await readJSONFile(dbMetaFilePath) as DBIndex;
return dbMeta;
}
export async function writeDbMeta(dbMeta: DBMeta): Promise<void> {
// first check local
const dbMetaFilePath = getDbMetaFilePath();
await writeJSONFile(dbMetaFilePath, dbMeta);
}
export async function writeDbIndex(dbIndex: DBIndex): Promise<void> {
// first check local
const dbIndexFilePath = getDbIndexFilePath();
await writeJSONFile(dbIndexFilePath, dbIndex);
}
export function getRemoteData<T>(_file: string): T {
throw new Error("not implemented");
// return {
// sources: {},
// };
}
export async function writeJSONFile(filePath: string, data: unknown) {
const file = JSON.stringify(data, null, 2);
// ensure dir exists
const dir = path.dirname(filePath);
await fs.ensureDir(dir);
await Deno.writeTextFile(filePath, file + "\n");
}
export function getFullYear(date: Date): string {
return date.getUTCFullYear().toString();
}
export function getFullMonth(date: Date): string {
const month = date.getUTCMonth() + 1;
return month < 10 ? `0${month}` : month.toString();
}
export function getFullDay(date: Date): string {
const day = date.getUTCDate();
return day < 10 ? `0${day}` : day.toString();
}
export function getUTCDay(date: Date): string {
return `${getFullYear(date)}-${getFullMonth(date)}-${getFullDay(date)}`;
}
export function urlToFilePath(url: string): string {
const urlObj = new URL(url);
const pathname = urlObj.pathname;
return pathnameToFilePath(pathname);
}
export function pathnameToFilePath(pathname: string): string {
// is ends with /
const basename = posixPath.basename(pathname);
if (pathname.endsWith("/") || !basename.includes(".")) {
return posixPath.join(
"/",
CONTENT_DIR,
pathname.slice(1),
INDEX_MARKDOWN_PATH,
);
} else if (pathname.endsWith(".md")) {
return posixPath.join("/", CONTENT_DIR, pathname.slice(1));
} else {
return pathname;
}
}
export function pathnameToWeekFilePath(pathname: string): string {
return posixPath.join(
"/",
CONTENT_DIR,
pathname.slice(1),
"week",
INDEX_MARKDOWN_PATH,
);
}
export function pathnameToOverviewFilePath(pathname: string): string {
return posixPath.join(
"/",
CONTENT_DIR,
pathname.slice(1),
"readme",
INDEX_MARKDOWN_PATH,
);
}
export function pathnameToFeedUrl(pathname: string, isDay: boolean): string {
const domain = getDomain();
return domain + posixPath.join(pathname, isDay ? "" : "week", "rss.xml");
}
export function pathnameToUrl(pathname: string): string {
const domain = getDomain();
return domain + pathname;
}
export async function got(
url: string,
init: RequestInit = {},
): Promise<string> {
const c = new AbortController();
const id = setTimeout(() => c.abort(), 30000);
const headers = new Headers(init.headers);
headers.set(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; rv:105.0) Gecko/20100101 Firefox/105.0",
);
const params: RequestInit = {
...init,
headers,
signal: c.signal,
};
const r = await fetch(url, params);
clearTimeout(id);
if (r.ok) {
return r.text();
} else {
throw new Error(`fetch ${url} failed with status ${r.status}`);
}
}
export function getCachedFolder(
url: string,
method: string,
isDb: boolean,
): string {
const urlObj = new URL(url);
const host = urlObj.host;
const pathname = urlObj.pathname;
const params = urlObj.searchParams;
const cacheFileFolder = path.join(
getCachePath(isDb),
"http",
encodeURIComponent(host),
method,
pathname,
encodeURIComponent(params.toString()),
);
return cacheFileFolder;
}
export function getCachedFileInfo(
url: string,
method: string,
isDb: boolean,
expired: number,
): string[] {
return [getCachedFolder(url, method, isDb), (Date.now() + expired) + ".txt"];
}
export async function writeCacheFile(
url: string,
method: string,
body: string,
isDb: boolean,
expired?: number,
) {
expired = expired || 1000 * 60 * 60 * 24 * 3;
if (isDev()) {
expired = expired || 1000 * 60 * 60 * 24 * 30;
}
const [cacheFileFolder, cacheFilePath] = getCachedFileInfo(
url,
method,
isDb,
expired,
);
await writeTextFile(path.join(cacheFileFolder, cacheFilePath), body);
return body;
}
export async function readCachedFile(
url: string,
method: string,
isDb: boolean,
): Promise<string> {
// check folder is exists
const cachedFolder = getCachedFolder(url, method, isDb);
for await (const file of Deno.readDir(cachedFolder)) {
if (file.isFile && file.name.endsWith(".txt")) {
// check is expired
const expired = parseInt(file.name.slice(0, -4));
const filepath = path.join(cachedFolder, file.name);
if (Date.now() - expired < 0) {
// not expired
return readTextFile(filepath);
} else {
// expired
await Deno.remove(filepath);
}
}
}
throw new NotFound("cached file is expired");
}
export async function gotWithDbCache(
url: string,
init: RequestInit,
options?: CustomRequestOptions,
): Promise<string> {
// check is exists cache
let cacheFileContent;
try {
cacheFileContent = await readCachedFile(url, init?.method ?? "GET", true);
log.debug(`use cache file for ${url}`);
} catch (e) {
if (e.name === "NotFound") {
// ignore
log.debug(`not found cache file for ${url}`);
} else {
throw e;
}
}
if (cacheFileContent !== undefined) {
return cacheFileContent;
}
const responseText = await got(url, init);
const expires = options?.expires ?? 15 * 24 * 60 * 60 * 1000;
await writeCacheFile(
url,
init?.method ?? "GET",
responseText,
true,
expires,
);
return responseText;
}
export async function gotWithCache(
url: string,
init: RequestInit,
options?: CustomRequestOptions,
): Promise<string> {
// check is exists cache
let cacheFileContent;
try {
cacheFileContent = await readCachedFile(url, init?.method ?? "GET", false);
log.debug(`use cache file for ${url}`);
} catch (e) {
if (e.name === "NotFound") {
// ignore
log.debug(`not found cache file for ${url}`);
} else {
throw e;
}
}
if (cacheFileContent !== undefined) {
return cacheFileContent;
}
const responseText = await got(url, init);
const expires = options?.expires ?? 3 * 24 * 60 * 60 * 1000;
await writeCacheFile(
url,
init?.method ?? "GET",
responseText,
false,
expires,
);
return responseText;
}
export async function gotGithubStar(
owner: string,
repo: string,
dbCachedStars: Record<string, ExpiredValue>,
): Promise<string> {
// check is there is any cache
const key = `${owner}/${repo}`;
const cached = getDbExpiredItem(dbCachedStars, key);
if (cached !== undefined) {
return cached;
}
const url = `https://img.shields.io/github/stars/${key}`;
const response = await gotWithCache(url, {});
const endWith = "</text></a></g></svg>";
if (response.endsWith(endWith)) {
const text = response.slice(0, -endWith.length);
const start = text.lastIndexOf(">") + 1;
const star = text.slice(start);
// write to cache
writeDbExpiredItem(
dbCachedStars,
key,
star,
15 * 24 * 60 * 60 * 1000,
);
return star;
} else {
log.debug(`got github star failed for ${owner}/${repo}`);
return "";
}
// parse svg got start count
}
export async function promiseLimit<T>(
funcs: (() => Promise<T>)[],
limit = 100,
): Promise<T[]> {
let results: T[] = [];
while (funcs.length) {
// 100 at a time
results = [
...results,
...await Promise.all(funcs.splice(0, limit).map((f) => f())),
];
log.debug(`promise limit ${funcs.length} left`);
}
return results;
}
export const formatUTC = (date: Date, formatString: string) => {
date = new Date(date.getTime() + 0 * 60 * 60 * 1000);
const formatter = new DateTimeFormatter(formatString);
return formatter.format(date, {
timeZone: "UTC",
});
};
export const formatHumanTime = (date: Date) => {
const now = new Date();
const nowYear = formatUTC(now, "yyyy");
const dateYear = formatUTC(date, "yyyy");
const isThisYear = nowYear === dateYear;
if (isThisYear) {
return formatUTC(date, "MM/dd");
} else {
return formatUTC(date, "yy/MM/dd");
}
};
export const formatNumber = (num: number): string => {
const formatter = Intl.NumberFormat("en", { notation: "compact" });
return formatter.format(num);
};
export function getBaseFeed(): BaseFeed {
const domain = getDomain();
return {
version: "https://jsonfeed.org/version/1",
icon: `${domain}/icon.png`,
favicon: `${domain}/favicon.ico`,
language: "en",
};
}
export const slug = function (tag: string): string {
// @ts-ignore: npm module
return slugFn(kebabCase(tag));
};
export function formatPaginationHtml(
page: PaginationInfo,
currentPathname: string,
): string {
let text = "";
const isWeek = currentPathname.endsWith("/week/");
if (page.prev || page.next) {