@goodandready/dsh-russian-lang 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import crypto from 'node:crypto'
2
2
  import { registerPluginUpdater } from './plugin-updater.js'
3
3
  import { registerTranslator } from './translator.js'
4
- import { getCoreDictionaries, getPluginDictionaries, getPluginDictionariesByNames, getZhRuMap } from './locales.js'
4
+ import { getCoreDictionaries, getPluginDictionaries, getPluginDictionariesByNames, getAllDictionaries, getZhRuMap } from './locales.js'
5
5
  // dsh-russian-lang — серверная половина.
6
6
  //
7
7
  // Словари и переключатель живут в браузере (lib/client.js). Хосту переводить
@@ -30,7 +30,11 @@ const sendJsonWithEtag = (request, response, obj, maxAge = 300) => {
30
30
  'cache-control': `public, max-age=${maxAge}, must-revalidate`,
31
31
  'content-length': body.length,
32
32
  })
33
- response.end(body)
33
+ if (request?.method === 'HEAD') {
34
+ response.end()
35
+ } else {
36
+ response.end(body)
37
+ }
34
38
  }
35
39
 
36
40
  export const name = '@goodandready/dsh-russian-lang'
@@ -104,6 +108,11 @@ export function apply(ctx) {
104
108
  path: '/api/dsh-russian-lang/dict/core',
105
109
  handler: async (request, response) => {
106
110
  try {
111
+ if (request.method !== 'GET' && request.method !== 'HEAD') {
112
+ response.writeHead(405, { allow: 'GET, HEAD', 'content-type': 'application/json; charset=utf-8' })
113
+ response.end(JSON.stringify({ error: 'Method not allowed' }))
114
+ return
115
+ }
107
116
  const core = getCoreDictionaries()
108
117
  const zhRu = getZhRuMap()
109
118
  sendJsonWithEtag(request, response, { core, zhRu })
@@ -120,6 +129,11 @@ export function apply(ctx) {
120
129
  path: '/api/dsh-russian-lang/dict/plugins',
121
130
  handler: async (request, response) => {
122
131
  try {
132
+ if (request.method !== 'GET' && request.method !== 'HEAD') {
133
+ response.writeHead(405, { allow: 'GET, HEAD', 'content-type': 'application/json; charset=utf-8' })
134
+ response.end(JSON.stringify({ error: 'Method not allowed' }))
135
+ return
136
+ }
123
137
  const url = new URL(request.url, 'http://localhost')
124
138
  const namesParam = url.searchParams.get('names') || url.searchParams.get('list') || ''
125
139
  const names = namesParam ? namesParam.split(',').map((s) => s.trim()).filter(Boolean) : null
@@ -138,10 +152,14 @@ export function apply(ctx) {
138
152
  path: '/api/dsh-russian-lang/dict/all',
139
153
  handler: async (request, response) => {
140
154
  try {
141
- const core = getCoreDictionaries()
142
- const plugins = getPluginDictionaries()
155
+ if (request.method !== 'GET' && request.method !== 'HEAD') {
156
+ response.writeHead(405, { allow: 'GET, HEAD', 'content-type': 'application/json; charset=utf-8' })
157
+ response.end(JSON.stringify({ error: 'Method not allowed' }))
158
+ return
159
+ }
160
+ const all = getAllDictionaries()
143
161
  const zhRu = getZhRuMap()
144
- sendJsonWithEtag(request, response, Object.assign({}, core, plugins, { core, plugins, zhRu }))
162
+ sendJsonWithEtag(request, response, Object.assign({}, all, { core: getCoreDictionaries(), plugins: getPluginDictionaries(), zhRu }))
145
163
  } catch (err) {
146
164
  response.writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
147
165
  response.end(JSON.stringify({ error: String(err) }))