@jam-comments/server-utilities 4.3.1 → 4.3.2
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.
|
@@ -91,7 +91,12 @@ exports.makeMarkupRequest = makeMarkupRequest;
|
|
|
91
91
|
function markupFetcher(platform, fetchImplementation = fetch) {
|
|
92
92
|
return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = (0, utils_1.getEnvironment)(), }) => {
|
|
93
93
|
path = path || "/";
|
|
94
|
-
const savedFile = (
|
|
94
|
+
const savedFile = (() => {
|
|
95
|
+
if (!(0, utils_1.tempDirectoryExists)()) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return (0, utils_1.readFile)(path) || (0, utils_1.getEmptyMarkup)();
|
|
99
|
+
})();
|
|
95
100
|
const markup = savedFile
|
|
96
101
|
? savedFile
|
|
97
102
|
: await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment }, fetchImplementation, platform);
|
|
@@ -353,6 +353,76 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
|
|
|
353
353
|
apiKey: "123abc",
|
|
354
354
|
})).rejects.toThrowError(/request failed! Status code: 500/);
|
|
355
355
|
});
|
|
356
|
+
(0, vitest_1.describe)("using saved markup", () => {
|
|
357
|
+
(0, vitest_1.it)("makes fresh request when temp directory does not exist", async () => {
|
|
358
|
+
const tempDirectoryExistsSpy = vitest_1.vi
|
|
359
|
+
.spyOn(utilsExports, "tempDirectoryExists")
|
|
360
|
+
.mockReturnValue(false);
|
|
361
|
+
const readFileSpy = vitest_1.vi.spyOn(utilsExports, "readFile");
|
|
362
|
+
const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
|
|
363
|
+
return {
|
|
364
|
+
status: 200,
|
|
365
|
+
ok: true,
|
|
366
|
+
text: () => "results!",
|
|
367
|
+
};
|
|
368
|
+
});
|
|
369
|
+
const fetcher = markupFetcher("test", fetchMock);
|
|
370
|
+
const result = await fetcher({
|
|
371
|
+
path: "/test",
|
|
372
|
+
domain: "test.com",
|
|
373
|
+
apiKey: "123abc",
|
|
374
|
+
environment: "production",
|
|
375
|
+
});
|
|
376
|
+
(0, vitest_1.expect)(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
|
|
377
|
+
(0, vitest_1.expect)(readFileSpy).not.toHaveBeenCalled();
|
|
378
|
+
(0, vitest_1.expect)(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", vitest_1.expect.anything());
|
|
379
|
+
(0, vitest_1.expect)(result).toEqual("results!");
|
|
380
|
+
});
|
|
381
|
+
(0, vitest_1.it)("uses saved markup when it exists", async () => {
|
|
382
|
+
const tempDirectoryExistsSpy = vitest_1.vi
|
|
383
|
+
.spyOn(utilsExports, "tempDirectoryExists")
|
|
384
|
+
.mockReturnValue(true);
|
|
385
|
+
const readFileSpy = vitest_1.vi
|
|
386
|
+
.spyOn(utilsExports, "readFile")
|
|
387
|
+
.mockReturnValue("saved markup");
|
|
388
|
+
const fetchMock = vitest_1.vi.fn();
|
|
389
|
+
const fetcher = markupFetcher("test", fetchMock);
|
|
390
|
+
const result = await fetcher({
|
|
391
|
+
path: "/test",
|
|
392
|
+
domain: "test.com",
|
|
393
|
+
apiKey: "123abc",
|
|
394
|
+
environment: "production",
|
|
395
|
+
});
|
|
396
|
+
(0, vitest_1.expect)(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
|
|
397
|
+
(0, vitest_1.expect)(readFileSpy).toHaveBeenCalledOnce();
|
|
398
|
+
(0, vitest_1.expect)(fetchMock).not.toHaveBeenCalled();
|
|
399
|
+
(0, vitest_1.expect)(result).toEqual("saved markup");
|
|
400
|
+
});
|
|
401
|
+
(0, vitest_1.it)("uses the EMPTY template when there's no saved file for a post", async () => {
|
|
402
|
+
const tempDirectoryExistsSpy = vitest_1.vi
|
|
403
|
+
.spyOn(utilsExports, "tempDirectoryExists")
|
|
404
|
+
.mockReturnValue(true);
|
|
405
|
+
const readFileSpy = vitest_1.vi
|
|
406
|
+
.spyOn(utilsExports, "readFile")
|
|
407
|
+
.mockReturnValue(null);
|
|
408
|
+
const getEmptyMarkupSpy = vitest_1.vi
|
|
409
|
+
.spyOn(utilsExports, "getEmptyMarkup")
|
|
410
|
+
.mockReturnValue("<!-- EMPTY -->");
|
|
411
|
+
const fetchMock = vitest_1.vi.fn();
|
|
412
|
+
const fetcher = markupFetcher("test", fetchMock);
|
|
413
|
+
const result = await fetcher({
|
|
414
|
+
path: "/test",
|
|
415
|
+
domain: "test.com",
|
|
416
|
+
apiKey: "123abc",
|
|
417
|
+
environment: "production",
|
|
418
|
+
});
|
|
419
|
+
(0, vitest_1.expect)(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
|
|
420
|
+
(0, vitest_1.expect)(readFileSpy).toHaveBeenCalledOnce();
|
|
421
|
+
(0, vitest_1.expect)(getEmptyMarkupSpy).toHaveBeenCalledOnce();
|
|
422
|
+
(0, vitest_1.expect)(fetchMock).not.toHaveBeenCalled();
|
|
423
|
+
(0, vitest_1.expect)(result).toEqual("<!-- EMPTY -->");
|
|
424
|
+
});
|
|
425
|
+
});
|
|
356
426
|
(0, vitest_1.describe)("timezone validation", () => {
|
|
357
427
|
(0, vitest_1.it)("throws error when invalid timezone is provided", async () => {
|
|
358
428
|
const fetchImplMock = vitest_1.vi.fn();
|
package/dist/cjs/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.saveFile = exports.readFile = exports.toSavedFileName = exports.createTempDirectory = exports.deleteTempDirectory = exports.unescapeHTML = exports.parseJson = exports.reAppendMarkup = exports.getEnvironment = exports.isValidTimezone = void 0;
|
|
3
|
+
exports.saveFile = exports.tempDirectoryExists = exports.getEmptyMarkup = exports.readFile = exports.toSavedFileName = exports.createTempDirectory = exports.deleteTempDirectory = exports.unescapeHTML = exports.parseJson = exports.reAppendMarkup = exports.getEnvironment = exports.isValidTimezone = void 0;
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const _1 = require(".");
|
|
6
6
|
function isValidTimezone(tz) {
|
|
@@ -78,7 +78,7 @@ function deleteTempDirectory() {
|
|
|
78
78
|
}
|
|
79
79
|
exports.deleteTempDirectory = deleteTempDirectory;
|
|
80
80
|
function createTempDirectory(mkdirSyncImpl = fs_1.mkdirSync, existsSyncImpl = fs_1.existsSync) {
|
|
81
|
-
if (!existsSyncImpl
|
|
81
|
+
if (!tempDirectoryExists(existsSyncImpl)) {
|
|
82
82
|
mkdirSyncImpl(_1.TEMP_DIRECTORY);
|
|
83
83
|
}
|
|
84
84
|
}
|
|
@@ -97,6 +97,14 @@ function readFile(name, readFileSyncImpl = fs_1.readFileSync) {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
exports.readFile = readFile;
|
|
100
|
+
function getEmptyMarkup() {
|
|
101
|
+
return readFile("EMPTY");
|
|
102
|
+
}
|
|
103
|
+
exports.getEmptyMarkup = getEmptyMarkup;
|
|
104
|
+
function tempDirectoryExists(existsSyncImpl = fs_1.existsSync) {
|
|
105
|
+
return existsSyncImpl(_1.TEMP_DIRECTORY);
|
|
106
|
+
}
|
|
107
|
+
exports.tempDirectoryExists = tempDirectoryExists;
|
|
100
108
|
function saveFile(name, data, writeFileImpl = fs_1.writeFile) {
|
|
101
109
|
const fileName = toSavedFileName(name);
|
|
102
110
|
return new Promise((resolve) => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { injectSchema } from "./injectSchema";
|
|
2
|
-
import { createTempDirectory, deleteTempDirectory, getEnvironment, isValidTimezone, parseJson, readFile, saveFile, } from "./utils";
|
|
2
|
+
import { createTempDirectory, deleteTempDirectory, getEmptyMarkup, getEnvironment, isValidTimezone, parseJson, readFile, saveFile, tempDirectoryExists, } from "./utils";
|
|
3
3
|
export async function fetchAll({ tz = undefined, domain, apiKey, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
|
|
4
4
|
const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
|
|
5
5
|
createTempDirectory();
|
|
@@ -84,7 +84,12 @@ export async function makeMarkupRequest({ tz = undefined, path = undefined, doma
|
|
|
84
84
|
export function markupFetcher(platform, fetchImplementation = fetch) {
|
|
85
85
|
return async ({ tz = undefined, path, domain, apiKey, schema, baseUrl = "https://go.jamcomments.com", environment = getEnvironment(), }) => {
|
|
86
86
|
path = path || "/";
|
|
87
|
-
const savedFile =
|
|
87
|
+
const savedFile = (() => {
|
|
88
|
+
if (!tempDirectoryExists()) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
return readFile(path) || getEmptyMarkup();
|
|
92
|
+
})();
|
|
88
93
|
const markup = savedFile
|
|
89
94
|
? savedFile
|
|
90
95
|
: await fetchFreshMarkup({ tz, path, domain, apiKey, baseUrl, environment }, fetchImplementation, platform);
|
|
@@ -328,6 +328,76 @@ describe("markupFetcher", () => {
|
|
|
328
328
|
apiKey: "123abc",
|
|
329
329
|
})).rejects.toThrowError(/request failed! Status code: 500/);
|
|
330
330
|
});
|
|
331
|
+
describe("using saved markup", () => {
|
|
332
|
+
it("makes fresh request when temp directory does not exist", async () => {
|
|
333
|
+
const tempDirectoryExistsSpy = vi
|
|
334
|
+
.spyOn(utilsExports, "tempDirectoryExists")
|
|
335
|
+
.mockReturnValue(false);
|
|
336
|
+
const readFileSpy = vi.spyOn(utilsExports, "readFile");
|
|
337
|
+
const fetchMock = vi.fn().mockImplementation(() => {
|
|
338
|
+
return {
|
|
339
|
+
status: 200,
|
|
340
|
+
ok: true,
|
|
341
|
+
text: () => "results!",
|
|
342
|
+
};
|
|
343
|
+
});
|
|
344
|
+
const fetcher = markupFetcher("test", fetchMock);
|
|
345
|
+
const result = await fetcher({
|
|
346
|
+
path: "/test",
|
|
347
|
+
domain: "test.com",
|
|
348
|
+
apiKey: "123abc",
|
|
349
|
+
environment: "production",
|
|
350
|
+
});
|
|
351
|
+
expect(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
|
|
352
|
+
expect(readFileSpy).not.toHaveBeenCalled();
|
|
353
|
+
expect(fetchMock).toHaveBeenCalledWith("https://go.jamcomments.com/api/v3/markup?domain=test.com&path=%2Ftest", expect.anything());
|
|
354
|
+
expect(result).toEqual("results!");
|
|
355
|
+
});
|
|
356
|
+
it("uses saved markup when it exists", async () => {
|
|
357
|
+
const tempDirectoryExistsSpy = vi
|
|
358
|
+
.spyOn(utilsExports, "tempDirectoryExists")
|
|
359
|
+
.mockReturnValue(true);
|
|
360
|
+
const readFileSpy = vi
|
|
361
|
+
.spyOn(utilsExports, "readFile")
|
|
362
|
+
.mockReturnValue("saved markup");
|
|
363
|
+
const fetchMock = vi.fn();
|
|
364
|
+
const fetcher = markupFetcher("test", fetchMock);
|
|
365
|
+
const result = await fetcher({
|
|
366
|
+
path: "/test",
|
|
367
|
+
domain: "test.com",
|
|
368
|
+
apiKey: "123abc",
|
|
369
|
+
environment: "production",
|
|
370
|
+
});
|
|
371
|
+
expect(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
|
|
372
|
+
expect(readFileSpy).toHaveBeenCalledOnce();
|
|
373
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
374
|
+
expect(result).toEqual("saved markup");
|
|
375
|
+
});
|
|
376
|
+
it("uses the EMPTY template when there's no saved file for a post", async () => {
|
|
377
|
+
const tempDirectoryExistsSpy = vi
|
|
378
|
+
.spyOn(utilsExports, "tempDirectoryExists")
|
|
379
|
+
.mockReturnValue(true);
|
|
380
|
+
const readFileSpy = vi
|
|
381
|
+
.spyOn(utilsExports, "readFile")
|
|
382
|
+
.mockReturnValue(null);
|
|
383
|
+
const getEmptyMarkupSpy = vi
|
|
384
|
+
.spyOn(utilsExports, "getEmptyMarkup")
|
|
385
|
+
.mockReturnValue("<!-- EMPTY -->");
|
|
386
|
+
const fetchMock = vi.fn();
|
|
387
|
+
const fetcher = markupFetcher("test", fetchMock);
|
|
388
|
+
const result = await fetcher({
|
|
389
|
+
path: "/test",
|
|
390
|
+
domain: "test.com",
|
|
391
|
+
apiKey: "123abc",
|
|
392
|
+
environment: "production",
|
|
393
|
+
});
|
|
394
|
+
expect(tempDirectoryExistsSpy).toHaveBeenCalledOnce();
|
|
395
|
+
expect(readFileSpy).toHaveBeenCalledOnce();
|
|
396
|
+
expect(getEmptyMarkupSpy).toHaveBeenCalledOnce();
|
|
397
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
398
|
+
expect(result).toEqual("<!-- EMPTY -->");
|
|
399
|
+
});
|
|
400
|
+
});
|
|
331
401
|
describe("timezone validation", () => {
|
|
332
402
|
it("throws error when invalid timezone is provided", async () => {
|
|
333
403
|
const fetchImplMock = vi.fn();
|
package/dist/esm/utils.js
CHANGED
|
@@ -69,7 +69,7 @@ export function deleteTempDirectory() {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
export function createTempDirectory(mkdirSyncImpl = mkdirSync, existsSyncImpl = existsSync) {
|
|
72
|
-
if (!existsSyncImpl
|
|
72
|
+
if (!tempDirectoryExists(existsSyncImpl)) {
|
|
73
73
|
mkdirSyncImpl(TEMP_DIRECTORY);
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -85,6 +85,12 @@ export function readFile(name, readFileSyncImpl = readFileSync) {
|
|
|
85
85
|
return null;
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
|
+
export function getEmptyMarkup() {
|
|
89
|
+
return readFile("EMPTY");
|
|
90
|
+
}
|
|
91
|
+
export function tempDirectoryExists(existsSyncImpl = existsSync) {
|
|
92
|
+
return existsSyncImpl(TEMP_DIRECTORY);
|
|
93
|
+
}
|
|
88
94
|
export function saveFile(name, data, writeFileImpl = writeFile) {
|
|
89
95
|
const fileName = toSavedFileName(name);
|
|
90
96
|
return new Promise((resolve) => {
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -9,4 +9,6 @@ export declare function deleteTempDirectory(): void;
|
|
|
9
9
|
export declare function createTempDirectory(mkdirSyncImpl?: typeof mkdirSync, existsSyncImpl?: typeof existsSync): void;
|
|
10
10
|
export declare function toSavedFileName(name: string): string;
|
|
11
11
|
export declare function readFile(name: any, readFileSyncImpl?: typeof readFileSync): string;
|
|
12
|
+
export declare function getEmptyMarkup(): string;
|
|
13
|
+
export declare function tempDirectoryExists(existsSyncImpl?: typeof existsSync): boolean;
|
|
12
14
|
export declare function saveFile(name: string, data: string, writeFileImpl?: any): Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jam-comments/server-utilities",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.2",
|
|
4
4
|
"description": "Various JavaScript utilities for JamComments.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"homepage": "https://jamcomments.com",
|
|
22
22
|
"license": "GPL-2.0",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@types/node": "^20.12.
|
|
24
|
+
"@types/node": "^20.12.12",
|
|
25
25
|
"prettier": "^3.2.5",
|
|
26
26
|
"typescript": "^5.4.5",
|
|
27
27
|
"vite": "^5.2.11",
|