@scalar/json-magic 0.11.6 → 0.12.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/CHANGELOG.md +24 -0
- package/dist/bundle/plugins/fetch-urls/index.d.ts.map +1 -1
- package/dist/bundle/plugins/fetch-urls/index.js +1 -1
- package/dist/bundle/plugins/fetch-urls/index.js.map +2 -2
- package/package.json +4 -4
- package/dist/bundle/create-limiter.d.ts +0 -21
- package/dist/bundle/create-limiter.d.ts.map +0 -1
- package/dist/bundle/create-limiter.js +0 -31
- package/dist/bundle/create-limiter.js.map +0 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @scalar/json-magic
|
|
2
2
|
|
|
3
|
+
## 0.12.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#8322](https://github.com/scalar/scalar/pull/8322): chore: bump required node version to >=22 (LTS)
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
#### Updated Dependencies
|
|
12
|
+
|
|
13
|
+
- **@scalar/helpers@0.3.0**
|
|
14
|
+
- [#8322](https://github.com/scalar/scalar/pull/8322): chore: bump required node version to >=22 (LTS)
|
|
15
|
+
|
|
16
|
+
## 0.11.7
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- [#8314](https://github.com/scalar/scalar/pull/8314): chore: limit concurrent operations while migrating workspaces
|
|
21
|
+
|
|
22
|
+
#### Updated Dependencies
|
|
23
|
+
|
|
24
|
+
- **@scalar/helpers@0.2.18**
|
|
25
|
+
- [#8314](https://github.com/scalar/scalar/pull/8314): chore: limit concurrent operations while migrating workspaces
|
|
26
|
+
|
|
3
27
|
## 0.11.6
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/bundle/plugins/fetch-urls/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/bundle/plugins/fetch-urls/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAI3D,KAAK,WAAW,GAAG,OAAO,CAAC;IACzB,OAAO,EAAE;QAAE,OAAO,EAAE,WAAW,CAAC;QAAC,OAAO,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,CAAA;IACtD,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC3F,CAAC,CAAA;AAcF;;;;;;;;;;;;;GAaG;AACH,wBAAsB,QAAQ,CAC5B,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EAChD,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,aAAa,CAAC,CA0CxB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,GAAG,YAAY,CAShG"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createLimiter } from "
|
|
1
|
+
import { createLimiter } from "@scalar/helpers/general/create-limiter";
|
|
2
2
|
import { isHttpUrl } from "../../../helpers/is-http-url.js";
|
|
3
3
|
import { normalize } from "../../../helpers/normalize.js";
|
|
4
4
|
const getHost = (url) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/bundle/plugins/fetch-urls/index.ts"],
|
|
4
|
-
"sourcesContent": ["import
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import { createLimiter } from '@scalar/helpers/general/create-limiter'\n\nimport type { LoaderPlugin, ResolveResult } from '@/bundle'\nimport { isHttpUrl } from '@/helpers/is-http-url'\nimport { normalize } from '@/helpers/normalize'\n\ntype FetchConfig = Partial<{\n headers: { headers: HeadersInit; domains: string[] }[]\n fetch: (input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>\n}>\n\n/**\n * Safely checks for host from a URL\n * Needed because we cannot create a URL from a relative remote URL ex: examples/openapi.json\n */\nconst getHost = (url: string): string | null => {\n try {\n return new URL(url).host\n } catch {\n return null\n }\n}\n\n/**\n * Fetches and normalizes data from a remote URL\n * @param url - The URL to fetch data from\n * @returns A promise that resolves to either the normalized data or an error result\n * @example\n * ```ts\n * const result = await fetchUrl('https://api.example.com/data.json')\n * if (result.ok) {\n * console.log(result.data) // The normalized data\n * } else {\n * console.log('Failed to fetch data')\n * }\n * ```\n */\nexport async function fetchUrl(\n url: string,\n limiter: <T>(fn: () => Promise<T>) => Promise<T>,\n config?: FetchConfig,\n): Promise<ResolveResult> {\n try {\n const host = getHost(url)\n\n // Get the headers that match the domain\n const headers = config?.headers?.find((a) => a.domains.find((d) => d === host) !== undefined)?.headers\n\n const exec = config?.fetch ?? fetch\n\n const result = await limiter(() =>\n exec(url, {\n headers,\n }),\n )\n\n if (result.ok) {\n const body = await result.text()\n\n return {\n ok: true,\n data: normalize(body),\n raw: body,\n }\n }\n\n const contentType = result.headers.get('Content-Type') ?? ''\n\n // Warn if the content type is HTML or XML as we only support JSON/YAML\n if (['text/html', 'application/xml'].includes(contentType)) {\n console.warn(`[WARN] We only support JSON/YAML formats, received ${contentType}`)\n }\n\n console.warn(`[WARN] Fetch failed with status ${result.status} ${result.statusText} for URL: ${url}`)\n return {\n ok: false,\n }\n } catch {\n console.warn(`[WARN] Failed to parse JSON/YAML from URL: ${url}`)\n return {\n ok: false,\n }\n }\n}\n\n/**\n * Creates a plugin for handling remote URL references.\n * This plugin validates and fetches data from HTTP/HTTPS URLs.\n *\n * @returns A plugin object with validate and exec functions\n * @example\n * const urlPlugin = fetchUrls()\n * if (urlPlugin.validate('https://example.com/schema.json')) {\n * const result = await urlPlugin.exec('https://example.com/schema.json')\n * }\n */\nexport function fetchUrls(config?: FetchConfig & Partial<{ limit: number | null }>): LoaderPlugin {\n // If there is a limit specified we limit the number of concurrent calls\n const limiter = config?.limit ? createLimiter(config.limit) : <T>(fn: () => Promise<T>) => fn()\n\n return {\n type: 'loader',\n validate: isHttpUrl,\n exec: (value) => fetchUrl(value, limiter, config),\n }\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,qBAAqB;AAG9B,SAAS,iBAAiB;AAC1B,SAAS,iBAAiB;AAW1B,MAAM,UAAU,CAAC,QAA+B;AAC9C,MAAI;AACF,WAAO,IAAI,IAAI,GAAG,EAAE;AAAA,EACtB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAgBA,eAAsB,SACpB,KACA,SACA,QACwB;AACxB,MAAI;AACF,UAAM,OAAO,QAAQ,GAAG;AAGxB,UAAM,UAAU,QAAQ,SAAS,KAAK,CAAC,MAAM,EAAE,QAAQ,KAAK,CAAC,MAAM,MAAM,IAAI,MAAM,MAAS,GAAG;AAE/F,UAAM,OAAO,QAAQ,SAAS;AAE9B,UAAM,SAAS,MAAM;AAAA,MAAQ,MAC3B,KAAK,KAAK;AAAA,QACR;AAAA,MACF,CAAC;AAAA,IACH;AAEA,QAAI,OAAO,IAAI;AACb,YAAM,OAAO,MAAM,OAAO,KAAK;AAE/B,aAAO;AAAA,QACL,IAAI;AAAA,QACJ,MAAM,UAAU,IAAI;AAAA,QACpB,KAAK;AAAA,MACP;AAAA,IACF;AAEA,UAAM,cAAc,OAAO,QAAQ,IAAI,cAAc,KAAK;AAG1D,QAAI,CAAC,aAAa,iBAAiB,EAAE,SAAS,WAAW,GAAG;AAC1D,cAAQ,KAAK,sDAAsD,WAAW,EAAE;AAAA,IAClF;AAEA,YAAQ,KAAK,mCAAmC,OAAO,MAAM,IAAI,OAAO,UAAU,aAAa,GAAG,EAAE;AACpG,WAAO;AAAA,MACL,IAAI;AAAA,IACN;AAAA,EACF,QAAQ;AACN,YAAQ,KAAK,8CAA8C,GAAG,EAAE;AAChE,WAAO;AAAA,MACL,IAAI;AAAA,IACN;AAAA,EACF;AACF;AAaO,SAAS,UAAU,QAAwE;AAEhG,QAAM,UAAU,QAAQ,QAAQ,cAAc,OAAO,KAAK,IAAI,CAAI,OAAyB,GAAG;AAE9F,SAAO;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,MAAM,CAAC,UAAU,SAAS,OAAO,SAAS,MAAM;AAAA,EAClD;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"url": "git+https://github.com/scalar/scalar.git",
|
|
11
11
|
"directory": "packages/json-magic"
|
|
12
12
|
},
|
|
13
|
-
"version": "0.
|
|
13
|
+
"version": "0.12.0",
|
|
14
14
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
15
|
+
"node": ">=22"
|
|
16
16
|
},
|
|
17
17
|
"type": "module",
|
|
18
18
|
"exports": {
|
|
@@ -79,12 +79,12 @@
|
|
|
79
79
|
"dependencies": {
|
|
80
80
|
"pathe": "^2.0.3",
|
|
81
81
|
"yaml": "^2.8.0",
|
|
82
|
-
"@scalar/helpers": "0.
|
|
82
|
+
"@scalar/helpers": "0.3.0"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
85
|
"fastify": "^5.3.3",
|
|
86
86
|
"vite": "^7.3.1",
|
|
87
|
-
"@scalar/build-tooling": "0.
|
|
87
|
+
"@scalar/build-tooling": "0.5.0"
|
|
88
88
|
},
|
|
89
89
|
"scripts": {
|
|
90
90
|
"build": "scalar-build-esbuild",
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a function that limits the number of concurrent executions of async functions.
|
|
3
|
-
*
|
|
4
|
-
* @param maxConcurrent - Maximum number of concurrent executions allowed
|
|
5
|
-
* @returns A function that wraps async functions to limit their concurrent execution
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* const limiter = createLimiter(2) // Allow max 2 concurrent executions
|
|
10
|
-
*
|
|
11
|
-
* // These will run with max 2 at a time
|
|
12
|
-
* const results = await Promise.all([
|
|
13
|
-
* limiter(() => fetch('/api/1')),
|
|
14
|
-
* limiter(() => fetch('/api/2')),
|
|
15
|
-
* limiter(() => fetch('/api/3')),
|
|
16
|
-
* limiter(() => fetch('/api/4'))
|
|
17
|
-
* ])
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export declare function createLimiter(maxConcurrent: number): <T>(fn: () => Promise<T>) => Promise<T>;
|
|
21
|
-
//# sourceMappingURL=create-limiter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create-limiter.d.ts","sourceRoot":"","sources":["../../src/bundle/create-limiter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAAC,aAAa,EAAE,MAAM,IAgB9B,CAAC,MAAM,MAAM,OAAO,CAAC,CAAC,CAAC,KAAG,OAAO,CAAC,CAAC,CAAC,CAgBxD"}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
function createLimiter(maxConcurrent) {
|
|
2
|
-
let activeCount = 0;
|
|
3
|
-
const queue = [];
|
|
4
|
-
const next = () => {
|
|
5
|
-
if (queue.length === 0 || activeCount >= maxConcurrent) {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
const resolve = queue.shift();
|
|
9
|
-
if (resolve) {
|
|
10
|
-
resolve();
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
const run = async (fn) => {
|
|
14
|
-
if (activeCount >= maxConcurrent) {
|
|
15
|
-
await new Promise((resolve) => queue.push(resolve));
|
|
16
|
-
}
|
|
17
|
-
activeCount++;
|
|
18
|
-
try {
|
|
19
|
-
const result = await fn();
|
|
20
|
-
return result;
|
|
21
|
-
} finally {
|
|
22
|
-
activeCount--;
|
|
23
|
-
next();
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
return run;
|
|
27
|
-
}
|
|
28
|
-
export {
|
|
29
|
-
createLimiter
|
|
30
|
-
};
|
|
31
|
-
//# sourceMappingURL=create-limiter.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/bundle/create-limiter.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Creates a function that limits the number of concurrent executions of async functions.\n *\n * @param maxConcurrent - Maximum number of concurrent executions allowed\n * @returns A function that wraps async functions to limit their concurrent execution\n *\n * @example\n * ```ts\n * const limiter = createLimiter(2) // Allow max 2 concurrent executions\n *\n * // These will run with max 2 at a time\n * const results = await Promise.all([\n * limiter(() => fetch('/api/1')),\n * limiter(() => fetch('/api/2')),\n * limiter(() => fetch('/api/3')),\n * limiter(() => fetch('/api/4'))\n * ])\n * ```\n */\nexport function createLimiter(maxConcurrent: number) {\n let activeCount = 0\n const queue: (() => void)[] = []\n\n const next = () => {\n if (queue.length === 0 || activeCount >= maxConcurrent) {\n return\n }\n\n const resolve = queue.shift()\n\n if (resolve) {\n resolve()\n }\n }\n\n const run = async <T>(fn: () => Promise<T>): Promise<T> => {\n if (activeCount >= maxConcurrent) {\n await new Promise<void>((resolve) => queue.push(resolve))\n }\n\n activeCount++\n try {\n const result = await fn()\n return result\n } finally {\n activeCount--\n next()\n }\n }\n\n return run\n}\n"],
|
|
5
|
-
"mappings": "AAmBO,SAAS,cAAc,eAAuB;AACnD,MAAI,cAAc;AAClB,QAAM,QAAwB,CAAC;AAE/B,QAAM,OAAO,MAAM;AACjB,QAAI,MAAM,WAAW,KAAK,eAAe,eAAe;AACtD;AAAA,IACF;AAEA,UAAM,UAAU,MAAM,MAAM;AAE5B,QAAI,SAAS;AACX,cAAQ;AAAA,IACV;AAAA,EACF;AAEA,QAAM,MAAM,OAAU,OAAqC;AACzD,QAAI,eAAe,eAAe;AAChC,YAAM,IAAI,QAAc,CAAC,YAAY,MAAM,KAAK,OAAO,CAAC;AAAA,IAC1D;AAEA;AACA,QAAI;AACF,YAAM,SAAS,MAAM,GAAG;AACxB,aAAO;AAAA,IACT,UAAE;AACA;AACA,WAAK;AAAA,IACP;AAAA,EACF;AAEA,SAAO;AACT;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|