@deepraxis/sdk 0.0.0-stage → 0.1.0-dev.6cb2ca2
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/LICENSE +202 -0
- package/METHODS.md +1126 -0
- package/README.md +279 -2
- package/dist/automation.d.ts +151 -0
- package/dist/automation.js +8 -0
- package/dist/core/bindings.d.ts +25 -0
- package/dist/core/bindings.js +45 -0
- package/dist/core/client.d.ts +15 -0
- package/dist/core/client.js +15 -0
- package/dist/core/credentials.d.ts +14 -0
- package/dist/core/credentials.js +33 -0
- package/dist/core/errors.d.ts +38 -0
- package/dist/core/errors.js +60 -0
- package/dist/core/multipart.d.ts +2 -0
- package/dist/core/multipart.js +25 -0
- package/dist/core/pagination.d.ts +12 -0
- package/dist/core/pagination.js +26 -0
- package/dist/core/retry.d.ts +13 -0
- package/dist/core/retry.js +56 -0
- package/dist/core/sse.d.ts +13 -0
- package/dist/core/sse.js +67 -0
- package/dist/core/transport.d.ts +36 -0
- package/dist/core/transport.js +121 -0
- package/dist/generated/client/client.gen.d.ts +2 -0
- package/dist/generated/client/client.gen.js +216 -0
- package/dist/generated/client/index.d.ts +10 -0
- package/dist/generated/client/index.js +6 -0
- package/dist/generated/client/types.gen.d.ts +120 -0
- package/dist/generated/client/types.gen.js +2 -0
- package/dist/generated/client/utils.gen.d.ts +37 -0
- package/dist/generated/client/utils.gen.js +228 -0
- package/dist/generated/client.gen.d.ts +12 -0
- package/dist/generated/client.gen.js +3 -0
- package/dist/generated/core/auth.gen.d.ts +25 -0
- package/dist/generated/core/auth.gen.js +14 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +25 -0
- package/dist/generated/core/bodySerializer.gen.js +57 -0
- package/dist/generated/core/params.gen.d.ts +43 -0
- package/dist/generated/core/params.gen.js +109 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +33 -0
- package/dist/generated/core/pathSerializer.gen.js +106 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/generated/core/queryKeySerializer.gen.js +92 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/generated/core/serverSentEvents.gen.js +132 -0
- package/dist/generated/core/types.gen.d.ts +83 -0
- package/dist/generated/core/types.gen.js +2 -0
- package/dist/generated/core/utils.gen.d.ts +19 -0
- package/dist/generated/core/utils.gen.js +87 -0
- package/dist/generated/index.d.ts +2 -0
- package/dist/generated/index.js +2 -0
- package/dist/generated/sdk.gen.d.ts +2172 -0
- package/dist/generated/sdk.gen.js +6579 -0
- package/dist/generated/types.gen.d.ts +56802 -0
- package/dist/generated/types.gen.js +2 -0
- package/dist/helpers/application-publish.d.ts +41 -0
- package/dist/helpers/application-publish.js +116 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +5 -0
- package/dist/operations.generated.d.ts +10965 -0
- package/dist/operations.generated.js +13837 -0
- package/dist/resources.generated.d.ts +3852 -0
- package/dist/resources.generated.js +724 -0
- package/package.json +50 -4
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Praxis } from '../core/client.js';
|
|
2
|
+
import type { ApplicationAssetsConfig, ApplicationBinding } from '../generated/types.gen.js';
|
|
3
|
+
export type AssetsConfig = ApplicationAssetsConfig;
|
|
4
|
+
export interface PublishInput {
|
|
5
|
+
/** 应用名;存在同名应用时复用。 */
|
|
6
|
+
name: string;
|
|
7
|
+
/** 站点路径到 UTF-8 文本或字节内容;自动补前导 /。 */
|
|
8
|
+
files: Record<string, string | Uint8Array>;
|
|
9
|
+
/** 可选 Worker 入口脚本;省略时发布纯静态站点。 */
|
|
10
|
+
main?: {
|
|
11
|
+
name: string;
|
|
12
|
+
content: string;
|
|
13
|
+
content_type?: string;
|
|
14
|
+
};
|
|
15
|
+
/** 运行时兼容日期,默认当天 UTC(YYYY-MM-DD)。 */
|
|
16
|
+
compatibility_date?: string;
|
|
17
|
+
/** 运行时兼容标志,原样传递;平台固定附加 global_fetch_strictly_public。 */
|
|
18
|
+
compatibility_flags?: string[];
|
|
19
|
+
/** 绑定声明(d1、kv_namespace、r2_bucket、secret_text、plain_text、json 等),原样传递。 */
|
|
20
|
+
bindings?: ApplicationBinding[];
|
|
21
|
+
/** 静态资源路由配置,原样传递。 */
|
|
22
|
+
assets_config?: AssetsConfig;
|
|
23
|
+
/** 是否等待部署完成,默认 true。 */
|
|
24
|
+
wait?: boolean;
|
|
25
|
+
/** 部署等待上限(毫秒),默认 120000。超时不会撤销部署。 */
|
|
26
|
+
timeout_ms?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface PublishResult {
|
|
29
|
+
url: string;
|
|
30
|
+
worker_id: string;
|
|
31
|
+
version_id: string;
|
|
32
|
+
deployment_id: string;
|
|
33
|
+
status: string;
|
|
34
|
+
created: boolean;
|
|
35
|
+
/** 本次上传的不同内容 hash 数量。 */
|
|
36
|
+
uploaded: number;
|
|
37
|
+
/** 服务端已存在的不同内容 hash 数量。 */
|
|
38
|
+
reused: number;
|
|
39
|
+
}
|
|
40
|
+
/** Publish through the public SDK resources, including content-addressed incremental uploads. */
|
|
41
|
+
export declare function publish(client: Pick<Praxis, 'application'>, input: PublishInput): Promise<PublishResult>;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { PraxisError } from "../core/errors.js";
|
|
2
|
+
// Application contract: 100k paths, 25 MiB/file; session buckets: 50 files / 40 MiB.
|
|
3
|
+
const FILE_BYTES = 25 * 1024 * 1024;
|
|
4
|
+
function base64(bytes) {
|
|
5
|
+
const chunks = [];
|
|
6
|
+
for (let offset = 0; offset < bytes.length; offset += 8192) {
|
|
7
|
+
chunks.push(String.fromCharCode(...bytes.subarray(offset, offset + 8192)));
|
|
8
|
+
}
|
|
9
|
+
return btoa(chunks.join(''));
|
|
10
|
+
}
|
|
11
|
+
/** Publish through the public SDK resources, including content-addressed incremental uploads. */
|
|
12
|
+
export async function publish(client, input) {
|
|
13
|
+
const timeout = input.timeout_ms ?? 120_000;
|
|
14
|
+
if (!Number.isFinite(timeout) || timeout < 0)
|
|
15
|
+
throw new RangeError('timeout_ms must be non-negative and finite');
|
|
16
|
+
const contentType = ['application/javascript+module', 'text/javascript', 'application/javascript', 'application/json',
|
|
17
|
+
'text/plain', 'application/wasm', 'application/octet-stream', 'application/x-python']
|
|
18
|
+
.find(type => type === (input.main?.content_type ?? 'application/javascript+module'));
|
|
19
|
+
if (!contentType)
|
|
20
|
+
throw new PraxisError({ code: 'INVALID_PARAMS', message: `Unsupported module content_type: ${input.main?.content_type}` });
|
|
21
|
+
const files = Object.entries(input.files);
|
|
22
|
+
if (files.length > 100_000)
|
|
23
|
+
throw new PraxisError({ code: 'LIMIT_EXCEEDED', message: 'At most 100000 asset paths can be published' });
|
|
24
|
+
const manifest = {};
|
|
25
|
+
const contents = new Map();
|
|
26
|
+
for (const [path, content] of files) {
|
|
27
|
+
const normalized = path.startsWith('/') ? path : `/${path}`;
|
|
28
|
+
if (Object.hasOwn(manifest, normalized))
|
|
29
|
+
throw new PraxisError({ code: 'INVALID_PARAMS', message: `Duplicate asset path: ${normalized}` });
|
|
30
|
+
const bytes = typeof content === 'string' ? new TextEncoder().encode(content) : new Uint8Array(content);
|
|
31
|
+
if (bytes.length > FILE_BYTES)
|
|
32
|
+
throw new PraxisError({ code: 'LIMIT_EXCEEDED', message: `Asset ${normalized} exceeds 25 MiB` });
|
|
33
|
+
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes));
|
|
34
|
+
const hash = Array.from(digest.subarray(0, 16), byte => byte.toString(16).padStart(2, '0')).join('');
|
|
35
|
+
manifest[normalized] = { hash, size: bytes.length };
|
|
36
|
+
contents.set(hash, bytes);
|
|
37
|
+
}
|
|
38
|
+
const { workers, assets } = client.application;
|
|
39
|
+
let worker;
|
|
40
|
+
for await (const candidate of await workers.list()) {
|
|
41
|
+
if (candidate.name === input.name) {
|
|
42
|
+
worker = candidate;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const created = !worker;
|
|
47
|
+
worker ??= await workers.create({ body: { name: input.name, subdomain: { enabled: true } } });
|
|
48
|
+
const path = { worker_id: worker.id };
|
|
49
|
+
const session = await workers.assetsUploadSession.create({ path, body: { manifest } });
|
|
50
|
+
let jwt = session.buckets.length ? null : session.jwt;
|
|
51
|
+
const uploaded = new Set();
|
|
52
|
+
for (const bucket of session.buckets) {
|
|
53
|
+
const batch = {};
|
|
54
|
+
let size = 0;
|
|
55
|
+
for (const hash of bucket) {
|
|
56
|
+
const bytes = contents.get(hash);
|
|
57
|
+
if (!bytes)
|
|
58
|
+
throw new PraxisError({ code: 'INVALID_RESPONSE', message: `Upload session requested unknown hash ${hash}` });
|
|
59
|
+
size += bytes.length;
|
|
60
|
+
batch[hash] = base64(bytes);
|
|
61
|
+
}
|
|
62
|
+
if (bucket.length > 50 || size > 40 * 1024 * 1024)
|
|
63
|
+
throw new PraxisError({ code: 'INVALID_RESPONSE', message: 'Upload session bucket exceeds 50 files or 40 MiB' });
|
|
64
|
+
const result = await assets.upload.create({ body: { jwt: session.jwt, files: batch } });
|
|
65
|
+
jwt = result.jwt;
|
|
66
|
+
for (const hash of bucket)
|
|
67
|
+
uploaded.add(hash);
|
|
68
|
+
}
|
|
69
|
+
if (!jwt)
|
|
70
|
+
throw new PraxisError({ code: 'INVALID_RESPONSE', message: 'Asset upload did not return a completion JWT' });
|
|
71
|
+
// The service contract accepts jwt instead of manifest. Its generated OpenAPI
|
|
72
|
+
// type currently omits jwt; keep this explicit shape until that schema catches up.
|
|
73
|
+
const versionAssets = { jwt, ...(input.assets_config ? { config: input.assets_config } : {}) };
|
|
74
|
+
const result = await workers.versions.create({ path, body: {
|
|
75
|
+
deploy: true,
|
|
76
|
+
compatibility_date: input.compatibility_date ?? new Date().toISOString().slice(0, 10),
|
|
77
|
+
assets: versionAssets,
|
|
78
|
+
...(input.compatibility_flags ? { compatibility_flags: input.compatibility_flags } : {}),
|
|
79
|
+
...(input.bindings ? { bindings: input.bindings } : {}),
|
|
80
|
+
...(input.main ? { main_module: input.main.name, modules: [{ name: input.main.name,
|
|
81
|
+
content_type: contentType,
|
|
82
|
+
content_base64: base64(new TextEncoder().encode(input.main.content)) }] } : {}),
|
|
83
|
+
} });
|
|
84
|
+
let deployment = result.deployment;
|
|
85
|
+
if (!deployment)
|
|
86
|
+
throw new PraxisError({ code: 'INVALID_RESPONSE', message: 'Version creation did not return a deployment' });
|
|
87
|
+
if (input.wait ?? true) {
|
|
88
|
+
const deadline = Date.now() + timeout;
|
|
89
|
+
const timedOut = () => new PraxisError({ code: 'DEPLOY_TIMEOUT', message: `Deployment ${deployment.id} did not become active within ${timeout} ms; deployment may still be running` });
|
|
90
|
+
let interval = 500;
|
|
91
|
+
while (deployment.praxis.status !== 'active') {
|
|
92
|
+
if (deployment.praxis.status === 'failed') {
|
|
93
|
+
const error = deployment.praxis.error;
|
|
94
|
+
throw new PraxisError({ code: error?.code ?? 'DEPLOYMENT_FAILED', message: error?.message ?? `Deployment ${deployment.id} failed`, raw: error });
|
|
95
|
+
}
|
|
96
|
+
const remaining = deadline - Date.now();
|
|
97
|
+
if (remaining <= 0)
|
|
98
|
+
throw timedOut();
|
|
99
|
+
await new Promise(resolve => setTimeout(resolve, Math.min(interval, remaining)));
|
|
100
|
+
if (Date.now() >= deadline)
|
|
101
|
+
throw timedOut();
|
|
102
|
+
try {
|
|
103
|
+
deployment = await workers.deployments.retrieve({ path: { ...path, deployment_id: deployment.id }, timeoutMs: deadline - Date.now() });
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (error instanceof PraxisError && error.code === 'TIMEOUT')
|
|
107
|
+
throw timedOut();
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
interval = Math.min(interval * 2, 2000);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return { url: worker.subdomain.url, worker_id: worker.id, version_id: result.version.id,
|
|
114
|
+
deployment_id: deployment.id, status: deployment.praxis.status, created,
|
|
115
|
+
uploaded: uploaded.size, reused: contents.size - uploaded.size };
|
|
116
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { Praxis } from './core/client.js';
|
|
2
|
+
export type { PraxisOptions, RequestOptions } from './core/client.js';
|
|
3
|
+
export { PraxisError, PraxisConnectionError, PraxisTimeoutError } from './core/errors.js';
|
|
4
|
+
export type { ProblemDetails } from './core/errors.js';
|
|
5
|
+
export { Page } from './core/pagination.js';
|
|
6
|
+
export { parseSSE } from './core/sse.js';
|
|
7
|
+
export type { SSEEvent, SSEOptions } from './core/sse.js';
|
|
8
|
+
export { publish } from './helpers/application-publish.js';
|
|
9
|
+
export type { PublishInput, PublishResult, AssetsConfig } from './helpers/application-publish.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Praxis } from "./core/client.js";
|
|
2
|
+
export { PraxisError, PraxisConnectionError, PraxisTimeoutError } from "./core/errors.js";
|
|
3
|
+
export { Page } from "./core/pagination.js";
|
|
4
|
+
export { parseSSE } from "./core/sse.js";
|
|
5
|
+
export { publish } from "./helpers/application-publish.js";
|