Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

thread.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const fs = require('fs');
  3. const crypto = require('crypto');
  4. const {parentPort} = require('worker_threads');
  5. const handlers = {
  6. hashFile: (algorithm, filePath) => new Promise((resolve, reject) => {
  7. const hasher = crypto.createHash(algorithm);
  8. fs.createReadStream(filePath)
  9. // TODO: Use `Stream.pipeline` when targeting Node.js 12.
  10. .on('error', reject)
  11. .pipe(hasher)
  12. .on('error', reject)
  13. .on('finish', () => {
  14. const {buffer} = new Uint8Array(hasher.read());
  15. resolve({value: buffer, transferList: [buffer]});
  16. });
  17. }),
  18. hash: async (algorithm, input) => {
  19. const hasher = crypto.createHash(algorithm);
  20. if (Array.isArray(input)) {
  21. for (const part of input) {
  22. hasher.update(part);
  23. }
  24. } else {
  25. hasher.update(input);
  26. }
  27. const {buffer} = new Uint8Array(hasher.digest());
  28. return {value: buffer, transferList: [buffer]};
  29. }
  30. };
  31. parentPort.on('message', async message => {
  32. try {
  33. const {method, args} = message;
  34. const handler = handlers[method];
  35. if (handler === undefined) {
  36. throw new Error(`Unknown method '${method}'`);
  37. }
  38. const {value, transferList} = await handler(...args);
  39. parentPort.postMessage({id: message.id, value}, transferList);
  40. } catch (error) {
  41. const newError = {message: error.message, stack: error.stack};
  42. for (const [key, value] of Object.entries(error)) {
  43. if (typeof value !== 'object') {
  44. newError[key] = value;
  45. }
  46. }
  47. parentPort.postMessage({id: message.id, error: newError});
  48. }
  49. });