0

I understand eval string-to-function is impossible to use on the browsers' application programming interfaces, but there must be another strategy to use third party dependencies without node.js on v8 engine, given Cloudflare does it in-house, unless they disable the exclusive method by necessity or otherwise on their edge servers for Workers. I imagine I could gather the AST of the commonjs module, as I was able to by rollup watch, but what might the actual steps be, by tooling? I mention AMD for it seems to rely on string-to-function (to-which I've notice Mozilla MDN says nothing much about it).

I have been exploring the require.js repositories, and they either use eval or AST

function DEFNODE(type, props, methods, base) {
    if (arguments.length < 4) base = AST_Node;
    if (!props) props = [];
    else props = props.split(/\s+/);
    var self_props = props;
    if (base && base.PROPS) props = props.concat(base.PROPS);
    var code = "return function AST_" + type + "(props){ if (props) { ";
    for (var i = props.length; --i >= 0; ) {
      code += "this." + props[i] + " = props." + props[i] + ";";
    }
    var proto = base && new base();
    if ((proto && proto.initialize) || (methods && methods.initialize))
      code += "this.initialize();";
    code += "}}";
    //constructor
    var cnstor = new Function(code)();
    if (proto) {
      cnstor.prototype = proto;
      cnstor.BASE = base;
    }
    if (base) base.SUBCLASSES.push(cnstor);
    cnstor.prototype.CTOR = cnstor;
    cnstor.PROPS = props || null;
    cnstor.SELF_PROPS = self_props;
    cnstor.SUBCLASSES = [];
    if (type) {
      cnstor.prototype.TYPE = cnstor.TYPE = type;
    }
    if (methods)
      for (i in methods)
        if (HOP(methods, i)) {
          if (/^\$/.test(i)) {
            cnstor[i.substr(1)] = methods[i];
          } else {
            cnstor.prototype[i] = methods[i];
          }
        }
    //a function that returns an object with [name]:method
    cnstor.DEFMETHOD = function (name, method) {
      this.prototype[name] = method;
    };
    if (typeof exports !== "undefined") exports[`AST_${type}`] = cnstor;

    return cnstor;
  }

  var AST_Token = DEFNODE(
    "Token",
    "type value line col pos endline endcol endpos nlb comments_before file raw",
    {},
    null
  );

https://codesandbox.io/s/infallible-darwin-8jcl2k?file=/src/mastercard-backbank/uglify/index.js

https://www.youtube.com/watch?v=EF7UW9HxOe4 HTML5/JavaScript with Maven

  • I don't understand the question. Are you asking how to load third-party libraries in a CloudFlare worker? A quick web search finds https://community.cloudflare.com/t/how-to-import-third-party-library-files-in-the-worker/16009, does that help? – jmrk May 16 '22 at 11:10
  • I also don't understand the question. I've read it a few times and I really have no idea what you're asking about, @NickCarducci – Kenton Varda May 16 '22 at 16:17
  • Can anonymous module definitions use AST to bypass the use of third party transpilers? How are commonjs modules exported, after compiling? Is there an AST that is consumable, or is that only with rollup watch that I was able to view with v8 controlled by a Cloudflare Worker. Does Webpack (1) use `eval`, or (2) do they manage to transpile with AST, under the hood, and I should try to use require.js and r.js source code. I forget why I started looking (edited in question) vs. webpack, yet I am here asking for I’ve found this page "uglify" in https://github.com/requirejs/r.js/tree/master/dist – Nick Carducci for Carface Bank May 16 '22 at 16:37
  • I'm still lost. FWIW, all "transipiling" performed by Cloudflare Workers happens in the command-line tooling *before* anything is uploaded to Cloudflare. It's accomplished by shelling out to standard tools like rollup, nothing Cloudflare-specific. Whereas Cloudflare's use of a custom V8 runtime is when actually executing the code. So I don't understand what you're getting at about transpiling having something to do with the custom V8 runtime. – Kenton Varda May 16 '22 at 17:17
  • I think I am lost because of this blog post https://blog.cloudflare.com/node-js-support-cloudflare-workers/ I certainly encountered issues in this version of my Durable Object Worker https://github.com/NickCarducci/mastercard-backbank/tree/a12bb23007f1917a3786197b6fd779c044320df7/src `import * as locs from "mastercard-locations"; import * as places from "mastercard-places";` basing off of the example https://github.com/cloudflare/durable-objects-rollup-esm/issues/13 I guess I'd rather use package.json for dependencies like these of interest to me instead of hit that built limit, probably. – Nick Carducci for Carface Bank May 16 '22 at 18:00
  • NodeResolve still uses require – Nick Carducci for Carface Bank May 16 '22 at 21:07
  • This shouldn't be a different question if the answer can be to write a java program. – Nick Carducci for Carface Bank May 16 '22 at 22:17

1 Answers1

0

Is it possible to make a C++ addon just to add a default object for node.js named exports or am I Y’ing up the wrong X

'.so' shared library for C++ dlopen/LoadLibrary (or #include?)

“I have to say that I'm amazed that there is code out there that loads one native addon from another native addon! Is it done by acquiring and then calling an instance of the require() function, or perhaps by using uv_dlopen() directly?”

N-API: An api for embedding Node in applications

"[there is no ]napi_env[ just yet]."

node-api: allow retrieval of add-on file name - Missing module in Init

Andreas Rossberg - is AST parsing, or initialize node.js abstraction for native c++, enough?

v8::String::NewFromUtf8(isolate, "Index from C++!");

Rising Stack - Node Source

"a macro implicit" parameter - bridge object between
C++ and JavaScript runtimes 
extract a function's parameters and set the return value.
#include <nan.h>

int build () {
  NAN_METHOD(Index) {
    info.GetReturnValue().Set(
      Nan::New("Index from C++!").ToLocalChecked()
    );
  }
}

  // Module initialization logic
NAN_MODULE_INIT(Initialize) {
  /*Export the `Index` function
    (equivalent to `export function Index (...)` in JS)*/
  NAN_EXPORT(target, Index);
}

New module "App" Initialize function from NAN_MODULE_INIT (an atomic?-macro)

"__napi_something doesn't exist."

"node-addon-API module for C++ code (N-API's C code's headers)"

NODE_MODULE(App, Initialize);

Sep 17, 2013, 4:42:17 AM to v8-u...@googlegroups.com "This comes up frequently, but the answer remains the same: scrap the idea. ;) Neither the V8 parser nor its AST are designed for external interfacing. In particular (1) V8's AST does not necessarily reflect JavaScript syntax 1-to-1, (2) we change it all the time, and (3) it depends on various V8 internals. And since all these points are important for V8, don't expect the situation to change.

/Andreas"

V8 c++: How to import module via code to script context (5/28/22, edit)

"The export keyword may only be used in a module interface unit. The keyword is attached to a declaration of an entity, and causes that declaration (and sometimes the definition) to become visible to module importers[ - except for] the export keyword in the module-declaration, which is just a re-use of the keyword (and does not actually “export” ...entities)."

SyntheticModule::virtual

ScriptCompiler::CompileModule() - "Corresponds to the ParseModule abstract operation in the ECMAScript specification."

Local<Function> foo_func = ...;//external
Local<Module> module = Module::CreateSyntheticModule(
    isolate, name,
    {String::NewFromUtf8(isolate, "foo")},
    [](Local<Context> context, Local<Module> module) {
      module->SetSyntheticModuleExport(
        String::NewFromUtf8(isolate, "foo"), foo_func
      );
    });

Context-Aware addons from node.js' commonjs modules

export module index;
export class Index {
  public:
    const char* app() {              
      return "done!";
    }
};
import index;
import <iostream>;

int main() {
    std::cout << Index().app() << '\n';
}

node-addon-api (new)

native abstractions (old)

"Thanks to the crazy changes in V8 (and some in Node core), keeping native addons compiling happily across versions, particularly 0.10 to 0.12 to 4.0, is a minor nightmare. The goal of this project is to store all logic necessary to develop native Node.js addons without having to inspect NODE_MODULE_VERSION and get yourself into a macro-tangle[ macro = extern atomics?]."

Scope Isolate (v8::Isolate), variable Local (v8::Local)

typed_array_to_native.cc

"require is part of the Asynchronous Module Definition AMD API[, without "string-to-function" eval/new Function()]," node.js makes objects, for it is written in C++.

"According to the algorithm, before finding ./node_modules/_/index.js, it tried looking for express in the core Node.js modules. This didn’t exist, so it looked in node_modules, and found a directory called _. (If there was a ./node_modules/_.js, it would load that directly.) It then loaded ./node_modules/_/package.json, and looked for an exports field, but this didn’t exist. It also looked for a main field, but this didn’t exist either. It then fell back to index.js, which it found. ...require() looks for node_modules in all of the parent directories of the caller."

But java?

I won't accept this answer until it works, but this looks promising:

https://developer.oracle.com/databases/nashorn-javascript-part1.html

If not to run a jar file or something, in the Worker:

https://github.com/nodyn/jvm-npm

require and build equivalent in maven, first, use "dist/index.js".

Specifically: [ScriptEngineManager][21]

https://stackoverflow.com/a/15787930/11711280

Actually: js.commonjs-require experimental

https://docs.oracle.com/en/graalvm/enterprise/21/docs/reference-manual/js/Modules/

Alternatively/favorably: commonjs builder in C (v8 and node.js)

https://www.reddit.com/r/java/comments/u7elf4/what_are_your_thoughts_on_java_isolates_on_graalvm/

Here I will explore v8/node.js src .h and .cc for this purpose

https://codesandbox.io/s/infallible-darwin-8jcl2k?file=/src/c.cpp

I'm curious why there is near machine-level C operability in Workers if not to use std::ifstream, and/or build-locally, without node.js require.

General Grievance
  • 4,555
  • 31
  • 31
  • 45