@yaebal/files 0.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 neverlane
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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @yaebal/files
2
+
3
+ resolve a `file_id` to its download URL (via `getFile`).
4
+
5
+ ## install
6
+
7
+ ```sh
8
+ pnpm add @yaebal/files
9
+ ```
10
+
11
+ ---
12
+
13
+ part of [**yaebal**](https://github.com/neverlane/yaebal) — a type-safe, runtime-agnostic Telegram Bot API framework. MIT.
package/lib/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { Context, Plugin } from "@yaebal/core";
2
+ export interface FilesControl {
3
+ /** resolve a `file_id` to its download URL (via `getFile`). */
4
+ fileLink(fileId: string): Promise<string>;
5
+ /** download a `file_id` into memory (buffers the whole file — not for streaming). */
6
+ download(fileId: string): Promise<Uint8Array>;
7
+ }
8
+ /** adds `ctx.files` with helpers to resolve and download telegram files. */
9
+ export declare function files(): Plugin<Context, {
10
+ files: FilesControl;
11
+ }>;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEpD,MAAM,WAAW,YAAY;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC1C,qFAAqF;IACrF,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC9C;AAED,4EAA4E;AAC5E,wBAAgB,KAAK,IAAI,MAAM,CAAC,OAAO,EAAE;IAAE,KAAK,EAAE,YAAY,CAAA;CAAE,CAAC,CA4BhE"}
package/lib/index.js ADDED
@@ -0,0 +1,25 @@
1
+ /** adds `ctx.files` with helpers to resolve and download telegram files. */
2
+ export function files() {
3
+ return (composer) => composer.derive((ctx) => {
4
+ const control = {
5
+ async fileLink(fileId) {
6
+ const file = await ctx.api.call("getFile", {
7
+ file_id: fileId,
8
+ });
9
+ if (!file.file_path) {
10
+ throw new Error(`files: getFile returned no file_path for "${fileId}"`);
11
+ }
12
+ return ctx.api.fileUrl(file.file_path);
13
+ },
14
+ async download(fileId) {
15
+ const res = await fetch(await control.fileLink(fileId));
16
+ if (!res.ok) {
17
+ throw new Error(`files: download failed (${res.status}) for "${fileId}"`);
18
+ }
19
+ return new Uint8Array(await res.arrayBuffer());
20
+ },
21
+ };
22
+ return { files: control };
23
+ });
24
+ }
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AASA,4EAA4E;AAC5E,MAAM,UAAU,KAAK;IACpB,OAAO,CAAC,QAAQ,EAAE,EAAE,CACnB,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE;QACvB,MAAM,OAAO,GAAiB;YAC7B,KAAK,CAAC,QAAQ,CAAC,MAAM;gBACpB,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,GAAG,CAAC,IAAI,CAAyB,SAAS,EAAE;oBAClE,OAAO,EAAE,MAAM;iBACf,CAAC,CAAC;gBAEH,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;oBACrB,MAAM,IAAI,KAAK,CAAC,6CAA6C,MAAM,GAAG,CAAC,CAAC;gBACzE,CAAC;gBAED,OAAO,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YACxC,CAAC;YACD,KAAK,CAAC,QAAQ,CAAC,MAAM;gBACpB,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,MAAM,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;gBAExD,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;oBACb,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,CAAC,MAAM,UAAU,MAAM,GAAG,CAAC,CAAC;gBAC3E,CAAC;gBAED,OAAO,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC;YAChD,CAAC;SACD,CAAC;QAEF,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,73 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { Composer, Context } from "@yaebal/core";
4
+ import { files } from "./index.js";
5
+ const noop = async () => { };
6
+ const entry = (c) => c.toMiddleware();
7
+ function fakeApi(filePath) {
8
+ const api = {
9
+ call: (_method, _params) => Promise.resolve({ file_path: filePath }),
10
+ fileUrl: (p) => `https://api.telegram.org/file/botTOKEN/${p}`,
11
+ };
12
+ return api;
13
+ }
14
+ const ctxWith = (api) => new Context({
15
+ api,
16
+ update: {
17
+ update_id: 1,
18
+ message: { message_id: 1, date: 0, chat: { id: 1, type: "private" } },
19
+ },
20
+ updateType: "message",
21
+ });
22
+ test("fileLink resolves a file_id to its download url", async () => {
23
+ let link = "";
24
+ const c = new Composer().install(files()).use(async (ctx, next) => {
25
+ link = await ctx.files.fileLink("ABC");
26
+ return next();
27
+ });
28
+ await entry(c)(ctxWith(fakeApi("photos/x.jpg")), noop);
29
+ assert.equal(link, "https://api.telegram.org/file/botTOKEN/photos/x.jpg");
30
+ });
31
+ test("fileLink throws when getFile has no file_path", async () => {
32
+ const c = new Composer().install(files()).use(async (ctx) => {
33
+ await assert.rejects(ctx.files.fileLink("ABC"), /no file_path/);
34
+ });
35
+ await entry(c)(ctxWith(fakeApi(undefined)), noop);
36
+ });
37
+ test("download fetches the resolved url into bytes", async () => {
38
+ const realFetch = globalThis.fetch;
39
+ globalThis.fetch = (async () => ({
40
+ ok: true,
41
+ arrayBuffer: async () => new Uint8Array([1, 2, 3]).buffer,
42
+ }));
43
+ try {
44
+ let bytes = [];
45
+ const c = new Composer().install(files()).use(async (ctx, next) => {
46
+ bytes = [...(await ctx.files.download("ABC"))];
47
+ return next();
48
+ });
49
+ await entry(c)(ctxWith(fakeApi("a.bin")), noop);
50
+ assert.deepEqual(bytes, [1, 2, 3]);
51
+ }
52
+ finally {
53
+ globalThis.fetch = realFetch;
54
+ }
55
+ });
56
+ test("download throws on a non-ok response (no silent error bytes)", async () => {
57
+ const realFetch = globalThis.fetch;
58
+ globalThis.fetch = (async () => ({
59
+ ok: false,
60
+ status: 404,
61
+ arrayBuffer: async () => new ArrayBuffer(0),
62
+ }));
63
+ try {
64
+ const c = new Composer().install(files()).use(async (ctx) => {
65
+ await assert.rejects(ctx.files.download("ABC"), /download failed \(404\)/);
66
+ });
67
+ await entry(c)(ctxWith(fakeApi("a.bin")), noop);
68
+ }
69
+ finally {
70
+ globalThis.fetch = realFetch;
71
+ }
72
+ });
73
+ //# sourceMappingURL=index.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAmB,MAAM,cAAc,CAAC;AAClE,OAAO,EAAqB,KAAK,EAAE,MAAM,YAAY,CAAC;AAEtD,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC;AAC5B,MAAM,KAAK,GAAG,CAAoB,CAAc,EAAE,EAAE,CACnD,CAAC,CAAC,YAAY,EAAoC,CAAC;AAEpD,SAAS,OAAO,CAAC,QAAiB;IACjC,MAAM,GAAG,GAAG;QACX,IAAI,EAAE,CAAC,OAAe,EAAE,OAAgB,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;QACrF,OAAO,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,0CAA0C,CAAC,EAAE;KAC5D,CAAC;IAEX,OAAO,GAAG,CAAC;AACZ,CAAC;AAED,MAAM,OAAO,GAAG,CAAC,GAAU,EAAE,EAAE,CAC9B,IAAI,OAAO,CAAC;IACX,GAAG;IACH,MAAM,EAAE;QACP,SAAS,EAAE,CAAC;QACZ,OAAO,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,SAAS,EA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C,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YACpE,MAAM,MAAM,CAAC,OAAO,CAAE,GAAW,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,yBAAyB,CAAC,CAAC;QACrF,CAAC,CAAC,CAAC;QAEH,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IACjD,CAAC;YAAS,CAAC;QACV,UAAU,CAAC,KAAK,GAAG,SAAS,CAAC;IAC9B,CAAC;AACF,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@yaebal/files",
3
+ "version": "0.0.1",
4
+ "description": "yebal files plugin — ctx.files: resolve and download telegram files.",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "import": "./lib/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "lib",
16
+ "src"
17
+ ],
18
+ "dependencies": {
19
+ "@yaebal/core": "0.0.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "latest"
23
+ },
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "keywords": [
28
+ "telegram",
29
+ "telegram-bot",
30
+ "yaebal",
31
+ "files",
32
+ "download"
33
+ ],
34
+ "license": "MIT",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/neverlane/yaebal",
38
+ "directory": "packages/files"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "scripts": {
44
+ "build": "tsc -p tsconfig.json",
45
+ "typecheck": "tsc -p tsconfig.json --noEmit",
46
+ "test": "node --test lib"
47
+ }
48
+ }
@@ -0,0 +1,92 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { Composer, Context, type Middleware } from "@yaebal/core";
4
+ import { type FilesControl, files } from "./index.js";
5
+
6
+ const noop = async () => {};
7
+ const entry = <C extends Context>(c: Composer<C>) =>
8
+ c.toMiddleware() as unknown as Middleware<Context>;
9
+
10
+ function fakeApi(filePath?: string) {
11
+ const api = {
12
+ call: (_method: string, _params: unknown) => Promise.resolve({ file_path: filePath }),
13
+ fileUrl: (p: string) => `https://api.telegram.org/file/botTOKEN/${p}`,
14
+ } as never;
15
+
16
+ return api;
17
+ }
18
+
19
+ const ctxWith = (api: never) =>
20
+ new Context({
21
+ api,
22
+ update: {
23
+ update_id: 1,
24
+ message: { message_id: 1, date: 0, chat: { id: 1, type: "private" } },
25
+ } as never,
26
+ updateType: "message",
27
+ });
28
+
29
+ type Ctx = Context & { files: FilesControl };
30
+
31
+ test("fileLink resolves a file_id to its download url", async () => {
32
+ let link = "";
33
+
34
+ const c = new Composer<Context>().install(files()).use(async (ctx, next) => {
35
+ link = await (ctx as Ctx).files.fileLink("ABC");
36
+ return next();
37
+ });
38
+
39
+ await entry(c)(ctxWith(fakeApi("photos/x.jpg")), noop);
40
+ assert.equal(link, "https://api.telegram.org/file/botTOKEN/photos/x.jpg");
41
+ });
42
+
43
+ test("fileLink throws when getFile has no file_path", async () => {
44
+ const c = new Composer<Context>().install(files()).use(async (ctx) => {
45
+ await assert.rejects((ctx as Ctx).files.fileLink("ABC"), /no file_path/);
46
+ });
47
+
48
+ await entry(c)(ctxWith(fakeApi(undefined)), noop);
49
+ });
50
+
51
+ test("download fetches the resolved url into bytes", async () => {
52
+ const realFetch = globalThis.fetch;
53
+
54
+ globalThis.fetch = (async () => ({
55
+ ok: true,
56
+ arrayBuffer: async () => new Uint8Array([1, 2, 3]).buffer,
57
+ })) as never;
58
+
59
+ try {
60
+ let bytes: number[] = [];
61
+
62
+ const c = new Composer<Context>().install(files()).use(async (ctx, next) => {
63
+ bytes = [...(await (ctx as Ctx).files.download("ABC"))];
64
+ return next();
65
+ });
66
+
67
+ await entry(c)(ctxWith(fakeApi("a.bin")), noop);
68
+ assert.deepEqual(bytes, [1, 2, 3]);
69
+ } finally {
70
+ globalThis.fetch = realFetch;
71
+ }
72
+ });
73
+
74
+ test("download throws on a non-ok response (no silent error bytes)", async () => {
75
+ const realFetch = globalThis.fetch;
76
+
77
+ globalThis.fetch = (async () => ({
78
+ ok: false,
79
+ status: 404,
80
+ arrayBuffer: async () => new ArrayBuffer(0),
81
+ })) as never;
82
+
83
+ try {
84
+ const c = new Composer<Context>().install(files()).use(async (ctx) => {
85
+ await assert.rejects((ctx as Ctx).files.download("ABC"), /download failed \(404\)/);
86
+ });
87
+
88
+ await entry(c)(ctxWith(fakeApi("a.bin")), noop);
89
+ } finally {
90
+ globalThis.fetch = realFetch;
91
+ }
92
+ });
package/src/index.ts ADDED
@@ -0,0 +1,39 @@
1
+ import type { Context, Plugin } from "@yaebal/core";
2
+
3
+ export interface FilesControl {
4
+ /** resolve a `file_id` to its download URL (via `getFile`). */
5
+ fileLink(fileId: string): Promise<string>;
6
+ /** download a `file_id` into memory (buffers the whole file — not for streaming). */
7
+ download(fileId: string): Promise<Uint8Array>;
8
+ }
9
+
10
+ /** adds `ctx.files` with helpers to resolve and download telegram files. */
11
+ export function files(): Plugin<Context, { files: FilesControl }> {
12
+ return (composer) =>
13
+ composer.derive((ctx) => {
14
+ const control: FilesControl = {
15
+ async fileLink(fileId) {
16
+ const file = await ctx.api.call<{ file_path?: string }>("getFile", {
17
+ file_id: fileId,
18
+ });
19
+
20
+ if (!file.file_path) {
21
+ throw new Error(`files: getFile returned no file_path for "${fileId}"`);
22
+ }
23
+
24
+ return ctx.api.fileUrl(file.file_path);
25
+ },
26
+ async download(fileId) {
27
+ const res = await fetch(await control.fileLink(fileId));
28
+
29
+ if (!res.ok) {
30
+ throw new Error(`files: download failed (${res.status}) for "${fileId}"`);
31
+ }
32
+
33
+ return new Uint8Array(await res.arrayBuffer());
34
+ },
35
+ };
36
+
37
+ return { files: control };
38
+ });
39
+ }