|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import { |
| 3 | + getMiniflareObjectBindings, |
| 4 | + getPersistPath, |
| 5 | + Plugin, |
| 6 | + ProxyNodeBinding, |
| 7 | + SERVICE_LOOPBACK, |
| 8 | + SharedBindings, |
| 9 | +} from "miniflare"; |
| 10 | +// The below imports (prefixed with `worker:`) |
| 11 | +// will be converted by our ESBuild plugin |
| 12 | +// into functions that load the transpiled Workers as JS |
| 13 | +import BINDING_WORKER from "worker:binding.worker"; |
| 14 | +import OBJECT_WORKER from "worker:object.worker"; |
| 15 | +import { z } from "zod"; |
| 16 | +import type { Service, Worker_Binding } from "miniflare"; |
| 17 | + |
| 18 | +export const UNSAFE_PLUGIN_NAME = "unsafe-plugin"; |
| 19 | + |
| 20 | +export const UnsafeServiceBindingOptionSchema = z |
| 21 | + .array( |
| 22 | + z.object({ |
| 23 | + name: z.string(), |
| 24 | + type: z.string(), |
| 25 | + plugin: z.object({ |
| 26 | + package: z.string(), |
| 27 | + name: z.string(), |
| 28 | + }), |
| 29 | + options: z.object({ emitLogs: z.boolean() }), |
| 30 | + }) |
| 31 | + ) |
| 32 | + .or(z.undefined()); |
| 33 | + |
| 34 | +export const UNSAFE_SERVICE_PLUGIN: Plugin< |
| 35 | + typeof UnsafeServiceBindingOptionSchema |
| 36 | +> = { |
| 37 | + options: UnsafeServiceBindingOptionSchema, |
| 38 | + /** |
| 39 | + * getBindings will add bindings to the user's Workers. Specifically, we add a binding to a service |
| 40 | + * that will expose an `UnsafeBindingServiceEntrypoint` |
| 41 | + * @param options - A map of bindings names to options provided for that binding. |
| 42 | + * @returns |
| 43 | + */ |
| 44 | + async getBindings(options) { |
| 45 | + return options?.map<Worker_Binding>((binding) => { |
| 46 | + return { |
| 47 | + name: binding.name, |
| 48 | + service: { |
| 49 | + name: `${UNSAFE_PLUGIN_NAME}:${binding.name}`, |
| 50 | + entrypoint: "UnsafeBindingServiceEntrypoint", |
| 51 | + }, |
| 52 | + }; |
| 53 | + }); |
| 54 | + }, |
| 55 | + getNodeBindings(options) { |
| 56 | + return Object.fromEntries( |
| 57 | + options?.map((binding) => [binding.name, new ProxyNodeBinding()]) ?? [] |
| 58 | + ); |
| 59 | + }, |
| 60 | + async getServices({ |
| 61 | + options, |
| 62 | + tmpPath, |
| 63 | + defaultPersistRoot, |
| 64 | + unsafeStickyBlobs, |
| 65 | + }) { |
| 66 | + if (!options || options.length === 0) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + const persistPath = getPersistPath( |
| 71 | + UNSAFE_PLUGIN_NAME, |
| 72 | + tmpPath, |
| 73 | + defaultPersistRoot, |
| 74 | + undefined |
| 75 | + ); |
| 76 | + |
| 77 | + await fs.mkdir(persistPath, { recursive: true }); |
| 78 | + |
| 79 | + // Create a service that will persist any data |
| 80 | + const storageService = { |
| 81 | + name: `${UNSAFE_PLUGIN_NAME}:storage`, |
| 82 | + disk: { path: persistPath, writable: true }, |
| 83 | + } satisfies Service; |
| 84 | + |
| 85 | + const objectService = { |
| 86 | + name: `${UNSAFE_PLUGIN_NAME}:object`, |
| 87 | + worker: { |
| 88 | + compatibilityDate: "2025-01-01", |
| 89 | + modules: [ |
| 90 | + { |
| 91 | + name: "object.worker.js", |
| 92 | + esModule: OBJECT_WORKER(), |
| 93 | + }, |
| 94 | + ], |
| 95 | + durableObjectNamespaces: [ |
| 96 | + { |
| 97 | + className: "UnsafeBindingObject", |
| 98 | + uniqueKey: `miniflare-unsafe-binding-UnsafeBindingObject`, |
| 99 | + }, |
| 100 | + ], |
| 101 | + // Store Durable Object SQL databases in persist path |
| 102 | + durableObjectStorage: { localDisk: storageService.name }, |
| 103 | + // Bind blob disk directory service to object |
| 104 | + bindings: [ |
| 105 | + { |
| 106 | + name: SharedBindings.MAYBE_SERVICE_BLOBS, |
| 107 | + service: { name: storageService.name }, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: SharedBindings.MAYBE_SERVICE_LOOPBACK, |
| 111 | + service: { name: SERVICE_LOOPBACK }, |
| 112 | + }, |
| 113 | + ...getMiniflareObjectBindings(unsafeStickyBlobs), |
| 114 | + ], |
| 115 | + }, |
| 116 | + } satisfies Service; |
| 117 | + |
| 118 | + const bindingWorker = options.map<Service>( |
| 119 | + (binding) => |
| 120 | + ({ |
| 121 | + name: `${UNSAFE_PLUGIN_NAME}:${binding.name}`, |
| 122 | + worker: { |
| 123 | + compatibilityDate: "2025-01-01", |
| 124 | + modules: [ |
| 125 | + { |
| 126 | + name: "binding.worker.js", |
| 127 | + esModule: BINDING_WORKER(), |
| 128 | + }, |
| 129 | + ], |
| 130 | + bindings: [ |
| 131 | + { |
| 132 | + name: "config", |
| 133 | + json: JSON.stringify(binding.options), |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "store", |
| 137 | + durableObjectNamespace: { |
| 138 | + className: "UnsafeBindingObject", |
| 139 | + serviceName: objectService.name, |
| 140 | + }, |
| 141 | + }, |
| 142 | + ], |
| 143 | + }, |
| 144 | + }) satisfies Service |
| 145 | + ); |
| 146 | + |
| 147 | + return [...bindingWorker, storageService, objectService]; |
| 148 | + }, |
| 149 | +}; |
0 commit comments