@vercel/prepare-flags-definitions 0.1.0-3ca407e-20260306094417
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 +7 -0
- package/LICENSE.md +21 -0
- package/dist/index.cjs +152 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +38 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +152 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @vercel/prepare-flags-definitions
|
|
2
|
+
|
|
3
|
+
## 0.1.0-3ca407e-20260306094417
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 96ba122: Initial release of `@vercel/prepare-flags-definitions`. Extracts the core flag definitions preparation logic from the Vercel CLI into a standalone, reusable package.
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Vercel, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/index.ts
|
|
2
|
+
var _crypto = require('crypto');
|
|
3
|
+
var _promises = require('fs/promises');
|
|
4
|
+
var _path = require('path');
|
|
5
|
+
var FLAGS_HOST = "https://flags.vercel.com";
|
|
6
|
+
var FLAGS_DEFINITIONS_VERSION = "1.0.1";
|
|
7
|
+
function obfuscate(sdkKey, prefixLength = 18) {
|
|
8
|
+
if (prefixLength >= sdkKey.length) return sdkKey;
|
|
9
|
+
return sdkKey.slice(0, prefixLength) + "*".repeat(sdkKey.length - prefixLength);
|
|
10
|
+
}
|
|
11
|
+
function hashSdkKey(sdkKey) {
|
|
12
|
+
return _crypto.createHash.call(void 0, "sha256").update(sdkKey).digest("hex");
|
|
13
|
+
}
|
|
14
|
+
function generateDefinitionsModule(sdkKeys, values) {
|
|
15
|
+
const stringified = sdkKeys.map((_, i) => JSON.stringify(values[i]));
|
|
16
|
+
const uniqueStrings = [];
|
|
17
|
+
const stringToIndex = /* @__PURE__ */ new Map();
|
|
18
|
+
for (const s of stringified) {
|
|
19
|
+
if (!stringToIndex.has(s)) {
|
|
20
|
+
stringToIndex.set(s, uniqueStrings.length);
|
|
21
|
+
uniqueStrings.push(s);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const keyToIndex = sdkKeys.map(
|
|
25
|
+
(_, i) => _nullishCoalesce(stringToIndex.get(stringified[i]), () => ( 0))
|
|
26
|
+
);
|
|
27
|
+
const hashedKeys = sdkKeys.map(hashSdkKey);
|
|
28
|
+
const lines = [
|
|
29
|
+
"const memo = (fn) => { let cached; return () => (cached ??= fn()); };",
|
|
30
|
+
""
|
|
31
|
+
];
|
|
32
|
+
for (let i = 0; i < uniqueStrings.length; i++) {
|
|
33
|
+
lines.push(
|
|
34
|
+
`const _d${i} = memo(() => JSON.parse(${JSON.stringify(uniqueStrings[i])}));`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
lines.push("");
|
|
38
|
+
lines.push("const map = {");
|
|
39
|
+
for (let i = 0; i < sdkKeys.length; i++) {
|
|
40
|
+
lines.push(` ${JSON.stringify(hashedKeys[i])}: _d${keyToIndex[i]},`);
|
|
41
|
+
}
|
|
42
|
+
lines.push("};");
|
|
43
|
+
lines.push("");
|
|
44
|
+
lines.push("export function get(hashedSdkKey) {");
|
|
45
|
+
lines.push(" return map[hashedSdkKey]?.() ?? null;");
|
|
46
|
+
lines.push("}");
|
|
47
|
+
lines.push("");
|
|
48
|
+
lines.push(
|
|
49
|
+
`export const version = ${JSON.stringify(FLAGS_DEFINITIONS_VERSION)};`
|
|
50
|
+
);
|
|
51
|
+
return lines.join("\n");
|
|
52
|
+
}
|
|
53
|
+
async function prepareFlagsDefinitions(options) {
|
|
54
|
+
const {
|
|
55
|
+
cwd,
|
|
56
|
+
env,
|
|
57
|
+
version = "unknown",
|
|
58
|
+
fetch: fetchFn = globalThis.fetch,
|
|
59
|
+
output
|
|
60
|
+
} = options;
|
|
61
|
+
output == null ? void 0 : output.debug("vercel-flags: checking env vars for SDK Keys");
|
|
62
|
+
const sdkKeys = Array.from(
|
|
63
|
+
Object.values(env).reduce((acc, value) => {
|
|
64
|
+
if (typeof value === "string") {
|
|
65
|
+
if (value.startsWith("vf_")) {
|
|
66
|
+
acc.add(value);
|
|
67
|
+
} else if (value.startsWith("flags:")) {
|
|
68
|
+
const params = new URLSearchParams(value.slice("flags:".length));
|
|
69
|
+
const sdkKey = params.get("sdkKey");
|
|
70
|
+
if (sdkKey == null ? void 0 : sdkKey.startsWith("vf_")) {
|
|
71
|
+
acc.add(sdkKey);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return acc;
|
|
76
|
+
}, /* @__PURE__ */ new Set())
|
|
77
|
+
);
|
|
78
|
+
output == null ? void 0 : output.debug(`vercel-flags: found ${sdkKeys.length} SDK keys`);
|
|
79
|
+
if (sdkKeys.length === 0) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const fetchPromise = Promise.all(
|
|
83
|
+
sdkKeys.map(async (sdkKey) => {
|
|
84
|
+
const headers = {
|
|
85
|
+
authorization: `Bearer ${sdkKey}`,
|
|
86
|
+
"user-agent": `prepare-flags-definitions/${version}`
|
|
87
|
+
};
|
|
88
|
+
if (env.VERCEL_PROJECT_ID) {
|
|
89
|
+
headers["x-vercel-project-id"] = env.VERCEL_PROJECT_ID;
|
|
90
|
+
}
|
|
91
|
+
if (env.VERCEL_ENV) {
|
|
92
|
+
headers["x-vercel-env"] = env.VERCEL_ENV;
|
|
93
|
+
}
|
|
94
|
+
if (env.VERCEL_DEPLOYMENT_ID) {
|
|
95
|
+
headers["x-vercel-deployment-id"] = env.VERCEL_DEPLOYMENT_ID;
|
|
96
|
+
}
|
|
97
|
+
if (env.VERCEL_REGION) {
|
|
98
|
+
headers["x-vercel-region"] = env.VERCEL_REGION;
|
|
99
|
+
}
|
|
100
|
+
const res = await fetchFn(`${FLAGS_HOST}/v1/datafile`, { headers });
|
|
101
|
+
if (!res.ok) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Failed to fetch flag definitions for ${obfuscate(sdkKey)}: ${res.status} ${res.statusText}`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return res.json();
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
const values = output ? await output.time("vercel-flags: load datafiles", fetchPromise) : await fetchPromise;
|
|
110
|
+
const definitionsJs = generateDefinitionsModule(sdkKeys, values);
|
|
111
|
+
const storageDir = _path.join.call(void 0, cwd, "node_modules", "@vercel", "flags-definitions");
|
|
112
|
+
const indexPath = _path.join.call(void 0, storageDir, "index.js");
|
|
113
|
+
const dtsPath = _path.join.call(void 0, storageDir, "index.d.ts");
|
|
114
|
+
const packageJsonPath = _path.join.call(void 0, storageDir, "package.json");
|
|
115
|
+
const dts = [
|
|
116
|
+
"export function get(hashedSdkKey: string): Record<string, unknown> | null;",
|
|
117
|
+
"export const version: string;",
|
|
118
|
+
""
|
|
119
|
+
].join("\n");
|
|
120
|
+
const packageJson = {
|
|
121
|
+
name: "@vercel/flags-definitions",
|
|
122
|
+
version: FLAGS_DEFINITIONS_VERSION,
|
|
123
|
+
type: "module",
|
|
124
|
+
main: "./index.js",
|
|
125
|
+
types: "./index.d.ts",
|
|
126
|
+
exports: {
|
|
127
|
+
".": {
|
|
128
|
+
types: "./index.d.ts",
|
|
129
|
+
import: "./index.js"
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
await _promises.mkdir.call(void 0, storageDir, { recursive: true });
|
|
134
|
+
await Promise.all([
|
|
135
|
+
_promises.writeFile.call(void 0, indexPath, definitionsJs),
|
|
136
|
+
_promises.writeFile.call(void 0, dtsPath, dts),
|
|
137
|
+
_promises.writeFile.call(void 0, packageJsonPath, JSON.stringify(packageJson, null, 2))
|
|
138
|
+
]);
|
|
139
|
+
output == null ? void 0 : output.debug("vercel-flags: created module");
|
|
140
|
+
output == null ? void 0 : output.debug(` \u2192 ${indexPath}`);
|
|
141
|
+
output == null ? void 0 : output.debug(` \u2192 ${dtsPath}`);
|
|
142
|
+
output == null ? void 0 : output.debug(` \u2192 ${packageJsonPath}`);
|
|
143
|
+
output == null ? void 0 : output.debug(
|
|
144
|
+
` \u2192 included definitions for keys "${sdkKeys.map((key) => obfuscate(key)).join(", ")}"`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
exports.generateDefinitionsModule = generateDefinitionsModule; exports.hashSdkKey = hashSdkKey; exports.prepareFlagsDefinitions = prepareFlagsDefinitions;
|
|
152
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/flags/flags/packages/prepare-flags-definitions/dist/index.cjs","../src/index.ts"],"names":[],"mappings":"AAAA;ACAA,gCAA2B;AAC3B,uCAAiC;AACjC,4BAAqB;AAErB,IAAM,WAAA,EAAa,0BAAA;AACnB,IAAM,0BAAA,EAA4B,OAAA;AAYlC,SAAS,SAAA,CAAU,MAAA,EAAgB,aAAA,EAAe,EAAA,EAAY;AAC5D,EAAA,GAAA,CAAI,aAAA,GAAgB,MAAA,CAAO,MAAA,EAAQ,OAAO,MAAA;AAC1C,EAAA,OACE,MAAA,CAAO,KAAA,CAAM,CAAA,EAAG,YAAY,EAAA,EAAI,GAAA,CAAI,MAAA,CAAO,MAAA,CAAO,OAAA,EAAS,YAAY,CAAA;AAE3E;AAKO,SAAS,UAAA,CAAW,MAAA,EAAwB;AACjD,EAAA,OAAO,gCAAA,QAAmB,CAAA,CAAE,MAAA,CAAO,MAAM,CAAA,CAAE,MAAA,CAAO,KAAK,CAAA;AACzD;AAgBO,SAAS,yBAAA,CACd,OAAA,EACA,MAAA,EACQ;AAER,EAAA,MAAM,YAAA,EAAc,OAAA,CAAQ,GAAA,CAAI,CAAC,CAAA,EAAG,CAAA,EAAA,GAAM,IAAA,CAAK,SAAA,CAAU,MAAA,CAAO,CAAC,CAAC,CAAC,CAAA;AAGnE,EAAA,MAAM,cAAA,EAA0B,CAAC,CAAA;AACjC,EAAA,MAAM,cAAA,kBAAgB,IAAI,GAAA,CAAoB,CAAA;AAC9C,EAAA,IAAA,CAAA,MAAW,EAAA,GAAK,WAAA,EAAa;AAC3B,IAAA,GAAA,CAAI,CAAC,aAAA,CAAc,GAAA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AAA;AACG,IAAA;AACH,IAAA;AACA,IAAA;AACC,IAAA;AACE,IAAA;AACF,MAAA;AACI,QAAA;AACC,QAAA;AACV,MAAA;AACF,IAAA;AACF,EAAA;AAEqC,EAAA;AACnB,EAAA;AACkB,IAAA;AACZ,IAAA;AACU,IAAA;AACjC,EAAA;AAEO,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACN,IAAA;AAAqF,EAAA;AAEzF;ADnFwC;AACA;AACA;AACA;AACA","file":"/home/runner/work/flags/flags/packages/prepare-flags-definitions/dist/index.cjs","sourcesContent":[null,"import { createHash } from 'node:crypto';\nimport { mkdir, writeFile } from 'node:fs/promises';\nimport { join } from 'node:path';\n\nconst FLAGS_HOST = 'https://flags.vercel.com';\nconst FLAGS_DEFINITIONS_VERSION = '1.0.1';\n\ntype BundledDefinitions = Record<string, unknown>;\n\nexport interface Output {\n debug(message: string): void;\n time<T>(label: string, promise: Promise<T>): Promise<T>;\n}\n\n/**\n * Obfuscates SDK key for logging (shows first 18 chars)\n */\nfunction obfuscate(sdkKey: string, prefixLength = 18): string {\n if (prefixLength >= sdkKey.length) return sdkKey;\n return (\n sdkKey.slice(0, prefixLength) + '*'.repeat(sdkKey.length - prefixLength)\n );\n}\n\n/**\n * Computes a SHA-256 hex digest of the given SDK key.\n */\nexport function hashSdkKey(sdkKey: string): string {\n return createHash('sha256').update(sdkKey).digest('hex');\n}\n\n/**\n * Generates a JS module with deduplicated, lazily-parsed definitions.\n *\n * The map keys are SHA-256 hashes of the SDK keys so that raw keys\n * are not embedded in the output.\n *\n * Output format:\n * ```js\n * const memo = (fn) => { let cached; return () => (cached ??= fn()); };\n * const _d0 = memo(() => JSON.parse('...'));\n * const map = { \"<sha256_hash>\": _d0 };\n * export function get(hashedSdkKey) { return map[hashedSdkKey]?.() ?? null; }\n * ```\n */\nexport function generateDefinitionsModule(\n sdkKeys: string[],\n values: BundledDefinitions[],\n): string {\n // Stringify each definition\n const stringified = sdkKeys.map((_, i) => JSON.stringify(values[i]));\n\n // Deduplicate: map unique strings to indices\n const uniqueStrings: string[] = [];\n const stringToIndex = new Map<string, number>();\n for (const s of stringified) {\n if (!stringToIndex.has(s)) {\n stringToIndex.set(s, uniqueStrings.length);\n uniqueStrings.push(s);\n }\n }\n\n // Map SDK keys to their definition index\n const keyToIndex = sdkKeys.map(\n (_, i) => stringToIndex.get(stringified[i]!) ?? 0,\n );\n\n // Hash each SDK key\n const hashedKeys = sdkKeys.map(hashSdkKey);\n\n // Generate JS\n const lines: string[] = [\n 'const memo = (fn) => { let cached; return () => (cached ??= fn()); };',\n '',\n ];\n\n // Add definition constants\n for (let i = 0; i < uniqueStrings.length; i++) {\n lines.push(\n `const _d${i} = memo(() => JSON.parse(${JSON.stringify(uniqueStrings[i])}));`,\n );\n }\n\n lines.push('');\n lines.push('const map = {');\n for (let i = 0; i < sdkKeys.length; i++) {\n lines.push(` ${JSON.stringify(hashedKeys[i])}: _d${keyToIndex[i]},`);\n }\n lines.push('};');\n lines.push('');\n lines.push('export function get(hashedSdkKey) {');\n lines.push(' return map[hashedSdkKey]?.() ?? null;');\n lines.push('}');\n lines.push('');\n lines.push(\n `export const version = ${JSON.stringify(FLAGS_DEFINITIONS_VERSION)};`,\n );\n\n return lines.join('\\n');\n}\n\n/**\n * Prepares flag definitions by reading SDK keys from environment variables,\n * fetching definitions from flags.vercel.com, and writing them into a\n * synthetic `@vercel/flags-definitions` package inside `node_modules/`.\n */\nexport async function prepareFlagsDefinitions(options: {\n cwd: string;\n env: Record<string, string | undefined>;\n version?: string;\n fetch?: typeof globalThis.fetch;\n output?: Output;\n}): Promise<void> {\n const {\n cwd,\n env,\n version = 'unknown',\n fetch: fetchFn = globalThis.fetch,\n output,\n } = options;\n\n output?.debug('vercel-flags: checking env vars for SDK Keys');\n\n // Collect unique SDK keys from environment variables\n // Supports both direct SDK keys (vf_ prefix) and flags: format\n const sdkKeys = Array.from(\n Object.values(env).reduce<Set<string>>((acc, value) => {\n if (typeof value === 'string') {\n if (value.startsWith('vf_')) {\n acc.add(value);\n } else if (value.startsWith('flags:')) {\n const params = new URLSearchParams(value.slice('flags:'.length));\n const sdkKey = params.get('sdkKey');\n if (sdkKey?.startsWith('vf_')) {\n acc.add(sdkKey);\n }\n }\n }\n return acc;\n }, new Set<string>()),\n );\n\n output?.debug(`vercel-flags: found ${sdkKeys.length} SDK keys`);\n\n if (sdkKeys.length === 0) {\n return;\n }\n\n // Fetch definitions for each SDK key\n const fetchPromise = Promise.all(\n sdkKeys.map(async (sdkKey) => {\n const headers: Record<string, string> = {\n authorization: `Bearer ${sdkKey}`,\n 'user-agent': `prepare-flags-definitions/${version}`,\n };\n\n // Add Vercel metadata headers if available\n if (env.VERCEL_PROJECT_ID) {\n headers['x-vercel-project-id'] = env.VERCEL_PROJECT_ID;\n }\n if (env.VERCEL_ENV) {\n headers['x-vercel-env'] = env.VERCEL_ENV;\n }\n if (env.VERCEL_DEPLOYMENT_ID) {\n headers['x-vercel-deployment-id'] = env.VERCEL_DEPLOYMENT_ID;\n }\n if (env.VERCEL_REGION) {\n headers['x-vercel-region'] = env.VERCEL_REGION;\n }\n\n const res = await fetchFn(`${FLAGS_HOST}/v1/datafile`, { headers });\n\n if (!res.ok) {\n throw new Error(\n `Failed to fetch flag definitions for ${obfuscate(sdkKey)}: ${res.status} ${res.statusText}`,\n );\n }\n\n return res.json() as Promise<BundledDefinitions>;\n }),\n );\n\n const values = output\n ? await output.time('vercel-flags: load datafiles', fetchPromise)\n : await fetchPromise;\n\n // Generate the JS module\n const definitionsJs = generateDefinitionsModule(sdkKeys, values);\n\n // Write to node_modules/@vercel/flags-definitions/\n const storageDir = join(cwd, 'node_modules', '@vercel', 'flags-definitions');\n const indexPath = join(storageDir, 'index.js');\n const dtsPath = join(storageDir, 'index.d.ts');\n const packageJsonPath = join(storageDir, 'package.json');\n\n const dts = [\n 'export function get(hashedSdkKey: string): Record<string, unknown> | null;',\n 'export const version: string;',\n '',\n ].join('\\n');\n\n const packageJson = {\n name: '@vercel/flags-definitions',\n version: FLAGS_DEFINITIONS_VERSION,\n type: 'module',\n main: './index.js',\n types: './index.d.ts',\n exports: {\n '.': {\n types: './index.d.ts',\n import: './index.js',\n },\n },\n };\n\n await mkdir(storageDir, { recursive: true });\n await Promise.all([\n writeFile(indexPath, definitionsJs),\n writeFile(dtsPath, dts),\n writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)),\n ]);\n\n output?.debug('vercel-flags: created module');\n output?.debug(` → ${indexPath}`);\n output?.debug(` → ${dtsPath}`);\n output?.debug(` → ${packageJsonPath}`);\n output?.debug(\n ` → included definitions for keys \"${sdkKeys.map((key) => obfuscate(key)).join(', ')}\"`,\n );\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
type BundledDefinitions = Record<string, unknown>;
|
|
2
|
+
interface Output {
|
|
3
|
+
debug(message: string): void;
|
|
4
|
+
time<T>(label: string, promise: Promise<T>): Promise<T>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Computes a SHA-256 hex digest of the given SDK key.
|
|
8
|
+
*/
|
|
9
|
+
declare function hashSdkKey(sdkKey: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Generates a JS module with deduplicated, lazily-parsed definitions.
|
|
12
|
+
*
|
|
13
|
+
* The map keys are SHA-256 hashes of the SDK keys so that raw keys
|
|
14
|
+
* are not embedded in the output.
|
|
15
|
+
*
|
|
16
|
+
* Output format:
|
|
17
|
+
* ```js
|
|
18
|
+
* const memo = (fn) => { let cached; return () => (cached ??= fn()); };
|
|
19
|
+
* const _d0 = memo(() => JSON.parse('...'));
|
|
20
|
+
* const map = { "<sha256_hash>": _d0 };
|
|
21
|
+
* export function get(hashedSdkKey) { return map[hashedSdkKey]?.() ?? null; }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function generateDefinitionsModule(sdkKeys: string[], values: BundledDefinitions[]): string;
|
|
25
|
+
/**
|
|
26
|
+
* Prepares flag definitions by reading SDK keys from environment variables,
|
|
27
|
+
* fetching definitions from flags.vercel.com, and writing them into a
|
|
28
|
+
* synthetic `@vercel/flags-definitions` package inside `node_modules/`.
|
|
29
|
+
*/
|
|
30
|
+
declare function prepareFlagsDefinitions(options: {
|
|
31
|
+
cwd: string;
|
|
32
|
+
env: Record<string, string | undefined>;
|
|
33
|
+
version?: string;
|
|
34
|
+
fetch?: typeof globalThis.fetch;
|
|
35
|
+
output?: Output;
|
|
36
|
+
}): Promise<void>;
|
|
37
|
+
|
|
38
|
+
export { type Output, generateDefinitionsModule, hashSdkKey, prepareFlagsDefinitions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
type BundledDefinitions = Record<string, unknown>;
|
|
2
|
+
interface Output {
|
|
3
|
+
debug(message: string): void;
|
|
4
|
+
time<T>(label: string, promise: Promise<T>): Promise<T>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Computes a SHA-256 hex digest of the given SDK key.
|
|
8
|
+
*/
|
|
9
|
+
declare function hashSdkKey(sdkKey: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Generates a JS module with deduplicated, lazily-parsed definitions.
|
|
12
|
+
*
|
|
13
|
+
* The map keys are SHA-256 hashes of the SDK keys so that raw keys
|
|
14
|
+
* are not embedded in the output.
|
|
15
|
+
*
|
|
16
|
+
* Output format:
|
|
17
|
+
* ```js
|
|
18
|
+
* const memo = (fn) => { let cached; return () => (cached ??= fn()); };
|
|
19
|
+
* const _d0 = memo(() => JSON.parse('...'));
|
|
20
|
+
* const map = { "<sha256_hash>": _d0 };
|
|
21
|
+
* export function get(hashedSdkKey) { return map[hashedSdkKey]?.() ?? null; }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function generateDefinitionsModule(sdkKeys: string[], values: BundledDefinitions[]): string;
|
|
25
|
+
/**
|
|
26
|
+
* Prepares flag definitions by reading SDK keys from environment variables,
|
|
27
|
+
* fetching definitions from flags.vercel.com, and writing them into a
|
|
28
|
+
* synthetic `@vercel/flags-definitions` package inside `node_modules/`.
|
|
29
|
+
*/
|
|
30
|
+
declare function prepareFlagsDefinitions(options: {
|
|
31
|
+
cwd: string;
|
|
32
|
+
env: Record<string, string | undefined>;
|
|
33
|
+
version?: string;
|
|
34
|
+
fetch?: typeof globalThis.fetch;
|
|
35
|
+
output?: Output;
|
|
36
|
+
}): Promise<void>;
|
|
37
|
+
|
|
38
|
+
export { type Output, generateDefinitionsModule, hashSdkKey, prepareFlagsDefinitions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createHash } from "crypto";
|
|
3
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
var FLAGS_HOST = "https://flags.vercel.com";
|
|
6
|
+
var FLAGS_DEFINITIONS_VERSION = "1.0.1";
|
|
7
|
+
function obfuscate(sdkKey, prefixLength = 18) {
|
|
8
|
+
if (prefixLength >= sdkKey.length) return sdkKey;
|
|
9
|
+
return sdkKey.slice(0, prefixLength) + "*".repeat(sdkKey.length - prefixLength);
|
|
10
|
+
}
|
|
11
|
+
function hashSdkKey(sdkKey) {
|
|
12
|
+
return createHash("sha256").update(sdkKey).digest("hex");
|
|
13
|
+
}
|
|
14
|
+
function generateDefinitionsModule(sdkKeys, values) {
|
|
15
|
+
const stringified = sdkKeys.map((_, i) => JSON.stringify(values[i]));
|
|
16
|
+
const uniqueStrings = [];
|
|
17
|
+
const stringToIndex = /* @__PURE__ */ new Map();
|
|
18
|
+
for (const s of stringified) {
|
|
19
|
+
if (!stringToIndex.has(s)) {
|
|
20
|
+
stringToIndex.set(s, uniqueStrings.length);
|
|
21
|
+
uniqueStrings.push(s);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const keyToIndex = sdkKeys.map(
|
|
25
|
+
(_, i) => stringToIndex.get(stringified[i]) ?? 0
|
|
26
|
+
);
|
|
27
|
+
const hashedKeys = sdkKeys.map(hashSdkKey);
|
|
28
|
+
const lines = [
|
|
29
|
+
"const memo = (fn) => { let cached; return () => (cached ??= fn()); };",
|
|
30
|
+
""
|
|
31
|
+
];
|
|
32
|
+
for (let i = 0; i < uniqueStrings.length; i++) {
|
|
33
|
+
lines.push(
|
|
34
|
+
`const _d${i} = memo(() => JSON.parse(${JSON.stringify(uniqueStrings[i])}));`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
lines.push("");
|
|
38
|
+
lines.push("const map = {");
|
|
39
|
+
for (let i = 0; i < sdkKeys.length; i++) {
|
|
40
|
+
lines.push(` ${JSON.stringify(hashedKeys[i])}: _d${keyToIndex[i]},`);
|
|
41
|
+
}
|
|
42
|
+
lines.push("};");
|
|
43
|
+
lines.push("");
|
|
44
|
+
lines.push("export function get(hashedSdkKey) {");
|
|
45
|
+
lines.push(" return map[hashedSdkKey]?.() ?? null;");
|
|
46
|
+
lines.push("}");
|
|
47
|
+
lines.push("");
|
|
48
|
+
lines.push(
|
|
49
|
+
`export const version = ${JSON.stringify(FLAGS_DEFINITIONS_VERSION)};`
|
|
50
|
+
);
|
|
51
|
+
return lines.join("\n");
|
|
52
|
+
}
|
|
53
|
+
async function prepareFlagsDefinitions(options) {
|
|
54
|
+
const {
|
|
55
|
+
cwd,
|
|
56
|
+
env,
|
|
57
|
+
version = "unknown",
|
|
58
|
+
fetch: fetchFn = globalThis.fetch,
|
|
59
|
+
output
|
|
60
|
+
} = options;
|
|
61
|
+
output == null ? void 0 : output.debug("vercel-flags: checking env vars for SDK Keys");
|
|
62
|
+
const sdkKeys = Array.from(
|
|
63
|
+
Object.values(env).reduce((acc, value) => {
|
|
64
|
+
if (typeof value === "string") {
|
|
65
|
+
if (value.startsWith("vf_")) {
|
|
66
|
+
acc.add(value);
|
|
67
|
+
} else if (value.startsWith("flags:")) {
|
|
68
|
+
const params = new URLSearchParams(value.slice("flags:".length));
|
|
69
|
+
const sdkKey = params.get("sdkKey");
|
|
70
|
+
if (sdkKey == null ? void 0 : sdkKey.startsWith("vf_")) {
|
|
71
|
+
acc.add(sdkKey);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return acc;
|
|
76
|
+
}, /* @__PURE__ */ new Set())
|
|
77
|
+
);
|
|
78
|
+
output == null ? void 0 : output.debug(`vercel-flags: found ${sdkKeys.length} SDK keys`);
|
|
79
|
+
if (sdkKeys.length === 0) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const fetchPromise = Promise.all(
|
|
83
|
+
sdkKeys.map(async (sdkKey) => {
|
|
84
|
+
const headers = {
|
|
85
|
+
authorization: `Bearer ${sdkKey}`,
|
|
86
|
+
"user-agent": `prepare-flags-definitions/${version}`
|
|
87
|
+
};
|
|
88
|
+
if (env.VERCEL_PROJECT_ID) {
|
|
89
|
+
headers["x-vercel-project-id"] = env.VERCEL_PROJECT_ID;
|
|
90
|
+
}
|
|
91
|
+
if (env.VERCEL_ENV) {
|
|
92
|
+
headers["x-vercel-env"] = env.VERCEL_ENV;
|
|
93
|
+
}
|
|
94
|
+
if (env.VERCEL_DEPLOYMENT_ID) {
|
|
95
|
+
headers["x-vercel-deployment-id"] = env.VERCEL_DEPLOYMENT_ID;
|
|
96
|
+
}
|
|
97
|
+
if (env.VERCEL_REGION) {
|
|
98
|
+
headers["x-vercel-region"] = env.VERCEL_REGION;
|
|
99
|
+
}
|
|
100
|
+
const res = await fetchFn(`${FLAGS_HOST}/v1/datafile`, { headers });
|
|
101
|
+
if (!res.ok) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Failed to fetch flag definitions for ${obfuscate(sdkKey)}: ${res.status} ${res.statusText}`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return res.json();
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
const values = output ? await output.time("vercel-flags: load datafiles", fetchPromise) : await fetchPromise;
|
|
110
|
+
const definitionsJs = generateDefinitionsModule(sdkKeys, values);
|
|
111
|
+
const storageDir = join(cwd, "node_modules", "@vercel", "flags-definitions");
|
|
112
|
+
const indexPath = join(storageDir, "index.js");
|
|
113
|
+
const dtsPath = join(storageDir, "index.d.ts");
|
|
114
|
+
const packageJsonPath = join(storageDir, "package.json");
|
|
115
|
+
const dts = [
|
|
116
|
+
"export function get(hashedSdkKey: string): Record<string, unknown> | null;",
|
|
117
|
+
"export const version: string;",
|
|
118
|
+
""
|
|
119
|
+
].join("\n");
|
|
120
|
+
const packageJson = {
|
|
121
|
+
name: "@vercel/flags-definitions",
|
|
122
|
+
version: FLAGS_DEFINITIONS_VERSION,
|
|
123
|
+
type: "module",
|
|
124
|
+
main: "./index.js",
|
|
125
|
+
types: "./index.d.ts",
|
|
126
|
+
exports: {
|
|
127
|
+
".": {
|
|
128
|
+
types: "./index.d.ts",
|
|
129
|
+
import: "./index.js"
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
await mkdir(storageDir, { recursive: true });
|
|
134
|
+
await Promise.all([
|
|
135
|
+
writeFile(indexPath, definitionsJs),
|
|
136
|
+
writeFile(dtsPath, dts),
|
|
137
|
+
writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2))
|
|
138
|
+
]);
|
|
139
|
+
output == null ? void 0 : output.debug("vercel-flags: created module");
|
|
140
|
+
output == null ? void 0 : output.debug(` \u2192 ${indexPath}`);
|
|
141
|
+
output == null ? void 0 : output.debug(` \u2192 ${dtsPath}`);
|
|
142
|
+
output == null ? void 0 : output.debug(` \u2192 ${packageJsonPath}`);
|
|
143
|
+
output == null ? void 0 : output.debug(
|
|
144
|
+
` \u2192 included definitions for keys "${sdkKeys.map((key) => obfuscate(key)).join(", ")}"`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
export {
|
|
148
|
+
generateDefinitionsModule,
|
|
149
|
+
hashSdkKey,
|
|
150
|
+
prepareFlagsDefinitions
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createHash } from 'node:crypto';\nimport { mkdir, writeFile } from 'node:fs/promises';\nimport { join } from 'node:path';\n\nconst FLAGS_HOST = 'https://flags.vercel.com';\nconst FLAGS_DEFINITIONS_VERSION = '1.0.1';\n\ntype BundledDefinitions = Record<string, unknown>;\n\nexport interface Output {\n debug(message: string): void;\n time<T>(label: string, promise: Promise<T>): Promise<T>;\n}\n\n/**\n * Obfuscates SDK key for logging (shows first 18 chars)\n */\nfunction obfuscate(sdkKey: string, prefixLength = 18): string {\n if (prefixLength >= sdkKey.length) return sdkKey;\n return (\n sdkKey.slice(0, prefixLength) + '*'.repeat(sdkKey.length - prefixLength)\n );\n}\n\n/**\n * Computes a SHA-256 hex digest of the given SDK key.\n */\nexport function hashSdkKey(sdkKey: string): string {\n return createHash('sha256').update(sdkKey).digest('hex');\n}\n\n/**\n * Generates a JS module with deduplicated, lazily-parsed definitions.\n *\n * The map keys are SHA-256 hashes of the SDK keys so that raw keys\n * are not embedded in the output.\n *\n * Output format:\n * ```js\n * const memo = (fn) => { let cached; return () => (cached ??= fn()); };\n * const _d0 = memo(() => JSON.parse('...'));\n * const map = { \"<sha256_hash>\": _d0 };\n * export function get(hashedSdkKey) { return map[hashedSdkKey]?.() ?? null; }\n * ```\n */\nexport function generateDefinitionsModule(\n sdkKeys: string[],\n values: BundledDefinitions[],\n): string {\n // Stringify each definition\n const stringified = sdkKeys.map((_, i) => JSON.stringify(values[i]));\n\n // Deduplicate: map unique strings to indices\n const uniqueStrings: string[] = [];\n const stringToIndex = new Map<string, number>();\n for (const s of stringified) {\n if (!stringToIndex.has(s)) {\n stringToIndex.set(s, uniqueStrings.length);\n uniqueStrings.push(s);\n }\n }\n\n // Map SDK keys to their definition index\n const keyToIndex = sdkKeys.map(\n (_, i) => stringToIndex.get(stringified[i]!) ?? 0,\n );\n\n // Hash each SDK key\n const hashedKeys = sdkKeys.map(hashSdkKey);\n\n // Generate JS\n const lines: string[] = [\n 'const memo = (fn) => { let cached; return () => (cached ??= fn()); };',\n '',\n ];\n\n // Add definition constants\n for (let i = 0; i < uniqueStrings.length; i++) {\n lines.push(\n `const _d${i} = memo(() => JSON.parse(${JSON.stringify(uniqueStrings[i])}));`,\n );\n }\n\n lines.push('');\n lines.push('const map = {');\n for (let i = 0; i < sdkKeys.length; i++) {\n lines.push(` ${JSON.stringify(hashedKeys[i])}: _d${keyToIndex[i]},`);\n }\n lines.push('};');\n lines.push('');\n lines.push('export function get(hashedSdkKey) {');\n lines.push(' return map[hashedSdkKey]?.() ?? null;');\n lines.push('}');\n lines.push('');\n lines.push(\n `export const version = ${JSON.stringify(FLAGS_DEFINITIONS_VERSION)};`,\n );\n\n return lines.join('\\n');\n}\n\n/**\n * Prepares flag definitions by reading SDK keys from environment variables,\n * fetching definitions from flags.vercel.com, and writing them into a\n * synthetic `@vercel/flags-definitions` package inside `node_modules/`.\n */\nexport async function prepareFlagsDefinitions(options: {\n cwd: string;\n env: Record<string, string | undefined>;\n version?: string;\n fetch?: typeof globalThis.fetch;\n output?: Output;\n}): Promise<void> {\n const {\n cwd,\n env,\n version = 'unknown',\n fetch: fetchFn = globalThis.fetch,\n output,\n } = options;\n\n output?.debug('vercel-flags: checking env vars for SDK Keys');\n\n // Collect unique SDK keys from environment variables\n // Supports both direct SDK keys (vf_ prefix) and flags: format\n const sdkKeys = Array.from(\n Object.values(env).reduce<Set<string>>((acc, value) => {\n if (typeof value === 'string') {\n if (value.startsWith('vf_')) {\n acc.add(value);\n } else if (value.startsWith('flags:')) {\n const params = new URLSearchParams(value.slice('flags:'.length));\n const sdkKey = params.get('sdkKey');\n if (sdkKey?.startsWith('vf_')) {\n acc.add(sdkKey);\n }\n }\n }\n return acc;\n }, new Set<string>()),\n );\n\n output?.debug(`vercel-flags: found ${sdkKeys.length} SDK keys`);\n\n if (sdkKeys.length === 0) {\n return;\n }\n\n // Fetch definitions for each SDK key\n const fetchPromise = Promise.all(\n sdkKeys.map(async (sdkKey) => {\n const headers: Record<string, string> = {\n authorization: `Bearer ${sdkKey}`,\n 'user-agent': `prepare-flags-definitions/${version}`,\n };\n\n // Add Vercel metadata headers if available\n if (env.VERCEL_PROJECT_ID) {\n headers['x-vercel-project-id'] = env.VERCEL_PROJECT_ID;\n }\n if (env.VERCEL_ENV) {\n headers['x-vercel-env'] = env.VERCEL_ENV;\n }\n if (env.VERCEL_DEPLOYMENT_ID) {\n headers['x-vercel-deployment-id'] = env.VERCEL_DEPLOYMENT_ID;\n }\n if (env.VERCEL_REGION) {\n headers['x-vercel-region'] = env.VERCEL_REGION;\n }\n\n const res = await fetchFn(`${FLAGS_HOST}/v1/datafile`, { headers });\n\n if (!res.ok) {\n throw new Error(\n `Failed to fetch flag definitions for ${obfuscate(sdkKey)}: ${res.status} ${res.statusText}`,\n );\n }\n\n return res.json() as Promise<BundledDefinitions>;\n }),\n );\n\n const values = output\n ? await output.time('vercel-flags: load datafiles', fetchPromise)\n : await fetchPromise;\n\n // Generate the JS module\n const definitionsJs = generateDefinitionsModule(sdkKeys, values);\n\n // Write to node_modules/@vercel/flags-definitions/\n const storageDir = join(cwd, 'node_modules', '@vercel', 'flags-definitions');\n const indexPath = join(storageDir, 'index.js');\n const dtsPath = join(storageDir, 'index.d.ts');\n const packageJsonPath = join(storageDir, 'package.json');\n\n const dts = [\n 'export function get(hashedSdkKey: string): Record<string, unknown> | null;',\n 'export const version: string;',\n '',\n ].join('\\n');\n\n const packageJson = {\n name: '@vercel/flags-definitions',\n version: FLAGS_DEFINITIONS_VERSION,\n type: 'module',\n main: './index.js',\n types: './index.d.ts',\n exports: {\n '.': {\n types: './index.d.ts',\n import: './index.js',\n },\n },\n };\n\n await mkdir(storageDir, { recursive: true });\n await Promise.all([\n writeFile(indexPath, definitionsJs),\n writeFile(dtsPath, dts),\n writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)),\n ]);\n\n output?.debug('vercel-flags: created module');\n output?.debug(` → ${indexPath}`);\n output?.debug(` → ${dtsPath}`);\n output?.debug(` → ${packageJsonPath}`);\n output?.debug(\n ` → included definitions for keys \"${sdkKeys.map((key) => obfuscate(key)).join(', ')}\"`,\n );\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AAC3B,SAAS,OAAO,iBAAiB;AACjC,SAAS,YAAY;AAErB,IAAM,aAAa;AACnB,IAAM,4BAA4B;AAYlC,SAAS,UAAU,QAAgB,eAAe,IAAY;AAC5D,MAAI,gBAAgB,OAAO,OAAQ,QAAO;AAC1C,SACE,OAAO,MAAM,GAAG,YAAY,IAAI,IAAI,OAAO,OAAO,SAAS,YAAY;AAE3E;AAKO,SAAS,WAAW,QAAwB;AACjD,SAAO,WAAW,QAAQ,EAAE,OAAO,MAAM,EAAE,OAAO,KAAK;AACzD;AAgBO,SAAS,0BACd,SACA,QACQ;AAER,QAAM,cAAc,QAAQ,IAAI,CAAC,GAAG,MAAM,KAAK,UAAU,OAAO,CAAC,CAAC,CAAC;AAGnE,QAAM,gBAA0B,CAAC;AACjC,QAAM,gBAAgB,oBAAI,IAAoB;AAC9C,aAAW,KAAK,aAAa;AAC3B,QAAI,CAAC,cAAc,IAAI,CAAC,GAAG;AACzB,oBAAc,IAAI,GAAG,cAAc,MAAM;AACzC,oBAAc,KAAK,CAAC;AAAA,IACtB;AAAA,EACF;AAGA,QAAM,aAAa,QAAQ;AAAA,IACzB,CAAC,GAAG,MAAM,cAAc,IAAI,YAAY,CAAC,CAAE,KAAK;AAAA,EAClD;AAGA,QAAM,aAAa,QAAQ,IAAI,UAAU;AAGzC,QAAM,QAAkB;AAAA,IACtB;AAAA,IACA;AAAA,EACF;AAGA,WAAS,IAAI,GAAG,IAAI,cAAc,QAAQ,KAAK;AAC7C,UAAM;AAAA,MACJ,WAAW,CAAC,4BAA4B,KAAK,UAAU,cAAc,CAAC,CAAC,CAAC;AAAA,IAC1E;AAAA,EACF;AAEA,QAAM,KAAK,EAAE;AACb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KAAK,CAAC;AAC3C,QAAM,QAAQ,IAAI;AAAA,IAChB,UAAU,WAAW,aAAa;AAAA,IAClC,UAAU,SAAS,GAAG;AAAA,IACtB,UAAU,iBAAiB,KAAK,UAAU,aAAa,MAAM,CAAC,CAAC;AAAA,EACjE,CAAC;AAED,mCAAQ,MAAM;AACd,mCAAQ,MAAM,YAAO,SAAS;AAC9B,mCAAQ,MAAM,YAAO,OAAO;AAC5B,mCAAQ,MAAM,YAAO,eAAe;AACpC,mCAAQ;AAAA,IACN,2CAAsC,QAAQ,IAAI,CAAC,QAAQ,UAAU,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC;AAAA;AAEzF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vercel/prepare-flags-definitions",
|
|
3
|
+
"version": "0.1.0-3ca407e-20260306094417",
|
|
4
|
+
"description": "",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"typesVersions": {
|
|
20
|
+
"*": {
|
|
21
|
+
".": [
|
|
22
|
+
"dist/*.d.ts",
|
|
23
|
+
"dist/*.d.cts"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"CHANGELOG.md"
|
|
30
|
+
],
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "20.11.17",
|
|
33
|
+
"tsup": "8.5.1",
|
|
34
|
+
"typescript": "5.6.3",
|
|
35
|
+
"vite": "6.4.1",
|
|
36
|
+
"vitest": "2.1.9"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"dev": "tsup --watch",
|
|
44
|
+
"check": "biome check",
|
|
45
|
+
"test": "vitest --run",
|
|
46
|
+
"test:watch": "vitest",
|
|
47
|
+
"type-check": "tsc --noEmit"
|
|
48
|
+
}
|
|
49
|
+
}
|