Skip to content

Commit

Permalink
fix response content-type and no assets dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Jun 27, 2021
1 parent 8551aa8 commit 291c155
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/models/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 29 additions & 16 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,14 +23,25 @@ declare const global: any

const default_prepare_key = (f: string) => f.replace(/.*?dist\/assets\//, '')

function pathExists(path: string): Promise<boolean> {
return fs.promises
.access(path, fs.constants.F_OK)
.then(() => true)
.catch(() => false)
}

async function load_config(): Promise<Config> {
const cwd = process.cwd()
const dev_server_config = path.join(cwd, 'edge-mock-config.js')
let config: Record<string, any> = {}
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')
}

Expand Down Expand Up @@ -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<void> {
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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 () => {
Expand Down

0 comments on commit 291c155

Please sign in to comment.