syntaxhighlighter - Building: loadReposFromCache(...).error is not a function - javascript

I am trying to use the plugin SyntaxHighlighter v4, but I cannot get the build process to work!
Following the instructions here, I get the following error:
$ ./node_modules/gulp/bin/gulp.js setup-project
[10:12:20] Requiring external module babel-register
[10:12:20] Using gulpfile C:\git\syntaxhighlighter\gulpfile.babel.js
[10:12:20] Starting 'setup-project:clone-repos'...
[10:12:20] 'setup-project:clone-repos' errored after 1.96 ms
[10:12:20] TypeError: loadReposFromCache(...).error is not a function
at loadRepos (C:/git/syntaxhighlighter/build/setup-project.js:39:48)
at Gulp.<anonymous> (C:/git/syntaxhighlighter/build/setup-project.js:48:5)
at module.exports (C:\git\syntaxhighlighter\node_modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (C:\git\syntaxhighlighter\node_modules\orchestrator\index.js:273:3)
at Gulp.Orchestrator._runStep (C:\git\syntaxhighlighter\node_modules\orchestrator\index.js:214:10)
at Gulp.Orchestrator.start (C:\git\syntaxhighlighter\node_modules\orchestrator\index.js:134:8)
at C:\git\syntaxhighlighter\node_modules\gulp\bin\gulp.js:129:20
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:606:11)
(node:2532) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: ENOENT: no such file or directory, open 'C:\git\syntaxhighlighter\.projects-cache.json'
It seems not to be importing the github repository files to the /repos/ directory. Can I do this manually somehow? Is there another way to get this to build so I can use it? Or even find the built files like in v3?
Here is the function that's failing in build/setup-project.js
gulp.task('setup-project:clone-repos', 'Clones all repositories from
SyntaxHighlighter GitHub organization', () =>
loadRepos()
.then(R.filter(repo => !fs.existsSync(pathToRepo(repo))))
.then(R.filter(repo => repo.name !== 'syntaxhighlighter'))
.then(R.map(R.curry(cloneRepo)))
.then(Promise.all)
);
Tracing backward we see:
const loadReposFromCache = () => fs.readFile.promise(REPOS_CACHE, 'utf8').then(JSON.parse);
const loadRepos = () => loadReposFromCache().error(loadReposFromGitHub).then(R.map(R.pick(['clone_url', 'name'])));
function loadReposFromGitHub() {
const request = require('request');
const opts = {
url: 'https://api.github.com/orgs/syntaxhighlighter/repos?per_page=300',
json: true,
headers: { 'User-Agent': 'node.js' },
};
return new Promise((resolve, reject) =>
request(opts, (err, response) => {
if (err) return reject(err);
const json = response.body;
fs.writeFile(REPOS_CACHE, JSON.stringify(json, null, 2));
resolve(json);
})
);
}

There are a couple of issues in the build code for that project.
For the specific issue here, the Songbird wrapper on Bluebird promises doesn't seem to match up any more - hence the ".error is not a function" (on songbird, but ok on bluebird).
So replace .error with .catch or replace require('songbird') with require('bluebird')
In either case, that's just the start of your build woes...
I've added this to the project's issue tracking anyway, but here's what I did to get it to buiid:
https://github.com/karljacuncha/syntaxhighlighter/commit/dc015fa299d4d249e8518664e205a838c55372cf

The build broke again (April 2021). I forked the project from karljacuncha's answer and changed the call for fs.writeFile to fs.writeFileSync
https://github.com/BartJolling/syntaxhighlighter/commit/7dbd08203cba8ef3be72cbe1abbfb3475be19ef4
I also included other fixes I found in the larger community and I fixed the usage of the -output parameter as well.

Related

Issues trying to check a couple of RSS feeds (from a JSON config file) in Node.JS (using rss-parser)

I'm currently trying to make a node.js script to read a couple of RSS feeds from a JSON file (config.json), check everyone and send new items to it's specific webhook for Discord.
The config file is the following:
[
{
"name": "r/aww",
"avatar": "https://example.com/avatar.jpg",
"rss_url": "https://www.reddit.com/r/aww/top/.rss",
"webhook_url": "https://discord.com/api/webhooks/..."
},
{
"name": "r/ImaginaryLandscapes",
"avatar": "https://example.com/avatar.jpg",
"rss_url": "https://www.reddit.com/r/ImaginaryLandscapes/top/.rss",
"webhook_url": "https://discord.com/api/webhooks/..."
}
]
And the code that i attempted to do, following sync examples from rss-parser npm package, is the following:
let Parser = require('rss-parser');
let parser = new Parser();
const config = require("./config.json");
config.forEach(element => {
parser.parseURL(element.rss_url, function(err, feed) {
console.log(feed);
feed.items.forEach(function(entry) {
console.log(entry.title + ':' + entry.link);
});
});
});
But trying to run it throws the following error:
undefined
/mnt/d/misc/feedchecker/script.js:28
feed.items.forEach(function(entry) {
^
TypeError: Cannot read property 'items' of undefined
at /mnt/d/misc/feedchecker/script.js:28:9
at Timeout.setTimeout [as _onTimeout] (/mnt/d/misc/feedchecker/node_modules/rss-parser/lib/utils.js:63:29)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
What I'm doing wrong on this code? Please note that i still didn't wrote the webhook sending part, I'm focused still on fetching the feeds first.
I suspect this specific error was caused because i was (without knowing) using Node.JS v10 (that is the version that is shipped by apt by default for WSL's Ubuntu).
Reinstalling Node.JS using NodeSource repository gives me the version 16 that is able to run the code without issues.

"multihash must be a buffer" error while trying to upload files to ipfs using ipfs.add()

I'm trying to spawn a ipfs node and upload a file to ipfs. But the ipfs.add() method always throws the error "multihash must be a buffer".
Googling suggests that the isBuffer check in node's multihash package is the problem but doesn't provide a solution.
But the same method works when I connect to ipfs node that is running locally using ipfs-http-client. This error occurs only when I try to spawn a node.
This is the code to upload the file
async uploadFile() {
const IPFS = require('ipfs')
const ipfs = await IPFS.create()
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => {
const buffer = Buffer.from(reader.result)
ipfs.add({content: buffer})
.then(files => {
resolve(files)
})
.catch(error => reject(error))
}
reader.readAsArrayBuffer(this.state.selectedFile)
})
}
This is the error thrown in the console
Uncaught (in promise) Error: multihash must be a Buffer
at Function.validateCID (0.chunk.js:58311)
at new CID (0.chunk.js:58143)
at new CID (0.chunk.js:58476)
at persist (0.chunk.js:157864)
at :3000/async http:/localhost:3000/static/js/0.chunk.js:156829
at async parallelBatch (0.chunk.js:183162)
at async buildFileBatch (0.chunk.js:156903)
at async batch (0.chunk.js:182292)
at async reduceToParents (0.chunk.js:156774)
at async balanced (0.chunk.js:156768)
at async all (0.chunk.js:182254)
at async fileBuilder (0.chunk.js:157010)
at async parallelBatch (0.chunk.js:183162)
at async treeBuilder (0.chunk.js:157803)
at async push../node_modules/ipfs-unixfs-importer/src/index.js.module.exports (0.chunk.js:157711)
at :3000/async http:/localhost:3000/static/js/0.chunk.js:119802
at :3000/async http:/localhost:3000/static/js/0.chunk.js:119828
at :3000/async http:/localhost:3000/static/js/0.chunk.js:119843
at async addAll (0.chunk.js:119787)
at async last (0.chunk.js:182560)
at async IPFS.add (0.chunk.js:120025)
I encountered the same error today on a project that had a dependency on an old version of the cids package.
Try running npm ls cids to see if you have a dependency on cids. If so, run npm install cids#latest to update and see if that fixes things.
Edit to add: I think it was the fact that cids depended on an old version of the multihashes package that was causing the issue. So if you don't have a dependency on cids, run npm ls and look for anything that's pulling in an old version of multihashes.
What version of js-ipfs are you using? That error message was changed to multihash must be a Uint8Array about seven months ago so there may be a more recent version available.

#Google-cloud/storage suddenly throwing weird exception after working for a long time

Context:
I have an API endpoint which serves .stl files stored in a public Google Cloud Storage bucket. Until sometime this week, it was working fine. I can step all the way through my code with the debugger. The NPM module in question is not even referenced in my project. I've tried using the exact code on Googles documentation for download and get the same exception: https://github.com/googleapis/nodejs-storage/blob/master/samples/downloadFile.js
What I've tried
npm rebuild #google-cloud/storage and different ways of using the same Google npm package.
Questions:
1.) Shouldn't the catch, catch the exception to prevent the crash?
2.) Anyone have any ideas on a workaround?
File:
https://storage.cloud.google.com/fancy_induction_ui/1inX1in.stl
Code:
getFile: async (req, res) => {
try {
const fileName = req.param('file');
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', 'attachment; filename=' + fileName + '');
const storage = new Storage();
let file = await storage.bucket('fancy_induction_ui').file(fileName).createReadStream();
file.pipe(res);
} catch (e) {
res.status(500).json({message: 'Something is wrong!', err: e.message});
}
}
Stacktrace:
path/to/code/node_modules/readable-stream/lib/_stream_writable.js:317
var isBuf = !state.objectMode && _isUint8Array(chunk);
^
TypeError: Cannot read property 'objectMode' of undefined
at DestroyableTransform.Writable.write (path/to/code/node_modules/readable-stream/lib/_stream_writable.js:317:22)
at PassThrough.ondata (_stream_readable.js:714:22)
at PassThrough.emit (events.js:321:20)
at PassThrough.EventEmitter.emit (domain.js:482:12)
at PassThrough.Readable.read (_stream_readable.js:512:10)
at flow (_stream_readable.js:989:34)
at resume_ (_stream_readable.js:970:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
In my opinion bug in different versions readable-stream in deep dependencies.
I have added
readable-stream: "3.6.0"
to my package.lock and that's fixed for me this issue.

Jest Call retries were exceeded

I have error in the following below test. My node version is : v12.10.0. is there any alternative of setTimeout?
test('demo code', async () => {
const cc = await projectSetup(project);
const onNotification = jest.fn();
cc.sendNotification();
await waitForExpect(() => {
expect(onNotification).toHaveBeenCalledTimes(2);
});
});
The Error log is as
Call retries were exceeded
at ChildProcessWorker.initialize (../../../node_modules/jest-worker/build/workers/ChildProcessWorker.js:230:21)
just add jest.useFakeTimers(); after your imports
...
jest.useFakeTimers();
test('demo code', async () => {
const cc = await projectSetup(project);
const onNotification = jest.fn();
cc.sendNotification();
await waitForExpect(() => {
expect(onNotification).toHaveBeenCalledTimes(2);
});
});
it works in my code
In my case, the actual problem was with the promise handling.
I got the same issue when I was running all my test cases in one go with the jest.
Solution:
Try running one test separately then see what error is coming.
I got the below error after running one problematic test separately where earlier I was getting the Call retries were exceeded:
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "TypeError: Cannot read property 'code' of undefined".] {
code: 'ERR_UNHANDLED_REJECTION'
}
With this, I was sure that the problem is with the catch block and when I added it in the async service API function then the test case worked perfectly fine.
Maybe you can also try the same and see if it works for you or not.
I am using the below config:
node: 15.13.0
npm: 7.8.0
jest: 26.6.3
Try running npm doctor using the latest npm version. It's a great tool and it helped me diagnose permission and ownership issues right away.
Takeaway:
Verify File/Folder Permissions & Ownership
Encountered same error when updating the vue-jest version to below listed versions
#vue/vue3-jest: ^27.0.0-alpha.4
#vue/cli-plugin-unit-jest: ~5.0.0,
node: v17.9.0 or v16.14.2
Error disappeared, once downgraded it to node version v14.x.x
Hunch is - the latest node versions are not compatible with the dependencies.
I was able to run the test's successfully doing the following;
Install npm i -D jest-canvas-mock
Update the jest.config.ts file to have:
export default {
...
testEnvironment: "jsdom",
setupFiles: ["jest-canvas-mock"],
}

gulp and babel with gulp-live-server

I'm trying to write an express server with ES6 and I'm using Babel to do the transpiling, but I'm having trouble getting it to work with gulp-live-server, as I can't make it restart properly when I change my files.
Currently I have the following:
// gulpfile.babel.js
import gulp from 'gulp';
import gls from 'gulp-live-server';
import babel from 'gulp-babel';
gulp.task('transpile', ['clean:server'], () => {
gulp.src(['server/**/*.js'])
.pipe(babel())
.pipe(gulp.dest('dist'));
});
gulp.task('server', ['transpile'], () => {
var server = gls.new('dist/app.js');
server.start();
gulp.watch(['server/**/*.js'], ['transpile']);
gulp.watch('dist/app.js', server.start.bind(server)); //error
});
But it's not working, this code returns a Gaze error:
internal/child_process.js:274
var err = this._handle.spawn(options);
^
TypeError: Bad argument
at TypeError (native)
at ChildProcess.spawn (internal/child_process.js:274:26)
at exports.spawn (child_process.js:339:9)
at Object.exports.start (/Users/oni/Documents/Projects/meanimo/node_modules/gulp-live-server/index.js:134:19)
at Gaze.<anonymous> (/Users/oni/Documents/Projects/meanimo/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/index.js:18:14)
at emitTwo (events.js:87:13)
at Gaze.emit (events.js:172:7)
at Gaze.emit (/Users/oni/Documents/Projects/meanimo/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:129:32)
at /Users/oni/Documents/Projects/meanimo/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:415:16
at StatWatcher._pollers.(anonymous function) (/Users/oni/Documents/Projects/meanimo/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:326:7)
The error has to do with the callback being passed to watch: server.start.bind(server), although that comes straight from the gls documentation...
I don't see my changes reflected and I can't seem to find any documentation on using gls with transpilers.
Please help.
OK I've fixed it and I'll share for posterity: for some reason, you have to wrap the server restart function on another function and execute it yourself. I think is has to do with the way Gaze is wrapping the subprocesses.
gulp.watch('dist/app.js', () => server.start());
That will do the trick.

Categories