opencode-translate 0.0.9 → 0.0.10
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/package.json +1 -1
- package/src/translator.ts +36 -2
package/package.json
CHANGED
package/src/translator.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createHash,
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto"
|
|
2
2
|
import { setTimeout as sleep } from "node:timers/promises"
|
|
3
3
|
import { generateText } from "ai"
|
|
4
4
|
import { createCredentialResolver } from "./auth"
|
|
@@ -51,8 +51,29 @@ function withTimeout<T>(promise: Promise<T>, timeoutMs: number, label: string):
|
|
|
51
51
|
|
|
52
52
|
const providerFactoryCache = new Map<string, unknown>()
|
|
53
53
|
|
|
54
|
+
const PART_ID_LENGTH = 26
|
|
55
|
+
const PART_ID_PREFIX = "prt"
|
|
56
|
+
const BASE62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
57
|
+
let partLastTimestamp = 0
|
|
58
|
+
let partCounter = 0
|
|
59
|
+
|
|
54
60
|
export function __resetTranslatorCachesForTest() {
|
|
55
61
|
providerFactoryCache.clear()
|
|
62
|
+
__resetSyntheticPartIDForTest()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function __resetSyntheticPartIDForTest() {
|
|
66
|
+
partLastTimestamp = 0
|
|
67
|
+
partCounter = 0
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function randomBase62(length: number): string {
|
|
71
|
+
const bytes = randomBytes(length)
|
|
72
|
+
let result = ""
|
|
73
|
+
for (let index = 0; index < length; index += 1) {
|
|
74
|
+
result += BASE62_CHARS[bytes[index] % BASE62_CHARS.length]
|
|
75
|
+
}
|
|
76
|
+
return result
|
|
56
77
|
}
|
|
57
78
|
|
|
58
79
|
function getStatus(error: unknown): number | undefined {
|
|
@@ -201,7 +222,20 @@ export function hashText(text: string): string {
|
|
|
201
222
|
}
|
|
202
223
|
|
|
203
224
|
export function createSyntheticPartID(): string {
|
|
204
|
-
|
|
225
|
+
const currentTimestamp = Date.now()
|
|
226
|
+
if (currentTimestamp !== partLastTimestamp) {
|
|
227
|
+
partLastTimestamp = currentTimestamp
|
|
228
|
+
partCounter = 0
|
|
229
|
+
}
|
|
230
|
+
partCounter += 1
|
|
231
|
+
|
|
232
|
+
const encoded = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(partCounter)
|
|
233
|
+
const timeBytes = Buffer.alloc(6)
|
|
234
|
+
for (let index = 0; index < 6; index += 1) {
|
|
235
|
+
timeBytes[index] = Number((encoded >> BigInt(40 - 8 * index)) & BigInt(0xff))
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return `${PART_ID_PREFIX}_${timeBytes.toString("hex")}${randomBase62(PART_ID_LENGTH - 12)}`
|
|
205
239
|
}
|
|
206
240
|
|
|
207
241
|
export function createTranslator(
|