summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe/npm_registry.py
blob: d97ced7cdaf3b02a20a3d4cf621f87ea4f85d261 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

import bb
import json
import subprocess

_ALWAYS_SAFE = frozenset('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                         'abcdefghijklmnopqrstuvwxyz'
                         '0123456789'
                         '_.-~()')

MISSING_OK = object()

REGISTRY = "https://registry.npmjs.org"

# we can not use urllib.parse here because npm expects lowercase
# hex-chars but urllib generates uppercase ones
def uri_quote(s, safe = '/'):
    res = ""
    safe_set = set(safe)
    for c in s:
        if c in _ALWAYS_SAFE or c in safe_set:
            res += c
        else:
            res += '%%%02x' % ord(c)
    return res

class PackageJson:
    def __init__(self, spec):
        self.__spec = spec

    @property
    def name(self):
        return self.__spec['name']

    @property
    def version(self):
        return self.__spec['version']

    @property
    def empty_manifest(self):
        return {
            'name': self.name,
            'description': self.__spec.get('description', ''),
            'versions': {},
        }

    def base_filename(self):
        return uri_quote(self.name, safe = '@')

    def as_manifest_entry(self, tarball_uri):
        res = {}

        ## NOTE: 'npm install' requires more than basic meta information;
        ## e.g. it takes 'bin' from this manifest entry but not the actual
        ## 'package.json'
        for (idx,dflt) in [('name', None),
                           ('description', ""),
                           ('version', None),
                           ('bin', MISSING_OK),
                           ('man', MISSING_OK),
                           ('scripts', MISSING_OK),
                           ('directories', MISSING_OK),
                           ('dependencies', MISSING_OK),
                           ('devDependencies', MISSING_OK),
                           ('optionalDependencies', MISSING_OK),
                           ('license', "unknown")]:
            if idx in self.__spec:
                res[idx] = self.__spec[idx]
            elif dflt == MISSING_OK:
                pass
            elif dflt != None:
                res[idx] = dflt
            else:
                raise Exception("%s-%s: missing key %s" % (self.name,
                                                           self.version,
                                                           idx))

        res['dist'] = {
            'tarball': tarball_uri,
        }

        return res

class ManifestImpl:
    def __init__(self, base_fname, spec):
        self.__base = base_fname
        self.__spec = spec

    def load(self):
        try:
            with open(self.filename, "r") as f:
                res = json.load(f)
        except IOError:
            res = self.__spec.empty_manifest

        return res

    def save(self, meta):
        with open(self.filename, "w") as f:
            json.dump(meta, f, indent = 2)

    @property
    def filename(self):
        return self.__base + ".meta"

class Manifest:
    def __init__(self, base_fname, spec):
        self.__base = base_fname
        self.__spec = spec
        self.__lockf = None
        self.__impl = None

    def __enter__(self):
        self.__lockf = bb.utils.lockfile(self.__base + ".lock")
        self.__impl  = ManifestImpl(self.__base, self.__spec)
        return self.__impl

    def __exit__(self, exc_type, exc_val, exc_tb):
        bb.utils.unlockfile(self.__lockf)

class NpmCache:
    def __init__(self, cache):
        self.__cache = cache

    @property
    def path(self):
        return self.__cache

    def run(self, type, key, fname):
        subprocess.run(['oe-npm-cache', self.__cache, type, key, fname],
                       check = True)

class NpmRegistry:
    def __init__(self, path, cache):
        self.__path = path
        self.__cache = NpmCache(cache + '/_cacache')
        bb.utils.mkdirhier(self.__path)
        bb.utils.mkdirhier(self.__cache.path)

    @staticmethod
    ## This function is critical and must match nodejs expectations
    def _meta_uri(spec):
        return REGISTRY + '/' + uri_quote(spec.name, safe = '@')

    @staticmethod
    ## Exact return value does not matter; just make it look like a
    ## usual registry url
    def _tarball_uri(spec):
        return '%s/%s/-/%s-%s.tgz' % (REGISTRY,
                                      uri_quote(spec.name, safe = '@'),
                                      uri_quote(spec.name, safe = '@/'),
                                      spec.version)

    def add_pkg(self, tarball, pkg_json):
        pkg_json = PackageJson(pkg_json)
        base = os.path.join(self.__path, pkg_json.base_filename())

        with Manifest(base, pkg_json) as manifest:
            meta = manifest.load()
            tarball_uri = self._tarball_uri(pkg_json)

            meta['versions'][pkg_json.version] = pkg_json.as_manifest_entry(tarball_uri)

            manifest.save(meta)

            ## Cache entries are a little bit dependent on the nodejs
            ## version; version specific cache implementation must
            ## mitigate differences
            self.__cache.run('meta', self._meta_uri(pkg_json), manifest.filename);
            self.__cache.run('tgz',  tarball_uri, tarball);