Skip to content

Commit

Permalink
#202 Windows compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
CroniD committed Nov 26, 2022
1 parent ae91c1e commit 9a279c8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
const fs = require('fs');
const EventEmitter = require('events');
const { pathToFileURL } = require('url');
const { Worker } = require('worker_threads');
const { join, resolve } = require('path');
const { debuglog } = require('util');
Expand Down Expand Up @@ -235,7 +236,13 @@ class Bree extends EventEmitter {
try {
const importPath = join(this.config.root, this.config.defaultRootIndex);
debug('importPath', importPath);
const obj = await import(importPath);
const importUrl = pathToFileURL(importPath).toString();
debug('importUrl', importUrl);
// hint: import statement expect a esm-url-string, not a file-path-string (https://github.com/breejs/bree/issues/202)
// otherwise the following error is expected:
// Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader.
// On Windows, absolute paths must be valid file:// URLs.
const obj = await import(importUrl);
if (typeof obj.default !== 'object') {
throw new ImportError(
`Root index file missing default export at: ${importPath}`
Expand Down
47 changes: 27 additions & 20 deletions test/job-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const later = require('@breejs/later');
const jobBuilder = require('../src/job-builder');

const root = path.join(__dirname, 'jobs');
const jobPathBasic = path.join(root, 'basic.js');

const baseConfig = {
root,
timeout: 0,
Expand All @@ -26,15 +28,20 @@ test(
job,
null,
{},
{ name: 'basic', path: `${root}/basic.js`, timeout: 0, interval: 0 }
{ name: 'basic', path: jobPathBasic, timeout: 0, interval: 0 }
);

test(
'job name as file name with extension',
job,
'basic.js',
{},
{ name: 'basic.js', path: `${root}/basic.js`, timeout: 0, interval: 0 }
{
name: 'basic.js',
path: jobPathBasic,
timeout: 0,
interval: 0
}
);

function basic() {
Expand Down Expand Up @@ -74,23 +81,23 @@ test(
job,
{ name: 'basic', path: '' },
{},
{ name: 'basic', path: `${root}/basic.js`, timeout: 0 }
{ name: 'basic', path: jobPathBasic, timeout: 0 }
);

test(
'job.path is blank and name of job is defined with extension',
job,
{ name: 'basic.js', path: '' },
{},
{ name: 'basic.js', path: `${root}/basic.js`, timeout: 0 }
{ name: 'basic.js', path: jobPathBasic, timeout: 0 }
);

test(
'job.path is path to file',
job,
{ path: `${root}/basic.js` },
{ path: jobPathBasic },
{},
{ path: `${root}/basic.js`, timeout: 0 }
{ path: jobPathBasic, timeout: 0 }
);

test(
Expand All @@ -104,26 +111,26 @@ test(
test(
'job.timeout is value',
job,
{ path: `${root}/basic.js`, timeout: 10 },
{ path: jobPathBasic, timeout: 10 },
{},
{ path: `${root}/basic.js`, timeout: 10 }
{ path: jobPathBasic, timeout: 10 }
);

test(
'job.interval is value',
job,
{ path: `${root}/basic.js`, interval: 10 },
{ path: jobPathBasic, interval: 10 },
{},
{ path: `${root}/basic.js`, interval: 10 }
{ path: jobPathBasic, interval: 10 }
);

test(
'job.cron is value',
job,
{ path: `${root}/basic.js`, cron: '* * * * *' },
{ path: jobPathBasic, cron: '* * * * *' },
{},
{
path: `${root}/basic.js`,
path: jobPathBasic,
cron: '* * * * *',
interval: later.parse.cron('* * * * *')
}
Expand All @@ -132,10 +139,10 @@ test(
test(
'job.cron is value with hasSeconds config',
job,
{ path: `${root}/basic.js`, cron: '* * * * *', hasSeconds: false },
{ path: jobPathBasic, cron: '* * * * *', hasSeconds: false },
{},
{
path: `${root}/basic.js`,
path: jobPathBasic,
cron: '* * * * *',
interval: later.parse.cron('* * * * *'),
hasSeconds: false
Expand All @@ -145,10 +152,10 @@ test(
test(
'job.cron is schedule',
job,
{ path: `${root}/basic.js`, cron: later.parse.cron('* * * * *') },
{ path: jobPathBasic, cron: later.parse.cron('* * * * *') },
{},
{
path: `${root}/basic.js`,
path: jobPathBasic,
cron: later.parse.cron('* * * * *'),
interval: later.parse.cron('* * * * *')
}
Expand All @@ -159,7 +166,7 @@ test(
job,
{ name: 'basic', interval: undefined },
{ interval: 10 },
{ name: 'basic', path: `${root}/basic.js`, timeout: 0, interval: 10 }
{ name: 'basic', path: jobPathBasic, timeout: 0, interval: 10 }
);

test(
Expand All @@ -170,7 +177,7 @@ test(
{
timezone: 'local',
name: 'basic',
path: `${root}/basic.js`,
path: jobPathBasic,
timeout: 0,
interval: 0
}
Expand Down Expand Up @@ -199,7 +206,7 @@ test(
{
timezone: 'local',
name: 'basic',
path: `${root}/basic.js`,
path: jobPathBasic,
timeout: 0
}
);
Expand All @@ -212,7 +219,7 @@ test(
{
timezone: 'America/New_York',
name: 'basic',
path: `${root}/basic.js`,
path: jobPathBasic,
timeout: 0
}
);

0 comments on commit 9a279c8

Please sign in to comment.