Skip to content

Commit aa0c8a1

Browse files
authored
Merge pull request #207 from CroniD/master
Windows compatibility
2 parents ae91c1e + 9a279c8 commit aa0c8a1

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
const fs = require('fs');
66
const EventEmitter = require('events');
7+
const { pathToFileURL } = require('url');
78
const { Worker } = require('worker_threads');
89
const { join, resolve } = require('path');
910
const { debuglog } = require('util');
@@ -235,7 +236,13 @@ class Bree extends EventEmitter {
235236
try {
236237
const importPath = join(this.config.root, this.config.defaultRootIndex);
237238
debug('importPath', importPath);
238-
const obj = await import(importPath);
239+
const importUrl = pathToFileURL(importPath).toString();
240+
debug('importUrl', importUrl);
241+
// hint: import statement expect a esm-url-string, not a file-path-string (https://github.com/breejs/bree/issues/202)
242+
// otherwise the following error is expected:
243+
// Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader.
244+
// On Windows, absolute paths must be valid file:// URLs.
245+
const obj = await import(importUrl);
239246
if (typeof obj.default !== 'object') {
240247
throw new ImportError(
241248
`Root index file missing default export at: ${importPath}`

test/job-builder.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const later = require('@breejs/later');
55
const jobBuilder = require('../src/job-builder');
66

77
const root = path.join(__dirname, 'jobs');
8+
const jobPathBasic = path.join(root, 'basic.js');
9+
810
const baseConfig = {
911
root,
1012
timeout: 0,
@@ -26,15 +28,20 @@ test(
2628
job,
2729
null,
2830
{},
29-
{ name: 'basic', path: `${root}/basic.js`, timeout: 0, interval: 0 }
31+
{ name: 'basic', path: jobPathBasic, timeout: 0, interval: 0 }
3032
);
3133

3234
test(
3335
'job name as file name with extension',
3436
job,
3537
'basic.js',
3638
{},
37-
{ name: 'basic.js', path: `${root}/basic.js`, timeout: 0, interval: 0 }
39+
{
40+
name: 'basic.js',
41+
path: jobPathBasic,
42+
timeout: 0,
43+
interval: 0
44+
}
3845
);
3946

4047
function basic() {
@@ -74,23 +81,23 @@ test(
7481
job,
7582
{ name: 'basic', path: '' },
7683
{},
77-
{ name: 'basic', path: `${root}/basic.js`, timeout: 0 }
84+
{ name: 'basic', path: jobPathBasic, timeout: 0 }
7885
);
7986

8087
test(
8188
'job.path is blank and name of job is defined with extension',
8289
job,
8390
{ name: 'basic.js', path: '' },
8491
{},
85-
{ name: 'basic.js', path: `${root}/basic.js`, timeout: 0 }
92+
{ name: 'basic.js', path: jobPathBasic, timeout: 0 }
8693
);
8794

8895
test(
8996
'job.path is path to file',
9097
job,
91-
{ path: `${root}/basic.js` },
98+
{ path: jobPathBasic },
9299
{},
93-
{ path: `${root}/basic.js`, timeout: 0 }
100+
{ path: jobPathBasic, timeout: 0 }
94101
);
95102

96103
test(
@@ -104,26 +111,26 @@ test(
104111
test(
105112
'job.timeout is value',
106113
job,
107-
{ path: `${root}/basic.js`, timeout: 10 },
114+
{ path: jobPathBasic, timeout: 10 },
108115
{},
109-
{ path: `${root}/basic.js`, timeout: 10 }
116+
{ path: jobPathBasic, timeout: 10 }
110117
);
111118

112119
test(
113120
'job.interval is value',
114121
job,
115-
{ path: `${root}/basic.js`, interval: 10 },
122+
{ path: jobPathBasic, interval: 10 },
116123
{},
117-
{ path: `${root}/basic.js`, interval: 10 }
124+
{ path: jobPathBasic, interval: 10 }
118125
);
119126

120127
test(
121128
'job.cron is value',
122129
job,
123-
{ path: `${root}/basic.js`, cron: '* * * * *' },
130+
{ path: jobPathBasic, cron: '* * * * *' },
124131
{},
125132
{
126-
path: `${root}/basic.js`,
133+
path: jobPathBasic,
127134
cron: '* * * * *',
128135
interval: later.parse.cron('* * * * *')
129136
}
@@ -132,10 +139,10 @@ test(
132139
test(
133140
'job.cron is value with hasSeconds config',
134141
job,
135-
{ path: `${root}/basic.js`, cron: '* * * * *', hasSeconds: false },
142+
{ path: jobPathBasic, cron: '* * * * *', hasSeconds: false },
136143
{},
137144
{
138-
path: `${root}/basic.js`,
145+
path: jobPathBasic,
139146
cron: '* * * * *',
140147
interval: later.parse.cron('* * * * *'),
141148
hasSeconds: false
@@ -145,10 +152,10 @@ test(
145152
test(
146153
'job.cron is schedule',
147154
job,
148-
{ path: `${root}/basic.js`, cron: later.parse.cron('* * * * *') },
155+
{ path: jobPathBasic, cron: later.parse.cron('* * * * *') },
149156
{},
150157
{
151-
path: `${root}/basic.js`,
158+
path: jobPathBasic,
152159
cron: later.parse.cron('* * * * *'),
153160
interval: later.parse.cron('* * * * *')
154161
}
@@ -159,7 +166,7 @@ test(
159166
job,
160167
{ name: 'basic', interval: undefined },
161168
{ interval: 10 },
162-
{ name: 'basic', path: `${root}/basic.js`, timeout: 0, interval: 10 }
169+
{ name: 'basic', path: jobPathBasic, timeout: 0, interval: 10 }
163170
);
164171

165172
test(
@@ -170,7 +177,7 @@ test(
170177
{
171178
timezone: 'local',
172179
name: 'basic',
173-
path: `${root}/basic.js`,
180+
path: jobPathBasic,
174181
timeout: 0,
175182
interval: 0
176183
}
@@ -199,7 +206,7 @@ test(
199206
{
200207
timezone: 'local',
201208
name: 'basic',
202-
path: `${root}/basic.js`,
209+
path: jobPathBasic,
203210
timeout: 0
204211
}
205212
);
@@ -212,7 +219,7 @@ test(
212219
{
213220
timezone: 'America/New_York',
214221
name: 'basic',
215-
path: `${root}/basic.js`,
222+
path: jobPathBasic,
216223
timeout: 0
217224
}
218225
);

0 commit comments

Comments
 (0)