Skip to content

Commit

Permalink
sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Oct 3, 2024
1 parent 7ec1eed commit 9e8960d
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/cloudflare-workers-bindings-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,9 @@
"workers-sdk": {
"prerelease": true,
"type": "extension"
},
"dependencies": {
"cloudflare": "^3.5.0",
"undici": "^5.28.4"
}
}
35 changes: 35 additions & 0 deletions packages/cloudflare-workers-bindings-extension/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { readFile } from "fs/promises";
import path from "path";
import type Cloudflare from "cloudflare";

export async function getSdk(workspaceRoot: string): Promise<Cloudflare> {
const wrangler = path.join(
path.join(
workspaceRoot,
"node_modules",
"wrangler",
"wrangler-dist",
"cli.js"
)
);

const { getSdk } = await import(wrangler);

return getSdk();
}

export async function parseTOML(workspaceRoot: string, file: string) {
const wrangler = path.join(
path.join(
workspaceRoot,
"node_modules",
"wrangler",
"wrangler-dist",
"cli.js"
)
);

const { parseTOML } = await import(wrangler);

return parseTOML(await readFile(file, "utf8"));
}
18 changes: 10 additions & 8 deletions packages/cloudflare-workers-bindings-extension/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import crypto from "crypto";
import path from "path";
import * as vscode from "vscode";
import { getSdk } from "./api";
import { showInputBox, showQuickPick } from "./basicInput";
import { multiStepInput } from "./multiStepInput";
import { quickOpen } from "./quickOpen";
import { Binding, DepNodeProvider } from "./workerBindings";

const encoder = new TextEncoder();
export function activate(context: vscode.ExtensionContext) {
export async function activate(context: vscode.ExtensionContext) {
const rootPath =
vscode.workspace.workspaceFolders &&
vscode.workspace.workspaceFolders.length > 0
Expand All @@ -20,18 +21,19 @@ export function activate(context: vscode.ExtensionContext) {
const watcher = vscode.workspace.createFileSystemWatcher("**/wrangler.toml");

context.subscriptions.push(watcher);
// watcher.onDidChange((uri) => {
// console.log("really changed");
// workerBindingsProvider.refresh();
// }); // listen to files being changed
watcher.onDidChange((uri) => {
console.log("really changed");
workerBindingsProvider.refresh();
}); // listen to files being changed

vscode.window.registerTreeDataProvider(
"workerBindings",
workerBindingsProvider
);
vscode.commands.registerCommand("workerBindings.refreshEntry", () =>
workerBindingsProvider.refresh()
);
vscode.commands.registerCommand("workerBindings.refreshEntry", async () => {
workerBindingsProvider.refresh();
console.log(await (await getSdk(rootPath!)).accounts.list());
});
vscode.commands.registerCommand("extension.openPackageOnNpm", (moduleName) =>
vscode.commands.executeCommand(
"vscode.open",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import crypto from "crypto";
import path from "path";
import { setTimeout } from "timers/promises";
import {
CancellationToken,
Disposable,
Expand All @@ -18,6 +19,7 @@ import {
window,
workspace,
} from "vscode";
import { getSdk, parseTOML } from "./api";

const encoder = new TextEncoder();
const kvApiResponse = {
Expand Down Expand Up @@ -127,11 +129,24 @@ export async function multiStepInput(
input: MultiStepInput,
state: Partial<State>
) {
const sdk = await getSdk(rootPath);
console.log(sdk);

const toml = await parseTOML(
rootPath,
path.join(rootPath, "wrangler.toml")
);
console.log(toml);
// TODO: support Wrangler account ID inference + caching, with dialog to choose account
const kvNamespaces = await sdk.kv.namespaces.list({
account_id: toml.account_id,
per_page: 100,
});
let existing = await input.showQuickPick({
title,
step: 2,
totalSteps: 4,
items: kvApiResponse.result.map(
items: kvNamespaces.result.map(
(r) =>
new BindingType(
r.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class DepNodeProvider implements vscode.TreeDataProvider<Binding> {
const { unstable_getMiniflareWorkerOptions } = await import(wrangler);

console.log(path.join(this.workspaceRoot!, "wrangler.toml"));
// TODO: multiroot workspaces
const options = await unstable_getMiniflareWorkerOptions(
path.join(this.workspaceRoot!, "wrangler.toml")
);
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
"blake3-wasm": "^2.1.5",
"chokidar": "^3.5.3",
"cloudflare": "^3.5.0",
"esbuild": "0.17.19",
"miniflare": "workspace:*",
"nanoid": "^3.3.3",
Expand Down
19 changes: 18 additions & 1 deletion packages/wrangler/src/cfetch/internal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from "node:assert";
import { fetch, File, FormData, Headers, Response } from "undici";
import Cloudflare from "cloudflare";
import { fetch, File, FormData, Headers, Request, Response } from "undici";
import { version as wranglerVersion } from "../../package.json";
import { getCloudflareApiBaseUrl } from "../environment-variables/misc-variables";
import { UserError } from "../errors";
Expand Down Expand Up @@ -117,6 +118,22 @@ export async function fetchInternal<ResponseType>(
}
}

export function getSdk() {
return new Cloudflare({
async fetch(info, init) {
const request = new Request(info, init);
const url = new URL(request.url);

return performApiFetch(
url.pathname.split("/client/v4")[1],
{ ...request },
url.searchParams,
request.signal
);
},
});
}

function truncate(text: string, maxLength: number): string {
const { length } = text;
if (length <= maxLength) {
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ const generateASSETSBinding: (
// eslint-disable-next-line @typescript-eslint/no-var-requires
require("./miniflare-cli/assets").default;
export { generateASSETSBinding as unstable_generateASSETSBinding };

export { getSdk } from "./cfetch/internal";

export { parseTOML } from "./parse";
Loading

0 comments on commit 9e8960d

Please sign in to comment.