@tpmjs/official-base64-encode 0.1.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/LICENSE +21 -0
- package/README.md +82 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +48 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2025 TPMJS
|
|
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,82 @@
|
|
|
1
|
+
# @tpmjs/official-base64-encode
|
|
2
|
+
|
|
3
|
+
Encode string or buffer to base64 format with support for multiple character encodings.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tpmjs/official-base64-encode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { base64EncodeTool } from '@tpmjs/official-base64-encode';
|
|
15
|
+
import { generateText } from 'ai';
|
|
16
|
+
|
|
17
|
+
const result = await generateText({
|
|
18
|
+
model: yourModel,
|
|
19
|
+
tools: {
|
|
20
|
+
base64Encode: base64EncodeTool,
|
|
21
|
+
},
|
|
22
|
+
prompt: 'Encode "Hello, World!" to base64',
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Parameters
|
|
27
|
+
|
|
28
|
+
- `data` (string, required): The data to encode to base64
|
|
29
|
+
- `encoding` (string, optional): Character encoding of the input data
|
|
30
|
+
- Options: `'utf8'` (default), `'binary'`, `'hex'`
|
|
31
|
+
|
|
32
|
+
## Returns
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
{
|
|
36
|
+
base64: string; // The base64 encoded string
|
|
37
|
+
byteLength: number; // The byte length of the original data
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Examples
|
|
42
|
+
|
|
43
|
+
### Encode UTF-8 text (default)
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
const result = await base64EncodeTool.execute({
|
|
47
|
+
data: 'Hello, World!',
|
|
48
|
+
});
|
|
49
|
+
// { base64: 'SGVsbG8sIFdvcmxkIQ==', byteLength: 13 }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Encode binary data
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
const result = await base64EncodeTool.execute({
|
|
56
|
+
data: '\x00\x01\x02\x03',
|
|
57
|
+
encoding: 'binary',
|
|
58
|
+
});
|
|
59
|
+
// { base64: 'AAECAw==', byteLength: 4 }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Encode hex string
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
const result = await base64EncodeTool.execute({
|
|
66
|
+
data: 'deadbeef',
|
|
67
|
+
encoding: 'hex',
|
|
68
|
+
});
|
|
69
|
+
// { base64: '3q2+7w==', byteLength: 4 }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Use Cases
|
|
73
|
+
|
|
74
|
+
- Encoding text for data URIs
|
|
75
|
+
- Preparing binary data for transmission
|
|
76
|
+
- Converting hex strings to base64
|
|
77
|
+
- Encoding authentication credentials
|
|
78
|
+
- Creating base64-encoded images or files
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as ai from 'ai';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base64 Encode Tool for TPMJS
|
|
5
|
+
* Encodes string data to base64 format with support for multiple character encodings
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Supported character encodings for base64 encoding
|
|
9
|
+
*/
|
|
10
|
+
type Encoding = 'utf8' | 'binary' | 'hex';
|
|
11
|
+
/**
|
|
12
|
+
* Input interface for base64 encoding
|
|
13
|
+
*/
|
|
14
|
+
interface Base64EncodeInput {
|
|
15
|
+
data: string;
|
|
16
|
+
encoding?: Encoding;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Output interface for base64 encode result
|
|
20
|
+
*/
|
|
21
|
+
interface Base64EncodeResult {
|
|
22
|
+
base64: string;
|
|
23
|
+
byteLength: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Base64 Encode Tool
|
|
27
|
+
* Encodes string or buffer data to base64 format
|
|
28
|
+
*/
|
|
29
|
+
declare const base64EncodeTool: ai.Tool<Base64EncodeInput, Base64EncodeResult>;
|
|
30
|
+
|
|
31
|
+
export { type Base64EncodeResult, base64EncodeTool, base64EncodeTool as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { tool, jsonSchema } from 'ai';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var base64EncodeTool = tool({
|
|
5
|
+
description: "Encode string or buffer to base64 format. Supports utf8 (default), binary, and hex character encodings. Returns the base64 encoded string and the byte length of the original data.",
|
|
6
|
+
inputSchema: jsonSchema({
|
|
7
|
+
type: "object",
|
|
8
|
+
properties: {
|
|
9
|
+
data: {
|
|
10
|
+
type: "string",
|
|
11
|
+
description: "The data to encode to base64"
|
|
12
|
+
},
|
|
13
|
+
encoding: {
|
|
14
|
+
type: "string",
|
|
15
|
+
enum: ["utf8", "binary", "hex"],
|
|
16
|
+
description: "Character encoding of the input data (default: utf8)"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
required: ["data"],
|
|
20
|
+
additionalProperties: false
|
|
21
|
+
}),
|
|
22
|
+
execute: async ({ data, encoding = "utf8" }) => {
|
|
23
|
+
if (typeof data !== "string") {
|
|
24
|
+
throw new Error("Data must be a string");
|
|
25
|
+
}
|
|
26
|
+
const validEncodings = ["utf8", "binary", "hex"];
|
|
27
|
+
if (!validEncodings.includes(encoding)) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`Invalid encoding: ${encoding}. Must be one of: ${validEncodings.join(", ")}`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const buffer = Buffer.from(data, encoding);
|
|
34
|
+
const base64 = buffer.toString("base64");
|
|
35
|
+
return {
|
|
36
|
+
base64,
|
|
37
|
+
byteLength: buffer.length
|
|
38
|
+
};
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`Failed to encode data: ${error instanceof Error ? error.message : String(error)}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
var index_default = base64EncodeTool;
|
|
47
|
+
|
|
48
|
+
export { base64EncodeTool, index_default as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tpmjs/official-base64-encode",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Encode string or buffer to base64 format with support for multiple character encodings",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"tpmjs",
|
|
8
|
+
"data",
|
|
9
|
+
"base64",
|
|
10
|
+
"encode",
|
|
11
|
+
"encoding"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"tsup": "^8.3.5",
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"@tpmjs/tsconfig": "0.0.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/anthropics/tpmjs.git",
|
|
33
|
+
"directory": "packages/tools/official/base64-encode"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://tpmjs.com",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"tpmjs": {
|
|
38
|
+
"category": "data",
|
|
39
|
+
"frameworks": [
|
|
40
|
+
"vercel-ai"
|
|
41
|
+
],
|
|
42
|
+
"tools": [
|
|
43
|
+
{
|
|
44
|
+
"name": "base64EncodeTool",
|
|
45
|
+
"description": "Encode string or buffer to base64 format with support for multiple character encodings",
|
|
46
|
+
"parameters": [
|
|
47
|
+
{
|
|
48
|
+
"name": "data",
|
|
49
|
+
"type": "string",
|
|
50
|
+
"description": "The data to encode",
|
|
51
|
+
"required": true
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "encoding",
|
|
55
|
+
"type": "string",
|
|
56
|
+
"description": "Character encoding (utf8, binary, hex)",
|
|
57
|
+
"required": false
|
|
58
|
+
}
|
|
59
|
+
],
|
|
60
|
+
"returns": {
|
|
61
|
+
"type": "Base64EncodeResult",
|
|
62
|
+
"description": "Object with base64 encoded string and byte length"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"ai": "6.0.0-beta.124"
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsup",
|
|
72
|
+
"dev": "tsup --watch",
|
|
73
|
+
"type-check": "tsc --noEmit",
|
|
74
|
+
"clean": "rm -rf dist .turbo"
|
|
75
|
+
}
|
|
76
|
+
}
|