-
-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make hafas-client work in browser / webpack #281
Comments
related: #56 (comment) |
I would like to ask what your plans are:
|
It has been a while since I last tried to run CORS proxyI have modified --- a/node_modules/hafas-client/lib/request.js
+++ b/node_modules/hafas-client/lib/request.js
@@ -156,7 +156,10 @@ const request = async (ctx, userAgent, reqData) => {
}
const reqId = randomBytes(3).toString('hex')
- const url = profile.endpoint + '?' + stringify(req.query)
+ let url = new URL('http://localhost:3030')
+ url.pathname = profile.endpoint
+ url.search = '?' + stringify(req.query)
+ url = url.href
const fetchReq = new Request(url, req)
profile.logRequest(ctx, fetchReq, reqId) Webpack setup{
"private": true,
"name": "hafas-client-web",
"version": "1.0.0",
"type": "module",
"main": "index.js",
"devDependencies": {
"assert": "^2.0.0",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"stream-browserify": "^3.0.0",
"util": "^0.12.5",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1"
},
"dependencies": {
"hafas-client": "^6.0.1"
}
} // webpack.config.js
import webpack from 'webpack'
import {createRequire} from 'node:module'
const require = createRequire(import.meta.url)
import {join as pathJoin, dirname} from 'node:path'
export default {
plugins: [
new webpack.DefinePlugin({
'process.env.DEBUG': JSON.stringify(undefined),
'process.env.NODE_DEBUG': JSON.stringify(undefined),
}),
],
resolve: {
fallback: {
// Node builtins
assert: require.resolve('assert/'),
buffer: require.resolve('buffer/'),
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
util: require.resolve('util/'),
zlib: require.resolve('browserify-zlib'),
},
},
experiments: {
topLevelAwait: true,
},
} // index.js
import {createClient} from 'hafas-client'
import {profile} from 'hafas-client/p/db/index.js'
const client = createClient(profile, 'hafas-client bundling experiment')
console.log(await client.locations('hbf')) npm install
webpack build -o dist --mode development ./index.js
npx serve dist necessary modifications in
|
I run hafas-client in the browser for trainsear.ch with a CORS proxy. The CORS proxy is just a dumb http server (in my case httproxide but really it could be any http server / reverse proxy). |
The proposed modifications look good, that would improve my experience and hopefully make my fork unnecessary at one point |
Using gzip with default settings, the |
In c2a71b0, I have done the 1st task. Whoever wants to get started on this can work on the 2nd and 3rd task right away. With the 4th task (dynamic |
I have published c2a71b0 as |
I fear the |
hafas-client has a lot of dependencies on node builtins that are not easily filled on browser platforms. my fork currently has some modifications to make this work, but sacrifices nodejs support and features.
It would be great to have both nodejs and browser (with some bundler like webpack) working out of the box.
The text was updated successfully, but these errors were encountered: