I have a File object and I want to pass it to PDF.js's getDocument method. On the frontend I would use the getDocument method like so:
pdfjs.getDocument(fileReader.readAsArrayBuffer(myFileHere))
How can I implement the above with Node.js? I am also using Express.js and Multer.
I have no idea whether PDF.js is able to run on Node, but let's assume it is.
At least with the readFile (or readFileSync if you prefer to be obtaining the contents "immediately") procedure available in Node, files may be read into Buffer objects, given the file's path.
If you take a look at the documentation of Buffer linked above, you'll also find specified that these are instances of UInt8Array:
The Buffer class is a subclass of JavaScript's Uint8Array class and extends it with methods that cover additional use cases.
Going further, an object of the UInt8Array class, also being an object of the TypedArray class (by the same specification that specifies the former), has a property named buffer, which lets you access the, well, underlying buffer1 that is... an ArrayBuffer!
So, you should be able to use a variation on the following, to pass an ArrayBuffer with some file's contents to PDF.js:
pdfjs.getDocument(require("fs").readFileSync("/path/to/file").buffer);
I have to assume you must read the file by path -- I assume your fileReader is a FileReader, which (contrary to its misleading name) reads Blob objects (and not files) -- and Node does not have any Blob class defined! So if you don't want to read the file by some path, then you'll have to tell us how you load file contents with Node at all.
1 Don't blame me for the word "buffer" all over here, it's buffers all the way down, apparently -- I didn't design any of these classes ;)
Related
I am trying to read a file content after executing a class GetContentsAPI, basically this class GetContentsAPI will write into the file /etc/api/token.
class Main{
require GetContentsAPI
file("/etc/api/token")
}
When I did the above steps, its says Evaluation Error: Error while evaluating a Function Call, Could not find any files from /etc/api. Not sure how to make sure the file is already created before trying to read.
Thanks James
The file() function reads the contents of a file during catalog building. You don't present any details of class GetContentsApi, but all of the standard puppet facilities that write to files (especially, but not limited to, File resources) write during catalog application. Unless you've cooked up something highly customized, the file() function will always read before GetContentsApi writes.
Moreover, in a master / agent setup (which is the only kind supported in current Puppet), catalog building happens on the master, whereas catalog application happens on the target node, which is usually a different machine, so you're unlikely even to be able to read what was written during a previous catalog-building run.
Also, file() just returns the file contents as a string, so it's not very useful to call it without using the return value somehow.
It's not at all clear what you're trying to achieve, but from what I can see, you are not going in a fruitful direction. Perhaps you should take a step back and ask a different question about that.
I would like to wrap a data file (~1MB) to golang app and then use that data in os.exec. The app runs on Linux.
How to define the data in the app, as a string or []byte, variable, or Const?
Should be defined in a global scope, or wrapped in a func?
How to pass the data from the app memory to the executed process ?
For building the data file(s) in to your program, you have a number of choices. You are correct in that you could manually copy/paste the data file(s) in to the program as types string, []byte as variables, but there are other packages/applications for go that handle this for you already that can also minimize your app's memory footprint. One example that comes to mind is go-bindata (link to repo) which does just this.
As for using the file in os/exec, I'm assuming you're trying to either pass the entire file to the program using either a file path or a raw string. For file paths, you would have to write the entire file to disk first. If you're trying to pass the entire file contents as a raw string, you can still use go-bindata or a string of the data file as arg in os/exec.Command.
P.S. - go-bindata has not seen updates in a while, so I would encourage you to seek more active forks or alternatives if you're having trouble using it.
I have a rather peculiar file format to work with:
Every line begins with the checksum of its content, followed by a new-line-character.
It looks like this:
[CHECKSUM OF LINE_1][LINE_1]\n
[CHECKSUM OF LINE_2][LINE_2]\n
[CHECKSUM OF LINE_3][LINE_3]\n
...
My goal: To allow any application to work with these files like they would work with any other text file - unaware of the additional checksums at the beginning of each line.
Since I work on a linux machine with debian wheezy (kernel 3.18.26) I want to use the LD_PRELOAD-mechanism to override the relevant file functions.
I have seen something like this with zlibc on https://zlibc.linux.lu/index.html - with an explanation of how it works ( https://zlibc.linux.lu/zlibc.html#SEC8 ).
But I dont get it. They only replace the file-opening functions. No read. No write. no fseek. Nothing. So how does it work?
Or - which functions would I have to intercept to handle every read or write operation on this file and handle them accordingly?
I didn't exactly check how it works but the reason seems to be quite simple.
Possible implementation:
zlibc open:
uncompress file you wanted to open to some temporary file
open this temporary file instead of yours
zlibc close:
Compress temporary file
Override original file
In this case you don't need to override read/write/etc because you can use original ones.
In your case you have two possible solutions:
open, that make a copy of your file with striped checksums. close that calculates checksums and override original file
read and write that are able to skip/calculate checksums.
Ad 2.
From What is the difference between read() and fread()?:
fread() is part of the C library, and provides buffered reads. It is
usually implemented by calling read() in order to fill its buffer
In this case I believe that overriding open and close will be less error prone because you can safely reuse original read, write, fread, fseek etc.
I have the following scenario:
I'm trying to have a user input a file in my language that I parse, and in that file he can include references to a file with methods in it, for now let's say it will be a .json file. So I have fs.readfile and in the callback I do JSON.parse. But the file the user uploaded uses those functions and before I run it I need the .json to be finished parsing and the functions added to my framework.
What's an elegant way to do this in node.js? I thought of using readfilesync or some kind of busy wait but there has to be a better way. I also thought about putting the call to the rest of the parsing process in the callback but there doesn't have to be a reference to a .json, it can use only the methods and what-not that I provide built into the language, and the reference can be anywhere in the file.
Suppose I have all of foo.js, foo.coffee, and foo.jsonin the same directory, and I say require './foo' from another (coffeescript) file in that location, what rule governs which one will be loaded?
A short experiment (using require.resolve './foo') would seem to indicate that the javascript file wins over the other two.
Indeed, looking at require.extensions it looks like .js being mentioned there as the first item—but then, object attribute names are inherently unordered in javascript, right?, so any name added to that property could potentially re-order the entries—could that lead to another resolution order?
Just wondering, as i couldn't find any documentation. it does become relevant when you do (and maybe you shouldn't) coffee --compile route/to/directory.
.js is loaded first (this also means that it's better to use full name.json for your fixture instead of name as it could be shadowed by name.js)
From "modules" documentation:
If the exact filename is not found, then node will attempt to load the required filename with the added extension of .js, .json, and then .node.
Also, read name resolution algorithm in pseudo-code:
LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text. STOP
2. If X.js is a file, load X.js as JavaScript text. STOP
3. If X.node is a file, load X.node as binary addon. STOP
After (1,2,3) extensions set in require.extensions are checked in the order they a set (for CoffeScript, require("coffe-script") installs .coffee handler).
The behavior in V8 is to iterate over named properties in the order they were originally assigned, so I would expect .js to always be first.
This post references that behavior