@adep/runtime 0.3.9 → 0.3.11
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/dist/functions/runtime/exports.d.ts +4 -2
- package/dist/functions/runtime/sandbox.d.ts +31 -0
- package/dist/index.js +70 -7
- package/dist/shared/schema-diff.d.ts +43 -0
- package/dist/shared/schema-diff.js +138 -3
- package/dist/storage/driver.d.ts +12 -0
- package/dist/storage/signature.d.ts +46 -1
- package/package.json +2 -2
|
@@ -13,8 +13,10 @@
|
|
|
13
13
|
*
|
|
14
14
|
* 子句可跨行(Prettier 对长说明符列表就换行),再导出的 `from` 也可能被排到子句的下一行。
|
|
15
15
|
*
|
|
16
|
-
* 超出子集的语法(装饰器、`import.meta`、动态 import 等)不在 M1
|
|
17
|
-
*
|
|
16
|
+
* 超出子集的语法(装饰器、`import.meta`、动态 import 等)不在 M1 沙箱承诺内。⚠️ FN-060 实证
|
|
17
|
+
* 分层:装饰器 / `import.meta` / top-level await 在加载器的**解析期**被拦下(`sandbox.ts` 包成
|
|
18
|
+
* 「源码语法错误(<出错的那个模块>:行)」);动态 `import(` 则是**执行期**拒绝
|
|
19
|
+
* (ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,提示挂 worker-entry 的调用拒绝面)。
|
|
18
20
|
*
|
|
19
21
|
* ⚠️ 子集外的写法**必须被本文件吃掉或是明确报错**:加载器把产物塞进
|
|
20
22
|
* `(function (module, exports, require) { … })`,只要漏掉一条语句,就是加载期 SyntaxError
|
|
@@ -50,6 +50,37 @@ export declare function resolveModulePath(fromDir: string, request: string, file
|
|
|
50
50
|
* 把「本地静默绕回、线上报错」的漂移提前到本地暴露。
|
|
51
51
|
*/
|
|
52
52
|
export declare function escapesFunctionRoot(fromDir: string, request: string): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 把语法错误统一包装成带**本模块**位置的错误(FN-035)。
|
|
55
|
+
*
|
|
56
|
+
* 位置必须在这里定锚,不能交给上层按堆栈猜:按堆栈猜是**顺序依赖**的——相邻两个模块的路径
|
|
57
|
+
* 都在堆栈里时,报谁取决于**文件集的键顺序**(实测两向都拿到了:入口键排在依赖之前就报入口,
|
|
58
|
+
* 排在之后才报依赖)。现场:countdown 模板的 `_shared/eventview.ts` 写了两处裸再导出 ⇒
|
|
59
|
+
* `/api/events` 与 `/api/shares` 双 500,报位是 `events.ts:6:38` / `shares.ts:18:26`,
|
|
60
|
+
* 而真凶从头到尾都在 `eventview.ts`(`shares.ts` 全文只有一条 import,报位却指着它)。
|
|
61
|
+
* 「正在被编译的那个模块」是唯一确知答案的地方,故转译 / 变换 / 解析三处都在这里定锚。
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* FN-060:动态 import 迁移提示(双宿主共用同一句,追加在报错信息末尾)。
|
|
65
|
+
* M1 沙箱是 ESM→CJS 精简子集,**显式排除动态 import**(exports.ts 头注释的设计约定,用户看不到)。
|
|
66
|
+
*/
|
|
67
|
+
export declare const DYNAMIC_IMPORT_HINT = "\u63D0\u793A\uFF1AM1 \u6C99\u7BB1\u4E0D\u652F\u6301\u52A8\u6001 import\uFF0C\u6539\u9759\u6001 import";
|
|
68
|
+
/**
|
|
69
|
+
* FN-060:沙箱动态 import 拒绝的**错误形态**判定。
|
|
70
|
+
* Node 与 Bun 的 vm 在未提供 `importModuleDynamically` 时都以 `ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`
|
|
71
|
+
* (消息「A dynamic import callback was not specified.」)拒绝;code 与消息双判兜底宿主措辞差异。
|
|
72
|
+
*
|
|
73
|
+
* ⚠️ 探针实证(2026-11-25,双宿主):动态 import **不走解析期**——加载 OK、执行期才拒绝,
|
|
74
|
+
* 故该错误只会以「调用拒绝」形态到达 worker-entry 的错误消息组装处(syntaxErrorWithLocation
|
|
75
|
+
* 拿不到它);worker-entry 按本判定追加提示,不按源码扫描(避免给无关运行时错误加噪音)。
|
|
76
|
+
*/
|
|
77
|
+
export declare function isDynamicImportRejection(error: unknown): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* FN-060:源码是否含动态 `import(`(宽松形态:`import` 前不能是标识符字符 / `.` / `$`,
|
|
80
|
+
* 排除 `x.import(` 与 `_import(` 这类同名成员与标识符)。
|
|
81
|
+
* 字符串 / 注释里的 `import(` 会误报——代价只是多一行提示,可接受:宁可提示一行,不可漏提示。
|
|
82
|
+
*/
|
|
83
|
+
export declare function containsDynamicImport(source: string): boolean;
|
|
53
84
|
/** 构造一个文件模块:返回其 exports(含入口的 default 处理器)。
|
|
54
85
|
* 导出:CF-036 状态对象加载器(state-objects/loader.ts)复用同一精简 require 沙箱。 */
|
|
55
86
|
export declare function loadModule(path: string, files: Readonly<Record<string, string>>, sandboxGlobals: Record<string, unknown>, cache: Map<string, Record<string, unknown>>, resolveBare?: BareSpecifierResolver, bareCache?: Map<string, unknown>, strictPaths?: boolean): Record<string, unknown>;
|
package/dist/index.js
CHANGED
|
@@ -1314,9 +1314,20 @@ var StorageError = class extends Error {
|
|
|
1314
1314
|
var STORAGE_CODES = {
|
|
1315
1315
|
invalidPath: "STORAGE_INVALID_PATH",
|
|
1316
1316
|
invalidData: "STORAGE_INVALID_DATA",
|
|
1317
|
+
invalidVisibility: "STORAGE_INVALID_VISIBILITY",
|
|
1318
|
+
invalidSignedUrlTtl: "STORAGE_SIGNED_URL_TTL_INVALID",
|
|
1319
|
+
signedUrlTtlExceeded: "STORAGE_SIGNED_URL_TTL_EXCEEDED",
|
|
1317
1320
|
notFound: "STORAGE_NOT_FOUND",
|
|
1318
1321
|
quotaExceeded: "STORAGE_QUOTA_EXCEEDED"
|
|
1319
1322
|
};
|
|
1323
|
+
function assertFileVisibility(value) {
|
|
1324
|
+
if (value === "public" || value === "private") return value;
|
|
1325
|
+
throw new StorageError(
|
|
1326
|
+
400,
|
|
1327
|
+
STORAGE_CODES.invalidVisibility,
|
|
1328
|
+
`\u975E\u6CD5\u7684\u6587\u4EF6\u53EF\u89C1\u6027 ${JSON.stringify(value)}\uFF08\u53EA\u652F\u6301 'public' / 'private'\uFF09`
|
|
1329
|
+
);
|
|
1330
|
+
}
|
|
1320
1331
|
function assertSafeStoragePath(path) {
|
|
1321
1332
|
if (typeof path !== "string" || path.length === 0) {
|
|
1322
1333
|
throw new StorageError(400, STORAGE_CODES.invalidPath, "\u5B58\u50A8\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A");
|
|
@@ -1532,17 +1543,51 @@ function constantTimeEqual(a, b) {
|
|
|
1532
1543
|
|
|
1533
1544
|
// packages/runtime/src/storage/signature.ts
|
|
1534
1545
|
var DEFAULT_SIGN_TTL_SECONDS = 15 * 60;
|
|
1546
|
+
var MAX_SIGNED_URL_TTL_SECONDS = 7 * 24 * 60 * 60;
|
|
1535
1547
|
var SIGN_EXPIRES_KEY = "x-expires";
|
|
1536
1548
|
var SIGN_SIGNATURE_KEY = "x-signature";
|
|
1549
|
+
function resolveSignedUrlTtl(ttlSeconds, options) {
|
|
1550
|
+
if (ttlSeconds === void 0) return DEFAULT_SIGN_TTL_SECONDS;
|
|
1551
|
+
if (!Number.isInteger(ttlSeconds) || ttlSeconds <= 0) {
|
|
1552
|
+
throw new StorageError(
|
|
1553
|
+
400,
|
|
1554
|
+
STORAGE_CODES.invalidSignedUrlTtl,
|
|
1555
|
+
`\u7B7E\u540D\u6709\u6548\u671F\u5FC5\u987B\u662F\u6B63\u6574\u6570\u79D2\uFF08\u6536\u5230 ${ttlSeconds}\uFF09`
|
|
1556
|
+
);
|
|
1557
|
+
}
|
|
1558
|
+
if (ttlSeconds > MAX_SIGNED_URL_TTL_SECONDS && options?.force !== true) {
|
|
1559
|
+
throw new StorageError(
|
|
1560
|
+
400,
|
|
1561
|
+
STORAGE_CODES.signedUrlTtlExceeded,
|
|
1562
|
+
`\u7B7E\u540D URL \u6709\u6548\u671F\u6700\u957F ${MAX_SIGNED_URL_TTL_SECONDS / 86400} \u5929\uFF08\u6536\u5230 ${ttlSeconds} \u79D2\uFF09\u3002\u9700\u8981\u957F\u671F\u7A33\u5B9A\u7684\u94FE\u63A5\u8BF7\u628A\u6587\u4EF6\u4E0A\u4F20\u4E3A visibility: 'public' \u5E76\u7528 getUrl() \u53D6\u76F4\u94FE\uFF1B\u786E\u9700\u8D85\u957F\u79C1\u6709\u94FE\u63A5\u8BF7\u663E\u5F0F\u4F20 { force: true }\uFF08\u8FC7\u671F\u524D\u94FE\u63A5\u6CC4\u6F0F\u5373\u957F\u671F\u53EF\u8BFB\uFF09\u3002`
|
|
1563
|
+
);
|
|
1564
|
+
}
|
|
1565
|
+
if (ttlSeconds > DEFAULT_SIGN_TTL_SECONDS && options?.force !== true) {
|
|
1566
|
+
console.warn(
|
|
1567
|
+
`[deprecation] \u79C1\u6709\u7B7E\u540D URL \u4F7F\u7528\u4E86\u8D85\u8FC7\u7F3A\u7701 15 \u5206\u949F\u7684\u957F TTL\uFF08${String(ttlSeconds)}s\uFF09\uFF1A\u957F TTL \u79C1\u6709\u94FE\u63A5\u5C06\u5728\u4E0A\u9650\u6536\u7A84\u65F6\u5931\u6548\uFF0C\u957F\u671F\u5C55\u793A\u8BF7\u8FC1\u79FB visibility: 'public' + getUrl() \u53D6\u76F4\u94FE\u3002`
|
|
1568
|
+
);
|
|
1569
|
+
}
|
|
1570
|
+
return ttlSeconds;
|
|
1571
|
+
}
|
|
1537
1572
|
function sign(secret, projectId, path, expires) {
|
|
1538
1573
|
const payload = `${projectId}|${path}|${expires}`;
|
|
1539
1574
|
return hmacSha256Hex(secret, payload);
|
|
1540
1575
|
}
|
|
1541
|
-
function
|
|
1542
|
-
|
|
1543
|
-
|
|
1576
|
+
function publicDownloadUrl(config, projectId, path) {
|
|
1577
|
+
return `${config.baseUrl}/api/v1/projects/${encodeURIComponent(projectId)}/files/${encodeURIComponent(path)}`;
|
|
1578
|
+
}
|
|
1579
|
+
function signDownloadUrlWithExpiry(config, projectId, path, ttlSeconds = DEFAULT_SIGN_TTL_SECONDS, options) {
|
|
1580
|
+
const ttl = resolveSignedUrlTtl(ttlSeconds, options);
|
|
1581
|
+
const expires = Math.floor(Date.now() / 1e3) + ttl;
|
|
1582
|
+
const base = publicDownloadUrl(config, projectId, path);
|
|
1544
1583
|
const signature = sign(config.secret, projectId, path, expires);
|
|
1545
|
-
return
|
|
1584
|
+
return {
|
|
1585
|
+
url: `${base}?${SIGN_EXPIRES_KEY}=${expires}&${SIGN_SIGNATURE_KEY}=${signature}`,
|
|
1586
|
+
expires
|
|
1587
|
+
};
|
|
1588
|
+
}
|
|
1589
|
+
function signDownloadUrl(config, projectId, path, ttlSeconds = DEFAULT_SIGN_TTL_SECONDS, options) {
|
|
1590
|
+
return signDownloadUrlWithExpiry(config, projectId, path, ttlSeconds, options).url;
|
|
1546
1591
|
}
|
|
1547
1592
|
function verifyDownloadSignature(config, projectId, path, query, nowSeconds2 = Math.floor(Date.now() / 1e3)) {
|
|
1548
1593
|
const expiresRaw = query.expires;
|
|
@@ -1607,9 +1652,10 @@ function createStorageCapability(driver, signer, projectId) {
|
|
|
1607
1652
|
switch (method) {
|
|
1608
1653
|
case "upload": {
|
|
1609
1654
|
const [path, data, options] = args;
|
|
1655
|
+
const visibility = assertFileVisibility(options?.visibility ?? DEFAULT_VISIBILITY);
|
|
1610
1656
|
return toStoredFile(
|
|
1611
1657
|
await driver.put(projectId, path, asBytes(data), {
|
|
1612
|
-
visibility
|
|
1658
|
+
visibility,
|
|
1613
1659
|
...options?.contentType === void 0 ? {} : { contentType: options.contentType }
|
|
1614
1660
|
})
|
|
1615
1661
|
);
|
|
@@ -1627,9 +1673,26 @@ function createStorageCapability(driver, signer, projectId) {
|
|
|
1627
1673
|
const [prefix] = args;
|
|
1628
1674
|
return (await driver.list(projectId, prefix)).map(toStoredFile);
|
|
1629
1675
|
}
|
|
1676
|
+
case "getUrl": {
|
|
1677
|
+
const [path] = args;
|
|
1678
|
+
const meta = await driver.getMeta(projectId, path);
|
|
1679
|
+
if (meta.visibility === "public") {
|
|
1680
|
+
const result2 = {
|
|
1681
|
+
url: publicDownloadUrl(signer, projectId, path),
|
|
1682
|
+
expiresAt: null
|
|
1683
|
+
};
|
|
1684
|
+
return result2;
|
|
1685
|
+
}
|
|
1686
|
+
const signed = signDownloadUrlWithExpiry(signer, projectId, path);
|
|
1687
|
+
const result = {
|
|
1688
|
+
url: signed.url,
|
|
1689
|
+
expiresAt: new Date(signed.expires * 1e3).toISOString()
|
|
1690
|
+
};
|
|
1691
|
+
return result;
|
|
1692
|
+
}
|
|
1630
1693
|
case "getSignedUrl": {
|
|
1631
|
-
const [path, ttlSeconds] = args;
|
|
1632
|
-
return signDownloadUrl(signer, projectId, path, ttlSeconds);
|
|
1694
|
+
const [path, ttlSeconds, options] = args;
|
|
1695
|
+
return signDownloadUrl(signer, projectId, path, ttlSeconds, options);
|
|
1633
1696
|
}
|
|
1634
1697
|
default:
|
|
1635
1698
|
throw new StorageError(
|
|
@@ -4,17 +4,38 @@ export interface SchemaColumn {
|
|
|
4
4
|
/** 规范化的类型文本(可能为空:SQLite 允许无类型列)。 */
|
|
5
5
|
type: string;
|
|
6
6
|
notNull: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* 显式声明了 `NULL`(且不是 `NOT NULL`)。
|
|
9
|
+
*
|
|
10
|
+
* 与 `notNull === false` 不同:没写可空性只是「schema 不完整」,不能据此断言线上更严格
|
|
11
|
+
* (DB-025 的反向可空性只对显式 `NULL` 报 warn)。
|
|
12
|
+
*/
|
|
13
|
+
explicitNullable: boolean;
|
|
7
14
|
/** DEFAULT 后的原始表达式;无默认值为 null。 */
|
|
8
15
|
defaultValue: string | null;
|
|
9
16
|
primaryKey: boolean;
|
|
17
|
+
/** 列级 `UNIQUE`(表级 `UNIQUE(...)` 见 `SchemaTable.uniqueConstraints`)。 */
|
|
18
|
+
unique: boolean;
|
|
10
19
|
}
|
|
11
20
|
/** schema.sql 解析出的单表定义。 */
|
|
12
21
|
export interface SchemaTable {
|
|
13
22
|
name: string;
|
|
14
23
|
columns: SchemaColumn[];
|
|
24
|
+
/** 表级 `UNIQUE(...)` 约束的列组(列级 UNIQUE 在 `columns[].unique`)。 */
|
|
25
|
+
uniqueConstraints: string[][];
|
|
15
26
|
/** 原始 `CREATE TABLE` 语句(缺失表时整条执行,保留索引/约束)。 */
|
|
16
27
|
statement: string;
|
|
17
28
|
}
|
|
29
|
+
/** schema.sql 里的 `CREATE [UNIQUE] INDEX` 声明(DB-025)。 */
|
|
30
|
+
export interface SchemaIndex {
|
|
31
|
+
name: string;
|
|
32
|
+
table: string;
|
|
33
|
+
/** 索引列(按声明顺序;表达式索引解析不出普通列时为空数组)。 */
|
|
34
|
+
columns: string[];
|
|
35
|
+
unique: boolean;
|
|
36
|
+
/** 原始语句(计划展示与 `dataStatements` 去重)。 */
|
|
37
|
+
statement: string;
|
|
38
|
+
}
|
|
18
39
|
/** 缺列执行计划。 */
|
|
19
40
|
export interface MissingColumnPlan {
|
|
20
41
|
table: string;
|
|
@@ -26,7 +47,29 @@ export interface MissingColumnPlan {
|
|
|
26
47
|
}
|
|
27
48
|
/** SQL 标识符引用(只用于**程序生成**的 DDL;用户值仍走绑定参数)。 */
|
|
28
49
|
export declare function quoteIdent(name: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* 解析单条 `ALTER TABLE <表> ADD [COLUMN] <列> …`,返回(表, 列)(已去引号、保留原大小写)。
|
|
52
|
+
* 其余形态返回 null——调用方按「不是补列」原样执行。
|
|
53
|
+
*
|
|
54
|
+
* 用途(CLI-108):本地 `--schema` 的 ADD COLUMN 幂等预检——先查 `PRAGMA table_info`
|
|
55
|
+
* 再决定是否执行,重复跑同一 schema.sql 不再因「列已存在」 fail loud。与平台 migrate
|
|
56
|
+
* 计划的生成(`quoteIdent`)共用同一套标识符约定;只认语句**首部**是 ALTER 的文本
|
|
57
|
+
* (语句拆分器已把触发器体整条剥出,字符串字面量里的 ALTER 文本不会以语句首部出现)。
|
|
58
|
+
* 关键字大小写不敏感(SQLite 语义),列名比对由调用方做大小写不敏感处理。
|
|
59
|
+
*/
|
|
60
|
+
export declare function parseAlterAddColumn(sql: string): {
|
|
61
|
+
table: string;
|
|
62
|
+
column: string;
|
|
63
|
+
} | null;
|
|
29
64
|
/** 解析 schema.sql 里的所有 `CREATE TABLE`(其它语句忽略,仍由 apply 阶段原样执行)。 */
|
|
30
65
|
export declare function parseSchemaTables(sql: string): SchemaTable[];
|
|
66
|
+
/** 解析 schema.sql 里的 `CREATE [UNIQUE] INDEX`(表达式索引返回空 columns,由消费方跳过)。 */
|
|
67
|
+
export declare function parseSchemaIndexes(sql: string): SchemaIndex[];
|
|
68
|
+
/**
|
|
69
|
+
* 从一段索引 DDL(PG `pg_indexes.indexdef` / `pg_get_indexdef`)提取列名。
|
|
70
|
+
*
|
|
71
|
+
* 与 schema.sql 共用同一套列条目解析;表达式索引返回 null(调用方按「列未知」处理)。
|
|
72
|
+
*/
|
|
73
|
+
export declare function parseIndexDdlColumns(ddl: string): string[] | null;
|
|
31
74
|
/** 生成缺列计划;不可自动执行的列标 `blocked`。 */
|
|
32
75
|
export declare function planMissingColumn(table: string, column: SchemaColumn): MissingColumnPlan;
|
|
@@ -67,6 +67,53 @@ function unquoteIdent(raw) {
|
|
|
67
67
|
}
|
|
68
68
|
return text;
|
|
69
69
|
}
|
|
70
|
+
function parsePlainIdent(raw) {
|
|
71
|
+
const text = raw.trim();
|
|
72
|
+
if (text.length === 0) return null;
|
|
73
|
+
const first = text[0];
|
|
74
|
+
if (first === '"' || first === "`" || first === "[" || first === "'") {
|
|
75
|
+
const unquoted = unquoteIdent(text);
|
|
76
|
+
return unquoted === text.trim() ? null : unquoted;
|
|
77
|
+
}
|
|
78
|
+
return /^[A-Za-z_][A-Za-z0-9_$]*$/.test(text) ? text : null;
|
|
79
|
+
}
|
|
80
|
+
function takeIdent(text) {
|
|
81
|
+
const trimmed = text.trimStart();
|
|
82
|
+
const first = trimmed[0];
|
|
83
|
+
if (first === '"' || first === "`" || first === "'") {
|
|
84
|
+
let i = 1;
|
|
85
|
+
while (i < trimmed.length) {
|
|
86
|
+
if (trimmed[i] === first) {
|
|
87
|
+
if (trimmed[i + 1] === first) {
|
|
88
|
+
i += 2;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
return { name: unquoteIdent(trimmed.slice(0, i + 1)), rest: trimmed.slice(i + 1) };
|
|
92
|
+
}
|
|
93
|
+
i += 1;
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
if (first === "[") {
|
|
98
|
+
const close = trimmed.indexOf("]");
|
|
99
|
+
if (close === -1) return null;
|
|
100
|
+
return { name: unquoteIdent(trimmed.slice(0, close + 1)), rest: trimmed.slice(close + 1) };
|
|
101
|
+
}
|
|
102
|
+
const bare = /^[A-Za-z_][A-Za-z0-9_$]*/.exec(trimmed);
|
|
103
|
+
if (bare === null) return null;
|
|
104
|
+
return { name: bare[0], rest: trimmed.slice(bare[0].length) };
|
|
105
|
+
}
|
|
106
|
+
function parseAlterAddColumn(sql) {
|
|
107
|
+
const head = /^ALTER\s+TABLE\s+/i.exec(sql.trim());
|
|
108
|
+
if (head === null) return null;
|
|
109
|
+
const table = takeIdent(sql.trim().slice(head[0].length));
|
|
110
|
+
if (table === null) return null;
|
|
111
|
+
const add = /^\s+ADD\s+(?:COLUMN\s+)?/i.exec(table.rest);
|
|
112
|
+
if (add === null) return null;
|
|
113
|
+
const column = takeIdent(table.rest.slice(add[0].length));
|
|
114
|
+
if (column === null) return null;
|
|
115
|
+
return { table: table.name, column: column.name };
|
|
116
|
+
}
|
|
70
117
|
function splitTopLevel(body) {
|
|
71
118
|
const parts = [];
|
|
72
119
|
let depth = 0;
|
|
@@ -160,19 +207,47 @@ function parseColumn(part) {
|
|
|
160
207
|
}
|
|
161
208
|
const type = rest.slice(0, typeEnd).trim();
|
|
162
209
|
rest = rest.slice(typeEnd).trim();
|
|
163
|
-
const notNull = /\bNOT\s+NULL\b/i.test(rest);
|
|
164
|
-
const primaryKey = /\bPRIMARY\s+KEY\b/i.test(rest);
|
|
165
210
|
const defaultMatch = /\bDEFAULT\s+(.+?)(?=\s+(?:NOT|PRIMARY|UNIQUE|CHECK|REFERENCES|COLLATE|GENERATED|AS|AUTOINCREMENT|CONSTRAINT)\b|$)/i.exec(
|
|
166
211
|
rest
|
|
167
212
|
);
|
|
213
|
+
const constraintText = defaultMatch === null ? rest : rest.replace(defaultMatch[0], " ");
|
|
214
|
+
const declarationText = stripNonNullabilityClauses(constraintText);
|
|
215
|
+
const notNull = /\bNOT\s+NULL\b/i.test(declarationText);
|
|
216
|
+
const explicitNullable = !notNull && /\bNULL\b/i.test(declarationText);
|
|
217
|
+
const primaryKey = /\bPRIMARY\s+KEY\b/i.test(declarationText);
|
|
218
|
+
const unique = /\bUNIQUE\b/i.test(declarationText);
|
|
168
219
|
return {
|
|
169
220
|
name,
|
|
170
221
|
type,
|
|
171
222
|
notNull,
|
|
223
|
+
explicitNullable,
|
|
172
224
|
primaryKey,
|
|
225
|
+
unique,
|
|
173
226
|
defaultValue: defaultMatch === null ? null : defaultMatch[1].trim()
|
|
174
227
|
};
|
|
175
228
|
}
|
|
229
|
+
function stripNonNullabilityClauses(text) {
|
|
230
|
+
return text.replace(
|
|
231
|
+
/\bREFERENCES\s+\S+(?:\s*\([^)]*\))?(?:\s+ON\s+(?:DELETE|UPDATE)\s+(?:CASCADE|RESTRICT|SET\s+NULL|SET\s+DEFAULT|NO\s+ACTION))*/gi,
|
|
232
|
+
" "
|
|
233
|
+
).replace(/\bCHECK\s*\([^)]*\)/gi, " ").replace(/\bGENERATED\s+ALWAYS\s+AS\s*\([^)]*\)(?:\s+STORED|\s+VIRTUAL)?/gi, " ");
|
|
234
|
+
}
|
|
235
|
+
function parseTableUnique(part) {
|
|
236
|
+
const trimmed = part.trim();
|
|
237
|
+
const match = /^(?:CONSTRAINT\s+(?:"(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|[A-Za-z_][A-Za-z0-9_$]*)\s+)?UNIQUE\s*\(/i.exec(
|
|
238
|
+
trimmed
|
|
239
|
+
);
|
|
240
|
+
if (match === null) return null;
|
|
241
|
+
const body = findTableBody(trimmed, match[0].lastIndexOf("("));
|
|
242
|
+
if (body === null) return null;
|
|
243
|
+
const columns = [];
|
|
244
|
+
for (const entry of splitTopLevel(body)) {
|
|
245
|
+
const column = parsePlainIdent(entry);
|
|
246
|
+
if (column === null) return null;
|
|
247
|
+
columns.push(column);
|
|
248
|
+
}
|
|
249
|
+
return columns.length === 0 ? null : columns;
|
|
250
|
+
}
|
|
176
251
|
function parseSchemaTables(sql) {
|
|
177
252
|
const tables = [];
|
|
178
253
|
const pattern = /CREATE\s+(?:TEMP|TEMPORARY\s+)?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?("(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|'[^']*'|[A-Za-z_][A-Za-z0-9_$.]*)\s*\(/gi;
|
|
@@ -186,14 +261,71 @@ function parseSchemaTables(sql) {
|
|
|
186
261
|
const rawName = match[1];
|
|
187
262
|
const name = unquoteIdent(rawName.split(".").pop() ?? rawName);
|
|
188
263
|
const columns = [];
|
|
264
|
+
const uniqueConstraints = [];
|
|
189
265
|
for (const part of splitTopLevel(body)) {
|
|
266
|
+
const unique = parseTableUnique(part);
|
|
267
|
+
if (unique !== null) uniqueConstraints.push(unique);
|
|
190
268
|
const column = parseColumn(part);
|
|
191
269
|
if (column !== null) columns.push(column);
|
|
192
270
|
}
|
|
193
|
-
tables.push({ name, columns, statement: statement.trim() });
|
|
271
|
+
tables.push({ name, columns, uniqueConstraints, statement: statement.trim() });
|
|
194
272
|
}
|
|
195
273
|
return tables;
|
|
196
274
|
}
|
|
275
|
+
var CREATE_INDEX_PATTERN = /^\s*CREATE\s+(UNIQUE\s+)?INDEX\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:(?:"(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|[A-Za-z_][A-Za-z0-9_$]*)\s*\.\s*)?("(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|[A-Za-z_][A-Za-z0-9_$]*)\s+ON\s+(?:(?:"(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|[A-Za-z_][A-Za-z0-9_$]*)\s*\.\s*)?("(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|[A-Za-z_][A-Za-z0-9_$]*)\s*(?:USING\s+[A-Za-z_][A-Za-z0-9_]*\s*)?\(/i;
|
|
276
|
+
function parseIndexColumn(raw) {
|
|
277
|
+
let text = raw.trim();
|
|
278
|
+
text = text.replace(/\s+NULLS\s+(?:FIRST|LAST)\s*$/i, "");
|
|
279
|
+
text = text.replace(/\s+(?:ASC|DESC)\s*$/i, "");
|
|
280
|
+
text = text.replace(
|
|
281
|
+
/\s+COLLATE\s+(?:"(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|[A-Za-z_][A-Za-z0-9_$]*)\s*$/i,
|
|
282
|
+
""
|
|
283
|
+
);
|
|
284
|
+
return parsePlainIdent(text);
|
|
285
|
+
}
|
|
286
|
+
function parseSchemaIndexes(sql) {
|
|
287
|
+
const indexes = [];
|
|
288
|
+
for (const statement of splitSqlStatements(sql)) {
|
|
289
|
+
const match = CREATE_INDEX_PATTERN.exec(statement);
|
|
290
|
+
if (match === null) continue;
|
|
291
|
+
const openIndex = match.index + match[0].length - 1;
|
|
292
|
+
const body = findTableBody(statement, openIndex);
|
|
293
|
+
if (body === null) continue;
|
|
294
|
+
const columns = [];
|
|
295
|
+
let expression = false;
|
|
296
|
+
for (const part of splitTopLevel(body)) {
|
|
297
|
+
const column = parseIndexColumn(part);
|
|
298
|
+
if (column === null) {
|
|
299
|
+
expression = true;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
columns.push(column);
|
|
303
|
+
}
|
|
304
|
+
indexes.push({
|
|
305
|
+
name: unquoteIdent(match[2]),
|
|
306
|
+
table: unquoteIdent(match[3].split(".").pop() ?? match[3]),
|
|
307
|
+
columns: expression ? [] : columns,
|
|
308
|
+
unique: match[1] !== void 0,
|
|
309
|
+
statement: statement.trim()
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
return indexes;
|
|
313
|
+
}
|
|
314
|
+
function parseIndexDdlColumns(ddl) {
|
|
315
|
+
const onMatch = /\s+ON\s+/i.exec(ddl);
|
|
316
|
+
if (onMatch === null) return null;
|
|
317
|
+
const openIndex = ddl.indexOf("(", onMatch.index + onMatch[0].length);
|
|
318
|
+
if (openIndex < 0) return null;
|
|
319
|
+
const body = findTableBody(ddl, openIndex);
|
|
320
|
+
if (body === null) return null;
|
|
321
|
+
const columns = [];
|
|
322
|
+
for (const part of splitTopLevel(body)) {
|
|
323
|
+
const column = parseIndexColumn(part);
|
|
324
|
+
if (column === null) return null;
|
|
325
|
+
columns.push(column);
|
|
326
|
+
}
|
|
327
|
+
return columns.length === 0 ? null : columns;
|
|
328
|
+
}
|
|
197
329
|
function planMissingColumn(table, column) {
|
|
198
330
|
const base = `ALTER TABLE ${quoteIdent(table)} ADD COLUMN ${quoteIdent(column.name)}`;
|
|
199
331
|
let blocked = null;
|
|
@@ -213,6 +345,9 @@ function planMissingColumn(table, column) {
|
|
|
213
345
|
};
|
|
214
346
|
}
|
|
215
347
|
export {
|
|
348
|
+
parseAlterAddColumn,
|
|
349
|
+
parseIndexDdlColumns,
|
|
350
|
+
parseSchemaIndexes,
|
|
216
351
|
parseSchemaTables,
|
|
217
352
|
planMissingColumn,
|
|
218
353
|
quoteIdent
|
package/dist/storage/driver.d.ts
CHANGED
|
@@ -57,9 +57,21 @@ export declare class StorageError extends Error {
|
|
|
57
57
|
export declare const STORAGE_CODES: {
|
|
58
58
|
readonly invalidPath: "STORAGE_INVALID_PATH";
|
|
59
59
|
readonly invalidData: "STORAGE_INVALID_DATA";
|
|
60
|
+
readonly invalidVisibility: "STORAGE_INVALID_VISIBILITY";
|
|
61
|
+
readonly invalidSignedUrlTtl: "STORAGE_SIGNED_URL_TTL_INVALID";
|
|
62
|
+
readonly signedUrlTtlExceeded: "STORAGE_SIGNED_URL_TTL_EXCEEDED";
|
|
60
63
|
readonly notFound: "STORAGE_NOT_FOUND";
|
|
61
64
|
readonly quotaExceeded: "STORAGE_QUOTA_EXCEEDED";
|
|
62
65
|
};
|
|
66
|
+
/**
|
|
67
|
+
* 运行期可见性校验(ST-005)。
|
|
68
|
+
*
|
|
69
|
+
* 用户函数是 JS,TS 的 `FileVisibility` 在运行期被擦除;而项目文件路由把
|
|
70
|
+
* 「不等于 `private`」一律当 public 直连(`routes.ts` 的 `meta.visibility === 'private'` 分支)。
|
|
71
|
+
* 放过一个 `'PUBLIC'` / `'privte'` 拼写就等于**把私有文件变成公开**——必须在写入前拒绝,
|
|
72
|
+
* 不能靠下游「看起来像 public 就直连」的兜底。
|
|
73
|
+
*/
|
|
74
|
+
export declare function assertFileVisibility(value: unknown): FileVisibility;
|
|
63
75
|
/**
|
|
64
76
|
* 路径穿越防护(验收 Scenario 4):拒绝绝对路径、`..` 段、空路径、Windows 盘符、控制字符。
|
|
65
77
|
* 合法路径为单层或多层相对段(如 `a`、`a/b.png`),每段不能是 `.`/`..`、空或含路径分隔符。
|
|
@@ -6,10 +6,55 @@ export interface DownloadSignerConfig {
|
|
|
6
6
|
}
|
|
7
7
|
/** M1 签名有效期(PRD §2.4 / ST-001 任务单:15 分钟)。 */
|
|
8
8
|
export declare const DEFAULT_SIGN_TTL_SECONDS: number;
|
|
9
|
+
/**
|
|
10
|
+
* 私有签名 URL 的最长有效期(ST-005):7 天。
|
|
11
|
+
*
|
|
12
|
+
* 签名 URL 的语义是「临时访问」,不设上限时调用方传 1 年就得到 1 年有效的外链——
|
|
13
|
+
* 链接一旦进聊天记录 / 剪贴板 / 日志就是长期可读。长期展示应把文件设为 `public`
|
|
14
|
+
* 并走无签名直链(`getUrl`);确需超长私有链接必须显式 `force`。
|
|
15
|
+
*/
|
|
16
|
+
export declare const MAX_SIGNED_URL_TTL_SECONDS: number;
|
|
9
17
|
export declare const SIGN_EXPIRES_KEY = "x-expires";
|
|
10
18
|
export declare const SIGN_SIGNATURE_KEY = "x-signature";
|
|
19
|
+
/**
|
|
20
|
+
* 校验并归一私有签名 TTL。
|
|
21
|
+
*
|
|
22
|
+
* 非正整数一律拒绝(`0` / 负数此前会生成「一出生就过期」的链接,静默无效);
|
|
23
|
+
* 超过上限且未显式 `force` 拒绝,文案给出 public + getUrl 的替代路径。
|
|
24
|
+
*/
|
|
25
|
+
export declare function resolveSignedUrlTtl(ttlSeconds: number | undefined, options?: {
|
|
26
|
+
force?: boolean;
|
|
27
|
+
}): number;
|
|
28
|
+
/**
|
|
29
|
+
* 公共文件直链(无签名;下载端不校验)。路径整段编码(`/` → `%2F`),对齐 `:path` 单段路由。
|
|
30
|
+
*
|
|
31
|
+
* 返回的是**绝对地址**(完整 `scheme://origin/path`):origin 取 `AUTH_URL` 配置
|
|
32
|
+
* (`DownloadSignerConfig.baseUrl`,与请求 Host 无关)——下游**禁止剥成相对路径**再拼
|
|
33
|
+
* 自己的网关:应用域网关会把 `/api/v1/...` 当自己的路由解析(FN_NOT_FOUND / 图片 404,
|
|
34
|
+
* jiantie 线上实证);本地 `adep serve`(http://127.0.0.1)与生产(https)origin 形态不同,
|
|
35
|
+
* 下游 src 白名单要两套都配。完整口径见 `docs/cloud-serverless-sdk.md` §5.3/§5.5 与 ADR-0034。
|
|
36
|
+
*
|
|
37
|
+
* 这是**项目桶命名空间**的外壳;云应用桶的外壳是
|
|
38
|
+
* `/api/v1/cloud/storage/raw/:key`(`server/domains/cloud/storage.ts`),
|
|
39
|
+
* 两者签名各自绑定 `(bucket, path)`、不可互换——见 ADR-0034。
|
|
40
|
+
*/
|
|
41
|
+
export declare function publicDownloadUrl(config: DownloadSignerConfig, projectId: string, path: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* 生成私有文件的外链(含签名查询参数),并返回签名内的过期时刻(unix 秒)。
|
|
44
|
+
*
|
|
45
|
+
* 与 `signDownloadUrl` 同一份计算:`getUrl` 需要把「URL 会何时失效」显式告诉调用方
|
|
46
|
+
* (`expiresAt`),不能另算一遍时间(两次 `Date.now()` 可能跨秒)。
|
|
47
|
+
*/
|
|
48
|
+
export declare function signDownloadUrlWithExpiry(config: DownloadSignerConfig, projectId: string, path: string, ttlSeconds?: number, options?: {
|
|
49
|
+
force?: boolean;
|
|
50
|
+
}): {
|
|
51
|
+
url: string;
|
|
52
|
+
expires: number;
|
|
53
|
+
};
|
|
11
54
|
/** 生成私有文件的外链(含签名查询参数)。 */
|
|
12
|
-
export declare function signDownloadUrl(config: DownloadSignerConfig, projectId: string, path: string, ttlSeconds?: number
|
|
55
|
+
export declare function signDownloadUrl(config: DownloadSignerConfig, projectId: string, path: string, ttlSeconds?: number, options?: {
|
|
56
|
+
force?: boolean;
|
|
57
|
+
}): string;
|
|
13
58
|
export type SignatureVerification = {
|
|
14
59
|
ok: true;
|
|
15
60
|
} | {
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
},
|
|
8
8
|
"homepage": "https://adep.top",
|
|
9
9
|
"bugs": "https://gitcode.com/yunqiang_wu/ai-yunke/issues",
|
|
10
|
-
"version": "0.3.
|
|
10
|
+
"version": "0.3.11",
|
|
11
11
|
"private": false,
|
|
12
12
|
"type": "module",
|
|
13
13
|
"sideEffects": false,
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@adep/types": "0.4.
|
|
35
|
+
"@adep/types": "0.4.3"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"test": "vitest run",
|