Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback


A library for managing deno native plugin dependencies
Go to Latest
import { existsSync } from "";import * as log from "";import * as path from "";import { encode, Hash } from "";
const os =;const md5 = new Hash("md5");
const PLUGIN_SUFFIX_MAP: { [os in Deno.OperatingSystem]: string } = { mac: ".dylib", win: ".dll", linux: ".so"};
const pluginSuffix = PLUGIN_SUFFIX_MAP[os];
export interface PreprareOptions { name: string; printLog?: boolean; checkCache?: boolean; urls: { mac?: string; linux?: string; win?: string; };}
export async function prepare(options: PreprareOptions) { const { name, urls, printLog = true, checkCache = true } = options;
if (printLog) { await log.setup({}); }
const remoteUrl = urls[os]; const remoteHash = md5.digest(encode(remoteUrl + pluginSuffix)).hex(); const cacheFileName = `${name}_${remoteHash}${pluginSuffix}`; const localPath = path.resolve(".deno_plugins", cacheFileName);
Deno.mkdirSync(".deno_plugins", { recursive: true });
if (!existsSync(localPath) || !checkCache) { if (!remoteUrl) { throw Error( `"${name}" plugin does not provide binaries suitable for the current system` ); }
if (remoteUrl.startsWith("file://")) { const fromPath = path.resolve(remoteUrl.slice(7)); await copyFromLocal(name, fromPath, localPath); } else { await downloadFromRemote(name, remoteUrl, localPath); } }`load deno plugin "${name}" from local "${localPath}"`); return Deno.openPlugin(localPath);}
async function downloadFromRemote( name: string, remoteUrl: string, savePath: string) {`downloading deno plugin "${name}" from "${remoteUrl}"`); const download = await fetch(remoteUrl);
if (download.status !== 200) { throw Error(`downloading plugin "${name}" from "${remoteUrl}" failed.`); }
const pluginFileData = await download.arrayBuffer(); await Deno.writeFile(savePath, new Uint8Array(pluginFileData));}
async function copyFromLocal(name: string, from: string, to: string) {`copy deno plugin "${name}" from "${from}"`);
if (!existsSync(from)) { throw Error( `copy plugin "${name}" from "${from}" failed, ${from} does not exist.` ); }
await Deno.copyFile(from, to);}