1
1
#include " commonjs.h"
2
2
3
- #include " modules-new.h"
4
3
#include " modules.h"
5
4
6
5
namespace workerd ::jsg {
@@ -10,95 +9,55 @@ CommonJsModuleContext::CommonJsModuleContext(jsg::Lock& js, kj::Path path)
10
9
path (kj::mv(path)),
11
10
exports(js.v8Isolate, module->getExports (js)) {}
12
11
13
- CommonJsModuleContext::CommonJsModuleContext (jsg::Lock& js, const jsg::Url& url)
14
- : module(jsg::alloc<CommonJsModuleObject>(js, kj::str(url.getHref()))),
15
- path(url.clone()),
16
- exports(js.v8Isolate, module->getExports (js)) {}
17
-
18
12
v8::Local<v8::Value> CommonJsModuleContext::require (jsg::Lock& js, kj::String specifier) {
19
- KJ_SWITCH_ONEOF (path) {
20
- KJ_CASE_ONEOF (p, kj::Path) {
21
- auto modulesForResolveCallback = getModulesForResolveCallback (js.v8Isolate );
22
- KJ_REQUIRE (modulesForResolveCallback != nullptr , " didn't expect resolveCallback() now" );
23
-
24
- if (isNodeJsCompatEnabled (js)) {
25
- KJ_IF_SOME (nodeSpec, checkNodeSpecifier (specifier)) {
26
- specifier = kj::mv (nodeSpec);
27
- }
28
- }
29
-
30
- kj::Path targetPath = ([&] {
31
- // If the specifier begins with one of our known prefixes, let's not resolve
32
- // it against the referrer.
33
- if (specifier.startsWith (" node:" ) || specifier.startsWith (" cloudflare:" ) ||
34
- specifier.startsWith (" workerd:" )) {
35
- return kj::Path::parse (specifier);
36
- }
37
- return p.parent ().eval (specifier);
38
- })();
39
-
40
- // require() is only exposed to worker bundle modules so the resolve here is only
41
- // permitted to require worker bundle or built-in modules. Internal modules are
42
- // excluded.
43
- auto & info =
44
- JSG_REQUIRE_NONNULL (modulesForResolveCallback->resolve (js, targetPath, p,
45
- ModuleRegistry::ResolveOption::DEFAULT,
46
- ModuleRegistry::ResolveMethod::REQUIRE, specifier.asPtr ()),
47
- Error, " No such module \" " , targetPath.toString (), " \" ." );
48
- // Adding imported from suffix here not necessary like it is for
49
- // resolveCallback, since we have a js stack that will include the parent
50
- // module's name and location of the failed require().
51
-
52
- ModuleRegistry::RequireImplOptions options = ModuleRegistry::RequireImplOptions::DEFAULT;
53
- if (getCommonJsExportDefault (js.v8Isolate )) {
54
- options = ModuleRegistry::RequireImplOptions::EXPORT_DEFAULT;
55
- }
56
-
57
- return ModuleRegistry::requireImpl (js, info, options);
58
- }
59
- KJ_CASE_ONEOF (u, jsg::Url) {
60
- return modules::ModuleRegistry::resolve (js, specifier, " default" _kj,
61
- modules::ResolveContext::Type::BUNDLE, modules::ResolveContext::Source::REQUIRE, u);
13
+ auto modulesForResolveCallback = getModulesForResolveCallback (js.v8Isolate );
14
+ KJ_REQUIRE (modulesForResolveCallback != nullptr , " didn't expect resolveCallback() now" );
15
+
16
+ if (isNodeJsCompatEnabled (js)) {
17
+ KJ_IF_SOME (nodeSpec, checkNodeSpecifier (specifier)) {
18
+ specifier = kj::mv (nodeSpec);
62
19
}
63
20
}
64
- KJ_UNREACHABLE;
21
+
22
+ kj::Path targetPath = ([&] {
23
+ // If the specifier begins with one of our known prefixes, let's not resolve
24
+ // it against the referrer.
25
+ if (specifier.startsWith (" node:" ) || specifier.startsWith (" cloudflare:" ) ||
26
+ specifier.startsWith (" workerd:" )) {
27
+ return kj::Path::parse (specifier);
28
+ }
29
+ return path.parent ().eval (specifier);
30
+ })();
31
+
32
+ // require() is only exposed to worker bundle modules so the resolve here is only
33
+ // permitted to require worker bundle or built-in modules. Internal modules are
34
+ // excluded.
35
+ auto & info = JSG_REQUIRE_NONNULL (modulesForResolveCallback->resolve (js, targetPath, path,
36
+ ModuleRegistry::ResolveOption::DEFAULT,
37
+ ModuleRegistry::ResolveMethod::REQUIRE, specifier.asPtr ()),
38
+ Error, " No such module \" " , targetPath.toString (), " \" ." );
39
+ // Adding imported from suffix here not necessary like it is for resolveCallback, since we have a
40
+ // js stack that will include the parent module's name and location of the failed require().
41
+
42
+ ModuleRegistry::RequireImplOptions options = ModuleRegistry::RequireImplOptions::DEFAULT;
43
+ if (getCommonJsExportDefault (js.v8Isolate )) {
44
+ options = ModuleRegistry::RequireImplOptions::EXPORT_DEFAULT;
45
+ }
46
+
47
+ return ModuleRegistry::requireImpl (js, info, options);
65
48
}
66
49
67
50
void CommonJsModuleContext::visitForMemoryInfo (MemoryTracker& tracker) const {
68
51
tracker.trackField (" exports" , exports);
69
- KJ_SWITCH_ONEOF (path) {
70
- KJ_CASE_ONEOF (p, kj::Path) {
71
- tracker.trackFieldWithSize (" path" , p.size ());
72
- }
73
- KJ_CASE_ONEOF (u, jsg::Url) {
74
- tracker.trackFieldWithSize (" path" , u.getHref ().size ());
75
- }
76
- }
52
+ tracker.trackFieldWithSize (" path" , path.size ());
77
53
}
78
54
79
55
kj::String CommonJsModuleContext::getFilename () const {
80
- KJ_SWITCH_ONEOF (path) {
81
- KJ_CASE_ONEOF (p, kj::Path) {
82
- return p.toString (true );
83
- }
84
- KJ_CASE_ONEOF (u, jsg::Url) {
85
- return kj::str (u.getPathname ());
86
- }
87
- }
88
- KJ_UNREACHABLE;
56
+ return path.toString (true );
89
57
}
90
58
91
59
kj::String CommonJsModuleContext::getDirname () const {
92
- KJ_SWITCH_ONEOF (path) {
93
- KJ_CASE_ONEOF (p, kj::Path) {
94
- return p.parent ().toString (true );
95
- }
96
- // TODO(new-module-registry): Appropriately convert file URL into path
97
- KJ_CASE_ONEOF (u, jsg::Url) {
98
- return kj::str (u.getPathname ());
99
- }
100
- }
101
- KJ_UNREACHABLE;
60
+ return path.parent ().toString (true );
102
61
}
103
62
104
63
jsg::Ref<CommonJsModuleObject> CommonJsModuleContext::getModule (jsg::Lock& js) {
@@ -108,7 +67,7 @@ jsg::Ref<CommonJsModuleObject> CommonJsModuleContext::getModule(jsg::Lock& js) {
108
67
v8::Local<v8::Value> CommonJsModuleContext::getExports (jsg::Lock& js) const {
109
68
return exports.getHandle (js);
110
69
}
111
- void CommonJsModuleContext::setExports (jsg::Lock& js, jsg:: Value value) {
70
+ void CommonJsModuleContext::setExports (jsg::Value value) {
112
71
exports = kj::mv (value);
113
72
}
114
73
@@ -139,66 +98,50 @@ NodeJsModuleContext::NodeJsModuleContext(jsg::Lock& js, kj::Path path)
139
98
path(kj::mv(path)),
140
99
exports(js.v8Ref(module->getExports (js))) {}
141
100
142
- NodeJsModuleContext::NodeJsModuleContext (jsg::Lock& js, const jsg::Url& url)
143
- : module(jsg::alloc<NodeJsModuleObject>(js, kj::str(url.getHref()))),
144
- path(url.clone()),
145
- exports(js.v8Isolate, module->getExports (js)) {}
146
-
147
101
v8::Local<v8::Value> NodeJsModuleContext::require (jsg::Lock& js, kj::String specifier) {
148
- KJ_SWITCH_ONEOF (path) {
149
- KJ_CASE_ONEOF (p, kj::Path) {
150
- // If it is a bare specifier known to be a Node.js built-in, then prefix the
151
- // specifier with node:
152
- bool isNodeBuiltin = false ;
153
- auto resolveOption = jsg::ModuleRegistry::ResolveOption::DEFAULT;
154
- KJ_IF_SOME (spec, checkNodeSpecifier (specifier)) {
155
- specifier = kj::mv (spec);
156
- isNodeBuiltin = true ;
157
- resolveOption = jsg::ModuleRegistry::ResolveOption::BUILTIN_ONLY;
158
- }
159
-
160
- // TODO(cleanup): This implementation from here on is identical to the
161
- // CommonJsModuleContext::require. We should consolidate these as the
162
- // next step.
163
-
164
- auto modulesForResolveCallback = jsg::getModulesForResolveCallback (js.v8Isolate );
165
- KJ_REQUIRE (modulesForResolveCallback != nullptr , " didn't expect resolveCallback() now" );
166
-
167
- kj::Path targetPath = ([&] {
168
- // If the specifier begins with one of our known prefixes, let's not resolve
169
- // it against the referrer.
170
- if (specifier.startsWith (" node:" ) || specifier.startsWith (" cloudflare:" ) ||
171
- specifier.startsWith (" workerd:" )) {
172
- return kj::Path::parse (specifier);
173
- }
174
- return p.parent ().eval (specifier);
175
- })();
176
-
177
- // require() is only exposed to worker bundle modules so the resolve here is only
178
- // permitted to require worker bundle or built-in modules. Internal modules are
179
- // excluded.
180
- auto & info =
181
- JSG_REQUIRE_NONNULL (modulesForResolveCallback->resolve (js, targetPath, p, resolveOption,
182
- ModuleRegistry::ResolveMethod::REQUIRE, specifier.asPtr ()),
183
- Error, " No such module \" " , targetPath.toString (), " \" ." );
184
- // Adding imported from suffix here not necessary like it is for resolveCallback,
185
- // since we have a js stack that will include the parent module's name and location
186
- // of the failed require().
187
-
188
- if (!isNodeBuiltin) {
189
- JSG_REQUIRE_NONNULL (
190
- info.maybeSynthetic , TypeError, " Cannot use require() to import an ES Module." );
191
- }
192
-
193
- return ModuleRegistry::requireImpl (
194
- js, info, ModuleRegistry::RequireImplOptions::EXPORT_DEFAULT);
195
- }
196
- KJ_CASE_ONEOF (u, jsg::Url) {
197
- return modules::ModuleRegistry::resolve (js, specifier, " default" _kj,
198
- modules::ResolveContext::Type::BUNDLE, modules::ResolveContext::Source::REQUIRE, u);
199
- }
102
+ // If it is a bare specifier known to be a Node.js built-in, then prefix the
103
+ // specifier with node:
104
+ bool isNodeBuiltin = false ;
105
+ auto resolveOption = jsg::ModuleRegistry::ResolveOption::DEFAULT;
106
+ KJ_IF_SOME (spec, checkNodeSpecifier (specifier)) {
107
+ specifier = kj::mv (spec);
108
+ isNodeBuiltin = true ;
109
+ resolveOption = jsg::ModuleRegistry::ResolveOption::BUILTIN_ONLY;
200
110
}
201
- KJ_UNREACHABLE;
111
+
112
+ // TODO(cleanup): This implementation from here on is identical to the
113
+ // CommonJsModuleContext::require. We should consolidate these as the
114
+ // next step.
115
+
116
+ auto modulesForResolveCallback = jsg::getModulesForResolveCallback (js.v8Isolate );
117
+ KJ_REQUIRE (modulesForResolveCallback != nullptr , " didn't expect resolveCallback() now" );
118
+
119
+ kj::Path targetPath = ([&] {
120
+ // If the specifier begins with one of our known prefixes, let's not resolve
121
+ // it against the referrer.
122
+ if (specifier.startsWith (" node:" ) || specifier.startsWith (" cloudflare:" ) ||
123
+ specifier.startsWith (" workerd:" )) {
124
+ return kj::Path::parse (specifier);
125
+ }
126
+ return path.parent ().eval (specifier);
127
+ })();
128
+
129
+ // require() is only exposed to worker bundle modules so the resolve here is only
130
+ // permitted to require worker bundle or built-in modules. Internal modules are
131
+ // excluded.
132
+ auto & info =
133
+ JSG_REQUIRE_NONNULL (modulesForResolveCallback->resolve (js, targetPath, path, resolveOption,
134
+ ModuleRegistry::ResolveMethod::REQUIRE, specifier.asPtr ()),
135
+ Error, " No such module \" " , targetPath.toString (), " \" ." );
136
+ // Adding imported from suffix here not necessary like it is for resolveCallback, since we have a
137
+ // js stack that will include the parent module's name and location of the failed require().
138
+
139
+ if (!isNodeBuiltin) {
140
+ JSG_REQUIRE_NONNULL (
141
+ info.maybeSynthetic , TypeError, " Cannot use require() to import an ES Module." );
142
+ }
143
+
144
+ return ModuleRegistry::requireImpl (js, info, ModuleRegistry::RequireImplOptions::EXPORT_DEFAULT);
202
145
}
203
146
204
147
v8::Local<v8::Value> NodeJsModuleContext::getBuffer (jsg::Lock& js) {
@@ -217,28 +160,11 @@ v8::Local<v8::Value> NodeJsModuleContext::getProcess(jsg::Lock& js) {
217
160
}
218
161
219
162
kj::String NodeJsModuleContext::getFilename () {
220
- KJ_SWITCH_ONEOF (path) {
221
- KJ_CASE_ONEOF (p, kj::Path) {
222
- return p.toString (true );
223
- }
224
- KJ_CASE_ONEOF (u, jsg::Url) {
225
- return kj::str (u.getPathname ());
226
- }
227
- }
228
- KJ_UNREACHABLE;
163
+ return path.toString (true );
229
164
}
230
165
231
166
kj::String NodeJsModuleContext::getDirname () {
232
- KJ_SWITCH_ONEOF (path) {
233
- KJ_CASE_ONEOF (p, kj::Path) {
234
- return p.parent ().toString (true );
235
- }
236
- // TODO(new-module-registry): Appropriately convert file URL into path
237
- KJ_CASE_ONEOF (u, jsg::Url) {
238
- return kj::str (u.getPathname ());
239
- }
240
- }
241
- KJ_UNREACHABLE;
167
+ return path.parent ().toString (true );
242
168
}
243
169
244
170
jsg::Ref<NodeJsModuleObject> NodeJsModuleContext::getModule (jsg::Lock& js) {
@@ -269,16 +195,4 @@ kj::StringPtr NodeJsModuleObject::getPath() {
269
195
return path;
270
196
}
271
197
272
- void NodeJsModuleContext::visitForMemoryInfo (MemoryTracker& tracker) const {
273
- tracker.trackField (" exports" , exports);
274
- KJ_SWITCH_ONEOF (path) {
275
- KJ_CASE_ONEOF (p, kj::Path) {
276
- tracker.trackFieldWithSize (" path" , p.size ());
277
- }
278
- KJ_CASE_ONEOF (u, jsg::Url) {
279
- tracker.trackFieldWithSize (" path" , u.getHref ().size ());
280
- }
281
- }
282
- }
283
-
284
198
} // namespace workerd::jsg
0 commit comments