lib/ruby_clean_css/exports.rb
# Defines both http & https modules
Defines both http & https modules
Defines both http & https modules
def define_http_in_cache(js_env)
        ctx = js_env.runtime
        http_exports_qname = js_env.attach_rb_functions_to_mod_cache('http', self::Http)
        https_exports_qname = js_env.define_cached_module('https')
        ctx.eval( <<~HTTP , filename: "#{__FILE__}/#{__method__}" )
          // same implementation for http & https
          #{https_exports_qname} = #{http_exports_qname};
          ((http) => {  // set a local shortcut to `http`'s exports
            'use strict';
            // Node's `get` can also accept the url as the 1st arg (but clean-css doesn't use that signature).
            http.get = (opts, cb) => {
              let errStr;
              let {body, statusCode, headers} = http._getHelper(opts);
              try {
                cb( new http._IncomingMessage(body, statusCode, headers) );
              } catch(err) {
                errStr = err.toString();
              }
              return new http._ClientRequest(errStr);
            };
            // These classes start with '_' so they're not accidentally used by other JS code.
            // This was called HttpGetResult in original ruby impl
            http._ClientRequest = class {
              constructor(errMsg) { this._errMsg = errMsg; }
              on(event, cb) {
                if (event === 'error' && this._errMsg) {
                  cb(new Error(this._errMsg));
                }
                return this;
              }
              setTimeout(timeoutMs, cb) { /* ignored */ }
            };
            // This was called ServerResponse in original ruby impl
            http._IncomingMessage = class {
              constructor(data, statusCode, headers) {
                this._data = data;
                this.statusCode = statusCode;
                this.headers = headers;
              }
              on(event, cb) {
                if (event === 'data') {
                  cb(this._data);
                } else {
                  cb();
                }
                return this;
              }
            };
          })( #{http_exports_qname} );
        HTTP
      end
def define_http_in_cache ( js_env ) ctx = js_env . runtime http_exports_qname = js_env . attach_rb_functions_to_mod_cache ( 'http' , self :: Http ) https_exports_qname = js_env . define_cached_module ( 'https' ) ctx . eval ( <<~HTTP , filename : "#{__FILE__}/#{__method__}" ) 
          // same implementation for http & https
           #{ https_exports_qname }  =  #{ http_exports_qname } ;

          ((http) => {  // set a local shortcut to `http`'s exports
            'use strict';
            // Node's `get` can also accept the url as the 1st arg (but clean-css doesn't use that signature).
            http.get = (opts, cb) => {
              let errStr;
              let {body, statusCode, headers} = http._getHelper(opts);
              try {
                cb( new http._IncomingMessage(body, statusCode, headers) );
              } catch(err) {
                errStr = err.toString();
              }
              return new http._ClientRequest(errStr);
            };

            // These classes start with '_' so they're not accidentally used by other JS code.

            // This was called HttpGetResult in original ruby impl
            http._ClientRequest = class {
              constructor(errMsg) { this._errMsg = errMsg; }
              on(event, cb) {
                if (event === 'error' && this._errMsg) {
                  cb(new Error(this._errMsg));
                }
                return this;
              }
              setTimeout(timeoutMs, cb) { /* ignored */ }
            };

            // This was called ServerResponse in original ruby impl
            http._IncomingMessage = class {
              constructor(data, statusCode, headers) {
                this._data = data;
                this.statusCode = statusCode;
                this.headers = headers;
              }
              on(event, cb) {
                if (event === 'data') {
                  cb(this._data);
                } else {
                  cb();
                }
                return this;
              }
            };
          })(  #{ http_exports_qname }  );
         HTTP end
Defines both http & https modules
Defines both http & https modules
# (Maybe move most of this JS to a '.js' file to get syntax highlighting)
js_env
<not_specific>