@ts-pf/file 0.1.0-beta.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/README.md +7 -0
- package/dist/codec.d.ts +24 -0
- package/dist/codec.d.ts.map +1 -0
- package/dist/codec.js +132 -0
- package/dist/codec.js.map +1 -0
- package/dist/files.d.ts +17 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +130 -0
- package/dist/files.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
- package/skills/ts-pf-file/SKILL.md +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @ts-pf/file
|
|
2
|
+
|
|
3
|
+
Opt-in `MultipartCodec` for binary attachments. JSON calls stay JSON; values that contain `File`/`Blob` are sent as `multipart/form-data` with the JSON envelope in part `rpc`. Pass the codec to `FetchHandler` / `FetchLink` from `@ts-pf/server-http` / `@ts-pf/client-http`.
|
|
4
|
+
|
|
5
|
+
Agent skill: [`skills/ts-pf-file/`](skills/ts-pf-file/). Sync with `npx skills experimental_sync -y`.
|
|
6
|
+
|
|
7
|
+
Wire spec: [`packages/protocol/PROTOCOL.md`](../protocol/PROTOCOL.md) (Binary attachments).
|
package/dist/codec.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type RpcBodySource, type RpcCodec, type RpcEncodedBody } from '@ts-pf/http';
|
|
2
|
+
import { type RpcRequest, type RpcResponse } from '@ts-pf/protocol';
|
|
3
|
+
export declare class MultipartCodec implements RpcCodec {
|
|
4
|
+
private readonly inner;
|
|
5
|
+
private readonly maxFiles;
|
|
6
|
+
private readonly maxFileSize;
|
|
7
|
+
constructor(options?: {
|
|
8
|
+
inner?: RpcCodec;
|
|
9
|
+
maxFiles?: number;
|
|
10
|
+
maxFileSize?: number;
|
|
11
|
+
});
|
|
12
|
+
encodeRequest(req: RpcRequest): Promise<RpcEncodedBody>;
|
|
13
|
+
decodeRequest(source: RpcBodySource): Promise<RpcRequest>;
|
|
14
|
+
encodeSuccess<T>(output: T): Promise<RpcEncodedBody>;
|
|
15
|
+
encodeFailure(error: {
|
|
16
|
+
code: string;
|
|
17
|
+
message: string;
|
|
18
|
+
data?: unknown;
|
|
19
|
+
}): RpcEncodedBody | Promise<RpcEncodedBody>;
|
|
20
|
+
decodeResponse<T = unknown>(source: RpcBodySource): Promise<RpcResponse<T>>;
|
|
21
|
+
private encodeWithFiles;
|
|
22
|
+
private readMultipart;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=codec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,cAAc,EACpB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAW,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAA;AA+C5E,qBAAa,cAAe,YAAW,QAAQ;IAC7C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAU;IAChC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;gBAExB,OAAO,CAAC,EAAE;QACpB,KAAK,CAAC,EAAE,QAAQ,CAAA;QAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,WAAW,CAAC,EAAE,MAAM,CAAA;KACrB;IAMK,aAAa,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,cAAc,CAAC;IAMvD,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC;IAczD,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC;IAM1D,aAAa,CAAC,KAAK,EAAE;QACnB,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,IAAI,CAAC,EAAE,OAAO,CAAA;KACf,GAAG,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;IAItC,cAAc,CAAC,CAAC,GAAG,OAAO,EAC9B,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;YAmBZ,eAAe;YAsBf,aAAa;CA+B5B"}
|
package/dist/codec.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { JSONCodec, } from '@ts-pf/http';
|
|
2
|
+
import { PFError } from '@ts-pf/protocol';
|
|
3
|
+
import { assertNoPlaceholders, extractFiles, fileFromPart, injectFiles, } from './files.js';
|
|
4
|
+
const DEFAULT_MAX_FILES = 32;
|
|
5
|
+
const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024;
|
|
6
|
+
function mime(contentType) {
|
|
7
|
+
if (contentType == null || contentType === '') {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
return contentType.split(';')[0]?.trim().toLowerCase() ?? '';
|
|
11
|
+
}
|
|
12
|
+
function isJsonContentType(contentType) {
|
|
13
|
+
const type = mime(contentType);
|
|
14
|
+
return type === '' || type === 'application/json';
|
|
15
|
+
}
|
|
16
|
+
function isMultipartContentType(contentType) {
|
|
17
|
+
return mime(contentType) === 'multipart/form-data';
|
|
18
|
+
}
|
|
19
|
+
function badRequest(message) {
|
|
20
|
+
return new PFError({ code: 'BAD_REQUEST', status: 400, message });
|
|
21
|
+
}
|
|
22
|
+
function jsonSource(text) {
|
|
23
|
+
return {
|
|
24
|
+
contentType: 'application/json',
|
|
25
|
+
text: async () => text,
|
|
26
|
+
formData: async () => new FormData(),
|
|
27
|
+
body: () => null,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async function jsonText(encoded) {
|
|
31
|
+
if (typeof encoded.body === 'string') {
|
|
32
|
+
return encoded.body;
|
|
33
|
+
}
|
|
34
|
+
throw new Error('MultipartCodec inner codec must produce a JSON string');
|
|
35
|
+
}
|
|
36
|
+
export class MultipartCodec {
|
|
37
|
+
inner;
|
|
38
|
+
maxFiles;
|
|
39
|
+
maxFileSize;
|
|
40
|
+
constructor(options) {
|
|
41
|
+
this.inner = options?.inner ?? new JSONCodec();
|
|
42
|
+
this.maxFiles = options?.maxFiles ?? DEFAULT_MAX_FILES;
|
|
43
|
+
this.maxFileSize = options?.maxFileSize ?? DEFAULT_MAX_FILE_SIZE;
|
|
44
|
+
}
|
|
45
|
+
async encodeRequest(req) {
|
|
46
|
+
return this.encodeWithFiles(req.input, (value) => this.inner.encodeRequest({ input: value }));
|
|
47
|
+
}
|
|
48
|
+
async decodeRequest(source) {
|
|
49
|
+
if (isMultipartContentType(source.contentType)) {
|
|
50
|
+
const { json, parts } = await this.readMultipart(source);
|
|
51
|
+
const decoded = await this.inner.decodeRequest(jsonSource(json));
|
|
52
|
+
return { input: injectFiles(decoded.input, parts) };
|
|
53
|
+
}
|
|
54
|
+
if (!isJsonContentType(source.contentType)) {
|
|
55
|
+
throw badRequest('Unsupported content type');
|
|
56
|
+
}
|
|
57
|
+
const decoded = await this.inner.decodeRequest(source);
|
|
58
|
+
assertNoPlaceholders(decoded.input);
|
|
59
|
+
return decoded;
|
|
60
|
+
}
|
|
61
|
+
async encodeSuccess(output) {
|
|
62
|
+
return this.encodeWithFiles(output, (value) => this.inner.encodeSuccess(value));
|
|
63
|
+
}
|
|
64
|
+
encodeFailure(error) {
|
|
65
|
+
return this.inner.encodeFailure(error);
|
|
66
|
+
}
|
|
67
|
+
async decodeResponse(source) {
|
|
68
|
+
if (isMultipartContentType(source.contentType)) {
|
|
69
|
+
const { json, parts } = await this.readMultipart(source);
|
|
70
|
+
const decoded = await this.inner.decodeResponse(jsonSource(json));
|
|
71
|
+
if (!decoded.ok) {
|
|
72
|
+
return decoded;
|
|
73
|
+
}
|
|
74
|
+
return { ok: true, output: injectFiles(decoded.output, parts) };
|
|
75
|
+
}
|
|
76
|
+
if (!isJsonContentType(source.contentType)) {
|
|
77
|
+
throw badRequest('Unsupported content type');
|
|
78
|
+
}
|
|
79
|
+
const decoded = await this.inner.decodeResponse(source);
|
|
80
|
+
if (decoded.ok) {
|
|
81
|
+
assertNoPlaceholders(decoded.output);
|
|
82
|
+
}
|
|
83
|
+
return decoded;
|
|
84
|
+
}
|
|
85
|
+
async encodeWithFiles(value, encodeJson) {
|
|
86
|
+
const extracted = extractFiles(value, {
|
|
87
|
+
maxFiles: this.maxFiles,
|
|
88
|
+
maxFileSize: this.maxFileSize,
|
|
89
|
+
});
|
|
90
|
+
const encoded = await encodeJson(extracted.value);
|
|
91
|
+
if (extracted.files.length === 0) {
|
|
92
|
+
return encoded;
|
|
93
|
+
}
|
|
94
|
+
const json = await jsonText(encoded);
|
|
95
|
+
const form = new FormData();
|
|
96
|
+
form.set('rpc', new Blob([json], { type: 'application/json' }));
|
|
97
|
+
for (const [index, file] of extracted.files.entries()) {
|
|
98
|
+
const filename = file instanceof File && file.name ? file.name : 'blob';
|
|
99
|
+
form.set(String(index), file, filename);
|
|
100
|
+
}
|
|
101
|
+
return { contentType: 'multipart/form-data', body: form };
|
|
102
|
+
}
|
|
103
|
+
async readMultipart(source) {
|
|
104
|
+
const form = await source.formData();
|
|
105
|
+
const rpc = form.get('rpc');
|
|
106
|
+
if (rpc == null) {
|
|
107
|
+
throw badRequest('Missing rpc part');
|
|
108
|
+
}
|
|
109
|
+
const json = typeof rpc === 'string' ? rpc : await rpc.text();
|
|
110
|
+
const parts = new Map();
|
|
111
|
+
form.forEach((part, key) => {
|
|
112
|
+
if (key === 'rpc') {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (typeof part === 'string') {
|
|
116
|
+
throw badRequest('File part must be binary');
|
|
117
|
+
}
|
|
118
|
+
if (parts.has(key)) {
|
|
119
|
+
throw badRequest('Duplicate file part');
|
|
120
|
+
}
|
|
121
|
+
if (parts.size >= this.maxFiles) {
|
|
122
|
+
throw badRequest('Too many files');
|
|
123
|
+
}
|
|
124
|
+
if (part.size > this.maxFileSize) {
|
|
125
|
+
throw badRequest('File too large');
|
|
126
|
+
}
|
|
127
|
+
parts.set(key, fileFromPart(part));
|
|
128
|
+
});
|
|
129
|
+
return { json, parts };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,GAIV,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,OAAO,EAAqC,MAAM,iBAAiB,CAAA;AAC5E,OAAO,EACL,oBAAoB,EACpB,YAAY,EACZ,YAAY,EACZ,WAAW,GACZ,MAAM,YAAY,CAAA;AAEnB,MAAM,iBAAiB,GAAG,EAAE,CAAA;AAC5B,MAAM,qBAAqB,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAA;AAE9C,SAAS,IAAI,CAAC,WAA0B;IACtC,IAAI,WAAW,IAAI,IAAI,IAAI,WAAW,KAAK,EAAE,EAAE,CAAC;QAC9C,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,IAAI,EAAE,CAAA;AAC9D,CAAC;AAED,SAAS,iBAAiB,CAAC,WAA0B;IACnD,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,CAAA;IAC9B,OAAO,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,kBAAkB,CAAA;AACnD,CAAC;AAED,SAAS,sBAAsB,CAAC,WAA0B;IACxD,OAAO,IAAI,CAAC,WAAW,CAAC,KAAK,qBAAqB,CAAA;AACpD,CAAC;AAED,SAAS,UAAU,CAAC,OAAe;IACjC,OAAO,IAAI,OAAO,CAAC,EA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IAAI,CAAC,WAAW,EAAE,CAAC;gBACjC,MAAM,UAAU,CAAC,gBAAgB,CAAC,CAAA;YACpC,CAAC;YACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,YAAY,CAAC,IAAI,CAAC,CAAC,CAAA;QACpC,CAAC,CAAC,CAAA;QACF,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;IACxB,CAAC;CACF"}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type FilePlaceholder = {
|
|
2
|
+
readonly $pf: 'file';
|
|
3
|
+
readonly id: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function isFilePlaceholder(value: unknown): value is FilePlaceholder;
|
|
6
|
+
export declare function basename(name: string): string;
|
|
7
|
+
export declare function extractFiles(value: unknown, limits: {
|
|
8
|
+
maxFiles: number;
|
|
9
|
+
maxFileSize: number;
|
|
10
|
+
}): {
|
|
11
|
+
value: unknown;
|
|
12
|
+
files: Blob[];
|
|
13
|
+
};
|
|
14
|
+
export declare function assertNoPlaceholders(value: unknown): void;
|
|
15
|
+
export declare function injectFiles(value: unknown, parts: Map<string, Blob>): unknown;
|
|
16
|
+
export declare function fileFromPart(part: Blob): Blob;
|
|
17
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,eAAe,GAAG;IAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAAA;AAE3E,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,eAAe,CAM1E;AAWD,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAI7C;AAED,wBAAgB,YAAY,CAC1B,KAAK,EAAE,OAAO,EACd,MAAM,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,GAChD;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,IAAI,EAAE,CAAA;CAAE,CAwCnC;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAyBzD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,OAAO,CAoC7E;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAO7C"}
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { PFError } from '@ts-pf/protocol';
|
|
2
|
+
const SKIP_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
3
|
+
export function isFilePlaceholder(value) {
|
|
4
|
+
if (typeof value !== 'object' || value === null) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
const record = value;
|
|
8
|
+
return record.$pf === 'file' && typeof record.id === 'string';
|
|
9
|
+
}
|
|
10
|
+
function isPlainObject(value) {
|
|
11
|
+
const proto = Object.getPrototypeOf(value);
|
|
12
|
+
return proto === Object.prototype || proto === null;
|
|
13
|
+
}
|
|
14
|
+
function badRequest(message) {
|
|
15
|
+
return new PFError({ code: 'BAD_REQUEST', status: 400, message });
|
|
16
|
+
}
|
|
17
|
+
export function basename(name) {
|
|
18
|
+
const trimmed = name.replaceAll('\\', '/');
|
|
19
|
+
const slash = trimmed.lastIndexOf('/');
|
|
20
|
+
return slash === -1 ? trimmed : trimmed.slice(slash + 1);
|
|
21
|
+
}
|
|
22
|
+
export function extractFiles(value, limits) {
|
|
23
|
+
const files = [];
|
|
24
|
+
const seen = new WeakSet();
|
|
25
|
+
const walk = (node) => {
|
|
26
|
+
if (typeof Blob !== 'undefined' && node instanceof Blob) {
|
|
27
|
+
if (files.length >= limits.maxFiles) {
|
|
28
|
+
throw badRequest('Too many files');
|
|
29
|
+
}
|
|
30
|
+
if (node.size > limits.maxFileSize) {
|
|
31
|
+
throw badRequest('File too large');
|
|
32
|
+
}
|
|
33
|
+
const id = String(files.length);
|
|
34
|
+
files.push(node);
|
|
35
|
+
return { $pf: 'file', id };
|
|
36
|
+
}
|
|
37
|
+
if (node === null || typeof node !== 'object') {
|
|
38
|
+
return node;
|
|
39
|
+
}
|
|
40
|
+
if (seen.has(node)) {
|
|
41
|
+
throw badRequest('Circular value');
|
|
42
|
+
}
|
|
43
|
+
seen.add(node);
|
|
44
|
+
if (Array.isArray(node)) {
|
|
45
|
+
return node.map(walk);
|
|
46
|
+
}
|
|
47
|
+
if (!isPlainObject(node)) {
|
|
48
|
+
return node;
|
|
49
|
+
}
|
|
50
|
+
const out = {};
|
|
51
|
+
for (const [key, child] of Object.entries(node)) {
|
|
52
|
+
if (SKIP_KEYS.has(key)) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
out[key] = walk(child);
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
};
|
|
59
|
+
return { value: walk(value), files };
|
|
60
|
+
}
|
|
61
|
+
export function assertNoPlaceholders(value) {
|
|
62
|
+
const walk = (node) => {
|
|
63
|
+
if (isFilePlaceholder(node)) {
|
|
64
|
+
throw badRequest('File placeholder in JSON body');
|
|
65
|
+
}
|
|
66
|
+
if (node === null || typeof node !== 'object') {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (Array.isArray(node)) {
|
|
70
|
+
for (const child of node) {
|
|
71
|
+
walk(child);
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (!isPlainObject(node)) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
for (const [key, child] of Object.entries(node)) {
|
|
79
|
+
if (SKIP_KEYS.has(key)) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
walk(child);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
walk(value);
|
|
86
|
+
}
|
|
87
|
+
export function injectFiles(value, parts) {
|
|
88
|
+
const used = new Set();
|
|
89
|
+
const walk = (node) => {
|
|
90
|
+
if (isFilePlaceholder(node)) {
|
|
91
|
+
const file = parts.get(node.id);
|
|
92
|
+
if (!file) {
|
|
93
|
+
throw badRequest(`Missing file part ${node.id}`);
|
|
94
|
+
}
|
|
95
|
+
used.add(node.id);
|
|
96
|
+
return file;
|
|
97
|
+
}
|
|
98
|
+
if (node === null || typeof node !== 'object') {
|
|
99
|
+
return node;
|
|
100
|
+
}
|
|
101
|
+
if (Array.isArray(node)) {
|
|
102
|
+
return node.map(walk);
|
|
103
|
+
}
|
|
104
|
+
if (!isPlainObject(node)) {
|
|
105
|
+
return node;
|
|
106
|
+
}
|
|
107
|
+
const out = {};
|
|
108
|
+
for (const [key, child] of Object.entries(node)) {
|
|
109
|
+
if (SKIP_KEYS.has(key)) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
out[key] = walk(child);
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
};
|
|
116
|
+
const result = walk(value);
|
|
117
|
+
if (used.size !== parts.size) {
|
|
118
|
+
throw badRequest('Unexpected file part');
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
export function fileFromPart(part) {
|
|
123
|
+
const name = part instanceof File ? basename(part.name) : '';
|
|
124
|
+
const type = part.type;
|
|
125
|
+
if (name.length > 0) {
|
|
126
|
+
return type ? new File([part], name, { type }) : new File([part], name);
|
|
127
|
+
}
|
|
128
|
+
return type ? new Blob([part], { type }) : new Blob([part]);
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.js","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;A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AC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IAC5D,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;IACtB,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAA;IACzE,CAAC;IACD,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;AAC7D,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ts-pf/file",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Opt-in MultipartCodec for File/Blob attachments",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/crobinson42/ts-pf.git",
|
|
9
|
+
"directory": "packages/file"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/crobinson42/ts-pf/tree/main/packages/file#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/crobinson42/ts-pf/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./src/index.ts",
|
|
20
|
+
"import": "./src/index.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"skills"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc -p tsconfig.build.json",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"type-check": "tsc --noEmit",
|
|
40
|
+
"lint": "biome check src tests"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@ts-pf/http": "0.1.0-beta.0",
|
|
44
|
+
"@ts-pf/protocol": "0.1.0-beta.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@ts-pf/client": "0.1.0-beta.0",
|
|
48
|
+
"@ts-pf/client-http": "0.1.0-beta.0",
|
|
49
|
+
"@ts-pf/contract": "0.1.0-beta.0",
|
|
50
|
+
"@ts-pf/server": "0.1.0-beta.0",
|
|
51
|
+
"@ts-pf/server-http": "0.1.0-beta.0",
|
|
52
|
+
"zod": "^4.0.0"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ts-pf-file
|
|
3
|
+
description: Use when sending File or Blob attachments over ts-pf HTTP with MultipartCodec. Triggers: @ts-pf/file, MultipartCodec, multipart/form-data.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @ts-pf/file
|
|
7
|
+
|
|
8
|
+
Opt-in `MultipartCodec` for binary attachments. JSON calls stay JSON.
|
|
9
|
+
|
|
10
|
+
Install: `npm i @ts-pf/file@beta`
|
|
11
|
+
|
|
12
|
+
Link for agents: `npx skills experimental_sync -y`
|
|
13
|
+
|
|
14
|
+
## Do
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { MultipartCodec } from '@ts-pf/file'
|
|
18
|
+
import { FetchHandler } from '@ts-pf/server-http'
|
|
19
|
+
import { FetchLink } from '@ts-pf/client-http'
|
|
20
|
+
|
|
21
|
+
const codec = new MultipartCodec() // optional { maxFiles, maxFileSize, inner }
|
|
22
|
+
new FetchHandler(app, { codec })
|
|
23
|
+
new FetchLink({ url: '/rpc', codec })
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Values that contain `File`/`Blob` are sent as `multipart/form-data` with the JSON envelope in part `rpc`. Put the same codec on handler and link.
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
- `MultipartCodec`
|
|
31
|
+
|
|
32
|
+
## Pair with
|
|
33
|
+
|
|
34
|
+
- `ts-pf-server-http` / `ts-pf-client-http` (`{ codec }`)
|
|
35
|
+
- Wire: `@ts-pf/protocol` `PROTOCOL.md` (Binary attachments)
|
|
36
|
+
|
|
37
|
+
## Don't
|
|
38
|
+
|
|
39
|
+
- `PFFile`, `file()`, or exported walk helpers.
|
|
40
|
+
- File size caps as a `HandlerPlugin` — they are codec options.
|