triangle-utils 1.4.113 → 1.4.115
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/src/UtilsMisc.d.ts +1 -15
- package/dist/src/UtilsMisc.js +4 -64
- package/dist/src/UtilsS3.d.ts +8 -0
- package/dist/src/UtilsS3.js +17 -0
- package/package.json +4 -3
- package/src/UtilsMisc.ts +5 -81
- package/src/UtilsS3.ts +20 -0
package/dist/src/UtilsMisc.d.ts
CHANGED
|
@@ -21,23 +21,9 @@ export declare class UtilsMisc {
|
|
|
21
21
|
static get_current_milliseconds(): number;
|
|
22
22
|
static get_random_string(length: number, encoding?: "binary" | "hex" | "base64"): string;
|
|
23
23
|
static sha256(input: string): Promise<string>;
|
|
24
|
+
static mini_sha256(input: string): Promise<string>;
|
|
24
25
|
static encrypt(secret_key: string, text: string): Encryption;
|
|
25
26
|
static decrypt(secret_key: string, encryption: Encryption): string;
|
|
26
27
|
static get_secret_hash(username: string, cognito_client_id: string, cognito_client_secret: string): string;
|
|
27
|
-
static get_chunk_indices(text_length: number, max_length?: number, overlap?: number): [number, number][];
|
|
28
|
-
static chunkify(text: string, max_length?: number, overlap?: number): {
|
|
29
|
-
chunk_index: [number, number];
|
|
30
|
-
chunk_text: string;
|
|
31
|
-
}[];
|
|
32
|
-
static chunkify_lined(text: string, max_length?: number, overlap?: number): {
|
|
33
|
-
chunk_index: [number, number];
|
|
34
|
-
chunk_text: string;
|
|
35
|
-
}[];
|
|
36
|
-
static chunkify_pages(pages: string[], max_length?: number, overlap?: number): {
|
|
37
|
-
chunk_index: [number, number];
|
|
38
|
-
chunk_text: string;
|
|
39
|
-
}[];
|
|
40
|
-
static instantiate<T>(constructor: new (x: any) => T, x: any): T | undefined;
|
|
41
|
-
static instantiator<T>(constructor: new (x: any) => T): (x: any) => T | undefined;
|
|
42
28
|
}
|
|
43
29
|
export {};
|
package/dist/src/UtilsMisc.js
CHANGED
|
@@ -86,6 +86,10 @@ export class UtilsMisc {
|
|
|
86
86
|
const hash = hash_array.map(b => b.toString(16).padStart(2, "0")).join("");
|
|
87
87
|
return hash;
|
|
88
88
|
}
|
|
89
|
+
static async mini_sha256(input) {
|
|
90
|
+
return await this.sha256(input)
|
|
91
|
+
.then(hash => hash.substring(0, 32));
|
|
92
|
+
}
|
|
89
93
|
static encrypt(secret_key, text) {
|
|
90
94
|
const iv = crypto.randomBytes(16);
|
|
91
95
|
const cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(secret_key, "hex"), iv);
|
|
@@ -110,68 +114,4 @@ export class UtilsMisc {
|
|
|
110
114
|
.digest("base64");
|
|
111
115
|
return secret_hash;
|
|
112
116
|
}
|
|
113
|
-
static get_chunk_indices(text_length, max_length = 2500, overlap = 50) {
|
|
114
|
-
const num_chunks = Math.ceil((text_length + overlap) / max_length);
|
|
115
|
-
const chunk_length = Math.ceil((text_length - overlap) / num_chunks + overlap);
|
|
116
|
-
const chunk_indices = [];
|
|
117
|
-
for (let i = 0; i < num_chunks; i++) {
|
|
118
|
-
chunk_indices.push([
|
|
119
|
-
(chunk_length - overlap) * i,
|
|
120
|
-
(chunk_length - overlap) * i + chunk_length
|
|
121
|
-
]);
|
|
122
|
-
}
|
|
123
|
-
return chunk_indices;
|
|
124
|
-
}
|
|
125
|
-
static chunkify(text, max_length = 2500, overlap = 50) {
|
|
126
|
-
const chunk_indices = this.get_chunk_indices(text.length, max_length, overlap);
|
|
127
|
-
const chunks = chunk_indices.map(chunk_index => ({
|
|
128
|
-
chunk_index: chunk_index,
|
|
129
|
-
chunk_text: text.substring(...chunk_index)
|
|
130
|
-
}));
|
|
131
|
-
return chunks;
|
|
132
|
-
}
|
|
133
|
-
static chunkify_lined(text, max_length = 2500, overlap = 50) {
|
|
134
|
-
const chunk_indices = this.get_chunk_indices(text.length, max_length, overlap);
|
|
135
|
-
const lined_chunk_indices = [];
|
|
136
|
-
for (const chunk_index of chunk_indices) {
|
|
137
|
-
const lined_chunk_index = [
|
|
138
|
-
text.lastIndexOf("\n", chunk_index[0]) + 1,
|
|
139
|
-
text.indexOf("\n", chunk_index[1]) + 1
|
|
140
|
-
];
|
|
141
|
-
if (lined_chunk_index[1] < lined_chunk_index[0]) {
|
|
142
|
-
lined_chunk_index[1] += text.length;
|
|
143
|
-
}
|
|
144
|
-
lined_chunk_indices.push(lined_chunk_index);
|
|
145
|
-
}
|
|
146
|
-
return lined_chunk_indices.map(chunk_index => ({
|
|
147
|
-
chunk_index: chunk_index,
|
|
148
|
-
chunk_text: text.substring(...chunk_index)
|
|
149
|
-
}));
|
|
150
|
-
}
|
|
151
|
-
static chunkify_pages(pages, max_length = 2500, overlap = 50) {
|
|
152
|
-
const chunks = [];
|
|
153
|
-
for (const page of pages) {
|
|
154
|
-
const page_chunks = UtilsMisc.chunkify(page, max_length, overlap);
|
|
155
|
-
const start_index = (chunks.length > 0 ? (chunks[chunks.length - 1].chunk_index[1]) : 0);
|
|
156
|
-
for (const page_chunk of page_chunks) {
|
|
157
|
-
chunks.push({
|
|
158
|
-
chunk_index: [page_chunk.chunk_index[0] + start_index, page_chunk.chunk_index[1] + start_index],
|
|
159
|
-
chunk_text: page_chunk.chunk_text + "\n\n"
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return chunks;
|
|
164
|
-
}
|
|
165
|
-
static instantiate(constructor, x) {
|
|
166
|
-
try {
|
|
167
|
-
const instance = new constructor(x);
|
|
168
|
-
return instance;
|
|
169
|
-
}
|
|
170
|
-
catch (error) {
|
|
171
|
-
return undefined;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
static instantiator(constructor) {
|
|
175
|
-
return (x) => UtilsMisc.instantiate(constructor, x);
|
|
176
|
-
}
|
|
177
117
|
}
|
package/dist/src/UtilsS3.d.ts
CHANGED
|
@@ -15,6 +15,11 @@ export declare class UtilsS3 {
|
|
|
15
15
|
ChecksumCRC64NVME?: string | undefined;
|
|
16
16
|
ChecksumSHA1?: string | undefined;
|
|
17
17
|
ChecksumSHA256?: string | undefined;
|
|
18
|
+
ChecksumSHA512?: string | undefined;
|
|
19
|
+
ChecksumMD5?: string | undefined;
|
|
20
|
+
ChecksumXXHASH64?: string | undefined;
|
|
21
|
+
ChecksumXXHASH3?: string | undefined;
|
|
22
|
+
ChecksumXXHASH128?: string | undefined;
|
|
18
23
|
ChecksumType?: import("@aws-sdk/client-s3").ChecksumType | undefined;
|
|
19
24
|
ETag?: string | undefined;
|
|
20
25
|
MissingMeta?: number | undefined;
|
|
@@ -54,6 +59,9 @@ export declare class UtilsS3 {
|
|
|
54
59
|
create(s3_id: string, content: string | Buffer | Readable, options?: {
|
|
55
60
|
content_type_id?: string;
|
|
56
61
|
}): Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput | undefined>;
|
|
62
|
+
upload(s3_id: string, content: string | Buffer | Readable, options?: {
|
|
63
|
+
content_type_id?: string;
|
|
64
|
+
}): Promise<void>;
|
|
57
65
|
delete(s3_id: string): Promise<void>;
|
|
58
66
|
get_download_url(s3_id: string): Promise<string | undefined>;
|
|
59
67
|
get_upload_url(s3_id: string, options?: {
|
package/dist/src/UtilsS3.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { S3, NoSuchKey, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3";
|
|
2
2
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
3
|
+
import { Upload } from "@aws-sdk/lib-storage";
|
|
3
4
|
import { Readable } from "stream";
|
|
4
5
|
function get_s3_input(s3_id) {
|
|
5
6
|
const [bucket, path] = (s3_id.match(/^s3\:\/\/([^\/]+)\/([^$]+)$/) || []).slice(1, 3);
|
|
@@ -146,6 +147,22 @@ export class UtilsS3 {
|
|
|
146
147
|
ContentType: content_type_id
|
|
147
148
|
});
|
|
148
149
|
}
|
|
150
|
+
async upload(s3_id, content, options = {}) {
|
|
151
|
+
const content_type_id = options.content_type_id !== undefined ? options.content_type_id : undefined;
|
|
152
|
+
const s3_input = get_s3_input(s3_id);
|
|
153
|
+
if (s3_input === undefined) {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
const upload = new Upload({
|
|
157
|
+
client: this.s3,
|
|
158
|
+
params: {
|
|
159
|
+
...s3_input,
|
|
160
|
+
Body: content,
|
|
161
|
+
ContentType: content_type_id
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
await upload.done();
|
|
165
|
+
}
|
|
149
166
|
async delete(s3_id) {
|
|
150
167
|
const s3_input = get_s3_input(s3_id);
|
|
151
168
|
if (s3_input === undefined) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "triangle-utils",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.115",
|
|
4
4
|
"main": "dist/src/index.js",
|
|
5
5
|
"types": "dist/src/index.d.ts",
|
|
6
6
|
"directories": {
|
|
@@ -20,11 +20,12 @@
|
|
|
20
20
|
"@aws-sdk/client-bedrock-runtime": "^3.953.0",
|
|
21
21
|
"@aws-sdk/client-cognito-identity-provider": "^3.1004.0",
|
|
22
22
|
"@aws-sdk/client-dynamodb": "^3.953.0",
|
|
23
|
-
"@aws-sdk/client-s3": "^3.
|
|
23
|
+
"@aws-sdk/client-s3": "^3.1106.0",
|
|
24
24
|
"@aws-sdk/client-s3vectors": "^3.953.0",
|
|
25
25
|
"@aws-sdk/client-secrets-manager": "^3.965.0",
|
|
26
26
|
"@aws-sdk/client-ses": "^3.1032.0",
|
|
27
|
-
"@aws-sdk/
|
|
27
|
+
"@aws-sdk/lib-storage": "^3.1106.0",
|
|
28
|
+
"@aws-sdk/s3-request-presigner": "^3.1106.0",
|
|
28
29
|
"@types/jsdom": "^28.0.1",
|
|
29
30
|
"@types/node": "^25.2.3",
|
|
30
31
|
"@types/nodemailer": "^7.0.9",
|
package/src/UtilsMisc.ts
CHANGED
|
@@ -108,6 +108,11 @@ export class UtilsMisc {
|
|
|
108
108
|
return hash
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
static async mini_sha256(input : string) : Promise<string> {
|
|
112
|
+
return await this.sha256(input)
|
|
113
|
+
.then(hash => hash.substring(0, 32))
|
|
114
|
+
}
|
|
115
|
+
|
|
111
116
|
static encrypt(secret_key : string, text : string) : Encryption {
|
|
112
117
|
const iv = crypto.randomBytes(16)
|
|
113
118
|
const cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(secret_key, "hex"), iv)
|
|
@@ -135,85 +140,4 @@ export class UtilsMisc {
|
|
|
135
140
|
return secret_hash
|
|
136
141
|
}
|
|
137
142
|
|
|
138
|
-
static get_chunk_indices(text_length : number, max_length = 2500, overlap = 50) : [number, number][] {
|
|
139
|
-
const num_chunks = Math.ceil((text_length + overlap) / max_length)
|
|
140
|
-
const chunk_length = Math.ceil((text_length - overlap) / num_chunks + overlap)
|
|
141
|
-
const chunk_indices : [number, number][] = []
|
|
142
|
-
for (let i = 0; i < num_chunks; i++) {
|
|
143
|
-
chunk_indices.push([
|
|
144
|
-
(chunk_length - overlap) * i,
|
|
145
|
-
(chunk_length - overlap) * i + chunk_length
|
|
146
|
-
])
|
|
147
|
-
}
|
|
148
|
-
return chunk_indices
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
static chunkify(text : string, max_length = 2500, overlap = 50) : {
|
|
152
|
-
chunk_index : [number, number],
|
|
153
|
-
chunk_text : string
|
|
154
|
-
}[] {
|
|
155
|
-
const chunk_indices = this.get_chunk_indices(text.length, max_length, overlap)
|
|
156
|
-
const chunks = chunk_indices.map(chunk_index => ({
|
|
157
|
-
chunk_index : chunk_index,
|
|
158
|
-
chunk_text : text.substring(...chunk_index)
|
|
159
|
-
}))
|
|
160
|
-
return chunks
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
static chunkify_lined(text : string, max_length = 2500, overlap = 50) : {
|
|
164
|
-
chunk_index : [number, number],
|
|
165
|
-
chunk_text : string
|
|
166
|
-
}[] {
|
|
167
|
-
const chunk_indices : [number, number][] = this.get_chunk_indices(text.length, max_length, overlap)
|
|
168
|
-
const lined_chunk_indices : [number, number][] = []
|
|
169
|
-
for (const chunk_index of chunk_indices) {
|
|
170
|
-
const lined_chunk_index : [number, number] = [
|
|
171
|
-
text.lastIndexOf("\n", chunk_index[0]) + 1,
|
|
172
|
-
text.indexOf("\n", chunk_index[1]) + 1
|
|
173
|
-
]
|
|
174
|
-
if (lined_chunk_index[1] < lined_chunk_index[0]) {
|
|
175
|
-
lined_chunk_index[1] += text.length
|
|
176
|
-
}
|
|
177
|
-
lined_chunk_indices.push(lined_chunk_index)
|
|
178
|
-
}
|
|
179
|
-
return lined_chunk_indices.map(chunk_index => ({
|
|
180
|
-
chunk_index : chunk_index,
|
|
181
|
-
chunk_text : text.substring(...chunk_index)
|
|
182
|
-
}))
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
static chunkify_pages(pages : string[], max_length = 2500, overlap = 50) : {
|
|
186
|
-
chunk_index : [number, number],
|
|
187
|
-
chunk_text : string
|
|
188
|
-
}[] {
|
|
189
|
-
const chunks : {
|
|
190
|
-
chunk_index : [number, number],
|
|
191
|
-
chunk_text : string
|
|
192
|
-
}[] = []
|
|
193
|
-
for (const page of pages) {
|
|
194
|
-
const page_chunks = UtilsMisc.chunkify(page, max_length, overlap)
|
|
195
|
-
const start_index : number = (chunks.length > 0 ? (chunks[chunks.length - 1].chunk_index[1]) : 0)
|
|
196
|
-
for (const page_chunk of page_chunks) {
|
|
197
|
-
chunks.push({
|
|
198
|
-
chunk_index : [page_chunk.chunk_index[0] + start_index, page_chunk.chunk_index[1] + start_index],
|
|
199
|
-
chunk_text : page_chunk.chunk_text + "\n\n"
|
|
200
|
-
})
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return chunks
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
static instantiate<T>(constructor : new (x : any) => T, x : any) : T | undefined {
|
|
207
|
-
try {
|
|
208
|
-
const instance = new constructor(x)
|
|
209
|
-
return instance
|
|
210
|
-
} catch (error) {
|
|
211
|
-
return undefined
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
static instantiator<T>(constructor : new (x : any) => T) : (x : any) => T | undefined {
|
|
216
|
-
return (x : any) => UtilsMisc.instantiate(constructor, x)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
143
|
}
|
package/src/UtilsS3.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { S3, NoSuchKey, GetObjectCommand, PutObjectCommand, ListObjectsV2CommandOutput, ListObjectsV2CommandInput } from "@aws-sdk/client-s3"
|
|
2
2
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
|
|
3
|
+
import { Upload } from "@aws-sdk/lib-storage"
|
|
3
4
|
import { Readable } from "stream"
|
|
4
5
|
|
|
5
6
|
interface S3Input {
|
|
@@ -161,6 +162,25 @@ export class UtilsS3 {
|
|
|
161
162
|
})
|
|
162
163
|
}
|
|
163
164
|
|
|
165
|
+
async upload(s3_id : string, content : string | Buffer | Readable, options : {
|
|
166
|
+
content_type_id? : string
|
|
167
|
+
} = {}) : Promise<void> {
|
|
168
|
+
const content_type_id = options.content_type_id !== undefined ? options.content_type_id : undefined
|
|
169
|
+
const s3_input = get_s3_input(s3_id)
|
|
170
|
+
if (s3_input === undefined) {
|
|
171
|
+
return undefined
|
|
172
|
+
}
|
|
173
|
+
const upload = new Upload({
|
|
174
|
+
client : this.s3,
|
|
175
|
+
params : {
|
|
176
|
+
...s3_input,
|
|
177
|
+
Body : content,
|
|
178
|
+
ContentType : content_type_id
|
|
179
|
+
}
|
|
180
|
+
})
|
|
181
|
+
await upload.done()
|
|
182
|
+
}
|
|
183
|
+
|
|
164
184
|
async delete(s3_id : string) : Promise<void>{
|
|
165
185
|
const s3_input = get_s3_input(s3_id)
|
|
166
186
|
if (s3_input === undefined) {
|