Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 663664a

Browse files
committed
remove writeTo method
1 parent 5a5aff3 commit 663664a

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

server/response.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { APIResponse as IResponse } from '../types.d.ts'
2-
import log from '../shared/log.ts'
3-
import compress from './compress.ts'
42

53
export class APIResponse implements IResponse {
64
status = 200
@@ -33,42 +31,4 @@ export class APIResponse implements IResponse {
3331
this.body = JSON.stringify(data, undefined, space)
3432
return this
3533
}
36-
37-
async writeTo({ request, respondWith }: Deno.RequestEvent, status?: number): Promise<void> {
38-
let { body, headers, } = this
39-
let contentType: string | null = null
40-
if (headers.has('Content-Type')) {
41-
contentType = headers.get('Content-Type')!
42-
} else if (typeof body === 'string') {
43-
contentType = 'text/plain; charset=utf-8'
44-
headers.set('Content-Type', contentType)
45-
}
46-
if (!headers.has('Date')) {
47-
headers.set('Date', (new Date).toUTCString())
48-
}
49-
50-
const acceptEncoding = request.headers.get('accept-encoding')
51-
if (acceptEncoding && body && contentType) {
52-
let data = new Uint8Array()
53-
if (typeof body === 'string') {
54-
data = new TextEncoder().encode(body)
55-
} else if (body instanceof Uint8Array) {
56-
data = body
57-
} else if (body instanceof ArrayBuffer) {
58-
data = new Uint8Array(body)
59-
}
60-
const contentEncoding = compress.accept(acceptEncoding, contentType, data.length)
61-
if (contentEncoding) {
62-
body = await compress.compress(data, contentEncoding)
63-
headers.set('Vary', 'Origin')
64-
headers.set('Content-Encoding', contentEncoding)
65-
}
66-
}
67-
68-
try {
69-
await respondWith(new Response(body, { headers, status: status || this.status }))
70-
} catch (err) {
71-
log.warn('http:', err.message)
72-
}
73-
}
7434
}

server/server.ts

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,40 @@ export class Server {
8585
}
8686

8787
const resp = new APIResponse()
88+
const end = async (status?: number): Promise<void> => {
89+
let { body, headers, } = resp
90+
let contentType: string | null = null
91+
if (headers.has('Content-Type')) {
92+
contentType = headers.get('Content-Type')!
93+
} else if (typeof body === 'string') {
94+
contentType = 'text/plain; charset=utf-8'
95+
headers.set('Content-Type', contentType)
96+
}
97+
98+
const acceptEncoding = req.headers.get('accept-encoding')
99+
if (acceptEncoding && body && contentType) {
100+
let data = new Uint8Array()
101+
if (typeof body === 'string') {
102+
data = new TextEncoder().encode(body)
103+
} else if (body instanceof Uint8Array) {
104+
data = body
105+
} else if (body instanceof ArrayBuffer) {
106+
data = new Uint8Array(body)
107+
}
108+
const contentEncoding = compress.accept(acceptEncoding, contentType, data.length)
109+
if (contentEncoding) {
110+
body = await compress.compress(data, contentEncoding)
111+
headers.set('Vary', 'Origin')
112+
headers.set('Content-Encoding', contentEncoding)
113+
}
114+
}
115+
116+
try {
117+
await respondWith(new Response(body, { headers, status: status || resp.status }))
118+
} catch (err) {
119+
log.warn('http:', err.message)
120+
}
121+
}
88122

89123
// set server header
90124
resp.setHeader('Server', 'Aleph.js')
@@ -107,9 +141,11 @@ export class Server {
107141
const [path, search] = util.splitBy(util.atobUrl(util.trimSuffix(util.trimPrefix(pathname, '/_aleph/data/'), '.json')), '?')
108142
const data = await aleph.getSSRData({ pathname: path, search: search ? '?' + search : undefined })
109143
if (data === null) {
110-
resp.json(null).writeTo(e)
144+
resp.json(null)
145+
end()
111146
} else {
112-
resp.json(data).writeTo(e)
147+
resp.json(data)
148+
end()
113149
}
114150
return
115151
}
@@ -118,7 +154,7 @@ export class Server {
118154
if (relPath == '/main.js') {
119155
resp.body = await aleph.createMainJS(false)
120156
resp.setHeader('Content-Type', 'application/javascript; charset=utf-8')
121-
resp.writeTo(e)
157+
end()
122158
return
123159
}
124160

@@ -139,14 +175,14 @@ export class Server {
139175
if (content) {
140176
const hash = aleph.gteModuleHash(module)
141177
if (hash === req.headers.get('If-None-Match')) {
142-
resp.writeTo(e, 304)
178+
end(304)
143179
return
144180
}
145181

146182
resp.setHeader('ETag', hash)
147183
resp.setHeader('Content-Type', 'application/javascript; charset=utf-8')
148184
resp.body = content
149-
resp.writeTo(e)
185+
end()
150186
return
151187
}
152188
}
@@ -158,19 +194,19 @@ export class Server {
158194
const info = Deno.lstatSync(filePath)
159195
const lastModified = info.mtime?.toUTCString() ?? (new Date).toUTCString()
160196
if (lastModified === req.headers.get('If-Modified-Since')) {
161-
resp.writeTo(e, 304)
197+
end(304)
162198
return
163199
}
164200

165201
resp.body = await Deno.readFile(filePath)
166202
resp.setHeader('Last-Modified', lastModified)
167203
resp.setHeader('Content-Type', getContentType(filePath))
168-
resp.writeTo(e)
204+
end()
169205
return
170206
}
171207

172208
resp.body = 'file not found'
173-
resp.writeTo(e, 404)
209+
end(404)
174210
return
175211
}
176212

@@ -180,14 +216,14 @@ export class Server {
180216
const info = Deno.lstatSync(filePath)
181217
const lastModified = info.mtime?.toUTCString() ?? (new Date).toUTCString()
182218
if (lastModified === req.headers.get('If-Modified-Since')) {
183-
resp.writeTo(e, 304)
219+
end(304)
184220
return
185221
}
186222

187223
resp.body = await Deno.readFile(filePath)
188224
resp.setHeader('Last-Modified', lastModified)
189225
resp.setHeader('Content-Type', getContentType(filePath))
190-
resp.writeTo(e)
226+
end()
191227
return
192228
}
193229

@@ -207,7 +243,8 @@ export class Server {
207243
if (util.isFunction(h)) {
208244
await h(context)
209245
} else {
210-
await resp.json({ status: 500, message: 'bad api handler' }).writeTo(e, 500)
246+
resp.json({ status: 500, message: 'bad api handler' })
247+
end(500)
211248
}
212249
}]
213250
let pointer = 0
@@ -246,14 +283,16 @@ export class Server {
246283
}
247284
await next()
248285
if (!responded) {
249-
resp.writeTo(e)
286+
end()
250287
}
251288
} catch (err) {
252-
resp.json({ status: 500, message: err.message }).writeTo(e, 500)
289+
resp.json({ status: 500, message: err.message })
290+
end(500)
253291
log.error('invoke API:', err)
254292
}
255293
} else {
256-
resp.json({ status: 404, message: 'not found' }).writeTo(e, 404)
294+
resp.json({ status: 404, message: 'not found' })
295+
end(404)
257296
}
258297
return
259298
}
@@ -265,7 +304,7 @@ export class Server {
265304
})
266305
resp.body = html
267306
resp.setHeader('Content-Type', 'text/html; charset=utf-8')
268-
resp.writeTo(e, status)
307+
end(status)
269308
} catch (err) {
270309
try {
271310
// todo: custom error page

0 commit comments

Comments
 (0)