Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodePolyfills needed to complete build, lest "Uncaught SyntaxError: Delete of an unqualified identifier in strict mode" #13

Closed
NickCarducci opened this issue Nov 1, 2021 · 1 comment

Comments

@NickCarducci
Copy link

NickCarducci commented Nov 1, 2021

I need nodePolyfills to make this build and publish, but the binding name namespace isn't in env, nor is it global (but would be unexpected for durable object as docs describe).

export default {
  async fetch(request, env/*, ctx*/) { 
return new Response(JSON.Stringify(env));
//1. should I use context in conjunction? https://githubmemory.com/index.php/repo/cloudflare/workers-rs
    try {
      return await noException(request, env);
      // wrap the body of your callback in a try/catch block to ensure it cannot throw an exception.
      // is return, "the body?"
    } catch (e) {
      return new Response(e.message);
    }
  }
};
async function noException(req, env) {
  return new Response(JSON.stringify(env));
};

returns an empty object. 2. Is a KV namespace required? The docs have them as differently listed than Durable Object namespace identified in bindings = in wrangler.toml

Other than those two questions, 3. I am unsure if I should have [site] uncommented, and 4. if following error directions in having named environments (further down Github Actions list) is appropriate.

@NickCarducci
Copy link
Author

NickCarducci commented Nov 24, 2021

Edit: named exports is impossible to tree-climb as of now unless the cjs commonjs package is built that way, and nodePolyfills("module") for createRequire is ' not shimmed, just returns mock'. Now I will try to input rollup namedExports on my dependencies, and the src/shim.mjs → dist/shim.mjs... (!) Plugin rollup-plugin-cjs-es: 'node_modules/mastercard-locations/index.js' doesn't export names expected by 'src/browseri.js'

the durable_object binding needs to be repeated for the environment

[env.production]
name = "mastercard-backbank"

[env.staging]
name = "mastercard-backbank-staging"

[durable_objects]
bindings = [{ name = "EXAMPLE_CLASS", class_name = "DurableObjectExample" }]

[env.staging.durable_objects]
bindings = [{ name = "EXAMPLE_CLASS", class_name = "DurableObjectExample" }]

[env.production.durable_objects]
bindings = [{ name = "EXAMPLE_CLASS", class_name = "DurableObjectExample" }]

browserify "exporting" common modules (iife but standalone, uses define) ignores module.exports, relies on window and window isn't accessible by the webworker. NodePolyfills was required for bundling node packages, but they seemingly cannot be exported. Thinking hard about using the rollup sourcemaps after browserify makes its iife, and any other methods of exporting node dependencies for use in the durable object and its esm modules... webworkify seems to also be thrown upon its use of define, wrapping the durable object from another file...

self is read-only, but I am now trying browserify-api programmatically in the constructor of the Durable Object, where this is put-able

Is this nearly the very purpose of KV storage? Should I attempt to define the commonjs module.exports module to something on this in the Durable Object contructor? Potentially with fs.createWriteStream and the node-rollup plugin for fs... I'd bet, "not, but there is a way" to run node packages a-la-carte in the browser, since your team does it upon request, which we can all benefit from trying to avoid operating in such a way. Edit: Best move may be rollup api each dependency, although 'key'-value storage for AST seems promising-for/the-point-of-something-related-to common javascript handicap in the browser, no?

export class DurableObjectExample {
  constructor(el, env) {
    console.log(el.textContent, "- From the example module");
    this.el = el;
    this.env = env;
    this.el.blockConcurrencyWhile(async () => {
      let stored = await this.el.storage.get("value");
      // After initialization, future reads do not need to access storage.
      this.value = stored || 0;
    });
    
    /*"./browserii.js", {
      entry: "./browseri.js",
      output: "./browserii.js"
    } best guess https://stackoverflow.com/questions/34752771/how-can-i-get-browserifys-bundle-function-to-emit-an-end-event*/
    browserify()
      .add("./browseri.js")
      .bundle(function (err) {
        if (err) throw err;
      })
    /*(path,opts) => import(path).then((window) => 
    //const { locs, places, crs } = window.Browseri()
    this.modules = { ...window.Browseri() })*/
      .pipe(fs.createWriteStream("./browserii.js")); 
    
    import("./browserii.js").then((window) => {
      //const { locs, places, crs } = window.Browseri()
      this.modules = { ...window.Browseri() };
    });
  }
...
}

Edit: My example in comments uses rollup api - as opposed to browserify here - for AST capturing, and although 'key'-value storage may have a real use case in replacing the global namespace, maybe preventing redundant transpiling to (and from?) AST in builders, and seems to be foretold in such a way in other content, albeit is meshing together for me and am unsure if this is an original thought or obvious, now I'm trying to rollup each dependency instead, naming each export in the commonjs plugin, seemingly their legacy plugin has a similar agenda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant