about summary refs log tree commit diff stats
path: root/src/html/script.nim
blob: 05e0b5ccb7b4c90f6df676387f16e5d073ee02dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import js/javascript
import loader/request
import types/referer
import types/url

type
  ParserMetadata* = enum
    PARSER_INSERTED, NOT_PARSER_INSERTED

  ScriptType* = enum
    NO_SCRIPTTYPE, CLASSIC, MODULE, IMPORTMAP

  ScriptResultType* = enum
    RESULT_NULL, RESULT_SCRIPT, RESULT_IMPORT_MAP_PARSE, RESULT_FETCHING

type
  EnvironmentSettings* = ref object
    scripting*: bool
    moduleMap*: ModuleMap

  Script* = object
    #TODO setings
    baseURL*: URL
    options*: ScriptOptions
    mutedErrors*: bool
    #TODO parse error/error to rethrow
    record*: JSValue

  ScriptOptions* = object
    nonce*: string
    integrity*: string
    parserMetadata*: ParserMetadata
    credentialsMode*: CredentialsMode
    referrerPolicy*: Option[ReferrerPolicy]
    renderBlocking*: bool

  ScriptResult* = ref object
    case t*: ScriptResultType
    of RESULT_NULL:
      discard
    of RESULT_SCRIPT:
      script*: Script
    of RESULT_FETCHING:
      discard
    of RESULT_IMPORT_MAP_PARSE:
      discard #TODO

  ModuleMapEntry = object
    key: tuple[url, moduleType: string]
    value*: ScriptResult

  ModuleMap* = seq[ModuleMapEntry]

proc find*(moduleMap: ModuleMap, url: URL, moduleType: string): int =
  let surl = $url
  for i, entry in moduleMap:
    if entry.key.moduleType == moduleType and entry.key.url == surl:
      return i
  return -1

func fetchDestinationFromModuleType*(defaultDestination: RequestDestination,
    moduleType: string): RequestDestination =
  if moduleType == "json":
    return RequestDestination.JSON
  if moduleType == "css":
    return RequestDestination.STYLE
  return defaultDestination