@jam-comments/server-utilities 5.6.0 → 5.8.0

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.
package/dist/cjs/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.logError = exports.log = exports.fetchAll = exports.markupFetcher = void 0;
3
+ exports.logError = exports.log = exports.removeFalseyValues = exports.fetchAll = exports.markupFetcher = void 0;
4
4
  var markupFetcher_1 = require("./markupFetcher");
5
5
  Object.defineProperty(exports, "markupFetcher", { enumerable: true, get: function () { return markupFetcher_1.markupFetcher; } });
6
6
  Object.defineProperty(exports, "fetchAll", { enumerable: true, get: function () { return markupFetcher_1.fetchAll; } });
7
+ var utils_1 = require("./utils");
8
+ Object.defineProperty(exports, "removeFalseyValues", { enumerable: true, get: function () { return utils_1.removeFalseyValues; } });
7
9
  var log_1 = require("./log");
8
10
  Object.defineProperty(exports, "log", { enumerable: true, get: function () { return log_1.log; } });
9
11
  Object.defineProperty(exports, "logError", { enumerable: true, get: function () { return log_1.logError; } });
@@ -1,26 +1,6 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
4
  };
25
5
  Object.defineProperty(exports, "__esModule", { value: true });
26
6
  exports.fetchAll = fetchAll;
@@ -30,11 +10,11 @@ exports.makeMarkupRequest = makeMarkupRequest;
30
10
  exports.markupFetcher = markupFetcher;
31
11
  const injectSchema_1 = require("./injectSchema");
32
12
  const utils_1 = require("./utils");
13
+ const store_1 = __importDefault(require("./store"));
33
14
  const BASE_URL = "https://go.jamcomments.com";
34
15
  async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
35
- const { createTempDirectory, deleteTempDirectory, saveFile } = await Promise.resolve().then(() => __importStar(require("./nodeUtils")));
36
16
  const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
37
- await createTempDirectory();
17
+ store_1.default.init();
38
18
  let shouldKeepFetching = true;
39
19
  let page = 1;
40
20
  console.log("Fetching comments from JamComments! This might take a sec.");
@@ -51,10 +31,9 @@ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey
51
31
  copy,
52
32
  });
53
33
  console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
54
- const saveMarkupPromises = items.map((item) => {
55
- return saveFile(item.path, item.markup);
34
+ items.forEach((item) => {
35
+ store_1.default.set(item.path, item.markup);
56
36
  });
57
- await Promise.all(saveMarkupPromises);
58
37
  if (current_page === last_page) {
59
38
  shouldKeepFetching = false;
60
39
  }
@@ -64,7 +43,7 @@ async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey
64
43
  }
65
44
  }
66
45
  catch (error) {
67
- await deleteTempDirectory();
46
+ store_1.default.clear();
68
47
  throw error;
69
48
  }
70
49
  }
@@ -124,15 +103,11 @@ async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFormat, c
124
103
  function markupFetcher(platform, fetchImplementation = fetch) {
125
104
  return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = (0, utils_1.getEnvironment)(), copy = {}, }) => {
126
105
  path = path || "/";
127
- const savedFile = await (async () => {
128
- if (typeof process === "undefined") {
129
- return null;
130
- }
131
- const { tempDirectoryExists, readFile, getEmptyMarkup } = await Promise.resolve().then(() => __importStar(require("./nodeUtils")));
132
- if (!(await tempDirectoryExists())) {
106
+ const savedFile = (() => {
107
+ if (!store_1.default.isInitialized()) {
133
108
  return null;
134
109
  }
135
- return (await readFile(path)) || getEmptyMarkup();
110
+ return store_1.default.get(path) || store_1.default.getEmptyMarkup();
136
111
  })();
137
112
  const markup = savedFile
138
113
  ? savedFile
@@ -22,23 +22,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
26
29
  const vitest_1 = require("vitest");
27
30
  const injectSchema = __importStar(require("./injectSchema"));
28
31
  const fetcherExports = __importStar(require("./markupFetcher"));
29
32
  const node_test_1 = require("node:test");
30
- const utilsExports = __importStar(require("./nodeUtils"));
31
- const { deleteTempDirectory } = utilsExports;
33
+ const store_1 = __importDefault(require("./store"));
32
34
  const { markupFetcher, batchMarkupFetcher } = fetcherExports;
33
35
  (0, vitest_1.beforeEach)(() => {
34
- deleteTempDirectory();
36
+ store_1.default.clear();
35
37
  });
36
38
  (0, node_test_1.afterEach)(() => {
37
39
  vitest_1.vi.resetAllMocks();
38
40
  });
39
41
  (0, vitest_1.describe)("fetchAll()", () => {
40
42
  (0, vitest_1.it)("fetches all comments in a single request", async () => {
41
- const saveFileSpy = vitest_1.vi.spyOn(utilsExports, "saveFile");
43
+ const saveFileSpy = vitest_1.vi.spyOn(store_1.default, "set");
42
44
  const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
43
45
  data: [
44
46
  { path: "/test", markup: "markup1" },
@@ -78,7 +80,7 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
78
80
  (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
79
81
  });
80
82
  (0, vitest_1.it)("fetches all comments in multiple requests", async () => {
81
- const saveFileSpy = vitest_1.vi.spyOn(utilsExports, "saveFile");
83
+ const saveFileSpy = vitest_1.vi.spyOn(store_1.default, "set");
82
84
  const mockBatchFetcher = vitest_1.vi
83
85
  .fn()
84
86
  .mockReturnValueOnce({
@@ -144,7 +146,7 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
144
146
  (0, vitest_1.expect)(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
145
147
  });
146
148
  (0, vitest_1.it)("deletes the temp directory of anything fails", async () => {
147
- const deleteTempDirectorySpy = vitest_1.vi.spyOn(utilsExports, "deleteTempDirectory");
149
+ const deleteTempDirectorySpy = vitest_1.vi.spyOn(store_1.default, "clear");
148
150
  const mockBatchFetcher = vitest_1.vi.fn().mockImplementation(() => {
149
151
  throw new Error("test error");
150
152
  });
@@ -164,7 +166,7 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
164
166
  (0, vitest_1.expect)(deleteTempDirectorySpy).toHaveBeenCalledOnce();
165
167
  });
166
168
  (0, vitest_1.it)("handles copy overrides", async () => {
167
- const saveFileSpy = vitest_1.vi.spyOn(utilsExports, "saveFile");
169
+ const saveFileSpy = vitest_1.vi.spyOn(store_1.default, "set");
168
170
  const mockBatchFetcher = vitest_1.vi.fn().mockReturnValue({
169
171
  data: [
170
172
  { path: "/test", markup: "markup1" },
@@ -497,9 +499,9 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
497
499
  (0, vitest_1.describe)("using saved markup", () => {
498
500
  (0, vitest_1.it)("makes fresh request when temp directory does not exist", async () => {
499
501
  const tempDirectoryExistsSpy = vitest_1.vi
500
- .spyOn(utilsExports, "tempDirectoryExists")
501
- .mockReturnValue(Promise.resolve(false));
502
- const readFileSpy = vitest_1.vi.spyOn(utilsExports, "readFile");
502
+ .spyOn(store_1.default, "isInitialized")
503
+ .mockReturnValue(false);
504
+ const readFileSpy = vitest_1.vi.spyOn(store_1.default, "get");
503
505
  const fetchMock = vitest_1.vi.fn().mockImplementation(() => {
504
506
  return {
505
507
  status: 200,
@@ -521,11 +523,11 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
521
523
  });
522
524
  (0, vitest_1.it)("uses saved markup when it exists", async () => {
523
525
  const tempDirectoryExistsSpy = vitest_1.vi
524
- .spyOn(utilsExports, "tempDirectoryExists")
525
- .mockReturnValue(Promise.resolve(true));
526
+ .spyOn(store_1.default, "isInitialized")
527
+ .mockReturnValue(true);
526
528
  const readFileSpy = vitest_1.vi
527
- .spyOn(utilsExports, "readFile")
528
- .mockReturnValue(Promise.resolve("saved markup"));
529
+ .spyOn(store_1.default, "get")
530
+ .mockReturnValue("saved markup");
529
531
  const fetchMock = vitest_1.vi.fn();
530
532
  const fetcher = markupFetcher("test", fetchMock);
531
533
  const result = await fetcher({
@@ -541,14 +543,12 @@ const { markupFetcher, batchMarkupFetcher } = fetcherExports;
541
543
  });
542
544
  (0, vitest_1.it)("uses the EMPTY template when there's no saved file for a post", async () => {
543
545
  const tempDirectoryExistsSpy = vitest_1.vi
544
- .spyOn(utilsExports, "tempDirectoryExists")
545
- .mockReturnValue(Promise.resolve(true));
546
- const readFileSpy = vitest_1.vi
547
- .spyOn(utilsExports, "readFile")
548
- .mockReturnValue(null);
546
+ .spyOn(store_1.default, "isInitialized")
547
+ .mockReturnValue(true);
548
+ const readFileSpy = vitest_1.vi.spyOn(store_1.default, "get").mockReturnValue(null);
549
549
  const getEmptyMarkupSpy = vitest_1.vi
550
- .spyOn(utilsExports, "getEmptyMarkup")
551
- .mockReturnValue(Promise.resolve("<!-- EMPTY -->"));
550
+ .spyOn(store_1.default, "getEmptyMarkup")
551
+ .mockReturnValue("<!-- EMPTY -->");
552
552
  const fetchMock = vitest_1.vi.fn();
553
553
  const fetcher = markupFetcher("test", fetchMock);
554
554
  const result = await fetcher({
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ let STORE = null;
4
+ const store = {
5
+ init() {
6
+ STORE = new Map();
7
+ },
8
+ isInitialized() {
9
+ return STORE !== null;
10
+ },
11
+ set(path, markup) {
12
+ STORE.set(store.toPathKey(path), markup);
13
+ },
14
+ get(path) {
15
+ return STORE.get(store.toPathKey(path)) || null;
16
+ },
17
+ clear() {
18
+ if (STORE) {
19
+ STORE.clear();
20
+ STORE = null;
21
+ }
22
+ },
23
+ getEmptyMarkup() {
24
+ return STORE.get("EMPTY");
25
+ },
26
+ toPathKey(path) {
27
+ return path.replace(/^\//, "").replace(/\//g, "::");
28
+ },
29
+ };
30
+ exports.default = store;
package/dist/cjs/utils.js CHANGED
@@ -4,7 +4,6 @@ exports.isValidTimezone = isValidTimezone;
4
4
  exports.getEnvironment = getEnvironment;
5
5
  exports.parseJson = parseJson;
6
6
  exports.unescapeHTML = unescapeHTML;
7
- exports.toSavedFileName = toSavedFileName;
8
7
  exports.removeFalseyValues = removeFalseyValues;
9
8
  function isValidTimezone(tz) {
10
9
  try {
@@ -62,9 +61,6 @@ function unescapeHTML(str) {
62
61
  }
63
62
  });
64
63
  }
65
- function toSavedFileName(name) {
66
- return name.replace(/^\//, "").replace(/\//g, "::");
67
- }
68
64
  function removeFalseyValues(obj) {
69
65
  const filteredItems = Object.entries(obj).filter(([, value]) => value);
70
66
  return Object.fromEntries(filteredItems);
@@ -44,11 +44,6 @@ const env = process.env;
44
44
  });
45
45
  });
46
46
  });
47
- (0, vitest_1.describe)("toSavedFileName()", () => {
48
- (0, vitest_1.it)("replaces slashes with double colons", () => {
49
- (0, vitest_1.expect)((0, utils_1.toSavedFileName)("/hey/there")).toEqual("hey::there");
50
- });
51
- });
52
47
  (0, vitest_1.describe)("removeFalseyValues()", () => {
53
48
  (0, vitest_1.it)("should remove falsey values", () => {
54
49
  (0, vitest_1.expect)((0, utils_1.removeFalseyValues)({ a: 1, b: 0, d: null, e: "" })).toEqual({
package/dist/esm/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export { markupFetcher, fetchAll } from "./markupFetcher";
2
+ export { removeFalseyValues } from "./utils";
2
3
  export { log, logError } from "./log";
@@ -1,10 +1,10 @@
1
1
  import { injectSchema } from "./injectSchema";
2
2
  import { getEnvironment, isValidTimezone, parseJson } from "./utils";
3
+ import store from "./store";
3
4
  const BASE_URL = "https://go.jamcomments.com";
4
5
  export async function fetchAll({ tz = undefined, dateFormat = undefined, domain, apiKey, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }, platform, fetchImplementation = fetch, batchMarkupFetcherImpl = batchMarkupFetcher) {
5
- const { createTempDirectory, deleteTempDirectory, saveFile } = await import("./nodeUtils");
6
6
  const fetchBatchMarkup = batchMarkupFetcherImpl(platform, fetchImplementation);
7
- await createTempDirectory();
7
+ store.init();
8
8
  let shouldKeepFetching = true;
9
9
  let page = 1;
10
10
  console.log("Fetching comments from JamComments! This might take a sec.");
@@ -21,10 +21,9 @@ export async function fetchAll({ tz = undefined, dateFormat = undefined, domain,
21
21
  copy,
22
22
  });
23
23
  console.log(`Checking for comment data. Batch: ${current_page}/${last_page}`);
24
- const saveMarkupPromises = items.map((item) => {
25
- return saveFile(item.path, item.markup);
24
+ items.forEach((item) => {
25
+ store.set(item.path, item.markup);
26
26
  });
27
- await Promise.all(saveMarkupPromises);
28
27
  if (current_page === last_page) {
29
28
  shouldKeepFetching = false;
30
29
  }
@@ -34,7 +33,7 @@ export async function fetchAll({ tz = undefined, dateFormat = undefined, domain,
34
33
  }
35
34
  }
36
35
  catch (error) {
37
- await deleteTempDirectory();
36
+ store.clear();
38
37
  throw error;
39
38
  }
40
39
  }
@@ -94,15 +93,11 @@ export async function makeMarkupRequest({ tz, path, page, domain, apiKey, dateFo
94
93
  export function markupFetcher(platform, fetchImplementation = fetch) {
95
94
  return async ({ tz = undefined, path, domain, apiKey, schema, dateFormat, baseUrl = BASE_URL, environment = getEnvironment(), copy = {}, }) => {
96
95
  path = path || "/";
97
- const savedFile = await (async () => {
98
- if (typeof process === "undefined") {
96
+ const savedFile = (() => {
97
+ if (!store.isInitialized()) {
99
98
  return null;
100
99
  }
101
- const { tempDirectoryExists, readFile, getEmptyMarkup } = await import("./nodeUtils");
102
- if (!(await tempDirectoryExists())) {
103
- return null;
104
- }
105
- return (await readFile(path)) || getEmptyMarkup();
100
+ return store.get(path) || store.getEmptyMarkup();
106
101
  })();
107
102
  const markup = savedFile
108
103
  ? savedFile
@@ -2,18 +2,17 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
2
2
  import * as injectSchema from "./injectSchema";
3
3
  import * as fetcherExports from "./markupFetcher";
4
4
  import { afterEach } from "node:test";
5
- import * as utilsExports from "./nodeUtils";
6
- const { deleteTempDirectory } = utilsExports;
5
+ import store from "./store";
7
6
  const { markupFetcher, batchMarkupFetcher } = fetcherExports;
8
7
  beforeEach(() => {
9
- deleteTempDirectory();
8
+ store.clear();
10
9
  });
11
10
  afterEach(() => {
12
11
  vi.resetAllMocks();
13
12
  });
14
13
  describe("fetchAll()", () => {
15
14
  it("fetches all comments in a single request", async () => {
16
- const saveFileSpy = vi.spyOn(utilsExports, "saveFile");
15
+ const saveFileSpy = vi.spyOn(store, "set");
17
16
  const mockBatchFetcher = vi.fn().mockReturnValue({
18
17
  data: [
19
18
  { path: "/test", markup: "markup1" },
@@ -53,7 +52,7 @@ describe("fetchAll()", () => {
53
52
  expect(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
54
53
  });
55
54
  it("fetches all comments in multiple requests", async () => {
56
- const saveFileSpy = vi.spyOn(utilsExports, "saveFile");
55
+ const saveFileSpy = vi.spyOn(store, "set");
57
56
  const mockBatchFetcher = vi
58
57
  .fn()
59
58
  .mockReturnValueOnce({
@@ -119,7 +118,7 @@ describe("fetchAll()", () => {
119
118
  expect(saveFileSpy).toHaveBeenCalledWith("/test2", "markup2");
120
119
  });
121
120
  it("deletes the temp directory of anything fails", async () => {
122
- const deleteTempDirectorySpy = vi.spyOn(utilsExports, "deleteTempDirectory");
121
+ const deleteTempDirectorySpy = vi.spyOn(store, "clear");
123
122
  const mockBatchFetcher = vi.fn().mockImplementation(() => {
124
123
  throw new Error("test error");
125
124
  });
@@ -139,7 +138,7 @@ describe("fetchAll()", () => {
139
138
  expect(deleteTempDirectorySpy).toHaveBeenCalledOnce();
140
139
  });
141
140
  it("handles copy overrides", async () => {
142
- const saveFileSpy = vi.spyOn(utilsExports, "saveFile");
141
+ const saveFileSpy = vi.spyOn(store, "set");
143
142
  const mockBatchFetcher = vi.fn().mockReturnValue({
144
143
  data: [
145
144
  { path: "/test", markup: "markup1" },
@@ -472,9 +471,9 @@ describe("markupFetcher", () => {
472
471
  describe("using saved markup", () => {
473
472
  it("makes fresh request when temp directory does not exist", async () => {
474
473
  const tempDirectoryExistsSpy = vi
475
- .spyOn(utilsExports, "tempDirectoryExists")
476
- .mockReturnValue(Promise.resolve(false));
477
- const readFileSpy = vi.spyOn(utilsExports, "readFile");
474
+ .spyOn(store, "isInitialized")
475
+ .mockReturnValue(false);
476
+ const readFileSpy = vi.spyOn(store, "get");
478
477
  const fetchMock = vi.fn().mockImplementation(() => {
479
478
  return {
480
479
  status: 200,
@@ -496,11 +495,11 @@ describe("markupFetcher", () => {
496
495
  });
497
496
  it("uses saved markup when it exists", async () => {
498
497
  const tempDirectoryExistsSpy = vi
499
- .spyOn(utilsExports, "tempDirectoryExists")
500
- .mockReturnValue(Promise.resolve(true));
498
+ .spyOn(store, "isInitialized")
499
+ .mockReturnValue(true);
501
500
  const readFileSpy = vi
502
- .spyOn(utilsExports, "readFile")
503
- .mockReturnValue(Promise.resolve("saved markup"));
501
+ .spyOn(store, "get")
502
+ .mockReturnValue("saved markup");
504
503
  const fetchMock = vi.fn();
505
504
  const fetcher = markupFetcher("test", fetchMock);
506
505
  const result = await fetcher({
@@ -516,14 +515,12 @@ describe("markupFetcher", () => {
516
515
  });
517
516
  it("uses the EMPTY template when there's no saved file for a post", async () => {
518
517
  const tempDirectoryExistsSpy = vi
519
- .spyOn(utilsExports, "tempDirectoryExists")
520
- .mockReturnValue(Promise.resolve(true));
521
- const readFileSpy = vi
522
- .spyOn(utilsExports, "readFile")
523
- .mockReturnValue(null);
518
+ .spyOn(store, "isInitialized")
519
+ .mockReturnValue(true);
520
+ const readFileSpy = vi.spyOn(store, "get").mockReturnValue(null);
524
521
  const getEmptyMarkupSpy = vi
525
- .spyOn(utilsExports, "getEmptyMarkup")
526
- .mockReturnValue(Promise.resolve("<!-- EMPTY -->"));
522
+ .spyOn(store, "getEmptyMarkup")
523
+ .mockReturnValue("<!-- EMPTY -->");
527
524
  const fetchMock = vi.fn();
528
525
  const fetcher = markupFetcher("test", fetchMock);
529
526
  const result = await fetcher({
@@ -0,0 +1,28 @@
1
+ let STORE = null;
2
+ const store = {
3
+ init() {
4
+ STORE = new Map();
5
+ },
6
+ isInitialized() {
7
+ return STORE !== null;
8
+ },
9
+ set(path, markup) {
10
+ STORE.set(store.toPathKey(path), markup);
11
+ },
12
+ get(path) {
13
+ return STORE.get(store.toPathKey(path)) || null;
14
+ },
15
+ clear() {
16
+ if (STORE) {
17
+ STORE.clear();
18
+ STORE = null;
19
+ }
20
+ },
21
+ getEmptyMarkup() {
22
+ return STORE.get("EMPTY");
23
+ },
24
+ toPathKey(path) {
25
+ return path.replace(/^\//, "").replace(/\//g, "::");
26
+ },
27
+ };
28
+ export default store;
package/dist/esm/utils.js CHANGED
@@ -54,9 +54,6 @@ export function unescapeHTML(str) {
54
54
  }
55
55
  });
56
56
  }
57
- export function toSavedFileName(name) {
58
- return name.replace(/^\//, "").replace(/\//g, "::");
59
- }
60
57
  export function removeFalseyValues(obj) {
61
58
  const filteredItems = Object.entries(obj).filter(([, value]) => value);
62
59
  return Object.fromEntries(filteredItems);
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
- import { getEnvironment, removeFalseyValues, toSavedFileName } from "./utils";
2
+ import { getEnvironment, removeFalseyValues } from "./utils";
3
3
  const env = process.env;
4
4
  beforeEach(() => {
5
5
  vi.resetModules();
@@ -42,11 +42,6 @@ describe("getEnvironment()", () => {
42
42
  });
43
43
  });
44
44
  });
45
- describe("toSavedFileName()", () => {
46
- it("replaces slashes with double colons", () => {
47
- expect(toSavedFileName("/hey/there")).toEqual("hey::there");
48
- });
49
- });
50
45
  describe("removeFalseyValues()", () => {
51
46
  it("should remove falsey values", () => {
52
47
  expect(removeFalseyValues({ a: 1, b: 0, d: null, e: "" })).toEqual({
@@ -1,3 +1,4 @@
1
1
  export type { CustomCopy } from "./markupFetcher";
2
2
  export { markupFetcher, fetchAll } from "./markupFetcher";
3
+ export { removeFalseyValues } from "./utils";
3
4
  export { log, logError } from "./log";
@@ -0,0 +1,10 @@
1
+ declare const store: {
2
+ init(): void;
3
+ isInitialized(): boolean;
4
+ set(path: string, markup: string): void;
5
+ get(path: string): string;
6
+ clear(): void;
7
+ getEmptyMarkup(): string;
8
+ toPathKey(path: string): string;
9
+ };
10
+ export default store;
@@ -2,5 +2,4 @@ export declare function isValidTimezone(tz: string): boolean;
2
2
  export declare function getEnvironment(): string;
3
3
  export declare function parseJson(json: string): any;
4
4
  export declare function unescapeHTML(str: any): any;
5
- export declare function toSavedFileName(name: string): string;
6
5
  export declare function removeFalseyValues<T>(obj: T): Partial<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jam-comments/server-utilities",
3
- "version": "5.6.0",
3
+ "version": "5.8.0",
4
4
  "description": "Various JavaScript utilities for JamComments.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -22,11 +22,11 @@
22
22
  "homepage": "https://jamcomments.com",
23
23
  "license": "GPL-2.0",
24
24
  "devDependencies": {
25
- "@types/node": "^20.14.11",
25
+ "@types/node": "^22.5.1",
26
26
  "prettier": "^3.3.3",
27
27
  "typescript": "^5.5.4",
28
- "vite": "^5.3.4",
29
- "vitest": "^2.0.4"
28
+ "vite": "^5.4.2",
29
+ "vitest": "^2.0.5"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"
@@ -1,78 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getTempDirectory = getTempDirectory;
27
- exports.deleteTempDirectory = deleteTempDirectory;
28
- exports.createTempDirectory = createTempDirectory;
29
- exports.readFile = readFile;
30
- exports.getEmptyMarkup = getEmptyMarkup;
31
- exports.tempDirectoryExists = tempDirectoryExists;
32
- exports.saveFile = saveFile;
33
- const utils_1 = require("./utils");
34
- async function getTempDirectory() {
35
- const path = await Promise.resolve().then(() => __importStar(require("path")));
36
- return path.join(process.cwd(), "_temp_jc");
37
- }
38
- async function deleteTempDirectory() {
39
- const tempDirectory = await getTempDirectory();
40
- const fs = await Promise.resolve().then(() => __importStar(require("fs")));
41
- if (fs.existsSync(tempDirectory)) {
42
- fs.rmdirSync(tempDirectory, { recursive: true });
43
- }
44
- }
45
- async function createTempDirectory(mkdirSyncImpl, existsSyncImpl) {
46
- const tempDirectory = await getTempDirectory();
47
- const fs = await Promise.resolve().then(() => __importStar(require("fs")));
48
- if (!(await tempDirectoryExists(existsSyncImpl || fs.existsSync))) {
49
- (mkdirSyncImpl || fs.mkdirSync)(tempDirectory);
50
- }
51
- }
52
- async function readFile(name, readFileSyncImpl) {
53
- const fileName = (0, utils_1.toSavedFileName)(name);
54
- const fs = await Promise.resolve().then(() => __importStar(require("fs")));
55
- try {
56
- return (readFileSyncImpl || fs.readFileSync)(`${await getTempDirectory()}/${fileName}`, "utf8");
57
- }
58
- catch (error) {
59
- return null;
60
- }
61
- }
62
- function getEmptyMarkup() {
63
- return readFile("EMPTY");
64
- }
65
- async function tempDirectoryExists(existsSyncImpl) {
66
- const fs = await Promise.resolve().then(() => __importStar(require("fs")));
67
- return (existsSyncImpl || fs.existsSync)(await getTempDirectory());
68
- }
69
- async function saveFile(name, data, writeFileImpl) {
70
- const fileName = (0, utils_1.toSavedFileName)(name);
71
- const tempDirectory = await getTempDirectory();
72
- const fs = await Promise.resolve().then(() => __importStar(require("fs")));
73
- return new Promise((resolve) => {
74
- (writeFileImpl || fs.writeFile)(`${tempDirectory}/${fileName}`, data, () => {
75
- resolve();
76
- });
77
- });
78
- }
@@ -1,48 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const vitest_1 = require("vitest");
4
- const nodeUtils_1 = require("./nodeUtils");
5
- (0, vitest_1.describe)("createTempDirectory()", () => {
6
- (0, vitest_1.it)("creates temp directory fresh", async () => {
7
- const tempDirectory = await (0, nodeUtils_1.getTempDirectory)();
8
- const mkdirSyncMock = vitest_1.vi.fn();
9
- const existsSyncMock = vitest_1.vi.fn().mockReturnValue(false);
10
- await new Promise((resolve) => {
11
- (0, vitest_1.expect)(async () => {
12
- await (0, nodeUtils_1.createTempDirectory)(mkdirSyncMock, existsSyncMock);
13
- resolve();
14
- }).not.toThrow();
15
- });
16
- (0, vitest_1.expect)(mkdirSyncMock).toHaveBeenCalledWith(tempDirectory);
17
- });
18
- (0, vitest_1.it)("does not throw error when it already exists", () => {
19
- const mkdirSyncMock = vitest_1.vi.fn().mockImplementation(() => {
20
- throw new Error("Directory already exists");
21
- });
22
- const existsSyncMock = vitest_1.vi.fn().mockReturnValue(true);
23
- (0, vitest_1.expect)(() => (0, nodeUtils_1.createTempDirectory)(mkdirSyncMock, existsSyncMock)).not.toThrow();
24
- (0, vitest_1.expect)(mkdirSyncMock).not.toHaveBeenCalledWith();
25
- });
26
- });
27
- (0, vitest_1.describe)("readFile()", () => {
28
- (0, vitest_1.it)("reads the file", async () => {
29
- const readFileSyncMock = vitest_1.vi.fn().mockReturnValue("file contents");
30
- const tempDirectory = await (0, nodeUtils_1.getTempDirectory)();
31
- (0, vitest_1.expect)(await (0, nodeUtils_1.readFile)("hey/there", readFileSyncMock)).toEqual("file contents");
32
- (0, vitest_1.expect)(readFileSyncMock).toHaveBeenCalledWith(`${tempDirectory}/hey::there`, "utf8");
33
- });
34
- (0, vitest_1.it)("returns null when file doesn't exist", async () => {
35
- const readFileSyncMock = vitest_1.vi.fn().mockImplementation(() => {
36
- throw new Error("File not found");
37
- });
38
- (0, vitest_1.expect)(await (0, nodeUtils_1.readFile)("hey/there", readFileSyncMock)).toEqual(null);
39
- });
40
- });
41
- (0, vitest_1.describe)("saveFile()", () => {
42
- (0, vitest_1.it)("saves the file", async () => {
43
- const writeFileMock = vitest_1.vi.fn().mockImplementation((file, data, cb) => cb());
44
- const tempDirectory = await (0, nodeUtils_1.getTempDirectory)();
45
- await (0, nodeUtils_1.saveFile)("hey/there", "file contents", writeFileMock);
46
- (0, vitest_1.expect)(writeFileMock).toHaveBeenCalledWith(`${tempDirectory}/hey::there`, "file contents", vitest_1.expect.any(Function));
47
- });
48
- });
@@ -1,46 +0,0 @@
1
- import { toSavedFileName } from "./utils";
2
- export async function getTempDirectory() {
3
- const path = await import("path");
4
- return path.join(process.cwd(), "_temp_jc");
5
- }
6
- export async function deleteTempDirectory() {
7
- const tempDirectory = await getTempDirectory();
8
- const fs = await import("fs");
9
- if (fs.existsSync(tempDirectory)) {
10
- fs.rmdirSync(tempDirectory, { recursive: true });
11
- }
12
- }
13
- export async function createTempDirectory(mkdirSyncImpl, existsSyncImpl) {
14
- const tempDirectory = await getTempDirectory();
15
- const fs = await import("fs");
16
- if (!(await tempDirectoryExists(existsSyncImpl || fs.existsSync))) {
17
- (mkdirSyncImpl || fs.mkdirSync)(tempDirectory);
18
- }
19
- }
20
- export async function readFile(name, readFileSyncImpl) {
21
- const fileName = toSavedFileName(name);
22
- const fs = await import("fs");
23
- try {
24
- return (readFileSyncImpl || fs.readFileSync)(`${await getTempDirectory()}/${fileName}`, "utf8");
25
- }
26
- catch (error) {
27
- return null;
28
- }
29
- }
30
- export function getEmptyMarkup() {
31
- return readFile("EMPTY");
32
- }
33
- export async function tempDirectoryExists(existsSyncImpl) {
34
- const fs = await import("fs");
35
- return (existsSyncImpl || fs.existsSync)(await getTempDirectory());
36
- }
37
- export async function saveFile(name, data, writeFileImpl) {
38
- const fileName = toSavedFileName(name);
39
- const tempDirectory = await getTempDirectory();
40
- const fs = await import("fs");
41
- return new Promise((resolve) => {
42
- (writeFileImpl || fs.writeFile)(`${tempDirectory}/${fileName}`, data, () => {
43
- resolve();
44
- });
45
- });
46
- }
@@ -1,46 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import { createTempDirectory, getTempDirectory, readFile, saveFile, } from "./nodeUtils";
3
- describe("createTempDirectory()", () => {
4
- it("creates temp directory fresh", async () => {
5
- const tempDirectory = await getTempDirectory();
6
- const mkdirSyncMock = vi.fn();
7
- const existsSyncMock = vi.fn().mockReturnValue(false);
8
- await new Promise((resolve) => {
9
- expect(async () => {
10
- await createTempDirectory(mkdirSyncMock, existsSyncMock);
11
- resolve();
12
- }).not.toThrow();
13
- });
14
- expect(mkdirSyncMock).toHaveBeenCalledWith(tempDirectory);
15
- });
16
- it("does not throw error when it already exists", () => {
17
- const mkdirSyncMock = vi.fn().mockImplementation(() => {
18
- throw new Error("Directory already exists");
19
- });
20
- const existsSyncMock = vi.fn().mockReturnValue(true);
21
- expect(() => createTempDirectory(mkdirSyncMock, existsSyncMock)).not.toThrow();
22
- expect(mkdirSyncMock).not.toHaveBeenCalledWith();
23
- });
24
- });
25
- describe("readFile()", () => {
26
- it("reads the file", async () => {
27
- const readFileSyncMock = vi.fn().mockReturnValue("file contents");
28
- const tempDirectory = await getTempDirectory();
29
- expect(await readFile("hey/there", readFileSyncMock)).toEqual("file contents");
30
- expect(readFileSyncMock).toHaveBeenCalledWith(`${tempDirectory}/hey::there`, "utf8");
31
- });
32
- it("returns null when file doesn't exist", async () => {
33
- const readFileSyncMock = vi.fn().mockImplementation(() => {
34
- throw new Error("File not found");
35
- });
36
- expect(await readFile("hey/there", readFileSyncMock)).toEqual(null);
37
- });
38
- });
39
- describe("saveFile()", () => {
40
- it("saves the file", async () => {
41
- const writeFileMock = vi.fn().mockImplementation((file, data, cb) => cb());
42
- const tempDirectory = await getTempDirectory();
43
- await saveFile("hey/there", "file contents", writeFileMock);
44
- expect(writeFileMock).toHaveBeenCalledWith(`${tempDirectory}/hey::there`, "file contents", expect.any(Function));
45
- });
46
- });
@@ -1,7 +0,0 @@
1
- export declare function getTempDirectory(): Promise<string>;
2
- export declare function deleteTempDirectory(): Promise<void>;
3
- export declare function createTempDirectory(mkdirSyncImpl?: typeof import("fs").mkdirSync, existsSyncImpl?: typeof import("fs").existsSync): Promise<void>;
4
- export declare function readFile(name: string, readFileSyncImpl?: typeof import("fs").readFileSync): Promise<string>;
5
- export declare function getEmptyMarkup(): Promise<string>;
6
- export declare function tempDirectoryExists(existsSyncImpl?: typeof import("fs").existsSync): Promise<boolean>;
7
- export declare function saveFile(name: string, data: string, writeFileImpl?: typeof import("fs").writeFile): Promise<void>;
@@ -1 +0,0 @@
1
- export {};