xzwebx-httpfilter 1.1.0

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.
Binary file
@@ -0,0 +1,16 @@
1
+ const _module = require('module')
2
+ const path = require('path')
3
+ const makeRequire = require('./make-require')
4
+ const { loadBytecode } = require('./loader')
5
+
6
+ _module._extensions['.bytecode'] = function (module, filename) {
7
+ const script = loadBytecode(filename, false);
8
+ const wrapperFn = script.runInThisContext({
9
+ filename: filename,
10
+ displayErrors: true,
11
+ lineOffset: 0,
12
+ columnOffset: 0,
13
+ })
14
+ const require = makeRequire(module)
15
+ wrapperFn.bind(module.exports)(module.exports, require, module, filename, path.dirname(filename))
16
+ }
package/loader.js ADDED
@@ -0,0 +1,71 @@
1
+ const fs = require('fs')
2
+ const vm = require('vm')
3
+ const v8 = require('v8')
4
+ v8.setFlagsFromString('--no-flush-bytecode')
5
+
6
+ const HeaderOffsetMap = {
7
+ 'magic': 0,
8
+ 'version_hash': 4,
9
+ 'source_hash': 8,
10
+ 'flag_hash': 12
11
+ };
12
+
13
+ let _flag_buf
14
+
15
+ function getFlagBuf() {
16
+ if (!_flag_buf) {
17
+ const script = new vm.Script("")
18
+ _flag_buf = getHeader(script.createCachedData(), 'flag_hash')
19
+ }
20
+ return _flag_buf
21
+ }
22
+
23
+ function getHeader(buffer, type) {
24
+ const offset = HeaderOffsetMap[type]
25
+ return buffer.slice(offset, offset + 4)
26
+ }
27
+
28
+ function setHeader(buffer, type, vBuffer) {
29
+ vBuffer.copy(buffer, HeaderOffsetMap[type])
30
+ }
31
+
32
+ function buf2num(buf) {
33
+ let ret = 0
34
+ ret |= buf[3] << 24
35
+ ret |= buf[2] << 16
36
+ ret |= buf[1] << 8
37
+ ret |= buf[0]
38
+
39
+ return ret
40
+ }
41
+
42
+ function loadBytecode(filePath) {
43
+ const bytecode = fs.readFileSync(filePath, null)
44
+
45
+ setHeader(bytecode, 'flag_hash', getFlagBuf())
46
+
47
+ const sourceHash = buf2num(getHeader(bytecode, 'source_hash'))
48
+ const script = new vm.Script(' '.repeat(sourceHash), {
49
+ filename: filePath,
50
+ cachedData: bytecode,
51
+ lineOffset: 0,
52
+ displayErrors: true
53
+ });
54
+
55
+ if (script.cachedDataRejected) {
56
+ throw new Error('something is wrong')
57
+ }
58
+ return script
59
+ }
60
+
61
+ if (process.mainModule && process.mainModule.filename === __filename) {
62
+ const scirpt = loadBytecode(process.argv[2])
63
+ scirpt.runInThisContext({
64
+ filename: process.argv[2],
65
+ displayErrors: true,
66
+ lineOffset: 0,
67
+ columnOffset: 0,
68
+ });
69
+ }
70
+
71
+ module.exports.loadBytecode = loadBytecode
@@ -0,0 +1,32 @@
1
+ function validateString(value, name) {
2
+ if (typeof value !== 'string') {
3
+ throw new Error(`${name} is not string`)
4
+ }
5
+ }
6
+
7
+ function makeRequireFunction(mod) {
8
+ // see node.js lib/internal/modules/cjs/helpers.js
9
+ const Module = mod.constructor
10
+
11
+ const require = function require(path) {
12
+ return mod.require(path)
13
+ };
14
+
15
+ require.resolve = function resolve(request, options) {
16
+ validateString(request, 'request')
17
+ return Module._resolveFilename(request, mod, false, options)
18
+ }
19
+
20
+ require.resolve.paths = function paths(request) {
21
+ validateString(request, 'request')
22
+ return Module._resolveLookupPaths(request, mod)
23
+ };
24
+
25
+ require.main = process.mainModule
26
+ require.extensions = Module._extensions
27
+ require.cache = Module._cache
28
+
29
+ return require
30
+ }
31
+
32
+ module.exports = makeRequireFunction
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "xzwebx-httpfilter",
3
+ "version": "1.1.0",
4
+ "description": "",
5
+ "main": "xzwebx-httpfilter.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "xzwebx",
11
+ "http",
12
+ "filter"
13
+ ],
14
+ "author": "xzwebx",
15
+ "license": "ISC"
16
+ }
@@ -0,0 +1,16 @@
1
+ require('./hook-require')
2
+ const HttpFilter = require('./HttpFilter.bytecode')
3
+
4
+ const H = {}
5
+ H.Init = function(language, data1, data2, data3, data4, data5) {
6
+ HttpFilter.Init(language, data1, data2, data3, data4, data5)
7
+ }
8
+
9
+ H.SetRoutes = function (app, express, projectPath) {
10
+ HttpFilter.SetRoutes(app, express, projectPath)
11
+ }
12
+
13
+ H.CheckReq = function (req, res, next) {
14
+ HttpFilter.CheckReq(req, res, next)
15
+ }
16
+ module.exports = H