@decocms/runtime 1.0.0-alpha.10 → 1.0.0-alpha.12

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,107 +0,0 @@
1
- // Helper functions for DeconfigResource
2
-
3
- export const normalizeDirectory = (dir: string) => {
4
- // Ensure directory starts with / and doesn't end with /
5
- const normalized = dir.startsWith("/") ? dir : `/${dir}`;
6
- return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
7
- };
8
-
9
- export const ResourcePath = {
10
- build: (directory: string, resourceId: string) => {
11
- const normalizedDir = normalizeDirectory(directory);
12
- return `${normalizedDir}/${resourceId}.json`;
13
- },
14
- extract: (path: string) => {
15
- const match = path.match(/^(.+)\/(.+)\.json$/);
16
- if (!match) {
17
- throw new Error("Invalid resource path");
18
- }
19
- return { directory: match[1], resourceId: match[2] };
20
- },
21
- };
22
-
23
- export const ResourceUri = {
24
- build: (integrationId: string, resourceName: string, resourceId: string) => {
25
- return `rsc://${integrationId}/${resourceName}/${resourceId}`;
26
- },
27
- unwind: (uri: string) => {
28
- const match = uri.match(/^rsc:\/\/[^/]+\/([^/]+)\/(.+)$/);
29
- if (!match) {
30
- throw new Error("Invalid Resources 2.0 URI format");
31
- }
32
- return { resourceName: match[1], resourceId: match[2] };
33
- },
34
- };
35
-
36
- export function getMetadataValue(metadata: unknown, key: string): unknown {
37
- if (!metadata || typeof metadata !== "object") return undefined;
38
- const metaObj = metadata as Record<string, unknown>;
39
- if (key in metaObj) return metaObj[key];
40
- const nested = metaObj.metadata;
41
- if (nested && typeof nested === "object" && key in nested) {
42
- return (nested as Record<string, unknown>)[key];
43
- }
44
- return undefined;
45
- }
46
-
47
- export function getMetadataString(
48
- metadata: unknown,
49
- key: string,
50
- ): string | undefined {
51
- const value = getMetadataValue(metadata, key);
52
- return typeof value === "string" ? value : undefined;
53
- }
54
-
55
- export const toAsyncIterator = <T>(
56
- emitter: EventSource,
57
- eventType: string = "message",
58
- ): AsyncIterable<T> => {
59
- const queue: T[] = [];
60
- let done = false;
61
- let waitPromise: ((data?: T) => void) | null = null;
62
-
63
- const triggerLoop = () => {
64
- if (waitPromise) {
65
- waitPromise();
66
- waitPromise = null;
67
- }
68
- };
69
-
70
- const messageHandler = (data: MessageEvent) => {
71
- try {
72
- queue.push(JSON.parse(data.data));
73
- } catch {
74
- // Silently ignore malformed data or optionally log error
75
- return;
76
- }
77
- triggerLoop();
78
- };
79
-
80
- const errorHandler = () => {
81
- done = true;
82
- triggerLoop();
83
- };
84
-
85
- emitter.addEventListener(eventType, messageHandler);
86
- emitter.addEventListener("error", errorHandler);
87
-
88
- return {
89
- async *[Symbol.asyncIterator]() {
90
- try {
91
- while (true) {
92
- const value = queue.shift();
93
- if (value) {
94
- yield value;
95
- } else {
96
- if (done) return;
97
- await new Promise((resolve) => (waitPromise = resolve));
98
- }
99
- }
100
- } finally {
101
- emitter.removeEventListener(eventType, messageHandler);
102
- emitter.removeEventListener("error", errorHandler);
103
- emitter.close();
104
- }
105
- },
106
- };
107
- };
@@ -1 +0,0 @@
1
- export * from "./resources.ts";