renderscreenshot 1.0.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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +366 -0
  3. package/dist/cjs/cache.js +125 -0
  4. package/dist/cjs/cache.js.map +1 -0
  5. package/dist/cjs/client.js +304 -0
  6. package/dist/cjs/client.js.map +1 -0
  7. package/dist/cjs/errors.js +85 -0
  8. package/dist/cjs/errors.js.map +1 -0
  9. package/dist/cjs/index.js +44 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/options.js +659 -0
  12. package/dist/cjs/options.js.map +1 -0
  13. package/dist/cjs/types.js +3 -0
  14. package/dist/cjs/types.js.map +1 -0
  15. package/dist/cjs/webhooks.js +152 -0
  16. package/dist/cjs/webhooks.js.map +1 -0
  17. package/dist/esm/cache.js +121 -0
  18. package/dist/esm/cache.js.map +1 -0
  19. package/dist/esm/client.js +300 -0
  20. package/dist/esm/client.js.map +1 -0
  21. package/dist/esm/errors.js +81 -0
  22. package/dist/esm/errors.js.map +1 -0
  23. package/dist/esm/index.js +34 -0
  24. package/dist/esm/index.js.map +1 -0
  25. package/dist/esm/options.js +655 -0
  26. package/dist/esm/options.js.map +1 -0
  27. package/dist/esm/types.js +2 -0
  28. package/dist/esm/types.js.map +1 -0
  29. package/dist/esm/webhooks.js +147 -0
  30. package/dist/esm/webhooks.js.map +1 -0
  31. package/dist/types/cache.d.ts +96 -0
  32. package/dist/types/cache.d.ts.map +1 -0
  33. package/dist/types/client.d.ts +147 -0
  34. package/dist/types/client.d.ts.map +1 -0
  35. package/dist/types/errors.d.ts +51 -0
  36. package/dist/types/errors.d.ts.map +1 -0
  37. package/dist/types/index.d.ts +35 -0
  38. package/dist/types/index.d.ts.map +1 -0
  39. package/dist/types/options.d.ts +265 -0
  40. package/dist/types/options.d.ts.map +1 -0
  41. package/dist/types/types.d.ts +249 -0
  42. package/dist/types/types.d.ts.map +1 -0
  43. package/dist/types/webhooks.d.ts +60 -0
  44. package/dist/types/webhooks.d.ts.map +1 -0
  45. package/package.json +84 -0
@@ -0,0 +1,304 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Client = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const errors_js_1 = require("./errors.js");
6
+ const options_js_1 = require("./options.js");
7
+ const cache_js_1 = require("./cache.js");
8
+ const DEFAULT_BASE_URL = 'https://api.renderscreenshot.com';
9
+ const DEFAULT_TIMEOUT = 30000;
10
+ const API_VERSION = 'v1';
11
+ /**
12
+ * Parse a response body from the API
13
+ */
14
+ async function parseResponseBody(response) {
15
+ const contentType = response.headers.get('content-type') ?? '';
16
+ if (contentType.includes('application/json')) {
17
+ return response.json();
18
+ }
19
+ return response.text();
20
+ }
21
+ /**
22
+ * Handle API errors and convert to RenderScreenshotError
23
+ */
24
+ async function handleApiError(response) {
25
+ const retryAfterHeader = response.headers.get('retry-after');
26
+ const retryAfter = retryAfterHeader !== null ? parseInt(retryAfterHeader, 10) : undefined;
27
+ const body = await parseResponseBody(response);
28
+ const errorBody = typeof body === 'object' && body !== null ? body : {};
29
+ const errorParams = {};
30
+ if (typeof errorBody['code'] === 'string')
31
+ errorParams.code = errorBody['code'];
32
+ if (typeof errorBody['message'] === 'string')
33
+ errorParams.message = errorBody['message'];
34
+ if (typeof errorBody['error'] === 'string')
35
+ errorParams.error = errorBody['error'];
36
+ throw errors_js_1.RenderScreenshotError.fromResponse(response.status, errorParams, retryAfter);
37
+ }
38
+ /**
39
+ * RenderScreenshot API client
40
+ */
41
+ class Client {
42
+ apiKey;
43
+ baseUrl;
44
+ timeout;
45
+ /** Cache management methods */
46
+ cache;
47
+ /**
48
+ * Create a new RenderScreenshot client
49
+ * @param apiKey - Your API key (rs_live_* or rs_test_*)
50
+ * @param options - Optional client configuration
51
+ */
52
+ constructor(apiKey, options = {}) {
53
+ if (apiKey === '' || apiKey === undefined) {
54
+ throw new Error('API key is required');
55
+ }
56
+ this.apiKey = apiKey;
57
+ this.baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
58
+ this.timeout = options.timeout ?? DEFAULT_TIMEOUT;
59
+ this.cache = new cache_js_1.CacheManager(this);
60
+ }
61
+ /**
62
+ * Get the API key for internal use
63
+ * @internal
64
+ */
65
+ getApiKey() {
66
+ return this.apiKey;
67
+ }
68
+ /**
69
+ * Get the base URL for internal use
70
+ * @internal
71
+ */
72
+ getBaseUrl() {
73
+ return this.baseUrl;
74
+ }
75
+ /**
76
+ * Get the timeout for internal use
77
+ * @internal
78
+ */
79
+ getTimeout() {
80
+ return this.timeout;
81
+ }
82
+ /**
83
+ * Make an authenticated request to the API
84
+ * @internal
85
+ */
86
+ async request(method, path, options = {}) {
87
+ const url = `${this.baseUrl}/${API_VERSION}${path}`;
88
+ const controller = new AbortController();
89
+ const timeoutId = setTimeout(() => {
90
+ controller.abort();
91
+ }, this.timeout);
92
+ try {
93
+ const headers = {
94
+ Authorization: `Bearer ${this.apiKey}`,
95
+ 'User-Agent': 'renderscreenshot-node/1.0.0',
96
+ ...options.headers,
97
+ };
98
+ if (options.body !== undefined) {
99
+ headers['Content-Type'] = 'application/json';
100
+ }
101
+ const fetchOptions = {
102
+ method,
103
+ headers,
104
+ signal: controller.signal,
105
+ };
106
+ if (options.body !== undefined) {
107
+ fetchOptions.body = JSON.stringify(options.body);
108
+ }
109
+ const response = await fetch(url, fetchOptions);
110
+ if (!response.ok) {
111
+ await handleApiError(response);
112
+ }
113
+ if (options.responseType === 'buffer') {
114
+ const arrayBuffer = await response.arrayBuffer();
115
+ return Buffer.from(arrayBuffer);
116
+ }
117
+ return (await response.json());
118
+ }
119
+ catch (error) {
120
+ if (error instanceof errors_js_1.RenderScreenshotError) {
121
+ throw error;
122
+ }
123
+ if (error instanceof Error && error.name === 'AbortError') {
124
+ throw errors_js_1.RenderScreenshotError.timeout();
125
+ }
126
+ throw errors_js_1.RenderScreenshotError.internal(error instanceof Error ? error.message : 'Unknown error');
127
+ }
128
+ finally {
129
+ clearTimeout(timeoutId);
130
+ }
131
+ }
132
+ /**
133
+ * Generate a signed URL for embedding screenshots
134
+ *
135
+ * @param options - Screenshot options (TakeOptions instance or config object)
136
+ * @param expiresAt - Expiration time for the signed URL
137
+ * @returns Signed URL string
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * const url = client.generateUrl(
142
+ * TakeOptions.url('https://example.com').preset('og_card'),
143
+ * new Date(Date.now() + 24 * 60 * 60 * 1000) // 24 hours
144
+ * );
145
+ * ```
146
+ */
147
+ generateUrl(options, expiresAt) {
148
+ const config = options instanceof options_js_1.TakeOptions ? options.toConfig() : options;
149
+ // Extract the public key from api key (rs_pub_xxx or use the full key for signing)
150
+ // For signed URLs, we need a secret key (rs_secret_xxx) and a public key (rs_pub_xxx)
151
+ // If user provides rs_live_xxx, we'll use it directly
152
+ // Build query params from config
153
+ const params = {};
154
+ if (config.url !== undefined)
155
+ params['url'] = config.url;
156
+ if (config.width !== undefined)
157
+ params['width'] = String(config.width);
158
+ if (config.height !== undefined)
159
+ params['height'] = String(config.height);
160
+ if (config.scale !== undefined)
161
+ params['scale'] = String(config.scale);
162
+ if (config.mobile !== undefined)
163
+ params['mobile'] = String(config.mobile);
164
+ if (config.fullPage !== undefined)
165
+ params['full_page'] = String(config.fullPage);
166
+ if (config.element !== undefined)
167
+ params['element'] = config.element;
168
+ if (config.format !== undefined)
169
+ params['format'] = config.format;
170
+ if (config.quality !== undefined)
171
+ params['quality'] = String(config.quality);
172
+ if (config.preset !== undefined)
173
+ params['preset'] = config.preset;
174
+ if (config.device !== undefined)
175
+ params['device'] = config.device;
176
+ if (config.waitFor !== undefined)
177
+ params['wait_for'] = config.waitFor;
178
+ if (config.delay !== undefined)
179
+ params['delay'] = String(config.delay);
180
+ if (config.blockAds !== undefined)
181
+ params['block_ads'] = String(config.blockAds);
182
+ if (config.blockTrackers !== undefined)
183
+ params['block_trackers'] = String(config.blockTrackers);
184
+ if (config.blockCookieBanners !== undefined)
185
+ params['block_cookie_banners'] = String(config.blockCookieBanners);
186
+ if (config.blockChatWidgets !== undefined)
187
+ params['block_chat_widgets'] = String(config.blockChatWidgets);
188
+ if (config.darkMode !== undefined)
189
+ params['dark_mode'] = String(config.darkMode);
190
+ if (config.cacheTtl !== undefined)
191
+ params['cache_ttl'] = String(config.cacheTtl);
192
+ // Sort params alphabetically and create canonical string
193
+ const sortedKeys = Object.keys(params).sort();
194
+ const canonical = sortedKeys.map((k) => `${k}=${params[k]}`).join('&');
195
+ // Add expiration
196
+ const expires = Math.floor(expiresAt.getTime() / 1000);
197
+ const message = `${canonical}&expires=${expires}`;
198
+ // Generate signature
199
+ const signature = (0, node_crypto_1.createHmac)('sha256', this.apiKey).update(message).digest('hex');
200
+ return `${this.baseUrl}/${API_VERSION}/screenshot?${message}&signature=${signature}`;
201
+ }
202
+ /**
203
+ * Take a screenshot and return the binary data
204
+ *
205
+ * @param options - Screenshot options (TakeOptions instance or config object)
206
+ * @returns Buffer containing the screenshot binary data
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * const image = await client.take(
211
+ * TakeOptions.url('https://example.com').preset('og_card')
212
+ * );
213
+ * await fs.writeFile('screenshot.png', image);
214
+ * ```
215
+ */
216
+ async take(options) {
217
+ const params = options instanceof options_js_1.TakeOptions ? options.toParams() : options_js_1.TakeOptions.from(options).toParams();
218
+ return this.request('POST', '/screenshot', {
219
+ body: params,
220
+ responseType: 'buffer',
221
+ });
222
+ }
223
+ /**
224
+ * Take a screenshot and return JSON metadata with URLs
225
+ *
226
+ * @param options - Screenshot options (TakeOptions instance or config object)
227
+ * @returns Screenshot response with metadata and URLs
228
+ *
229
+ * @example
230
+ * ```typescript
231
+ * const response = await client.takeJson(
232
+ * TakeOptions.url('https://example.com').preset('og_card')
233
+ * );
234
+ * console.log(response.url); // CDN URL to the screenshot
235
+ * ```
236
+ */
237
+ async takeJson(options) {
238
+ const params = options instanceof options_js_1.TakeOptions ? options.toParams() : options_js_1.TakeOptions.from(options).toParams();
239
+ return this.request('POST', '/screenshot', {
240
+ body: { ...params, response_type: 'json' },
241
+ });
242
+ }
243
+ async batch(urlsOrRequests, options) {
244
+ // Type guard to check if it's an array of strings (URLs)
245
+ const isUrlArray = (arr) => arr.every((item) => typeof item === 'string');
246
+ let body;
247
+ if (isUrlArray(urlsOrRequests)) {
248
+ const opts = options instanceof options_js_1.TakeOptions
249
+ ? options.toParams()
250
+ : options !== undefined
251
+ ? options_js_1.TakeOptions.from(options).toParams()
252
+ : {};
253
+ body = {
254
+ urls: urlsOrRequests,
255
+ options: opts,
256
+ };
257
+ }
258
+ else {
259
+ body = {
260
+ requests: urlsOrRequests.map((req) => ({
261
+ url: req.url,
262
+ ...options_js_1.TakeOptions.from(req.options ?? {}).toParams(),
263
+ })),
264
+ };
265
+ }
266
+ return this.request('POST', '/batch', { body });
267
+ }
268
+ /**
269
+ * Get the status of a batch job
270
+ *
271
+ * @param batchId - The batch job ID
272
+ * @returns Batch status and results
273
+ */
274
+ async getBatch(batchId) {
275
+ return this.request('GET', `/batch/${batchId}`);
276
+ }
277
+ /**
278
+ * List all available presets
279
+ *
280
+ * @returns Array of preset configurations
281
+ */
282
+ async presets() {
283
+ return this.request('GET', '/presets');
284
+ }
285
+ /**
286
+ * Get a single preset by ID
287
+ *
288
+ * @param id - Preset identifier
289
+ * @returns Preset configuration
290
+ */
291
+ async preset(id) {
292
+ return this.request('GET', `/presets/${id}`);
293
+ }
294
+ /**
295
+ * List all available device presets
296
+ *
297
+ * @returns Array of device configurations
298
+ */
299
+ async devices() {
300
+ return this.request('GET', '/devices');
301
+ }
302
+ }
303
+ exports.Client = Client;
304
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":";;;AAAA,6CAAyC;AAUzC,2CAAoD;AACpD,6CAA2C;AAC3C,yCAA0C;AAE1C,MAAM,gBAAgB,GAAG,kCAAkC,CA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C,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,QAAQ,EAAE;iBAClD,CAAC,CAAC;aACJ,CAAC;QACJ,CAAC;QAED,OAAO,IAAI,CAAC,OAAO,CAAgB,MAAM,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IACjE,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,QAAQ,CAAC,OAAe;QAC5B,OAAO,IAAI,CAAC,OAAO,CAAgB,KAAK,EAAE,UAAU,OAAO,EAAE,CAAC,CAAC;IACjE,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,OAAO;QACX,OAAO,IAAI,CAAC,OAAO,CAAe,KAAK,EAAE,UAAU,CAAC,CAAC;IACvD,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,MAAM,CAAC,EAAU;QACrB,OAAO,IAAI,CAAC,OAAO,CAAa,KAAK,EAAE,YAAY,EAAE,EAAE,CAAC,CAAC;IAC3D,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,OAAO;QACX,OAAO,IAAI,CAAC,OAAO,CAAe,KAAK,EAAE,UAAU,CAAC,CAAC;IACvD,CAAC;CACF;AApUD,wBAoUC"}
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RenderScreenshotError = void 0;
4
+ /**
5
+ * Mapping of error codes to their retryable status
6
+ */
7
+ const RETRYABLE_ERRORS = new Set([
8
+ 'rate_limited',
9
+ 'timeout',
10
+ 'render_failed',
11
+ 'internal_error',
12
+ ]);
13
+ /**
14
+ * Custom error class for RenderScreenshot API errors
15
+ */
16
+ class RenderScreenshotError extends Error {
17
+ /** HTTP status code */
18
+ httpStatus;
19
+ /** Error code from the API */
20
+ code;
21
+ /** Whether this error can be retried */
22
+ retryable;
23
+ /** Seconds to wait before retrying (for rate limits) */
24
+ retryAfter;
25
+ constructor(httpStatus, code, message, retryAfter) {
26
+ super(message);
27
+ this.name = 'RenderScreenshotError';
28
+ this.httpStatus = httpStatus;
29
+ this.code = code;
30
+ this.retryable = RETRYABLE_ERRORS.has(code);
31
+ if (retryAfter !== undefined) {
32
+ this.retryAfter = retryAfter;
33
+ }
34
+ // Maintains proper stack trace for where error was thrown (V8 only)
35
+ if (Error.captureStackTrace !== undefined) {
36
+ Error.captureStackTrace(this, RenderScreenshotError);
37
+ }
38
+ }
39
+ /**
40
+ * Create an error from an API response
41
+ */
42
+ static fromResponse(httpStatus, body, retryAfter) {
43
+ const code = (body.code ?? 'internal_error');
44
+ const message = body.message ?? body.error ?? 'An unknown error occurred';
45
+ return new RenderScreenshotError(httpStatus, code, message, retryAfter);
46
+ }
47
+ /**
48
+ * Create an invalid URL error
49
+ */
50
+ static invalidUrl(url) {
51
+ return new RenderScreenshotError(400, 'invalid_url', `Invalid URL provided: ${url}`);
52
+ }
53
+ /**
54
+ * Create an invalid request error
55
+ */
56
+ static invalidRequest(message) {
57
+ return new RenderScreenshotError(400, 'invalid_request', message);
58
+ }
59
+ /**
60
+ * Create an unauthorized error
61
+ */
62
+ static unauthorized() {
63
+ return new RenderScreenshotError(401, 'unauthorized', 'Invalid or missing API key');
64
+ }
65
+ /**
66
+ * Create a rate limited error
67
+ */
68
+ static rateLimited(retryAfter) {
69
+ return new RenderScreenshotError(429, 'rate_limited', 'Rate limit exceeded. Please wait before making more requests.', retryAfter);
70
+ }
71
+ /**
72
+ * Create a timeout error
73
+ */
74
+ static timeout() {
75
+ return new RenderScreenshotError(408, 'timeout', 'Screenshot request timed out');
76
+ }
77
+ /**
78
+ * Create an internal error
79
+ */
80
+ static internal(message) {
81
+ return new RenderScreenshotError(500, 'internal_error', message ?? 'An internal error occurred');
82
+ }
83
+ }
84
+ exports.RenderScreenshotError = RenderScreenshotError;
85
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":";;;AAcA;;GAEG;AACH,MAAM,gBAAgB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I,qBAAqB,CAAC,GAAG,EAAE,aAAa,EAAE,yBAAyB,GAAG,EAAE,CAAC,CAAC;IACvF,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,OAAe;QACnC,OAAO,IAAI,qBAAqB,CAAC,GAAG,EAAE,iBAAiB,EAAE,OAAO,CAAC,CAAC;IACpE,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,YAAY;QACjB,OAAO,IAAI,qBAAqB,CAAC,GAAG,EAAE,cAAc,EAAE,4BAA4B,CAAC,CAAC;IACtF,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,UAAmB;QACpC,OAAO,IAAI,qBAAqB,CAC9B,GAAG,EACH,cAAc,EACd,+DAA+D,EAC/D,UAAU,CACX,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,OAAO;QACZ,OAAO,IAAI,qBAAqB,CAAC,GAAG,EAAE,SAAS,EAAE,8BAA8B,CAAC,CAAC;IACnF,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,QAAQ,CAAC,OAAgB;QAC9B,OAAO,IAAI,qBAAqB,CAC9B,GAAG,EACH,gBAAgB,EAChB,OAAO,IAAI,4BAA4B,CACxC,CAAC;IACJ,CAAC;CACF;AA5FD,sDA4FC"}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ /**
3
+ * RenderScreenshot Node.js SDK
4
+ *
5
+ * Official Node.js/TypeScript SDK for the RenderScreenshot API.
6
+ *
7
+ * @packageDocumentation
8
+ * @module renderscreenshot
9
+ *
10
+ * @example
11
+ * Basic usage:
12
+ * ```typescript
13
+ * import { Client, TakeOptions } from 'renderscreenshot';
14
+ *
15
+ * const client = new Client('rs_live_xxxxx');
16
+ *
17
+ * // Take a screenshot
18
+ * const image = await client.take(
19
+ * TakeOptions.url('https://example.com').preset('og_card')
20
+ * );
21
+ *
22
+ * // Generate a signed URL for embedding
23
+ * const url = client.generateUrl(
24
+ * TakeOptions.url('https://example.com').preset('og_card'),
25
+ * new Date(Date.now() + 24 * 60 * 60 * 1000)
26
+ * );
27
+ * ```
28
+ */
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.extractWebhookHeaders = exports.parseWebhook = exports.verifyWebhook = exports.CacheManager = exports.RenderScreenshotError = exports.TakeOptions = exports.Client = void 0;
31
+ // Main exports
32
+ var client_js_1 = require("./client.js");
33
+ Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_js_1.Client; } });
34
+ var options_js_1 = require("./options.js");
35
+ Object.defineProperty(exports, "TakeOptions", { enumerable: true, get: function () { return options_js_1.TakeOptions; } });
36
+ var errors_js_1 = require("./errors.js");
37
+ Object.defineProperty(exports, "RenderScreenshotError", { enumerable: true, get: function () { return errors_js_1.RenderScreenshotError; } });
38
+ var cache_js_1 = require("./cache.js");
39
+ Object.defineProperty(exports, "CacheManager", { enumerable: true, get: function () { return cache_js_1.CacheManager; } });
40
+ var webhooks_js_1 = require("./webhooks.js");
41
+ Object.defineProperty(exports, "verifyWebhook", { enumerable: true, get: function () { return webhooks_js_1.verifyWebhook; } });
42
+ Object.defineProperty(exports, "parseWebhook", { enumerable: true, get: function () { return webhooks_js_1.parseWebhook; } });
43
+ Object.defineProperty(exports, "extractWebhookHeaders", { enumerable: true, get: function () { return webhooks_js_1.extractWebhookHeaders; } });
44
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;;;AAEH,eAAe;AACf,yCAAqC;AAA5B,mGAAA,MAAM,OAAA;AACf,2CAA2C;AAAlC,yGAAA,WAAW,OAAA;AACpB,yCAAoD;AAA3C,kHAAA,qBAAqB,OAAA;AAE9B,uCAA0C;AAAjC,wGAAA,YAAY,OAAA;AACrB,6CAAmF;AAA1E,4GAAA,aAAa,OAAA;AAAE,2GAAA,YAAY,OAAA;AAAE,oHAAA,qBAAqB,OAAA"}