How to run npm 'filestream' example code?

casamia

I'm planning to use file stream api in the browser. By searching, I found this filestream module.

https://github.com/DamonOehlman/filestream

The author wrote a example usage code. Here it is. (File name : drag-n-drop.js)

var crel = require('crel');
var detect = require('feature/detect');
var dnd = require('drag-and-drop-files');
var img = crel('img');
var video = crel('video', { autoplay: true });
var FileReadStream = require('filestream/read');
var FileWriteStream = require('filestream/write');

function upload(files) {
  var queue = [].concat(files);

  function sendNext() {
    var writer = new FileWriteStream();
    var next = queue.shift();

    console.log('sending file');
    new FileReadStream(next).pipe(writer).on('file', function(file) {
      console.log('file created: ', file);
      img.src = detect('URL').createObjectURL(file);
      // video.src = detect('URL').createObjectURL(next);

      if (queue.length > 0) {
        sendNext();
      }
    });
  }

  sendNext();
}

dnd(document.body, upload);

document.body.appendChild(crel('style', 'body, html { margin: 0; width: 100%; height: 100% }'));
document.body.appendChild(img);
document.body.appendChild(video);
    1.

In this code... I'm totally frustrated. Which side this code works for? Server side? Or client side code?

If server side code, where is the create-server methods and how document.body.~ codes are read?

If client side code, how to use 'require' method in the browser?

Most of all, is this runnable code?

    2.

Putting aside the previous questions, I tried to run this code. To do that, I installed 'crel', 'feature', 'drag-and-drop-files' modules, and give command : $node drag-n-drop.js

But, it doesn't works, and the error message is like this. This also frustrate me...

[appPath]/node_modules/crel/crel.js:91
        element = crel[isElementString](element) ? element : d.createElement(e
                                                               ^
TypeError: undefined is not a function
    at crel ([appPath]/node_modules/crel/crel.js:91:64)
    at Object.<anonymous> ([appPath]/node_modules/filestream/examples/drag-n-drop.js:4:11)

Help!

Update. 3.

Hey. Can I ask you one more question? I'm adapting the filestream module to my code, referencing with the above example code. While doing, I got stuck in the detect('URL') code. I read the 'feature' module in the npm page and read the description carefully, but I still can't understand that. Refer to this page link , I don't know why author use detect('URL'), rather than window.URL. Can you explain it? I really appreciate with you.

Chen-Tsu Lin

Question 1:

document.body usually is a window object's property, so the example is for client side. Or you can use some module, for example: jsdom. Then you can use window in node.

require is a function in CommonJS module specifications. you can use browserify or webpack to compile it for client side.

Question 2:

As above, you should use CommonJS module build tool or use jsdom for server side.

createElement is a method on window.document.

Update:

Question 3:

require('feature/detect');

will require detect.js in feature npm module

As you can see, it tests ms, o, moz, webkit prefixs with target feature on window.

In the bottom of below link, there is a Browser compatibility table.

https://developer.mozilla.org/en-US/docs/Web/API/Window/URL

In Chrome 8.0, Opera 15.0 and Safari 6.0 URL exists as webkitURL.

This is why author do that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to run vulkan example code in windows by mingw-w64?

How to run Node JS code from npm inside of Swift

How do I run an npm script directly in VS code terminal

javascript example code cannot run

How to run `npm run build`

How to publish a code with example

How example can be run in test?

How to run BlocObserver example in cli

Failing to compile/run Rascal example code

Unable to run example code using Quantlib on Mac

npm run dev command does not work on vuejs' hackernews example

How do I successfully run JavaCV code example (JavaFxPlayVideoAndAudio.java)

Need code example on how to run an Android service forever in the background even when device sleeping, like Whatsapp?

How do I properly install the libshaderc component to run Vulkan example code in Android Studio?

Rot13 Example: How do I run this piece code in Google App Engine?

How To Have Code Run For A Real Time Update For Condition Met Without The Need Of An Event(Button For Example) Java

How to run an npm package locally?

How to run npm lib in lambda

How to run npm through ssh

how does npm run work?

npm run build throws error code ELIFECYCLE

Run npm command on keybinding in Visual Studio Code

How to run "npm start" run forever

npm run build gives out npm ERR! code ELIFECYCLE

Trying to run concurrently (npm ERR! code ELIFECYCLE npm ERR!)

How to run npm script in visual studio code launch.json file

basic explain for what does npm start do with my reactjs code and how to run it from browser

How can I write a VS Code Task to run my NPM script?

Receiving an npm error after running "npm run build" : npm ERR! code ELIFECYCLE npm ERR! errno 2

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive