How To Require Lua Socket? - lua

I'm very new to lua development with file manipulation, and now trying to import the lua socket package into my project according to this post, but I can't run even the code below.
I guess the error message indicates I need to import not only the socket.lua but also .\socket\core (probably .dll, since it doesn't have core.lua), while a reply at the post suggested importing only the file.
I'm stuck in just the beginning... What do I have to do for the next step?
local function main()
local socket = require("socket")
end
main()
Exception in thread "main" com.naef.jnlua.LuaRuntimeException: ...n32.win32.x86_64\workspace\TestForCiv\src\socket.lua:13: module 'socket.core' not found:
no field package.preload['socket.core']
no file '.\socket\core.lua'
no file 'C:\Program Files\Java\jre1.8.0_151\bin\lua\socket\core.lua'
no file 'C:\Program Files\Java\jre1.8.0_151\bin\lua\socket\core\init.lua'
...(a bunch of no file errors continues)
Edit: I added the folder structure. Even I add the .dll file it returns the same error.

I don't know the details of you configuration, but try this
require ("src.socket")
you should require a module from the root path of the lib

Related

OpenResty: not found 'socket' module

I install lua socket module by run the following command:
apt install lua-socket
When use local socket = require('socket') in OpenResty config, I got the following error:
module 'socket' not found:
no field package.preload['socket']
no file '/usr/local/openresty/site/lualib/socket.ljbc'
no file '/usr/local/openresty/site/lualib/socket/init.ljbc'
no file '/usr/local/openresty/lualib/socket.ljbc'
no file '/usr/local/openresty/lualib/socket/init.ljbc'
no file '/usr/local/openresty/site/lualib/socket.lua'
no file '/usr/local/openresty/site/lualib/socket/init.lua'
no file '/usr/local/openresty/lualib/socket.lua'
no file '/usr/local/openresty/lualib/socket/init.lua'
no file './socket.lua'
no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/socket.lua'
no file '/usr/local/share/lua/5.1/socket.lua'
no file '/usr/local/share/lua/5.1/socket/init.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/socket.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/socket/init.lua'
no file '/usr/local/openresty/site/lualib/socket.so'
no file '/usr/local/openresty/lualib/socket.so'
no file './socket.so'
no file '/usr/local/lib/lua/5.1/socket.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/socket.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
OpenResty Version: 1.19.3.1
How can use socket module in OpenResty?
I think https://github.com/thibaultCha/lua-resty-socket should be used in openresty ecosystem.

Why lua require won't search current directory?

say I've two files test.lua and m.lua in a folder, in test.lua as:
require("m")
then I run this file, howerver it raise an error:
lua: /Users/xx/works/scripts/test.lua:43: module 'm' not found:
no field package.preload['m']
no file '/usr/local/share/lua/5.3/m.lua'
no file '/usr/local/share/lua/5.3/m/init.lua'
no file '/usr/local/lib/lua/5.3/m.lua'
no file '/usr/local/lib/lua/5.3/m/init.lua'
no file './m.lua'
no file './m/init.lua'
no file '/usr/local/lib/lua/5.3/m.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './m.so'
As you can see the line no file './m.lua' appears but this is not true. ./m.lua exists, and file permission stuff is OK. If I hardcode the path:
package.path = package.path..';'..'/Users/xx/works/scripts/?.lua'
require('m')
It will work as expected.
What should I do to make lua search current directory fisrt (like python's import) when require a module
The current directory is the directory where you launch lua from.
The command line is missing in your example, if you used lua test.lua then it shoud work, if you used lua works/scripts/test.lua then it will not work.

Docker, ENOENT: no such file or directory

I have a Storage constant that is used in a file called listingController.js
const storage = Storage({
keyFilename: "../key/keyname.json"
});
Everything works fine when I'm not using Docker but after I create a Docker image and deploy it on server I get the following error:
ENOENT: no such file or directory, open '/key/keyname.json'
at wrapError (/app/node_modules/gcs-resumable-upload/build/src/index.js:17:12)
at /app/node_modules/gcs-resumable-upload/build/src/index.js:235:19
at getToken (/app/node_modules/google-auto-auth/index.js:27:9)
at getAuthClient (/app/node_modules/google-auto-auth/index.js:233:9)
at <anonymous>
Here I see a problem that the '..' is ignored in front of the path which is why I think that file is not found.
Here is my project structure:
src
--- key
----- keyname.json
----- firebasekeyfilename.json
--- controller
----- listingController.js
----- firebaseController.js
I have tried all different combinations of file names and paths but I cannot get it to find that file.
Does anyone have a clue why this is happening?
In my firebaseController I have the following reference to a similar file in the same folder and it works fine.
var serviceAccount = require("../key/firebasekeyfilename");
The only difference is that the path is inside require() and I guess that requires a different path.
Been stuck with this for a couple of days now, any pointers would be appreciated, thank!

Yeoman - How to extract zipped files in generator?

I want to build a Yeoman generator that needs to unzip a file.
From their documentation, it seems this process is done using this.registerTransformStream(...). It says it accept any gulp plugin, so I tried gulp-unzip (link)
Here's my code:
// index.js
...
writing: function() {
var source = this.templatePath('zip'); // the folder where the zipped file is
var destination = this.destinationRoot();
this.fs.copy(source, destination);
this.registerTransformStream(unzip() );
}
...
The result seems promising, first it shows all the file list then I get Error: write after end error.
Here's the dump:
create license.txt
create readme.html
create config.php
...
...
events.js:141
throw er; // Unhandled 'error' event
^
Error: write after end
at writeAfterEnd (C:\Users\myname\Documents\project\generator-test\node_modules\gulp-unzip\node_modules\readable-stream\lib\_stream_writable.js:144:12)
at Transform.Writable.write (C:\Users\myname\Documents\project\generator-test\node_modules\gulp-unzip\node_modules\readable-stream\lib\_stream_writable.js:192:5)
at DestroyableTransform.ondata (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:531:20)
at emitOne (events.js:77:13)
at DestroyableTransform.emit (events.js:169:7)
at readableAddChunk (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:198:18)
at DestroyableTransform.Readable.push (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:157:10)
at DestroyableTransform.Transform.push (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:123:32)
at DestroyableTransform._transform (C:\Users\myname\Documents\project\generator-test\node_modules\mem-fs-editor\lib\actions\commit.js:34:12)
at DestroyableTransform.Transform._read (C:\Users\myname\Documents\project\generator-test\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:159:10)
The destination folder is empty after this. It seems the stream is trying to write the unzipped file but failed.
Does anyone solved this problem before? Or is there alternative way by just using the built-in fs?
Thanks a lot

Using tdbloader.bat to upload a RDF file and querying using tdbquery.bat

I want to upload an RDF file using tdbloader.bat (C:\apache-jena-2.10.0\bat, in my case).
My turtle file is located in C:\apache-jena-2.10.0, and is named test.ttl. What will be the command line to load the turtle file using tdbloader.bat? After it is loaded, what would be a command line to query with tdbquery.bat?
I have used the following command-
tdbloader.bat -loc c:\apache-jena-2.10.0 test.ttl
But it returned a FileNotFound Exception.
The file test.ttl isnt in the same directory as bat files which is causing FileNotFound exception. A correction would be to copy test.ttl to C:\apache-jena-2.10.0\bat. After doing that execute command
tdbloader.bat -loc potato test.ttl.
This would load test.ttl to a folder named potato in same directory as tdbloader.bat. Hope it helps

Resources