diff --git a/.gitignore b/.gitignore index 5c36d50..e270423 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,19 @@ *.log node_modules + +build/ +build-pre-gyp/ +Release/ +deps/*/Debug/ +*.so +*.a +*.sln +*.vcxproj +*.vcxproj.filters +*.tlog +*.obj +*.1sdk.pdb +*.lastbuildstate +prebuilds/ +.nyc_output diff --git a/README b/README index 95f8feb..5c0f7cf 100644 --- a/README +++ b/README @@ -45,3 +45,26 @@ endpoint is used for this transaction. This could be extended to allow the user to update the title (and other meta data) at any time during the upload process and have the data sent to the server in the background after each update to the text fields. + + +### Sphinx + +* https://cmusphinx.github.io/wiki/tutorialpocketsphinx/#installation-on-unix-system +* https://github.com/cmusphinx/sphinxbase +* https://github.com/cmusphinx/pocketsphinx +* https://github.com/cesine/cmuclmtk-for-nodejs + + +* https://docs.bazel.build/versions/master/install-os-x.html +* https://angular.io/guide/bazel +* https://ij.bazel.build/docs/import-project.html + + +## Development + +```bash +npm install -g node-gyp +npm install +npm run rebuild +npm test +``` diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..c16e1c5 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,6 @@ +{ + "targets": [{ + "target_name": "nodejs_sphinx", + "sources": ["src/nodejs-sphinx.cc"] + }] +} diff --git a/lib/nodejs-sphinx.js b/lib/nodejs-sphinx.js new file mode 100644 index 0000000..321df84 --- /dev/null +++ b/lib/nodejs-sphinx.js @@ -0,0 +1,5 @@ +var addon = require('bindings')('nodejs_sphinx'); + +module.exports = { + hello: addon.hello, +}; diff --git a/package.json b/package.json index dfa7ff6..4c4d032 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,14 @@ "lib": "lib" }, "scripts": { + "install": "node-gyp-build", + "start": "node server.js", "test": "mocha --recursive test", - "start": "node server.js" + "test-prebuild": "cross-env PREBUILDS_ONLY=1 npm t", + "rebuild": "npm run install --build-from-source", + "prebuild": "prebuildify -t 8.14.0 --napi --strip" }, + "gypfile": true, "repository": { "type": "git", "url": "git+https://github.com/cesine/nodejs-sphinx.git" @@ -23,13 +28,23 @@ "url": "https://github.com/cesine/nodejs-sphinx/issues" }, "homepage": "https://github.com/cesine/nodejs-sphinx#readme", + "dependencies": { + "bindings": "^1.5.0", + "bunyan": "^1.8.12", + "formidable": "^1.2.1", + "napi-macros": "~2.0.0", + "node-gyp-build": "~4.1.0" + }, "devDependencies": { "chai": "^4.2.0", + "cross-env": "^6.0.0", "mocha": "^6.2.2", + "node-gyp": "^6.0.0", + "prebuildify": "^3.0.0", + "prebuildify-ci": "^1.0.4", "supertest": "^4.0.2" }, - "dependencies": { - "bunyan": "^1.8.12", - "formidable": "^1.2.1" + "engines": { + "node": ">=8.6.0" } } diff --git a/server.js b/server.js index 5d6a9c0..4e49946 100644 --- a/server.js +++ b/server.js @@ -7,7 +7,8 @@ var formidable = require('./lib/formidable'); var paperboy = require('./lib/paperboy'); var log = bunyan.createLogger({ - name: 'nodejs-sphinx' + name: 'nodejs-sphinx', + level: process.env.LOG_LEVEL || 'warn', }); var PUBLIC = path.join(path.dirname(__filename), 'public'); @@ -47,7 +48,7 @@ function handleError(err, req, res) { res.end(); } -function handleUpload(req, res) { +function handleUpload(req, res, next) { var uuid = match[1]; uuid = uuid.replace(/.mp3/, ""); uuid = uuid.replace(/.srt/, ""); @@ -81,7 +82,14 @@ function handleUpload(req, res) { //safeFilename=safeFilename.replace(/_client\./,"."); safeFilename = safeFilename.replace(/\.mp3/, ".amr"); var tempdir = "../nodejs-pocketsphinxtemp/"; - fs.mkdirSync(tempdir); + try { + fs.mkdirSync(tempdir); + } catch (err) { + if (err.code !== 'EEXIST') { + return next(err); + } + log.info(err, 'Error creating directory'); + } fs.renameSync(path, tempdir + safeFilename); safeFilenameServer = safeFilename.replace(/_client/, "_server"); @@ -223,13 +231,13 @@ function handleStatic(req, res) { }); } -var server = http.createServer(function(req, res) { +var server = http.createServer(function(req, res, next) { /*TODO check for API key and non banned install id in this regex */ regex = new RegExp('/upload/(.+)'); match = regex.exec(req.url); if (match && req.method.toLowerCase() == 'post') { try { - handleUpload(req, res); + handleUpload(req, res, next); } catch (err) { handleError(err, req, res); } diff --git a/src/nodejs-sphinx.cc b/src/nodejs-sphinx.cc new file mode 100644 index 0000000..09deedb --- /dev/null +++ b/src/nodejs-sphinx.cc @@ -0,0 +1,25 @@ +#define NAPI_VERSION 3 + +#include +#include + +napi_value Method(napi_env env, napi_callback_info info) { + napi_status status; + napi_value world; + status = napi_create_string_utf8(env, "world", 5, &world); + assert(status == napi_ok); + return world; +} + +#define DECLARE_NAPI_METHOD(name, func) \ + { name, 0, func, 0, 0, 0, napi_default, 0 } + +napi_value Init(napi_env env, napi_value exports) { + napi_status status; + napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method); + status = napi_define_properties(env, exports, 1, &desc); + assert(status == napi_ok); + return exports; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/test/nodejs-sphinx.js b/test/nodejs-sphinx.js new file mode 100644 index 0000000..f9201da --- /dev/null +++ b/test/nodejs-sphinx.js @@ -0,0 +1,9 @@ +var expect = require('chai').expect; + +var lib = require('../lib/nodejs-sphinx'); + +describe('/nodejs-sphinx', function() { + it('should load', function() { + expect(lib.hello()).to.equal('world'); + }); +});