@anvilkit/plugin-asset-manager 0.1.1

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.
Files changed (159) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +159 -0
  3. package/dist/adapters/data-url.cjs +78 -0
  4. package/dist/adapters/data-url.d.cts +6 -0
  5. package/dist/adapters/data-url.d.cts.map +1 -0
  6. package/dist/adapters/data-url.d.ts +6 -0
  7. package/dist/adapters/data-url.d.ts.map +1 -0
  8. package/dist/adapters/data-url.js +44 -0
  9. package/dist/adapters/extract-image-dimensions.cjs +69 -0
  10. package/dist/adapters/extract-image-dimensions.d.cts +21 -0
  11. package/dist/adapters/extract-image-dimensions.d.cts.map +1 -0
  12. package/dist/adapters/extract-image-dimensions.d.ts +21 -0
  13. package/dist/adapters/extract-image-dimensions.d.ts.map +1 -0
  14. package/dist/adapters/extract-image-dimensions.js +35 -0
  15. package/dist/adapters/in-memory.cjs +62 -0
  16. package/dist/adapters/in-memory.d.cts +3 -0
  17. package/dist/adapters/in-memory.d.cts.map +1 -0
  18. package/dist/adapters/in-memory.d.ts +3 -0
  19. package/dist/adapters/in-memory.d.ts.map +1 -0
  20. package/dist/adapters/in-memory.js +28 -0
  21. package/dist/adapters/s3-presigned.cjs +166 -0
  22. package/dist/adapters/s3-presigned.d.cts +59 -0
  23. package/dist/adapters/s3-presigned.d.cts.map +1 -0
  24. package/dist/adapters/s3-presigned.d.ts +59 -0
  25. package/dist/adapters/s3-presigned.d.ts.map +1 -0
  26. package/dist/adapters/s3-presigned.js +129 -0
  27. package/dist/asset-reference.cjs +38 -0
  28. package/dist/asset-reference.d.cts +10 -0
  29. package/dist/asset-reference.d.cts.map +1 -0
  30. package/dist/asset-reference.d.ts +10 -0
  31. package/dist/asset-reference.d.ts.map +1 -0
  32. package/dist/asset-reference.js +4 -0
  33. package/dist/csp.cjs +83 -0
  34. package/dist/csp.d.cts +45 -0
  35. package/dist/csp.d.cts.map +1 -0
  36. package/dist/csp.d.ts +45 -0
  37. package/dist/csp.d.ts.map +1 -0
  38. package/dist/csp.js +49 -0
  39. package/dist/errors.cjs +58 -0
  40. package/dist/errors.d.cts +15 -0
  41. package/dist/errors.d.cts.map +1 -0
  42. package/dist/errors.d.ts +15 -0
  43. package/dist/errors.d.ts.map +1 -0
  44. package/dist/errors.js +21 -0
  45. package/dist/header-action.cjs +44 -0
  46. package/dist/header-action.d.cts +3 -0
  47. package/dist/header-action.d.cts.map +1 -0
  48. package/dist/header-action.d.ts +3 -0
  49. package/dist/header-action.d.ts.map +1 -0
  50. package/dist/header-action.js +10 -0
  51. package/dist/index.cjs +90 -0
  52. package/dist/index.d.cts +18 -0
  53. package/dist/index.d.cts.map +1 -0
  54. package/dist/index.d.ts +18 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +10 -0
  57. package/dist/infer-kind.cjs +45 -0
  58. package/dist/infer-kind.d.cts +8 -0
  59. package/dist/infer-kind.d.cts.map +1 -0
  60. package/dist/infer-kind.d.ts +8 -0
  61. package/dist/infer-kind.d.ts.map +1 -0
  62. package/dist/infer-kind.js +11 -0
  63. package/dist/plugin.cjs +258 -0
  64. package/dist/plugin.d.cts +9 -0
  65. package/dist/plugin.d.cts.map +1 -0
  66. package/dist/plugin.d.ts +9 -0
  67. package/dist/plugin.d.ts.map +1 -0
  68. package/dist/plugin.js +212 -0
  69. package/dist/registry.cjs +198 -0
  70. package/dist/registry.d.cts +3 -0
  71. package/dist/registry.d.cts.map +1 -0
  72. package/dist/registry.d.ts +3 -0
  73. package/dist/registry.d.ts.map +1 -0
  74. package/dist/registry.js +164 -0
  75. package/dist/resolver.cjs +185 -0
  76. package/dist/resolver.d.cts +10 -0
  77. package/dist/resolver.d.cts.map +1 -0
  78. package/dist/resolver.d.ts +10 -0
  79. package/dist/resolver.d.ts.map +1 -0
  80. package/dist/resolver.js +148 -0
  81. package/dist/retry.cjs +123 -0
  82. package/dist/retry.d.cts +71 -0
  83. package/dist/retry.d.cts.map +1 -0
  84. package/dist/retry.d.ts +71 -0
  85. package/dist/retry.d.ts.map +1 -0
  86. package/dist/retry.js +86 -0
  87. package/dist/studio-asset-source.cjs +211 -0
  88. package/dist/studio-asset-source.d.cts +52 -0
  89. package/dist/studio-asset-source.d.cts.map +1 -0
  90. package/dist/studio-asset-source.d.ts +52 -0
  91. package/dist/studio-asset-source.d.ts.map +1 -0
  92. package/dist/studio-asset-source.js +171 -0
  93. package/dist/testing/index.cjs +66 -0
  94. package/dist/testing/index.d.cts +24 -0
  95. package/dist/testing/index.d.cts.map +1 -0
  96. package/dist/testing/index.d.ts +24 -0
  97. package/dist/testing/index.d.ts.map +1 -0
  98. package/dist/testing/index.js +29 -0
  99. package/dist/types.cjs +18 -0
  100. package/dist/types.d.cts +132 -0
  101. package/dist/types.d.cts.map +1 -0
  102. package/dist/types.d.ts +132 -0
  103. package/dist/types.d.ts.map +1 -0
  104. package/dist/types.js +0 -0
  105. package/dist/ui/AssetBrowser.cjs +271 -0
  106. package/dist/ui/AssetBrowser.d.cts +45 -0
  107. package/dist/ui/AssetBrowser.d.cts.map +1 -0
  108. package/dist/ui/AssetBrowser.d.ts +45 -0
  109. package/dist/ui/AssetBrowser.d.ts.map +1 -0
  110. package/dist/ui/AssetBrowser.js +237 -0
  111. package/dist/ui/AssetCommandPalette.cjs +135 -0
  112. package/dist/ui/AssetCommandPalette.d.cts +21 -0
  113. package/dist/ui/AssetCommandPalette.d.cts.map +1 -0
  114. package/dist/ui/AssetCommandPalette.d.ts +21 -0
  115. package/dist/ui/AssetCommandPalette.d.ts.map +1 -0
  116. package/dist/ui/AssetCommandPalette.js +101 -0
  117. package/dist/ui/AssetManagerUI.cjs +169 -0
  118. package/dist/ui/AssetManagerUI.d.cts +15 -0
  119. package/dist/ui/AssetManagerUI.d.cts.map +1 -0
  120. package/dist/ui/AssetManagerUI.d.ts +15 -0
  121. package/dist/ui/AssetManagerUI.d.ts.map +1 -0
  122. package/dist/ui/AssetManagerUI.js +135 -0
  123. package/dist/ui/DeleteAssetDialog.cjs +70 -0
  124. package/dist/ui/DeleteAssetDialog.d.cts +22 -0
  125. package/dist/ui/DeleteAssetDialog.d.cts.map +1 -0
  126. package/dist/ui/DeleteAssetDialog.d.ts +22 -0
  127. package/dist/ui/DeleteAssetDialog.d.ts.map +1 -0
  128. package/dist/ui/DeleteAssetDialog.js +36 -0
  129. package/dist/ui/MetadataPanel.cjs +147 -0
  130. package/dist/ui/MetadataPanel.d.cts +21 -0
  131. package/dist/ui/MetadataPanel.d.cts.map +1 -0
  132. package/dist/ui/MetadataPanel.d.ts +21 -0
  133. package/dist/ui/MetadataPanel.d.ts.map +1 -0
  134. package/dist/ui/MetadataPanel.js +113 -0
  135. package/dist/ui/ReplaceAssetDialog.cjs +125 -0
  136. package/dist/ui/ReplaceAssetDialog.d.cts +14 -0
  137. package/dist/ui/ReplaceAssetDialog.d.cts.map +1 -0
  138. package/dist/ui/ReplaceAssetDialog.d.ts +14 -0
  139. package/dist/ui/ReplaceAssetDialog.d.ts.map +1 -0
  140. package/dist/ui/ReplaceAssetDialog.js +91 -0
  141. package/dist/ui/UploadButton.cjs +189 -0
  142. package/dist/ui/UploadButton.d.cts +17 -0
  143. package/dist/ui/UploadButton.d.cts.map +1 -0
  144. package/dist/ui/UploadButton.d.ts +17 -0
  145. package/dist/ui/UploadButton.d.ts.map +1 -0
  146. package/dist/ui/UploadButton.js +155 -0
  147. package/dist/ui/index.cjs +60 -0
  148. package/dist/ui/index.d.cts +15 -0
  149. package/dist/ui/index.d.cts.map +1 -0
  150. package/dist/ui/index.d.ts +15 -0
  151. package/dist/ui/index.d.ts.map +1 -0
  152. package/dist/ui/index.js +7 -0
  153. package/dist/validate-upload-result.cjs +149 -0
  154. package/dist/validate-upload-result.d.cts +9 -0
  155. package/dist/validate-upload-result.d.cts.map +1 -0
  156. package/dist/validate-upload-result.d.ts +9 -0
  157. package/dist/validate-upload-result.d.ts.map +1 -0
  158. package/dist/validate-upload-result.js +115 -0
  159. package/package.json +131 -0
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ createAssetRegistry: ()=>createAssetRegistry
28
+ });
29
+ const external_infer_kind_cjs_namespaceObject = require("./infer-kind.cjs");
30
+ const DEFAULT_SEARCH_LIMIT = 50;
31
+ function createAssetRegistry() {
32
+ const assetsById = new Map();
33
+ const listeners = new Set();
34
+ const notify = ()=>{
35
+ for (const listener of listeners)listener();
36
+ };
37
+ return {
38
+ register (asset) {
39
+ const stored = freezeUploadResult(asset);
40
+ assetsById.set(stored.id, stored);
41
+ notify();
42
+ return stored;
43
+ },
44
+ get (id) {
45
+ return assetsById.get(id);
46
+ },
47
+ list () {
48
+ return Object.freeze([
49
+ ...assetsById.values()
50
+ ]);
51
+ },
52
+ delete (id) {
53
+ const removed = assetsById.delete(id);
54
+ if (removed) notify();
55
+ return removed;
56
+ },
57
+ rename (id, name) {
58
+ const current = assetsById.get(id);
59
+ if (void 0 === current) return;
60
+ const trimmed = name.trim();
61
+ const next = freezeUploadResult({
62
+ ...current,
63
+ ..."" === trimmed ? {} : {
64
+ name: trimmed
65
+ }
66
+ });
67
+ assetsById.set(id, next);
68
+ notify();
69
+ return next;
70
+ },
71
+ replace (id, next) {
72
+ if (!assetsById.has(id)) return;
73
+ const merged = freezeUploadResult({
74
+ ...next,
75
+ id
76
+ });
77
+ assetsById.set(id, merged);
78
+ notify();
79
+ return merged;
80
+ },
81
+ setTags (id, tags) {
82
+ const current = assetsById.get(id);
83
+ if (void 0 === current) return;
84
+ const normalized = normalizeTags(tags);
85
+ const { tags: _existing, ...rest } = current;
86
+ const next = freezeUploadResult(normalized.length > 0 ? {
87
+ ...rest,
88
+ tags: normalized
89
+ } : rest);
90
+ assetsById.set(id, next);
91
+ notify();
92
+ return next;
93
+ },
94
+ search (options) {
95
+ return runSearch(assetsById, options ?? {});
96
+ },
97
+ subscribe (listener) {
98
+ listeners.add(listener);
99
+ return ()=>{
100
+ listeners.delete(listener);
101
+ };
102
+ }
103
+ };
104
+ }
105
+ function freezeUploadResult(asset) {
106
+ const nextAsset = {
107
+ id: asset.id,
108
+ url: asset.url,
109
+ ...void 0 !== asset.name ? {
110
+ name: asset.name
111
+ } : {},
112
+ ...asset.meta ? {
113
+ meta: Object.freeze({
114
+ ...void 0 !== asset.meta.size ? {
115
+ size: asset.meta.size
116
+ } : {},
117
+ ...void 0 !== asset.meta.mimeType ? {
118
+ mimeType: asset.meta.mimeType
119
+ } : {},
120
+ ...void 0 !== asset.meta.width ? {
121
+ width: asset.meta.width
122
+ } : {},
123
+ ...void 0 !== asset.meta.height ? {
124
+ height: asset.meta.height
125
+ } : {}
126
+ })
127
+ } : {},
128
+ ...asset.tags && asset.tags.length > 0 ? {
129
+ tags: Object.freeze(normalizeTags(asset.tags))
130
+ } : {}
131
+ };
132
+ return Object.freeze(nextAsset);
133
+ }
134
+ function normalizeTags(tags) {
135
+ const seen = new Set();
136
+ const out = [];
137
+ for (const raw of tags){
138
+ const tag = raw.trim().toLowerCase();
139
+ if ("" !== tag) {
140
+ if (!seen.has(tag)) {
141
+ seen.add(tag);
142
+ out.push(tag);
143
+ }
144
+ }
145
+ }
146
+ return out;
147
+ }
148
+ function runSearch(assetsById, options) {
149
+ const query = options.query?.trim().toLowerCase() ?? "";
150
+ const kindFilter = options.kinds && options.kinds.length > 0 ? options.kinds : void 0;
151
+ const tagFilter = options.tags && options.tags.length > 0 ? options.tags.map((t)=>t.trim().toLowerCase()).filter((t)=>"" !== t) : void 0;
152
+ const matches = [];
153
+ for (const entry of assetsById.values()){
154
+ if (matchesQuery(entry, query)) {
155
+ if (void 0 === kindFilter || kindFilter.includes((0, external_infer_kind_cjs_namespaceObject.inferAssetKind)(entry))) {
156
+ if (void 0 === tagFilter || matchesAllTags(entry, tagFilter)) matches.push(entry);
157
+ }
158
+ }
159
+ }
160
+ const total = matches.length;
161
+ const limit = void 0 !== options.limit && options.limit > 0 ? options.limit : DEFAULT_SEARCH_LIMIT;
162
+ const offset = parseCursor(options.cursor);
163
+ const slice = matches.slice(offset, offset + limit);
164
+ const nextOffset = offset + slice.length;
165
+ const nextCursor = nextOffset < total ? String(nextOffset) : void 0;
166
+ return Object.freeze({
167
+ items: Object.freeze(slice),
168
+ total,
169
+ nextCursor
170
+ });
171
+ }
172
+ function matchesQuery(entry, query) {
173
+ if ("" === query) return true;
174
+ if (entry.id.toLowerCase().includes(query)) return true;
175
+ if (entry.name?.toLowerCase().includes(query)) return true;
176
+ if (entry.meta?.mimeType?.toLowerCase().includes(query)) return true;
177
+ if (entry.tags?.some((tag)=>tag.toLowerCase().includes(query))) return true;
178
+ return false;
179
+ }
180
+ function matchesAllTags(entry, required) {
181
+ if (void 0 === entry.tags || 0 === entry.tags.length) return false;
182
+ const have = new Set(entry.tags.map((t)=>t.toLowerCase()));
183
+ for (const tag of required)if (!have.has(tag)) return false;
184
+ return true;
185
+ }
186
+ function parseCursor(cursor) {
187
+ if (void 0 === cursor) return 0;
188
+ const value = Number.parseInt(cursor, 10);
189
+ if (!Number.isFinite(value) || value < 0) return 0;
190
+ return value;
191
+ }
192
+ exports.createAssetRegistry = __webpack_exports__.createAssetRegistry;
193
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
194
+ "createAssetRegistry"
195
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
196
+ Object.defineProperty(exports, '__esModule', {
197
+ value: true
198
+ });
@@ -0,0 +1,3 @@
1
+ import type { AssetRegistry } from "./types.js";
2
+ export declare function createAssetRegistry(): AssetRegistry;
3
+ //# sourceMappingURL=registry.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.cts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,aAAa,EAKb,MAAM,YAAY,CAAC;AAIpB,wBAAgB,mBAAmB,IAAI,aAAa,CA6EnD"}
@@ -0,0 +1,3 @@
1
+ import type { AssetRegistry } from "./types.js";
2
+ export declare function createAssetRegistry(): AssetRegistry;
3
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,aAAa,EAKb,MAAM,YAAY,CAAC;AAIpB,wBAAgB,mBAAmB,IAAI,aAAa,CA6EnD"}
@@ -0,0 +1,164 @@
1
+ import { inferAssetKind } from "./infer-kind.js";
2
+ const DEFAULT_SEARCH_LIMIT = 50;
3
+ function createAssetRegistry() {
4
+ const assetsById = new Map();
5
+ const listeners = new Set();
6
+ const notify = ()=>{
7
+ for (const listener of listeners)listener();
8
+ };
9
+ return {
10
+ register (asset) {
11
+ const stored = freezeUploadResult(asset);
12
+ assetsById.set(stored.id, stored);
13
+ notify();
14
+ return stored;
15
+ },
16
+ get (id) {
17
+ return assetsById.get(id);
18
+ },
19
+ list () {
20
+ return Object.freeze([
21
+ ...assetsById.values()
22
+ ]);
23
+ },
24
+ delete (id) {
25
+ const removed = assetsById.delete(id);
26
+ if (removed) notify();
27
+ return removed;
28
+ },
29
+ rename (id, name) {
30
+ const current = assetsById.get(id);
31
+ if (void 0 === current) return;
32
+ const trimmed = name.trim();
33
+ const next = freezeUploadResult({
34
+ ...current,
35
+ ..."" === trimmed ? {} : {
36
+ name: trimmed
37
+ }
38
+ });
39
+ assetsById.set(id, next);
40
+ notify();
41
+ return next;
42
+ },
43
+ replace (id, next) {
44
+ if (!assetsById.has(id)) return;
45
+ const merged = freezeUploadResult({
46
+ ...next,
47
+ id
48
+ });
49
+ assetsById.set(id, merged);
50
+ notify();
51
+ return merged;
52
+ },
53
+ setTags (id, tags) {
54
+ const current = assetsById.get(id);
55
+ if (void 0 === current) return;
56
+ const normalized = normalizeTags(tags);
57
+ const { tags: _existing, ...rest } = current;
58
+ const next = freezeUploadResult(normalized.length > 0 ? {
59
+ ...rest,
60
+ tags: normalized
61
+ } : rest);
62
+ assetsById.set(id, next);
63
+ notify();
64
+ return next;
65
+ },
66
+ search (options) {
67
+ return runSearch(assetsById, options ?? {});
68
+ },
69
+ subscribe (listener) {
70
+ listeners.add(listener);
71
+ return ()=>{
72
+ listeners.delete(listener);
73
+ };
74
+ }
75
+ };
76
+ }
77
+ function freezeUploadResult(asset) {
78
+ const nextAsset = {
79
+ id: asset.id,
80
+ url: asset.url,
81
+ ...void 0 !== asset.name ? {
82
+ name: asset.name
83
+ } : {},
84
+ ...asset.meta ? {
85
+ meta: Object.freeze({
86
+ ...void 0 !== asset.meta.size ? {
87
+ size: asset.meta.size
88
+ } : {},
89
+ ...void 0 !== asset.meta.mimeType ? {
90
+ mimeType: asset.meta.mimeType
91
+ } : {},
92
+ ...void 0 !== asset.meta.width ? {
93
+ width: asset.meta.width
94
+ } : {},
95
+ ...void 0 !== asset.meta.height ? {
96
+ height: asset.meta.height
97
+ } : {}
98
+ })
99
+ } : {},
100
+ ...asset.tags && asset.tags.length > 0 ? {
101
+ tags: Object.freeze(normalizeTags(asset.tags))
102
+ } : {}
103
+ };
104
+ return Object.freeze(nextAsset);
105
+ }
106
+ function normalizeTags(tags) {
107
+ const seen = new Set();
108
+ const out = [];
109
+ for (const raw of tags){
110
+ const tag = raw.trim().toLowerCase();
111
+ if ("" !== tag) {
112
+ if (!seen.has(tag)) {
113
+ seen.add(tag);
114
+ out.push(tag);
115
+ }
116
+ }
117
+ }
118
+ return out;
119
+ }
120
+ function runSearch(assetsById, options) {
121
+ const query = options.query?.trim().toLowerCase() ?? "";
122
+ const kindFilter = options.kinds && options.kinds.length > 0 ? options.kinds : void 0;
123
+ const tagFilter = options.tags && options.tags.length > 0 ? options.tags.map((t)=>t.trim().toLowerCase()).filter((t)=>"" !== t) : void 0;
124
+ const matches = [];
125
+ for (const entry of assetsById.values()){
126
+ if (matchesQuery(entry, query)) {
127
+ if (void 0 === kindFilter || kindFilter.includes(inferAssetKind(entry))) {
128
+ if (void 0 === tagFilter || matchesAllTags(entry, tagFilter)) matches.push(entry);
129
+ }
130
+ }
131
+ }
132
+ const total = matches.length;
133
+ const limit = void 0 !== options.limit && options.limit > 0 ? options.limit : DEFAULT_SEARCH_LIMIT;
134
+ const offset = parseCursor(options.cursor);
135
+ const slice = matches.slice(offset, offset + limit);
136
+ const nextOffset = offset + slice.length;
137
+ const nextCursor = nextOffset < total ? String(nextOffset) : void 0;
138
+ return Object.freeze({
139
+ items: Object.freeze(slice),
140
+ total,
141
+ nextCursor
142
+ });
143
+ }
144
+ function matchesQuery(entry, query) {
145
+ if ("" === query) return true;
146
+ if (entry.id.toLowerCase().includes(query)) return true;
147
+ if (entry.name?.toLowerCase().includes(query)) return true;
148
+ if (entry.meta?.mimeType?.toLowerCase().includes(query)) return true;
149
+ if (entry.tags?.some((tag)=>tag.toLowerCase().includes(query))) return true;
150
+ return false;
151
+ }
152
+ function matchesAllTags(entry, required) {
153
+ if (void 0 === entry.tags || 0 === entry.tags.length) return false;
154
+ const have = new Set(entry.tags.map((t)=>t.toLowerCase()));
155
+ for (const tag of required)if (!have.has(tag)) return false;
156
+ return true;
157
+ }
158
+ function parseCursor(cursor) {
159
+ if (void 0 === cursor) return 0;
160
+ const value = Number.parseInt(cursor, 10);
161
+ if (!Number.isFinite(value) || value < 0) return 0;
162
+ return value;
163
+ }
164
+ export { createAssetRegistry };
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ resolveAssets: ()=>resolveAssets,
28
+ createIRAssetResolver: ()=>createIRAssetResolver
29
+ });
30
+ const external_errors_cjs_namespaceObject = require("./errors.cjs");
31
+ const external_validate_upload_result_cjs_namespaceObject = require("./validate-upload-result.cjs");
32
+ const URL_REJECTION_VALIDATION_CODES = new Set([
33
+ "EMPTY_UPLOAD_URL",
34
+ "UNSCHEMED_UPLOAD_URL",
35
+ "DISALLOWED_UPLOAD_URL_SCHEME",
36
+ "INVALID_UPLOAD_ID",
37
+ "PATH_TRAVERSAL_URL",
38
+ "MIXED_SCRIPT_HOSTNAME"
39
+ ]);
40
+ const ASSET_REFERENCE_PREFIX = "asset://";
41
+ const ASSET_PROP_KEYS = new Set([
42
+ "src",
43
+ "imageUrl",
44
+ "imageSrc",
45
+ "url",
46
+ "videoUrl",
47
+ "videoSrc",
48
+ "fontUrl",
49
+ "scriptUrl",
50
+ "styleUrl",
51
+ "backgroundSrc",
52
+ "backgroundImage",
53
+ "poster",
54
+ "thumbnailSrc"
55
+ ]);
56
+ function createIRAssetResolver(options) {
57
+ return (url)=>{
58
+ const assetId = parseAssetReference(url);
59
+ if (null === assetId) return null;
60
+ const asset = options.registry.get(assetId);
61
+ if (!asset) throw new external_errors_cjs_namespaceObject.AssetResolutionError(assetId, "ASSET_NOT_FOUND");
62
+ try {
63
+ const validated = (0, external_validate_upload_result_cjs_namespaceObject.validateUploadResult)({
64
+ id: asset.id,
65
+ url: asset.url,
66
+ ...asset.meta ? {
67
+ meta: asset.meta
68
+ } : {}
69
+ }, {
70
+ dataUrlAllowlistOptIn: options.dataUrlAllowlistOptIn,
71
+ allowMixedScriptHostnames: options.allowMixedScriptHostnames
72
+ });
73
+ const resolution = {
74
+ url: validated.url,
75
+ ...validated.meta ? {
76
+ meta: validated.meta
77
+ } : {}
78
+ };
79
+ return resolution;
80
+ } catch (error) {
81
+ const message = error instanceof Error ? error.message : `Could not resolve asset "${assetId}"`;
82
+ const code = error instanceof external_errors_cjs_namespaceObject.AssetValidationError && URL_REJECTION_VALIDATION_CODES.has(error.code) ? "ASSET_URL_REJECTED" : "ASSET_VALIDATION_FAILED";
83
+ throw new external_errors_cjs_namespaceObject.AssetResolutionError(assetId, code, message, {
84
+ cause: error
85
+ });
86
+ }
87
+ };
88
+ }
89
+ async function resolveAssets(ir, resolver) {
90
+ const rewriteMap = new Map();
91
+ const assetUrls = collectAssetUrls(ir);
92
+ for (const url of assetUrls){
93
+ const resolution = await resolver(url);
94
+ if (null !== resolution) rewriteMap.set(url, resolution);
95
+ }
96
+ const nextIr = {
97
+ version: ir.version,
98
+ root: cloneNode(ir.root, rewriteMap),
99
+ assets: ir.assets.map((asset)=>cloneAsset(asset, rewriteMap)),
100
+ metadata: {
101
+ ...ir.metadata
102
+ }
103
+ };
104
+ return deepFreeze(nextIr);
105
+ }
106
+ function collectAssetUrls(ir) {
107
+ const urls = new Set();
108
+ for (const asset of ir.assets)if ("" !== asset.url.trim()) urls.add(asset.url);
109
+ collectNodeAssetUrls(ir.root, urls);
110
+ return urls;
111
+ }
112
+ function collectNodeAssetUrls(node, urls) {
113
+ collectValueAssetUrls(node.props, urls);
114
+ if (node.assets) {
115
+ for (const asset of node.assets)if ("" !== asset.url.trim()) urls.add(asset.url);
116
+ }
117
+ if (node.children) for (const child of node.children)collectNodeAssetUrls(child, urls);
118
+ }
119
+ function collectValueAssetUrls(value, urls, key) {
120
+ if (Array.isArray(value)) {
121
+ for (const item of value)collectValueAssetUrls(item, urls);
122
+ return;
123
+ }
124
+ if ("string" == typeof value) {
125
+ if (void 0 !== key && ASSET_PROP_KEYS.has(key) && "" !== value.trim()) urls.add(value);
126
+ return;
127
+ }
128
+ if (null === value || "object" != typeof value) return;
129
+ for (const [entryKey, entryValue] of Object.entries(value))collectValueAssetUrls(entryValue, urls, entryKey);
130
+ }
131
+ function parseAssetReference(url) {
132
+ if (!url.startsWith(ASSET_REFERENCE_PREFIX)) return null;
133
+ const assetId = url.slice(ASSET_REFERENCE_PREFIX.length).trim();
134
+ return "" === assetId ? null : assetId;
135
+ }
136
+ function cloneNode(node, rewriteMap) {
137
+ return {
138
+ id: node.id,
139
+ type: node.type,
140
+ props: cloneValue(node.props, rewriteMap),
141
+ ...node.children ? {
142
+ children: node.children.map((child)=>cloneNode(child, rewriteMap))
143
+ } : {},
144
+ ...node.assets ? {
145
+ assets: node.assets.map((asset)=>cloneAsset(asset, rewriteMap))
146
+ } : {}
147
+ };
148
+ }
149
+ function cloneAsset(asset, rewriteMap) {
150
+ const resolution = rewriteMap.get(asset.url);
151
+ return {
152
+ id: asset.id,
153
+ kind: asset.kind,
154
+ url: resolution?.url ?? asset.url,
155
+ ...resolution?.meta ?? asset.meta ? {
156
+ meta: resolution?.meta ?? asset.meta
157
+ } : {}
158
+ };
159
+ }
160
+ function cloneValue(value, rewriteMap, key) {
161
+ if (Array.isArray(value)) return value.map((item)=>cloneValue(item, rewriteMap));
162
+ if ("string" == typeof value) {
163
+ if (void 0 !== key && !ASSET_PROP_KEYS.has(key)) return value;
164
+ return rewriteMap.get(value)?.url ?? value;
165
+ }
166
+ if (null === value || "object" != typeof value) return value;
167
+ const nextValue = {};
168
+ for (const [entryKey, entryValue] of Object.entries(value))nextValue[entryKey] = cloneValue(entryValue, rewriteMap, entryKey);
169
+ return nextValue;
170
+ }
171
+ function deepFreeze(value) {
172
+ if (null === value || "object" != typeof value || Object.isFrozen(value)) return value;
173
+ Object.freeze(value);
174
+ for (const entry of Object.values(value))deepFreeze(entry);
175
+ return value;
176
+ }
177
+ exports.createIRAssetResolver = __webpack_exports__.createIRAssetResolver;
178
+ exports.resolveAssets = __webpack_exports__.resolveAssets;
179
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
180
+ "createIRAssetResolver",
181
+ "resolveAssets"
182
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
183
+ Object.defineProperty(exports, '__esModule', {
184
+ value: true
185
+ });
@@ -0,0 +1,10 @@
1
+ import type { IRAssetResolver, PageIR } from "@anvilkit/core/types";
2
+ import type { AssetRegistry } from "./types.js";
3
+ export interface CreateIRAssetResolverOptions {
4
+ readonly registry: AssetRegistry;
5
+ readonly dataUrlAllowlistOptIn?: boolean;
6
+ readonly allowMixedScriptHostnames?: boolean;
7
+ }
8
+ export declare function createIRAssetResolver(options: CreateIRAssetResolverOptions): IRAssetResolver;
9
+ export declare function resolveAssets(ir: PageIR, resolver: IRAssetResolver): Promise<PageIR>;
10
+ //# sourceMappingURL=resolver.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.d.cts","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEX,eAAe,EACf,MAAM,EAGN,MAAM,sBAAsB,CAAC;AAG9B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AA6BhD,MAAM,WAAW,4BAA4B;IAC5C,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAC;IACjC,QAAQ,CAAC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IACzC,QAAQ,CAAC,yBAAyB,CAAC,EAAE,OAAO,CAAC;CAC7C;AAED,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,4BAA4B,GACnC,eAAe,CAgDjB;AAED,wBAAsB,aAAa,CAClC,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,eAAe,GACvB,OAAO,CAAC,MAAM,CAAC,CAmBjB"}
@@ -0,0 +1,10 @@
1
+ import type { IRAssetResolver, PageIR } from "@anvilkit/core/types";
2
+ import type { AssetRegistry } from "./types.js";
3
+ export interface CreateIRAssetResolverOptions {
4
+ readonly registry: AssetRegistry;
5
+ readonly dataUrlAllowlistOptIn?: boolean;
6
+ readonly allowMixedScriptHostnames?: boolean;
7
+ }
8
+ export declare function createIRAssetResolver(options: CreateIRAssetResolverOptions): IRAssetResolver;
9
+ export declare function resolveAssets(ir: PageIR, resolver: IRAssetResolver): Promise<PageIR>;
10
+ //# sourceMappingURL=resolver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEX,eAAe,EACf,MAAM,EAGN,MAAM,sBAAsB,CAAC;AAG9B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AA6BhD,MAAM,WAAW,4BAA4B;IAC5C,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAC;IACjC,QAAQ,CAAC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IACzC,QAAQ,CAAC,yBAAyB,CAAC,EAAE,OAAO,CAAC;CAC7C;AAED,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,4BAA4B,GACnC,eAAe,CAgDjB;AAED,wBAAsB,aAAa,CAClC,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,eAAe,GACvB,OAAO,CAAC,MAAM,CAAC,CAmBjB"}