utils.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const { spawnSync } = require('child_process');
  2. function fromCodePoint(codepoint) {
  3. var code = typeof codepoint === 'string' ?
  4. parseInt(codepoint, 16) : codepoint;
  5. if (code < 0x10000) {
  6. return String.fromCharCode(code);
  7. }
  8. code -= 0x10000;
  9. return String.fromCharCode(
  10. 0xD800 + (code >> 10),
  11. 0xDC00 + (code & 0x3FF)
  12. );
  13. }
  14. module.exports.fromCodePoint = fromCodePoint;
  15. function toJSON(codePoints) {
  16. return codePoints.split('-').map(function (point) {
  17. return UTF162JSON(fromCodePoint(point));
  18. }).join('');
  19. }
  20. module.exports.toJSON = toJSON;
  21. function UTF162JSON(text) {
  22. for (var i = 0, r = []; i < text.length; i++) {
  23. r.push('\\u' + ('000' + text.charCodeAt(i).toString(16)).slice(-4));
  24. }
  25. return r.join('');
  26. }
  27. module.exports.UTF162JSON = UTF162JSON;
  28. function getIntegrityHash(filename) {
  29. const algorithm = 'sha384';
  30. const digest = spawnSync('openssl', ['dgst', `-${algorithm}`, '-binary', filename]);
  31. if (digest.status || digest.signal){
  32. throw new Error(digest.stderr.toString('utf8'));
  33. }
  34. return `${algorithm}-${digest.stdout.toString('base64')}`;
  35. }
  36. module.exports.getIntegrityHash = getIntegrityHash;