diff --git a/package.json b/package.json index 840c33c..216157f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "edge-mock", - "version": "0.0.14", + "version": "0.0.15", "description": "types for testing an developer edge applications", "main": "index.js", "types": "index.d.ts", diff --git a/src/models/Response.ts b/src/models/Response.ts index 6c2df34..2fe7f3e 100644 --- a/src/models/Response.ts +++ b/src/models/Response.ts @@ -18,6 +18,9 @@ export class EdgeResponse extends EdgeBody implements Response { const headers = asHeaders(init.headers) const boundary = findBoundary(headers, body) super(body, boundary) + if (typeof body == 'string' && !headers.has('content-type')) { + headers.set('content-type', 'text/plain') + } this.headers = headers this.status = init.status === undefined ? 200 : init.status this.ok = this.status >= 200 && this.status < 300 diff --git a/src/server.ts b/src/server.ts index ec73844..e4aedb3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,12 +1,13 @@ #!/usr/bin/env node import path from 'path' +import fs from 'fs' import express, {Response as ExpressResponse} from 'express' import webpack from 'webpack' import livereload from 'livereload' import {makeEdgeEnv, EdgeKVNamespace, EdgeEnv} from './index' import live_fetch from './live_fetch' -import {catArraysBufferViews, encode} from 'edge-mock/utils' +import {catArraysBufferViews, encode} from './utils' export interface Config { webpack_config: string @@ -22,14 +23,25 @@ declare const global: any const default_prepare_key = (f: string) => f.replace(/.*?dist\/assets\//, '') +function pathExists(path: string): Promise { + return fs.promises + .access(path, fs.constants.F_OK) + .then(() => true) + .catch(() => false) +} + async function load_config(): Promise { const cwd = process.cwd() const dev_server_config = path.join(cwd, 'edge-mock-config.js') let config: Record = {} - try { - config = await import(dev_server_config) - console.log('edge-mock-config.js found, using it for config') - } catch (e) { + if (await pathExists(dev_server_config)) { + try { + config = await import(dev_server_config) + console.log('edge-mock-config.js found, using it for config') + } catch (e) { + console.error('error loading', dev_server_config, e) + } + } else { console.log('edge-mock-config.js not found, using default config') } @@ -67,24 +79,24 @@ class WebpackState { } async function start_webpack(config: Config): Promise<[EdgeEnv, WebpackState]> { - const kv_namespace = new EdgeKVNamespace() + let static_content_kv: EdgeKVNamespace - const global_extra = { - __STATIC_CONTENT: kv_namespace, - __STATIC_CONTENT_MANIFEST: '{}', - fetch: live_fetch, - } - const env = makeEdgeEnv(global_extra) + const env = makeEdgeEnv({fetch: live_fetch}) const webpack_state = new WebpackState() - const wp_config = await import(config.webpack_config) - async function on_webpack_success(stats: MultiStats): Promise { console.log(stats.toString('minimal')) delete require.cache[require.resolve(config.dist_path)] - await kv_namespace._add_files(config.dist_assets_path, config.prepare_key) - global.__STATIC_CONTENT_MANIFEST = kv_namespace._manifestJson() + if (await pathExists(config.dist_assets_path)) { + if (!static_content_kv) { + static_content_kv = new EdgeKVNamespace() + global.__STATIC_CONTENT = static_content_kv + console.log('adding KV store "__STATIC_CONTENT" to global namespace') + } + await static_content_kv._add_files(config.dist_assets_path, config.prepare_key) + global.__STATIC_CONTENT_MANIFEST = static_content_kv._manifestJson() + } env.clearEventListener() try { await import(config.dist_path) @@ -95,6 +107,7 @@ async function start_webpack(config: Config): Promise<[EdgeEnv, WebpackState]> { webpack_state.clearError() } + const wp_config = await import(config.webpack_config) webpack(wp_config.default).watch({}, (err, stats) => { if (err) { console.error(err) diff --git a/tests/response.test.ts b/tests/response.test.ts index 5df7966..6c1731b 100644 --- a/tests/response.test.ts +++ b/tests/response.test.ts @@ -5,6 +5,7 @@ describe('EdgeResponse', () => { test('string', async () => { const response = new EdgeResponse('abc') expect(response.status).toStrictEqual(200) + expect(response.headers.get('content-type')).toEqual('text/plain') expect(response.statusText).toStrictEqual('') expect(response.type).toStrictEqual('default') expect(response.bodyUsed).toStrictEqual(false) @@ -24,6 +25,7 @@ describe('EdgeResponse', () => { const blob = new EdgeBlob([new Uint8Array([97, 98, 99])]) const response = new EdgeResponse(blob) expect(await response.text()).toEqual('abc') + expect(response.headers.get('content-type')).toEqual(null) }) test('blob-arrayBuffer', async () => {