-
-
Notifications
You must be signed in to change notification settings - Fork 813
Expand file tree
/
Copy pathplugin.dev.ts
More file actions
119 lines (103 loc) · 3.29 KB
/
plugin.dev.ts
File metadata and controls
119 lines (103 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type { NitroAppPlugin } from "nitropack/types";
import type { GetPlatformProxyOptions, PlatformProxy } from "wrangler";
import { useRuntimeConfig } from "nitropack/runtime";
import { getRequestURL } from "h3";
const proxy = await _getPlatformProxy().catch((error) => {
console.error("Failed to initialize wrangler bindings proxy", error);
return _createStubProxy();
});
(globalThis as any).__env__ = proxy.env;
(globalThis as any).__wait_until__ = proxy.ctx.waitUntil.bind(proxy.ctx);
export default <NitroAppPlugin>function (nitroApp) {
nitroApp.hooks.hook("request", async (event) => {
// Inject the various cf values from the proxy in event and event.context
event.context.cf = proxy.cf;
event.context.waitUntil = proxy.ctx.waitUntil.bind(proxy.ctx);
const request = new Request(getRequestURL(event)) as Request & {
cf: typeof proxy.cf;
};
request.cf = proxy.cf;
event.context.cloudflare = {
...event.context.cloudflare,
request,
env: proxy.env,
context: proxy.ctx,
};
// Replicate Nitro production behavior
// https://github.com/unjs/nitro/blob/main/src/runtime/entries/cloudflare-pages.ts#L55
// https://github.com/unjs/nitro/blob/main/src/runtime/app.ts#L120
(event.node.req as any).__unenv__ = {
...(event.node.req as any).__unenv__,
waitUntil: event.context.waitUntil,
};
});
// https://github.com/pi0/nitro-cloudflare-dev/issues/5
// https://github.com/unjs/hookable/issues/98
// @ts-expect-error
nitroApp.hooks._hooks.request.unshift(nitroApp.hooks._hooks.request.pop());
// Dispose proxy when Nitro is closed
nitroApp.hooks.hook("close", () => {
return proxy?.dispose();
});
};
async function _getPlatformProxy() {
const _pkg = "wrangler"; // Bypass bundling!
const { getPlatformProxy } = (await import(_pkg).catch(() => {
throw new Error(
"Package `wrangler` not found, please install it with: `npx nypm@latest add -D wrangler`"
);
})) as typeof import("wrangler");
const runtimeConfig: {
wrangler: {
configPath: string;
persistDir: string;
environment?: string;
};
} = useRuntimeConfig();
const proxyOptions: GetPlatformProxyOptions = {
configPath: runtimeConfig.wrangler.configPath,
persist: { path: runtimeConfig.wrangler.persistDir },
};
// TODO: investigate why
// https://github.com/pi0/nitro-cloudflare-dev/issues/51
if (runtimeConfig.wrangler.environment) {
proxyOptions.environment = runtimeConfig.wrangler.environment;
}
const proxy = await getPlatformProxy(proxyOptions);
return proxy;
}
function _createStubProxy(): PlatformProxy {
return {
env: {},
cf: {} as any,
ctx: {
waitUntil() {},
passThroughOnException() {},
props: {},
},
caches: {
open(): Promise<_CacheStub> {
const result = Promise.resolve(new _CacheStub());
return result;
},
get default(): _CacheStub {
return new _CacheStub();
},
},
dispose: () => Promise.resolve(),
};
}
class _CacheStub {
delete(): Promise<boolean> {
const result = Promise.resolve(false);
return result;
}
match() {
const result = Promise.resolve(undefined);
return result;
}
put(): Promise<void> {
const result = Promise.resolve();
return result;
}
}