@flue/sdk 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +22 -13
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -396,12 +396,10 @@ var CloudflarePlugin = class {
396
396
  }`;
397
397
  }).join("\n\n");
398
398
  const { config: userConfig } = await this.getUserConfig(ctx.outputDir);
399
- const sandboxClassNames = detectSandboxBindings(userConfig);
400
- const sandboxReExports = sandboxClassNames.map((name) => `export { Sandbox as ${name} } from '@cloudflare/sandbox';`).join("\n");
401
- const sandboxClassImport = sandboxClassNames.length > 0 ? `import { Sandbox as __FlueCfSandbox } from '@cloudflare/sandbox';` : "";
402
399
  return `
403
400
  // Auto-generated by @flue/sdk build (cloudflare)
404
401
  import { Agent, routeAgentRequest } from 'agents';
402
+ import { DurableObject } from 'cloudflare:workers';
405
403
  import { Bash, InMemoryFs } from 'just-bash';
406
404
  import {
407
405
  createFlueContext,
@@ -410,7 +408,7 @@ import {
410
408
  resolveModel,
411
409
  } from '@flue/sdk/internal';
412
410
  import { runWithCloudflareContext, cfSandboxToSessionEnv } from '@flue/sdk/cloudflare';
413
- ${sandboxClassImport ? "\n" + sandboxClassImport : ""}
411
+
414
412
  ${agentImports}
415
413
 
416
414
  // ─── Config ─────────────────────────────────────────────────────────────────
@@ -454,19 +452,30 @@ async function createLocalEnv() {
454
452
 
455
453
  /**
456
454
  * Detect and wrap external sandbox instances (e.g. from @cloudflare/sandbox's
457
- * getSandbox()). Returns SessionEnv if the value is a @cloudflare/sandbox
455
+ * getSandbox()). Returns SessionEnv if the value looks like a Durable Object
458
456
  * RPC stub, null otherwise.
459
457
  *
460
- * NOTE: We must use \`instanceof\` here, not structural duck-typing. The value
461
- * returned by \`getSandbox()\` is a workerd RPC Proxy that returns \`true\` for
462
- * any \`in\` check and \`'function'\` for \`typeof <anything>\`, so structural
463
- * checks (positive or negative) are unreliable against it. \`instanceof\` walks
464
- * the prototype chain via the runtime, which the proxy can't fake.
458
+ * NOTE on detection: The value returned by \`getSandbox()\` is a workerd RPC
459
+ * Proxy. \`in\` and \`typeof\` against it return \`true\`/\`'function'\` for any
460
+ * property name, so structural duck-typing is unreliable.
461
+ *
462
+ * \`instanceof <UserSandboxClass>\` ALSO does not work: the RPC stub's
463
+ * prototype chain is workerd's internal \`DurableObject\` runtime class, not
464
+ * the user-defined \`Sandbox\` class (the user's class only exists on the
465
+ * in-DO side; the caller side gets a generic stub). Empirically:
466
+ * typeof stub === 'object'
467
+ * Object.getPrototypeOf(stub).constructor.name === 'DurableObject'
468
+ *
469
+ * \`instanceof DurableObject\` (imported from \`cloudflare:workers\`) is the
470
+ * one signal that holds: it walks the prototype chain via the runtime and
471
+ * matches any DO RPC stub. We treat any DO stub passed to \`init({ sandbox })\`
472
+ * as intended for \`@cloudflare/sandbox\`, since that's the only documented
473
+ * use case for that argument shape on the Cloudflare target.
465
474
  */
466
475
  function resolveSandbox(sandbox) {
467
- ${sandboxClassNames.length > 0 ? `if (sandbox instanceof __FlueCfSandbox) {
476
+ if (sandbox instanceof DurableObject) {
468
477
  return cfSandboxToSessionEnv(sandbox);
469
- }` : "/* no @cloudflare/sandbox bindings declared in wrangler config */"}
478
+ }
470
479
  return null;
471
480
  }
472
481
 
@@ -696,7 +705,7 @@ ${agentClasses}
696
705
  // \`@cloudflare/sandbox\` so each user-chosen class_name resolves at the
697
706
  // bundle's top level. The binding + container image configuration is owned
698
707
  // by the user's wrangler.jsonc.
699
- ${sandboxReExports}
708
+ ${detectSandboxBindings(userConfig).map((name) => `export { Sandbox as ${name} } from '@cloudflare/sandbox';`).join("\n")}
700
709
 
701
710
  // ─── Worker Fetch Handler ───────────────────────────────────────────────────
702
711
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flue/sdk",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "exports": {