Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 1x 1x 1x | // Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as net from 'net'
import * as fs from 'fs'
import RPCServer from './rpc'
import Runner from '../runner'
const HEADER_LEN = 4
const sockAddr = process.env.APISIX_LISTEN_ADDRESS.replace(/^unix:/, '')
const runner = new Runner()
const rpcServer = new RPCServer(runner)
const args = process.argv.slice(2)
console.log(`JavaScript Plugin Runner Listening on ${sockAddr}`)
args.forEach((path) => {
try {
const plugin = new (require(path) as any)()
runner.registerPlugin(plugin)
} catch (e) {
console.error(e)
}
})
let connCount = -1
const server = net.createServer((conn) => {
console.log(`Client connected`)
let receivedBytes = 0
let dataLength: number = null
let ty: number = null
let buf = Buffer.alloc(0)
let done = false
connCount++
let connId = connCount
conn.on('data', async (d: Buffer) => {
console.debug(`Conn#${connId}: receive data: ${d.length} bytes`)
if (done) {
// new data package received, reinit
receivedBytes = 0
dataLength = null
ty = null
buf = Buffer.alloc(0)
done = false
}
Eif (dataLength === null) {
buf = Buffer.concat([buf, d])
} else {
d.copy(buf, receivedBytes)
}
receivedBytes += d.length
Eif (dataLength === null) {
Eif (receivedBytes >= HEADER_LEN) {
ty = buf[0]
dataLength = Buffer.from([0, buf[1], buf[2], buf[3]]).readInt32BE()
const new_buf = Buffer.alloc(dataLength)
buf.copy(new_buf, 0, HEADER_LEN)
buf = new_buf
console.debug(`Conn#${connId} rpc header: `, {ty, dataLength})
}
}
Eif (dataLength !== null && receivedBytes >= HEADER_LEN + dataLength) {
done = true
const bytes = await rpcServer.dispatch(ty, buf)
const respSize = bytes.length
const header = Buffer.alloc(HEADER_LEN)
header.writeUInt32BE(respSize, 0)
header[0] = ty
conn.write(header)
conn.write(bytes, (err) => {
console.error(err)
})
}
})
conn.on('close', () => {
console.debug(`Connection closed`)
})
conn.on('error', (err) => {
console.error(err)
})
})
server.listen(sockAddr, () => {
fs.chmodSync(sockAddr, 0o777)
})
process.on('beforeExit', () => {
// clean up sock file
server.close()
});
|