gnitial import of @protobuf/inquire
Change-Id: I7e3ca3831f8e2ca4f877179dcbc22ff696dcf395
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..c3fc82e
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,3 @@
+npm-debug.*
+node_modules/
+coverage/
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..be2b397
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,26 @@
+Copyright (c) 2016, Daniel Wirtz All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+* Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+* Neither the name of its author, nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/METADATA b/METADATA
new file mode 100644
index 0000000..8acb1d5
--- /dev/null
+++ b/METADATA
@@ -0,0 +1,12 @@
+name: "@protobufjs/inquire"
+description:
+ "Import (require) a module only if the module is available and hides the require call from bundlers"
+third_party {
+ url {
+ type: GIT
+ value: "https://github.com/protobufjs/protobuf.js/tree/master/lib/inquire"
+ }
+ version: 56b1e64979dae757b67a21d326e16acee39f2267
+ last_upgrade_date { year: 2023 month: 03 day: 27 }
+ license_type: NOTICE
+}
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..3eabd86
--- /dev/null
+++ b/README.md
@@ -0,0 +1,13 @@
+@protobufjs/inquire
+===================
+[](https://www.npmjs.com/package/@protobufjs/inquire)
+
+Requires a module only if available and hides the require call from bundlers.
+
+API
+---
+
+* **inquire(moduleName: `string`): `?Object`**<br />
+ Requires a module only if available.
+
+**License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause)
diff --git a/index.d.ts b/index.d.ts
new file mode 100644
index 0000000..1f5a865
--- /dev/null
+++ b/index.d.ts
@@ -0,0 +1,9 @@
+export = inquire;
+
+/**
+ * Requires a module only if available.
+ * @memberof util
+ * @param {string} moduleName Module to require
+ * @returns {?Object} Required module if available and not empty, otherwise `null`
+ */
+declare function inquire(moduleName: string): Object;
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..bae28e7
--- /dev/null
+++ b/index.js
@@ -0,0 +1,40 @@
+"use strict";
+module.exports = inquire;
+
+/**
+ * Requires a module only if available.
+ * @memberof util
+ * @param {string} moduleName Module to require
+ * @returns {?Object} Required module if available and not empty, otherwise `null`
+ */
+function inquire(moduleName) {
+ try {
+ if (typeof self !== 'undefined' && self.trustedTypes && self.trustedTypes.createPolicy) {
+ const escapeScriptPolicy = trustedTypes.createPolicy("myEscapePolicy", {
+ createScript: (string) => "require " + string,
+ });
+ safeScript = escapeScriptPolicy.createScript(moduleName);
+ var mod = eval(safeScript);
+ } else {
+ var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
+ }
+ if (mod && (mod.length || Object.keys(mod).length))
+ return mod;
+ } catch (e) {} // eslint-disable-line no-empty
+ return null;
+}
+
+/*
+// maybe worth a shot to prevent renaming issues:
+// see: https://github.com/webpack/webpack/blob/master/lib/dependencies/CommonJsRequireDependencyParserPlugin.js
+// triggers on:
+// - expression require.cache
+// - expression require (???)
+// - call require
+// - call require:commonjs:item
+// - call require:commonjs:context
+
+Object.defineProperty(Function.prototype, "__self", { get: function() { return this; } });
+var r = require.__self;
+delete Function.prototype.__self;
+*/
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..1315279
--- /dev/null
+++ b/package.json
@@ -0,0 +1,17 @@
+{
+ "name": "@protobufjs/inquire",
+ "description": "Requires a module only if available and hides the require call from bundlers.",
+ "version": "1.1.0",
+ "author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/dcodeIO/protobuf.js.git"
+ },
+ "license": "BSD-3-Clause",
+ "main": "index.js",
+ "types": "index.d.ts",
+ "devDependencies": {
+ "istanbul": "^0.4.5",
+ "tape": "^4.6.3"
+ }
+}
diff --git a/tests/data/array.js b/tests/data/array.js
new file mode 100644
index 0000000..0847b28
--- /dev/null
+++ b/tests/data/array.js
@@ -0,0 +1 @@
+module.exports = [1];
diff --git a/tests/data/emptyArray.js b/tests/data/emptyArray.js
new file mode 100644
index 0000000..e0a30c5
--- /dev/null
+++ b/tests/data/emptyArray.js
@@ -0,0 +1 @@
+module.exports = [];
diff --git a/tests/data/emptyObject.js b/tests/data/emptyObject.js
new file mode 100644
index 0000000..f053ebf
--- /dev/null
+++ b/tests/data/emptyObject.js
@@ -0,0 +1 @@
+module.exports = {};
diff --git a/tests/data/object.js b/tests/data/object.js
new file mode 100644
index 0000000..3b75bca
--- /dev/null
+++ b/tests/data/object.js
@@ -0,0 +1 @@
+module.exports = { a: 1 };
diff --git a/tests/index.js b/tests/index.js
new file mode 100644
index 0000000..4a555ca
--- /dev/null
+++ b/tests/index.js
@@ -0,0 +1,20 @@
+var tape = require("tape");
+
+var inquire = require("..");
+
+tape.test("inquire", function(test) {
+
+ test.equal(inquire("buffer").Buffer, Buffer, "should be able to require \"buffer\"");
+
+ test.equal(inquire("%invalid"), null, "should not be able to require \"%invalid\"");
+
+ test.equal(inquire("./tests/data/emptyObject"), null, "should return null when requiring a module exporting an empty object");
+
+ test.equal(inquire("./tests/data/emptyArray"), null, "should return null when requiring a module exporting an empty array");
+
+ test.same(inquire("./tests/data/object"), { a: 1 }, "should return the object if a non-empty object");
+
+ test.same(inquire("./tests/data/array"), [ 1 ], "should return the module if a non-empty array");
+
+ test.end();
+});