|
| 1 | +import { |
| 2 | + createErrorCallback, |
| 3 | + type LoadedLibrary, |
| 4 | + type SerialLib, |
| 5 | + symbols, |
| 6 | +} from "./ffi.ts"; |
| 7 | + |
| 8 | +type EmbeddedBinary = { |
| 9 | + target: string; |
| 10 | + filename: string; |
| 11 | + encoding: "base64"; |
| 12 | + sha256?: string; |
| 13 | + data: string; |
| 14 | +}; |
| 15 | + |
| 16 | +let extractedLibraryPath: string | null = null; |
| 17 | + |
| 18 | +function base64ToBytes(base64: string): Uint8Array { |
| 19 | + const bin = atob(base64); |
| 20 | + const out = new Uint8Array(bin.length); |
| 21 | + for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i); |
| 22 | + return out; |
| 23 | +} |
| 24 | + |
| 25 | +async function loadEmbeddedBinary(): Promise<EmbeddedBinary> { |
| 26 | + const os = Deno.build.os; |
| 27 | + const arch = Deno.build.arch; |
| 28 | + const target = `${os}-${arch}`; |
| 29 | + |
| 30 | + if (target !== "linux-x86_64") { |
| 31 | + throw new Error( |
| 32 | + `@serial/cpp-bindings-linux only ships an embedded binary for linux-x86_64 right now (got ${target}).`, |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + // Keep this as a dynamic import so local dev builds don't require a real binary JSON. |
| 37 | + const mod = await import("./binaries/linux-x86_64.json", { |
| 38 | + with: { type: "json" }, |
| 39 | + }); |
| 40 | + return mod.default as EmbeddedBinary; |
| 41 | +} |
| 42 | + |
| 43 | +export type EnsureLibraryOptions = { |
| 44 | + /** |
| 45 | + * Directory to write the extracted .so into. |
| 46 | + * Defaults to a temporary directory. |
| 47 | + */ |
| 48 | + dir?: string; |
| 49 | + /** |
| 50 | + * If true, re-write the file even if we already extracted it in this process. |
| 51 | + */ |
| 52 | + force?: boolean; |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Extract the embedded `.so` (stored as JSON/base64) to a real file on disk and return its path. |
| 57 | + * |
| 58 | + * Required permissions: |
| 59 | + * - `--allow-read` (to read the embedded JSON in the package) |
| 60 | + * - `--allow-write` (to write the `.so` to disk) |
| 61 | + */ |
| 62 | +export async function ensureSharedLibraryFile( |
| 63 | + options: EnsureLibraryOptions = {}, |
| 64 | +): Promise<string> { |
| 65 | + if (extractedLibraryPath && !options.force) return extractedLibraryPath; |
| 66 | + |
| 67 | + const embedded = await loadEmbeddedBinary(); |
| 68 | + if (!embedded.data) { |
| 69 | + throw new Error( |
| 70 | + "Embedded binary JSON is empty. If you're running from source, generate it via jsr/scripts/embed_so.ts.", |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + const dir = options.dir ?? |
| 75 | + await Deno.makeTempDir({ prefix: "serial-cpp-bindings-linux-" }); |
| 76 | + const path = `${dir}/${embedded.filename}`; |
| 77 | + const bytes = base64ToBytes(embedded.data); |
| 78 | + |
| 79 | + await Deno.writeFile(path, bytes, { mode: 0o755 }); |
| 80 | + extractedLibraryPath = path; |
| 81 | + return path; |
| 82 | +} |
| 83 | + |
| 84 | +export type LoadSerialLibOptions = { |
| 85 | + /** |
| 86 | + * If provided, skips extraction and loads the .so from this path. |
| 87 | + */ |
| 88 | + libraryPath?: string; |
| 89 | + /** |
| 90 | + * Directory to write the extracted .so into (if `libraryPath` is not provided). |
| 91 | + */ |
| 92 | + extractDir?: string; |
| 93 | + /** |
| 94 | + * Force re-extract (useful if you manage the directory yourself). |
| 95 | + */ |
| 96 | + forceExtract?: boolean; |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * Load the native library via `Deno.dlopen`. |
| 101 | + * |
| 102 | + * Required permissions: |
| 103 | + * - `--allow-ffi` |
| 104 | + * - `--allow-read` (to load the .so) |
| 105 | + * - `--allow-write` (only if extracting the embedded binary) |
| 106 | + */ |
| 107 | +export async function loadSerialLib( |
| 108 | + options: LoadSerialLibOptions = {}, |
| 109 | +): Promise<LoadedLibrary> { |
| 110 | + await Promise.resolve(); // keep async for API stability |
| 111 | + |
| 112 | + const path = options.libraryPath ?? |
| 113 | + await ensureSharedLibraryFile({ |
| 114 | + dir: options.extractDir, |
| 115 | + force: options.forceExtract, |
| 116 | + }); |
| 117 | + |
| 118 | + return Deno.dlopen(path, symbols) as LoadedLibrary; |
| 119 | +} |
| 120 | + |
| 121 | +export { createErrorCallback, symbols }; |
| 122 | +export type { LoadedLibrary, SerialLib }; |
0 commit comments