tinacms 0.0.0-e48ff80-20251203000400 → 0.0.0-e52f289-20251216060413

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.
@@ -1,3 +1,7 @@
1
1
  import type { Cache } from './index';
2
- export declare const makeCacheDir: (dir: string, fs: any, path: any, os: any) => Promise<string>;
3
- export declare const NodeCache: (dir: string) => Promise<Cache>;
2
+ type FsModule = typeof import('node:fs');
3
+ type PathModule = typeof import('node:path');
4
+ type OsModule = typeof import('node:os');
5
+ export declare const makeCacheDir: (dir: string, fsArg: FsModule, pathArg: PathModule, osArg: OsModule) => string | null;
6
+ export declare const NodeCache: (dir: string) => Promise<Cache | null>;
7
+ export {};
package/dist/client.js CHANGED
@@ -1,11 +1,123 @@
1
1
  var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __esm = (fn, res) => function __init() {
4
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
6
5
  };
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+
11
+ // src/cache/node-cache.ts
12
+ var node_cache_exports = {};
13
+ __export(node_cache_exports, {
14
+ NodeCache: () => NodeCache,
15
+ makeCacheDir: () => makeCacheDir
16
+ });
17
+ var resolveModule, getRootPath, makeCacheDir, NodeCache;
18
+ var init_node_cache = __esm({
19
+ "src/cache/node-cache.ts"() {
20
+ resolveModule = (mod) => {
21
+ if (mod && typeof mod === "object" && "default" in mod && mod.default) {
22
+ return mod.default;
23
+ }
24
+ return mod;
25
+ };
26
+ getRootPath = (pathParts, pathArg) => {
27
+ if (pathParts.length === 0) return null;
28
+ const isWindows = pathArg.sep === "\\";
29
+ const root = pathParts[0];
30
+ return isWindows ? `${root}${pathArg.sep}` : `${pathArg.sep}${root}`;
31
+ };
32
+ makeCacheDir = (dir, fsArg, pathArg, osArg) => {
33
+ const normalizedDir = pathArg.normalize(dir);
34
+ const pathParts = normalizedDir.split(pathArg.sep).filter(Boolean);
35
+ const cacheHash = pathParts[pathParts.length - 1];
36
+ const rootPath = getRootPath(pathParts, pathArg);
37
+ const rootExists = rootPath && fsArg.existsSync(rootPath);
38
+ const cacheDir = rootExists ? normalizedDir : pathArg.join(osArg.tmpdir(), cacheHash);
39
+ try {
40
+ fsArg.mkdirSync(cacheDir, { recursive: true });
41
+ } catch (error) {
42
+ console.warn(
43
+ `Warning: Failed to create cache directory: ${error.message}. Caching will be disabled.`
44
+ );
45
+ return null;
46
+ }
47
+ return cacheDir;
48
+ };
49
+ NodeCache = async (dir) => {
50
+ try {
51
+ const [fsModule, pathModule, osModule, cryptoModule] = await Promise.all([
52
+ import("node:fs"),
53
+ import("node:path"),
54
+ import("node:os"),
55
+ import("node:crypto")
56
+ ]);
57
+ const fs = resolveModule(fsModule);
58
+ const path = resolveModule(pathModule);
59
+ const os = resolveModule(osModule);
60
+ const crypto = resolveModule(cryptoModule);
61
+ if (typeof path?.join !== "function") {
62
+ console.warn(
63
+ "Warning: Node.js path module not available. Caching will be disabled."
64
+ );
65
+ return null;
66
+ }
67
+ const cacheDir = makeCacheDir(dir, fs, path, os);
68
+ if (cacheDir === null) {
69
+ return null;
70
+ }
71
+ return {
72
+ makeKey: (key) => {
73
+ const input = key && key instanceof Object ? JSON.stringify(key) : key || "";
74
+ return crypto.createHash("sha256").update(input).digest("hex");
75
+ },
76
+ get: async (key) => {
77
+ let readValue;
78
+ const cacheFilename = path.join(cacheDir, key);
79
+ try {
80
+ const data = await fs.promises.readFile(cacheFilename, "utf-8");
81
+ readValue = JSON.parse(data);
82
+ } catch (e) {
83
+ if (e.code !== "ENOENT") {
84
+ console.warn(
85
+ `Warning: Failed to read cache file ${cacheFilename}: ${e.message}`
86
+ );
87
+ }
88
+ }
89
+ return readValue;
90
+ },
91
+ set: async (key, value) => {
92
+ const cacheFilename = path.join(cacheDir, key);
93
+ try {
94
+ await fs.promises.writeFile(cacheFilename, JSON.stringify(value), {
95
+ encoding: "utf-8",
96
+ flag: "wx"
97
+ });
98
+ } catch (e) {
99
+ if (e.code !== "EEXIST") {
100
+ console.warn(
101
+ `Warning: Failed to write cache file ${cacheFilename}: ${e.message}`
102
+ );
103
+ }
104
+ }
105
+ }
106
+ };
107
+ } catch (e) {
108
+ console.warn(
109
+ "Warning: Failed to initialize cache. Caching will be disabled.",
110
+ e.message
111
+ );
112
+ return null;
113
+ }
114
+ };
115
+ }
116
+ });
117
+
118
+ // src/unifiedClient/index.ts
7
119
  import AsyncLock from "async-lock";
8
- const TINA_HOST = "content.tinajs.io";
120
+ var TINA_HOST = "content.tinajs.io";
9
121
  function replaceGithubPathSplit(url, replacement) {
10
122
  const parts = url.split("github/");
11
123
  if (parts.length > 1 && replacement) {
@@ -14,7 +126,15 @@ function replaceGithubPathSplit(url, replacement) {
14
126
  return url;
15
127
  }
16
128
  }
17
- class TinaClient {
129
+ var TinaClient = class {
130
+ apiUrl;
131
+ readonlyToken;
132
+ queries;
133
+ errorPolicy;
134
+ initialized = false;
135
+ cacheLock;
136
+ cacheDir;
137
+ cache;
18
138
  constructor({
19
139
  token,
20
140
  url,
@@ -22,16 +142,8 @@ class TinaClient {
22
142
  errorPolicy,
23
143
  cacheDir
24
144
  }) {
25
- __publicField(this, "apiUrl");
26
- __publicField(this, "readonlyToken");
27
- __publicField(this, "queries");
28
- __publicField(this, "errorPolicy");
29
- __publicField(this, "initialized", false);
30
- __publicField(this, "cacheLock");
31
- __publicField(this, "cacheDir");
32
- __publicField(this, "cache");
33
145
  this.apiUrl = url;
34
- this.readonlyToken = token == null ? void 0 : token.trim();
146
+ this.readonlyToken = token?.trim();
35
147
  this.queries = queries(this);
36
148
  this.errorPolicy = errorPolicy || "throw";
37
149
  this.cacheDir = cacheDir || "";
@@ -41,10 +153,12 @@ class TinaClient {
41
153
  return;
42
154
  }
43
155
  try {
44
- if (this.cacheDir && typeof window === "undefined" && typeof require !== "undefined") {
45
- const { NodeCache } = await import("./node-cache-c9558e1e.js");
46
- this.cache = await NodeCache(this.cacheDir);
47
- this.cacheLock = new AsyncLock();
156
+ if (this.cacheDir && typeof window === "undefined") {
157
+ const { NodeCache: NodeCache2 } = await Promise.resolve().then(() => (init_node_cache(), node_cache_exports));
158
+ this.cache = await NodeCache2(this.cacheDir);
159
+ if (this.cache) {
160
+ this.cacheLock = new AsyncLock();
161
+ }
48
162
  }
49
163
  } catch (e) {
50
164
  console.error(e);
@@ -52,7 +166,6 @@ class TinaClient {
52
166
  this.initialized = true;
53
167
  }
54
168
  async request({ errorPolicy, ...args }, options) {
55
- var _a;
56
169
  await this.init();
57
170
  const errorPolicyDefined = errorPolicy || this.errorPolicy;
58
171
  const headers = new Headers();
@@ -60,17 +173,17 @@ class TinaClient {
60
173
  headers.append("X-API-KEY", this.readonlyToken);
61
174
  }
62
175
  headers.append("Content-Type", "application/json");
63
- if (options == null ? void 0 : options.fetchOptions) {
64
- if ((_a = options == null ? void 0 : options.fetchOptions) == null ? void 0 : _a.headers) {
176
+ if (options?.fetchOptions) {
177
+ if (options?.fetchOptions?.headers) {
65
178
  Object.entries(options.fetchOptions.headers).forEach(([key2, value]) => {
66
179
  headers.append(key2, value);
67
180
  });
68
181
  }
69
182
  }
70
- const { headers: _, ...providedFetchOptions } = (options == null ? void 0 : options.fetchOptions) || {};
183
+ const { headers: _, ...providedFetchOptions } = options?.fetchOptions || {};
71
184
  const bodyString = JSON.stringify({
72
185
  query: args.query,
73
- variables: (args == null ? void 0 : args.variables) || {}
186
+ variables: args?.variables || {}
74
187
  });
75
188
  const optionsObject = {
76
189
  method: "POST",
@@ -80,7 +193,7 @@ class TinaClient {
80
193
  ...providedFetchOptions
81
194
  };
82
195
  const draftBranch = headers.get("x-branch");
83
- const url = replaceGithubPathSplit((args == null ? void 0 : args.url) || this.apiUrl, draftBranch);
196
+ const url = replaceGithubPathSplit(args?.url || this.apiUrl, draftBranch);
84
197
  let key = "";
85
198
  let result;
86
199
  if (this.cache) {
@@ -107,7 +220,7 @@ class TinaClient {
107
220
  }
108
221
  return result;
109
222
  }
110
- }
223
+ };
111
224
  async function requestFromServer(url, query, optionsObject, errorPolicyDefined) {
112
225
  const res = await fetch(url, optionsObject);
113
226
  if (!res.ok) {
@@ -128,8 +241,8 @@ async function requestFromServer(url, query, optionsObject, errorPolicyDefined)
128
241
  );
129
242
  }
130
243
  const result = {
131
- data: json == null ? void 0 : json.data,
132
- errors: (json == null ? void 0 : json.errors) || null,
244
+ data: json?.data,
245
+ errors: json?.errors || null,
133
246
  query
134
247
  };
135
248
  return result;